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.2 by gregl, Fri Oct 31 11:44:09 1997 UTC vs.
Revision 3.17 by gregl, Mon Dec 8 18:51:15 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 23 | Line 26 | char   froot[MAXPATH];         /* root file name */
26  
27   int     nowarn = 0;             /* turn warnings off? */
28  
26 double  expval = 1.;            /* global exposure value */
27
29   int     ncprocs = 0;            /* desired number of compute processes */
30  
31   char    *outdev = NULL;         /* output device name */
32  
33 + int     readinp = 0;            /* read commands from stdin */
34 +
35   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 61 | Line 72 | char   *argv[];
72                  case 'f':                       /* force overwrite */
73                          force++;
74                          break;
75 +                case 'i':                       /* read input from stdin */
76 +                        readinp++;
77 +                        break;
78                  case 'n':                       /* compute processes */
79                          if (i >= argc-2)
80                                  goto userr;
# Line 102 | Line 116 | char   *argv[];
116                                  "holodeck file exists -- use -f to overwrite");
117                                                          /* create holodeck */
118                  creatholo(&hdg);
119 <        } else                                  /* else load holodeck */
119 >        } else {                                /* else load holodeck */
120                  loadholo();
121 +                                                        /* check settings */
122 +                checkvalues();
123 +                                                        /* load RIF if any */
124 +                if (vdef(RIF))
125 +                        getradfile(vval(RIF));
126 +                                                        /* set defaults */
127 +                setdefaults(NULL);
128 +        }
129                                                  /* initialize */
130          initrholo();
131 <                                                /* run */
131 >                                                /* main loop */
132          while (rholo())
133                  ;
134                                                  /* done */
# Line 119 | Line 141 | userr:
141   }
142  
143  
144 + onsig(signo)                            /* fatal signal */
145 + int  signo;
146 + {
147 +        static int  gotsig = 0;
148 +
149 +        if (gotsig++)                   /* two signals and we're gone! */
150 +                _exit(signo);
151 +
152 +        alarm(30);                      /* allow 30 seconds to clean up */
153 +        signal(SIGALRM, SIG_DFL);       /* make certain we do die */
154 +        eputs("signal - ");
155 +        eputs(sigerr[signo]);
156 +        eputs("\n");
157 +        quit(3);
158 + }
159 +
160 +
161 + sigdie(signo, msg)                      /* set fatal signal */
162 + int  signo;
163 + char  *msg;
164 + {
165 +        if (signal(signo, onsig) == SIG_IGN)
166 +                signal(signo, SIG_IGN);
167 +        sigerr[signo] = msg;
168 + }
169 +
170 +
171 + int
172 + resfmode(fd, mod)               /* restrict open file access mode */
173 + int     fd, mod;
174 + {
175 +        struct stat     stbuf;
176 +                                        /* get original mode */
177 +        if (fstat(fd, &stbuf) < 0)
178 +                error(SYSTEM, "cannot stat open holodeck file");
179 +        mod &= stbuf.st_mode;           /* always more restrictive */
180 +        if (mod == stbuf.st_mode)
181 +                return(-1);             /* already set */
182 +                                        /* else change it */
183 +        if (fchmod(fd, mod) < 0) {
184 +                error(WARNING, "cannot change holodeck file access mode");
185 +                return(-1);
186 +        }
187 +        return(stbuf.st_mode);          /* return original mode */
188 + }
189 +
190 +
191   initrholo()                     /* get our holodeck running */
192   {
193          extern int      global_packet();
194          register int    i;
195 <                                                /* check output device */
196 <        if (outdev != NULL)
197 <                open_display(outdev);
195 >                                                /* close holodeck on exec() */
196 >        fcntl(hdlist[0]->fd, F_SETFD, FD_CLOEXEC);
197 >
198 >        if (outdev != NULL)                     /* open output device */
199 >                disp_open(outdev);
200          else if (ncprocs > 0)                   /* else use global ray feed */
201                  init_global();
202 <                                                /* record the time */
203 <        starttime = time(NULL);
202 >                                                /* record disk space limit */
203 >        if (!vdef(DISKSPACE))
204 >                maxdisk = 0;
205 >        else
206 >                maxdisk = 1024.*1024.*vflt(DISKSPACE);
207 >                                                /* record end time */
208          if (!vdef(TIME) || vflt(TIME) <= FTINY)
209                  endtime = 0;
210          else
211                  endtime = starttime + vflt(TIME)*3600.;
212                                                  /* set up memory cache */
213 <        hdcachesize = 1024.*1024.*vflt(CACHE);
213 >        if (outdev == NULL)
214 >                hdcachesize = 0;        /* manual flushing */
215 >        else if (vdef(CACHE))
216 >                hdcachesize = 1024.*1024.*vflt(CACHE);
217                                                  /* open report file */
218          if (vdef(REPORT)) {
219                  register char   *s = sskip2(vval(REPORT), 1);
# Line 171 | Line 249 | initrholo()                    /* get our holodeck running */
249                          freepacks[i].next = &freepacks[i+1];
250                  }
251          }
252 +                                        /* set up signal handling */
253 +        sigdie(SIGINT, "Interrupt");
254 +        sigdie(SIGHUP, "Hangup");
255 +        sigdie(SIGTERM, "Terminate");
256 +        sigdie(SIGPIPE, "Broken pipe");
257 +        sigdie(SIGALRM, "Alarm clock");
258 + #ifdef  SIGXCPU
259 +        sigdie(SIGXCPU, "CPU limit exceeded");
260 +        sigdie(SIGXFSZ, "File size exceeded");
261 + #endif
262 +                                                /* protect holodeck file */
263 +        orig_mode = resfmode(hdlist[0]->fd, ncprocs>0 ? 0 : 0444);
264          return;
265   memerr:
266          error(SYSTEM, "out of memory in initrholo");
# Line 184 | Line 274 | rholo()                                /* holodeck main loop */
274          register PACKET *p;
275          time_t  t;
276          long    l;
277 <                                        /* check display */
278 <        if (outdev != NULL && !disp_check(idle))
279 <                return(0);
277 >
278 >        if (outdev != NULL)             /* check display */
279 >                if (!disp_check(idle))
280 >                        return(0);
281                                          /* display only? */
282          if (ncprocs <= 0)
283                  return(1);
284                                          /* check file size */
285 <        if ((l = 1024.*1024.*vflt(DISKSPACE)) > 0 &&
286 <                        hdfiluse(hdlist[0]->fd, 0) + hdmemuse(0) >= l)
285 >        if (maxdisk > 0 && hdfilen(hdlist[0]->fd) >= maxdisk) {
286 >                error(WARNING, "file limit exceeded");
287                  return(0);
288 +        }
289                                          /* check time */
290 <        if (endtime > 0 || vdef(REPORT))
290 >        if (endtime > 0 || reporttime > 0)
291                  t = time(NULL);
292 <        if (endtime > 0 && t >= endtime)
292 >        if (endtime > 0 && t >= endtime) {
293 >                error(WARNING, "time limit exceeded");
294                  return(0);
295 <        if (vdef(REPORT) && t >= reporttime)
295 >        }
296 >        if (reporttime > 0 && t >= reporttime)
297                  report(t);
298                                          /* get packets to process */
299          while (freepacks != NULL) {
# Line 230 | Line 324 | time_t t;
324          fprintf(stderr, "%s: %ld packets (%ld rays) done after %.2f hours\n",
325                          progname, npacksdone, nraysdone, (t-starttime)/3600.);
326          fflush(stderr);
327 <        reporttime = t + (time_t)(vflt(REPORT)*60.);
327 >        if (vdef(REPORT))
328 >                reporttime = t + (time_t)(vflt(REPORT)*60.+.5);
329   }
330  
331  
# Line 250 | Line 345 | register HDGRID        *gp;
345                  sprintf(errmsg, "ignoring all but first %s", vnam(SECTION));
346                  error(WARNING, errmsg);
347          }
253        if (sscanf(vval(SECTION),
254                        "%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf",
255                        &gp->orig[0], &gp->orig[1], &gp->orig[2],
256                        &gp->xv[0][0], &gp->xv[0][1], &gp->xv[0][2],
257                        &gp->xv[1][0], &gp->xv[1][1], &gp->xv[1][2],
258                        &gp->xv[2][0], &gp->xv[2][1], &gp->xv[2][2]) != 12)
259                badvalue(SECTION);
260        maxlen = 0.;
261        for (i = 0; i < 3; i++)
262                if ((len[i] = VLEN(gp->xv[i])) > maxlen)
263                        maxlen = len[i];
264        if (!vdef(GRID)) {
265                sprintf(buf, "%.4f", maxlen/8.);
266                vval(GRID) = savqstr(buf);
267                vdef(GRID)++;
268        }
269        if ((d = vflt(GRID)) <= FTINY)
270                badvalue(GRID);
271        for (i = 0; i < 3; i++)
272                gp->grid[i] = len[i]/d + (1.-FTINY);
273        if (!vdef(EXPOSURE)) {
274                sprintf(errmsg, "%s must be defined", vnam(EXPOSURE));
275                error(USER, errmsg);
276        }
277        expval = vval(EXPOSURE)[0] == '-' || vval(EXPOSURE)[0] == '+' ?
278                        pow(2., vflt(EXPOSURE)) : vflt(EXPOSURE);
348          if (!vdef(OCTREE)) {
349                  if ((vval(OCTREE) = bmalloc(strlen(froot)+5)) == NULL)
350                          error(SYSTEM, "out of memory");
351                  sprintf(vval(OCTREE), "%s.oct", froot);
352                  vdef(OCTREE)++;
353          }
285        if (!vdef(DISKSPACE)) {
286                sprintf(errmsg,
287                        "no %s setting, assuming 100 Mbytes available",
288                                vnam(DISKSPACE));
289                error(WARNING, errmsg);
290                vval(DISKSPACE) = "100";
291                vdef(DISKSPACE)++;
292        }
293        if (!vdef(CACHE)) {
294                sprintf(errmsg,
295                        "no %s setting, assuming 10 Mbytes available",
296                                vnam(CACHE));
297                error(WARNING, errmsg);
298                vval(CACHE) = "10";
299                vdef(CACHE)++;
300        }
354          if (!vdef(OBSTRUCTIONS)) {
355                  vval(OBSTRUCTIONS) = "T";
356                  vdef(OBSTRUCTIONS)++;
357          }
358 +        if (!vdef(VDIST)) {
359 +                vval(VDIST) = "F";
360 +                vdef(VDIST)++;
361 +        }
362          if (!vdef(OCCUPANCY)) {
363                  vval(OCCUPANCY) = "U";
364                  vdef(OCCUPANCY)++;
# Line 309 | Line 366 | register HDGRID        *gp;
366                                  /* append rendering options */
367          if (vdef(RENDER))
368                  rtargc += wordstring(rtargv+rtargc, vval(RENDER));
369 +        
370 +        if (gp == NULL)         /* already initialized? */
371 +                return;
372 +                                /* set grid parameters */
373 +        gp->grid[0] = gp->grid[1] = gp->grid[2] = 0;
374 +        if (sscanf(vval(SECTION),
375 +                "%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %hd %hd %hd",
376 +                        &gp->orig[0], &gp->orig[1], &gp->orig[2],
377 +                        &gp->xv[0][0], &gp->xv[0][1], &gp->xv[0][2],
378 +                        &gp->xv[1][0], &gp->xv[1][1], &gp->xv[1][2],
379 +                        &gp->xv[2][0], &gp->xv[2][1], &gp->xv[2][2],
380 +                        &gp->grid[0], &gp->grid[1], &gp->grid[2]) < 12)
381 +                badvalue(SECTION);
382 +        maxlen = 0.;
383 +        for (i = 0; i < 3; i++)
384 +                if ((len[i] = VLEN(gp->xv[i])) > maxlen)
385 +                        maxlen = len[i];
386 +        if (!vdef(GRID)) {
387 +                sprintf(buf, "%.4f", maxlen/8.);
388 +                vval(GRID) = savqstr(buf);
389 +                vdef(GRID)++;
390 +        }
391 +        if ((d = vflt(GRID)) <= FTINY)
392 +                badvalue(GRID);
393 +        for (i = 0; i < 3; i++)
394 +                if (gp->grid[i] <= 0)
395 +                        gp->grid[i] = len[i]/d + (1.-FTINY);
396   }
397  
398  
399   creatholo(gp)                   /* create a holodeck output file */
400   HDGRID  *gp;
401   {
402 +        extern char     VersionID[];
403          long    endloc = 0;
404 +        int     fd;
405          FILE    *fp;
406                                          /* open & truncate file */
407          if ((fp = fopen(hdkfile, "w+")) == NULL) {
# Line 324 | Line 410 | HDGRID *gp;
410          }
411                                          /* write information header */
412          newheader("RADIANCE", fp);
413 +        fprintf(fp, "SOFTWARE= %s\n", VersionID);
414          printvars(fp);
415          fputformat(HOLOFMT, fp);
416          fputc('\n', fp);
417          putw(HOLOMAGIC, fp);            /* put magic number & terminus */
418          fwrite(&endloc, sizeof(long), 1, fp);
419 <        fflush(fp);                     /* flush buffer */
420 <        hdinit(fileno(fp), gp);         /* allocate and initialize index */
421 <                        /* we're dropping fp here.... */
419 >        fd = dup(fileno(fp));
420 >        fclose(fp);                     /* flush and close stdio stream */
421 >        hdinit(fd, gp);                 /* allocate and initialize index */
422   }
423  
424  
# Line 361 | Line 448 | char   *s;
448  
449   loadholo()                      /* start loading a holodeck from fname */
450   {
451 +        extern long     ftell();
452          FILE    *fp;
453 <        long    endloc;
454 <                                        /* open input file */
455 <        if ((fp = fopen(hdkfile, "r+")) == NULL) {
456 <                sprintf(errmsg, "cannot open \"%s\" for appending", hdkfile);
453 >        int     fd;
454 >        long    fpos;
455 >                                        /* open holodeck file */
456 >        if ((fp = fopen(hdkfile, ncprocs>0 ? "r+" : "r")) == NULL) {
457 >                sprintf(errmsg, "cannot open \"%s\" for %s", hdkfile,
458 >                                ncprocs>0 ? "appending" : "reading");
459                  error(SYSTEM, errmsg);
460          }
461                                          /* load variables from header */
# Line 376 | Line 466 | loadholo()                     /* start loading a holodeck from fname */
466                                  hdkfile);
467                  error(USER, errmsg);
468          }
469 <        fread(&endloc, sizeof(long), 1, fp);
470 <        if (endloc != 0)
469 >        fread(&fpos, sizeof(long), 1, fp);
470 >        if (fpos != 0)
471                  error(WARNING, "ignoring multiple sections in holodeck file");
472 <        fseek(fp, 0L, 1);                       /* align system file pointer */
473 <        hdinit(fileno(fp), NULL);               /* allocate and load index */
474 <                        /* we're dropping fp here.... */
472 >        fpos = ftell(fp);                       /* get stdio position */
473 >        fd = dup(fileno(fp));
474 >        fclose(fp);                             /* done with stdio */
475 >        lseek(fd, fpos, 0);                     /* align system file pointer */
476 >        hdinit(fd, NULL);                       /* allocate and load index */
477   }
478  
479  
480   done_packets(pl)                /* handle finished packets */
481   PACKET  *pl;
482   {
483 +        static int      n2flush = 0;
484          register PACKET *p;
485  
486          while (pl != NULL) {
# Line 397 | Line 490 | PACKET *pl;
490                                  (char *)hdnewrays(hdlist[p->hd],p->bi,p->nr),
491                                  p->nr*sizeof(RAYVAL));
492                          if (outdev != NULL)     /* display it */
493 <                                disp_packet(p);
493 >                                disp_packet((PACKHEAD *)p);
494 >                        if (hdcachesize <= 0)   /* manual flushing */
495 >                                n2flush += p->nr;
496 >                        nraysdone += p->nr;
497 >                        npacksdone++;
498                  }
402                nraysdone += p->nr;
403                npacksdone++;
499                  p->nr = 0;                      /* push onto free list */
500                  p->next = freepacks;
501                  freepacks = p;
502          }
503 +        if (n2flush > 512*RPACKSIZ*ncprocs) {
504 +                hdflush(NULL);                  /* flush holodeck buffers */
505 +                n2flush = 0;
506 +        }
507   }
508  
509  
510   getradfile(rfargs)              /* run rad and get needed variables */
511   char    *rfargs;
512   {
513 <        static short    mvar[] = {VIEW,OCTREE,EXPOSURE,-1};
513 >        static short    mvar[] = {OCTREE,-1};
514          static char     tf1[] = TEMPLATE;
515          char    tf2[64];
516          char    combuf[256];
517 +        char    *pippt;
518          register int    i;
519          register char   *cp;
520                                          /* create rad command */
# Line 424 | Line 524 | char   *rfargs;
524                  "rad -v 0 -s -e -w %s OPTFILE=%s | egrep '^[ \t]*(NOMATCH",
525                          rfargs, tf1);
526          cp = combuf;
527 <        while (*cp) cp++;               /* match unset variables */
527 >        while (*cp){
528 >                if (*cp == '|') pippt = cp;
529 >                cp++;
530 >        }                               /* match unset variables */
531          for (i = 0; mvar[i] >= 0; i++)
532                  if (!vdef(mvar[i])) {
533                          *cp++ = '|';
534                          strcpy(cp, vnam(mvar[i]));
535                          while (*cp) cp++;
536 +                        pippt = NULL;
537                  }
538 <        sprintf(cp, ")[ \t]*=' > %s", tf2);
538 >        if (pippt != NULL)
539 >                strcpy(pippt, "> /dev/null");   /* nothing to match */
540 >        else
541 >                sprintf(cp, ")[ \t]*=' > %s", tf2);
542          if (system(combuf)) {
543                  error(SYSTEM, "cannot execute rad command");
544                  unlink(tf2);                    /* clean up */
545                  unlink(tf1);
546                  quit(1);
547          }
548 <        loadvars(tf2);                  /* load variables */
548 >        if (pippt == NULL) {
549 >                loadvars(tf2);                  /* load variables */
550 >                unlink(tf2);
551 >        }
552          rtargc += wordfile(rtargv+rtargc, tf1); /* get rtrace options */
553 <        unlink(tf2);                    /* clean up */
444 <        unlink(tf1);
553 >        unlink(tf1);                    /* clean up */
554   }
555  
556  
# Line 508 | Line 617 | int    ec;
617                          if (vdef(REPORT)) {
618                                  long    fsiz, fuse;
619                                  report(0);
620 <                                fsiz = lseek(hdlist[0]->fd, 0L, 2);
620 >                                fsiz = hdfilen(hdlist[0]->fd);
621                                  fuse = hdfiluse(hdlist[0]->fd, 1);
622                                  fprintf(stderr,
623 <                        "%s: %.1f Mbyte holodeck file, %.2f%% fragmentation\n",
623 >                        "%s: %.1f Mbyte holodeck file, %.1f%% fragmentation\n",
624                                                  hdkfile, fsiz/(1024.*1024.),
625                                                  100.*(fsiz-fuse)/fsiz);
626                          }
627                  } else
628                          hdflush(NULL);
629          }
630 +        if (orig_mode >= 0)             /* reset holodeck access mode */
631 +                fchmod(hdlist[0]->fd, orig_mode);
632 +        if (outdev != NULL)             /* close display */
633 +                disp_close();
634          exit(ec ? ec : status);         /* exit */
635   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines