ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rholo.c
Revision: 3.68
Committed: Thu Sep 9 01:41:21 2004 UTC (19 years, 7 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R6, rad3R6P1
Changes since 3.67: +12 -9 lines
Log Message:
Reduced frequency of fragmentation checks and improved logic

File Contents

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