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.18 by gregl, Thu Jan 1 16:40:38 1998 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         32767   /* 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
31   #endif
32  
33 < #define FRAGBLK         64      /* number of fragments to allocate at a time */
33 > #define FRAGBLK         256     /* 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.) */
39  
40 < static struct fragment {
40 > static struct fraglist {
41          short   nlinks;         /* number of holodeck sections using us */
42          short   writerr;        /* write error encountered */
43          int     nfrags;         /* number of known fragments */
44          BEAMI   *fi;            /* fragments, descending file position */
45          long    flen;           /* last known file length */
46 < } *hdfrag;              /* fragment lists, indexed by file descriptor */
46 > } *hdfragl;             /* fragment lists, indexed by file descriptor */
47  
48 < static int      nhdfrags;       /* size of hdfrag array */
48 > static int      nhdfragls;      /* size of hdfragl array */
49  
50  
51   hdattach(fd)            /* start tracking file fragments for some section */
52   register int    fd;
53   {
54 <        if (fd >= nhdfrags) {
55 <                if (nhdfrags)
56 <                        hdfrag = (struct fragment *)realloc((char *)hdfrag,
57 <                                        (fd+1)*sizeof(struct fragment));
54 >        if (fd >= nhdfragls) {
55 >                if (nhdfragls)
56 >                        hdfragl = (struct fraglist *)realloc((char *)hdfragl,
57 >                                        (fd+1)*sizeof(struct fraglist));
58                  else
59 <                        hdfrag = (struct fragment *)malloc(
60 <                                        (fd+1)*sizeof(struct fragment));
61 <                if (hdfrag == NULL)
59 >                        hdfragl = (struct fraglist *)malloc(
60 >                                        (fd+1)*sizeof(struct fraglist));
61 >                if (hdfragl == NULL)
62                          error(SYSTEM, "out of memory in hdattach");
63 <                bzero((char *)(hdfrag+nhdfrags),
64 <                                (fd+1-nhdfrags)*sizeof(struct fragment));
65 <                nhdfrags = fd+1;
63 >                bzero((char *)(hdfragl+nhdfragls),
64 >                                (fd+1-nhdfragls)*sizeof(struct fraglist));
65 >                nhdfragls = fd+1;
66          }
67 <        hdfrag[fd].nlinks++;
68 <        hdfrag[fd].flen = lseek(fd, 0L, 2);     /* get file length */
67 >        hdfragl[fd].nlinks++;
68 >        hdfragl[fd].flen = lseek(fd, 0L, 2);    /* get file length */
69   }
70  
71  
# Line 74 | Line 75 | register int   fd;
75   hdrelease(fd)           /* stop tracking file fragments for some section */
76   register int    fd;
77   {
78 <        if (fd < 0 | fd >= nhdfrags || !hdfrag[fd].nlinks)
78 >        if (fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks)
79                  return;
80 <        if (!--hdfrag[fd].nlinks && hdfrag[fd].nfrags) {
81 <                free((char *)hdfrag[fd].fi);
82 <                hdfrag[fd].fi = NULL;
83 <                hdfrag[fd].nfrags = 0;
80 >        if (!--hdfragl[fd].nlinks && hdfragl[fd].nfrags) {
81 >                free((char *)hdfragl[fd].fi);
82 >                hdfragl[fd].fi = NULL;
83 >                hdfragl[fd].nfrags = 0;
84          }
85   }
86  
# 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 212 | Line 213 | int    all;                    /* include overhead (painful) */
213                  }
214          }
215          if (all)
216 <                for (j = 0; j < nhdfrags; j++) {
217 <                        total += sizeof(struct fragment);
218 <                        if (hdfrag[j].nfrags)
216 >                for (j = 0; j < nhdfragls; j++) {
217 >                        total += sizeof(struct fraglist);
218 >                        if (hdfragl[j].nfrags)
219                                  total += FRAGBLK*sizeof(BEAMI) *
220 <                                        ((hdfrag[j].nfrags-1)/FRAGBLK + 1) ;
220 >                                        ((hdfragl[j].nfrags-1)/FRAGBLK + 1) ;
221                  }
222          return(total);
223   }
# Line 230 | Line 231 | int    fd;
231  
232          if (fd < 0)
233                  return(-1);
234 <        if (fd >= nhdfrags || !hdfrag[fd].nlinks) {
234 >        if (fd >= nhdfragls || !hdfragl[fd].nlinks) {
235                  if ((fpos = lseek(fd, 0L, 1)) < 0)
236                          return(-1);
237                  flen = lseek(fd, 0L, 2);
238                  lseek(fd, fpos, 0);
239                  return(flen);
240          }
241 <        return(hdfrag[fd].flen);
241 >        return(hdfragl[fd].flen);
242   }
243  
244  
# 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 quick loading */
352 + register HDBEAMI        *hb1, *hb2;
353 + {
354 +        register long   c;
355 +                                /* residents go first */
356 +        if (hb2->h->bl[hb2->b] != NULL)
357 +                return(hb1->h->bl[hb1->b] == NULL);
358 +        if (hb1->h->bl[hb1->b] != NULL)
359 +                return(-1);
360 +                                /* otherwise sort by file descriptor */
361 +        if ((c = hb1->h->fd - hb2->h->fd))
362 +                return(c);
363 +                                /* then by position in file */
364 +        c = hb1->h->bi[hb1->b].fo - hb2->h->bi[hb2->b].fo;
365 +        return(c > 0 ? 1 : c < 0 ? -1 : 0);
366 + }
367 +
368 +
369 + hdloadbeams(hb, n, bf)  /* load a list of beams in optimal order */
370 + register HDBEAMI        *hb;    /* list gets sorted by hdfilord() */
371 + int     n;                      /* list length */
372 + int     (*bf)();                /* callback function (optional) */
373 + {
374 +        unsigned        origcachesize, memuse;
375 +        register BEAM   *bp;
376 +        int     bytesloaded, needbytes, bytes2free;
377 +        register int    i;
378 +                                        /* precheck consistency */
379 +        for (i = n; i--; )
380 +                if (hb[i].h == NULL || hb[i].b < 1 | hb[i].b > nbeams(hb[i].h))
381 +                        error(CONSISTENCY, "bad beam in hdloadbeams");
382 +                                        /* sort list for optimal access */
383 +        qsort((char *)hb, n, sizeof(HDBEAMI), hdfilord);
384 +        bytesloaded = 0;                /* run through loaded beams */
385 +        for ( ; n && (bp = hb->h->bl[hb->b]) != NULL; n--, hb++) {
386 +                bp->tick = hdclock;             /* preempt swap */
387 +                bytesloaded += bp->nrm;
388 +                if (bf != NULL)
389 +                        (*bf)(bp, hb->h, hb->b);
390 +        }
391 +        bytesloaded *= sizeof(RAYVAL);
392 +        if ((origcachesize = hdcachesize) > 0) {
393 +                needbytes = 0;          /* figure out memory needs */
394 +                for (i = n; i--; )
395 +                        needbytes += hb[i].h->bi[hb[i].b].nrd;
396 +                needbytes *= sizeof(RAYVAL);
397 +                do {                            /* free enough memory */
398 +                        memuse = hdmemuse(0);
399 +                        bytes2free = needbytes - (int)(hdcachesize-memuse);
400 +                        if (bytes2free > (int)(memuse - bytesloaded))
401 +                                bytes2free = memuse - bytesloaded;
402 +                } while (bytes2free > 0 &&
403 +                                hdfreecache(100*bytes2free/memuse, NULL) < 0);
404 +                hdcachesize = 0;                /* load beams w/o swap */
405 +        }
406 +        for (i = 0; i < n; i++)
407 +                if ((bp = hdgetbeam(hb[i].h, hb[i].b)) != NULL && bf != NULL)
408 +                        (*bf)(bp, hb[i].h, hb[i].b);
409 +        hdcachesize = origcachesize;    /* resume dynamic swapping */
410 + }
411 +
412 +
413 + int
414   hdsyncbeam(hp, i)               /* sync beam in memory with beam on disk */
415   register HOLO   *hp;
416   register int    i;
# Line 355 | Line 419 | register int   i;
419          long    nfo;
420          unsigned int    n;
421                                          /* check file status */
422 <        if (hdfrag[hp->fd].writerr)
422 >        if (hdfragl[hp->fd].writerr)
423                  return(-1);
424   #ifdef DEBUG
425          if (i < 1 | i > nbeams(hp))
# Line 365 | Line 429 | register int   i;
429          if (hp->bl[i] == NULL || (nrays = hp->bl[i]->nrm) == hp->bi[i].nrd)
430                  return(0);
431                                          /* locate fragment */
432 <        if (hp->fd >= nhdfrags || !hdfrag[hp->fd].nlinks) /* untracked */
433 <                hp->bi[i].fo = lseek(hp->fd, 0L, 2);
432 >        if (hp->bi[i].fo + hp->bi[i].nrd*sizeof(RAYVAL) ==
433 >                        hdfragl[hp->fd].flen)           /* EOF special case */
434 >                hdfragl[hp->fd].flen = (nfo=hp->bi[i].fo) + nrays*sizeof(RAYVAL);
435  
371        else if (hp->bi[i].fo + hp->bi[i].nrd*sizeof(RAYVAL) ==
372                        hdfrag[hp->fd].flen)            /* EOF special case */
373                hdfrag[hp->fd].flen = (nfo=hp->bi[i].fo) + nrays*sizeof(RAYVAL);
374
436          else {                                          /* general case */
437 <                register struct fragment        *f = &hdfrag[hp->fd];
437 >                register struct fraglist        *f = &hdfragl[hp->fd];
438                  register int    j, k;
439 <                                        /* relinquish old fragment */
439 >                n = f->nfrags;          /* relinquish old fragment */
440                  if (hp->bi[i].nrd) {
441                          j = f->nfrags++;
442 < #ifdef MAXFRAG
442 > #if MAXFRAG
443                          if (j >= MAXFRAG-1)
444                                  f->nfrags--;
445   #endif
# Line 399 | Line 460 | register int   i;
460                                          f->fi[j].nrd = hp->bi[i].nrd;
461                                          break;
462                                  }
463 <                                copystruct(f->fi+j, f->fi+j-1);
463 >                                copystruct(f->fi+j, f->fi+(j-1));
464                          }
465                                          /* coalesce adjacent fragments */
466 <                        for (j = k = 0; k < f->nfrags; j++, k++) {
467 <                                if (k > j)
468 <                                        copystruct(f->fi+j, f->fi+k);
469 <                                while (k+1 < f->nfrags && f->fi[k+1].fo +
470 <                                                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 <                                }
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 <                        f->nfrags = j;
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; )
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)
# Line 426 | Line 487 | register int   i;
487                          f->flen += nrays*sizeof(RAYVAL);
488                  } else {                /* else use fragment */
489                          nfo = f->fi[k].fo;
490 <                        if (f->fi[k].nrd == nrays) {    /* delete fragment */
491 <                                f->nfrags--;
492 <                                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 <                        }
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 */
506                  errno = 0;
# Line 442 | Line 508 | register int   i;
508                          error(SYSTEM, "cannot seek on holodeck file");
509                  n = hp->bl[i]->nrm * sizeof(RAYVAL);
510                  if (write(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) {
511 <                        hdfrag[hp->fd].writerr++;
512 <                        hdsync(hp, 0);          /* sync directory */
511 >                        hdfragl[hp->fd].writerr++;
512 >                        hdsync(NULL, 0);        /* sync directories */
513                          error(SYSTEM, "write error in hdsyncbeam");
514                  }
515          }
# Line 468 | Line 534 | register int   i;
534                          nchanged += hdfreebeam(hdlist[i], 0);
535                  return(nchanged);
536          }
537 <        if (hdfrag[hp->fd].writerr)     /* check for file error */
537 >        if (hdfragl[hp->fd].writerr)    /* check for file error */
538                  return(0);
539          if (i == 0) {                   /* clear entire holodeck */
540                  nchanged = 0;
# Line 537 | Line 603 | register int   i;
603   }
604  
605  
606 < hdlrulist(ha, ba, n, hp)        /* add beams from holodeck to LRU list */
607 < register HOLO   *ha[];                  /* section list (NULL terminated) */
608 < register int    ba[];                   /* beam index to go with section */
609 < int     n;                              /* length of arrays minus 1 */
606 > int
607 > hdlrulist(hb, nents, n, hp)     /* add beams from holodeck to LRU list */
608 > register HDBEAMI        *hb;            /* beam list */
609 > int     nents;                          /* current list length */
610 > int     n;                              /* maximum list length */
611   register HOLO   *hp;                    /* section we're adding from */
612   {
613          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;
614                                          /* insert each beam from hp */
615          for (i = nbeams(hp); i > 0; i--) {
616                  if (hp->bl[i] == NULL)          /* check if loaded */
617                          continue;
618 <                if ((j = ++nents) > n)          /* grow list if we can */
618 > #if 0
619 >                if (hp->bl[i]->tick == hdclock) /* preempt swap? */
620 >                        continue;
621 > #endif
622 >                if ((j = ++nents) >= n)         /* grow list if we can */
623                          nents--;
624                  for ( ; ; ) {                   /* bubble into place */
625                          if (!--j || hp->bl[i]->tick >=
626 <                                        ha[j-1]->bl[ba[j-1]]->tick) {
627 <                                ha[j] = hp;
628 <                                ba[j] = i;
626 >                                        hb[j-1].h->bl[hb[j-1].b]->tick) {
627 >                                hb[j].h = hp;
628 >                                hb[j].b = i;
629                                  break;
630                          }
631 <                        ha[j] = ha[j-1];
566 <                        ba[j] = ba[j-1];
631 >                        copystruct(hb+j, hb+(j-1));
632                  }
633          }
634 <        ha[nents] = NULL;               /* all done */
570 <        ba[nents] = 0;
634 >        return(nents);                  /* return new list length */
635   }
636  
637  
638 + int
639   hdfreecache(pct, honly)         /* free up cache space, writing changes */
640   int     pct;                            /* maximum percentage to free */
641   register HOLO   *honly;                 /* NULL means check all */
642   {
643 <        HOLO    *hp[FREEBEAMS+1];
579 <        int     bn[FREEBEAMS+1];
643 >        HDBEAMI hb[FREEBEAMS];
644          int     freetarget;
645 +        int     n;
646          register int    i;
647                                                  /* compute free target */
648          freetarget = (honly != NULL) ? blglob(honly)->nrm :
649                          hdmemuse(0)/sizeof(RAYVAL) ;
650          freetarget = freetarget*pct/100;
651 +        if (freetarget <= 0)
652 +                return(0);
653                                                  /* find least recently used */
654 <        hp[0] = NULL;
588 <        bn[0] = 0;
654 >        n = 0;
655          if (honly != NULL)
656 <                hdlrulist(hp, bn, FREEBEAMS, honly);
656 >                n = hdlrulist(hb, n, FREEBEAMS, honly);
657          else
658                  for (i = 0; hdlist[i] != NULL; i++)
659 <                        hdlrulist(hp, bn, FREEBEAMS, hdlist[i]);
659 >                        n = hdlrulist(hb, n, FREEBEAMS, hdlist[i]);
660                                                  /* free LRU beams */
661 <        for (i = 0; hp[i] != NULL; i++) {
662 <                hdfreebeam(hp[i], bn[i]);
663 <                if ((freetarget -= hp[i]->bi[bn[i]].nrd) <= 0)
661 >        for (i = 0; i < n; i++) {
662 >                hdfreebeam(hb[i].h, hb[i].b);
663 >                if ((freetarget -= hb[i].h->bi[hb[i].b].nrd) <= 0)
664                          break;
665          }
666          hdsync(honly, 0);       /* synchronize directories as necessary */
667 +        return(-freetarget);    /* return how far past goal we went */
668   }
669  
670  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines