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.13 by gregl, Fri Dec 12 21:29:34 1997 UTC vs.
Revision 3.15 by gregl, Mon Dec 29 15:17:45 1997 UTC

# Line 21 | Line 21 | static char SCCSid[] = "$SunId$ SGI";
21   #ifndef PCTFREE
22   #define PCTFREE         20      /* maximum fraction to free (%) */
23   #endif
24 + #ifndef MAXFRAG
25 + #define MAXFRAG         131000  /* maximum fragments/file to track (0==inf) */
26 + #endif
27  
25 /* define MAXFRAG if you want to limit fragment tracking memory */
26
28   #ifndef BSD
29   #define write   writebuf        /* safe i/o routines */
30   #define read    readbuf
# Line 31 | Line 32 | static char SCCSid[] = "$SunId$ SGI";
32  
33   #define FRAGBLK         64      /* number of fragments to allocate at a time */
34  
35 < int     hdcachesize = CACHESIZE*1024*1024;      /* target cache size (bytes) */
35 > unsigned        hdcachesize = CACHESIZE*1024*1024;      /* target cache size */
36   unsigned long   hdclock;        /* clock value */
37  
38   HOLO    *hdlist[HDMAX+1];       /* holodeck pointers (NULL term.) */
# Line 177 | Line 178 | int    all;
178                  return(n);
179          }
180                                          /* sync the beams */
181 <        for (j = all ? nbeams(hp) : 0; j > 0; j--)
181 >        for (j = (all ? nbeams(hp) : 0); j > 0; j--)
182                  if (hp->bl[j] != NULL)
183                          hdsyncbeam(hp, j);
184          if (!hp->dirty)                 /* directory clean? */
# Line 193 | Line 194 | int    all;
194   }
195  
196  
197 < long
197 > unsigned
198   hdmemuse(all)           /* return memory usage (in bytes) */
199   int     all;                    /* include overhead (painful) */
200   {
# Line 308 | Line 309 | int    nr;                     /* number of new rays desired */
309          p = hdbray(hp->bl[i]) + hp->bl[i]->nrm;
310          hp->bl[i]->nrm += nr;                   /* update in-core structure */
311          bzero((char *)p, nr*sizeof(RAYVAL));
312 <        hp->bl[i]->tick = ++hdclock;            /* update LRU clock */
313 <        blglob(hp)->tick = hdclock;
312 >        hp->bl[i]->tick = hdclock;              /* update LRU clock */
313 >        blglob(hp)->tick = hdclock++;
314          return(p);                              /* point to new rays */
315   memerr:
316          error(SYSTEM, "out of memory in hdnewrays");
# Line 340 | Line 341 | register int   i;
341                  if (read(hp->fd, (char *)hdbray(hp->bl[i]), n) != n)
342                          error(SYSTEM, "error reading beam from holodeck file");
343          }
344 <        hp->bl[i]->tick = ++hdclock;    /* update LRU clock */
345 <        blglob(hp)->tick = hdclock;
344 >        hp->bl[i]->tick = hdclock;      /* update LRU clock */
345 >        blglob(hp)->tick = hdclock++;
346          return(hp->bl[i]);
347   }
348  
349  
350   int
351 + hdfilord(hb1, hb2)      /* order beams for optimal loading */
352 + register HDBEAMI        *hb1, *hb2;
353 + {
354 +        register int    c;
355 +                                /* sort by file descriptor first */
356 +        if ((c = hb1->h->fd - hb2->h->fd))
357 +                return(c);
358 +                                /* then by position in file */
359 +        return(hb1->h->bi[hb1->b].fo - hb2->h->bi[hb2->b].fo);
360 + }
361 +
362 +
363 + hdloadbeams(hb, n, bf)  /* load a list of beams in optimal order */
364 + register HDBEAMI        *hb;    /* list gets sorted by hdfilord() */
365 + int     n;                      /* list length */
366 + int     (*bf)();                /* callback function (optional) */
367 + {
368 +        unsigned        origcachesize, memuse;
369 +        register BEAM   *bp;
370 +        int     bytesloaded, needbytes, bytes2free;
371 +        register int    i;
372 +                                        /* precheck consistency */
373 +        for (i = n; i--; )
374 +                if (hb[i].h == NULL || hb[i].b < 1 | hb[i].b > nbeams(hb[i].h))
375 +                        error(CONSISTENCY, "bad beam in hdloadbeams");
376 +                                        /* sort list for optimal access */
377 +        qsort((char *)hb, n, sizeof(HDBEAMI), hdfilord);
378 +        bytesloaded = needbytes = 0;    /* figure out memory needs */
379 +        if ((origcachesize = hdcachesize) > 0) {
380 +                for (i = n; i--; )
381 +                        if ((bp = hb[i].h->bl[hb[i].b]) != NULL) {
382 +                                bp->tick = hdclock;     /* preempt swap */
383 +                                bytesloaded += bp->nrm;
384 +                        } else                          /* prepare to load */
385 +                                needbytes += hb[i].h->bi[hb[i].b].nrd;
386 +                bytesloaded *= sizeof(RAYVAL);
387 +                needbytes *= sizeof(RAYVAL);
388 +                do {                            /* free enough memory */
389 +                        memuse = hdmemuse(0);
390 +                        bytes2free = needbytes - (signed)(hdcachesize-memuse);
391 +                        if (bytes2free > (signed)(memuse - bytesloaded))
392 +                                bytes2free = memuse - bytesloaded;
393 +                } while (bytes2free > 0 &&
394 +                                hdfreecache(100*bytes2free/memuse, NULL) < 0);
395 +        }
396 +        hdcachesize = 0;                /* load the ordered beams w/o swap */
397 +        for (i = 0; i < n; i++)
398 +                if ((bp = hdgetbeam(hb[i].h, hb[i].b)) != NULL && bf != NULL)
399 +                        (*bf)(bp, hb[i].h, hb[i].b);
400 +        hdcachesize = origcachesize;    /* resume dynamic swapping */
401 + }
402 +
403 +
404 + int
405   hdsyncbeam(hp, i)               /* sync beam in memory with beam on disk */
406   register HOLO   *hp;
407   register int    i;
# Line 378 | Line 433 | register int   i;
433                                          /* relinquish old fragment */
434                  if (hp->bi[i].nrd) {
435                          j = f->nfrags++;
436 < #ifdef MAXFRAG
436 > #if MAXFRAG
437                          if (j >= MAXFRAG-1)
438                                  f->nfrags--;
439   #endif
# Line 399 | Line 454 | register int   i;
454                                          f->fi[j].nrd = hp->bi[i].nrd;
455                                          break;
456                                  }
457 <                                copystruct(f->fi+j, f->fi+j-1);
457 >                                copystruct(f->fi+j, f->fi+(j-1));
458                          }
459                                          /* coalesce adjacent fragments */
460 <                        for (j = k = 0; k < f->nfrags; j++, k++) {
461 <                                if (k > j)
462 <                                        copystruct(f->fi+j, f->fi+k);
463 <                                while (k+1 < f->nfrags && f->fi[k+1].fo +
409 <                                                f->fi[k+1].nrd*sizeof(RAYVAL)
410 <                                                        == f->fi[j].fo) {
411 <                                        f->fi[j].fo -=
412 <                                                f->fi[++k].nrd*sizeof(RAYVAL);
413 <                                        f->fi[j].nrd += f->fi[k].nrd;
414 <                                }
460 >                        if (j && f->fi[j-1].fo == f->fi[j].fo +
461 >                                        f->fi[j].nrd*sizeof(RAYVAL)) {
462 >                                f->fi[j].nrd += f->fi[j-1].nrd;
463 >                                f->fi[j-1].nrd = 0;
464                          }
465 <                        f->nfrags = j;
465 >                        if (j+1 < f->nfrags && f->fi[j].fo == f->fi[j+1].fo +
466 >                                        f->fi[j+1].nrd*sizeof(RAYVAL)) {
467 >                                f->fi[j+1].nrd += f->fi[j].nrd;
468 >                                f->fi[j].nrd = 0;
469 >                        }
470                  }
471                  k = -1;                 /* find closest-sized fragment */
472 <                for (j = nrays ? f->nfrags : 0; j-- > 0; )
472 >                for (j = (nrays ? f->nfrags : 0); j-- > 0; )
473                          if (f->fi[j].nrd >= nrays &&
474                                          (k < 0 || f->fi[j].nrd < f->fi[k].nrd))
475                                  if (f->fi[k=j].nrd == nrays)
# Line 426 | Line 479 | register int   i;
479                          f->flen += nrays*sizeof(RAYVAL);
480                  } else {                /* else use fragment */
481                          nfo = f->fi[k].fo;
482 <                        if (f->fi[k].nrd == nrays) {    /* delete fragment */
483 <                                f->nfrags--;
431 <                                for (j = k; j < f->nfrags; j++)
432 <                                        copystruct(f->fi+j, f->fi+j+1);
433 <                        } else {                        /* else shrink it */
434 <                                f->fi[k].fo += nrays*sizeof(RAYVAL);
435 <                                f->fi[k].nrd -= nrays;
436 <                        }
482 >                        f->fi[k].fo += nrays*sizeof(RAYVAL);
483 >                        f->fi[k].nrd -= nrays;
484                  }
485 +                                        /* delete empty remnants */
486 +                for (j = k = 0; k < f->nfrags; j++, k++) {
487 +                        while (f->fi[k].nrd == 0)
488 +                                if (++k >= f->nfrags)
489 +                                        goto endloop;
490 +                        if (k > j)
491 +                                copystruct(f->fi+j, f->fi+k);
492 +                }
493 +        endloop:
494 +                f->nfrags = j;
495          }
496          if (nrays) {            /* write the new fragment */
497                  errno = 0;
# Line 537 | Line 594 | register int   i;
594   }
595  
596  
597 < hdlrulist(ha, ba, n, hp)        /* add beams from holodeck to LRU list */
598 < register HOLO   *ha[];                  /* section list (NULL terminated) */
599 < register int    ba[];                   /* beam index to go with section */
600 < int     n;                              /* length of arrays minus 1 */
597 > int
598 > hdlrulist(hb, nents, n, hp)     /* add beams from holodeck to LRU list */
599 > register HDBEAMI        *hb;            /* beam list */
600 > int     nents;                          /* current list length */
601 > int     n;                              /* maximum list length */
602   register HOLO   *hp;                    /* section we're adding from */
603   {
604          register int    i, j;
547        int     nents;
548                                        /* find last entry in LRU list */
549        for (j = 0; ha[j] != NULL; j++)
550                ;
551        nents = j;
605                                          /* insert each beam from hp */
606          for (i = nbeams(hp); i > 0; i--) {
607                  if (hp->bl[i] == NULL)          /* check if loaded */
608                          continue;
609 <                if ((j = ++nents) > n)          /* grow list if we can */
609 > #if 0
610 >                if (hp->bl[i]->tick == hdclock) /* preempt swap? */
611 >                        continue;
612 > #endif
613 >                if ((j = ++nents) >= n)         /* grow list if we can */
614                          nents--;
615                  for ( ; ; ) {                   /* bubble into place */
616                          if (!--j || hp->bl[i]->tick >=
617 <                                        ha[j-1]->bl[ba[j-1]]->tick) {
618 <                                ha[j] = hp;
619 <                                ba[j] = i;
617 >                                        hb[j-1].h->bl[hb[j-1].b]->tick) {
618 >                                hb[j].h = hp;
619 >                                hb[j].b = i;
620                                  break;
621                          }
622 <                        ha[j] = ha[j-1];
566 <                        ba[j] = ba[j-1];
622 >                        copystruct(hb+j, hb+(j-1));
623                  }
624          }
625 <        ha[nents] = NULL;               /* all done */
570 <        ba[nents] = 0;
625 >        return(nents);                  /* return new list length */
626   }
627  
628  
629 + int
630   hdfreecache(pct, honly)         /* free up cache space, writing changes */
631   int     pct;                            /* maximum percentage to free */
632   register HOLO   *honly;                 /* NULL means check all */
633   {
634 <        HOLO    *hp[FREEBEAMS+1];
579 <        int     bn[FREEBEAMS+1];
634 >        HDBEAMI hb[FREEBEAMS];
635          int     freetarget;
636 +        int     n;
637          register int    i;
638                                                  /* compute free target */
639          freetarget = (honly != NULL) ? blglob(honly)->nrm :
640                          hdmemuse(0)/sizeof(RAYVAL) ;
641          freetarget = freetarget*pct/100;
642 +        if (freetarget <= 0)
643 +                return(0);
644                                                  /* find least recently used */
645 <        hp[0] = NULL;
588 <        bn[0] = 0;
645 >        n = 0;
646          if (honly != NULL)
647 <                hdlrulist(hp, bn, FREEBEAMS, honly);
647 >                n = hdlrulist(hb, n, FREEBEAMS, honly);
648          else
649                  for (i = 0; hdlist[i] != NULL; i++)
650 <                        hdlrulist(hp, bn, FREEBEAMS, hdlist[i]);
650 >                        n = hdlrulist(hb, n, FREEBEAMS, hdlist[i]);
651                                                  /* free LRU beams */
652 <        for (i = 0; hp[i] != NULL; i++) {
653 <                hdfreebeam(hp[i], bn[i]);
654 <                if ((freetarget -= hp[i]->bi[bn[i]].nrd) <= 0)
652 >        for (i = 0; i < n; i++) {
653 >                hdfreebeam(hb[i].h, hb[i].b);
654 >                if ((freetarget -= hb[i].h->bi[hb[i].b].nrd) <= 0)
655                          break;
656          }
657          hdsync(honly, 0);       /* synchronize directories as necessary */
658 +        return(-freetarget);    /* return how far past goal we went */
659   }
660  
661  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines