ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rholo.c
(Generate patch)

Comparing ray/src/hd/rholo.c (file contents):
Revision 3.1 by gregl, Fri Oct 31 10:23:29 1997 UTC vs.
Revision 3.14 by gregl, Mon Dec 1 16:34:21 1997 UTC

# Line 9 | Line 9 | static char SCCSid[] = "$SunId$ SGI";
9   */
10  
11   #include "rholo.h"
12 + #include "random.h"
13   #include "paths.h"
14 + #include <signal.h>
15   #include <sys/types.h>
16 + #include <sys/stat.h>
17  
18                          /* the following must be consistent with rholo.h */
19   int     NVARS = NRHVARS;                /* total number of variables */
# Line 33 | Line 36 | time_t starttime;              /* time we got started */
36   time_t  endtime;                /* time we should end by */
37   time_t  reporttime;             /* time for next report */
38  
39 + long    maxdisk;                /* maximum file space (bytes) */
40 +
41   int     rtargc = 1;             /* rtrace command */
42   char    *rtargv[128] = {"rtrace", NULL};
43  
44 + int     orig_mode = -1;         /* original file mode (-1 if unchanged) */
45 +
46   long    nraysdone = 0L;         /* number of rays done */
47   long    npacksdone = 0L;        /* number of packets done */
48  
49   PACKET  *freepacks;             /* available packets */
50  
51 + char  *sigerr[NSIG];            /* signal error messages */
52 +
53   extern time_t   time();
54  
55  
# Line 51 | Line 60 | char   *argv[];
60          HDGRID  hdg;
61          int     i;
62          int     force = 0;
63 <
63 >                                                /* mark start time */
64 >        starttime = time(NULL);
65 >        initurand(10240);                       /* initialize urand */
66          progname = argv[0];                     /* get arguments */
67          for (i = 1; i < argc && argv[i][0] == '-'; i++)
68                  switch (argv[i][1]) {
# Line 102 | Line 113 | char   *argv[];
113                                  "holodeck file exists -- use -f to overwrite");
114                                                          /* create holodeck */
115                  creatholo(&hdg);
116 <        } else                                  /* else load holodeck */
116 >        } else {                                /* else load holodeck */
117                  loadholo();
118 +                if (vdef(RIF))                          /* load RIF if any */
119 +                        getradfile(vval(RIF));
120 +        }
121                                                  /* initialize */
122          initrholo();
123                                                  /* run */
# Line 119 | Line 133 | userr:
133   }
134  
135  
136 + onsig(signo)                            /* fatal signal */
137 + int  signo;
138 + {
139 +        static int  gotsig = 0;
140 +
141 +        if (gotsig++)                   /* two signals and we're gone! */
142 +                _exit(signo);
143 +
144 +        alarm(30);                      /* allow 30 seconds to clean up */
145 +        signal(SIGALRM, SIG_DFL);       /* make certain we do die */
146 +        eputs("signal - ");
147 +        eputs(sigerr[signo]);
148 +        eputs("\n");
149 +        quit(3);
150 + }
151 +
152 +
153 + sigdie(signo, msg)                      /* set fatal signal */
154 + int  signo;
155 + char  *msg;
156 + {
157 +        if (signal(signo, onsig) == SIG_IGN)
158 +                signal(signo, SIG_IGN);
159 +        sigerr[signo] = msg;
160 + }
161 +
162 +
163 + int
164 + resfmode(fd, mod)               /* restrict open file access mode */
165 + int     fd, mod;
166 + {
167 +        struct stat     stbuf;
168 +                                        /* get original mode */
169 +        if (fstat(fd, &stbuf) < 0)
170 +                error(SYSTEM, "cannot stat open holodeck file");
171 +        mod &= stbuf.st_mode;           /* always more restrictive */
172 +        if (mod == stbuf.st_mode)
173 +                return(-1);             /* already set */
174 +                                        /* else change it */
175 +        if (fchmod(fd, mod) < 0) {
176 +                error(WARNING, "cannot change holodeck file access mode");
177 +                return(-1);
178 +        }
179 +        return(stbuf.st_mode);          /* return original mode */
180 + }
181 +
182 +
183   initrholo()                     /* get our holodeck running */
184   {
185          extern int      global_packet();
186          register int    i;
187 <                                                /* check output device */
188 <        if (outdev != NULL)
189 <                open_display(outdev);
187 >
188 >        if (outdev != NULL)                     /* open output device */
189 >                disp_open(outdev);
190          else if (ncprocs > 0)                   /* else use global ray feed */
191                  init_global();
192 <                                                /* record the time */
193 <        starttime = time(NULL);
192 >                                                /* record disk space limit */
193 >        if (!vdef(DISKSPACE))
194 >                maxdisk = 0;
195 >        else
196 >                maxdisk = 1024.*1024.*vflt(DISKSPACE);
197 >                                                /* record end time */
198          if (!vdef(TIME) || vflt(TIME) <= FTINY)
199                  endtime = 0;
200          else
201                  endtime = starttime + vflt(TIME)*3600.;
202                                                  /* set up memory cache */
203 <        hdcachesize = 1024.*1024.*vflt(CACHE);
203 >        if (outdev == NULL)
204 >                hdcachesize = 0;        /* manual flushing */
205 >        else if (vdef(CACHE))
206 >                hdcachesize = 1024.*1024.*vflt(CACHE);
207                                                  /* open report file */
208          if (vdef(REPORT)) {
209                  register char   *s = sskip2(vval(REPORT), 1);
# Line 171 | Line 239 | initrholo()                    /* get our holodeck running */
239                          freepacks[i].next = &freepacks[i+1];
240                  }
241          }
242 +                                        /* set up signal handling */
243 +        sigdie(SIGINT, "Interrupt");
244 +        sigdie(SIGHUP, "Hangup");
245 +        sigdie(SIGTERM, "Terminate");
246 +        sigdie(SIGPIPE, "Broken pipe");
247 +        sigdie(SIGALRM, "Alarm clock");
248 + #ifdef  SIGXCPU
249 +        sigdie(SIGXCPU, "CPU limit exceeded");
250 +        sigdie(SIGXFSZ, "File size exceeded");
251 + #endif
252 +                                                /* protect holodeck file */
253 +        orig_mode = resfmode(hdlist[0]->fd, ncprocs>0 ? 0 : 0444);
254          return;
255   memerr:
256          error(SYSTEM, "out of memory in initrholo");
# Line 184 | Line 264 | rholo()                                /* holodeck main loop */
264          register PACKET *p;
265          time_t  t;
266          long    l;
267 <                                        /* check display */
268 <        if (outdev != NULL && !disp_check(idle))
269 <                return(0);
267 >
268 >        if (outdev != NULL)             /* check display */
269 >                if (!disp_check(idle))
270 >                        return(0);
271                                          /* display only? */
272          if (ncprocs <= 0)
273                  return(1);
274                                          /* check file size */
275 <        if (l = 1024.*1024.*vflt(DISKSPACE) > 0 &&
276 <                        hdfiluse(hdlist[0]->fd, 0) + hdmemuse(0) >= l)
275 >        if (maxdisk > 0 && hdfilen(hdlist[0]->fd) >= maxdisk) {
276 >                error(WARNING, "file limit exceeded");
277                  return(0);
278 +        }
279                                          /* check time */
280 <        if (endtime > 0 || vdef(REPORT))
280 >        if (endtime > 0 || reporttime > 0)
281                  t = time(NULL);
282 <        if (endtime > 0 && t >= endtime)
282 >        if (endtime > 0 && t >= endtime) {
283 >                error(WARNING, "time limit exceeded");
284                  return(0);
285 <        if (vdef(REPORT) && t >= reporttime)
285 >        }
286 >        if (reporttime > 0 && t >= reporttime)
287                  report(t);
288                                          /* get packets to process */
289          while (freepacks != NULL) {
# Line 227 | Line 311 | time_t t;
311   {
312          if (t == 0)
313                  t = time(NULL);
314 <        fprintf(stderr, "%s: %ld packets done (%ld rays) after %.2f hours\n",
314 >        fprintf(stderr, "%s: %ld packets (%ld rays) done after %.2f hours\n",
315                          progname, npacksdone, nraysdone, (t-starttime)/3600.);
316 +        fflush(stderr);
317 +        if (vdef(REPORT))
318 +                reporttime = t + (time_t)(vflt(REPORT)*60.+.5);
319   }
320  
321  
# Line 260 | Line 347 | register HDGRID        *gp;
347                  if ((len[i] = VLEN(gp->xv[i])) > maxlen)
348                          maxlen = len[i];
349          if (!vdef(GRID)) {
350 <                sprintf(buf, "%.4f", maxlen/16.);
350 >                sprintf(buf, "%.4f", maxlen/8.);
351                  vval(GRID) = savqstr(buf);
352                  vdef(GRID)++;
353          }
# Line 280 | Line 367 | register HDGRID        *gp;
367                  sprintf(vval(OCTREE), "%s.oct", froot);
368                  vdef(OCTREE)++;
369          }
283        if (!vdef(DISKSPACE)) {
284                sprintf(errmsg,
285                        "no %s setting, assuming 100 Mbytes available",
286                                vnam(DISKSPACE));
287                error(WARNING, errmsg);
288                vval(DISKSPACE) = "100";
289                vdef(DISKSPACE)++;
290        }
291        if (!vdef(CACHE)) {
292                sprintf(errmsg,
293                        "no %s setting, assuming 10 Mbytes available",
294                                vnam(CACHE));
295                error(WARNING, errmsg);
296                vval(CACHE) = "10";
297                vdef(CACHE)++;
298        }
370          if (!vdef(OBSTRUCTIONS)) {
371                  vval(OBSTRUCTIONS) = "T";
372                  vdef(OBSTRUCTIONS)++;
373          }
374 +        if (!vdef(VDIST)) {
375 +                vval(VDIST) = "F";
376 +                vdef(VDIST)++;
377 +        }
378          if (!vdef(OCCUPANCY)) {
379                  vval(OCCUPANCY) = "U";
380                  vdef(OCCUPANCY)++;
# Line 313 | Line 388 | register HDGRID        *gp;
388   creatholo(gp)                   /* create a holodeck output file */
389   HDGRID  *gp;
390   {
391 +        extern char     VersionID[];
392          long    endloc = 0;
393 +        int     fd;
394          FILE    *fp;
395                                          /* open & truncate file */
396          if ((fp = fopen(hdkfile, "w+")) == NULL) {
# Line 322 | Line 399 | HDGRID *gp;
399          }
400                                          /* write information header */
401          newheader("RADIANCE", fp);
402 +        fprintf(fp, "SOFTWARE= %s\n", VersionID);
403          printvars(fp);
404          fputformat(HOLOFMT, fp);
405          fputc('\n', fp);
406          putw(HOLOMAGIC, fp);            /* put magic number & terminus */
407          fwrite(&endloc, sizeof(long), 1, fp);
408 <        fflush(fp);                     /* flush buffer */
409 <        initholo(fileno(fp), gp);       /* allocate and initialize index */
410 <                        /* we're dropping fp here.... */
408 >        fd = dup(fileno(fp));
409 >        fclose(fp);                     /* flush and close stdio stream */
410 >        hdinit(fd, gp);                 /* allocate and initialize index */
411   }
412  
413  
# Line 340 | Line 418 | char   *s;
418          register char   *cp;
419          char    fmt[32];
420  
421 <        if (headidval(fmt, s)) {
421 >        if (formatval(fmt, s)) {
422                  if (strcmp(fmt, HOLOFMT)) {
423                          sprintf(errmsg, "%s file \"%s\" has %s%s",
424                                          HOLOFMT, hdkfile, FMTSTR, fmt);
# Line 359 | Line 437 | char   *s;
437  
438   loadholo()                      /* start loading a holodeck from fname */
439   {
440 +        extern long     ftell();
441          FILE    *fp;
442 <        long    endloc;
443 <                                        /* open input file */
444 <        if ((fp = fopen(hdkfile, "r+")) == NULL) {
445 <                sprintf(errmsg, "cannot open \"%s\" for appending", hdkfile);
442 >        int     fd;
443 >        long    fpos;
444 >                                        /* open holodeck file */
445 >        if ((fp = fopen(hdkfile, ncprocs>0 ? "r+" : "r")) == NULL) {
446 >                sprintf(errmsg, "cannot open \"%s\" for %s", hdkfile,
447 >                                ncprocs>0 ? "appending" : "reading");
448                  error(SYSTEM, errmsg);
449          }
450                                          /* load variables from header */
# Line 374 | Line 455 | loadholo()                     /* start loading a holodeck from fname */
455                                  hdkfile);
456                  error(USER, errmsg);
457          }
458 <        fread(&endloc, sizeof(long), 1, fp);
459 <        if (endloc != 0)
458 >        fread(&fpos, sizeof(long), 1, fp);
459 >        if (fpos != 0)
460                  error(WARNING, "ignoring multiple sections in holodeck file");
461 <        fseek(fp, 0L, 1);                       /* align system file pointer */
462 <        initholo(fileno(fp), NULL);             /* allocate and load index */
463 <                        /* we're dropping fp here.... */
461 >        fpos = ftell(fp);                       /* get stdio position */
462 >        fd = dup(fileno(fp));
463 >        fclose(fp);                             /* done with stdio */
464 >        lseek(fd, fpos, 0);                     /* align system file pointer */
465 >        hdinit(fd, NULL);                       /* allocate and load index */
466   }
467  
468  
469   done_packets(pl)                /* handle finished packets */
470   PACKET  *pl;
471   {
472 +        static int      n2flush = 0;
473          register PACKET *p;
474  
475          while (pl != NULL) {
# Line 395 | Line 479 | PACKET *pl;
479                                  (char *)hdnewrays(hdlist[p->hd],p->bi,p->nr),
480                                  p->nr*sizeof(RAYVAL));
481                          if (outdev != NULL)     /* display it */
482 <                                disp_packet(p);
482 >                                disp_packet((PACKHEAD *)p);
483 >                        if (hdcachesize <= 0)   /* manual flushing */
484 >                                n2flush += p->nr;
485 >                        nraysdone += p->nr;
486 >                        npacksdone++;
487                  }
400                nraysdone += p->nr;
401                npacksdone++;
488                  p->nr = 0;                      /* push onto free list */
489                  p->next = freepacks;
490                  freepacks = p;
491          }
492 +        if (n2flush > 512*RPACKSIZ*ncprocs) {
493 +                hdflush(NULL);                  /* flush holodeck buffers */
494 +                n2flush = 0;
495 +        }
496   }
497  
498  
499   getradfile(rfargs)              /* run rad and get needed variables */
500   char    *rfargs;
501   {
502 <        static short    mvar[] = {VIEW,OCTREE,EXPOSURE,REPORT,-1};
502 >        static short    mvar[] = {OCTREE,EXPOSURE,-1};
503          static char     tf1[] = TEMPLATE;
504          char    tf2[64];
505          char    combuf[256];
506 +        char    *pippt;
507          register int    i;
508          register char   *cp;
509                                          /* create rad command */
# Line 422 | Line 513 | char   *rfargs;
513                  "rad -v 0 -s -e -w %s OPTFILE=%s | egrep '^[ \t]*(NOMATCH",
514                          rfargs, tf1);
515          cp = combuf;
516 <        while (*cp) cp++;               /* match unset variables */
516 >        while (*cp){
517 >                if (*cp == '|') pippt = cp;
518 >                cp++;
519 >        }                               /* match unset variables */
520          for (i = 0; mvar[i] >= 0; i++)
521                  if (!vdef(mvar[i])) {
522                          *cp++ = '|';
523                          strcpy(cp, vnam(mvar[i]));
524                          while (*cp) cp++;
525 +                        pippt = NULL;
526                  }
527 <        sprintf(cp, ")[ \t]*=' > %s", tf2);
527 >        if (pippt != NULL)
528 >                strcpy(pippt, "> /dev/null");   /* nothing to match */
529 >        else
530 >                sprintf(cp, ")[ \t]*=' > %s", tf2);
531          if (system(combuf)) {
532                  error(SYSTEM, "cannot execute rad command");
533                  unlink(tf2);                    /* clean up */
534                  unlink(tf1);
535                  quit(1);
536          }
537 <        loadvars(tf2);                  /* load variables */
537 >        if (pippt == NULL) {
538 >                loadvars(tf2);                  /* load variables */
539 >                unlink(tf2);
540 >        }
541          rtargc += wordfile(rtargv+rtargc, tf1); /* get rtrace options */
542 <        unlink(tf2);                    /* clean up */
442 <        unlink(tf1);
542 >        unlink(tf1);                    /* clean up */
543   }
544  
545  
# Line 506 | Line 606 | int    ec;
606                          if (vdef(REPORT)) {
607                                  long    fsiz, fuse;
608                                  report(0);
609 <                                fsiz = lseek(hdlist[0]->fd, 0L, 2);
609 >                                fsiz = hdfilen(hdlist[0]->fd);
610                                  fuse = hdfiluse(hdlist[0]->fd, 1);
611                                  fprintf(stderr,
612 <                        "%s: %.1f Mbyte holodeck file, %.2f%% fragmentation\n",
612 >                        "%s: %.1f Mbyte holodeck file, %.1f%% fragmentation\n",
613                                                  hdkfile, fsiz/(1024.*1024.),
614                                                  100.*(fsiz-fuse)/fsiz);
615                          }
616                  } else
617                          hdflush(NULL);
618          }
619 +        if (orig_mode >= 0)             /* reset holodeck access mode */
620 +                fchmod(hdlist[0]->fd, orig_mode);
621 +        if (outdev != NULL)             /* close display */
622 +                disp_close();
623          exit(ec ? ec : status);         /* exit */
624   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines