14 |
|
#include <sys/types.h> |
15 |
|
#include <sys/stat.h> |
16 |
|
|
17 |
+ |
#ifndef MAXQTIME |
18 |
+ |
#define MAXQTIME 5 /* target maximum seconds in queue */ |
19 |
+ |
#endif |
20 |
|
/* the following must be consistent with rholo.h */ |
21 |
|
int NVARS = NRHVARS; /* total number of variables */ |
22 |
|
|
51 |
|
long npacksdone = 0L; /* number of packets done */ |
52 |
|
|
53 |
|
PACKET *freepacks; /* available packets */ |
54 |
+ |
int avgqlen; /* average queue length when full */ |
55 |
|
|
56 |
|
char *sigerr[NSIG]; /* signal error messages */ |
57 |
|
|
62 |
|
int argc; |
63 |
|
char *argv[]; |
64 |
|
{ |
61 |
– |
HDGRID hdg; |
65 |
|
int i; |
66 |
< |
/* mark start time */ |
64 |
< |
starttime = time(NULL); |
66 |
> |
|
67 |
|
initurand(16384); /* initialize urand */ |
68 |
|
progname = argv[0]; /* get arguments */ |
69 |
|
for (i = 1; i < argc && argv[i][0] == '-'; i++) |
90 |
|
default: |
91 |
|
goto userr; |
92 |
|
} |
91 |
– |
/* do we have a job? */ |
92 |
– |
if (outdev == NULL && ncprocs <= 0) |
93 |
– |
goto userr; |
93 |
|
/* get root file name */ |
94 |
+ |
if (i >= argc) |
95 |
+ |
goto userr; |
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 |
< |
getradfile(); |
97 |
> |
/* load variables? */ |
98 |
> |
if (i < argc) |
99 |
> |
if (argv[i][0] != '-' && argv[i][0] != '+') |
100 |
> |
loadvars(argv[i]); /* load variables from file */ |
101 |
> |
|
102 |
> |
if (i >= argc || argv[i][0] == '+') |
103 |
> |
loadholo(); /* load existing holodeck */ |
104 |
> |
|
105 |
> |
while (++i < argc) /* get command line settings */ |
106 |
> |
if (setvariable(argv[i], matchvar) < 0) { |
107 |
> |
sprintf(errmsg, "unknown variable: %s", argv[i]); |
108 |
> |
error(USER, errmsg); |
109 |
> |
} |
110 |
> |
/* check settings */ |
111 |
> |
checkvalues(); |
112 |
> |
/* load RIF if any */ |
113 |
> |
getradfile(); |
114 |
> |
|
115 |
> |
if (hdlist[0] == NULL) { /* create new holodeck */ |
116 |
> |
HDGRID hdg[HDMAX]; |
117 |
|
/* set defaults */ |
118 |
< |
setdefaults(&hdg); |
118 |
> |
setdefaults(hdg); |
119 |
|
/* holodeck exists? */ |
120 |
|
if (!force && access(hdkfile, R_OK|W_OK) == 0) |
121 |
|
error(USER, |
122 |
|
"holodeck file exists -- use -f to overwrite"); |
123 |
|
/* create holodeck */ |
124 |
< |
creatholo(&hdg); |
125 |
< |
} else { /* else load holodeck */ |
119 |
< |
loadholo(); |
120 |
< |
/* check settings */ |
121 |
< |
checkvalues(); |
122 |
< |
/* load RIF if any */ |
123 |
< |
getradfile(); |
124 |
< |
/* set defaults */ |
124 |
> |
creatholo(hdg); |
125 |
> |
} else /* else just set defaults */ |
126 |
|
setdefaults(NULL); |
126 |
– |
} |
127 |
|
/* initialize */ |
128 |
|
initrholo(); |
129 |
|
/* main loop */ |
133 |
|
quit(0); |
134 |
|
userr: |
135 |
|
fprintf(stderr, |
136 |
< |
"Usage: %s {-n nprocs|-o disp} [-w][-f] output.hdk [control.hif [VAR=val ..]]\n", |
136 |
> |
"Usage: %s [-n nprocs][-o disp][-w][-f] output.hdk [control.hif|+|- [VAR=val ..]]\n", |
137 |
|
progname); |
138 |
|
quit(1); |
139 |
|
} |
147 |
|
if (gotsig++) /* two signals and we're gone! */ |
148 |
|
_exit(signo); |
149 |
|
|
150 |
< |
alarm(30); /* allow 30 seconds to clean up */ |
150 |
> |
alarm(60); /* allow 60 seconds to clean up */ |
151 |
|
signal(SIGALRM, SIG_DFL); /* make certain we do die */ |
152 |
|
eputs("signal - "); |
153 |
|
eputs(sigerr[signo]); |
202 |
|
maxdisk = 0; |
203 |
|
else |
204 |
|
maxdisk = 1024.*1024.*vflt(DISKSPACE); |
205 |
– |
/* record end time */ |
206 |
– |
if (!vdef(TIME) || vflt(TIME) <= FTINY) |
207 |
– |
endtime = 0; |
208 |
– |
else |
209 |
– |
endtime = starttime + vflt(TIME)*3600. + .5; |
205 |
|
/* set up memory cache */ |
206 |
|
if (outdev == NULL) |
207 |
|
hdcachesize = 0; /* manual flushing */ |
213 |
|
if (*s && freopen(s, "a", stderr) == NULL) |
214 |
|
quit(2); |
215 |
|
} |
216 |
+ |
/* mark the starting time */ |
217 |
+ |
starttime = time(NULL); |
218 |
+ |
/* compute end time */ |
219 |
+ |
if (!vdef(TIME) || vflt(TIME) <= FTINY) |
220 |
+ |
endtime = 0; |
221 |
+ |
else |
222 |
+ |
endtime = starttime + vflt(TIME)*3600. + .5; |
223 |
|
/* start rtrace */ |
224 |
|
if (ncprocs > 0) { |
225 |
|
i = start_rtrace(); |
233 |
|
freepacks = (PACKET *)bmalloc(i*sizeof(PACKET)); |
234 |
|
if (freepacks == NULL) |
235 |
|
goto memerr; |
236 |
+ |
if (!(avgqlen = i/nprocs)) /* record mean queue length */ |
237 |
+ |
avgqlen = 1; |
238 |
|
freepacks[--i].nr = 0; |
239 |
|
freepacks[i].next = NULL; |
240 |
< |
if (!vbool(OBSTRUCTIONS)) { |
240 |
> |
if (!vdef(OBSTRUCTIONS) || !vbool(OBSTRUCTIONS)) { |
241 |
|
freepacks[i].offset = (float *)bmalloc( |
242 |
|
RPACKSIZ*sizeof(float)*(i+1) ); |
243 |
|
if (freepacks[i].offset == NULL) |
271 |
|
|
272 |
|
rholo() /* holodeck main loop */ |
273 |
|
{ |
274 |
< |
static int idle = 1; |
274 |
> |
static int idle = 0; |
275 |
|
PACKET *pl = NULL, *plend; |
276 |
+ |
int pksiz; |
277 |
|
register PACKET *p; |
278 |
|
time_t t; |
279 |
< |
long l; |
275 |
< |
|
276 |
< |
if (outdev != NULL) /* check display */ |
277 |
< |
if (!disp_check(idle)) |
278 |
< |
return(0); |
279 |
< |
/* display only? */ |
279 |
> |
/* check display */ |
280 |
|
if (nprocs <= 0) |
281 |
< |
return(outdev != NULL); |
281 |
> |
idle = 1; |
282 |
> |
if (outdev != NULL) { |
283 |
> |
if (!disp_check(idle)) |
284 |
> |
return(0); /* quit request */ |
285 |
> |
if (nprocs <= 0) |
286 |
> |
return(1); |
287 |
> |
} else if (idle) |
288 |
> |
return(0); /* all done */ |
289 |
|
/* check file size */ |
290 |
|
if (maxdisk > 0 && hdfilen(hdlist[0]->fd) >= maxdisk) { |
291 |
|
error(WARNING, "file limit exceeded"); |
292 |
|
done_rtrace(); |
286 |
– |
idle = 1; |
293 |
|
return(1); /* comes back */ |
294 |
|
} |
295 |
< |
/* check time */ |
290 |
< |
if (endtime > 0 || reporttime > 0) |
291 |
< |
t = time(NULL); |
295 |
> |
t = time(NULL); /* check time */ |
296 |
|
if (endtime > 0 && t >= endtime) { |
297 |
|
error(WARNING, "time limit exceeded"); |
298 |
|
done_rtrace(); |
295 |
– |
idle = 1; |
299 |
|
return(1); /* comes back */ |
300 |
|
} |
301 |
|
if (reporttime > 0 && t >= reporttime) |
302 |
|
report(t); |
303 |
< |
/* get packets to process */ |
303 |
> |
/* figure out good packet size */ |
304 |
> |
#if MAXQTIME |
305 |
> |
pksiz = nraysdone*MAXQTIME/(avgqlen*(t - starttime + 1L)); |
306 |
> |
if (pksiz < 1) |
307 |
> |
pksiz = 1; |
308 |
> |
else if (pksiz > RPACKSIZ) |
309 |
> |
#endif |
310 |
> |
pksiz = RPACKSIZ; |
311 |
> |
idle = 0; /* get packets to process */ |
312 |
|
while (freepacks != NULL) { |
313 |
|
p = freepacks; freepacks = p->next; p->next = NULL; |
314 |
< |
if (!next_packet(p)) { |
314 |
> |
if (!next_packet(p, pksiz)) { |
315 |
|
p->next = freepacks; freepacks = p; |
316 |
|
idle = 1; |
317 |
|
break; |
331 |
|
{ |
332 |
|
extern char *atos(); |
333 |
|
register int i; |
334 |
+ |
int n; |
335 |
|
double len[3], maxlen, d; |
336 |
|
char buf[64]; |
337 |
|
|
339 |
|
sprintf(errmsg, "%s must be defined", vnam(SECTION)); |
340 |
|
error(USER, errmsg); |
341 |
|
} |
330 |
– |
if (vdef(SECTION) > 1) { |
331 |
– |
sprintf(errmsg, "ignoring all but first %s", vnam(SECTION)); |
332 |
– |
error(WARNING, errmsg); |
333 |
– |
} |
342 |
|
if (!vdef(OCTREE)) { |
343 |
|
if ((vval(OCTREE) = bmalloc(strlen(froot)+5)) == NULL) |
344 |
|
error(SYSTEM, "out of memory"); |
345 |
|
sprintf(vval(OCTREE), "%s.oct", froot); |
346 |
|
vdef(OCTREE)++; |
347 |
|
} |
340 |
– |
if (!vdef(OBSTRUCTIONS)) { |
341 |
– |
vval(OBSTRUCTIONS) = "T"; |
342 |
– |
vdef(OBSTRUCTIONS)++; |
343 |
– |
} |
348 |
|
if (!vdef(VDIST)) { |
349 |
|
vval(VDIST) = "F"; |
350 |
|
vdef(VDIST)++; |
351 |
|
} |
348 |
– |
if (!vdef(OCCUPANCY)) { |
349 |
– |
vval(OCCUPANCY) = "U"; |
350 |
– |
vdef(OCCUPANCY)++; |
351 |
– |
} |
352 |
|
/* append rendering options */ |
353 |
|
if (vdef(RENDER)) |
354 |
|
rtargc += wordstring(rtargv+rtargc, vval(RENDER)); |
356 |
|
if (gp == NULL) /* already initialized? */ |
357 |
|
return; |
358 |
|
/* set grid parameters */ |
359 |
< |
gp->grid[0] = gp->grid[1] = gp->grid[2] = 0; |
360 |
< |
if (sscanf(vval(SECTION), |
359 |
> |
for (n = 0; n < vdef(SECTION); n++, gp++) { |
360 |
> |
gp->grid[0] = gp->grid[1] = gp->grid[2] = 0; |
361 |
> |
if (sscanf(nvalue(SECTION, n), |
362 |
|
"%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %hd %hd %hd", |
363 |
< |
&gp->orig[0], &gp->orig[1], &gp->orig[2], |
364 |
< |
&gp->xv[0][0], &gp->xv[0][1], &gp->xv[0][2], |
365 |
< |
&gp->xv[1][0], &gp->xv[1][1], &gp->xv[1][2], |
366 |
< |
&gp->xv[2][0], &gp->xv[2][1], &gp->xv[2][2], |
367 |
< |
&gp->grid[0], &gp->grid[1], &gp->grid[2]) < 12) |
368 |
< |
badvalue(SECTION); |
369 |
< |
maxlen = 0.; |
370 |
< |
for (i = 0; i < 3; i++) |
371 |
< |
if ((len[i] = VLEN(gp->xv[i])) > maxlen) |
372 |
< |
maxlen = len[i]; |
373 |
< |
if (!vdef(GRID)) { |
374 |
< |
sprintf(buf, "%.4f", maxlen/8.); |
375 |
< |
vval(GRID) = savqstr(buf); |
376 |
< |
vdef(GRID)++; |
363 |
> |
&gp->orig[0], &gp->orig[1], &gp->orig[2], |
364 |
> |
&gp->xv[0][0], &gp->xv[0][1], &gp->xv[0][2], |
365 |
> |
&gp->xv[1][0], &gp->xv[1][1], &gp->xv[1][2], |
366 |
> |
&gp->xv[2][0], &gp->xv[2][1], &gp->xv[2][2], |
367 |
> |
&gp->grid[0], &gp->grid[1], &gp->grid[2]) < 12) |
368 |
> |
badvalue(SECTION); |
369 |
> |
maxlen = 0.; |
370 |
> |
for (i = 0; i < 3; i++) |
371 |
> |
if ((len[i] = VLEN(gp->xv[i])) > maxlen) |
372 |
> |
maxlen = len[i]; |
373 |
> |
if (!vdef(GRID)) |
374 |
> |
d = 0.125*maxlen; |
375 |
> |
else if ((d = vflt(GRID)) <= FTINY) |
376 |
> |
badvalue(GRID); |
377 |
> |
for (i = 0; i < 3; i++) |
378 |
> |
if (gp->grid[i] <= 0) |
379 |
> |
gp->grid[i] = len[i]/d + (1.-FTINY); |
380 |
|
} |
377 |
– |
if ((d = vflt(GRID)) <= FTINY) |
378 |
– |
badvalue(GRID); |
379 |
– |
for (i = 0; i < 3; i++) |
380 |
– |
if (gp->grid[i] <= 0) |
381 |
– |
gp->grid[i] = len[i]/d + (1.-FTINY); |
381 |
|
} |
382 |
|
|
383 |
|
|
385 |
|
HDGRID *gp; |
386 |
|
{ |
387 |
|
extern char VersionID[]; |
388 |
< |
long endloc = 0; |
388 |
> |
int4 lastloc, nextloc; |
389 |
> |
int n; |
390 |
|
int fd; |
391 |
|
FILE *fp; |
392 |
|
/* open & truncate file */ |
400 |
|
printvars(fp); |
401 |
|
fputformat(HOLOFMT, fp); |
402 |
|
fputc('\n', fp); |
403 |
< |
putw(HOLOMAGIC, fp); /* put magic number & terminus */ |
404 |
< |
fwrite(&endloc, sizeof(long), 1, fp); |
403 |
> |
putw(HOLOMAGIC, fp); /* put magic number */ |
404 |
|
fd = dup(fileno(fp)); |
405 |
|
fclose(fp); /* flush and close stdio stream */ |
406 |
< |
hdinit(fd, gp); /* allocate and initialize index */ |
406 |
> |
lastloc = lseek(fd, 0L, 2); |
407 |
> |
for (n = vdef(SECTION); n--; gp++) { /* initialize each section */ |
408 |
> |
nextloc = 0L; |
409 |
> |
write(fd, (char *)&nextloc, sizeof(nextloc)); |
410 |
> |
hdinit(fd, gp); /* writes beam index */ |
411 |
> |
if (!n) |
412 |
> |
break; |
413 |
> |
nextloc = hdfilen(fd); /* write section pointer */ |
414 |
> |
if (lseek(fd, (long)lastloc, 0) < 0) |
415 |
> |
error(SYSTEM, |
416 |
> |
"cannot seek on holodeck file in creatholo"); |
417 |
> |
write(fd, (char *)&nextloc, sizeof(nextloc)); |
418 |
> |
lseek(fd, (long)(lastloc=nextloc), 0); |
419 |
> |
} |
420 |
|
} |
421 |
|
|
422 |
|
|
449 |
|
extern long ftell(); |
450 |
|
FILE *fp; |
451 |
|
int fd; |
452 |
< |
long fpos; |
452 |
> |
int n; |
453 |
> |
int4 nextloc; |
454 |
|
/* open holodeck file */ |
455 |
|
if ((fp = fopen(hdkfile, ncprocs>0 ? "r+" : "r")) == NULL) { |
456 |
|
sprintf(errmsg, "cannot open \"%s\" for %s", hdkfile, |
465 |
|
hdkfile); |
466 |
|
error(USER, errmsg); |
467 |
|
} |
468 |
< |
fread(&fpos, sizeof(long), 1, fp); |
456 |
< |
if (fpos != 0) |
457 |
< |
error(WARNING, "ignoring multiple sections in holodeck file"); |
458 |
< |
fpos = ftell(fp); /* get stdio position */ |
468 |
> |
nextloc = ftell(fp); /* get stdio position */ |
469 |
|
fd = dup(fileno(fp)); |
470 |
|
fclose(fp); /* done with stdio */ |
471 |
< |
lseek(fd, fpos, 0); /* align system file pointer */ |
472 |
< |
hdinit(fd, NULL); /* allocate and load index */ |
471 |
> |
for (n = 0; nextloc > 0L; n++) { /* initialize each section */ |
472 |
> |
lseek(fd, (long)nextloc, 0); |
473 |
> |
read(fd, (char *)&nextloc, sizeof(nextloc)); |
474 |
> |
hdinit(fd, NULL); |
475 |
> |
} |
476 |
> |
if (n != vdef(SECTION)) { |
477 |
> |
sprintf(errmsg, "number of sections does not match %s setting", |
478 |
> |
vnam(SECTION)); |
479 |
> |
error(WARNING, errmsg); |
480 |
> |
} |
481 |
|
} |
482 |
|
|
483 |
|
|
496 |
|
if (outdev != NULL) /* display it */ |
497 |
|
disp_packet((PACKHEAD *)p); |
498 |
|
if (hdcachesize <= 0) /* manual flushing */ |
499 |
< |
n2flush += p->nr; |
499 |
> |
n2flush++; |
500 |
|
nraysdone += p->nr; |
501 |
|
npacksdone++; |
502 |
+ |
p->nr = 0; |
503 |
|
} |
504 |
< |
p->nr = 0; /* push onto free list */ |
486 |
< |
p->next = freepacks; |
504 |
> |
p->next = freepacks; /* push onto free list */ |
505 |
|
freepacks = p; |
506 |
|
} |
507 |
< |
if (n2flush > 512*RPACKSIZ*nprocs) { |
507 |
> |
if (n2flush > 300/MAXQTIME*avgqlen*nprocs) { |
508 |
|
hdflush(NULL); /* flush holodeck buffers */ |
509 |
|
n2flush = 0; |
510 |
|
} |