ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rholo.c
(Generate patch)

Comparing ray/src/hd/rholo.c (file contents):
Revision 3.8 by gregl, Mon Nov 10 18:05:24 1997 UTC vs.
Revision 3.48 by gwlarson, Tue Feb 2 16:13:24 1999 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines