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

Comparing ray/src/hd/holofile.c (file contents):
Revision 3.18 by gregl, Thu Jan 1 16:40:38 1998 UTC vs.
Revision 3.25 by gwlarson, Fri Sep 18 13:51:01 1998 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1997 Silicon Graphics, Inc. */
1 > /* Copyright (c) 1998 Silicon Graphics, Inc. */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ SGI";
# Line 16 | Line 16 | static char SCCSid[] = "$SunId$ SGI";
16   #define CACHESIZE       16      /* default cache size (Mbytes, 0==inf) */
17   #endif
18   #ifndef FREEBEAMS
19 < #define FREEBEAMS       512     /* maximum beams to free at a time */
19 > #define FREEBEAMS       1500    /* maximum beams to free at a time */
20   #endif
21   #ifndef PCTFREE
22 < #define PCTFREE         20      /* maximum fraction to free (%) */
22 > #define PCTFREE         15      /* maximum fraction to free (%) */
23   #endif
24   #ifndef MAXFRAG
25   #define MAXFRAG         32767   /* maximum fragments/file to track (0==inf) */
# Line 105 | Line 105 | hdinit(fd, hproto)     /* initialize a holodeck section in
105   int     fd;                     /* corresponding file descriptor */
106   HDGRID  *hproto;                /* holodeck section grid */
107   {
108 +        long    rtrunc;
109          long    fpos;
110          register HOLO   *hp;
111          register int    n;
# Line 126 | Line 127 | HDGRID *hproto;                /* holodeck section grid */
127                          error(SYSTEM, "failure loading holodeck directory");
128                                                  /* check that it's clean */
129                  if (hp->bi[nbeams(hp)].fo < 0)
130 <                        error(USER, "dirty holodeck section");
130 >                        error(WARNING, "dirty holodeck section");
131          } else {                        /* assume we're creating it */
132                  if ((hp = hdalloc(hproto)) == NULL)
133                          goto memerr;
# Line 144 | Line 145 | HDGRID *hproto;                /* holodeck section grid */
145          hdattach(fd);
146                                          /* check rays on disk */
147          fpos = hdfilen(fd);
148 <        biglob(hp)->nrd = 0;
148 >        biglob(hp)->nrd = rtrunc = 0;
149          for (n = hproto == NULL ? nbeams(hp) : 0; n > 0; n--)
150                  if (hp->bi[n].nrd)
151 <                        if (hp->bi[n].fo + hp->bi[n].nrd > fpos)
152 <                                hp->bi[n].nrd = 0;      /* off end */
153 <                        else
151 >                        if (hp->bi[n].fo + hp->bi[n].nrd > fpos) {
152 >                                rtrunc += hp->bi[n].nrd;
153 >                                hp->bi[n].nrd = 0;
154 >                        } else
155                                  biglob(hp)->nrd += hp->bi[n].nrd;
156 +        if (rtrunc) {
157 +                sprintf(errmsg, "truncated section, %ld rays lost (%.1f%%)",
158 +                                rtrunc, 100.*rtrunc/(rtrunc+biglob(hp)->nrd));
159 +                error(WARNING, errmsg);
160 +        }
161                                          /* add to holodeck list */
162          for (n = 0; n < HDMAX; n++)
163                  if (hdlist[n] == NULL) {
# Line 309 | Line 316 | int    nr;                     /* number of new rays desired */
316          p = hdbray(hp->bl[i]) + hp->bl[i]->nrm;
317          hp->bl[i]->nrm += nr;                   /* update in-core structure */
318          bzero((char *)p, nr*sizeof(RAYVAL));
319 <        hp->bl[i]->tick = hdclock;              /* update LRU clock */
313 <        blglob(hp)->tick = hdclock++;
319 >        blglob(hp)->tick = hp->bl[i]->tick = hdclock++; /* update LRU clock */
320          return(p);                              /* point to new rays */
321   memerr:
322          error(SYSTEM, "out of memory in hdnewrays");
# Line 341 | Line 347 | register int   i;
347                  if (read(hp->fd, (char *)hdbray(hp->bl[i]), n) != n)
348                          error(SYSTEM, "error reading beam from holodeck file");
349          }
350 <        hp->bl[i]->tick = hdclock;      /* update LRU clock */
345 <        blglob(hp)->tick = hdclock++;
350 >        blglob(hp)->tick = hp->bl[i]->tick = hdclock++; /* update LRU clock */
351          return(hp->bl[i]);
352   }
353  
# Line 372 | Line 377 | int    n;                      /* list length */
377   int     (*bf)();                /* callback function (optional) */
378   {
379          unsigned        origcachesize, memuse;
375        register BEAM   *bp;
380          int     bytesloaded, needbytes, bytes2free;
381 +        register BEAM   *bp;
382          register int    i;
383                                          /* precheck consistency */
384 +        if (n <= 0) return;
385          for (i = n; i--; )
386                  if (hb[i].h == NULL || hb[i].b < 1 | hb[i].b > nbeams(hb[i].h))
387                          error(CONSISTENCY, "bad beam in hdloadbeams");
# Line 383 | Line 389 | int    (*bf)();                /* callback function (optional) */
389          qsort((char *)hb, n, sizeof(HDBEAMI), hdfilord);
390          bytesloaded = 0;                /* run through loaded beams */
391          for ( ; n && (bp = hb->h->bl[hb->b]) != NULL; n--, hb++) {
392 <                bp->tick = hdclock;             /* preempt swap */
392 >                bp->tick = hdclock;     /* preempt swap */
393                  bytesloaded += bp->nrm;
394                  if (bf != NULL)
395 <                        (*bf)(bp, hb->h, hb->b);
395 >                        (*bf)(bp, hb);
396          }
397          bytesloaded *= sizeof(RAYVAL);
398          if ((origcachesize = hdcachesize) > 0) {
# Line 405 | Line 411 | int    (*bf)();                /* callback function (optional) */
411          }
412          for (i = 0; i < n; i++)
413                  if ((bp = hdgetbeam(hb[i].h, hb[i].b)) != NULL && bf != NULL)
414 <                        (*bf)(bp, hb[i].h, hb[i].b);
414 >                        (*bf)(bp, hb+i);
415          hdcachesize = origcachesize;    /* resume dynamic swapping */
416   }
417  
418  
419 + hdfreefrag(fd, bi)                      /* free a file fragment */
420 + int     fd;
421 + register BEAMI  *bi;
422 + {
423 +        register struct fraglist        *f;
424 +        register int    j, k;
425 +
426 +        if (bi->nrd == 0)
427 +                return;
428 + #ifdef DEBUG
429 +        if (fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks)
430 +                error(CONSISTENCY, "bad file descriptor in hdfreefrag");
431 + #endif
432 +        f = &hdfragl[fd];
433 +        if (f->nfrags % FRAGBLK == 0) { /* delete empty remnants */
434 +                for (j = k = 0; k < f->nfrags; j++, k++) {
435 +                        while (f->fi[k].nrd == 0)
436 +                                if (++k >= f->nfrags)
437 +                                        goto endloop;
438 +                        if (k > j)
439 +                                copystruct(f->fi+j, f->fi+k);
440 +                }
441 +        endloop:
442 +                f->nfrags = j;
443 +        }
444 +        j = f->nfrags++;                /* allocate a slot in free list */
445 + #if MAXFRAG
446 +        if (j >= MAXFRAG-1)
447 +                f->nfrags--;
448 + #endif
449 +        if (j % FRAGBLK == 0) {         /* more free list space */
450 +                if (f->fi == NULL)
451 +                        f->fi = (BEAMI *)malloc(FRAGBLK*sizeof(BEAMI));
452 +                else
453 +                        f->fi = (BEAMI *)realloc((char *)f->fi,
454 +                                        (j+FRAGBLK)*sizeof(BEAMI));
455 +                if (f->fi == NULL)
456 +                        error(SYSTEM, "out of memory in hdfreefrag");
457 +        }
458 +        for ( ; ; j--) {                /* insert in descending list */
459 +                if (!j || bi->fo < f->fi[j-1].fo) {
460 +                        f->fi[j].fo = bi->fo;
461 +                        f->fi[j].nrd = bi->nrd;
462 +                        break;
463 +                }
464 +                copystruct(f->fi+j, f->fi+(j-1));
465 +        }
466 +                                        /* coalesce adjacent fragments */
467 +                                                /* successors never empty */
468 +        if (j && f->fi[j-1].fo == f->fi[j].fo + f->fi[j].nrd*sizeof(RAYVAL)) {
469 +                f->fi[j].nrd += f->fi[j-1].nrd;
470 +                f->fi[j-1].nrd = 0;
471 +        }
472 +        for (k = j+1; k < f->nfrags; k++)       /* get non-empty predecessor */
473 +                if (f->fi[k].nrd) {
474 +                        if (f->fi[j].fo == f->fi[k].fo +
475 +                                        f->fi[k].nrd*sizeof(RAYVAL)) {
476 +                                f->fi[k].nrd += f->fi[j].nrd;
477 +                                f->fi[j].nrd = 0;
478 +                        }
479 +                        break;
480 +                }
481 + }
482 +
483 +
484 + long
485 + hdallocfrag(fd, nrays)          /* allocate a file fragment */
486 + int     fd;
487 + unsigned int4   nrays;
488 + {
489 +        register struct fraglist        *f;
490 +        register int    j, k;
491 +        long    nfo;
492 +
493 +        if (nrays == 0)
494 +                return(-1L);
495 + #ifdef DEBUG
496 +        if (fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks)
497 +                error(CONSISTENCY, "bad file descriptor in hdallocfrag");
498 + #endif
499 +        f = &hdfragl[fd];
500 +        k = -1;                         /* find closest-sized fragment */
501 +        for (j = f->nfrags; j-- > 0; )
502 +                if (f->fi[j].nrd >= nrays &&
503 +                                (k < 0 || f->fi[j].nrd < f->fi[k].nrd))
504 +                        if (f->fi[k=j].nrd == nrays)
505 +                                break;
506 +        if (k < 0) {                    /* no fragment -- extend file */
507 +                nfo = f->flen;
508 +                f->flen += nrays*sizeof(RAYVAL);
509 +        } else {                        /* else use fragment */
510 +                nfo = f->fi[k].fo;
511 +                f->fi[k].fo += nrays*sizeof(RAYVAL);
512 +                f->fi[k].nrd -= nrays;
513 +        }
514 +        return(nfo);
515 + }
516 +
517 +
518   int
519   hdsyncbeam(hp, i)               /* sync beam in memory with beam on disk */
520   register HOLO   *hp;
521   register int    i;
522   {
523 <        unsigned int    nrays;
419 <        long    nfo;
523 >        unsigned int4   nrays;
524          unsigned int    n;
525 +        long    nfo;
526                                          /* check file status */
527          if (hdfragl[hp->fd].writerr)
528                  return(-1);
# Line 428 | Line 533 | register int   i;
533                                          /* is current fragment OK? */
534          if (hp->bl[i] == NULL || (nrays = hp->bl[i]->nrm) == hp->bi[i].nrd)
535                  return(0);
536 <                                        /* locate fragment */
537 <        if (hp->bi[i].fo + hp->bi[i].nrd*sizeof(RAYVAL) ==
538 <                        hdfragl[hp->fd].flen)           /* EOF special case */
539 <                hdfragl[hp->fd].flen = (nfo=hp->bi[i].fo) + nrays*sizeof(RAYVAL);
435 <
436 <        else {                                          /* general case */
437 <                register struct fraglist        *f = &hdfragl[hp->fd];
438 <                register int    j, k;
439 <                n = f->nfrags;          /* relinquish old fragment */
440 <                if (hp->bi[i].nrd) {
441 <                        j = f->nfrags++;
442 < #if MAXFRAG
443 <                        if (j >= MAXFRAG-1)
444 <                                f->nfrags--;
445 < #endif
446 <                        if (j % FRAGBLK == 0) {         /* more frag. space */
447 <                                if (f->fi == NULL)
448 <                                        f->fi = (BEAMI *)malloc(
449 <                                                        FRAGBLK*sizeof(BEAMI));
450 <                                else
451 <                                        f->fi = (BEAMI *)realloc((char *)f->fi,
452 <                                                (j+FRAGBLK)*sizeof(BEAMI));
453 <                                if (f->fi == NULL)
454 <                                        error(SYSTEM,
455 <                                                "out of memory in hdsyncbeam");
456 <                        }
457 <                        for ( ; ; j--) {        /* insert in descending list */
458 <                                if (!j || hp->bi[i].fo < f->fi[j-1].fo) {
459 <                                        f->fi[j].fo = hp->bi[i].fo;
460 <                                        f->fi[j].nrd = hp->bi[i].nrd;
461 <                                        break;
462 <                                }
463 <                                copystruct(f->fi+j, f->fi+(j-1));
464 <                        }
465 <                                        /* coalesce adjacent fragments */
466 <                        if (j && f->fi[j-1].fo == f->fi[j].fo +
467 <                                        f->fi[j].nrd*sizeof(RAYVAL)) {
468 <                                f->fi[j].nrd += f->fi[j-1].nrd;
469 <                                f->fi[j-1].nrd = 0;
470 <                                n = j-1;
471 <                        }
472 <                        if (j+1 < f->nfrags && f->fi[j].fo == f->fi[j+1].fo +
473 <                                        f->fi[j+1].nrd*sizeof(RAYVAL)) {
474 <                                f->fi[j+1].nrd += f->fi[j].nrd;
475 <                                f->fi[j].nrd = 0;
476 <                                if (j < n) n = j;
477 <                        }
478 <                }
479 <                k = -1;                 /* find closest-sized fragment */
480 <                for (j = (nrays ? f->nfrags : 0); j-- > 0; )
481 <                        if (f->fi[j].nrd >= nrays &&
482 <                                        (k < 0 || f->fi[j].nrd < f->fi[k].nrd))
483 <                                if (f->fi[k=j].nrd == nrays)
484 <                                        break;
485 <                if (k < 0) {            /* no fragment -- extend file */
486 <                        nfo = f->flen;
487 <                        f->flen += nrays*sizeof(RAYVAL);
488 <                } else {                /* else use fragment */
489 <                        nfo = f->fi[k].fo;
490 <                        f->fi[k].fo += nrays*sizeof(RAYVAL);
491 <                        if (!(f->fi[k].nrd -= nrays) && k < n)
492 <                                n = k;
493 <                }
494 <                                        /* delete empty remnants */
495 <                for (j = k = n; k < f->nfrags; j++, k++) {
496 <                        while (f->fi[k].nrd == 0)
497 <                                if (++k >= f->nfrags)
498 <                                        goto endloop;
499 <                        if (k > j)
500 <                                copystruct(f->fi+j, f->fi+k);
501 <                }
502 <        endloop:
503 <                f->nfrags = j;
504 <        }
505 <        if (nrays) {            /* write the new fragment */
536 >        if (hp->bi[i].nrd)              /* relinquish old fragment */
537 >                hdfreefrag(hp->fd, &hp->bi[i]);
538 >        if (nrays) {                    /* get and write new fragment */
539 >                nfo = hdallocfrag(hp->fd, nrays);
540                  errno = 0;
541                  if (lseek(hp->fd, nfo, 0) < 0)
542                          error(SYSTEM, "cannot seek on holodeck file");
# Line 512 | Line 546 | register int   i;
546                          hdsync(NULL, 0);        /* sync directories */
547                          error(SYSTEM, "write error in hdsyncbeam");
548                  }
549 <        }
549 >                hp->bi[i].fo = nfo;
550 >        } else
551 >                hp->bi[i].fo = 0L;
552          biglob(hp)->nrd += nrays - hp->bi[i].nrd;
553          hp->bi[i].nrd = nrays;
554 <        hp->bi[i].fo = nfo;
519 <        markdirty(hp);          /* section directory now out of date */
554 >        markdirty(hp);                  /* section directory now out of date */
555          return(1);
556   }
557  
# Line 612 | Line 647 | register HOLO  *hp;                    /* section we're adding from */
647   {
648          register int    i, j;
649                                          /* insert each beam from hp */
650 <        for (i = nbeams(hp); i > 0; i--) {
650 >        for (i = 1; i <= nbeams(hp); i++) {
651                  if (hp->bl[i] == NULL)          /* check if loaded */
652                          continue;
653   #if 0
# Line 644 | Line 679 | register HOLO  *honly;                 /* NULL means check all */
679          int     freetarget;
680          int     n;
681          register int    i;
682 + #ifdef DEBUG
683 +        unsigned        membefore;
684 +
685 +        membefore = hdmemuse(0);
686 + #endif
687                                                  /* compute free target */
688          freetarget = (honly != NULL) ? blglob(honly)->nrm :
689                          hdmemuse(0)/sizeof(RAYVAL) ;
# Line 664 | Line 704 | register HOLO  *honly;                 /* NULL means check all */
704                          break;
705          }
706          hdsync(honly, 0);       /* synchronize directories as necessary */
707 + #ifdef DEBUG
708 +        sprintf(errmsg,
709 +        "%dK before, %dK after hdfreecache (%dK total), %d rays short\n",
710 +                membefore>>10, hdmemuse(0)>>10, hdmemuse(1)>>10, freetarget);
711 +        wputs(errmsg);
712 + #endif
713          return(-freetarget);    /* return how far past goal we went */
714   }
715  
# Line 676 | Line 722 | register HOLO  *hp;            /* NULL means clean up all */
722          if (hp == NULL) {               /* NULL means clean up everything */
723                  while (hdlist[0] != NULL)
724                          hddone(hdlist[0]);
725 +                free((char *)hdfragl);
726 +                hdfragl = NULL; nhdfragls = 0;
727                  return;
728          }
729                                          /* flush all data and free memory */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines