--- ray/src/hd/rholo.c 1998/01/04 18:36:43 3.34 +++ ray/src/hd/rholo.c 1999/08/05 19:20:10 3.51 @@ -1,4 +1,4 @@ -/* Copyright (c) 1997 Silicon Graphics, Inc. */ +/* Copyright (c) 1999 Regents of the University of California */ #ifndef lint static char SCCSid[] = "$SunId$ SGI"; @@ -11,12 +11,22 @@ static char SCCSid[] = "$SunId$ SGI"; #include "rholo.h" #include "random.h" #include -#include #include +#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 */ @@ -26,15 +36,13 @@ 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 */ @@ -51,10 +59,12 @@ long nraysdone = 0L; /* number of rays done */ long npacksdone = 0L; /* number of packets done */ PACKET *freepacks; /* available packets */ -int avgqlen; /* average queue length when full */ +int totqlen; /* maximum queue length (number of packets) */ char *sigerr[NSIG]; /* signal error messages */ +extern int nowarn; /* turn warnings off? */ + extern time_t time(); @@ -72,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; @@ -109,14 +122,16 @@ char *argv[]; } /* check settings */ checkvalues(); - /* load RIF if rtrace */ - if (ncprocs) - getradfile(); + /* load rad input file */ + getradfile(); if (hdlist[0] == NULL) { /* create new holodeck */ 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, @@ -134,7 +149,7 @@ 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); } @@ -145,11 +160,13 @@ 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"); @@ -176,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) { @@ -205,7 +222,7 @@ initrholo() /* get our holodeck running */ 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 */ @@ -223,7 +240,7 @@ initrholo() /* get our holodeck running */ 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 */ @@ -234,8 +251,6 @@ initrholo() /* get our holodeck running */ freepacks = (PACKET *)bmalloc(i*sizeof(PACKET)); if (freepacks == NULL) goto memerr; - if (!(avgqlen = i/nprocs)) /* record mean queue length */ - avgqlen = 1; freepacks[--i].nr = 0; freepacks[i].next = NULL; if (!vdef(OBSTRUCTIONS) || !vbool(OBSTRUCTIONS)) { @@ -262,7 +277,7 @@ initrholo() /* get our holodeck running */ sigdie(SIGXCPU, "CPU limit exceeded"); sigdie(SIGXFSZ, "File size exceeded"); #endif - /* protect holodeck file */ + /* protect holodeck file */ orig_mode = resfmode(hdlist[0]->fd, ncprocs>0 ? 0 : 0444); return; memerr: @@ -272,8 +287,10 @@ memerr: rholo() /* holodeck main loop */ { + static long nextfragwarn = 100*(1L<<20); static int idle = 0; PACKET *pl = NULL, *plend; + long fsiz; int pksiz; register PACKET *p; time_t t; @@ -287,12 +304,21 @@ rholo() /* holodeck main loop */ return(1); } else if (idle) return(0); /* all done */ - /* check file size */ - if (maxdisk > 0 && hdfilen(hdlist[0]->fd) >= maxdisk) { + fsiz = hdfilen(hdlist[0]->fd); /* check file size */ + if (maxdisk > 0 && fsiz >= maxdisk) { error(WARNING, "file limit exceeded"); done_rtrace(); return(1); /* comes back */ } +#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"); @@ -302,13 +328,14 @@ rholo() /* holodeck main loop */ if (reporttime > 0 && t >= reporttime) report(t); /* figure out good packet size */ + pksiz = RPACKSIZ; #if MAXQTIME - pksiz = nraysdone*MAXQTIME/(avgqlen*(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; @@ -333,7 +360,7 @@ register HDGRID *gp; extern char *atos(); register int i; int n; - double len[3], maxlen, d; + double len[3], d; char buf[64]; if (!vdef(SECTION)) { @@ -367,13 +394,15 @@ register HDGRID *gp; &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)) - d = 0.125*maxlen; - else if ((d = vflt(GRID)) <= FTINY) + 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) @@ -421,6 +450,7 @@ HDGRID *gp; } +int headline(s) /* process information header line */ char *s; { @@ -434,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 == '#') { @@ -442,6 +472,7 @@ char *s; break; } setvariable(s, matchvar); /* don't flag errors */ + return(0); } @@ -452,11 +483,23 @@ loadholo() /* start loading a holodeck from fname */ 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"); - error(SYSTEM, errmsg); + + 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); @@ -496,7 +539,7 @@ PACKET *pl; 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++; @@ -505,8 +548,11 @@ PACKET *pl; p->next = freepacks; /* push onto free list */ freepacks = p; } - if (n2flush > 300/MAXQTIME*avgqlen*nprocs) { - hdflush(NULL); /* flush holodeck buffers */ + if (n2flush >= RTFLUSH) { + if (outdev != NULL) + hdsync(NULL, 1); + else + hdflush(NULL); n2flush = 0; } } @@ -554,14 +600,6 @@ register char *s; } -wputs(s) /* put warning string to stderr */ -char *s; -{ - if (!nowarn) - eputs(s); -} - - quit(ec) /* exit program gracefully */ int ec; { @@ -570,7 +608,7 @@ int ec; if (hdlist[0] != NULL) { /* close holodeck */ if (nprocs > 0) status = done_rtrace(); /* calls hdsync() */ - if (ncprocs > 0 && vdef(REPORT)) { + if (ncprocs > 0 & force >= 0 && vdef(REPORT)) { long fsiz, fuse; fsiz = hdfilen(hdlist[0]->fd); fuse = hdfiluse(hdlist[0]->fd, 1);