--- ray/src/hd/rholo.c 1997/12/15 20:43:01 3.28 +++ ray/src/hd/rholo.c 2003/06/30 14:59:12 3.59 @@ -1,19 +1,31 @@ -/* Copyright (c) 1997 Silicon Graphics, Inc. */ - #ifndef lint -static char SCCSid[] = "$SunId$ SGI"; +static const char RCSid[] = "$Id: rholo.c,v 3.59 2003/06/30 14:59:12 schorsch Exp $"; #endif - /* * Radiance holodeck generation controller */ -#include "rholo.h" -#include "random.h" #include -#include #include +#include +#include "rholo.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 */ @@ -23,21 +35,19 @@ char *progname; /* our program name */ char *hdkfile; /* holodeck file name */ char froot[256]; /* root file name */ -int nowarn = 0; /* turn warnings off? */ - int ncprocs = 0; /* desired number of compute processes */ 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}; @@ -48,9 +58,12 @@ long nraysdone = 0L; /* number of rays done */ long npacksdone = 0L; /* number of packets done */ PACKET *freepacks; /* available packets */ +int totqlen; /* maximum queue length (number of packets) */ char *sigerr[NSIG]; /* signal error messages */ +extern int nowarn; /* turn warnings off? */ + extern time_t time(); @@ -58,10 +71,8 @@ main(argc, argv) int argc; char *argv[]; { - HDGRID hdg; int i; - /* mark start time */ - starttime = time(NULL); + initurand(16384); /* initialize urand */ progname = argv[0]; /* get arguments */ for (i = 1; i < argc && argv[i][0] == '-'; i++) @@ -70,8 +81,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; @@ -89,6 +103,8 @@ char *argv[]; goto userr; } /* get root file name */ + if (i >= argc) + goto userr; rootname(froot, hdkfile=argv[i++]); /* load variables? */ if (i < argc) @@ -105,18 +121,22 @@ char *argv[]; } /* check settings */ checkvalues(); - /* load RIF if any */ + /* load rad input file */ getradfile(); if (hdlist[0] == NULL) { /* create new holodeck */ + HDGRID hdg[HDMAX]; /* set defaults */ - setdefaults(&hdg); + 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, "holodeck file exists -- use -f to overwrite"); /* create holodeck */ - creatholo(&hdg); + creatholo(hdg); } else /* else just set defaults */ setdefaults(NULL); /* initialize */ @@ -128,22 +148,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"); @@ -170,7 +193,7 @@ int fd, mod; if (fstat(fd, &stbuf) < 0) error(SYSTEM, "cannot stat open holodeck file"); mod &= stbuf.st_mode; /* always more restrictive */ - if (mod == stbuf.st_mode) + if (mod == (stbuf.st_mode & 0777)) return(-1); /* already set */ /* else change it */ if (fchmod(fd, mod) < 0) { @@ -194,17 +217,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); - /* record end time */ - if (!vdef(TIME) || vflt(TIME) <= FTINY) - endtime = 0; - else - endtime = starttime + vflt(TIME)*3600. + .5; /* 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 */ @@ -213,9 +231,16 @@ initrholo() /* get our holodeck running */ if (*s && freopen(s, "a", stderr) == NULL) quit(2); } + /* mark the starting time */ + starttime = time(NULL); + /* compute end time */ + if (!vdef(TIME) || vflt(TIME) <= FTINY) + endtime = 0; + else + endtime = starttime + vflt(TIME)*3600. + .5; /* start rtrace */ if (ncprocs > 0) { - i = start_rtrace(); + totqlen = i = start_rtrace(); if (i < 1) error(USER, "cannot start rtrace process(es)"); if (vdef(REPORT)) { /* make first report */ @@ -252,8 +277,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"); @@ -262,29 +287,39 @@ memerr: rholo() /* holodeck main loop */ { - static int idle = 1; + static long nextfragwarn = 100*(1L<<20); + static int idle = 0; PACKET *pl = NULL, *plend; + off_t fsiz; + int pksiz; register PACKET *p; time_t t; - long l; - - if (outdev != NULL) /* check display */ - if (!disp_check(idle)) - return(0); - /* display only? */ - if (nprocs <= 0) { + /* check display */ + if (nprocs <= 0) idle = 1; - return(outdev != NULL); - } - /* check file size */ - if (maxdisk > 0 && hdfilen(hdlist[0]->fd) >= maxdisk) { + if (outdev != NULL) { + if (!disp_check(idle)) + return(0); /* quit request */ + if (nprocs <= 0) + return(1); + } else if (idle) + return(0); /* all done */ + fsiz = hdfilen(hdlist[0]->fd); /* check file size */ + if (maxdisk > 0 && fsiz >= maxdisk) { error(WARNING, "file limit exceeded"); done_rtrace(); return(1); /* comes back */ } - /* check time */ - if (endtime > 0 || reporttime > 0) - t = time(NULL); +#if FRAGWARN + if (fsiz >= nextfragwarn && + (fsiz-hdfiluse(hdlist[0]->fd,0))/(fsiz/100) > FRAGWARN) { + sprintf(errmsg, "holodeck file fragmentation is %.0f%%", + 100.*(fsiz-hdfiluse(hdlist[0]->fd,1))/fsiz); + error(WARNING, errmsg); + nextfragwarn = fsiz + (fsiz>>2); /* decent interval */ + } +#endif + t = time(NULL); /* check time */ if (endtime > 0 && t >= endtime) { error(WARNING, "time limit exceeded"); done_rtrace(); @@ -292,10 +327,19 @@ rholo() /* holodeck main loop */ } if (reporttime > 0 && t >= reporttime) report(t); + /* figure out good packet size */ + pksiz = RPACKSIZ; +#if MAXQTIME + if (!chunkycmp) { + pksiz = nraysdone*MAXQTIME/(totqlen*(t - starttime + 1L)); + if (pksiz < 1) pksiz = 1; + else if (pksiz > RPACKSIZ) pksiz = RPACKSIZ; + } +#endif idle = 0; /* get packets to process */ while (freepacks != NULL) { p = freepacks; freepacks = p->next; p->next = NULL; - if (!next_packet(p)) { + if (!next_packet(p, pksiz)) { p->next = freepacks; freepacks = p; idle = 1; break; @@ -315,17 +359,14 @@ register HDGRID *gp; { extern char *atos(); register int i; - double len[3], maxlen, d; + int n; + double len[3], d; char buf[64]; if (!vdef(SECTION)) { sprintf(errmsg, "%s must be defined", vnam(SECTION)); error(USER, errmsg); } - if (vdef(SECTION) > 1) { - sprintf(errmsg, "ignoring all but first %s", vnam(SECTION)); - error(WARNING, errmsg); - } if (!vdef(OCTREE)) { if ((vval(OCTREE) = bmalloc(strlen(froot)+5)) == NULL) error(SYSTEM, "out of memory"); @@ -343,29 +384,30 @@ register HDGRID *gp; if (gp == NULL) /* already initialized? */ return; /* set grid parameters */ - gp->grid[0] = gp->grid[1] = gp->grid[2] = 0; - if (sscanf(vval(SECTION), + for (n = 0; n < vdef(SECTION); n++, gp++) { + gp->grid[0] = gp->grid[1] = gp->grid[2] = 0; + if (sscanf(nvalue(SECTION, n), "%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %hd %hd %hd", - &gp->orig[0], &gp->orig[1], &gp->orig[2], - &gp->xv[0][0], &gp->xv[0][1], &gp->xv[0][2], - &gp->xv[1][0], &gp->xv[1][1], &gp->xv[1][2], - &gp->xv[2][0], &gp->xv[2][1], &gp->xv[2][2], - &gp->grid[0], &gp->grid[1], &gp->grid[2]) < 12) - badvalue(SECTION); - maxlen = 0.; - for (i = 0; i < 3; i++) - if ((len[i] = VLEN(gp->xv[i])) > maxlen) - maxlen = len[i]; - if (!vdef(GRID)) { - sprintf(buf, "%.4f", maxlen/8.); - vval(GRID) = savqstr(buf); - vdef(GRID)++; + &gp->orig[0], &gp->orig[1], &gp->orig[2], + &gp->xv[0][0], &gp->xv[0][1], &gp->xv[0][2], + &gp->xv[1][0], &gp->xv[1][1], &gp->xv[1][2], + &gp->xv[2][0], &gp->xv[2][1], &gp->xv[2][2], + &gp->grid[0], &gp->grid[1], &gp->grid[2]) < 12) + badvalue(SECTION); + for (i = 0; i < 3; i++) + len[i] = VLEN(gp->xv[i]); + if (!vdef(GRID)) { + d = 2/2e5*( len[0]*len[0]*(len[1]*len[1] + + len[2]*len[2] + 4*len[1]*len[2]) + + len[1]*len[1]*len[2]*(len[2] + 4*len[0]) + + 4*len[0]*len[1]*len[2]*len[2] ); + d = sqrt(sqrt(d)); + } else if ((d = vflt(GRID)) <= FTINY) + badvalue(GRID); + for (i = 0; i < 3; i++) + if (gp->grid[i] <= 0) + gp->grid[i] = len[i]/d + (1.-FTINY); } - if ((d = vflt(GRID)) <= FTINY) - badvalue(GRID); - for (i = 0; i < 3; i++) - if (gp->grid[i] <= 0) - gp->grid[i] = len[i]/d + (1.-FTINY); } @@ -373,7 +415,8 @@ creatholo(gp) /* create a holodeck output file */ HDGRID *gp; { extern char VersionID[]; - long endloc = 0; + int32 lastloc, nextloc; + int n; int fd; FILE *fp; /* open & truncate file */ @@ -387,14 +430,27 @@ HDGRID *gp; printvars(fp); fputformat(HOLOFMT, fp); fputc('\n', fp); - putw(HOLOMAGIC, fp); /* put magic number & terminus */ - fwrite(&endloc, sizeof(long), 1, fp); + putw(HOLOMAGIC, fp); /* put magic number */ fd = dup(fileno(fp)); fclose(fp); /* flush and close stdio stream */ - hdinit(fd, gp); /* allocate and initialize index */ + lastloc = lseek(fd, (off_t)0, 2); + for (n = vdef(SECTION); n--; gp++) { /* initialize each section */ + nextloc = 0L; + write(fd, (char *)&nextloc, sizeof(nextloc)); + hdinit(fd, gp); /* writes beam index */ + if (!n) + break; + nextloc = hdfilen(fd); /* write section pointer */ + if (lseek(fd, (off_t)lastloc, 0) < 0) + error(SYSTEM, + "cannot seek on holodeck file in creatholo"); + write(fd, (char *)&nextloc, sizeof(nextloc)); + lseek(fd, (off_t)(lastloc=nextloc), 0); + } } +int headline(s) /* process information header line */ char *s; { @@ -408,7 +464,7 @@ char *s; HOLOFMT, hdkfile, FMTSTR, fmt); error(USER, errmsg); } - return; + return(0); } for (cp = s; *cp; cp++) /* take off any comments */ if (*cp == '#') { @@ -416,20 +472,33 @@ char *s; break; } setvariable(s, matchvar); /* don't flag errors */ + return(0); } loadholo() /* start loading a holodeck from fname */ { - extern long ftell(); FILE *fp; int fd; - long fpos; - /* open holodeck file */ - if ((fp = fopen(hdkfile, ncprocs>0 ? "r+" : "r")) == NULL) { - sprintf(errmsg, "cannot open \"%s\" for %s", hdkfile, - ncprocs>0 ? "appending" : "reading"); - error(SYSTEM, errmsg); + int n; + 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); @@ -439,14 +508,19 @@ loadholo() /* start loading a holodeck from fname */ hdkfile); error(USER, errmsg); } - fread(&fpos, sizeof(long), 1, fp); - if (fpos != 0) - error(WARNING, "ignoring multiple sections in holodeck file"); - fpos = ftell(fp); /* get stdio position */ + nextloc = ftell(fp); /* get stdio position */ fd = dup(fileno(fp)); fclose(fp); /* done with stdio */ - lseek(fd, fpos, 0); /* align system file pointer */ - hdinit(fd, NULL); /* allocate and load index */ + for (n = 0; nextloc > 0L; n++) { /* initialize each section */ + lseek(fd, (off_t)nextloc, 0); + read(fd, (char *)&nextloc, sizeof(nextloc)); + hdinit(fd, NULL); + } + if (n != vdef(SECTION)) { + sprintf(errmsg, "number of sections does not match %s setting", + vnam(SECTION)); + error(WARNING, errmsg); + } } @@ -459,22 +533,25 @@ 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 */ - n2flush += p->nr; + if (hdcachesize <= 0) + n2flush++; nraysdone += p->nr; npacksdone++; + p->nr = 0; } - p->nr = 0; /* push onto free list */ - p->next = freepacks; + p->next = freepacks; /* push onto free list */ freepacks = p; } - if (n2flush > 512*RPACKSIZ*nprocs) { - hdflush(NULL); /* flush holodeck buffers */ + if (n2flush >= RTFLUSH) { + if (outdev != NULL) + hdsync(NULL, 1); + else + hdflush(NULL); n2flush = 0; } } @@ -503,6 +580,7 @@ int vc; } +void eputs(s) /* put error message to stderr */ register char *s; { @@ -522,14 +600,7 @@ register char *s; } -wputs(s) /* put warning string to stderr */ -char *s; -{ - if (!nowarn) - eputs(s); -} - - +void quit(ec) /* exit program gracefully */ int ec; { @@ -538,8 +609,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,