5 |
|
* Gather rtrace output to compute contributions from particular sources |
6 |
|
*/ |
7 |
|
|
8 |
+ |
#include "standard.h" |
9 |
|
#include <ctype.h> |
9 |
– |
#include "rtio.h" |
10 |
– |
#include "rterror.h" |
10 |
|
#include "platform.h" |
11 |
|
#include "rtprocess.h" |
12 |
|
#include "selcall.h" |
63 |
|
int bsiz; /* ray tree buffer length */ |
64 |
|
char *buf; /* ray tree buffer */ |
65 |
|
int nbr; /* number of bytes from rtrace */ |
66 |
< |
}; /* rtrace process */ |
66 |
> |
}; /* rtrace process buffer */ |
67 |
|
|
68 |
|
/* rtrace command and defaults */ |
69 |
|
char *rtargv[256] = { "rtrace", "-dt", "0", "-dj", ".5", "-dr", "3", |
70 |
< |
"-ab", "1", "-ad", "512", }; |
70 |
> |
"-ab", "1", "-ad", "128", }; |
71 |
|
int rtargc = 11; |
72 |
|
/* overriding rtrace options */ |
73 |
|
char *myrtopts[] = { "-o~~TmWdp", "-h-", |
79 |
|
struct rtproc *rt_unproc = NULL; /* unprocessed ray trees */ |
80 |
|
|
81 |
|
char persistfn[] = "pfXXXXXX"; /* persist file name */ |
83 |
– |
char fmt[8]; /* rtrace i/o format */ |
82 |
|
|
83 |
|
int gargc; /* global argc */ |
84 |
|
char **gargv; /* global argv */ |
106 |
|
|
107 |
|
MODCONT *addmodifier(char *modn, char *outf, char *binv); |
108 |
|
|
111 |
– |
int done_rprocs(struct rtproc *rtp); |
109 |
|
void init(int np); |
110 |
< |
void tracecontribs(FILE *fp); |
110 |
> |
int done_rprocs(struct rtproc *rtp); |
111 |
> |
void trace_contribs(FILE *fp); |
112 |
|
struct rtproc *wait_rproc(void); |
113 |
|
struct rtproc *get_rproc(void); |
114 |
< |
void process_rays(struct rtproc *rtp); |
114 |
> |
void queue_raytree(struct rtproc *rtp); |
115 |
> |
void process_queue(void); |
116 |
|
|
117 |
|
void putcontrib(const DCOLOR cnt, FILE *fout); |
118 |
|
void add_contrib(const char *modn); |
158 |
|
int nprocs = 1; |
159 |
|
char *curout = NULL; |
160 |
|
char *binval = NULL; |
161 |
+ |
char fmt[8]; |
162 |
|
int i, j; |
163 |
|
/* global program name */ |
164 |
|
gargv = argv; |
188 |
|
case '\0': |
189 |
|
header = !header; |
190 |
|
continue; |
191 |
< |
case '+': case '1': case 'T': case 't': |
191 |
> |
case '+': case '1': |
192 |
> |
case 'T': case 't': |
193 |
> |
case 'Y': case 'y': |
194 |
|
header = 1; |
195 |
|
continue; |
196 |
< |
case '-': case '0': case 'F': case 'f': |
196 |
> |
case '-': case '0': |
197 |
> |
case 'F': case 'f': |
198 |
> |
case 'N': case 'n': |
199 |
|
header = 0; |
200 |
|
continue; |
201 |
|
} |
269 |
|
execv(rtpath, rtargv); |
270 |
|
perror(rtpath); /* execv() should not return */ |
271 |
|
exit(1); |
272 |
< |
} else if (nprocs > 1) { /* add persist file if parallel */ |
272 |
> |
} |
273 |
> |
if (nprocs > 1) { /* add persist file if parallel */ |
274 |
|
rtargv[rtargc++] = "-PP"; |
275 |
|
rtargv[rtargc++] = mktemp(persistfn); |
276 |
|
} |
284 |
|
rtargv[rtargc] = NULL; |
285 |
|
/* start rtrace & compute contributions */ |
286 |
|
init(nprocs); |
287 |
< |
tracecontribs(stdin); |
287 |
> |
trace_contribs(stdin); |
288 |
|
quit(0); |
289 |
|
} |
290 |
|
|
336 |
|
exit(status); /* flushes all output streams */ |
337 |
|
} |
338 |
|
|
339 |
< |
/* start rtrace and initialize buffers */ |
339 |
> |
/* start rtrace processes and initialize */ |
340 |
|
void |
341 |
|
init(int np) |
342 |
|
{ |
350 |
|
scompile("Dx=$1;Dy=$2;Dz=$3;", NULL, 0); |
351 |
|
scompile("Px=$4;Py=$5;Pz=$6;", NULL, 0); |
352 |
|
/* set up signal handling */ |
353 |
< |
#ifdef SIGPIPE /* not present on Windows */ |
353 |
> |
signal(SIGINT, quit); |
354 |
> |
#ifdef SIGHUP |
355 |
> |
signal(SIGHUP, quit); |
356 |
> |
#endif |
357 |
> |
#ifdef SIGTERM |
358 |
> |
signal(SIGTERM, quit); |
359 |
> |
#endif |
360 |
> |
#ifdef SIGPIPE |
361 |
|
signal(SIGPIPE, quit); |
362 |
|
#endif |
363 |
|
rtp = &rt0; /* start rtrace process(es) */ |
562 |
|
int |
563 |
|
getinp(char *buf, FILE *fp) |
564 |
|
{ |
565 |
+ |
char *cp; |
566 |
+ |
int i; |
567 |
+ |
|
568 |
|
switch (inpfmt) { |
569 |
|
case 'a': |
570 |
< |
if (fgets(buf, 128, fp) == NULL) |
571 |
< |
return 0; |
570 |
> |
cp = buf; /* make sure we get 6 floats */ |
571 |
> |
for (i = 0; i < 6; i++) { |
572 |
> |
if (fgetword(cp, buf+127-cp, fp) == NULL) |
573 |
> |
return 0; |
574 |
> |
if ((cp = fskip(cp)) == NULL || *cp) |
575 |
> |
return 0; |
576 |
> |
*cp++ = ' '; |
577 |
> |
} |
578 |
> |
getc(fp); /* get/put eol */ |
579 |
> |
*cp-- = '\0'; *cp = '\n'; |
580 |
|
return strlen(buf); |
581 |
|
case 'f': |
582 |
|
if (fread(buf, sizeof(float), 6, fp) < 6) |
702 |
|
} |
703 |
|
} |
704 |
|
|
705 |
< |
/* process (or save) ray tree produced by rtrace process */ |
705 |
> |
/* queue completed ray tree produced by rtrace process */ |
706 |
|
void |
707 |
< |
process_rays(struct rtproc *rtp) |
707 |
> |
queue_raytree(struct rtproc *rtp) |
708 |
|
{ |
709 |
< |
struct rtproc *rtu; |
710 |
< |
/* check if time to process it */ |
711 |
< |
if (rtp->raynum == lastdone+1) { |
709 |
> |
struct rtproc *rtu, *rtl = NULL; |
710 |
> |
/* insert following ray order */ |
711 |
> |
for (rtu = rt_unproc; rtu != NULL; rtu = (rtl=rtu)->next) |
712 |
> |
if (rtp->raynum < rtu->raynum) |
713 |
> |
break; |
714 |
> |
rtu = (struct rtproc *)malloc(sizeof(struct rtproc)); |
715 |
> |
if (rtu == NULL) |
716 |
> |
error(SYSTEM, "out of memory in queue_raytree"); |
717 |
> |
*rtu = *rtp; |
718 |
> |
if (rtl == NULL) { |
719 |
> |
rtu->next = rt_unproc; |
720 |
> |
rt_unproc = rtu; |
721 |
> |
} else { |
722 |
> |
rtu->next = rtl->next; |
723 |
> |
rtl->next = rtu; |
724 |
> |
} |
725 |
> |
rtp->raynum = 0; /* clear path for next ray tree */ |
726 |
> |
rtp->bsiz = 0; |
727 |
> |
rtp->buf = NULL; |
728 |
> |
rtp->nbr = 0; |
729 |
> |
} |
730 |
> |
|
731 |
> |
/* process completed ray trees from our queue */ |
732 |
> |
void |
733 |
> |
process_queue(void) |
734 |
> |
{ |
735 |
> |
char modname[128]; |
736 |
> |
/* ray-ordered queue */ |
737 |
> |
while (rt_unproc != NULL && rt_unproc->raynum == lastdone+1) { |
738 |
> |
struct rtproc *rtp = rt_unproc; |
739 |
|
int n = rtp->nbr; |
740 |
|
const char *cp = rtp->buf; |
691 |
– |
char modname[128]; |
741 |
|
while (n > 0) { /* process rays */ |
742 |
|
register char *mnp = modname; |
743 |
|
/* skip leading tabs */ |
761 |
|
} |
762 |
|
done_contrib(); /* sum up contributions & output */ |
763 |
|
lastdone = rtp->raynum; |
764 |
< |
free(rtp->buf); |
765 |
< |
/* catch up with unprocessed list */ |
766 |
< |
while (rt_unproc != NULL && rt_unproc->raynum == lastdone+1) { |
718 |
< |
process_rays(rt_unproc); |
719 |
< |
rt_unproc = (rtu=rt_unproc)->next; |
720 |
< |
free(rtu); |
721 |
< |
} |
722 |
< |
} else { /* else insert in unprocessed list */ |
723 |
< |
struct rtproc *rtl = NULL; |
724 |
< |
for (rtu = rt_unproc; rtu != NULL; rtu = (rtl=rtu)->next) |
725 |
< |
if (rtp->raynum < rtu->raynum) |
726 |
< |
break; |
727 |
< |
rtu = (struct rtproc *)malloc(sizeof(struct rtproc)); |
728 |
< |
if (rtu == NULL) |
729 |
< |
error(SYSTEM, "out of memory in process_rays"); |
730 |
< |
*rtu = *rtp; |
731 |
< |
if (rtl == NULL) { |
732 |
< |
rtu->next = rt_unproc; |
733 |
< |
rt_unproc = rtu; |
734 |
< |
} else { |
735 |
< |
rtu->next = rtl->next; |
736 |
< |
rtl->next = rtu; |
737 |
< |
} |
764 |
> |
free(rtp->buf); /* free up buffer space */ |
765 |
> |
rt_unproc = rtp->next; |
766 |
> |
free(rtp); /* done with this ray tree */ |
767 |
|
} |
739 |
– |
rtp->raynum = 0; /* clear path for next ray tree */ |
740 |
– |
rtp->bsiz = 0; |
741 |
– |
rtp->buf = NULL; |
742 |
– |
rtp->nbr = 0; |
768 |
|
} |
769 |
|
|
770 |
|
/* wait for rtrace process to finish with ray tree */ |
821 |
|
if (rt->nbr >= 4 && !memcmp(rt->buf+rt->nbr-4, |
822 |
|
"~\t~\t", 4)) { |
823 |
|
rt->nbr -= 4; /* elide terminator */ |
824 |
< |
process_rays(rt); |
824 |
> |
queue_raytree(rt); |
825 |
|
rtfree = rt; /* ready for next ray */ |
826 |
|
} |
827 |
|
} |
843 |
|
|
844 |
|
/* trace ray contributions (main loop) */ |
845 |
|
void |
846 |
< |
tracecontribs(FILE *fin) |
846 |
> |
trace_contribs(FILE *fin) |
847 |
|
{ |
848 |
|
char inpbuf[128]; |
849 |
|
int iblen; |
852 |
|
while ((iblen = getinp(inpbuf, fin)) > 0) { |
853 |
|
if (lastray+1 < lastray) { /* counter rollover? */ |
854 |
|
while (wait_rproc() != NULL) |
855 |
< |
; |
855 |
> |
process_queue(); |
856 |
|
lastdone = lastray = 0; |
857 |
|
} |
858 |
|
rtp = get_rproc(); /* get avail. rtrace process */ |
859 |
< |
rtp->raynum = ++lastray; /* assign this ray to it */ |
859 |
> |
rtp->raynum = ++lastray; /* assign ray to it */ |
860 |
|
writebuf(rtp->pd.w, inpbuf, iblen); |
861 |
|
if (!--raysleft) |
862 |
< |
break; /* explicit EOF */ |
862 |
> |
break; |
863 |
> |
process_queue(); /* catch up with results */ |
864 |
|
} |
865 |
|
while (wait_rproc() != NULL) /* process outstanding rays */ |
866 |
< |
; |
866 |
> |
process_queue(); |
867 |
|
if (raysleft > 0) |
868 |
|
error(USER, "unexpected EOF on input"); |
869 |
|
} |