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.8 by gregl, Tue Nov 11 11:31:54 1997 UTC vs.
Revision 3.14 by gregl, Thu Dec 18 15:12:23 1997 UTC

# Line 31 | Line 31 | static char SCCSid[] = "$SunId$ SGI";
31  
32   #define FRAGBLK         64      /* number of fragments to allocate at a time */
33  
34 < int     hdcachesize = CACHESIZE*1024*1024;      /* target cache size (bytes) */
34 > unsigned        hdcachesize = CACHESIZE*1024*1024;      /* target cache size */
35   unsigned long   hdclock;        /* clock value */
36  
37   HOLO    *hdlist[HDMAX+1];       /* holodeck pointers (NULL term.) */
38  
39   static struct fragment {
40          short   nlinks;         /* number of holodeck sections using us */
41 <        short   nfrags;         /* number of known fragments */
41 >        short   writerr;        /* write error encountered */
42 >        int     nfrags;         /* number of known fragments */
43          BEAMI   *fi;            /* fragments, descending file position */
44          long    flen;           /* last known file length */
45   } *hdfrag;              /* fragment lists, indexed by file descriptor */
# Line 138 | Line 139 | HDGRID *hproto;                /* holodeck section grid */
139          hp->fd = fd;    
140          hp->dirty = 0;
141          biglob(hp)->fo = fpos + sizeof(HDGRID);
142 <        biglob(hp)->nrd = 0;            /* count rays on disk */
143 <        for (n = nbeams(hp); n > 0; n--)
144 <                biglob(hp)->nrd += hp->bi[n].nrd;
142 >                                        /* start tracking fragments */
143 >        hdattach(fd);
144 >                                        /* check rays on disk */
145 >        fpos = hdfilen(fd);
146 >        biglob(hp)->nrd = 0;
147 >        for (n = hproto == NULL ? nbeams(hp) : 0; n > 0; n--)
148 >                if (hp->bi[n].nrd)
149 >                        if (hp->bi[n].fo + hp->bi[n].nrd > fpos)
150 >                                hp->bi[n].nrd = 0;      /* off end */
151 >                        else
152 >                                biglob(hp)->nrd += hp->bi[n].nrd;
153                                          /* add to holodeck list */
154          for (n = 0; n < HDMAX; n++)
155                  if (hdlist[n] == NULL) {
156                          hdlist[n] = hp;
157                          break;
158                  }
150                                        /* start tracking fragments (last) */
151        hdattach(fd);
159                                          /* all done */
160          return(hp);
161   memerr:
# Line 157 | Line 164 | memerr:
164  
165  
166   int
167 < hdsync(hp)                      /* update directory on disk if necessary */
167 > hdsync(hp, all)                 /* update beams and directory on disk */
168   register HOLO   *hp;
169 + int     all;
170   {
171          register int    j, n;
172  
173 <        if (hp == NULL) {               /* do all */
173 >        if (hp == NULL) {               /* do all holodecks */
174                  n = 0;
175                  for (j = 0; hdlist[j] != NULL; j++)
176 <                        n += hdsync(hdlist[j]);
176 >                        n += hdsync(hdlist[j], all);
177                  return(n);
178          }
179 <        if (!hp->dirty)                 /* check first */
179 >                                        /* sync the beams */
180 >        for (j = all ? nbeams(hp) : 0; j > 0; j--)
181 >                if (hp->bl[j] != NULL)
182 >                        hdsyncbeam(hp, j);
183 >        if (!hp->dirty)                 /* directory clean? */
184                  return(0);
185          errno = 0;
186          if (lseek(hp->fd, biglob(hp)->fo, 0) < 0)
# Line 181 | Line 193 | register HOLO  *hp;
193   }
194  
195  
196 < long
196 > unsigned
197   hdmemuse(all)           /* return memory usage (in bytes) */
198   int     all;                    /* include overhead (painful) */
199   {
# Line 296 | Line 308 | int    nr;                     /* number of new rays desired */
308          p = hdbray(hp->bl[i]) + hp->bl[i]->nrm;
309          hp->bl[i]->nrm += nr;                   /* update in-core structure */
310          bzero((char *)p, nr*sizeof(RAYVAL));
311 <        hp->bl[i]->tick = ++hdclock;            /* update LRU clock */
312 <        blglob(hp)->tick = hdclock;
311 >        hp->bl[i]->tick = hdclock;              /* update LRU clock */
312 >        blglob(hp)->tick = hdclock++;
313          return(p);                              /* point to new rays */
314   memerr:
315          error(SYSTEM, "out of memory in hdnewrays");
# Line 328 | Line 340 | register int   i;
340                  if (read(hp->fd, (char *)hdbray(hp->bl[i]), n) != n)
341                          error(SYSTEM, "error reading beam from holodeck file");
342          }
343 <        hp->bl[i]->tick = ++hdclock;    /* update LRU clock */
344 <        blglob(hp)->tick = hdclock;
343 >        hp->bl[i]->tick = hdclock;      /* update LRU clock */
344 >        blglob(hp)->tick = hdclock++;
345          return(hp->bl[i]);
346   }
347  
348  
349   int
350 < hdgetbi(hp, i)                  /* allocate a file fragment */
350 > hdfilord(hb1, hb2)      /* order beams for optimal loading */
351 > register HDBEAMI        *hb1, *hb2;
352 > {
353 >        register int    c;
354 >                                /* sort by file descriptor first */
355 >        if ((c = hb1->h->fd - hb2->h->fd))
356 >                return(c);
357 >                                /* then by position in file */
358 >        return(hb1->h->bi[hb1->b].fo - hb2->h->bi[hb2->b].fo);
359 > }
360 >
361 >
362 > hdloadbeams(hb, n, bf)  /* load a list of beams in optimal order */
363 > register HDBEAMI        *hb;    /* list gets sorted by hdfilord() */
364 > int     n;                      /* list length */
365 > int     (*bf)();                /* callback function (optional) */
366 > {
367 >        unsigned        origcachesize, memuse;
368 >        register BEAM   *bp;
369 >        int     bytesloaded, needbytes, bytes2free;
370 >        register int    i;
371 >                                        /* precheck consistency */
372 >        for (i = n; i--; )
373 >                if (hb[i].h == NULL || hb[i].b < 1 | hb[i].b > nbeams(hb[i].h))
374 >                        error(CONSISTENCY, "bad beam in hdloadbeams");
375 >                                        /* sort list for optimal access */
376 >        qsort((char *)hb, n, sizeof(HDBEAMI), hdfilord);
377 >        if ((origcachesize = hdcachesize) == 0)
378 >                goto loadbeams;
379 >        bytesloaded = needbytes = 0;    /* figure out memory needs */
380 >        for (i = 0; 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 > loadbeams:
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;
408   {
409 <        int     nrays = hp->bl[i]->nrm;
410 <
411 <        if (hp->bi[i].nrd == nrays)     /* current one will do? */
409 >        unsigned int    nrays;
410 >        long    nfo;
411 >        unsigned int    n;
412 >                                        /* check file status */
413 >        if (hdfrag[hp->fd].writerr)
414 >                return(-1);
415 > #ifdef DEBUG
416 >        if (i < 1 | i > nbeams(hp))
417 >                error(CONSISTENCY, "bad beam index in hdsyncbeam");
418 > #endif
419 >                                        /* is current fragment OK? */
420 >        if (hp->bl[i] == NULL || (nrays = hp->bl[i]->nrm) == hp->bi[i].nrd)
421                  return(0);
422 <
422 >                                        /* locate fragment */
423          if (hp->fd >= nhdfrags || !hdfrag[hp->fd].nlinks) /* untracked */
424                  hp->bi[i].fo = lseek(hp->fd, 0L, 2);
425  
426          else if (hp->bi[i].fo + hp->bi[i].nrd*sizeof(RAYVAL) ==
427                          hdfrag[hp->fd].flen)            /* EOF special case */
428 <                hdfrag[hp->fd].flen = hp->bi[i].fo + nrays*sizeof(RAYVAL);
428 >                hdfrag[hp->fd].flen = (nfo=hp->bi[i].fo) + nrays*sizeof(RAYVAL);
429  
430          else {                                          /* general case */
431                  register struct fragment        *f = &hdfrag[hp->fd];
# Line 370 | Line 446 | register int   i;
446                                                  (j+FRAGBLK)*sizeof(BEAMI));
447                                  if (f->fi == NULL)
448                                          error(SYSTEM,
449 <                                                "out of memory in hdgetbi");
449 >                                                "out of memory in hdsyncbeam");
450                          }
451                          for ( ; ; j--) {        /* insert in descending list */
452                                  if (!j || hp->bi[i].fo < f->fi[j-1].fo) {
# Line 378 | 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++) {
# Line 395 | Line 471 | register int   i;
471                          f->nfrags = j;
472                  }
473                  k = -1;                 /* find closest-sized fragment */
474 <                for (j = f->nfrags; j-- > 0; )
474 >                for (j = nrays ? f->nfrags : 0; j-- > 0; )
475                          if (f->fi[j].nrd >= nrays &&
476                                          (k < 0 || f->fi[j].nrd < f->fi[k].nrd))
477                                  if (f->fi[k=j].nrd == nrays)
478                                          break;
479                  if (k < 0) {            /* no fragment -- extend file */
480 <                        hp->bi[i].fo = f->flen;
480 >                        nfo = f->flen;
481                          f->flen += nrays*sizeof(RAYVAL);
482                  } else {                /* else use fragment */
483 <                        hp->bi[i].fo = f->fi[k].fo;
483 >                        nfo = f->fi[k].fo;
484                          if (f->fi[k].nrd == nrays) {    /* delete fragment */
485                                  f->nfrags--;
486                                  for (j = k; j < f->nfrags; j++)
487 <                                        copystruct(f->fi+j, f->fi+j+1);
487 >                                        copystruct(f->fi+j, f->fi+(j+1));
488                          } else {                        /* else shrink it */
489                                  f->fi[k].fo += nrays*sizeof(RAYVAL);
490                                  f->fi[k].nrd -= nrays;
491                          }
492                  }
493          }
494 +        if (nrays) {            /* write the new fragment */
495 +                errno = 0;
496 +                if (lseek(hp->fd, nfo, 0) < 0)
497 +                        error(SYSTEM, "cannot seek on holodeck file");
498 +                n = hp->bl[i]->nrm * sizeof(RAYVAL);
499 +                if (write(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) {
500 +                        hdfrag[hp->fd].writerr++;
501 +                        hdsync(hp, 0);          /* sync directory */
502 +                        error(SYSTEM, "write error in hdsyncbeam");
503 +                }
504 +        }
505          biglob(hp)->nrd += nrays - hp->bi[i].nrd;
506          hp->bi[i].nrd = nrays;
507 +        hp->bi[i].fo = nfo;
508          markdirty(hp);          /* section directory now out of date */
509          return(1);
510   }
# Line 427 | Line 515 | hdfreebeam(hp, i)              /* free beam, writing if dirty */
515   register HOLO   *hp;
516   register int    i;
517   {
518 <        int     nchanged, n;
518 >        int     nchanged;
519  
520          if (hp == NULL) {               /* clear all holodecks */
521                  nchanged = 0;
# Line 435 | Line 523 | register int   i;
523                          nchanged += hdfreebeam(hdlist[i], 0);
524                  return(nchanged);
525          }
526 +        if (hdfrag[hp->fd].writerr)     /* check for file error */
527 +                return(0);
528          if (i == 0) {                   /* clear entire holodeck */
529                  nchanged = 0;
530 <                for (i = 1; i <= nbeams(hp); i++)
531 <                        nchanged += hdfreebeam(hp, i);
530 >                for (i = nbeams(hp); i > 0; i--)
531 >                        if (hp->bl[i] != NULL)
532 >                                nchanged += hdfreebeam(hp, i);
533                  return(nchanged);
534          }
535 + #ifdef DEBUG
536          if (i < 1 | i > nbeams(hp))
537                  error(CONSISTENCY, "bad beam index to hdfreebeam");
538 + #endif
539          if (hp->bl[i] == NULL)
540                  return(0);
541                                          /* check for additions */
542          nchanged = hp->bl[i]->nrm - hp->bi[i].nrd;
543 <        if (nchanged) {
544 <                hdgetbi(hp, i);                 /* allocate a file position */
452 <                errno = 0;
453 <                if (lseek(hp->fd, hp->bi[i].fo, 0) < 0)
454 <                        error(SYSTEM, "cannot seek on holodeck file");
455 <                n = hp->bl[i]->nrm * sizeof(RAYVAL);
456 <                if (write(hp->fd, (char *)hdbray(hp->bl[i]), n) != n)
457 <                        error(SYSTEM, "write error in hdfreebeam");
458 <        }
543 >        if (nchanged)
544 >                hdsyncbeam(hp, i);              /* write new fragment */
545          blglob(hp)->nrm -= hp->bl[i]->nrm;
546          free((char *)hp->bl[i]);                /* free memory */
547          hp->bl[i] = NULL;
# Line 463 | Line 549 | register int   i;
549   }
550  
551  
552 < hdlrulist(ha, ba, n, hp)        /* add beams from holodeck to LRU list */
553 < register HOLO   *ha[];                  /* section list (NULL terminated) */
554 < register int    ba[];                   /* beam index to go with section */
555 < int     n;                              /* length of arrays minus 1 */
552 > int
553 > hdkillbeam(hp, i)               /* delete beam from holodeck */
554 > register HOLO   *hp;
555 > register int    i;
556 > {
557 >        static BEAM     emptybeam;
558 >        int     nchanged;
559 >
560 >        if (hp == NULL) {               /* clobber all holodecks */
561 >                nchanged = 0;
562 >                for (i = 0; hdlist[i] != NULL; i++)
563 >                        nchanged += hdkillbeam(hdlist[i], 0);
564 >                return(nchanged);
565 >        }
566 >        if (i == 0) {                   /* clobber entire holodeck */
567 >                nchanged = 0;
568 >                for (i = nbeams(hp); i > 0; i--)
569 >                        if (hp->bi[i].nrd > 0 || hp->bl[i] != NULL)
570 >                                nchanged += hdkillbeam(hp, i);
571 > #ifdef DEBUG
572 >                if (biglob(hp)->nrd != 0 | blglob(hp)->nrm != 0)
573 >                        error(CONSISTENCY, "bad beam count in hdkillbeam");
574 > #endif
575 >                return(nchanged);
576 >        }
577 > #ifdef DEBUG
578 >        if (i < 1 | i > nbeams(hp))
579 >                error(CONSISTENCY, "bad beam index to hdkillbeam");
580 > #endif
581 >        if (hp->bl[i] != NULL) {        /* free memory */
582 >                blglob(hp)->nrm -= nchanged = hp->bl[i]->nrm;
583 >                free((char *)hp->bl[i]);
584 >        } else
585 >                nchanged = hp->bi[i].nrd;
586 >        if (hp->bi[i].nrd) {            /* free file fragment */
587 >                hp->bl[i] = &emptybeam;
588 >                hdsyncbeam(hp, i);
589 >        }
590 >        hp->bl[i] = NULL;
591 >        return(nchanged);
592 > }
593 >
594 >
595 > int
596 > hdlrulist(hb, nents, n, hp)     /* add beams from holodeck to LRU list */
597 > register HDBEAMI        *hb;            /* beam list */
598 > int     nents;                          /* current list length */
599 > int     n;                              /* maximum list length */
600   register HOLO   *hp;                    /* section we're adding from */
601   {
602          register int    i, j;
473        int     nents;
474                                        /* find last entry in LRU list */
475        for (j = 0; ha[j] != NULL; j++)
476                ;
477        nents = j;
603                                          /* insert each beam from hp */
604 <        for (i = nbeams(hp); i > 0; i-- ) {
604 >        for (i = nbeams(hp); i > 0; i--) {
605                  if (hp->bl[i] == NULL)          /* check if loaded */
606                          continue;
607 <                if ((j = ++nents) > n)          /* grow list if we can */
607 > #if 0
608 >                if (hp->bl[i]->tick == hdclock) /* preempt swap? */
609 >                        continue;
610 > #endif
611 >                if ((j = ++nents) >= n)         /* grow list if we can */
612                          nents--;
613                  for ( ; ; ) {                   /* bubble into place */
614                          if (!--j || hp->bl[i]->tick >=
615 <                                        ha[j-1]->bl[ba[j-1]]->tick) {
616 <                                ha[j] = hp;
617 <                                ba[j] = i;
615 >                                        hb[j-1].h->bl[hb[j-1].b]->tick) {
616 >                                hb[j].h = hp;
617 >                                hb[j].b = i;
618                                  break;
619                          }
620 <                        ha[j] = ha[j-1];
492 <                        ba[j] = ba[j-1];
620 >                        copystruct(hb+j, hb+(j-1));
621                  }
622          }
623 <        ha[nents] = NULL;               /* all done */
496 <        ba[nents] = 0;
623 >        return(nents);                  /* return new list length */
624   }
625  
626  
627 + int
628   hdfreecache(pct, honly)         /* free up cache space, writing changes */
629   int     pct;                            /* maximum percentage to free */
630   register HOLO   *honly;                 /* NULL means check all */
631   {
632 <        HOLO    *hp[FREEBEAMS+1];
505 <        int     bn[FREEBEAMS+1];
632 >        HDBEAMI hb[FREEBEAMS];
633          int     freetarget;
634 +        int     n;
635          register int    i;
636                                                  /* compute free target */
637          freetarget = (honly != NULL) ? blglob(honly)->nrm :
638                          hdmemuse(0)/sizeof(RAYVAL) ;
639          freetarget = freetarget*pct/100;
640 +        if (freetarget <= 0)
641 +                return(0);
642                                                  /* find least recently used */
643 <        hp[0] = NULL;
514 <        bn[0] = 0;
643 >        n = 0;
644          if (honly != NULL)
645 <                hdlrulist(hp, bn, FREEBEAMS, honly);
645 >                n = hdlrulist(hb, n, FREEBEAMS, honly);
646          else
647                  for (i = 0; hdlist[i] != NULL; i++)
648 <                        hdlrulist(hp, bn, FREEBEAMS, hdlist[i]);
648 >                        n = hdlrulist(hb, n, FREEBEAMS, hdlist[i]);
649                                                  /* free LRU beams */
650 <        for (i = 0; hp[i] != NULL; i++) {
651 <                hdfreebeam(hp[i], bn[i]);
652 <                if ((freetarget -= hp[i]->bi[bn[i]].nrd) <= 0)
650 >        for (i = 0; i < n; i++) {
651 >                hdfreebeam(hb[i].h, hb[i].b);
652 >                if ((freetarget -= hb[i].h->bi[hb[i].b].nrd) <= 0)
653                          break;
654          }
655 <        hdsync(honly);          /* synchronize directories as necessary */
655 >        hdsync(honly, 0);       /* synchronize directories as necessary */
656 >        return(-freetarget);    /* return how far past goal we went */
657   }
658  
659  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines