ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rholo.c
Revision: 3.44
Committed: Wed Dec 30 08:02:37 1998 UTC (25 years, 3 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 3.43: +16 -2 lines
Log Message:
added warning about excessive file fragmentation

File Contents

# User Rev Content
1 gregl 3.35 /* Copyright (c) 1998 Silicon Graphics, Inc. */
2 gregl 3.1
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ SGI";
5     #endif
6    
7     /*
8     * Radiance holodeck generation controller
9     */
10    
11     #include "rholo.h"
12 gregl 3.14 #include "random.h"
13 gregl 3.12 #include <signal.h>
14 gregl 3.1 #include <sys/types.h>
15 gregl 3.12 #include <sys/stat.h>
16 gregl 3.1
17 gwlarson 3.44 #ifndef FRAGWARN
18     #define FRAGWARN 20 /* fragmentation for warning (%) */
19     #endif
20 gregl 3.33 #ifndef MAXQTIME
21     #define MAXQTIME 5 /* target maximum seconds in queue */
22     #endif
23 gregl 3.1 /* the following must be consistent with rholo.h */
24     int NVARS = NRHVARS; /* total number of variables */
25    
26     VARIABLE vv[] = RHVINIT; /* variable-value pairs */
27    
28     char *progname; /* our program name */
29     char *hdkfile; /* holodeck file name */
30 gregl 3.22 char froot[256]; /* root file name */
31 gregl 3.1
32     int ncprocs = 0; /* desired number of compute processes */
33    
34     char *outdev = NULL; /* output device name */
35    
36 gregl 3.17 int readinp = 0; /* read commands from stdin */
37    
38 gregl 3.20 int force = 0; /* allow overwrite of holodeck */
39    
40 gregl 3.1 time_t starttime; /* time we got started */
41     time_t endtime; /* time we should end by */
42     time_t reporttime; /* time for next report */
43    
44 gregl 3.8 long maxdisk; /* maximum file space (bytes) */
45    
46 gregl 3.1 int rtargc = 1; /* rtrace command */
47     char *rtargv[128] = {"rtrace", NULL};
48    
49 gregl 3.12 int orig_mode = -1; /* original file mode (-1 if unchanged) */
50    
51 gregl 3.1 long nraysdone = 0L; /* number of rays done */
52     long npacksdone = 0L; /* number of packets done */
53    
54     PACKET *freepacks; /* available packets */
55 gwlarson 3.38 int totqlen; /* maximum queue length when full */
56 gregl 3.1
57 gregl 3.12 char *sigerr[NSIG]; /* signal error messages */
58    
59 gwlarson 3.40 extern int nowarn; /* turn warnings off? */
60    
61 gregl 3.1 extern time_t time();
62    
63    
64     main(argc, argv)
65     int argc;
66     char *argv[];
67     {
68     int i;
69 gregl 3.33
70 gregl 3.18 initurand(16384); /* initialize urand */
71 gregl 3.1 progname = argv[0]; /* get arguments */
72     for (i = 1; i < argc && argv[i][0] == '-'; i++)
73     switch (argv[i][1]) {
74     case 'w': /* turn off warnings */
75     nowarn++;
76     break;
77     case 'f': /* force overwrite */
78     force++;
79     break;
80 gregl 3.17 case 'i': /* read input from stdin */
81     readinp++;
82     break;
83 gregl 3.1 case 'n': /* compute processes */
84     if (i >= argc-2)
85     goto userr;
86     ncprocs = atoi(argv[++i]);
87     break;
88     case 'o': /* output display */
89     if (i >= argc-2)
90     goto userr;
91     outdev = argv[++i];
92     break;
93     default:
94     goto userr;
95     }
96     /* get root file name */
97 gregl 3.32 if (i >= argc)
98     goto userr;
99 gregl 3.1 rootname(froot, hdkfile=argv[i++]);
100 gregl 3.27 /* load variables? */
101     if (i < argc)
102     if (argv[i][0] != '-' && argv[i][0] != '+')
103     loadvars(argv[i]); /* load variables from file */
104    
105     if (i >= argc || argv[i][0] == '+')
106     loadholo(); /* load existing holodeck */
107    
108     while (++i < argc) /* get command line settings */
109     if (setvariable(argv[i], matchvar) < 0) {
110     sprintf(errmsg, "unknown variable: %s", argv[i]);
111     error(USER, errmsg);
112     }
113     /* check settings */
114     checkvalues();
115 gregl 3.36 /* load rad input file */
116     getradfile();
117 gregl 3.27
118 gregl 3.28 if (hdlist[0] == NULL) { /* create new holodeck */
119 gregl 3.29 HDGRID hdg[HDMAX];
120 gregl 3.1 /* set defaults */
121 gregl 3.29 setdefaults(hdg);
122 gregl 3.1 /* holodeck exists? */
123     if (!force && access(hdkfile, R_OK|W_OK) == 0)
124     error(USER,
125     "holodeck file exists -- use -f to overwrite");
126     /* create holodeck */
127 gregl 3.29 creatholo(hdg);
128 gregl 3.27 } else /* else just set defaults */
129 gregl 3.15 setdefaults(NULL);
130 gregl 3.1 /* initialize */
131     initrholo();
132 gregl 3.15 /* main loop */
133 gregl 3.1 while (rholo())
134     ;
135     /* done */
136     quit(0);
137     userr:
138     fprintf(stderr,
139 gregl 3.27 "Usage: %s [-n nprocs][-o disp][-w][-f] output.hdk [control.hif|+|- [VAR=val ..]]\n",
140 gregl 3.1 progname);
141     quit(1);
142     }
143    
144    
145 gregl 3.12 onsig(signo) /* fatal signal */
146     int signo;
147     {
148     static int gotsig = 0;
149    
150     if (gotsig++) /* two signals and we're gone! */
151     _exit(signo);
152    
153 gregl 3.28 alarm(60); /* allow 60 seconds to clean up */
154 gregl 3.12 signal(SIGALRM, SIG_DFL); /* make certain we do die */
155     eputs("signal - ");
156     eputs(sigerr[signo]);
157     eputs("\n");
158     quit(3);
159     }
160    
161    
162     sigdie(signo, msg) /* set fatal signal */
163     int signo;
164     char *msg;
165     {
166     if (signal(signo, onsig) == SIG_IGN)
167     signal(signo, SIG_IGN);
168     sigerr[signo] = msg;
169     }
170    
171    
172     int
173     resfmode(fd, mod) /* restrict open file access mode */
174     int fd, mod;
175     {
176     struct stat stbuf;
177     /* get original mode */
178     if (fstat(fd, &stbuf) < 0)
179     error(SYSTEM, "cannot stat open holodeck file");
180     mod &= stbuf.st_mode; /* always more restrictive */
181 gregl 3.35 if (mod == (stbuf.st_mode & 0777))
182 gregl 3.12 return(-1); /* already set */
183     /* else change it */
184     if (fchmod(fd, mod) < 0) {
185     error(WARNING, "cannot change holodeck file access mode");
186     return(-1);
187     }
188     return(stbuf.st_mode); /* return original mode */
189     }
190    
191    
192 gregl 3.1 initrholo() /* get our holodeck running */
193     {
194     extern int global_packet();
195     register int i;
196 gregl 3.17 /* close holodeck on exec() */
197     fcntl(hdlist[0]->fd, F_SETFD, FD_CLOEXEC);
198 gregl 3.4
199     if (outdev != NULL) /* open output device */
200     disp_open(outdev);
201 gregl 3.1 else if (ncprocs > 0) /* else use global ray feed */
202     init_global();
203 gregl 3.8 /* record disk space limit */
204     if (!vdef(DISKSPACE))
205     maxdisk = 0;
206     else
207 gregl 3.9 maxdisk = 1024.*1024.*vflt(DISKSPACE);
208 gregl 3.1 /* set up memory cache */
209 gregl 3.5 if (outdev == NULL)
210     hdcachesize = 0; /* manual flushing */
211     else if (vdef(CACHE))
212     hdcachesize = 1024.*1024.*vflt(CACHE);
213 gregl 3.1 /* open report file */
214     if (vdef(REPORT)) {
215     register char *s = sskip2(vval(REPORT), 1);
216     if (*s && freopen(s, "a", stderr) == NULL)
217     quit(2);
218     }
219 gregl 3.33 /* mark the starting time */
220     starttime = time(NULL);
221     /* compute end time */
222     if (!vdef(TIME) || vflt(TIME) <= FTINY)
223     endtime = 0;
224     else
225     endtime = starttime + vflt(TIME)*3600. + .5;
226 gregl 3.1 /* start rtrace */
227     if (ncprocs > 0) {
228 gwlarson 3.38 totqlen = i = start_rtrace();
229 gregl 3.1 if (i < 1)
230 gregl 3.20 error(USER, "cannot start rtrace process(es)");
231 gregl 3.1 if (vdef(REPORT)) { /* make first report */
232     printargs(rtargc, rtargv, stderr);
233     report(0);
234     }
235     /* allocate packets */
236     freepacks = (PACKET *)bmalloc(i*sizeof(PACKET));
237     if (freepacks == NULL)
238     goto memerr;
239     freepacks[--i].nr = 0;
240     freepacks[i].next = NULL;
241 gregl 3.25 if (!vdef(OBSTRUCTIONS) || !vbool(OBSTRUCTIONS)) {
242 gregl 3.1 freepacks[i].offset = (float *)bmalloc(
243     RPACKSIZ*sizeof(float)*(i+1) );
244     if (freepacks[i].offset == NULL)
245     goto memerr;
246     } else
247     freepacks[i].offset = NULL;
248     while (i--) {
249     freepacks[i].nr = 0;
250     freepacks[i].offset = freepacks[i+1].offset == NULL ?
251     NULL : freepacks[i+1].offset+RPACKSIZ ;
252     freepacks[i].next = &freepacks[i+1];
253     }
254     }
255 gregl 3.12 /* set up signal handling */
256     sigdie(SIGINT, "Interrupt");
257     sigdie(SIGHUP, "Hangup");
258     sigdie(SIGTERM, "Terminate");
259     sigdie(SIGPIPE, "Broken pipe");
260     sigdie(SIGALRM, "Alarm clock");
261     #ifdef SIGXCPU
262     sigdie(SIGXCPU, "CPU limit exceeded");
263     sigdie(SIGXFSZ, "File size exceeded");
264     #endif
265     /* protect holodeck file */
266     orig_mode = resfmode(hdlist[0]->fd, ncprocs>0 ? 0 : 0444);
267 gregl 3.1 return;
268     memerr:
269     error(SYSTEM, "out of memory in initrholo");
270     }
271    
272    
273     rholo() /* holodeck main loop */
274     {
275 gwlarson 3.44 static long nextfragwarn = 100*(1L<<20);
276 gregl 3.30 static int idle = 0;
277 gregl 3.1 PACKET *pl = NULL, *plend;
278 gwlarson 3.44 long fsiz;
279 gregl 3.33 int pksiz;
280 gregl 3.1 register PACKET *p;
281     time_t t;
282 gregl 3.30 /* check display */
283     if (nprocs <= 0)
284     idle = 1;
285     if (outdev != NULL) {
286 gregl 3.4 if (!disp_check(idle))
287 gregl 3.30 return(0); /* quit request */
288     if (nprocs <= 0)
289     return(1);
290     } else if (idle)
291     return(0); /* all done */
292 gwlarson 3.44 fsiz = hdfilen(hdlist[0]->fd); /* check file size */
293     if (maxdisk > 0 && fsiz >= maxdisk) {
294 gregl 3.8 error(WARNING, "file limit exceeded");
295 gregl 3.21 done_rtrace();
296     return(1); /* comes back */
297 gregl 3.8 }
298 gwlarson 3.44 #if FRAGWARN
299     if (fsiz >= nextfragwarn &&
300     (fsiz-hdfiluse(hdlist[0]->fd,0))/(fsiz/100) > FRAGWARN) {
301     sprintf(errmsg, "holodeck file fragmentation is %.0f%%",
302     100.*(fsiz-hdfiluse(hdlist[0]->fd,1))/fsiz);
303     error(WARNING, errmsg);
304     nextfragwarn = fsiz + (fsiz>>2); /* decent interval */
305     }
306     #endif
307 gregl 3.33 t = time(NULL); /* check time */
308 gregl 3.8 if (endtime > 0 && t >= endtime) {
309     error(WARNING, "time limit exceeded");
310 gregl 3.21 done_rtrace();
311     return(1); /* comes back */
312 gregl 3.8 }
313 gregl 3.3 if (reporttime > 0 && t >= reporttime)
314 gregl 3.1 report(t);
315 gregl 3.33 /* figure out good packet size */
316     #if MAXQTIME
317 gwlarson 3.38 pksiz = nraysdone*MAXQTIME/(totqlen*(t - starttime + 1L));
318 gregl 3.33 if (pksiz < 1)
319     pksiz = 1;
320     else if (pksiz > RPACKSIZ)
321     #endif
322     pksiz = RPACKSIZ;
323 gregl 3.28 idle = 0; /* get packets to process */
324 gregl 3.1 while (freepacks != NULL) {
325     p = freepacks; freepacks = p->next; p->next = NULL;
326 gregl 3.33 if (!next_packet(p, pksiz)) {
327 gregl 3.1 p->next = freepacks; freepacks = p;
328 gregl 3.21 idle = 1;
329 gregl 3.1 break;
330     }
331     if (pl == NULL) pl = p;
332     else plend->next = p;
333     plend = p;
334     }
335 gregl 3.21 /* process packets */
336 gregl 3.1 done_packets(do_packets(pl));
337     return(1); /* and continue */
338     }
339    
340    
341     setdefaults(gp) /* set default values */
342     register HDGRID *gp;
343     {
344     extern char *atos();
345     register int i;
346 gregl 3.29 int n;
347 gregl 3.37 double len[3], d;
348 gregl 3.1 char buf[64];
349    
350     if (!vdef(SECTION)) {
351     sprintf(errmsg, "%s must be defined", vnam(SECTION));
352     error(USER, errmsg);
353     }
354     if (!vdef(OCTREE)) {
355     if ((vval(OCTREE) = bmalloc(strlen(froot)+5)) == NULL)
356     error(SYSTEM, "out of memory");
357     sprintf(vval(OCTREE), "%s.oct", froot);
358     vdef(OCTREE)++;
359 gregl 3.14 }
360     if (!vdef(VDIST)) {
361     vval(VDIST) = "F";
362     vdef(VDIST)++;
363 gregl 3.1 }
364     /* append rendering options */
365     if (vdef(RENDER))
366     rtargc += wordstring(rtargv+rtargc, vval(RENDER));
367 gregl 3.15
368     if (gp == NULL) /* already initialized? */
369     return;
370     /* set grid parameters */
371 gregl 3.29 for (n = 0; n < vdef(SECTION); n++, gp++) {
372     gp->grid[0] = gp->grid[1] = gp->grid[2] = 0;
373     if (sscanf(nvalue(SECTION, n),
374 gregl 3.16 "%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %hd %hd %hd",
375 gregl 3.29 &gp->orig[0], &gp->orig[1], &gp->orig[2],
376     &gp->xv[0][0], &gp->xv[0][1], &gp->xv[0][2],
377     &gp->xv[1][0], &gp->xv[1][1], &gp->xv[1][2],
378     &gp->xv[2][0], &gp->xv[2][1], &gp->xv[2][2],
379     &gp->grid[0], &gp->grid[1], &gp->grid[2]) < 12)
380     badvalue(SECTION);
381     for (i = 0; i < 3; i++)
382 gregl 3.37 len[i] = VLEN(gp->xv[i]);
383     if (!vdef(GRID)) {
384 gwlarson 3.43 d = 2/2e5*( len[0]*len[0]*(len[1]*len[1] +
385 gregl 3.37 len[2]*len[2] + 4*len[1]*len[2])
386     + len[1]*len[1]*len[2]*(len[2] + 4*len[0])
387     + 4*len[0]*len[1]*len[2]*len[2] );
388     d = sqrt(sqrt(d));
389     } else if ((d = vflt(GRID)) <= FTINY)
390 gregl 3.29 badvalue(GRID);
391     for (i = 0; i < 3; i++)
392     if (gp->grid[i] <= 0)
393     gp->grid[i] = len[i]/d + (1.-FTINY);
394 gregl 3.15 }
395 gregl 3.1 }
396    
397    
398     creatholo(gp) /* create a holodeck output file */
399     HDGRID *gp;
400     {
401 gregl 3.12 extern char VersionID[];
402 gregl 3.29 int4 lastloc, nextloc;
403     int n;
404 gregl 3.10 int fd;
405 gregl 3.1 FILE *fp;
406     /* open & truncate file */
407     if ((fp = fopen(hdkfile, "w+")) == NULL) {
408     sprintf(errmsg, "cannot open \"%s\" for writing", hdkfile);
409     error(SYSTEM, errmsg);
410     }
411     /* write information header */
412     newheader("RADIANCE", fp);
413 gregl 3.12 fprintf(fp, "SOFTWARE= %s\n", VersionID);
414 gregl 3.1 printvars(fp);
415     fputformat(HOLOFMT, fp);
416     fputc('\n', fp);
417 gregl 3.29 putw(HOLOMAGIC, fp); /* put magic number */
418 gregl 3.10 fd = dup(fileno(fp));
419     fclose(fp); /* flush and close stdio stream */
420 gregl 3.29 lastloc = lseek(fd, 0L, 2);
421     for (n = vdef(SECTION); n--; gp++) { /* initialize each section */
422     nextloc = 0L;
423     write(fd, (char *)&nextloc, sizeof(nextloc));
424     hdinit(fd, gp); /* writes beam index */
425     if (!n)
426     break;
427     nextloc = hdfilen(fd); /* write section pointer */
428     if (lseek(fd, (long)lastloc, 0) < 0)
429     error(SYSTEM,
430     "cannot seek on holodeck file in creatholo");
431     write(fd, (char *)&nextloc, sizeof(nextloc));
432     lseek(fd, (long)(lastloc=nextloc), 0);
433     }
434 gregl 3.1 }
435    
436    
437 gwlarson 3.42 int
438 gregl 3.1 headline(s) /* process information header line */
439     char *s;
440     {
441     extern char FMTSTR[];
442     register char *cp;
443     char fmt[32];
444    
445 gregl 3.2 if (formatval(fmt, s)) {
446 gregl 3.1 if (strcmp(fmt, HOLOFMT)) {
447     sprintf(errmsg, "%s file \"%s\" has %s%s",
448     HOLOFMT, hdkfile, FMTSTR, fmt);
449     error(USER, errmsg);
450     }
451 gwlarson 3.42 return(0);
452 gregl 3.1 }
453     for (cp = s; *cp; cp++) /* take off any comments */
454     if (*cp == '#') {
455     *cp = '\0';
456     break;
457     }
458     setvariable(s, matchvar); /* don't flag errors */
459 gwlarson 3.42 return(0);
460 gregl 3.1 }
461    
462    
463     loadholo() /* start loading a holodeck from fname */
464     {
465 gregl 3.10 extern long ftell();
466 gregl 3.1 FILE *fp;
467 gregl 3.10 int fd;
468 gregl 3.29 int n;
469     int4 nextloc;
470 gregl 3.4 /* open holodeck file */
471     if ((fp = fopen(hdkfile, ncprocs>0 ? "r+" : "r")) == NULL) {
472 gwlarson 3.39 sprintf(errmsg, "cannot %s \"%s\"",
473     ncprocs>0 ? "append" : "read", hdkfile);
474 gregl 3.1 error(SYSTEM, errmsg);
475     }
476     /* load variables from header */
477     getheader(fp, headline, NULL);
478     /* check magic number */
479     if (getw(fp) != HOLOMAGIC) {
480     sprintf(errmsg, "bad magic number in holodeck file \"%s\"",
481     hdkfile);
482     error(USER, errmsg);
483     }
484 gregl 3.29 nextloc = ftell(fp); /* get stdio position */
485 gregl 3.10 fd = dup(fileno(fp));
486     fclose(fp); /* done with stdio */
487 gregl 3.29 for (n = 0; nextloc > 0L; n++) { /* initialize each section */
488     lseek(fd, (long)nextloc, 0);
489     read(fd, (char *)&nextloc, sizeof(nextloc));
490     hdinit(fd, NULL);
491     }
492     if (n != vdef(SECTION)) {
493     sprintf(errmsg, "number of sections does not match %s setting",
494     vnam(SECTION));
495     error(WARNING, errmsg);
496     }
497 gregl 3.1 }
498    
499    
500     done_packets(pl) /* handle finished packets */
501     PACKET *pl;
502     {
503 gregl 3.13 static int n2flush = 0;
504 gregl 3.1 register PACKET *p;
505    
506     while (pl != NULL) {
507     p = pl; pl = p->next; p->next = NULL;
508     if (p->nr > 0) { /* add to holodeck */
509     bcopy((char *)p->ra,
510     (char *)hdnewrays(hdlist[p->hd],p->bi,p->nr),
511     p->nr*sizeof(RAYVAL));
512     if (outdev != NULL) /* display it */
513 gregl 3.7 disp_packet((PACKHEAD *)p);
514 gregl 3.13 if (hdcachesize <= 0) /* manual flushing */
515 gregl 3.33 n2flush++;
516 gregl 3.6 nraysdone += p->nr;
517     npacksdone++;
518 gregl 3.33 p->nr = 0;
519 gregl 3.1 }
520 gregl 3.33 p->next = freepacks; /* push onto free list */
521 gregl 3.1 freepacks = p;
522 gregl 3.5 }
523 gwlarson 3.41 #if MAXQTIME
524 gwlarson 3.38 if (n2flush > 300/MAXQTIME*totqlen) {
525 gwlarson 3.41 #else
526     if (n2flush > 50*totqlen) {
527     #endif
528     if (outdev == NULL)
529     hdflush(NULL); /* flush holodeck buffers */
530     else
531     hdsync(NULL, 1); /* sync holodeck file */
532 gregl 3.13 n2flush = 0;
533 gregl 3.1 }
534     }
535    
536    
537     rootname(rn, fn) /* remove tail from end of fn */
538     register char *rn, *fn;
539     {
540     char *tp, *dp;
541    
542     for (tp = NULL, dp = rn; *rn = *fn++; rn++)
543 gregl 3.22 if (*rn == '/')
544 gregl 3.1 dp = rn;
545     else if (*rn == '.')
546     tp = rn;
547     if (tp != NULL && tp > dp)
548     *tp = '\0';
549     }
550    
551    
552     badvalue(vc) /* report bad variable value and exit */
553     int vc;
554     {
555     sprintf(errmsg, "bad value for variable '%s'", vnam(vc));
556     error(USER, errmsg);
557     }
558    
559    
560     eputs(s) /* put error message to stderr */
561     register char *s;
562     {
563     static int midline = 0;
564    
565     if (!*s)
566     return;
567     if (!midline++) { /* prepend line with program name */
568     fputs(progname, stderr);
569     fputs(": ", stderr);
570     }
571     fputs(s, stderr);
572     if (s[strlen(s)-1] == '\n') {
573     fflush(stderr);
574     midline = 0;
575     }
576     }
577    
578    
579     quit(ec) /* exit program gracefully */
580     int ec;
581     {
582     int status = 0;
583    
584 gregl 3.24 if (hdlist[0] != NULL) { /* close holodeck */
585 gregl 3.21 if (nprocs > 0)
586 gregl 3.24 status = done_rtrace(); /* calls hdsync() */
587 gregl 3.19 if (ncprocs > 0 && vdef(REPORT)) {
588     long fsiz, fuse;
589     fsiz = hdfilen(hdlist[0]->fd);
590     fuse = hdfiluse(hdlist[0]->fd, 1);
591     fprintf(stderr,
592 gregl 3.3 "%s: %.1f Mbyte holodeck file, %.1f%% fragmentation\n",
593 gregl 3.19 hdkfile, fsiz/(1024.*1024.),
594     100.*(fsiz-fuse)/fsiz);
595     }
596 gregl 3.1 }
597 gregl 3.12 if (orig_mode >= 0) /* reset holodeck access mode */
598     fchmod(hdlist[0]->fd, orig_mode);
599 gregl 3.11 if (outdev != NULL) /* close display */
600     disp_close();
601 gregl 3.1 exit(ec ? ec : status); /* exit */
602     }