--- ray/src/util/rtcontrib.c 2005/06/11 15:01:03 1.21 +++ ray/src/util/rtcontrib.c 2005/10/07 03:45:15 1.33 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: rtcontrib.c,v 1.21 2005/06/11 15:01:03 greg Exp $"; +static const char RCSid[] = "$Id: rtcontrib.c,v 1.33 2005/10/07 03:45:15 greg Exp $"; #endif /* * Gather rtrace output to compute contributions from particular sources @@ -25,9 +25,9 @@ int treebufsiz = BUFSIZ; /* current tree buffer size typedef double DCOLOR[3]; /* double-precision color */ /* - * The modcont structure is used to accumulate ray contributions + * The MODCONT structure is used to accumulate ray contributions * for a particular modifier, which may be subdivided into bins - * if binv is non-NULL. If outspec contains a %s in it, this will + * if binv is non-zero. If outspec contains a %s in it, this will * be replaced with the modifier name. If outspec contains a %d in it, * this will be used to create one output file per bin, otherwise all bins * will be written to the same file, in order. If the global outfmt @@ -46,17 +46,42 @@ static void mcfree(void *p) { epfree((*(MODCONT *)p).b LUTAB modconttab = LU_SINIT(NULL,mcfree); /* modifier lookup table */ -/* close output stream */ -static void closefile(void *p) { fclose((FILE *)p); } +#define CNT_UNKNOWN 0 /* unknown record length */ +#define CNT_PIPE (-1) /* output to a pipe */ +/* + * The STREAMOUT structure holds an open FILE pointer and a count of + * the number of RGB triplets per record, or CNT_UNKNOWN (0) if + * unknown or CNT_PIPE (-1) if writing to a command. + */ +typedef struct { + FILE *ofp; /* output file pointer */ + int reclen; /* triplets/record */ +} STREAMOUT; -LUTAB ofiletab = LU_SINIT(free,closefile); /* output file table */ +/* close output stream and free record */ +static void +closestream(void *p) +{ + STREAMOUT *sop = (STREAMOUT *)p; + int status; + if (sop->reclen == CNT_PIPE) + status = pclose(sop->ofp); + else + status = fclose(sop->ofp); + if (status) + error(SYSTEM, "error closing output stream"); + free(p); +} +LUTAB ofiletab = LU_SINIT(free,closestream); /* output file table */ + #define OF_MODIFIER 01 #define OF_BIN 02 -FILE *getofile(const char *ospec, const char *mname, int bn); +STREAMOUT *getostream(const char *ospec, const char *mname, int bn, int noopen); int ofname(char *oname, const char *ospec, const char *mname, int bn); void printheader(FILE *fout, const char *info); +void printresolu(FILE *fout); /* * The rcont structure is used to manage i/o with a particular @@ -112,9 +137,12 @@ int using_stdout = 0; /* are we using stdout? */ const char *modname[MAXMODLIST]; /* ordered modifier name list */ int nmods = 0; /* number of modifiers */ -MODCONT *addmodifier(char *modn, char *outf, char *binv); -void addmodfile(char *fname, char *outf, char *binv); +#define queue_length() (lastray - lastdone) +MODCONT *growmodifier(MODCONT *mp, int nb); +MODCONT *addmodifier(char *modn, char *outf, char *binv, int bincnt); +void addmodfile(char *fname, char *outf, char *binv, int bincnt); + void init(int np); int done_rprocs(struct rtproc *rtp); void recover_output(FILE *fin); @@ -128,6 +156,18 @@ void put_contrib(const DCOLOR cnt, FILE *fout); void add_contrib(const char *modn); void done_contrib(void); +/* return number of open rtrace processes */ +static int +nrtprocs(void) +{ + int nrtp = 0; + struct rtproc *rtp; + + for (rtp = &rt0; rtp != NULL; rtp = rtp->next) + nrtp += rtp->pd.running; + return(nrtp); +} + /* set input/output format */ static void setformat(const char *fmt) @@ -171,8 +211,16 @@ main(int argc, char *argv[]) int recover = 0; char *curout = NULL; char *binval = NULL; + int bincnt = 0; char fmt[8]; int i, j; + /* need at least one argument */ + if (argc < 2) { + fprintf(stderr, +"Usage: %s [-n nprocs][-r][-e expr][-f source][-o ospec][-b binv] {-m mod | -M file} [rtrace options] octree\n", + argv[0]); + exit(1); + } /* global program name */ gargv = argv; /* initialize calcomp routines */ @@ -251,21 +299,26 @@ main(int argc, char *argv[]) if (argv[i][2] || i >= argc-2) break; yres = atoi(argv[++i]); continue; - case 'b': /* bin expression */ - if (argv[i][2] || i >= argc-2) break; + case 'b': /* bin expression/count */ + if (i >= argc-2) break; + if (argv[i][2] == 'n') { + bincnt = atoi(argv[++i]); + continue; + } + if (argv[i][2]) break; binval = argv[++i]; continue; case 'm': /* modifier name */ if (argv[i][2] || i >= argc-2) break; rtargv[rtargc++] = "-ti"; rtargv[rtargc++] = argv[++i]; - addmodifier(argv[i], curout, binval); + addmodifier(argv[i], curout, binval, bincnt); continue; case 'M': /* modifier file */ if (argv[i][2] || i >= argc-2) break; rtargv[rtargc++] = "-tI"; rtargv[rtargc++] = argv[++i]; - addmodfile(argv[i], curout, binval); + addmodfile(argv[i], curout, binval, bincnt); continue; } rtargv[rtargc++] = argv[i]; /* assume rtrace option */ @@ -315,20 +368,23 @@ main(int argc, char *argv[]) error(USER, "missing octree argument"); rtargv[rtargc++] = octree = argv[i]; rtargv[rtargc] = NULL; - /* start rtrace & compute contributions */ + /* start rtrace */ init(nprocs); if (recover) /* perform recovery if requested */ recover_output(stdin); - trace_contribs(stdin); + trace_contribs(stdin); /* compute contributions */ quit(0); } +#ifndef SIGALRM +#define SIGALRM SIGTERM +#endif /* kill persistent rtrace process */ static void killpersist(void) { FILE *fp = fopen(persistfn, "r"); - int pid; + RT_PID pid; if (fp == NULL) return; @@ -432,12 +488,27 @@ init(int np) waitflush = xres; } +/* grow modifier to accommodate more bins */ +MODCONT * +growmodifier(MODCONT *mp, int nb) +{ + if (nb <= mp->nbins) + return mp; + mp = (MODCONT *)realloc(mp, sizeof(MODCONT) + sizeof(DCOLOR)*(nb-1)); + if (mp == NULL) + error(SYSTEM, "out of memory in growmodifier"); + memset(mp->cbin+mp->nbins, 0, sizeof(DCOLOR)*(nb-mp->nbins)); + mp->nbins = nb; + return mp; +} + /* add modifier to our list to track */ MODCONT * -addmodifier(char *modn, char *outf, char *binv) +addmodifier(char *modn, char *outf, char *binv, int bincnt) { LUENT *lep = lu_find(&modconttab, modn); MODCONT *mp; + int i; if (lep->data != NULL) { sprintf(errmsg, "duplicate modifier '%s'", modn); @@ -450,7 +521,6 @@ addmodifier(char *modn, char *outf, char *binv) mp = (MODCONT *)malloc(sizeof(MODCONT)); if (mp == NULL) error(SYSTEM, "out of memory in addmodifier"); - lep->data = (char *)mp; mp->outspec = outf; /* XXX assumes static string */ mp->modname = modn; /* XXX assumes static string */ if (binv != NULL) @@ -459,12 +529,20 @@ addmodifier(char *modn, char *outf, char *binv) mp->binv = eparse("0"); mp->nbins = 1; setcolor(mp->cbin[0], 0., 0., 0.); + if (mp->binv->type == NUM) /* assume one bin if constant */ + bincnt = 1; + else if (bincnt > 1) + mp = growmodifier(mp, bincnt); + lep->data = (char *)mp; + /* allocate output streams */ + for (i = outf==NULL || outf[0]=='!' ? 0 : bincnt; i--; ) + getostream(mp->outspec, mp->modname, i, 1); return mp; } /* add modifiers from a file list */ void -addmodfile(char *fname, char *outf, char *binv) +addmodfile(char *fname, char *outf, char *binv, int bincnt) { char *mname[MAXMODLIST]; int i; @@ -474,7 +552,7 @@ addmodfile(char *fname, char *outf, char *binv) error(SYSTEM, errmsg); } for (i = 0; mname[i]; i++) /* add each one */ - addmodifier(mname[i], outf, binv); + addmodifier(mname[i], outf, binv, bincnt); } /* put string to stderr */ @@ -574,6 +652,12 @@ printheader(FILE *fout, const char *info) break; } fputc('\n', fout); +} + +/* write resolution string to given output stream */ +void +printresolu(FILE *fout) +{ if (xres > 0) { if (yres > 0) /* resolution string */ fprtresolu(xres, yres, fout); @@ -581,23 +665,29 @@ printheader(FILE *fout, const char *info) } } -/* Get output file pointer (open and write header if new) */ -FILE * -getofile(const char *ospec, const char *mname, int bn) +/* Get output stream pointer (open and write header if new and noopen==0) */ +STREAMOUT * +getostream(const char *ospec, const char *mname, int bn, int noopen) { - int ofl; - char oname[1024]; - LUENT *lep; + static STREAMOUT stdos; + int ofl; + char oname[1024]; + LUENT *lep; + STREAMOUT *sop; if (ospec == NULL) { /* use stdout? */ - if (!using_stdout) { + if (!noopen && !using_stdout) { + stdos.reclen = 0; if (outfmt != 'a') SET_FILE_BINARY(stdout); if (header) printheader(stdout, NULL); + printresolu(stdout); + using_stdout = 1; } - using_stdout = 1; - return stdout; + stdos.ofp = stdout; + stdos.reclen += noopen; + return &stdos; } ofl = ofname(oname, ospec, mname, bn); /* get output name */ if (ofl < 0) { @@ -607,23 +697,31 @@ getofile(const char *ospec, const char *mname, int bn) lep = lu_find(&ofiletab, oname); /* look it up */ if (lep->key == NULL) /* new entry */ lep->key = strcpy((char *)malloc(strlen(oname)+1), oname); - if (lep->data == NULL) { /* open output file */ - FILE *fp; + sop = (STREAMOUT *)lep->data; + if (sop == NULL) { /* allocate stream */ + sop = (STREAMOUT *)malloc(sizeof(STREAMOUT)); + if (sop == NULL) + error(SYSTEM, "out of memory in getostream"); + sop->reclen = oname[0] == '!' ? CNT_PIPE : 0; + sop->ofp = NULL; /* open iff noopen==0 */ + lep->data = (char *)sop; + } + if (!noopen && sop->ofp == NULL) { /* open output stream */ int i; if (oname[0] == '!') /* output to command */ - fp = popen(oname+1, "w"); + sop->ofp = popen(oname+1, "w"); else - fp = fopen(oname, "w"); - if (fp == NULL) { + sop->ofp = fopen(oname, "w"); + if (sop->ofp == NULL) { sprintf(errmsg, "cannot open '%s' for writing", oname); error(SYSTEM, errmsg); } if (outfmt != 'a') - SET_FILE_BINARY(fp); + SET_FILE_BINARY(sop->ofp); if (header) { char info[512]; char *cp = info; - if (ofl & OF_MODIFIER) { + if (ofl & OF_MODIFIER || sop->reclen == 1) { sprintf(cp, "MODIFIER=%s\n", mname); while (*cp) ++cp; } @@ -632,20 +730,22 @@ getofile(const char *ospec, const char *mname, int bn) while (*cp) ++cp; } *cp = '\0'; - printheader(fp, info); + printheader(sop->ofp, info); } + printresolu(sop->ofp); /* play catch-up */ for (i = 0; i < lastdone; i++) { static const DCOLOR nocontrib = BLKCOLOR; - put_contrib(nocontrib, fp); + put_contrib(nocontrib, sop->ofp); if (outfmt == 'a') - putc('\n', fp); + putc('\n', sop->ofp); } if (xres > 0) - fflush(fp); - lep->data = (char *)fp; + fflush(sop->ofp); } - return (FILE *)lep->data; /* return open file pointer */ + if (sop->reclen != CNT_PIPE) /* add to length if noopen */ + sop->reclen += noopen; + return sop; /* return open stream */ } /* read input ray into buffer */ @@ -708,15 +808,8 @@ add_contrib(const char *modn) bn = (int)(evalue(mp->binv) + .5); if (bn <= 0) bn = 0; - else if (bn > mp->nbins) { /* new bin */ - mp = (MODCONT *)realloc(mp, sizeof(MODCONT) + - bn*sizeof(DCOLOR)); - if (mp == NULL) - error(SYSTEM, "out of memory in add_contrib"); - memset(mp->cbin+mp->nbins, 0, sizeof(DCOLOR)*(bn+1-mp->nbins)); - mp->nbins = bn+1; - le->data = (char *)mp; - } + else if (bn >= mp->nbins) /* new bin */ + le->data = (char *)(mp = growmodifier(mp, bn+1)); addcolor(mp->cbin[bn], rparams); } @@ -724,13 +817,13 @@ add_contrib(const char *modn) static int puteol(const LUENT *e, void *p) { - FILE *fp = (FILE *)e->data; + STREAMOUT *sop = (STREAMOUT *)e->data; if (outfmt == 'a') - putc('\n', fp); + putc('\n', sop->ofp); if (!waitflush) - fflush(fp); - if (ferror(fp)) { + fflush(sop->ofp); + if (ferror(sop->ofp)) { sprintf(errmsg, "write error on file '%s'", e->key); error(SYSTEM, errmsg); } @@ -770,22 +863,22 @@ put_contrib(const DCOLOR cnt, FILE *fout) void done_contrib(void) { - int i, j; - MODCONT *mp; - FILE *fp; + int i, j; + MODCONT *mp; + STREAMOUT *sop; /* output modifiers in order */ for (i = 0; i < nmods; i++) { mp = (MODCONT *)lu_find(&modconttab,modname[i])->data; - fp = getofile(mp->outspec, mp->modname, 0); - put_contrib(mp->cbin[0], fp); + sop = getostream(mp->outspec, mp->modname, 0,0); + put_contrib(mp->cbin[0], sop->ofp); if (mp->nbins > 3 && /* minor optimization */ - fp == getofile(mp->outspec, mp->modname, 1)) + sop == getostream(mp->outspec, mp->modname, 1,0)) for (j = 1; j < mp->nbins; j++) - put_contrib(mp->cbin[j], fp); + put_contrib(mp->cbin[j], sop->ofp); else for (j = 1; j < mp->nbins; j++) put_contrib(mp->cbin[j], - getofile(mp->outspec, mp->modname, j)); + getostream(mp->outspec,mp->modname,j,0)->ofp); /* clear for next ray tree */ memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins); } @@ -907,7 +1000,7 @@ wait_rproc(void) if (rt->bsiz + BUFSIZ <= treebufsiz) rt->bsiz = treebufsiz; else - rt->bsiz = treebufsiz += BUFSIZ; + treebufsiz = rt->bsiz += BUFSIZ; rt->buf = (char *)realloc(rt->buf, rt->bsiz); } if (rt->buf == NULL) @@ -948,7 +1041,8 @@ trace_contribs(FILE *fin) struct rtproc *rtp; /* loop over input */ while ((iblen = getinp(inpbuf, fin)) > 0) { - if (lastray+1 < lastray) { /* counter rollover? */ + if (lastray+1 < lastray || /* need reset? */ + queue_length() > 5*nrtprocs()) { while (wait_rproc() != NULL) process_queue(); lastdone = lastray = 0; @@ -971,7 +1065,12 @@ trace_contribs(FILE *fin) static int myseeko(const LUENT *e, void *p) { - if (fseeko((FILE *)e->data, *(off_t *)p, SEEK_CUR) < 0) { + STREAMOUT *sop = (STREAMOUT *)e->data; + off_t nbytes = *(off_t *)p; + + if (sop->reclen > 1) + nbytes = nbytes * sop->reclen; + if (fseeko(sop->ofp, nbytes, SEEK_CUR) < 0) { sprintf(errmsg, "seek error on file '%s'", e->key); error(SYSTEM, errmsg); } @@ -981,16 +1080,17 @@ myseeko(const LUENT *e, void *p) void recover_output(FILE *fin) { - off_t lastout = -1; - int outvsiz; - char *outvfmt; - int i, j; - MODCONT *mp; - int ofl; - char oname[1024]; - LUENT *ment, *oent; - FILE *fp; - off_t nvals; + off_t lastout = -1; + int outvsiz, recsiz; + char *outvfmt; + int i, j; + MODCONT *mp; + int ofl; + char oname[1024]; + LUENT *ment, *oent; + STREAMOUT sout; + off_t nvals; + int xr, yr; switch (outfmt) { case 'a': @@ -1018,54 +1118,73 @@ recover_output(FILE *fin) mp = (MODCONT *)ment->data; if (mp->outspec == NULL) error(USER, "cannot recover from stdout"); + if (mp->outspec[0] == '!') + error(USER, "cannot recover from command"); for (j = 0; ; j++) { /* check each bin's file */ ofl = ofname(oname, mp->outspec, mp->modname, j); if (ofl < 0) error(USER, "bad output file specification"); oent = lu_find(&ofiletab, oname); - if (oent->data != NULL) { /* saw this one already */ + if (oent->data != NULL) { + sout = *(STREAMOUT *)oent->data; + } else { + sout.reclen = 0; + sout.ofp = NULL; + } + if (sout.ofp != NULL) { /* already open? */ if (ofl & OF_BIN) continue; break; } - if (oname[0] == '!') - error(USER, "cannot recover from command"); /* open output */ - fp = fopen(oname, "rb+"); - if (fp == NULL) - break; /* must be end of modifier */ - nvals = lseek(fileno(fp), 0, SEEK_END); + sout.ofp = fopen(oname, "rb+"); + if (sout.ofp == NULL) { + if (j) + break; /* assume end of modifier */ + sprintf(errmsg, "missing recover file '%s'", + oname); + error(USER, errmsg); + } + nvals = lseek(fileno(sout.ofp), 0, SEEK_END); if (nvals <= 0) { lastout = 0; /* empty output, quit here */ - fclose(fp); + fclose(sout.ofp); break; } - lseek(fileno(fp), 0, SEEK_SET); - if (header) { - int xr, yr; - if (checkheader(fp, outvfmt, NULL) != 1) { + if (sout.reclen == CNT_UNKNOWN) { + if (!(ofl & OF_BIN)) { sprintf(errmsg, - "format mismatch for '%s'", + "need -bn to recover file '%s'", oname); error(USER, errmsg); } - if (xres > 0 && yres > 0 && - (!fscnresolu(&xr, &yr, fp) || - xr != xres || - yr != yres)) { - sprintf(errmsg, - "resolution mismatch for '%s'", - oname); - error(USER, errmsg); - } + recsiz = outvsiz; + } else + recsiz = outvsiz * sout.reclen; + + lseek(fileno(sout.ofp), 0, SEEK_SET); + if (header && checkheader(sout.ofp, outvfmt, NULL) != 1) { + sprintf(errmsg, "format mismatch for '%s'", + oname); + error(USER, errmsg); } - nvals = (nvals - (off_t)ftell(fp)) / outvsiz; - if (lastout < 0 || nvals < lastout) + if ((xres > 0) & (yres > 0) && + (!fscnresolu(&xr, &yr, sout.ofp) || + xr != xres || + yr != yres)) { + sprintf(errmsg, "resolution mismatch for '%s'", + oname); + error(USER, errmsg); + } + nvals = (nvals - (off_t)ftell(sout.ofp)) / recsiz; + if ((lastout < 0) | (nvals < lastout)) lastout = nvals; - if (oent->key == NULL) /* new entry */ + if (oent->key == NULL) /* new entry */ oent->key = strcpy((char *) malloc(strlen(oname)+1), oname); - oent->data = (char *)fp; + if (oent->data == NULL) + oent->data = (char *)malloc(sizeof(STREAMOUT)); + *(STREAMOUT *)oent->data = sout; if (!(ofl & OF_BIN)) break; /* no bin separation */ } @@ -1074,24 +1193,22 @@ recover_output(FILE *fin) lu_done(&ofiletab); /* reclose all outputs */ return; } - if (j > mp->nbins) { /* preallocate modifier bins */ - mp = (MODCONT *)realloc(mp, sizeof(MODCONT) + - (j-1)*sizeof(DCOLOR)); - if (mp == NULL) - error(SYSTEM, "out of memory in recover_output"); - memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins); - mp->nbins = j; - ment->data = (char *)mp; - } + if (j > mp->nbins) /* reallocate modifier bins */ + ment->data = (char *)(mp = growmodifier(mp, j)); } if (lastout < 0) { error(WARNING, "no output files to recover"); return; } + if (raysleft && lastout >= raysleft) { + error(WARNING, "output appears to be complete"); + /* XXX should read & discard input? */ + quit(0); + } /* seek on all files */ nvals = lastout * outvsiz; lu_doall(&ofiletab, myseeko, &nvals); - /* discard input */ + /* skip repeated input */ for (nvals = 0; nvals < lastout; nvals++) if (getinp(oname, fin) <= 0) error(USER, "unexpected EOF on input");