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.54 by greg, Thu May 29 16:26:22 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   */
# Line 11 | Line 8 | static char SCCSid[] = "$SunId$ SGI";
8   #include "rholo.h"
9   #include "random.h"
10   #include <signal.h>
14 #include <sys/types.h>
11   #include <sys/stat.h>
12  
13 + #ifndef FRAGWARN
14 + #define FRAGWARN        20              /* fragmentation for warning (%) */
15 + #endif
16   #ifndef MAXQTIME
17   #define MAXQTIME        5               /* target maximum seconds in queue */
18   #endif
19 +                                        /* manual cache flushing frequency */
20 + #ifndef RTFLUSH
21 + #if MAXQTIME
22 + #define RTFLUSH         (300/MAXQTIME*totqlen)  /* <= 5 minutes */
23 + #else
24 + #define RTFLUSH         (50*totqlen)            /* just guess */
25 + #endif
26 + #endif
27                          /* the following must be consistent with rholo.h */
28   int     NVARS = NRHVARS;                /* total number of variables */
29  
# Line 26 | Line 33 | char   *progname;              /* our program name */
33   char    *hdkfile;               /* holodeck file name */
34   char    froot[256];             /* root file name */
35  
29 int     nowarn = 0;             /* turn warnings off? */
30
36   int     ncprocs = 0;            /* desired number of compute processes */
37  
38   char    *outdev = NULL;         /* output device name */
39  
40   int     readinp = 0;            /* read commands from stdin */
41  
42 < int     force = 0;              /* allow overwrite of holodeck */
42 > int     force = 0;              /* allow overwrite of holodeck (-1 == read-only) */
43  
44   time_t  starttime;              /* time we got started */
45   time_t  endtime;                /* time we should end by */
46   time_t  reporttime;             /* time for next report */
47  
48 < long    maxdisk;                /* maximum file space (bytes) */
48 > off_t   maxdisk;                /* maximum file space (bytes) */
49  
50   int     rtargc = 1;             /* rtrace command */
51   char    *rtargv[128] = {"rtrace", NULL};
# Line 51 | Line 56 | long   nraysdone = 0L;         /* number of rays done */
56   long    npacksdone = 0L;        /* number of packets done */
57  
58   PACKET  *freepacks;             /* available packets */
59 < int     avgqlen;                /* average queue length when full */
59 > int     totqlen;                /* maximum queue length (number of packets) */
60  
61   char  *sigerr[NSIG];            /* signal error messages */
62  
63 + extern int      nowarn;         /* turn warnings off? */
64 +
65   extern time_t   time();
66  
67  
# Line 72 | Line 79 | char   *argv[];
79                          nowarn++;
80                          break;
81                  case 'f':                       /* force overwrite */
82 <                        force++;
82 >                        force = 1;
83                          break;
84 +                case 'r':                       /* read-only mode */
85 +                        force = -1;
86 +                        break;
87                  case 'i':                       /* read input from stdin */
88                          readinp++;
89                          break;
# Line 109 | Line 119 | char   *argv[];
119                  }
120                                                  /* check settings */
121          checkvalues();
122 <                                                /* load RIF if rtrace */
123 <        if (ncprocs)
114 <                getradfile();
122 >                                                /* load rad input file */
123 >        getradfile();
124  
125          if (hdlist[0] == NULL) {                /* create new holodeck */
126                  HDGRID  hdg[HDMAX];
127                                                          /* set defaults */
128                  setdefaults(hdg);
129 +                                                        /* check read-only */
130 +                if (force < 0)
131 +                        error(USER, "cannot create read-only holodeck");
132                                                          /* holodeck exists? */
133                  if (!force && access(hdkfile, R_OK|W_OK) == 0)
134                          error(USER,
# Line 134 | Line 146 | char   *argv[];
146          quit(0);
147   userr:
148          fprintf(stderr,
149 < "Usage: %s [-n nprocs][-o disp][-w][-f] output.hdk [control.hif|+|- [VAR=val ..]]\n",
149 > "Usage: %s [-n nprocs][-o disp][-w][-r|-f] output.hdk [control.hif|+|- [VAR=val ..]]\n",
150                          progname);
151          quit(1);
152   }
# Line 145 | Line 157 | int  signo;
157   {
158          static int  gotsig = 0;
159  
160 <        if (gotsig++)                   /* two signals and we're gone! */
160 >        if (gotsig > 1)                 /* we're going as fast as we can! */
161 >                return;
162 >        if (gotsig++) {                 /* two signals and we split */
163 >                hdsync(NULL, 0);        /* don't leave w/o saying goodbye */
164                  _exit(signo);
165 <
166 <        alarm(60);                      /* allow 60 seconds to clean up */
152 <        signal(SIGALRM, SIG_DFL);       /* make certain we do die */
165 >        }
166 >        alarm(300);                     /* allow 5 minutes to clean up */
167          eputs("signal - ");
168          eputs(sigerr[signo]);
169          eputs("\n");
# Line 176 | Line 190 | int    fd, mod;
190          if (fstat(fd, &stbuf) < 0)
191                  error(SYSTEM, "cannot stat open holodeck file");
192          mod &= stbuf.st_mode;           /* always more restrictive */
193 <        if (mod == stbuf.st_mode)
193 >        if (mod == (stbuf.st_mode & 0777))
194                  return(-1);             /* already set */
195                                          /* else change it */
196          if (fchmod(fd, mod) < 0) {
# Line 200 | Line 214 | initrholo()                    /* get our holodeck running */
214                  init_global();
215                                                  /* record disk space limit */
216          if (!vdef(DISKSPACE))
217 <                maxdisk = 0;
217 >                maxdisk = (1L<<(sizeof(off_t)*8-2)) - 1024;
218          else
219                  maxdisk = 1024.*1024.*vflt(DISKSPACE);
220                                                  /* set up memory cache */
221          if (outdev == NULL)
222 <                hdcachesize = 0;        /* manual flushing */
222 >                hdcachesize = 0;                /* manual flushing */
223          else if (vdef(CACHE))
224                  hdcachesize = 1024.*1024.*vflt(CACHE);
225                                                  /* open report file */
# Line 223 | Line 237 | initrholo()                    /* get our holodeck running */
237                  endtime = starttime + vflt(TIME)*3600. + .5;
238                                                  /* start rtrace */
239          if (ncprocs > 0) {
240 <                i = start_rtrace();
240 >                totqlen = i = start_rtrace();
241                  if (i < 1)
242                          error(USER, "cannot start rtrace process(es)");
243                  if (vdef(REPORT)) {             /* make first report */
# Line 234 | Line 248 | initrholo()                    /* get our holodeck running */
248                  freepacks = (PACKET *)bmalloc(i*sizeof(PACKET));
249                  if (freepacks == NULL)
250                          goto memerr;
237                if (!(avgqlen = i/nprocs))      /* record mean queue length */
238                        avgqlen = 1;
251                  freepacks[--i].nr = 0;
252                  freepacks[i].next = NULL;
253                  if (!vdef(OBSTRUCTIONS) || !vbool(OBSTRUCTIONS)) {
# Line 262 | Line 274 | initrholo()                    /* get our holodeck running */
274          sigdie(SIGXCPU, "CPU limit exceeded");
275          sigdie(SIGXFSZ, "File size exceeded");
276   #endif
277 <                                                /* protect holodeck file */
278 <        orig_mode = resfmode(hdlist[0]->fd, ncprocs>0 ? 0 : 0444);
277 >                                        /* protect holodeck file */
278 >        orig_mode = resfmode(hdlist[0]->fd, ncprocs>0&force>=0 ? 0 : 0444);
279          return;
280   memerr:
281          error(SYSTEM, "out of memory in initrholo");
# Line 272 | Line 284 | memerr:
284  
285   rholo()                         /* holodeck main loop */
286   {
287 +        static long     nextfragwarn = 100*(1L<<20);
288          static int      idle = 0;
289          PACKET  *pl = NULL, *plend;
290 +        off_t   fsiz;
291          int     pksiz;
292          register PACKET *p;
293          time_t  t;
# Line 287 | Line 301 | rholo()                                /* holodeck main loop */
301                          return(1);
302          } else if (idle)
303                  return(0);              /* all done */
304 <                                        /* check file size */
305 <        if (maxdisk > 0 && hdfilen(hdlist[0]->fd) >= maxdisk) {
304 >        fsiz = hdfilen(hdlist[0]->fd);  /* check file size */
305 >        if (maxdisk > 0 && fsiz >= maxdisk) {
306                  error(WARNING, "file limit exceeded");
307                  done_rtrace();
308                  return(1);      /* comes back */
309          }
310 + #if FRAGWARN
311 +        if (fsiz >= nextfragwarn &&
312 +                (fsiz-hdfiluse(hdlist[0]->fd,0))/(fsiz/100) > FRAGWARN) {
313 +                sprintf(errmsg, "holodeck file fragmentation is %.0f%%",
314 +                                100.*(fsiz-hdfiluse(hdlist[0]->fd,1))/fsiz);
315 +                error(WARNING, errmsg);
316 +                nextfragwarn = fsiz + (fsiz>>2);        /* decent interval */
317 +        }
318 + #endif
319          t = time(NULL);                 /* check time */
320          if (endtime > 0 && t >= endtime) {
321                  error(WARNING, "time limit exceeded");
# Line 302 | Line 325 | rholo()                                /* holodeck main loop */
325          if (reporttime > 0 && t >= reporttime)
326                  report(t);
327                                          /* figure out good packet size */
328 +        pksiz = RPACKSIZ;
329   #if MAXQTIME
330 <        pksiz = nraysdone*MAXQTIME/(avgqlen*(t - starttime + 1L));
331 <        if (pksiz < 1)
332 <                pksiz = 1;
333 <        else if (pksiz > RPACKSIZ)
330 >        if (!chunkycmp) {
331 >                pksiz = nraysdone*MAXQTIME/(totqlen*(t - starttime + 1L));
332 >                if (pksiz < 1) pksiz = 1;
333 >                else if (pksiz > RPACKSIZ) pksiz = RPACKSIZ;
334 >        }
335   #endif
311                pksiz = RPACKSIZ;
336          idle = 0;                       /* get packets to process */
337          while (freepacks != NULL) {
338                  p = freepacks; freepacks = p->next; p->next = NULL;
# Line 333 | Line 357 | register HDGRID        *gp;
357          extern char     *atos();
358          register int    i;
359          int     n;
360 <        double  len[3], maxlen, d;
360 >        double  len[3], d;
361          char    buf[64];
362  
363          if (!vdef(SECTION)) {
# Line 367 | Line 391 | register HDGRID        *gp;
391                                  &gp->xv[2][0], &gp->xv[2][1], &gp->xv[2][2],
392                                  &gp->grid[0], &gp->grid[1], &gp->grid[2]) < 12)
393                          badvalue(SECTION);
370                maxlen = 0.;
394                  for (i = 0; i < 3; i++)
395 <                        if ((len[i] = VLEN(gp->xv[i])) > maxlen)
396 <                                maxlen = len[i];
397 <                if (!vdef(GRID))
398 <                        d = 0.125*maxlen;
399 <                else if ((d = vflt(GRID)) <= FTINY)
395 >                        len[i] = VLEN(gp->xv[i]);
396 >                if (!vdef(GRID)) {
397 >                        d = 2/2e5*( len[0]*len[0]*(len[1]*len[1] +
398 >                                        len[2]*len[2] + 4*len[1]*len[2])
399 >                                + len[1]*len[1]*len[2]*(len[2] + 4*len[0])
400 >                                + 4*len[0]*len[1]*len[2]*len[2] );
401 >                        d = sqrt(sqrt(d));
402 >                } else if ((d = vflt(GRID)) <= FTINY)
403                          badvalue(GRID);
404                  for (i = 0; i < 3; i++)
405                          if (gp->grid[i] <= 0)
# Line 404 | Line 430 | HDGRID *gp;
430          putw(HOLOMAGIC, fp);            /* put magic number */
431          fd = dup(fileno(fp));
432          fclose(fp);                     /* flush and close stdio stream */
433 <        lastloc = lseek(fd, 0L, 2);
433 >        lastloc = lseek(fd, (off_t)0L, 2);
434          for (n = vdef(SECTION); n--; gp++) {    /* initialize each section */
435                  nextloc = 0L;
436                  write(fd, (char *)&nextloc, sizeof(nextloc));
# Line 412 | Line 438 | HDGRID *gp;
438                  if (!n)
439                          break;
440                  nextloc = hdfilen(fd);          /* write section pointer */
441 <                if (lseek(fd, (long)lastloc, 0) < 0)
441 >                if (lseek(fd, (off_t)lastloc, 0) < 0)
442                          error(SYSTEM,
443                                  "cannot seek on holodeck file in creatholo");
444                  write(fd, (char *)&nextloc, sizeof(nextloc));
445 <                lseek(fd, (long)(lastloc=nextloc), 0);
445 >                lseek(fd, (off_t)(lastloc=nextloc), 0);
446          }
447   }
448  
449  
450 + int
451   headline(s)                     /* process information header line */
452   char    *s;
453   {
# Line 434 | Line 461 | char   *s;
461                                          HOLOFMT, hdkfile, FMTSTR, fmt);
462                          error(USER, errmsg);
463                  }
464 <                return;
464 >                return(0);
465          }
466          for (cp = s; *cp; cp++)         /* take off any comments */
467                  if (*cp == '#') {
# Line 442 | Line 469 | char   *s;
469                          break;
470                  }
471          setvariable(s, matchvar);       /* don't flag errors */
472 +        return(0);
473   }
474  
475  
476   loadholo()                      /* start loading a holodeck from fname */
477   {
450        extern long     ftell();
478          FILE    *fp;
479          int     fd;
480          int     n;
481          int4    nextloc;
482 <                                        /* open holodeck file */
483 <        if ((fp = fopen(hdkfile, ncprocs>0 ? "r+" : "r")) == NULL) {
484 <                sprintf(errmsg, "cannot open \"%s\" for %s", hdkfile,
485 <                                ncprocs>0 ? "appending" : "reading");
486 <                error(SYSTEM, errmsg);
482 >        
483 >        if (ncprocs > 0 & force >= 0)
484 >                fp = fopen(hdkfile, "r+");
485 >        else
486 >                fp = NULL;
487 >        if (fp == NULL) {
488 >                if ((fp = fopen(hdkfile, "r")) == NULL) {
489 >                        sprintf(errmsg, "cannot open \"%s\"", hdkfile);
490 >                        error(SYSTEM, errmsg);
491 >                }
492 >                if (ncprocs > 0) {
493 >                        sprintf(errmsg,
494 >                        "\"%s\" opened read-only; new rays will be discarded",
495 >                                        hdkfile);
496 >                        error(WARNING, errmsg);
497 >                        force = -1;
498 >                }
499          }
500                                          /* load variables from header */
501          getheader(fp, headline, NULL);
# Line 470 | Line 509 | loadholo()                     /* start loading a holodeck from fname */
509          fd = dup(fileno(fp));
510          fclose(fp);                             /* done with stdio */
511          for (n = 0; nextloc > 0L; n++) {        /* initialize each section */
512 <                lseek(fd, (long)nextloc, 0);
512 >                lseek(fd, (off_t)nextloc, 0);
513                  read(fd, (char *)&nextloc, sizeof(nextloc));
514                  hdinit(fd, NULL);
515          }
# Line 491 | Line 530 | PACKET *pl;
530          while (pl != NULL) {
531                  p = pl; pl = p->next; p->next = NULL;
532                  if (p->nr > 0) {                /* add to holodeck */
533 <                        bcopy((char *)p->ra,
534 <                                (char *)hdnewrays(hdlist[p->hd],p->bi,p->nr),
533 >                        bcopy((void *)p->ra,
534 >                                (void *)hdnewrays(hdlist[p->hd],p->bi,p->nr),
535                                  p->nr*sizeof(RAYVAL));
536                          if (outdev != NULL)     /* display it */
537                                  disp_packet((PACKHEAD *)p);
538 <                        if (hdcachesize <= 0)   /* manual flushing */
538 >                        if (hdcachesize <= 0)
539                                  n2flush++;
540                          nraysdone += p->nr;
541                          npacksdone++;
# Line 505 | Line 544 | PACKET *pl;
544                  p->next = freepacks;            /* push onto free list */
545                  freepacks = p;
546          }
547 <        if (n2flush > 300/MAXQTIME*avgqlen*nprocs) {
548 <                hdflush(NULL);                  /* flush holodeck buffers */
547 >        if (n2flush >= RTFLUSH) {
548 >                if (outdev != NULL)
549 >                        hdsync(NULL, 1);
550 >                else
551 >                        hdflush(NULL);
552                  n2flush = 0;
553          }
554   }
# Line 535 | Line 577 | int    vc;
577   }
578  
579  
580 + void
581   eputs(s)                        /* put error message to stderr */
582   register char  *s;
583   {
# Line 554 | Line 597 | register char  *s;
597   }
598  
599  
600 < wputs(s)                        /* put warning string to stderr */
558 < char    *s;
559 < {
560 <        if (!nowarn)
561 <                eputs(s);
562 < }
563 <
564 <
600 > void
601   quit(ec)                        /* exit program gracefully */
602   int     ec;
603   {
# Line 570 | Line 606 | int    ec;
606          if (hdlist[0] != NULL) {        /* close holodeck */
607                  if (nprocs > 0)
608                          status = done_rtrace();         /* calls hdsync() */
609 <                if (ncprocs > 0 && vdef(REPORT)) {
610 <                        long    fsiz, fuse;
609 >                if (ncprocs > 0 & force >= 0 && vdef(REPORT)) {
610 >                        off_t   fsiz, fuse;
611                          fsiz = hdfilen(hdlist[0]->fd);
612                          fuse = hdfiluse(hdlist[0]->fd, 1);
613                          fprintf(stderr,

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines