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.44 by gwlarson, Wed Dec 30 08:02:37 1998 UTC vs.
Revision 3.61 by greg, Mon Oct 20 16:01:55 2003 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  
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 "rholo.h"
13 + #include "platform.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 35 | Line 42 | 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 52 | 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     totqlen;                /* maximum queue length when full */
62 > int     totqlen;                /* maximum queue length (number of packets) */
63  
64   char  *sigerr[NSIG];            /* signal error messages */
65  
# Line 75 | 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 119 | Line 129 | char   *argv[];
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 136 | 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 */
154 <        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 202 | 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 262 | Line 278 | initrholo()                    /* get our holodeck running */
278          sigdie(SIGXCPU, "CPU limit exceeded");
279          sigdie(SIGXFSZ, "File size exceeded");
280   #endif
281 <                                                /* protect holodeck file */
282 <        orig_mode = resfmode(hdlist[0]->fd, ncprocs>0 ? 0 : 0444);
281 >                                        /* protect holodeck file */
282 >        orig_mode = resfmode(hdlist[0]->fd, (ncprocs>0) & (force>=0) ? 0 : 0444);
283          return;
284   memerr:
285          error(SYSTEM, "out of memory in initrholo");
# Line 275 | Line 291 | rholo()                                /* holodeck main loop */
291          static long     nextfragwarn = 100*(1L<<20);
292          static int      idle = 0;
293          PACKET  *pl = NULL, *plend;
294 <        long    fsiz;
294 >        off_t   fsiz;
295          int     pksiz;
296          register PACKET *p;
297          time_t  t;
# Line 313 | Line 329 | rholo()                                /* holodeck main loop */
329          if (reporttime > 0 && t >= reporttime)
330                  report(t);
331                                          /* figure out good packet size */
332 +        pksiz = RPACKSIZ;
333   #if MAXQTIME
334 <        pksiz = nraysdone*MAXQTIME/(totqlen*(t - starttime + 1L));
335 <        if (pksiz < 1)
336 <                pksiz = 1;
337 <        else if (pksiz > RPACKSIZ)
334 >        if (!chunkycmp) {
335 >                pksiz = nraysdone*MAXQTIME/(totqlen*(t - starttime + 1L));
336 >                if (pksiz < 1) pksiz = 1;
337 >                else if (pksiz > RPACKSIZ) pksiz = RPACKSIZ;
338 >        }
339   #endif
322                pksiz = RPACKSIZ;
340          idle = 0;                       /* get packets to process */
341          while (freepacks != NULL) {
342                  p = freepacks; freepacks = p->next; p->next = NULL;
# Line 399 | Line 416 | creatholo(gp)                  /* create a holodeck output file */
416   HDGRID  *gp;
417   {
418          extern char     VersionID[];
419 <        int4    lastloc, nextloc;
419 >        int32   lastloc, nextloc;
420          int     n;
421          int     fd;
422          FILE    *fp;
# Line 417 | Line 434 | HDGRID *gp;
434          putw(HOLOMAGIC, fp);            /* put magic number */
435          fd = dup(fileno(fp));
436          fclose(fp);                     /* flush and close stdio stream */
437 <        lastloc = lseek(fd, 0L, 2);
437 >        lastloc = lseek(fd, (off_t)0, SEEK_END);
438          for (n = vdef(SECTION); n--; gp++) {    /* initialize each section */
439                  nextloc = 0L;
440                  write(fd, (char *)&nextloc, sizeof(nextloc));
# Line 425 | Line 442 | HDGRID *gp;
442                  if (!n)
443                          break;
444                  nextloc = hdfilen(fd);          /* write section pointer */
445 <                if (lseek(fd, (long)lastloc, 0) < 0)
445 >                if (lseek(fd, (off_t)lastloc, SEEK_SET) < 0)
446                          error(SYSTEM,
447                                  "cannot seek on holodeck file in creatholo");
448                  write(fd, (char *)&nextloc, sizeof(nextloc));
449 <                lseek(fd, (long)(lastloc=nextloc), 0);
449 >                lseek(fd, (off_t)(lastloc=nextloc), SEEK_SET);
450          }
451   }
452  
# Line 462 | Line 479 | char   *s;
479  
480   loadholo()                      /* start loading a holodeck from fname */
481   {
465        extern long     ftell();
482          FILE    *fp;
483          int     fd;
484          int     n;
485 <        int4    nextloc;
486 <                                        /* open holodeck file */
487 <        if ((fp = fopen(hdkfile, ncprocs>0 ? "r+" : "r")) == NULL) {
488 <                sprintf(errmsg, "cannot %s \"%s\"",
489 <                                ncprocs>0 ? "append" : "read", hdkfile);
490 <                error(SYSTEM, errmsg);
485 >        int32   nextloc;
486 >        
487 >        if ((ncprocs > 0) & (force >= 0))
488 >                fp = fopen(hdkfile, "r+");
489 >        else
490 >                fp = NULL;
491 >        if (fp == NULL) {
492 >                if ((fp = fopen(hdkfile, "r")) == NULL) {
493 >                        sprintf(errmsg, "cannot open \"%s\"", hdkfile);
494 >                        error(SYSTEM, errmsg);
495 >                }
496 >                if (ncprocs > 0) {
497 >                        sprintf(errmsg,
498 >                        "\"%s\" opened read-only; new rays will be discarded",
499 >                                        hdkfile);
500 >                        error(WARNING, errmsg);
501 >                        force = -1;
502 >                }
503          }
504                                          /* load variables from header */
505          getheader(fp, headline, NULL);
# Line 485 | Line 513 | loadholo()                     /* start loading a holodeck from fname */
513          fd = dup(fileno(fp));
514          fclose(fp);                             /* done with stdio */
515          for (n = 0; nextloc > 0L; n++) {        /* initialize each section */
516 <                lseek(fd, (long)nextloc, 0);
516 >                lseek(fd, (off_t)nextloc, SEEK_SET);
517                  read(fd, (char *)&nextloc, sizeof(nextloc));
518                  hdinit(fd, NULL);
519          }
# Line 506 | Line 534 | PACKET *pl;
534          while (pl != NULL) {
535                  p = pl; pl = p->next; p->next = NULL;
536                  if (p->nr > 0) {                /* add to holodeck */
537 <                        bcopy((char *)p->ra,
538 <                                (char *)hdnewrays(hdlist[p->hd],p->bi,p->nr),
537 >                        memcpy( (void *)hdnewrays(hdlist[p->hd],p->bi,p->nr),
538 >                                (void *)p->ra,
539                                  p->nr*sizeof(RAYVAL));
540                          if (outdev != NULL)     /* display it */
541                                  disp_packet((PACKHEAD *)p);
542 <                        if (hdcachesize <= 0)   /* manual flushing */
542 >                        if (hdcachesize <= 0)
543                                  n2flush++;
544                          nraysdone += p->nr;
545                          npacksdone++;
# Line 520 | Line 548 | PACKET *pl;
548                  p->next = freepacks;            /* push onto free list */
549                  freepacks = p;
550          }
551 < #if MAXQTIME
552 <        if (n2flush > 300/MAXQTIME*totqlen) {
553 < #else
526 <        if (n2flush > 50*totqlen) {
527 < #endif
528 <                if (outdev == NULL)
529 <                        hdflush(NULL);          /* flush holodeck buffers */
551 >        if (n2flush >= RTFLUSH) {
552 >                if (outdev != NULL)
553 >                        hdsync(NULL, 1);
554                  else
555 <                        hdsync(NULL, 1);        /* sync holodeck file */
555 >                        hdflush(NULL);
556                  n2flush = 0;
557          }
558   }
# Line 539 | Line 563 | register char  *rn, *fn;
563   {
564          char    *tp, *dp;
565  
566 <        for (tp = NULL, dp = rn; *rn = *fn++; rn++)
566 >        for (tp = NULL, dp = rn; (*rn = *fn++); rn++) {
567                  if (*rn == '/')
568                          dp = rn;
569                  else if (*rn == '.')
570                          tp = rn;
571 +        }
572          if (tp != NULL && tp > dp)
573                  *tp = '\0';
574   }
# Line 557 | Line 582 | int    vc;
582   }
583  
584  
585 + void
586   eputs(s)                        /* put error message to stderr */
587   register char  *s;
588   {
# Line 576 | Line 602 | register char  *s;
602   }
603  
604  
605 + void
606   quit(ec)                        /* exit program gracefully */
607   int     ec;
608   {
# Line 584 | Line 611 | int    ec;
611          if (hdlist[0] != NULL) {        /* close holodeck */
612                  if (nprocs > 0)
613                          status = done_rtrace();         /* calls hdsync() */
614 <                if (ncprocs > 0 && vdef(REPORT)) {
615 <                        long    fsiz, fuse;
614 >                if ((ncprocs > 0) & (force >= 0) && vdef(REPORT)) {
615 >                        off_t   fsiz, fuse;
616                          fsiz = hdfilen(hdlist[0]->fd);
617                          fuse = hdfiluse(hdlist[0]->fd, 1);
618                          fprintf(stderr,

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines