| 1 | – | /* Copyright (c) 1999 Silicon Graphics, Inc. */ | 
| 2 | – |  | 
| 1 |  | #ifndef lint | 
| 2 | < | static char SCCSid[] = "$SunId$ SGI"; | 
| 2 | > | static const char       RCSid[] = "$Id$"; | 
| 3 |  | #endif | 
| 6 | – |  | 
| 4 |  | /* | 
| 5 |  | * Radiance holodeck generation controller | 
| 6 |  | */ | 
| 7 |  |  | 
| 11 | – | #include "rholo.h" | 
| 12 | – | #include "random.h" | 
| 8 |  | #include <signal.h> | 
| 14 | – | #include <sys/types.h> | 
| 9 |  | #include <sys/stat.h> | 
| 10 | + | #include <string.h> | 
| 11 |  |  | 
| 12 | < | #ifndef RTFLUSH | 
| 13 | < | #define RTFLUSH         0               /* flush one at a time in batch */ | 
| 14 | < | #endif | 
| 12 | > | #include "platform.h" | 
| 13 | > | #include "rholo.h" | 
| 14 | > | #include "random.h" | 
| 15 | > |  | 
| 16 |  | #ifndef FRAGWARN | 
| 17 |  | #define FRAGWARN        20              /* fragmentation for warning (%) */ | 
| 18 |  | #endif | 
| 19 |  | #ifndef MAXQTIME | 
| 20 |  | #define MAXQTIME        5               /* target maximum seconds in queue */ | 
| 21 |  | #endif | 
| 22 | + | /* manual cache flushing frequency */ | 
| 23 | + | #ifndef RTFLUSH | 
| 24 | + | #if MAXQTIME | 
| 25 | + | #define RTFLUSH         (300/MAXQTIME*totqlen)  /* <= 5 minutes */ | 
| 26 | + | #else | 
| 27 | + | #define RTFLUSH         (50*totqlen)            /* just guess */ | 
| 28 | + | #endif | 
| 29 | + | #endif | 
| 30 |  | /* the following must be consistent with rholo.h */ | 
| 31 |  | int     NVARS = NRHVARS;                /* total number of variables */ | 
| 32 |  |  | 
| 42 |  |  | 
| 43 |  | int     readinp = 0;            /* read commands from stdin */ | 
| 44 |  |  | 
| 45 | < | int     force = 0;              /* allow overwrite of holodeck */ | 
| 45 | > | int     force = 0;              /* allow overwrite of holodeck (-1 == read-only) */ | 
| 46 |  |  | 
| 47 |  | time_t  starttime;              /* time we got started */ | 
| 48 |  | time_t  endtime;                /* time we should end by */ | 
| 49 |  | time_t  reporttime;             /* time for next report */ | 
| 50 |  |  | 
| 51 | < | long    maxdisk;                /* maximum file space (bytes) */ | 
| 51 | > | off_t   maxdisk;                /* maximum file space (bytes) */ | 
| 52 |  |  | 
| 53 |  | int     rtargc = 1;             /* rtrace command */ | 
| 54 |  | char    *rtargv[128] = {"rtrace", NULL}; | 
| 59 |  | long    npacksdone = 0L;        /* number of packets done */ | 
| 60 |  |  | 
| 61 |  | PACKET  *freepacks;             /* available packets */ | 
| 62 | < | int     totqlen;                /* maximum queue length when full */ | 
| 62 | > | int     totqlen;                /* maximum queue length (number of packets) */ | 
| 63 |  |  | 
| 64 |  | char  *sigerr[NSIG];            /* signal error messages */ | 
| 65 |  |  | 
| 82 |  | nowarn++; | 
| 83 |  | break; | 
| 84 |  | case 'f':                       /* force overwrite */ | 
| 85 | < | force++; | 
| 85 | > | force = 1; | 
| 86 |  | break; | 
| 87 | + | case 'r':                       /* read-only mode */ | 
| 88 | + | force = -1; | 
| 89 | + | break; | 
| 90 |  | case 'i':                       /* read input from stdin */ | 
| 91 |  | readinp++; | 
| 92 |  | break; | 
| 129 |  | HDGRID  hdg[HDMAX]; | 
| 130 |  | /* set defaults */ | 
| 131 |  | setdefaults(hdg); | 
| 132 | + | /* check read-only */ | 
| 133 | + | if (force < 0) | 
| 134 | + | error(USER, "cannot create read-only holodeck"); | 
| 135 |  | /* holodeck exists? */ | 
| 136 |  | if (!force && access(hdkfile, R_OK|W_OK) == 0) | 
| 137 |  | error(USER, | 
| 149 |  | quit(0); | 
| 150 |  | userr: | 
| 151 |  | fprintf(stderr, | 
| 152 | < | "Usage: %s [-n nprocs][-o disp][-w][-f] output.hdk [control.hif|+|- [VAR=val ..]]\n", | 
| 152 | > | "Usage: %s [-n nprocs][-o disp][-w][-r|-f] output.hdk [control.hif|+|- [VAR=val ..]]\n", | 
| 153 |  | progname); | 
| 154 |  | quit(1); | 
| 155 |  | } | 
| 156 |  |  | 
| 157 |  |  | 
| 158 | + | void | 
| 159 |  | onsig(signo)                            /* fatal signal */ | 
| 160 |  | int  signo; | 
| 161 |  | { | 
| 218 |  | init_global(); | 
| 219 |  | /* record disk space limit */ | 
| 220 |  | if (!vdef(DISKSPACE)) | 
| 221 | < | maxdisk = 0; | 
| 221 | > | maxdisk = ((off_t)1<<(sizeof(off_t)*8-2)) - 1024; | 
| 222 |  | else | 
| 223 |  | maxdisk = 1024.*1024.*vflt(DISKSPACE); | 
| 224 |  | /* set up memory cache */ | 
| 225 |  | if (outdev == NULL) | 
| 226 | < | hdcachesize = 0;        /* manual flushing */ | 
| 226 | > | hdcachesize = 0;                /* manual flushing */ | 
| 227 |  | else if (vdef(CACHE)) | 
| 228 |  | hdcachesize = 1024.*1024.*vflt(CACHE); | 
| 229 |  | /* open report file */ | 
| 270 |  | } | 
| 271 |  | /* set up signal handling */ | 
| 272 |  | sigdie(SIGINT, "Interrupt"); | 
| 262 | – | sigdie(SIGHUP, "Hangup"); | 
| 273 |  | sigdie(SIGTERM, "Terminate"); | 
| 274 | + | #ifdef SIGHUP | 
| 275 | + | sigdie(SIGHUP, "Hangup"); | 
| 276 | + | #endif | 
| 277 | + | #ifdef SIGPIPE | 
| 278 |  | sigdie(SIGPIPE, "Broken pipe"); | 
| 279 | + | #endif | 
| 280 | + | #ifdef SIGALRM | 
| 281 |  | sigdie(SIGALRM, "Alarm clock"); | 
| 282 | + | #endif | 
| 283 |  | #ifdef  SIGXCPU | 
| 284 |  | sigdie(SIGXCPU, "CPU limit exceeded"); | 
| 285 | + | #endif | 
| 286 | + | #ifdef  SIGXFSZ | 
| 287 |  | sigdie(SIGXFSZ, "File size exceeded"); | 
| 288 |  | #endif | 
| 289 | < | /* protect holodeck file */ | 
| 290 | < | orig_mode = resfmode(hdlist[0]->fd, ncprocs>0 ? 0 : 0444); | 
| 289 | > | /* protect holodeck file */ | 
| 290 | > | orig_mode = resfmode(hdlist[0]->fd, (ncprocs>0) & (force>=0) ? 0 : 0444); | 
| 291 |  | return; | 
| 292 |  | memerr: | 
| 293 |  | error(SYSTEM, "out of memory in initrholo"); | 
| 299 |  | static long     nextfragwarn = 100*(1L<<20); | 
| 300 |  | static int      idle = 0; | 
| 301 |  | PACKET  *pl = NULL, *plend; | 
| 302 | < | long    fsiz; | 
| 302 | > | off_t   fsiz; | 
| 303 |  | int     pksiz; | 
| 304 |  | register PACKET *p; | 
| 305 |  | time_t  t; | 
| 337 |  | if (reporttime > 0 && t >= reporttime) | 
| 338 |  | report(t); | 
| 339 |  | /* figure out good packet size */ | 
| 340 | + | pksiz = RPACKSIZ; | 
| 341 |  | #if MAXQTIME | 
| 342 | < | pksiz = nraysdone*MAXQTIME/(totqlen*(t - starttime + 1L)); | 
| 343 | < | if (pksiz < 1) | 
| 344 | < | pksiz = 1; | 
| 345 | < | else if (pksiz > RPACKSIZ) | 
| 342 | > | if (!chunkycmp) { | 
| 343 | > | pksiz = nraysdone*MAXQTIME/(totqlen*(t - starttime + 1L)); | 
| 344 | > | if (pksiz < 1) pksiz = 1; | 
| 345 | > | else if (pksiz > RPACKSIZ) pksiz = RPACKSIZ; | 
| 346 | > | } | 
| 347 |  | #endif | 
| 327 | – | pksiz = RPACKSIZ; | 
| 348 |  | idle = 0;                       /* get packets to process */ | 
| 349 |  | while (freepacks != NULL) { | 
| 350 |  | p = freepacks; freepacks = p->next; p->next = NULL; | 
| 424 |  | HDGRID  *gp; | 
| 425 |  | { | 
| 426 |  | extern char     VersionID[]; | 
| 427 | < | int4    lastloc, nextloc; | 
| 427 | > | int32   lastloc, nextloc; | 
| 428 |  | int     n; | 
| 429 |  | int     fd; | 
| 430 |  | FILE    *fp; | 
| 442 |  | putw(HOLOMAGIC, fp);            /* put magic number */ | 
| 443 |  | fd = dup(fileno(fp)); | 
| 444 |  | fclose(fp);                     /* flush and close stdio stream */ | 
| 445 | < | lastloc = lseek(fd, 0L, 2); | 
| 445 | > | lastloc = lseek(fd, (off_t)0, SEEK_END); | 
| 446 |  | for (n = vdef(SECTION); n--; gp++) {    /* initialize each section */ | 
| 447 |  | nextloc = 0L; | 
| 448 |  | write(fd, (char *)&nextloc, sizeof(nextloc)); | 
| 450 |  | if (!n) | 
| 451 |  | break; | 
| 452 |  | nextloc = hdfilen(fd);          /* write section pointer */ | 
| 453 | < | if (lseek(fd, (long)lastloc, 0) < 0) | 
| 453 | > | if (lseek(fd, (off_t)lastloc, SEEK_SET) < 0) | 
| 454 |  | error(SYSTEM, | 
| 455 |  | "cannot seek on holodeck file in creatholo"); | 
| 456 |  | write(fd, (char *)&nextloc, sizeof(nextloc)); | 
| 457 | < | lseek(fd, (long)(lastloc=nextloc), 0); | 
| 457 | > | lseek(fd, (off_t)(lastloc=nextloc), SEEK_SET); | 
| 458 |  | } | 
| 459 |  | } | 
| 460 |  |  | 
| 487 |  |  | 
| 488 |  | loadholo()                      /* start loading a holodeck from fname */ | 
| 489 |  | { | 
| 470 | – | extern long     ftell(); | 
| 490 |  | FILE    *fp; | 
| 491 |  | int     fd; | 
| 492 |  | int     n; | 
| 493 | < | int4    nextloc; | 
| 494 | < | /* open holodeck file */ | 
| 495 | < | if ((fp = fopen(hdkfile, ncprocs>0 ? "r+" : "r")) == NULL) { | 
| 496 | < | sprintf(errmsg, "cannot %s \"%s\"", | 
| 497 | < | ncprocs>0 ? "append" : "read", hdkfile); | 
| 498 | < | error(SYSTEM, errmsg); | 
| 493 | > | int32   nextloc; | 
| 494 | > |  | 
| 495 | > | if ((ncprocs > 0) & (force >= 0)) | 
| 496 | > | fp = fopen(hdkfile, "r+"); | 
| 497 | > | else | 
| 498 | > | fp = NULL; | 
| 499 | > | if (fp == NULL) { | 
| 500 | > | if ((fp = fopen(hdkfile, "r")) == NULL) { | 
| 501 | > | sprintf(errmsg, "cannot open \"%s\"", hdkfile); | 
| 502 | > | error(SYSTEM, errmsg); | 
| 503 | > | } | 
| 504 | > | if (ncprocs > 0) { | 
| 505 | > | sprintf(errmsg, | 
| 506 | > | "\"%s\" opened read-only; new rays will be discarded", | 
| 507 | > | hdkfile); | 
| 508 | > | error(WARNING, errmsg); | 
| 509 | > | force = -1; | 
| 510 | > | } | 
| 511 |  | } | 
| 512 |  | /* load variables from header */ | 
| 513 |  | getheader(fp, headline, NULL); | 
| 521 |  | fd = dup(fileno(fp)); | 
| 522 |  | fclose(fp);                             /* done with stdio */ | 
| 523 |  | for (n = 0; nextloc > 0L; n++) {        /* initialize each section */ | 
| 524 | < | lseek(fd, (long)nextloc, 0); | 
| 524 | > | lseek(fd, (off_t)nextloc, SEEK_SET); | 
| 525 |  | read(fd, (char *)&nextloc, sizeof(nextloc)); | 
| 526 |  | hdinit(fd, NULL); | 
| 527 |  | } | 
| 542 |  | while (pl != NULL) { | 
| 543 |  | p = pl; pl = p->next; p->next = NULL; | 
| 544 |  | if (p->nr > 0) {                /* add to holodeck */ | 
| 545 | < | bcopy((char *)p->ra, | 
| 546 | < | (char *)hdnewrays(hdlist[p->hd],p->bi,p->nr), | 
| 545 | > | memcpy( (void *)hdnewrays(hdlist[p->hd],p->bi,p->nr), | 
| 546 | > | (void *)p->ra, | 
| 547 |  | p->nr*sizeof(RAYVAL)); | 
| 548 |  | if (outdev != NULL)     /* display it */ | 
| 549 |  | disp_packet((PACKHEAD *)p); | 
| 550 | < | if (hdcachesize <= 0) { | 
| 520 | < | #if RTFLUSH | 
| 521 | < | if (outdev == NULL) | 
| 522 | < | hdfreebeam(hdlist[p->hd], p->bi); | 
| 523 | < | #endif | 
| 550 | > | if (hdcachesize <= 0) | 
| 551 |  | n2flush++; | 
| 525 | – | } | 
| 552 |  | nraysdone += p->nr; | 
| 553 |  | npacksdone++; | 
| 554 |  | p->nr = 0; | 
| 556 |  | p->next = freepacks;            /* push onto free list */ | 
| 557 |  | freepacks = p; | 
| 558 |  | } | 
| 559 | < | #if MAXQTIME | 
| 534 | < | if (n2flush > 300/MAXQTIME*totqlen) { | 
| 535 | < | #else | 
| 536 | < | if (n2flush > 50*totqlen) { | 
| 537 | < | #endif | 
| 538 | < | #if RTFLUSH | 
| 539 | < | hdsync(NULL, outdev!=NULL);     /* sync beams & directories */ | 
| 540 | < | #else | 
| 559 | > | if (n2flush >= RTFLUSH) { | 
| 560 |  | if (outdev != NULL) | 
| 561 |  | hdsync(NULL, 1); | 
| 562 |  | else | 
| 563 |  | hdflush(NULL); | 
| 545 | – | #endif | 
| 564 |  | n2flush = 0; | 
| 565 |  | } | 
| 566 |  | } | 
| 571 |  | { | 
| 572 |  | char    *tp, *dp; | 
| 573 |  |  | 
| 574 | < | for (tp = NULL, dp = rn; *rn = *fn++; rn++) | 
| 574 | > | for (tp = NULL, dp = rn; (*rn = *fn++); rn++) { | 
| 575 |  | if (*rn == '/') | 
| 576 |  | dp = rn; | 
| 577 |  | else if (*rn == '.') | 
| 578 |  | tp = rn; | 
| 579 | + | } | 
| 580 |  | if (tp != NULL && tp > dp) | 
| 581 |  | *tp = '\0'; | 
| 582 |  | } | 
| 590 |  | } | 
| 591 |  |  | 
| 592 |  |  | 
| 593 | + | void | 
| 594 |  | eputs(s)                        /* put error message to stderr */ | 
| 595 |  | register char  *s; | 
| 596 |  | { | 
| 610 |  | } | 
| 611 |  |  | 
| 612 |  |  | 
| 613 | + | void | 
| 614 |  | quit(ec)                        /* exit program gracefully */ | 
| 615 |  | int     ec; | 
| 616 |  | { | 
| 619 |  | if (hdlist[0] != NULL) {        /* close holodeck */ | 
| 620 |  | if (nprocs > 0) | 
| 621 |  | status = done_rtrace();         /* calls hdsync() */ | 
| 622 | < | if (ncprocs > 0 && vdef(REPORT)) { | 
| 623 | < | long    fsiz, fuse; | 
| 622 | > | if ((ncprocs > 0) & (force >= 0) && vdef(REPORT)) { | 
| 623 | > | off_t   fsiz, fuse; | 
| 624 |  | fsiz = hdfilen(hdlist[0]->fd); | 
| 625 |  | fuse = hdfiluse(hdlist[0]->fd, 1); | 
| 626 |  | fprintf(stderr, |