--- ray/src/hd/rholo.c 1997/11/06 16:11:48 3.7 +++ ray/src/hd/rholo.c 1998/05/14 13:05:27 3.39 @@ -1,4 +1,4 @@ -/* Copyright (c) 1997 Silicon Graphics, Inc. */ +/* Copyright (c) 1998 Silicon Graphics, Inc. */ #ifndef lint static char SCCSid[] = "$SunId$ SGI"; @@ -9,9 +9,14 @@ static char SCCSid[] = "$SunId$ SGI"; */ #include "rholo.h" -#include "paths.h" +#include "random.h" +#include #include +#include +#ifndef MAXQTIME +#define MAXQTIME 5 /* target maximum seconds in queue */ +#endif /* the following must be consistent with rholo.h */ int NVARS = NRHVARS; /* total number of variables */ @@ -19,28 +24,37 @@ VARIABLE vv[] = RHVINIT; /* variable-value pairs */ char *progname; /* our program name */ char *hdkfile; /* holodeck file name */ -char froot[MAXPATH]; /* root file name */ +char froot[256]; /* root file name */ int nowarn = 0; /* turn warnings off? */ -double expval = 1.; /* global exposure value */ - 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 */ + 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) */ + int rtargc = 1; /* rtrace command */ char *rtargv[128] = {"rtrace", NULL}; +int orig_mode = -1; /* original file mode (-1 if unchanged) */ + 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 */ +char *sigerr[NSIG]; /* signal error messages */ + extern time_t time(); @@ -48,11 +62,9 @@ main(argc, argv) int argc; char *argv[]; { - HDGRID hdg; int i; - int force = 0; - /* mark start time */ - starttime = time(NULL); + + initurand(16384); /* initialize urand */ progname = argv[0]; /* get arguments */ for (i = 1; i < argc && argv[i][0] == '-'; i++) switch (argv[i][1]) { @@ -62,6 +74,9 @@ char *argv[]; case 'f': /* force overwrite */ force++; break; + case 'i': /* read input from stdin */ + readinp++; + break; case 'n': /* compute processes */ if (i >= argc-2) goto userr; @@ -75,68 +90,118 @@ char *argv[]; default: goto userr; } - /* do we have a job? */ - if (outdev == NULL && ncprocs <= 0) - goto userr; /* get root file name */ + if (i >= argc) + goto userr; rootname(froot, hdkfile=argv[i++]); - /* load... */ - if (i < argc) { /* variables */ - loadvars(argv[i++]); - /* cmdline settings */ - for ( ; i < argc; i++) - if (setvariable(argv[i], matchvar) < 0) { - sprintf(errmsg, "unknown variable: %s", - argv[i]); - error(USER, errmsg); - } - /* check settings */ - checkvalues(); - /* load RIF if any */ - if (vdef(RIF)) - getradfile(vval(RIF)); + /* load variables? */ + if (i < argc) + if (argv[i][0] != '-' && argv[i][0] != '+') + loadvars(argv[i]); /* load variables from file */ + + if (i >= argc || argv[i][0] == '+') + loadholo(); /* load existing holodeck */ + + while (++i < argc) /* get command line settings */ + if (setvariable(argv[i], matchvar) < 0) { + sprintf(errmsg, "unknown variable: %s", argv[i]); + error(USER, errmsg); + } + /* check settings */ + checkvalues(); + /* load rad input file */ + getradfile(); + + if (hdlist[0] == NULL) { /* create new holodeck */ + HDGRID hdg[HDMAX]; /* set defaults */ - setdefaults(&hdg); + setdefaults(hdg); /* holodeck exists? */ if (!force && access(hdkfile, R_OK|W_OK) == 0) error(USER, "holodeck file exists -- use -f to overwrite"); /* create holodeck */ - creatholo(&hdg); - } else { /* else load holodeck */ - loadholo(); - if (vdef(RIF)) /* load RIF if any */ - getradfile(vval(RIF)); - } + creatholo(hdg); + } else /* else just set defaults */ + setdefaults(NULL); /* initialize */ initrholo(); - /* run */ + /* main loop */ while (rholo()) ; /* done */ 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][-f] output.hdk [control.hif|+|- [VAR=val ..]]\n", progname); quit(1); } +onsig(signo) /* fatal signal */ +int signo; +{ + static int gotsig = 0; + + if (gotsig++) /* two signals and we're gone! */ + _exit(signo); + + alarm(60); /* allow 60 seconds to clean up */ + signal(SIGALRM, SIG_DFL); /* make certain we do die */ + eputs("signal - "); + eputs(sigerr[signo]); + eputs("\n"); + quit(3); +} + + +sigdie(signo, msg) /* set fatal signal */ +int signo; +char *msg; +{ + if (signal(signo, onsig) == SIG_IGN) + signal(signo, SIG_IGN); + sigerr[signo] = msg; +} + + +int +resfmode(fd, mod) /* restrict open file access mode */ +int fd, mod; +{ + struct stat stbuf; + /* get original mode */ + if (fstat(fd, &stbuf) < 0) + error(SYSTEM, "cannot stat open holodeck file"); + mod &= stbuf.st_mode; /* always more restrictive */ + if (mod == (stbuf.st_mode & 0777)) + return(-1); /* already set */ + /* else change it */ + if (fchmod(fd, mod) < 0) { + error(WARNING, "cannot change holodeck file access mode"); + return(-1); + } + return(stbuf.st_mode); /* return original mode */ +} + + initrholo() /* get our holodeck running */ { extern int global_packet(); register int i; + /* close holodeck on exec() */ + fcntl(hdlist[0]->fd, F_SETFD, FD_CLOEXEC); if (outdev != NULL) /* open output device */ disp_open(outdev); else if (ncprocs > 0) /* else use global ray feed */ init_global(); - /* record end time */ - if (!vdef(TIME) || vflt(TIME) <= FTINY) - endtime = 0; + /* record disk space limit */ + if (!vdef(DISKSPACE)) + maxdisk = 0; else - endtime = starttime + vflt(TIME)*3600.; + maxdisk = 1024.*1024.*vflt(DISKSPACE); /* set up memory cache */ if (outdev == NULL) hdcachesize = 0; /* manual flushing */ @@ -148,11 +213,18 @@ 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"); + error(USER, "cannot start rtrace process(es)"); if (vdef(REPORT)) { /* make first report */ printargs(rtargc, rtargv, stderr); report(0); @@ -163,7 +235,7 @@ initrholo() /* get our holodeck running */ goto memerr; freepacks[--i].nr = 0; freepacks[i].next = NULL; - if (!vbool(OBSTRUCTIONS)) { + if (!vdef(OBSTRUCTIONS) || !vbool(OBSTRUCTIONS)) { freepacks[i].offset = (float *)bmalloc( RPACKSIZ*sizeof(float)*(i+1) ); if (freepacks[i].offset == NULL) @@ -177,6 +249,18 @@ initrholo() /* get our holodeck running */ freepacks[i].next = &freepacks[i+1]; } } + /* set up signal handling */ + sigdie(SIGINT, "Interrupt"); + sigdie(SIGHUP, "Hangup"); + sigdie(SIGTERM, "Terminate"); + sigdie(SIGPIPE, "Broken pipe"); + sigdie(SIGALRM, "Alarm clock"); +#ifdef SIGXCPU + sigdie(SIGXCPU, "CPU limit exceeded"); + sigdie(SIGXFSZ, "File size exceeded"); +#endif + /* protect holodeck file */ + orig_mode = resfmode(hdlist[0]->fd, ncprocs>0 ? 0 : 0444); return; memerr: error(SYSTEM, "out of memory in initrholo"); @@ -185,137 +269,125 @@ memerr: rholo() /* holodeck main loop */ { - static int idle = 1; + static int idle = 0; PACKET *pl = NULL, *plend; + int pksiz; register PACKET *p; time_t t; - long l; - - if (outdev != NULL) /* check display */ + /* check display */ + if (nprocs <= 0) + idle = 1; + if (outdev != NULL) { if (!disp_check(idle)) - return(0); - /* display only? */ - if (ncprocs <= 0) - return(1); + return(0); /* quit request */ + if (nprocs <= 0) + return(1); + } else if (idle) + return(0); /* all done */ /* check file size */ - if ((l = 1024.*1024.*vflt(DISKSPACE)) > 0 && - hdfiluse(hdlist[0]->fd, 0) + hdmemuse(0) >= l) - return(0); - /* check time */ - if (endtime > 0 || reporttime > 0) - t = time(NULL); - if (endtime > 0 && t >= endtime) - return(0); + if (maxdisk > 0 && hdfilen(hdlist[0]->fd) >= maxdisk) { + error(WARNING, "file limit exceeded"); + done_rtrace(); + return(1); /* comes back */ + } + t = time(NULL); /* check time */ + if (endtime > 0 && t >= endtime) { + error(WARNING, "time limit exceeded"); + done_rtrace(); + return(1); /* comes back */ + } if (reporttime > 0 && t >= reporttime) report(t); - /* get packets to process */ + /* figure out good packet size */ +#if MAXQTIME + pksiz = nraysdone*MAXQTIME/(totqlen*(t - starttime + 1L)); + if (pksiz < 1) + pksiz = 1; + else if (pksiz > RPACKSIZ) +#endif + pksiz = RPACKSIZ; + 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; } if (pl == NULL) pl = p; else plend->next = p; plend = p; } - idle = pl == NULL && freepacks != NULL; - /* are we out of stuff to do? */ - if (idle && outdev == NULL) - return(0); - /* else process packets */ + /* process packets */ done_packets(do_packets(pl)); return(1); /* and continue */ } -report(t) /* report progress so far */ -time_t t; -{ - if (t == 0) - t = time(NULL); - fprintf(stderr, "%s: %ld packets (%ld rays) done after %.2f hours\n", - progname, npacksdone, nraysdone, (t-starttime)/3600.); - fflush(stderr); - if (vdef(REPORT)) - reporttime = t + (time_t)(vflt(REPORT)*60.+.5); -} - - setdefaults(gp) /* set default values */ 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 (sscanf(vval(SECTION), - "%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf", - &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]) != 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)++; - } - if ((d = vflt(GRID)) <= FTINY) - badvalue(GRID); - for (i = 0; i < 3; i++) - gp->grid[i] = len[i]/d + (1.-FTINY); - if (!vdef(EXPOSURE)) { - sprintf(errmsg, "%s must be defined", vnam(EXPOSURE)); - error(USER, errmsg); - } - expval = vval(EXPOSURE)[0] == '-' || vval(EXPOSURE)[0] == '+' ? - pow(2., vflt(EXPOSURE)) : vflt(EXPOSURE); if (!vdef(OCTREE)) { if ((vval(OCTREE) = bmalloc(strlen(froot)+5)) == NULL) error(SYSTEM, "out of memory"); sprintf(vval(OCTREE), "%s.oct", froot); vdef(OCTREE)++; } - if (!vdef(DISKSPACE)) { - sprintf(errmsg, - "no %s setting, assuming 100 Mbytes available", - vnam(DISKSPACE)); - error(WARNING, errmsg); - vval(DISKSPACE) = "100"; - vdef(DISKSPACE)++; + if (!vdef(VDIST)) { + vval(VDIST) = "F"; + vdef(VDIST)++; } - if (!vdef(OBSTRUCTIONS)) { - vval(OBSTRUCTIONS) = "T"; - vdef(OBSTRUCTIONS)++; - } - if (!vdef(OCCUPANCY)) { - vval(OCCUPANCY) = "U"; - vdef(OCCUPANCY)++; - } /* append rendering options */ if (vdef(RENDER)) rtargc += wordstring(rtargv+rtargc, vval(RENDER)); + + if (gp == NULL) /* already initialized? */ + return; + /* set grid parameters */ + 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); + for (i = 0; i < 3; i++) + len[i] = VLEN(gp->xv[i]); + if (!vdef(GRID)) { + d = 2/5e5*( 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); + } } creatholo(gp) /* create a holodeck output file */ HDGRID *gp; { - long endloc = 0; + extern char VersionID[]; + int4 lastloc, nextloc; + int n; + int fd; FILE *fp; /* open & truncate file */ if ((fp = fopen(hdkfile, "w+")) == NULL) { @@ -324,14 +396,27 @@ HDGRID *gp; } /* write information header */ newheader("RADIANCE", fp); + fprintf(fp, "SOFTWARE= %s\n", VersionID); printvars(fp); fputformat(HOLOFMT, fp); fputc('\n', fp); - putw(HOLOMAGIC, fp); /* put magic number & terminus */ - fwrite(&endloc, sizeof(long), 1, fp); - fflush(fp); /* flush buffer */ - hdinit(fileno(fp), gp); /* allocate and initialize index */ - /* we're dropping fp here.... */ + putw(HOLOMAGIC, fp); /* put magic number */ + fd = dup(fileno(fp)); + fclose(fp); /* flush and close stdio stream */ + lastloc = lseek(fd, 0L, 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, (long)lastloc, 0) < 0) + error(SYSTEM, + "cannot seek on holodeck file in creatholo"); + write(fd, (char *)&nextloc, sizeof(nextloc)); + lseek(fd, (long)(lastloc=nextloc), 0); + } } @@ -361,12 +446,15 @@ char *s; loadholo() /* start loading a holodeck from fname */ { + extern long ftell(); FILE *fp; - long endloc; + int fd; + int n; + int4 nextloc; /* open holodeck file */ if ((fp = fopen(hdkfile, ncprocs>0 ? "r+" : "r")) == NULL) { - sprintf(errmsg, "cannot open \"%s\" for %s", hdkfile, - ncprocs>0 ? "appending" : "reading"); + sprintf(errmsg, "cannot %s \"%s\"", + ncprocs>0 ? "append" : "read", hdkfile); error(SYSTEM, errmsg); } /* load variables from header */ @@ -377,19 +465,26 @@ loadholo() /* start loading a holodeck from fname */ hdkfile); error(USER, errmsg); } - fread(&endloc, sizeof(long), 1, fp); - if (endloc != 0) - error(WARNING, "ignoring multiple sections in holodeck file"); - fseek(fp, 0L, 1); /* align system file pointer */ - hdinit(fileno(fp), NULL); /* allocate and load index */ - /* we're dropping fp here.... */ + nextloc = ftell(fp); /* get stdio position */ + fd = dup(fileno(fp)); + fclose(fp); /* done with stdio */ + for (n = 0; nextloc > 0L; n++) { /* initialize each section */ + lseek(fd, (long)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); + } } done_packets(pl) /* handle finished packets */ PACKET *pl; { - static int nunflushed = 0; + static int n2flush = 0; register PACKET *p; while (pl != NULL) { @@ -400,76 +495,29 @@ PACKET *pl; p->nr*sizeof(RAYVAL)); if (outdev != NULL) /* display it */ disp_packet((PACKHEAD *)p); - else - nunflushed += p->nr; + if (hdcachesize <= 0) /* manual flushing */ + 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 (nunflushed >= 256*RPACKSIZ) { + if (n2flush > 300/MAXQTIME*totqlen) { hdflush(NULL); /* flush holodeck buffers */ - nunflushed = 0; + n2flush = 0; } } -getradfile(rfargs) /* run rad and get needed variables */ -char *rfargs; -{ - static short mvar[] = {VIEW,OCTREE,EXPOSURE,-1}; - static char tf1[] = TEMPLATE; - char tf2[64]; - char combuf[256]; - char *pippt; - register int i; - register char *cp; - /* create rad command */ - mktemp(tf1); - sprintf(tf2, "%s.rif", tf1); - sprintf(combuf, - "rad -v 0 -s -e -w %s OPTFILE=%s | egrep '^[ \t]*(NOMATCH", - rfargs, tf1); - cp = combuf; - while (*cp){ - if (*cp == '|') pippt = cp; - cp++; - } /* match unset variables */ - for (i = 0; mvar[i] >= 0; i++) - if (!vdef(mvar[i])) { - *cp++ = '|'; - strcpy(cp, vnam(mvar[i])); - while (*cp) cp++; - pippt = NULL; - } - if (pippt != NULL) - strcpy(pippt, "> /dev/null"); /* nothing to match */ - else - sprintf(cp, ")[ \t]*=' > %s", tf2); - if (system(combuf)) { - error(SYSTEM, "cannot execute rad command"); - unlink(tf2); /* clean up */ - unlink(tf1); - quit(1); - } - if (pippt == NULL) { - loadvars(tf2); /* load variables */ - unlink(tf2); - } - rtargc += wordfile(rtargv+rtargc, tf1); /* get rtrace options */ - unlink(tf1); /* clean up */ -} - - rootname(rn, fn) /* remove tail from end of fn */ register char *rn, *fn; { char *tp, *dp; for (tp = NULL, dp = rn; *rn = *fn++; rn++) - if (ISDIRSEP(*rn)) + if (*rn == '/') dp = rn; else if (*rn == '.') tp = rn; @@ -518,25 +566,22 @@ int ec; { int status = 0; - if (outdev != NULL) /* close display */ - disp_close(); - if (hdlist[0] != NULL) { /* flush holodeck */ - if (ncprocs > 0) { - done_packets(flush_queue()); - status = end_rtrace(); /* close rtrace */ - hdflush(NULL); - if (vdef(REPORT)) { - long fsiz, fuse; - report(0); - fsiz = lseek(hdlist[0]->fd, 0L, 2); - fuse = hdfiluse(hdlist[0]->fd, 1); - fprintf(stderr, + if (hdlist[0] != NULL) { /* close holodeck */ + if (nprocs > 0) + status = done_rtrace(); /* calls hdsync() */ + if (ncprocs > 0 && vdef(REPORT)) { + long fsiz, fuse; + fsiz = hdfilen(hdlist[0]->fd); + fuse = hdfiluse(hdlist[0]->fd, 1); + fprintf(stderr, "%s: %.1f Mbyte holodeck file, %.1f%% fragmentation\n", - hdkfile, fsiz/(1024.*1024.), - 100.*(fsiz-fuse)/fsiz); - } - } else - hdflush(NULL); + hdkfile, fsiz/(1024.*1024.), + 100.*(fsiz-fuse)/fsiz); + } } + if (orig_mode >= 0) /* reset holodeck access mode */ + fchmod(hdlist[0]->fd, orig_mode); + if (outdev != NULL) /* close display */ + disp_close(); exit(ec ? ec : status); /* exit */ }