ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rholo.c
Revision: 3.20
Committed: Fri Dec 12 11:13:16 1997 UTC (26 years, 4 months ago) by gregl
Content type: text/plain
Branch: MAIN
Changes since 3.19: +16 -4 lines
Log Message:
implemented kill, clobber and restart commands

File Contents

# Content
1 /* Copyright (c) 1997 Silicon Graphics, Inc. */
2
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 #include "random.h"
13 #include "paths.h"
14 #include <signal.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17
18 /* the following must be consistent with rholo.h */
19 int NVARS = NRHVARS; /* total number of variables */
20
21 VARIABLE vv[] = RHVINIT; /* variable-value pairs */
22
23 char *progname; /* our program name */
24 char *hdkfile; /* holodeck file name */
25 char froot[MAXPATH]; /* root file name */
26
27 int nowarn = 0; /* turn warnings off? */
28
29 int ncprocs = 0; /* desired number of compute processes */
30
31 char *outdev = NULL; /* output device name */
32
33 int readinp = 0; /* read commands from stdin */
34
35 int force = 0; /* allow overwrite of holodeck */
36
37 time_t starttime; /* time we got started */
38 time_t endtime; /* time we should end by */
39 time_t reporttime; /* time for next report */
40
41 long maxdisk; /* maximum file space (bytes) */
42
43 int rtargc = 1; /* rtrace command */
44 char *rtargv[128] = {"rtrace", NULL};
45
46 int orig_mode = -1; /* original file mode (-1 if unchanged) */
47
48 long nraysdone = 0L; /* number of rays done */
49 long npacksdone = 0L; /* number of packets done */
50
51 PACKET *freepacks; /* available packets */
52
53 char *sigerr[NSIG]; /* signal error messages */
54
55 extern time_t time();
56
57
58 main(argc, argv)
59 int argc;
60 char *argv[];
61 {
62 HDGRID hdg;
63 int i;
64 /* mark start time */
65 starttime = time(NULL);
66 initurand(16384); /* initialize urand */
67 progname = argv[0]; /* get arguments */
68 for (i = 1; i < argc && argv[i][0] == '-'; i++)
69 switch (argv[i][1]) {
70 case 'w': /* turn off warnings */
71 nowarn++;
72 break;
73 case 'f': /* force overwrite */
74 force++;
75 break;
76 case 'i': /* read input from stdin */
77 readinp++;
78 break;
79 case 'n': /* compute processes */
80 if (i >= argc-2)
81 goto userr;
82 ncprocs = atoi(argv[++i]);
83 break;
84 case 'o': /* output display */
85 if (i >= argc-2)
86 goto userr;
87 outdev = argv[++i];
88 break;
89 default:
90 goto userr;
91 }
92 /* do we have a job? */
93 if (outdev == NULL && ncprocs <= 0)
94 goto userr;
95 /* get root file name */
96 rootname(froot, hdkfile=argv[i++]);
97 /* load... */
98 if (i < argc) { /* variables */
99 loadvars(argv[i++]);
100 /* cmdline settings */
101 for ( ; i < argc; i++)
102 if (setvariable(argv[i], matchvar) < 0) {
103 sprintf(errmsg, "unknown variable: %s",
104 argv[i]);
105 error(USER, errmsg);
106 }
107 /* check settings */
108 checkvalues();
109 /* load RIF if any */
110 if (vdef(RIF))
111 getradfile(vval(RIF));
112 /* set defaults */
113 setdefaults(&hdg);
114 /* holodeck exists? */
115 if (!force && access(hdkfile, R_OK|W_OK) == 0)
116 error(USER,
117 "holodeck file exists -- use -f to overwrite");
118 /* create holodeck */
119 creatholo(&hdg);
120 } else { /* else load holodeck */
121 loadholo();
122 /* check settings */
123 checkvalues();
124 /* load RIF if any */
125 if (vdef(RIF))
126 getradfile(vval(RIF));
127 /* set defaults */
128 setdefaults(NULL);
129 }
130 /* initialize */
131 initrholo();
132 /* main loop */
133 while (rholo())
134 ;
135 /* done */
136 quit(0);
137 userr:
138 fprintf(stderr,
139 "Usage: %s {-n nprocs|-o disp} [-w][-f] output.hdk [control.hif [VAR=val ..]]\n",
140 progname);
141 quit(1);
142 }
143
144
145 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 alarm(30); /* allow 30 seconds to clean up */
154 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 if (mod == stbuf.st_mode)
182 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 initrholo() /* get our holodeck running */
193 {
194 extern int global_packet();
195 register int i;
196 /* close holodeck on exec() */
197 fcntl(hdlist[0]->fd, F_SETFD, FD_CLOEXEC);
198
199 if (outdev != NULL) /* open output device */
200 disp_open(outdev);
201 else if (ncprocs > 0) /* else use global ray feed */
202 init_global();
203 /* record disk space limit */
204 if (!vdef(DISKSPACE))
205 maxdisk = 0;
206 else
207 maxdisk = 1024.*1024.*vflt(DISKSPACE);
208 /* record end time */
209 if (!vdef(TIME) || vflt(TIME) <= FTINY)
210 endtime = 0;
211 else
212 endtime = starttime + vflt(TIME)*3600.;
213 /* set up memory cache */
214 if (outdev == NULL)
215 hdcachesize = 0; /* manual flushing */
216 else if (vdef(CACHE))
217 hdcachesize = 1024.*1024.*vflt(CACHE);
218 /* open report file */
219 if (vdef(REPORT)) {
220 register char *s = sskip2(vval(REPORT), 1);
221 if (*s && freopen(s, "a", stderr) == NULL)
222 quit(2);
223 }
224 /* start rtrace */
225 if (ncprocs > 0) {
226 i = start_rtrace();
227 if (i < 1)
228 error(USER, "cannot start rtrace process(es)");
229 if (vdef(REPORT)) { /* make first report */
230 printargs(rtargc, rtargv, stderr);
231 report(0);
232 }
233 /* allocate packets */
234 freepacks = (PACKET *)bmalloc(i*sizeof(PACKET));
235 if (freepacks == NULL)
236 goto memerr;
237 freepacks[--i].nr = 0;
238 freepacks[i].next = NULL;
239 if (!vbool(OBSTRUCTIONS)) {
240 freepacks[i].offset = (float *)bmalloc(
241 RPACKSIZ*sizeof(float)*(i+1) );
242 if (freepacks[i].offset == NULL)
243 goto memerr;
244 } else
245 freepacks[i].offset = NULL;
246 while (i--) {
247 freepacks[i].nr = 0;
248 freepacks[i].offset = freepacks[i+1].offset == NULL ?
249 NULL : freepacks[i+1].offset+RPACKSIZ ;
250 freepacks[i].next = &freepacks[i+1];
251 }
252 }
253 /* set up signal handling */
254 sigdie(SIGINT, "Interrupt");
255 sigdie(SIGHUP, "Hangup");
256 sigdie(SIGTERM, "Terminate");
257 sigdie(SIGPIPE, "Broken pipe");
258 sigdie(SIGALRM, "Alarm clock");
259 #ifdef SIGXCPU
260 sigdie(SIGXCPU, "CPU limit exceeded");
261 sigdie(SIGXFSZ, "File size exceeded");
262 #endif
263 /* protect holodeck file */
264 orig_mode = resfmode(hdlist[0]->fd, ncprocs>0 ? 0 : 0444);
265 return;
266 memerr:
267 error(SYSTEM, "out of memory in initrholo");
268 }
269
270
271 rholo() /* holodeck main loop */
272 {
273 static int idle = 1;
274 PACKET *pl = NULL, *plend;
275 register PACKET *p;
276 time_t t;
277 long l;
278
279 if (outdev != NULL) /* check display */
280 if (!disp_check(idle))
281 return(0);
282 /* display only? */
283 if (nprocs <= 0)
284 return(1);
285 /* check file size */
286 if (maxdisk > 0 && hdfilen(hdlist[0]->fd) >= maxdisk) {
287 error(WARNING, "file limit exceeded");
288 return(0);
289 }
290 /* check time */
291 if (endtime > 0 || reporttime > 0)
292 t = time(NULL);
293 if (endtime > 0 && t >= endtime) {
294 error(WARNING, "time limit exceeded");
295 return(0);
296 }
297 if (reporttime > 0 && t >= reporttime)
298 report(t);
299 /* get packets to process */
300 while (freepacks != NULL) {
301 p = freepacks; freepacks = p->next; p->next = NULL;
302 if (!next_packet(p)) {
303 p->next = freepacks; freepacks = p;
304 break;
305 }
306 if (pl == NULL) pl = p;
307 else plend->next = p;
308 plend = p;
309 }
310 idle = pl == NULL && freepacks != NULL;
311 /* are we out of stuff to do? */
312 if (idle && outdev == NULL)
313 return(0);
314 /* else process packets */
315 done_packets(do_packets(pl));
316 return(1); /* and continue */
317 }
318
319
320 report(t) /* report progress so far */
321 time_t t;
322 {
323 if (t == 0)
324 t = time(NULL);
325 fprintf(stderr, "%s: %ld packets (%ld rays) done after %.2f hours\n",
326 progname, npacksdone, nraysdone, (t-starttime)/3600.);
327 fflush(stderr);
328 if (vdef(REPORT))
329 reporttime = t + (time_t)(vflt(REPORT)*60.+.5);
330 }
331
332
333 setdefaults(gp) /* set default values */
334 register HDGRID *gp;
335 {
336 extern char *atos();
337 register int i;
338 double len[3], maxlen, d;
339 char buf[64];
340
341 if (!vdef(SECTION)) {
342 sprintf(errmsg, "%s must be defined", vnam(SECTION));
343 error(USER, errmsg);
344 }
345 if (vdef(SECTION) > 1) {
346 sprintf(errmsg, "ignoring all but first %s", vnam(SECTION));
347 error(WARNING, errmsg);
348 }
349 if (!vdef(OCTREE)) {
350 if ((vval(OCTREE) = bmalloc(strlen(froot)+5)) == NULL)
351 error(SYSTEM, "out of memory");
352 sprintf(vval(OCTREE), "%s.oct", froot);
353 vdef(OCTREE)++;
354 }
355 if (!vdef(OBSTRUCTIONS)) {
356 vval(OBSTRUCTIONS) = "T";
357 vdef(OBSTRUCTIONS)++;
358 }
359 if (!vdef(VDIST)) {
360 vval(VDIST) = "F";
361 vdef(VDIST)++;
362 }
363 if (!vdef(OCCUPANCY)) {
364 vval(OCCUPANCY) = "U";
365 vdef(OCCUPANCY)++;
366 }
367 /* append rendering options */
368 if (vdef(RENDER))
369 rtargc += wordstring(rtargv+rtargc, vval(RENDER));
370
371 if (gp == NULL) /* already initialized? */
372 return;
373 /* set grid parameters */
374 gp->grid[0] = gp->grid[1] = gp->grid[2] = 0;
375 if (sscanf(vval(SECTION),
376 "%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %hd %hd %hd",
377 &gp->orig[0], &gp->orig[1], &gp->orig[2],
378 &gp->xv[0][0], &gp->xv[0][1], &gp->xv[0][2],
379 &gp->xv[1][0], &gp->xv[1][1], &gp->xv[1][2],
380 &gp->xv[2][0], &gp->xv[2][1], &gp->xv[2][2],
381 &gp->grid[0], &gp->grid[1], &gp->grid[2]) < 12)
382 badvalue(SECTION);
383 maxlen = 0.;
384 for (i = 0; i < 3; i++)
385 if ((len[i] = VLEN(gp->xv[i])) > maxlen)
386 maxlen = len[i];
387 if (!vdef(GRID)) {
388 sprintf(buf, "%.4f", maxlen/8.);
389 vval(GRID) = savqstr(buf);
390 vdef(GRID)++;
391 }
392 if ((d = vflt(GRID)) <= FTINY)
393 badvalue(GRID);
394 for (i = 0; i < 3; i++)
395 if (gp->grid[i] <= 0)
396 gp->grid[i] = len[i]/d + (1.-FTINY);
397 }
398
399
400 creatholo(gp) /* create a holodeck output file */
401 HDGRID *gp;
402 {
403 extern char VersionID[];
404 long endloc = 0;
405 int fd;
406 FILE *fp;
407 /* open & truncate file */
408 if ((fp = fopen(hdkfile, "w+")) == NULL) {
409 sprintf(errmsg, "cannot open \"%s\" for writing", hdkfile);
410 error(SYSTEM, errmsg);
411 }
412 /* write information header */
413 newheader("RADIANCE", fp);
414 fprintf(fp, "SOFTWARE= %s\n", VersionID);
415 printvars(fp);
416 fputformat(HOLOFMT, fp);
417 fputc('\n', fp);
418 putw(HOLOMAGIC, fp); /* put magic number & terminus */
419 fwrite(&endloc, sizeof(long), 1, fp);
420 fd = dup(fileno(fp));
421 fclose(fp); /* flush and close stdio stream */
422 hdinit(fd, gp); /* allocate and initialize index */
423 }
424
425
426 headline(s) /* process information header line */
427 char *s;
428 {
429 extern char FMTSTR[];
430 register char *cp;
431 char fmt[32];
432
433 if (formatval(fmt, s)) {
434 if (strcmp(fmt, HOLOFMT)) {
435 sprintf(errmsg, "%s file \"%s\" has %s%s",
436 HOLOFMT, hdkfile, FMTSTR, fmt);
437 error(USER, errmsg);
438 }
439 return;
440 }
441 for (cp = s; *cp; cp++) /* take off any comments */
442 if (*cp == '#') {
443 *cp = '\0';
444 break;
445 }
446 setvariable(s, matchvar); /* don't flag errors */
447 }
448
449
450 loadholo() /* start loading a holodeck from fname */
451 {
452 extern long ftell();
453 FILE *fp;
454 int fd;
455 long fpos;
456 /* open holodeck file */
457 if ((fp = fopen(hdkfile, ncprocs>0 ? "r+" : "r")) == NULL) {
458 sprintf(errmsg, "cannot open \"%s\" for %s", hdkfile,
459 ncprocs>0 ? "appending" : "reading");
460 error(SYSTEM, errmsg);
461 }
462 /* load variables from header */
463 getheader(fp, headline, NULL);
464 /* check magic number */
465 if (getw(fp) != HOLOMAGIC) {
466 sprintf(errmsg, "bad magic number in holodeck file \"%s\"",
467 hdkfile);
468 error(USER, errmsg);
469 }
470 fread(&fpos, sizeof(long), 1, fp);
471 if (fpos != 0)
472 error(WARNING, "ignoring multiple sections in holodeck file");
473 fpos = ftell(fp); /* get stdio position */
474 fd = dup(fileno(fp));
475 fclose(fp); /* done with stdio */
476 lseek(fd, fpos, 0); /* align system file pointer */
477 hdinit(fd, NULL); /* allocate and load index */
478 }
479
480
481 done_packets(pl) /* handle finished packets */
482 PACKET *pl;
483 {
484 static int n2flush = 0;
485 register PACKET *p;
486
487 while (pl != NULL) {
488 p = pl; pl = p->next; p->next = NULL;
489 if (p->nr > 0) { /* add to holodeck */
490 bcopy((char *)p->ra,
491 (char *)hdnewrays(hdlist[p->hd],p->bi,p->nr),
492 p->nr*sizeof(RAYVAL));
493 if (outdev != NULL) /* display it */
494 disp_packet((PACKHEAD *)p);
495 if (hdcachesize <= 0) /* manual flushing */
496 n2flush += p->nr;
497 nraysdone += p->nr;
498 npacksdone++;
499 }
500 p->nr = 0; /* push onto free list */
501 p->next = freepacks;
502 freepacks = p;
503 }
504 if (n2flush > 512*RPACKSIZ*nprocs) {
505 hdflush(NULL); /* flush holodeck buffers */
506 n2flush = 0;
507 }
508 }
509
510
511 checkrad() /* check to make sure octree is up to date */
512 {
513 char combuf[128];
514
515 if (!vdef(RIF))
516 return;
517 sprintf(combuf, "rad -v 0 -s -w %s", vval(RIF));
518 if (system(combuf))
519 error(WARNING, "error running rad");
520 }
521
522
523 getradfile(rfargs) /* run rad and get needed variables */
524 char *rfargs;
525 {
526 static short mvar[] = {OCTREE,-1};
527 static char tf1[] = TEMPLATE;
528 char tf2[64];
529 char combuf[256];
530 char *pippt;
531 register int i;
532 register char *cp;
533 /* create rad command */
534 mktemp(tf1);
535 sprintf(tf2, "%s.rif", tf1);
536 sprintf(combuf,
537 "rad -v 0 -s -e -w %s OPTFILE=%s | egrep '^[ \t]*(NOMATCH",
538 rfargs, tf1);
539 cp = combuf;
540 while (*cp){
541 if (*cp == '|') pippt = cp;
542 cp++;
543 } /* match unset variables */
544 for (i = 0; mvar[i] >= 0; i++)
545 if (!vdef(mvar[i])) {
546 *cp++ = '|';
547 strcpy(cp, vnam(mvar[i]));
548 while (*cp) cp++;
549 pippt = NULL;
550 }
551 if (pippt != NULL)
552 strcpy(pippt, "> /dev/null"); /* nothing to match */
553 else
554 sprintf(cp, ")[ \t]*=' > %s", tf2);
555 if (system(combuf)) {
556 unlink(tf2); /* clean up */
557 unlink(tf1);
558 error(SYSTEM, "cannot execute rad command");
559 }
560 if (pippt == NULL) {
561 loadvars(tf2); /* load variables */
562 unlink(tf2);
563 }
564 rtargc += wordfile(rtargv+rtargc, tf1); /* get rtrace options */
565 unlink(tf1); /* clean up */
566 }
567
568
569 rootname(rn, fn) /* remove tail from end of fn */
570 register char *rn, *fn;
571 {
572 char *tp, *dp;
573
574 for (tp = NULL, dp = rn; *rn = *fn++; rn++)
575 if (ISDIRSEP(*rn))
576 dp = rn;
577 else if (*rn == '.')
578 tp = rn;
579 if (tp != NULL && tp > dp)
580 *tp = '\0';
581 }
582
583
584 badvalue(vc) /* report bad variable value and exit */
585 int vc;
586 {
587 sprintf(errmsg, "bad value for variable '%s'", vnam(vc));
588 error(USER, errmsg);
589 }
590
591
592 eputs(s) /* put error message to stderr */
593 register char *s;
594 {
595 static int midline = 0;
596
597 if (!*s)
598 return;
599 if (!midline++) { /* prepend line with program name */
600 fputs(progname, stderr);
601 fputs(": ", stderr);
602 }
603 fputs(s, stderr);
604 if (s[strlen(s)-1] == '\n') {
605 fflush(stderr);
606 midline = 0;
607 }
608 }
609
610
611 wputs(s) /* put warning string to stderr */
612 char *s;
613 {
614 if (!nowarn)
615 eputs(s);
616 }
617
618
619 quit(ec) /* exit program gracefully */
620 int ec;
621 {
622 int status = 0;
623
624 if (hdlist[0] != NULL) { /* flush holodeck */
625 if (nprocs > 0) {
626 done_packets(flush_queue());
627 status = end_rtrace(); /* close rtrace */
628 }
629 hdflush(NULL);
630 if (ncprocs > 0 && vdef(REPORT)) {
631 long fsiz, fuse;
632 report(0);
633 fsiz = hdfilen(hdlist[0]->fd);
634 fuse = hdfiluse(hdlist[0]->fd, 1);
635 fprintf(stderr,
636 "%s: %.1f Mbyte holodeck file, %.1f%% fragmentation\n",
637 hdkfile, fsiz/(1024.*1024.),
638 100.*(fsiz-fuse)/fsiz);
639 }
640 }
641 if (orig_mode >= 0) /* reset holodeck access mode */
642 fchmod(hdlist[0]->fd, orig_mode);
643 if (outdev != NULL) /* close display */
644 disp_close();
645 exit(ec ? ec : status); /* exit */
646 }