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.38 by gwlarson, Wed Jan 28 12:32:50 1998 UTC vs.
Revision 3.66 by greg, Thu Sep 9 00:25:59 2004 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1998 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  
8 < #include "rholo.h"
9 < #include "random.h"
8 > #include <stdio.h>
9 > #include <time.h>
10   #include <signal.h>
14 #include <sys/types.h>
11   #include <sys/stat.h>
12 + #include <string.h>
13  
14 + #include "platform.h"
15 + #include "rterror.h"
16 + #include "resolu.h"
17 + #include "rholo.h"
18 + #include "random.h"
19 +
20 + #ifndef FRAGWARN
21 + #define FRAGWARN        20              /* fragmentation for warning (%) */
22 + #endif
23   #ifndef MAXQTIME
24   #define MAXQTIME        5               /* target maximum seconds in queue */
25   #endif
26 +                                        /* manual cache flushing frequency */
27 + #ifndef RTFLUSH
28 + #if MAXQTIME
29 + #define RTFLUSH         (300/MAXQTIME*totqlen)  /* <= 5 minutes */
30 + #else
31 + #define RTFLUSH         (50*totqlen)            /* just guess */
32 + #endif
33 + #endif
34                          /* the following must be consistent with rholo.h */
35   int     NVARS = NRHVARS;                /* total number of variables */
36  
# Line 26 | Line 40 | char   *progname;              /* our program name */
40   char    *hdkfile;               /* holodeck file name */
41   char    froot[256];             /* root file name */
42  
29 int     nowarn = 0;             /* turn warnings off? */
30
43   int     ncprocs = 0;            /* desired number of compute processes */
44  
45   char    *outdev = NULL;         /* output device name */
46  
47   int     readinp = 0;            /* read commands from stdin */
48  
49 < int     force = 0;              /* allow overwrite of holodeck */
49 > int     force = 0;              /* allow overwrite of holodeck (-1 == read-only) */
50  
51   time_t  starttime;              /* time we got started */
52   time_t  endtime;                /* time we should end by */
53   time_t  reporttime;             /* time for next report */
54  
55 < long    maxdisk;                /* maximum file space (bytes) */
55 > off_t   maxdisk;                /* maximum file space (bytes) */
56  
57   int     rtargc = 1;             /* rtrace command */
58   char    *rtargv[128] = {"rtrace", NULL};
# Line 51 | Line 63 | long   nraysdone = 0L;         /* number of rays done */
63   long    npacksdone = 0L;        /* number of packets done */
64  
65   PACKET  *freepacks;             /* available packets */
66 < int     totqlen;                /* maximum queue length when full */
66 > int     totqlen;                /* maximum queue length (number of packets) */
67  
68   char  *sigerr[NSIG];            /* signal error messages */
69  
70 < extern time_t   time();
70 > extern int      nowarn;         /* turn warnings off? */
71  
72 + static void onsig(int  signo);
73 + static void sigdie(int  signo, char  *msg);
74 + static int resfmode(int fd, int mod);
75 + static void initrholo(void);
76 + static int rholo(void);
77 + static void setdefaults(HDGRID  *gp);
78 + static void creatholo(HDGRID    *gp);
79 + static gethfunc headline;
80 + static void loadholo(void);
81 + static void rootname(char       *rn, char       *fn);
82 + static void badvalue(int        vc);
83  
84 < main(argc, argv)
85 < int     argc;
86 < char    *argv[];
84 >
85 > int
86 > main(
87 >        int     argc,
88 >        char    *argv[]
89 > )
90   {
91          int     i;
92  
# Line 72 | Line 98 | char   *argv[];
98                          nowarn++;
99                          break;
100                  case 'f':                       /* force overwrite */
101 <                        force++;
101 >                        force = 1;
102                          break;
103 +                case 'r':                       /* read-only mode */
104 +                        force = -1;
105 +                        break;
106                  case 'i':                       /* read input from stdin */
107                          readinp++;
108                          break;
# Line 116 | Line 145 | char   *argv[];
145                  HDGRID  hdg[HDMAX];
146                                                          /* set defaults */
147                  setdefaults(hdg);
148 +                                                        /* check read-only */
149 +                if (force < 0)
150 +                        error(USER, "cannot create read-only holodeck");
151                                                          /* holodeck exists? */
152                  if (!force && access(hdkfile, R_OK|W_OK) == 0)
153                          error(USER,
# Line 133 | Line 165 | char   *argv[];
165          quit(0);
166   userr:
167          fprintf(stderr,
168 < "Usage: %s [-n nprocs][-o disp][-w][-f] output.hdk [control.hif|+|- [VAR=val ..]]\n",
168 > "Usage: %s [-n nprocs][-o disp][-w][-r|-f] output.hdk [control.hif|+|- [VAR=val ..]]\n",
169                          progname);
170          quit(1);
171 +        return 1; /* pro forma return */
172   }
173  
174  
175 < onsig(signo)                            /* fatal signal */
176 < int  signo;
175 > static void
176 > onsig(                          /* fatal signal */
177 >        int  signo
178 > )
179   {
180          static int  gotsig = 0;
181  
182 <        if (gotsig++)                   /* two signals and we're gone! */
182 >        if (gotsig > 1)                 /* we're going as fast as we can! */
183 >                return;
184 >        if (gotsig++) {                 /* two signals and we split */
185 >                hdsync(NULL, 0);        /* don't leave w/o saying goodbye */
186                  _exit(signo);
187 <
188 <        alarm(60);                      /* allow 60 seconds to clean up */
151 <        signal(SIGALRM, SIG_DFL);       /* make certain we do die */
187 >        }
188 >        alarm(300);                     /* allow 5 minutes to clean up */
189          eputs("signal - ");
190          eputs(sigerr[signo]);
191          eputs("\n");
# Line 156 | Line 193 | int  signo;
193   }
194  
195  
196 < sigdie(signo, msg)                      /* set fatal signal */
197 < int  signo;
198 < char  *msg;
196 > static void
197 > sigdie(                 /* set fatal signal */
198 >        int  signo,
199 >        char  *msg
200 > )
201   {
202          if (signal(signo, onsig) == SIG_IGN)
203                  signal(signo, SIG_IGN);
# Line 166 | Line 205 | char  *msg;
205   }
206  
207  
208 < int
209 < resfmode(fd, mod)               /* restrict open file access mode */
210 < int     fd, mod;
208 > static int
209 > resfmode(               /* restrict open file access mode */
210 >        int     fd,
211 >        int     mod
212 > )
213   {
214          struct stat     stbuf;
215                                          /* get original mode */
# Line 186 | Line 227 | int    fd, mod;
227   }
228  
229  
230 < initrholo()                     /* get our holodeck running */
230 > static void
231 > initrholo(void)                 /* get our holodeck running */
232   {
233          extern int      global_packet();
234          register int    i;
# Line 199 | Line 241 | initrholo()                    /* get our holodeck running */
241                  init_global();
242                                                  /* record disk space limit */
243          if (!vdef(DISKSPACE))
244 <                maxdisk = 0;
244 >                maxdisk = ((off_t)1<<(sizeof(off_t)*8-2)) - 1024;
245          else
246                  maxdisk = 1024.*1024.*vflt(DISKSPACE);
247                                                  /* set up memory cache */
248          if (outdev == NULL)
249 <                hdcachesize = 0;        /* manual flushing */
249 >                hdcachesize = 0;                /* manual flushing */
250          else if (vdef(CACHE))
251                  hdcachesize = 1024.*1024.*vflt(CACHE);
252                                                  /* open report file */
# Line 251 | Line 293 | initrholo()                    /* get our holodeck running */
293          }
294                                          /* set up signal handling */
295          sigdie(SIGINT, "Interrupt");
254        sigdie(SIGHUP, "Hangup");
296          sigdie(SIGTERM, "Terminate");
297 + #ifdef SIGHUP
298 +        sigdie(SIGHUP, "Hangup");
299 + #endif
300 + #ifdef SIGPIPE
301          sigdie(SIGPIPE, "Broken pipe");
302 + #endif
303 + #ifdef SIGALRM
304          sigdie(SIGALRM, "Alarm clock");
305 + #endif
306   #ifdef  SIGXCPU
307          sigdie(SIGXCPU, "CPU limit exceeded");
308 + #endif
309 + #ifdef  SIGXFSZ
310          sigdie(SIGXFSZ, "File size exceeded");
311   #endif
312 <                                                /* protect holodeck file */
313 <        orig_mode = resfmode(hdlist[0]->fd, ncprocs>0 ? 0 : 0444);
312 >                                        /* protect holodeck file */
313 >        orig_mode = resfmode(hdlist[0]->fd, (ncprocs>0) & (force>=0) ? 0 : 0444);
314          return;
315   memerr:
316          error(SYSTEM, "out of memory in initrholo");
317   }
318  
319  
320 < rholo()                         /* holodeck main loop */
320 > static int
321 > rholo(void)                             /* holodeck main loop */
322   {
323 +        static long     nextfragwarn = 100*(1L<<20);
324          static int      idle = 0;
325          PACKET  *pl = NULL, *plend;
326 +        off_t   fsiz;
327          int     pksiz;
328          register PACKET *p;
329          time_t  t;
# Line 284 | Line 337 | rholo()                                /* holodeck main loop */
337                          return(1);
338          } else if (idle)
339                  return(0);              /* all done */
340 <                                        /* check file size */
341 <        if (maxdisk > 0 && hdfilen(hdlist[0]->fd) >= maxdisk) {
340 >        fsiz = hdfilen(hdlist[0]->fd);  /* check file size */
341 >        if (maxdisk > 0 && fsiz >= maxdisk) {
342                  error(WARNING, "file limit exceeded");
343                  done_rtrace();
344                  return(1);      /* comes back */
345          }
346 + #if FRAGWARN
347 +        if (fsiz >= nextfragwarn &&
348 +                    (fsiz-hdfiluse(hdlist[0]->fd,0))/(fsiz/100) > FRAGWARN) {
349 +                double  pctfrag = 100.*(fsiz-hdfiluse(hdlist[0]->fd,1))/fsiz;
350 +                if (pctfrag >= (double)FRAGWARN) {
351 +                        sprintf(errmsg, "holodeck file fragmentation is %.0f%%",
352 +                                        pctfrag);
353 +                        error(WARNING, errmsg);
354 +                        nextfragwarn = fsiz + (fsiz>>2);
355 +                }
356 +        }
357 + #endif
358          t = time(NULL);                 /* check time */
359          if (endtime > 0 && t >= endtime) {
360                  error(WARNING, "time limit exceeded");
# Line 299 | Line 364 | rholo()                                /* holodeck main loop */
364          if (reporttime > 0 && t >= reporttime)
365                  report(t);
366                                          /* figure out good packet size */
367 +        pksiz = RPACKSIZ;
368   #if MAXQTIME
369 <        pksiz = nraysdone*MAXQTIME/(totqlen*(t - starttime + 1L));
370 <        if (pksiz < 1)
371 <                pksiz = 1;
372 <        else if (pksiz > RPACKSIZ)
369 >        if (!chunkycmp) {
370 >                pksiz = nraysdone*MAXQTIME/(totqlen*(t - starttime + 1L));
371 >                if (pksiz < 1) pksiz = 1;
372 >                else if (pksiz > RPACKSIZ) pksiz = RPACKSIZ;
373 >        }
374   #endif
308                pksiz = RPACKSIZ;
375          idle = 0;                       /* get packets to process */
376          while (freepacks != NULL) {
377                  p = freepacks; freepacks = p->next; p->next = NULL;
# Line 324 | Line 390 | rholo()                                /* holodeck main loop */
390   }
391  
392  
393 < setdefaults(gp)                 /* set default values */
394 < register HDGRID *gp;
393 > static void
394 > setdefaults(                    /* set default values */
395 >        register HDGRID *gp
396 > )
397   {
398          extern char     *atos();
399          register int    i;
400          int     n;
401          double  len[3], d;
334        char    buf[64];
402  
403          if (!vdef(SECTION)) {
404                  sprintf(errmsg, "%s must be defined", vnam(SECTION));
# Line 367 | Line 434 | register HDGRID        *gp;
434                  for (i = 0; i < 3; i++)
435                          len[i] = VLEN(gp->xv[i]);
436                  if (!vdef(GRID)) {
437 <                        d = 2/5e5*( len[0]*len[0]*(len[1]*len[1] +
437 >                        d = 2/2e5*( len[0]*len[0]*(len[1]*len[1] +
438                                          len[2]*len[2] + 4*len[1]*len[2])
439                                  + len[1]*len[1]*len[2]*(len[2] + 4*len[0])
440                                  + 4*len[0]*len[1]*len[2]*len[2] );
# Line 381 | Line 448 | register HDGRID        *gp;
448   }
449  
450  
451 < creatholo(gp)                   /* create a holodeck output file */
452 < HDGRID  *gp;
451 > static void
452 > creatholo(                      /* create a holodeck output file */
453 >        HDGRID  *gp
454 > )
455   {
456          extern char     VersionID[];
457 <        int4    lastloc, nextloc;
457 >        int32   lastloc, nextloc;
458          int     n;
459          int     fd;
460          FILE    *fp;
# Line 403 | Line 472 | HDGRID *gp;
472          putw(HOLOMAGIC, fp);            /* put magic number */
473          fd = dup(fileno(fp));
474          fclose(fp);                     /* flush and close stdio stream */
475 <        lastloc = lseek(fd, 0L, 2);
475 >        lastloc = lseek(fd, (off_t)0, SEEK_END);
476          for (n = vdef(SECTION); n--; gp++) {    /* initialize each section */
477                  nextloc = 0L;
478                  write(fd, (char *)&nextloc, sizeof(nextloc));
# Line 411 | Line 480 | HDGRID *gp;
480                  if (!n)
481                          break;
482                  nextloc = hdfilen(fd);          /* write section pointer */
483 <                if (lseek(fd, (long)lastloc, 0) < 0)
483 >                if (lseek(fd, (off_t)lastloc, SEEK_SET) < 0)
484                          error(SYSTEM,
485                                  "cannot seek on holodeck file in creatholo");
486                  write(fd, (char *)&nextloc, sizeof(nextloc));
487 <                lseek(fd, (long)(lastloc=nextloc), 0);
487 >                lseek(fd, (off_t)(lastloc=nextloc), SEEK_SET);
488          }
489   }
490  
491  
492 < headline(s)                     /* process information header line */
493 < char    *s;
492 > static int
493 > headline(                       /* process information header line */
494 >        char    *s,
495 >        void    *p
496 > )
497   {
498          extern char     FMTSTR[];
499          register char   *cp;
# Line 433 | Line 505 | char   *s;
505                                          HOLOFMT, hdkfile, FMTSTR, fmt);
506                          error(USER, errmsg);
507                  }
508 <                return;
508 >                return(0);
509          }
510          for (cp = s; *cp; cp++)         /* take off any comments */
511                  if (*cp == '#') {
# Line 441 | Line 513 | char   *s;
513                          break;
514                  }
515          setvariable(s, matchvar);       /* don't flag errors */
516 +        return(0);
517   }
518  
519  
520 < loadholo()                      /* start loading a holodeck from fname */
520 > static void
521 > loadholo(void)                  /* start loading a holodeck from fname */
522   {
449        extern long     ftell();
523          FILE    *fp;
524          int     fd;
525          int     n;
526 <        int4    nextloc;
527 <                                        /* open holodeck file */
528 <        if ((fp = fopen(hdkfile, ncprocs>0 ? "r+" : "r")) == NULL) {
529 <                sprintf(errmsg, "cannot open \"%s\" for %s", hdkfile,
530 <                                ncprocs>0 ? "appending" : "reading");
531 <                error(SYSTEM, errmsg);
526 >        int32   nextloc;
527 >        
528 >        if ((ncprocs > 0) & (force >= 0))
529 >                fp = fopen(hdkfile, "r+");
530 >        else
531 >                fp = NULL;
532 >        if (fp == NULL) {
533 >                if ((fp = fopen(hdkfile, "r")) == NULL) {
534 >                        sprintf(errmsg, "cannot open \"%s\"", hdkfile);
535 >                        error(SYSTEM, errmsg);
536 >                }
537 >                if (ncprocs > 0) {
538 >                        sprintf(errmsg,
539 >                        "\"%s\" opened read-only; new rays will be discarded",
540 >                                        hdkfile);
541 >                        error(WARNING, errmsg);
542 >                        force = -1;
543 >                }
544          }
545                                          /* load variables from header */
546          getheader(fp, headline, NULL);
# Line 469 | Line 554 | loadholo()                     /* start loading a holodeck from fname */
554          fd = dup(fileno(fp));
555          fclose(fp);                             /* done with stdio */
556          for (n = 0; nextloc > 0L; n++) {        /* initialize each section */
557 <                lseek(fd, (long)nextloc, 0);
557 >                lseek(fd, (off_t)nextloc, SEEK_SET);
558                  read(fd, (char *)&nextloc, sizeof(nextloc));
559                  hdinit(fd, NULL);
560          }
# Line 481 | Line 566 | loadholo()                     /* start loading a holodeck from fname */
566   }
567  
568  
569 < done_packets(pl)                /* handle finished packets */
570 < PACKET  *pl;
569 > extern void
570 > done_packets(           /* handle finished packets */
571 >        PACKET  *pl
572 > )
573   {
574          static int      n2flush = 0;
575          register PACKET *p;
# Line 490 | Line 577 | PACKET *pl;
577          while (pl != NULL) {
578                  p = pl; pl = p->next; p->next = NULL;
579                  if (p->nr > 0) {                /* add to holodeck */
580 <                        bcopy((char *)p->ra,
581 <                                (char *)hdnewrays(hdlist[p->hd],p->bi,p->nr),
580 >                        memcpy( (void *)hdnewrays(hdlist[p->hd],p->bi,p->nr),
581 >                                (void *)p->ra,
582                                  p->nr*sizeof(RAYVAL));
583                          if (outdev != NULL)     /* display it */
584                                  disp_packet((PACKHEAD *)p);
585 <                        if (hdcachesize <= 0)   /* manual flushing */
585 >                        if (hdcachesize <= 0)
586                                  n2flush++;
587                          nraysdone += p->nr;
588                          npacksdone++;
# Line 504 | Line 591 | PACKET *pl;
591                  p->next = freepacks;            /* push onto free list */
592                  freepacks = p;
593          }
594 <        if (n2flush > 300/MAXQTIME*totqlen) {
595 <                hdflush(NULL);                  /* flush holodeck buffers */
594 >        if (n2flush >= RTFLUSH) {
595 >                if (outdev != NULL)
596 >                        hdsync(NULL, 1);
597 >                else
598 >                        hdflush(NULL);
599                  n2flush = 0;
600          }
601   }
602  
603  
604 < rootname(rn, fn)                /* remove tail from end of fn */
605 < register char   *rn, *fn;
604 > static void
605 > rootname(               /* remove tail from end of fn */
606 >        register char   *rn,
607 >        register char   *fn
608 > )
609   {
610          char    *tp, *dp;
611  
612 <        for (tp = NULL, dp = rn; *rn = *fn++; rn++)
612 >        for (tp = NULL, dp = rn; (*rn = *fn++); rn++) {
613                  if (*rn == '/')
614                          dp = rn;
615                  else if (*rn == '.')
616                          tp = rn;
617 +        }
618          if (tp != NULL && tp > dp)
619                  *tp = '\0';
620   }
621  
622  
623 < badvalue(vc)                    /* report bad variable value and exit */
624 < int     vc;
623 > static void
624 > badvalue(                       /* report bad variable value and exit */
625 >        int     vc
626 > )
627   {
628          sprintf(errmsg, "bad value for variable '%s'", vnam(vc));
629          error(USER, errmsg);
630   }
631  
632  
633 + void
634   eputs(s)                        /* put error message to stderr */
635   register char  *s;
636   {
# Line 553 | Line 650 | register char  *s;
650   }
651  
652  
653 < wputs(s)                        /* put warning string to stderr */
557 < char    *s;
558 < {
559 <        if (!nowarn)
560 <                eputs(s);
561 < }
562 <
563 <
653 > void
654   quit(ec)                        /* exit program gracefully */
655   int     ec;
656   {
# Line 569 | Line 659 | int    ec;
659          if (hdlist[0] != NULL) {        /* close holodeck */
660                  if (nprocs > 0)
661                          status = done_rtrace();         /* calls hdsync() */
662 <                if (ncprocs > 0 && vdef(REPORT)) {
663 <                        long    fsiz, fuse;
662 >                if ((ncprocs > 0) & (force >= 0) && vdef(REPORT)) {
663 >                        off_t   fsiz, fuse;
664                          fsiz = hdfilen(hdlist[0]->fd);
665                          fuse = hdfiluse(hdlist[0]->fd, 1);
666                          fprintf(stderr,

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines