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.12 by gregl, Fri Dec 12 19:52:31 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 173 | Line 180 | int    all;
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 dirty? */
183 >        if (!hp->dirty)                 /* directory clean? */
184                  return(0);
185          errno = 0;
186          if (lseek(hp->fd, biglob(hp)->fo, 0) < 0)
# Line 186 | Line 193 | int    all;
193   }
194  
195  
196 < long
196 > unsigned
197   hdmemuse(all)           /* return memory usage (in bytes) */
198   int     all;                    /* include overhead (painful) */
199   {
# Line 301 | 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 333 | 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 + 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;
# Line 347 | Line 409 | register int   i;
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");
# Line 354 | Line 419 | register int   i;
419                                          /* is current fragment OK? */
420          if (hp->bl[i] == NULL || (nrays = hp->bl[i]->nrm) == hp->bi[i].nrd)
421                  return(0);
422 <                                        /* check file status */
358 <        if (hp->dirty < 0)
359 <                return(-1);
360 <
422 >                                        /* locate fragment */
423          if (hp->fd >= nhdfrags || !hdfrag[hp->fd].nlinks) /* untracked */
424                  hp->bi[i].fo = lseek(hp->fd, 0L, 2);
425  
# Line 392 | 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 422 | Line 484 | register int   i;
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;
# Line 435 | Line 497 | register int   i;
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 <                        hp->dirty = -1;         /* avoid recursive error */
500 >                        hdfrag[hp->fd].writerr++;
501 >                        hdsync(hp, 0);          /* sync directory */
502                          error(SYSTEM, "write error in hdsyncbeam");
503                  }
504          }
# Line 460 | 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 = nbeams(hp); i > 0; i--)
# Line 527 | Line 592 | register int   i;
592   }
593  
594  
595 < hdlrulist(ha, ba, n, hp)        /* add beams from holodeck to LRU list */
596 < register HOLO   *ha[];                  /* section list (NULL terminated) */
597 < register int    ba[];                   /* beam index to go with section */
598 < int     n;                              /* length of arrays minus 1 */
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;
537        int     nents;
538                                        /* find last entry in LRU list */
539        for (j = 0; ha[j] != NULL; j++)
540                ;
541        nents = j;
603                                          /* insert each beam from hp */
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];
556 <                        ba[j] = ba[j-1];
620 >                        copystruct(hb+j, hb+(j-1));
621                  }
622          }
623 <        ha[nents] = NULL;               /* all done */
560 <        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];
569 <        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;
578 <        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, 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