--- ray/src/hd/rholo.c 1998/12/30 08:02:37 3.44 +++ ray/src/hd/rholo.c 2003/10/20 16:01:55 3.61 @@ -1,25 +1,32 @@ -/* Copyright (c) 1998 Silicon Graphics, Inc. */ - #ifndef lint -static char SCCSid[] = "$SunId$ SGI"; +static const char RCSid[] = "$Id: rholo.c,v 3.61 2003/10/20 16:01:55 greg Exp $"; #endif - /* * Radiance holodeck generation controller */ -#include "rholo.h" -#include "random.h" #include -#include #include +#include +#include "rholo.h" +#include "platform.h" +#include "random.h" + #ifndef FRAGWARN #define FRAGWARN 20 /* fragmentation for warning (%) */ #endif #ifndef MAXQTIME #define MAXQTIME 5 /* target maximum seconds in queue */ #endif + /* manual cache flushing frequency */ +#ifndef RTFLUSH +#if MAXQTIME +#define RTFLUSH (300/MAXQTIME*totqlen) /* <= 5 minutes */ +#else +#define RTFLUSH (50*totqlen) /* just guess */ +#endif +#endif /* the following must be consistent with rholo.h */ int NVARS = NRHVARS; /* total number of variables */ @@ -35,13 +42,13 @@ char *outdev = NULL; /* output device name */ int readinp = 0; /* read commands from stdin */ -int force = 0; /* allow overwrite of holodeck */ +int force = 0; /* allow overwrite of holodeck (-1 == read-only) */ time_t starttime; /* time we got started */ time_t endtime; /* time we should end by */ time_t reporttime; /* time for next report */ -long maxdisk; /* maximum file space (bytes) */ +off_t maxdisk; /* maximum file space (bytes) */ int rtargc = 1; /* rtrace command */ char *rtargv[128] = {"rtrace", NULL}; @@ -52,7 +59,7 @@ long nraysdone = 0L; /* number of rays done */ long npacksdone = 0L; /* number of packets done */ PACKET *freepacks; /* available packets */ -int totqlen; /* maximum queue length when full */ +int totqlen; /* maximum queue length (number of packets) */ char *sigerr[NSIG]; /* signal error messages */ @@ -75,8 +82,11 @@ char *argv[]; nowarn++; break; case 'f': /* force overwrite */ - force++; + force = 1; break; + case 'r': /* read-only mode */ + force = -1; + break; case 'i': /* read input from stdin */ readinp++; break; @@ -119,6 +129,9 @@ char *argv[]; HDGRID hdg[HDMAX]; /* set defaults */ setdefaults(hdg); + /* check read-only */ + if (force < 0) + error(USER, "cannot create read-only holodeck"); /* holodeck exists? */ if (!force && access(hdkfile, R_OK|W_OK) == 0) error(USER, @@ -136,22 +149,25 @@ char *argv[]; quit(0); userr: fprintf(stderr, -"Usage: %s [-n nprocs][-o disp][-w][-f] output.hdk [control.hif|+|- [VAR=val ..]]\n", +"Usage: %s [-n nprocs][-o disp][-w][-r|-f] output.hdk [control.hif|+|- [VAR=val ..]]\n", progname); quit(1); } +void onsig(signo) /* fatal signal */ int signo; { static int gotsig = 0; - if (gotsig++) /* two signals and we're gone! */ + if (gotsig > 1) /* we're going as fast as we can! */ + return; + if (gotsig++) { /* two signals and we split */ + hdsync(NULL, 0); /* don't leave w/o saying goodbye */ _exit(signo); - - alarm(60); /* allow 60 seconds to clean up */ - signal(SIGALRM, SIG_DFL); /* make certain we do die */ + } + alarm(300); /* allow 5 minutes to clean up */ eputs("signal - "); eputs(sigerr[signo]); eputs("\n"); @@ -202,12 +218,12 @@ initrholo() /* get our holodeck running */ init_global(); /* record disk space limit */ if (!vdef(DISKSPACE)) - maxdisk = 0; + maxdisk = ((off_t)1<<(sizeof(off_t)*8-2)) - 1024; else maxdisk = 1024.*1024.*vflt(DISKSPACE); /* set up memory cache */ if (outdev == NULL) - hdcachesize = 0; /* manual flushing */ + hdcachesize = 0; /* manual flushing */ else if (vdef(CACHE)) hdcachesize = 1024.*1024.*vflt(CACHE); /* open report file */ @@ -262,8 +278,8 @@ initrholo() /* get our holodeck running */ sigdie(SIGXCPU, "CPU limit exceeded"); sigdie(SIGXFSZ, "File size exceeded"); #endif - /* protect holodeck file */ - orig_mode = resfmode(hdlist[0]->fd, ncprocs>0 ? 0 : 0444); + /* protect holodeck file */ + orig_mode = resfmode(hdlist[0]->fd, (ncprocs>0) & (force>=0) ? 0 : 0444); return; memerr: error(SYSTEM, "out of memory in initrholo"); @@ -275,7 +291,7 @@ rholo() /* holodeck main loop */ static long nextfragwarn = 100*(1L<<20); static int idle = 0; PACKET *pl = NULL, *plend; - long fsiz; + off_t fsiz; int pksiz; register PACKET *p; time_t t; @@ -313,13 +329,14 @@ rholo() /* holodeck main loop */ if (reporttime > 0 && t >= reporttime) report(t); /* figure out good packet size */ + pksiz = RPACKSIZ; #if MAXQTIME - pksiz = nraysdone*MAXQTIME/(totqlen*(t - starttime + 1L)); - if (pksiz < 1) - pksiz = 1; - else if (pksiz > RPACKSIZ) + if (!chunkycmp) { + pksiz = nraysdone*MAXQTIME/(totqlen*(t - starttime + 1L)); + if (pksiz < 1) pksiz = 1; + else if (pksiz > RPACKSIZ) pksiz = RPACKSIZ; + } #endif - pksiz = RPACKSIZ; idle = 0; /* get packets to process */ while (freepacks != NULL) { p = freepacks; freepacks = p->next; p->next = NULL; @@ -399,7 +416,7 @@ creatholo(gp) /* create a holodeck output file */ HDGRID *gp; { extern char VersionID[]; - int4 lastloc, nextloc; + int32 lastloc, nextloc; int n; int fd; FILE *fp; @@ -417,7 +434,7 @@ HDGRID *gp; putw(HOLOMAGIC, fp); /* put magic number */ fd = dup(fileno(fp)); fclose(fp); /* flush and close stdio stream */ - lastloc = lseek(fd, 0L, 2); + lastloc = lseek(fd, (off_t)0, SEEK_END); for (n = vdef(SECTION); n--; gp++) { /* initialize each section */ nextloc = 0L; write(fd, (char *)&nextloc, sizeof(nextloc)); @@ -425,11 +442,11 @@ HDGRID *gp; if (!n) break; nextloc = hdfilen(fd); /* write section pointer */ - if (lseek(fd, (long)lastloc, 0) < 0) + if (lseek(fd, (off_t)lastloc, SEEK_SET) < 0) error(SYSTEM, "cannot seek on holodeck file in creatholo"); write(fd, (char *)&nextloc, sizeof(nextloc)); - lseek(fd, (long)(lastloc=nextloc), 0); + lseek(fd, (off_t)(lastloc=nextloc), SEEK_SET); } } @@ -462,16 +479,27 @@ char *s; loadholo() /* start loading a holodeck from fname */ { - extern long ftell(); FILE *fp; int fd; int n; - int4 nextloc; - /* open holodeck file */ - if ((fp = fopen(hdkfile, ncprocs>0 ? "r+" : "r")) == NULL) { - sprintf(errmsg, "cannot %s \"%s\"", - ncprocs>0 ? "append" : "read", hdkfile); - error(SYSTEM, errmsg); + int32 nextloc; + + if ((ncprocs > 0) & (force >= 0)) + fp = fopen(hdkfile, "r+"); + else + fp = NULL; + if (fp == NULL) { + if ((fp = fopen(hdkfile, "r")) == NULL) { + sprintf(errmsg, "cannot open \"%s\"", hdkfile); + error(SYSTEM, errmsg); + } + if (ncprocs > 0) { + sprintf(errmsg, + "\"%s\" opened read-only; new rays will be discarded", + hdkfile); + error(WARNING, errmsg); + force = -1; + } } /* load variables from header */ getheader(fp, headline, NULL); @@ -485,7 +513,7 @@ loadholo() /* start loading a holodeck from fname */ fd = dup(fileno(fp)); fclose(fp); /* done with stdio */ for (n = 0; nextloc > 0L; n++) { /* initialize each section */ - lseek(fd, (long)nextloc, 0); + lseek(fd, (off_t)nextloc, SEEK_SET); read(fd, (char *)&nextloc, sizeof(nextloc)); hdinit(fd, NULL); } @@ -506,12 +534,12 @@ PACKET *pl; while (pl != NULL) { p = pl; pl = p->next; p->next = NULL; if (p->nr > 0) { /* add to holodeck */ - bcopy((char *)p->ra, - (char *)hdnewrays(hdlist[p->hd],p->bi,p->nr), + memcpy( (void *)hdnewrays(hdlist[p->hd],p->bi,p->nr), + (void *)p->ra, p->nr*sizeof(RAYVAL)); if (outdev != NULL) /* display it */ disp_packet((PACKHEAD *)p); - if (hdcachesize <= 0) /* manual flushing */ + if (hdcachesize <= 0) n2flush++; nraysdone += p->nr; npacksdone++; @@ -520,15 +548,11 @@ PACKET *pl; p->next = freepacks; /* push onto free list */ freepacks = p; } -#if MAXQTIME - if (n2flush > 300/MAXQTIME*totqlen) { -#else - if (n2flush > 50*totqlen) { -#endif - if (outdev == NULL) - hdflush(NULL); /* flush holodeck buffers */ + if (n2flush >= RTFLUSH) { + if (outdev != NULL) + hdsync(NULL, 1); else - hdsync(NULL, 1); /* sync holodeck file */ + hdflush(NULL); n2flush = 0; } } @@ -539,11 +563,12 @@ register char *rn, *fn; { char *tp, *dp; - for (tp = NULL, dp = rn; *rn = *fn++; rn++) + for (tp = NULL, dp = rn; (*rn = *fn++); rn++) { if (*rn == '/') dp = rn; else if (*rn == '.') tp = rn; + } if (tp != NULL && tp > dp) *tp = '\0'; } @@ -557,6 +582,7 @@ int vc; } +void eputs(s) /* put error message to stderr */ register char *s; { @@ -576,6 +602,7 @@ register char *s; } +void quit(ec) /* exit program gracefully */ int ec; { @@ -584,8 +611,8 @@ int ec; if (hdlist[0] != NULL) { /* close holodeck */ if (nprocs > 0) status = done_rtrace(); /* calls hdsync() */ - if (ncprocs > 0 && vdef(REPORT)) { - long fsiz, fuse; + if ((ncprocs > 0) & (force >= 0) && vdef(REPORT)) { + off_t fsiz, fuse; fsiz = hdfilen(hdlist[0]->fd); fuse = hdfiluse(hdlist[0]->fd, 1); fprintf(stderr,