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.34 by gregl, Sun Jan 4 18:36:43 1998 UTC vs.
Revision 3.63 by greg, Wed Oct 22 02:06:34 2003 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1997 Silicon Graphics, Inc. */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ SGI";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   * Radiance holodeck generation controller
6   */
7  
11 #include "rholo.h"
12 #include "random.h"
8   #include <signal.h>
14 #include <sys/types.h>
9   #include <sys/stat.h>
10 + #include <string.h>
11  
12 + #include "platform.h"
13 + #include "rholo.h"
14 + #include "random.h"
15 +
16 + #ifndef FRAGWARN
17 + #define FRAGWARN        20              /* fragmentation for warning (%) */
18 + #endif
19   #ifndef MAXQTIME
20   #define MAXQTIME        5               /* target maximum seconds in queue */
21   #endif
22 +                                        /* manual cache flushing frequency */
23 + #ifndef RTFLUSH
24 + #if MAXQTIME
25 + #define RTFLUSH         (300/MAXQTIME*totqlen)  /* <= 5 minutes */
26 + #else
27 + #define RTFLUSH         (50*totqlen)            /* just guess */
28 + #endif
29 + #endif
30                          /* the following must be consistent with rholo.h */
31   int     NVARS = NRHVARS;                /* total number of variables */
32  
# Line 26 | Line 36 | char   *progname;              /* our program name */
36   char    *hdkfile;               /* holodeck file name */
37   char    froot[256];             /* root file name */
38  
29 int     nowarn = 0;             /* turn warnings off? */
30
39   int     ncprocs = 0;            /* desired number of compute processes */
40  
41   char    *outdev = NULL;         /* output device name */
42  
43   int     readinp = 0;            /* read commands from stdin */
44  
45 < int     force = 0;              /* allow overwrite of holodeck */
45 > int     force = 0;              /* allow overwrite of holodeck (-1 == read-only) */
46  
47   time_t  starttime;              /* time we got started */
48   time_t  endtime;                /* time we should end by */
49   time_t  reporttime;             /* time for next report */
50  
51 < long    maxdisk;                /* maximum file space (bytes) */
51 > off_t   maxdisk;                /* maximum file space (bytes) */
52  
53   int     rtargc = 1;             /* rtrace command */
54   char    *rtargv[128] = {"rtrace", NULL};
# Line 51 | Line 59 | long   nraysdone = 0L;         /* number of rays done */
59   long    npacksdone = 0L;        /* number of packets done */
60  
61   PACKET  *freepacks;             /* available packets */
62 < int     avgqlen;                /* average queue length when full */
62 > int     totqlen;                /* maximum queue length (number of packets) */
63  
64   char  *sigerr[NSIG];            /* signal error messages */
65  
66 + extern int      nowarn;         /* turn warnings off? */
67 +
68   extern time_t   time();
69  
70  
# Line 72 | Line 82 | char   *argv[];
82                          nowarn++;
83                          break;
84                  case 'f':                       /* force overwrite */
85 <                        force++;
85 >                        force = 1;
86                          break;
87 +                case 'r':                       /* read-only mode */
88 +                        force = -1;
89 +                        break;
90                  case 'i':                       /* read input from stdin */
91                          readinp++;
92                          break;
# Line 109 | Line 122 | char   *argv[];
122                  }
123                                                  /* check settings */
124          checkvalues();
125 <                                                /* load RIF if rtrace */
126 <        if (ncprocs)
114 <                getradfile();
125 >                                                /* load rad input file */
126 >        getradfile();
127  
128          if (hdlist[0] == NULL) {                /* create new holodeck */
129                  HDGRID  hdg[HDMAX];
130                                                          /* set defaults */
131                  setdefaults(hdg);
132 +                                                        /* check read-only */
133 +                if (force < 0)
134 +                        error(USER, "cannot create read-only holodeck");
135                                                          /* holodeck exists? */
136                  if (!force && access(hdkfile, R_OK|W_OK) == 0)
137                          error(USER,
# Line 134 | Line 149 | char   *argv[];
149          quit(0);
150   userr:
151          fprintf(stderr,
152 < "Usage: %s [-n nprocs][-o disp][-w][-f] output.hdk [control.hif|+|- [VAR=val ..]]\n",
152 > "Usage: %s [-n nprocs][-o disp][-w][-r|-f] output.hdk [control.hif|+|- [VAR=val ..]]\n",
153                          progname);
154          quit(1);
155   }
156  
157  
158 + void
159   onsig(signo)                            /* fatal signal */
160   int  signo;
161   {
162          static int  gotsig = 0;
163  
164 <        if (gotsig++)                   /* two signals and we're gone! */
164 >        if (gotsig > 1)                 /* we're going as fast as we can! */
165 >                return;
166 >        if (gotsig++) {                 /* two signals and we split */
167 >                hdsync(NULL, 0);        /* don't leave w/o saying goodbye */
168                  _exit(signo);
169 <
170 <        alarm(60);                      /* allow 60 seconds to clean up */
152 <        signal(SIGALRM, SIG_DFL);       /* make certain we do die */
169 >        }
170 >        alarm(300);                     /* allow 5 minutes to clean up */
171          eputs("signal - ");
172          eputs(sigerr[signo]);
173          eputs("\n");
# Line 176 | Line 194 | int    fd, mod;
194          if (fstat(fd, &stbuf) < 0)
195                  error(SYSTEM, "cannot stat open holodeck file");
196          mod &= stbuf.st_mode;           /* always more restrictive */
197 <        if (mod == stbuf.st_mode)
197 >        if (mod == (stbuf.st_mode & 0777))
198                  return(-1);             /* already set */
199                                          /* else change it */
200          if (fchmod(fd, mod) < 0) {
# Line 200 | Line 218 | initrholo()                    /* get our holodeck running */
218                  init_global();
219                                                  /* record disk space limit */
220          if (!vdef(DISKSPACE))
221 <                maxdisk = 0;
221 >                maxdisk = ((off_t)1<<(sizeof(off_t)*8-2)) - 1024;
222          else
223                  maxdisk = 1024.*1024.*vflt(DISKSPACE);
224                                                  /* set up memory cache */
225          if (outdev == NULL)
226 <                hdcachesize = 0;        /* manual flushing */
226 >                hdcachesize = 0;                /* manual flushing */
227          else if (vdef(CACHE))
228                  hdcachesize = 1024.*1024.*vflt(CACHE);
229                                                  /* open report file */
# Line 223 | Line 241 | initrholo()                    /* get our holodeck running */
241                  endtime = starttime + vflt(TIME)*3600. + .5;
242                                                  /* start rtrace */
243          if (ncprocs > 0) {
244 <                i = start_rtrace();
244 >                totqlen = i = start_rtrace();
245                  if (i < 1)
246                          error(USER, "cannot start rtrace process(es)");
247                  if (vdef(REPORT)) {             /* make first report */
# Line 234 | Line 252 | initrholo()                    /* get our holodeck running */
252                  freepacks = (PACKET *)bmalloc(i*sizeof(PACKET));
253                  if (freepacks == NULL)
254                          goto memerr;
237                if (!(avgqlen = i/nprocs))      /* record mean queue length */
238                        avgqlen = 1;
255                  freepacks[--i].nr = 0;
256                  freepacks[i].next = NULL;
257                  if (!vdef(OBSTRUCTIONS) || !vbool(OBSTRUCTIONS)) {
# Line 254 | Line 270 | initrholo()                    /* get our holodeck running */
270          }
271                                          /* set up signal handling */
272          sigdie(SIGINT, "Interrupt");
257        sigdie(SIGHUP, "Hangup");
273          sigdie(SIGTERM, "Terminate");
274 + #ifdef SIGHUP
275 +        sigdie(SIGHUP, "Hangup");
276 + #endif
277 + #ifdef SIGPIPE
278          sigdie(SIGPIPE, "Broken pipe");
279 + #endif
280 + #ifdef SIGALRM
281          sigdie(SIGALRM, "Alarm clock");
282 + #endif
283   #ifdef  SIGXCPU
284          sigdie(SIGXCPU, "CPU limit exceeded");
285 + #endif
286 + #ifdef  SIGXFSZ
287          sigdie(SIGXFSZ, "File size exceeded");
288   #endif
289 <                                                /* protect holodeck file */
290 <        orig_mode = resfmode(hdlist[0]->fd, ncprocs>0 ? 0 : 0444);
289 >                                        /* protect holodeck file */
290 >        orig_mode = resfmode(hdlist[0]->fd, (ncprocs>0) & (force>=0) ? 0 : 0444);
291          return;
292   memerr:
293          error(SYSTEM, "out of memory in initrholo");
# Line 272 | Line 296 | memerr:
296  
297   rholo()                         /* holodeck main loop */
298   {
299 +        static long     nextfragwarn = 100*(1L<<20);
300          static int      idle = 0;
301          PACKET  *pl = NULL, *plend;
302 +        off_t   fsiz;
303          int     pksiz;
304          register PACKET *p;
305          time_t  t;
# Line 287 | Line 313 | rholo()                                /* holodeck main loop */
313                          return(1);
314          } else if (idle)
315                  return(0);              /* all done */
316 <                                        /* check file size */
317 <        if (maxdisk > 0 && hdfilen(hdlist[0]->fd) >= maxdisk) {
316 >        fsiz = hdfilen(hdlist[0]->fd);  /* check file size */
317 >        if (maxdisk > 0 && fsiz >= maxdisk) {
318                  error(WARNING, "file limit exceeded");
319                  done_rtrace();
320                  return(1);      /* comes back */
321          }
322 + #if FRAGWARN
323 +        if (fsiz >= nextfragwarn &&
324 +                (fsiz-hdfiluse(hdlist[0]->fd,0))/(fsiz/100) > FRAGWARN) {
325 +                sprintf(errmsg, "holodeck file fragmentation is %.0f%%",
326 +                                100.*(fsiz-hdfiluse(hdlist[0]->fd,1))/fsiz);
327 +                error(WARNING, errmsg);
328 +                nextfragwarn = fsiz + (fsiz>>2);        /* decent interval */
329 +        }
330 + #endif
331          t = time(NULL);                 /* check time */
332          if (endtime > 0 && t >= endtime) {
333                  error(WARNING, "time limit exceeded");
# Line 302 | Line 337 | rholo()                                /* holodeck main loop */
337          if (reporttime > 0 && t >= reporttime)
338                  report(t);
339                                          /* figure out good packet size */
340 +        pksiz = RPACKSIZ;
341   #if MAXQTIME
342 <        pksiz = nraysdone*MAXQTIME/(avgqlen*(t - starttime + 1L));
343 <        if (pksiz < 1)
344 <                pksiz = 1;
345 <        else if (pksiz > RPACKSIZ)
342 >        if (!chunkycmp) {
343 >                pksiz = nraysdone*MAXQTIME/(totqlen*(t - starttime + 1L));
344 >                if (pksiz < 1) pksiz = 1;
345 >                else if (pksiz > RPACKSIZ) pksiz = RPACKSIZ;
346 >        }
347   #endif
311                pksiz = RPACKSIZ;
348          idle = 0;                       /* get packets to process */
349          while (freepacks != NULL) {
350                  p = freepacks; freepacks = p->next; p->next = NULL;
# Line 333 | Line 369 | register HDGRID        *gp;
369          extern char     *atos();
370          register int    i;
371          int     n;
372 <        double  len[3], maxlen, d;
372 >        double  len[3], d;
373          char    buf[64];
374  
375          if (!vdef(SECTION)) {
# Line 367 | Line 403 | register HDGRID        *gp;
403                                  &gp->xv[2][0], &gp->xv[2][1], &gp->xv[2][2],
404                                  &gp->grid[0], &gp->grid[1], &gp->grid[2]) < 12)
405                          badvalue(SECTION);
370                maxlen = 0.;
406                  for (i = 0; i < 3; i++)
407 <                        if ((len[i] = VLEN(gp->xv[i])) > maxlen)
408 <                                maxlen = len[i];
409 <                if (!vdef(GRID))
410 <                        d = 0.125*maxlen;
411 <                else if ((d = vflt(GRID)) <= FTINY)
407 >                        len[i] = VLEN(gp->xv[i]);
408 >                if (!vdef(GRID)) {
409 >                        d = 2/2e5*( len[0]*len[0]*(len[1]*len[1] +
410 >                                        len[2]*len[2] + 4*len[1]*len[2])
411 >                                + len[1]*len[1]*len[2]*(len[2] + 4*len[0])
412 >                                + 4*len[0]*len[1]*len[2]*len[2] );
413 >                        d = sqrt(sqrt(d));
414 >                } else if ((d = vflt(GRID)) <= FTINY)
415                          badvalue(GRID);
416                  for (i = 0; i < 3; i++)
417                          if (gp->grid[i] <= 0)
# Line 386 | Line 424 | creatholo(gp)                  /* create a holodeck output file */
424   HDGRID  *gp;
425   {
426          extern char     VersionID[];
427 <        int4    lastloc, nextloc;
427 >        int32   lastloc, nextloc;
428          int     n;
429          int     fd;
430          FILE    *fp;
# Line 404 | Line 442 | HDGRID *gp;
442          putw(HOLOMAGIC, fp);            /* put magic number */
443          fd = dup(fileno(fp));
444          fclose(fp);                     /* flush and close stdio stream */
445 <        lastloc = lseek(fd, 0L, 2);
445 >        lastloc = lseek(fd, (off_t)0, SEEK_END);
446          for (n = vdef(SECTION); n--; gp++) {    /* initialize each section */
447                  nextloc = 0L;
448                  write(fd, (char *)&nextloc, sizeof(nextloc));
# Line 412 | Line 450 | HDGRID *gp;
450                  if (!n)
451                          break;
452                  nextloc = hdfilen(fd);          /* write section pointer */
453 <                if (lseek(fd, (long)lastloc, 0) < 0)
453 >                if (lseek(fd, (off_t)lastloc, SEEK_SET) < 0)
454                          error(SYSTEM,
455                                  "cannot seek on holodeck file in creatholo");
456                  write(fd, (char *)&nextloc, sizeof(nextloc));
457 <                lseek(fd, (long)(lastloc=nextloc), 0);
457 >                lseek(fd, (off_t)(lastloc=nextloc), SEEK_SET);
458          }
459   }
460  
461  
462 + int
463   headline(s)                     /* process information header line */
464   char    *s;
465   {
# Line 434 | Line 473 | char   *s;
473                                          HOLOFMT, hdkfile, FMTSTR, fmt);
474                          error(USER, errmsg);
475                  }
476 <                return;
476 >                return(0);
477          }
478          for (cp = s; *cp; cp++)         /* take off any comments */
479                  if (*cp == '#') {
# Line 442 | Line 481 | char   *s;
481                          break;
482                  }
483          setvariable(s, matchvar);       /* don't flag errors */
484 +        return(0);
485   }
486  
487  
488   loadholo()                      /* start loading a holodeck from fname */
489   {
450        extern long     ftell();
490          FILE    *fp;
491          int     fd;
492          int     n;
493 <        int4    nextloc;
494 <                                        /* open holodeck file */
495 <        if ((fp = fopen(hdkfile, ncprocs>0 ? "r+" : "r")) == NULL) {
496 <                sprintf(errmsg, "cannot open \"%s\" for %s", hdkfile,
497 <                                ncprocs>0 ? "appending" : "reading");
498 <                error(SYSTEM, errmsg);
493 >        int32   nextloc;
494 >        
495 >        if ((ncprocs > 0) & (force >= 0))
496 >                fp = fopen(hdkfile, "r+");
497 >        else
498 >                fp = NULL;
499 >        if (fp == NULL) {
500 >                if ((fp = fopen(hdkfile, "r")) == NULL) {
501 >                        sprintf(errmsg, "cannot open \"%s\"", hdkfile);
502 >                        error(SYSTEM, errmsg);
503 >                }
504 >                if (ncprocs > 0) {
505 >                        sprintf(errmsg,
506 >                        "\"%s\" opened read-only; new rays will be discarded",
507 >                                        hdkfile);
508 >                        error(WARNING, errmsg);
509 >                        force = -1;
510 >                }
511          }
512                                          /* load variables from header */
513          getheader(fp, headline, NULL);
# Line 470 | Line 521 | loadholo()                     /* start loading a holodeck from fname */
521          fd = dup(fileno(fp));
522          fclose(fp);                             /* done with stdio */
523          for (n = 0; nextloc > 0L; n++) {        /* initialize each section */
524 <                lseek(fd, (long)nextloc, 0);
524 >                lseek(fd, (off_t)nextloc, SEEK_SET);
525                  read(fd, (char *)&nextloc, sizeof(nextloc));
526                  hdinit(fd, NULL);
527          }
# Line 491 | Line 542 | PACKET *pl;
542          while (pl != NULL) {
543                  p = pl; pl = p->next; p->next = NULL;
544                  if (p->nr > 0) {                /* add to holodeck */
545 <                        bcopy((char *)p->ra,
546 <                                (char *)hdnewrays(hdlist[p->hd],p->bi,p->nr),
545 >                        memcpy( (void *)hdnewrays(hdlist[p->hd],p->bi,p->nr),
546 >                                (void *)p->ra,
547                                  p->nr*sizeof(RAYVAL));
548                          if (outdev != NULL)     /* display it */
549                                  disp_packet((PACKHEAD *)p);
550 <                        if (hdcachesize <= 0)   /* manual flushing */
550 >                        if (hdcachesize <= 0)
551                                  n2flush++;
552                          nraysdone += p->nr;
553                          npacksdone++;
# Line 505 | Line 556 | PACKET *pl;
556                  p->next = freepacks;            /* push onto free list */
557                  freepacks = p;
558          }
559 <        if (n2flush > 300/MAXQTIME*avgqlen*nprocs) {
560 <                hdflush(NULL);                  /* flush holodeck buffers */
559 >        if (n2flush >= RTFLUSH) {
560 >                if (outdev != NULL)
561 >                        hdsync(NULL, 1);
562 >                else
563 >                        hdflush(NULL);
564                  n2flush = 0;
565          }
566   }
# Line 517 | Line 571 | register char  *rn, *fn;
571   {
572          char    *tp, *dp;
573  
574 <        for (tp = NULL, dp = rn; *rn = *fn++; rn++)
574 >        for (tp = NULL, dp = rn; (*rn = *fn++); rn++) {
575                  if (*rn == '/')
576                          dp = rn;
577                  else if (*rn == '.')
578                          tp = rn;
579 +        }
580          if (tp != NULL && tp > dp)
581                  *tp = '\0';
582   }
# Line 535 | Line 590 | int    vc;
590   }
591  
592  
593 + void
594   eputs(s)                        /* put error message to stderr */
595   register char  *s;
596   {
# Line 554 | Line 610 | register char  *s;
610   }
611  
612  
613 < wputs(s)                        /* put warning string to stderr */
558 < char    *s;
559 < {
560 <        if (!nowarn)
561 <                eputs(s);
562 < }
563 <
564 <
613 > void
614   quit(ec)                        /* exit program gracefully */
615   int     ec;
616   {
# Line 570 | Line 619 | int    ec;
619          if (hdlist[0] != NULL) {        /* close holodeck */
620                  if (nprocs > 0)
621                          status = done_rtrace();         /* calls hdsync() */
622 <                if (ncprocs > 0 && vdef(REPORT)) {
623 <                        long    fsiz, fuse;
622 >                if ((ncprocs > 0) & (force >= 0) && vdef(REPORT)) {
623 >                        off_t   fsiz, fuse;
624                          fsiz = hdfilen(hdlist[0]->fd);
625                          fuse = hdfiluse(hdlist[0]->fd, 1);
626                          fprintf(stderr,

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines