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.48 by gwlarson, Tue Feb 2 16:13:24 1999 UTC vs.
Revision 3.83 by greg, Thu Feb 9 21:54:11 2023 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1999 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 < #ifndef RTFLUSH
15 < #define RTFLUSH         0               /* flush one at a time in batch */
16 < #endif
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 */
24 > #define MAXQTIME        3               /* 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 38 | Line 46 | 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};
58 > char    *rtargv[MAXRTARGC] = {"rtrace", NULL};
59  
60   int     orig_mode = -1;         /* original file mode (-1 if unchanged) */
61  
# Line 55 | 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 int      nowarn;         /* turn warnings off? */
71  
72 < extern time_t   time();
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  
85 < main(argc, argv)
86 < int     argc;
87 < char    *argv[];
85 > int
86 > main(
87 >        int     argc,
88 >        char    *argv[]
89 > )
90   {
91          int     i;
92  
73        initurand(16384);                       /* initialize urand */
93          progname = argv[0];                     /* get arguments */
94          for (i = 1; i < argc && argv[i][0] == '-'; i++)
95                  switch (argv[i][1]) {
# Line 78 | Line 97 | char   *argv[];
97                          nowarn++;
98                          break;
99                  case 'f':                       /* force overwrite */
100 <                        force++;
100 >                        force = 1;
101                          break;
102 +                case 'r':                       /* read-only mode */
103 +                        force = -1;
104 +                        break;
105                  case 'i':                       /* read input from stdin */
106                          readinp++;
107                          break;
# Line 122 | Line 144 | char   *argv[];
144                  HDGRID  hdg[HDMAX];
145                                                          /* set defaults */
146                  setdefaults(hdg);
147 +                                                        /* check read-only */
148 +                if (force < 0)
149 +                        error(USER, "cannot create read-only holodeck");
150                                                          /* holodeck exists? */
151                  if (!force && access(hdkfile, R_OK|W_OK) == 0)
152                          error(USER,
# Line 139 | Line 164 | char   *argv[];
164          quit(0);
165   userr:
166          fprintf(stderr,
167 < "Usage: %s [-n nprocs][-o disp][-w][-f] output.hdk [control.hif|+|- [VAR=val ..]]\n",
167 > "Usage: %s [-n nprocs][-o disp][-i][-w][-r|-f] output.hdk [control.hif|+|- [VAR=val ..]]\n",
168                          progname);
169          quit(1);
170 +        return 1; /* pro forma return */
171   }
172  
173  
174 < onsig(signo)                            /* fatal signal */
175 < int  signo;
174 > static void
175 > onsig(                          /* fatal signal */
176 >        int  signo
177 > )
178   {
179          static int  gotsig = 0;
180  
# Line 156 | Line 184 | int  signo;
184                  hdsync(NULL, 0);        /* don't leave w/o saying goodbye */
185                  _exit(signo);
186          }
187 <        alarm(300);                     /* allow 5 minutes to clean up */
187 >        alarm(300);                     /* allow 10 minutes to clean up */
188          eputs("signal - ");
189          eputs(sigerr[signo]);
190          eputs("\n");
# Line 164 | Line 192 | int  signo;
192   }
193  
194  
195 < sigdie(signo, msg)                      /* set fatal signal */
196 < int  signo;
197 < char  *msg;
195 > static void
196 > sigdie(                 /* set fatal signal */
197 >        int  signo,
198 >        char  *msg
199 > )
200   {
201          if (signal(signo, onsig) == SIG_IGN)
202                  signal(signo, SIG_IGN);
# Line 174 | Line 204 | char  *msg;
204   }
205  
206  
207 < int
208 < resfmode(fd, mod)               /* restrict open file access mode */
209 < int     fd, mod;
207 > static int
208 > resfmode(               /* restrict open file access mode */
209 >        int     fd,
210 >        int     mod
211 > )
212   {
213          struct stat     stbuf;
214                                          /* get original mode */
# Line 194 | Line 226 | int    fd, mod;
226   }
227  
228  
229 < initrholo()                     /* get our holodeck running */
229 > static void
230 > initrholo(void)                 /* get our holodeck running */
231   {
232          extern int      global_packet();
233 <        register int    i;
233 >        int     i;
234                                                  /* close holodeck on exec() */
235          fcntl(hdlist[0]->fd, F_SETFD, FD_CLOEXEC);
236  
# Line 207 | Line 240 | initrholo()                    /* get our holodeck running */
240                  init_global();
241                                                  /* record disk space limit */
242          if (!vdef(DISKSPACE))
243 <                maxdisk = 0;
243 >                maxdisk = ((off_t)1<<(sizeof(off_t)*8-2)) - 1024;
244          else
245                  maxdisk = 1024.*1024.*vflt(DISKSPACE);
246                                                  /* set up memory cache */
247          if (outdev == NULL)
248 <                hdcachesize = 0;        /* manual flushing */
248 >                hdcachesize = 0;                /* manual flushing */
249          else if (vdef(CACHE))
250                  hdcachesize = 1024.*1024.*vflt(CACHE);
251                                                  /* open report file */
252          if (vdef(REPORT)) {
253 <                register char   *s = sskip2(vval(REPORT), 1);
253 >                char    *s = sskip2(vval(REPORT), 1);
254                  if (*s && freopen(s, "a", stderr) == NULL)
255                          quit(2);
256          }
# Line 259 | Line 292 | initrholo()                    /* get our holodeck running */
292          }
293                                          /* set up signal handling */
294          sigdie(SIGINT, "Interrupt");
262        sigdie(SIGHUP, "Hangup");
295          sigdie(SIGTERM, "Terminate");
296 + #ifdef SIGHUP
297 +        sigdie(SIGHUP, "Hangup");
298 + #endif
299 + #ifdef SIGPIPE
300          sigdie(SIGPIPE, "Broken pipe");
301 + #endif
302 + #ifdef SIGALRM
303          sigdie(SIGALRM, "Alarm clock");
304 + #endif
305   #ifdef  SIGXCPU
306          sigdie(SIGXCPU, "CPU limit exceeded");
307 + #endif
308 + #ifdef  SIGXFSZ
309          sigdie(SIGXFSZ, "File size exceeded");
310   #endif
311 <                                                /* protect holodeck file */
312 <        orig_mode = resfmode(hdlist[0]->fd, ncprocs>0 ? 0 : 0444);
311 >                                        /* protect holodeck file */
312 >        orig_mode = resfmode(hdlist[0]->fd, (ncprocs>0) & (force>=0) ? 0 : 0444);
313          return;
314   memerr:
315          error(SYSTEM, "out of memory in initrholo");
316   }
317  
318  
319 < rholo()                         /* holodeck main loop */
319 > static int
320 > rholo(void)                             /* holodeck main loop */
321   {
322 <        static long     nextfragwarn = 100*(1L<<20);
322 >        static off_t    nextfragwarn = 100L<<20;
323          static int      idle = 0;
324          PACKET  *pl = NULL, *plend;
325 <        long    fsiz;
325 >        off_t   fsiz;
326          int     pksiz;
327 <        register PACKET *p;
327 >        PACKET  *p;
328          time_t  t;
329                                          /* check display */
330          if (nprocs <= 0)
# Line 296 | Line 338 | rholo()                                /* holodeck main loop */
338                  return(0);              /* all done */
339          fsiz = hdfilen(hdlist[0]->fd);  /* check file size */
340          if (maxdisk > 0 && fsiz >= maxdisk) {
341 <                error(WARNING, "file limit exceeded");
341 >                error(USER, "file limit exceeded");
342                  done_rtrace();
343                  return(1);      /* comes back */
344          }
345   #if FRAGWARN
346 <        if (fsiz >= nextfragwarn &&
347 <                (fsiz-hdfiluse(hdlist[0]->fd,0))/(fsiz/100) > FRAGWARN) {
348 <                sprintf(errmsg, "holodeck file fragmentation is %.0f%%",
349 <                                100.*(fsiz-hdfiluse(hdlist[0]->fd,1))/fsiz);
350 <                error(WARNING, errmsg);
351 <                nextfragwarn = fsiz + (fsiz>>2);        /* decent interval */
346 >        if (fsiz >= nextfragwarn) {
347 >                double  pctfrag = 100.*(fsiz-hdfiluse(hdlist[0]->fd))/fsiz;
348 >                if (pctfrag >= (double)FRAGWARN) {
349 >                        sprintf(errmsg, "holodeck file fragmentation is %.0f%%",
350 >                                        pctfrag);
351 >                        error(WARNING, errmsg);
352 >                        nextfragwarn = fsiz + (fsiz>>2);
353 >                } else
354 >                        nextfragwarn = fsiz + (10L<<20);
355          }
356   #endif
357          t = time(NULL);                 /* check time */
358          if (endtime > 0 && t >= endtime) {
359 <                error(WARNING, "time limit exceeded");
359 >                error(USER, "time limit exceeded");
360                  done_rtrace();
361                  return(1);      /* comes back */
362          }
# Line 344 | Line 389 | rholo()                                /* holodeck main loop */
389   }
390  
391  
392 < setdefaults(gp)                 /* set default values */
393 < register HDGRID *gp;
392 > static void
393 > setdefaults(                    /* set default values */
394 >        HDGRID  *gp
395 > )
396   {
397          extern char     *atos();
398 <        register int    i;
398 >        int     i;
399          int     n;
400          double  len[3], d;
354        char    buf[64];
401  
402          if (!vdef(SECTION)) {
403                  sprintf(errmsg, "%s must be defined", vnam(SECTION));
# Line 369 | Line 415 | register HDGRID        *gp;
415          }
416                                  /* append rendering options */
417          if (vdef(RENDER))
418 <                rtargc += wordstring(rtargv+rtargc, vval(RENDER));
418 >                rtargc += wordstring(rtargv+rtargc, MAXRTARGC-rtargc, vval(RENDER));
419          
420          if (gp == NULL)         /* already initialized? */
421                  return;
# Line 401 | Line 447 | register HDGRID        *gp;
447   }
448  
449  
450 < creatholo(gp)                   /* create a holodeck output file */
451 < HDGRID  *gp;
450 > static void
451 > creatholo(                      /* create a holodeck output file */
452 >        HDGRID  *gp
453 > )
454   {
455          extern char     VersionID[];
456 <        int4    lastloc, nextloc;
456 >        off_t   lastloc, nextloc;
457          int     n;
458          int     fd;
459          FILE    *fp;
460                                          /* open & truncate file */
461 <        if ((fp = fopen(hdkfile, "w+")) == NULL) {
461 >        if ((fp = fopen(hdkfile, "wb+")) == NULL) {
462                  sprintf(errmsg, "cannot open \"%s\" for writing", hdkfile);
463                  error(SYSTEM, errmsg);
464          }
# Line 418 | Line 466 | HDGRID *gp;
466          newheader("RADIANCE", fp);
467          fprintf(fp, "SOFTWARE= %s\n", VersionID);
468          printvars(fp);
469 +        fputendian(fp);
470          fputformat(HOLOFMT, fp);
471          fputc('\n', fp);
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 431 | 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, 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, (lastloc=nextloc), SEEK_SET);
488          }
489   }
490  
491  
492 < int
493 < headline(s)                     /* process information header line */
494 < 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;
500 <        char    fmt[32];
499 >        char    *cp;
500 >        char    fmt[MAXFMTLEN];
501  
502          if (formatval(fmt, s)) {
503                  if (strcmp(fmt, HOLOFMT)) {
# Line 466 | Line 517 | char   *s;
517   }
518  
519  
520 < loadholo()                      /* start loading a holodeck from fname */
520 > static void
521 > loadholo(void)                  /* start loading a holodeck from fname */
522   {
471        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 %s \"%s\"",
530 <                                ncprocs>0 ? "append" : "read", hdkfile);
531 <                error(SYSTEM, errmsg);
526 >        off_t   nextloc;
527 >        
528 >        if ((ncprocs > 0) & (force >= 0))
529 >                fp = fopen(hdkfile, "rb+");
530 >        else
531 >                fp = NULL;
532 >        if (fp == NULL) {
533 >                if ((fp = fopen(hdkfile, "rb")) == NULL) {
534 >                        sprintf(errmsg, "cannot open \"%s\"", hdkfile);
535 >                        error(SYSTEM, errmsg);
536 >                }
537 >                if (ncprocs > 0) {
538 >                        sprintf(errmsg, "\"%s\" is read-only", hdkfile);
539 >                        if (outdev == NULL)
540 >                                error(USER, errmsg);
541 >                        strcat(errmsg, "; new rays will be discarded");
542 >                        error(WARNING, errmsg);
543 >                        force = -1;
544 >                }
545          }
546                                          /* load variables from header */
547          getheader(fp, headline, NULL);
# Line 491 | Line 555 | loadholo()                     /* start loading a holodeck from fname */
555          fd = dup(fileno(fp));
556          fclose(fp);                             /* done with stdio */
557          for (n = 0; nextloc > 0L; n++) {        /* initialize each section */
558 <                lseek(fd, (long)nextloc, 0);
558 >                lseek(fd, nextloc, SEEK_SET);
559                  read(fd, (char *)&nextloc, sizeof(nextloc));
560                  hdinit(fd, NULL);
561          }
# Line 503 | Line 567 | loadholo()                     /* start loading a holodeck from fname */
567   }
568  
569  
570 < done_packets(pl)                /* handle finished packets */
571 < PACKET  *pl;
570 > void
571 > done_packets(           /* handle finished packets */
572 >        PACKET  *pl
573 > )
574   {
575          static int      n2flush = 0;
576 <        register PACKET *p;
576 >        PACKET  *p;
577  
578          while (pl != NULL) {
579                  p = pl; pl = p->next; p->next = NULL;
580                  if (p->nr > 0) {                /* add to holodeck */
581 <                        bcopy((char *)p->ra,
582 <                                (char *)hdnewrays(hdlist[p->hd],p->bi,p->nr),
581 >                        memcpy( (void *)hdnewrays(hdlist[p->hd],p->bi,p->nr),
582 >                                (void *)p->ra,
583                                  p->nr*sizeof(RAYVAL));
584                          if (outdev != NULL)     /* display it */
585                                  disp_packet((PACKHEAD *)p);
586 <                        if (hdcachesize <= 0) {
521 < #if RTFLUSH
522 <                                if (outdev == NULL)
523 <                                        hdfreebeam(hdlist[p->hd], p->bi);
524 < #endif
586 >                        if (hdcachesize <= 0)
587                                  n2flush++;
526                        }
588                          nraysdone += p->nr;
589                          npacksdone++;
590                          p->nr = 0;
# Line 531 | Line 592 | PACKET *pl;
592                  p->next = freepacks;            /* push onto free list */
593                  freepacks = p;
594          }
595 < #if MAXQTIME
535 <        if (n2flush > 300/MAXQTIME*totqlen) {
536 < #else
537 <        if (n2flush > 50*totqlen) {
538 < #endif
539 < #if RTFLUSH
540 <                hdsync(NULL, outdev!=NULL);     /* sync beams & directories */
541 < #else
595 >        if (n2flush >= RTFLUSH) {
596                  if (outdev != NULL)
597                          hdsync(NULL, 1);
598                  else
599                          hdflush(NULL);
546 #endif
600                  n2flush = 0;
601          }
602   }
603  
604  
605 < rootname(rn, fn)                /* remove tail from end of fn */
606 < register char   *rn, *fn;
605 > static void
606 > rootname(               /* remove tail from end of fn */
607 >        char    *rn,
608 >        char    *fn
609 > )
610   {
611          char    *tp, *dp;
612  
613 <        for (tp = NULL, dp = rn; *rn = *fn++; rn++)
613 >        for (tp = NULL, dp = rn; (*rn = *fn++); rn++) {
614                  if (*rn == '/')
615                          dp = rn;
616                  else if (*rn == '.')
617                          tp = rn;
618 +        }
619          if (tp != NULL && tp > dp)
620                  *tp = '\0';
621   }
622  
623  
624 < badvalue(vc)                    /* report bad variable value and exit */
625 < int     vc;
624 > static void
625 > badvalue(                       /* report bad variable value and exit */
626 >        int     vc
627 > )
628   {
629          sprintf(errmsg, "bad value for variable '%s'", vnam(vc));
630          error(USER, errmsg);
631   }
632  
633  
634 < eputs(s)                        /* put error message to stderr */
635 < register char  *s;
634 > void
635 > eputs(const char *s)            /* put error message to stderr */
636   {
637          static int  midline = 0;
638  
# Line 591 | Line 650 | register char  *s;
650   }
651  
652  
653 < quit(ec)                        /* exit program gracefully */
654 < int     ec;
653 > void
654 > quit(int ec)                    /* exit program gracefully */
655   {
656          int     status = 0;
657  
658          if (hdlist[0] != NULL) {        /* close holodeck */
659                  if (nprocs > 0)
660                          status = done_rtrace();         /* calls hdsync() */
661 <                if (ncprocs > 0 && vdef(REPORT)) {
662 <                        long    fsiz, fuse;
661 >                if ((ncprocs > 0) & (force >= 0) && vdef(REPORT)) {
662 >                        off_t   fsiz, fuse;
663                          fsiz = hdfilen(hdlist[0]->fd);
664 <                        fuse = hdfiluse(hdlist[0]->fd, 1);
664 >                        fuse = hdfiluse(hdlist[0]->fd);
665                          fprintf(stderr,
666                          "%s: %.1f Mbyte holodeck file, %.1f%% fragmentation\n",
667                                          hdkfile, fsiz/(1024.*1024.),

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines