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.15 by gregl, Mon Dec 29 15:17:45 1997 UTC vs.
Revision 3.19 by gregl, Sun Jan 4 12:32:53 1998 UTC

# Line 22 | Line 22 | static char SCCSid[] = "$SunId$ SGI";
22   #define PCTFREE         20      /* maximum fraction to free (%) */
23   #endif
24   #ifndef MAXFRAG
25 < #define MAXFRAG         131000  /* maximum fragments/file to track (0==inf) */
25 > #define MAXFRAG         32767   /* maximum fragments/file to track (0==inf) */
26   #endif
27  
28   #ifndef BSD
# Line 30 | Line 30 | static char SCCSid[] = "$SunId$ SGI";
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   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 75 | 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 213 | 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 231 | 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 348 | Line 348 | register int   i;
348  
349  
350   int
351 < hdfilord(hb1, hb2)      /* order beams for optimal loading */
351 > hdfilord(hb1, hb2)      /* order beams for quick loading */
352   register HDBEAMI        *hb1, *hb2;
353   {
354 <        register int    c;
355 <                                /* sort by file descriptor first */
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 <        return(hb1->h->bi[hb1->b].fo - hb2->h->bi[hb2->b].fo);
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  
# Line 375 | Line 381 | int    (*bf)();                /* callback function (optional) */
381                          error(CONSISTENCY, "bad beam in hdloadbeams");
382                                          /* sort list for optimal access */
383          qsort((char *)hb, n, sizeof(HDBEAMI), hdfilord);
384 <        bytesloaded = needbytes = 0;    /* figure out memory needs */
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 <                        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);
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 - (signed)(hdcachesize-memuse);
400 <                        if (bytes2free > (signed)(memuse - bytesloaded))
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          }
396        hdcachesize = 0;                /* load the ordered beams w/o swap */
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);
# Line 401 | Line 410 | int    (*bf)();                /* callback function (optional) */
410   }
411  
412  
413 + hdfreefrag(fd, bi)                      /* free a file fragment */
414 + int     fd;
415 + register BEAMI  *bi;
416 + {
417 +        register struct fraglist        *f;
418 +        register int    j, k;
419 +
420 +        if (bi->nrd == 0)
421 +                return;
422 + #ifdef DEBUG
423 +        if (fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks)
424 +                error(CONSISTENCY, "bad file descriptor in hdfreefrag");
425 + #endif
426 +        f = &hdfragl[fd];
427 +        if (f->nfrags % FRAGBLK == 0) { /* delete empty remnants */
428 +                for (j = k = 0; k < f->nfrags; j++, k++) {
429 +                        while (f->fi[k].nrd == 0)
430 +                                if (++k >= f->nfrags)
431 +                                        goto endloop;
432 +                        if (k > j)
433 +                                copystruct(f->fi+j, f->fi+k);
434 +                }
435 +        endloop:
436 +                f->nfrags = j;
437 +        }
438 +        j = f->nfrags++;                /* allocate a slot in free list */
439 + #if MAXFRAG
440 +        if (j >= MAXFRAG-1)
441 +                f->nfrags--;
442 + #endif
443 +        if (j % FRAGBLK == 0) {         /* more free list space */
444 +                if (f->fi == NULL)
445 +                        f->fi = (BEAMI *)malloc(FRAGBLK*sizeof(BEAMI));
446 +                else
447 +                        f->fi = (BEAMI *)realloc((char *)f->fi,
448 +                                        (j+FRAGBLK)*sizeof(BEAMI));
449 +                if (f->fi == NULL)
450 +                        error(SYSTEM, "out of memory in hdfreefrag");
451 +        }
452 +        for ( ; ; j--) {                /* insert in descending list */
453 +                if (!j || bi->fo < f->fi[j-1].fo) {
454 +                        f->fi[j].fo = bi->fo;
455 +                        f->fi[j].nrd = bi->nrd;
456 +                        break;
457 +                }
458 +                copystruct(f->fi+j, f->fi+(j-1));
459 +        }
460 +                                        /* coalesce adjacent fragments */
461 +                                                /* successors never empty */
462 +        if (j && f->fi[j-1].fo == f->fi[j].fo + f->fi[j].nrd*sizeof(RAYVAL)) {
463 +                f->fi[j].nrd += f->fi[j-1].nrd;
464 +                f->fi[j-1].nrd = 0;
465 +        }
466 +        for (k = j+1; k < f->nfrags; k++)       /* get non-empty predecessor */
467 +                if (f->fi[k].nrd) {
468 +                        if (f->fi[j].fo == f->fi[k].fo +
469 +                                        f->fi[k].nrd*sizeof(RAYVAL)) {
470 +                                f->fi[k].nrd += f->fi[j].nrd;
471 +                                f->fi[j].nrd = 0;
472 +                        }
473 +                        break;
474 +                }
475 + }
476 +
477 +
478 + long
479 + hdallocfrag(fd, nrays)          /* allocate a file fragment */
480 + int     fd;
481 + unsigned int4   nrays;
482 + {
483 +        register struct fraglist        *f;
484 +        register int    j, k;
485 +        long    nfo;
486 +
487 +        if (nrays == 0)
488 +                return(-1L);
489 + #ifdef DEBUG
490 +        if (fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks)
491 +                error(CONSISTENCY, "bad file descriptor in hdallocfrag");
492 + #endif
493 +        f = &hdfragl[fd];
494 +        k = -1;                         /* find closest-sized fragment */
495 +        for (j = f->nfrags; j-- > 0; )
496 +                if (f->fi[j].nrd >= nrays &&
497 +                                (k < 0 || f->fi[j].nrd < f->fi[k].nrd))
498 +                        if (f->fi[k=j].nrd == nrays)
499 +                                break;
500 +        if (k < 0) {                    /* no fragment -- extend file */
501 +                nfo = f->flen;
502 +                f->flen += nrays*sizeof(RAYVAL);
503 +        } else {                        /* else use fragment */
504 +                nfo = f->fi[k].fo;
505 +                f->fi[k].fo += nrays*sizeof(RAYVAL);
506 +                f->fi[k].nrd -= nrays;
507 +        }
508 +        return(nfo);
509 + }
510 +
511 +
512   int
513   hdsyncbeam(hp, i)               /* sync beam in memory with beam on disk */
514   register HOLO   *hp;
515   register int    i;
516   {
517 <        unsigned int    nrays;
410 <        long    nfo;
517 >        unsigned int4   nrays;
518          unsigned int    n;
519 +        long    nfo;
520                                          /* check file status */
521 <        if (hdfrag[hp->fd].writerr)
521 >        if (hdfragl[hp->fd].writerr)
522                  return(-1);
523   #ifdef DEBUG
524          if (i < 1 | i > nbeams(hp))
# Line 419 | Line 527 | register int   i;
527                                          /* is current fragment OK? */
528          if (hp->bl[i] == NULL || (nrays = hp->bl[i]->nrm) == hp->bi[i].nrd)
529                  return(0);
530 <                                        /* locate fragment */
531 <        if (hp->fd >= nhdfrags || !hdfrag[hp->fd].nlinks) /* untracked */
532 <                hp->bi[i].fo = lseek(hp->fd, 0L, 2);
533 <
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 = (nfo=hp->bi[i].fo) + nrays*sizeof(RAYVAL);
429 <
430 <        else {                                          /* general case */
431 <                register struct fragment        *f = &hdfrag[hp->fd];
432 <                register int    j, k;
433 <                                        /* relinquish old fragment */
434 <                if (hp->bi[i].nrd) {
435 <                        j = f->nfrags++;
436 < #if MAXFRAG
437 <                        if (j >= MAXFRAG-1)
438 <                                f->nfrags--;
439 < #endif
440 <                        if (j % FRAGBLK == 0) {         /* more frag. space */
441 <                                if (f->fi == NULL)
442 <                                        f->fi = (BEAMI *)malloc(
443 <                                                        FRAGBLK*sizeof(BEAMI));
444 <                                else
445 <                                        f->fi = (BEAMI *)realloc((char *)f->fi,
446 <                                                (j+FRAGBLK)*sizeof(BEAMI));
447 <                                if (f->fi == NULL)
448 <                                        error(SYSTEM,
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) {
453 <                                        f->fi[j].fo = hp->bi[i].fo;
454 <                                        f->fi[j].nrd = hp->bi[i].nrd;
455 <                                        break;
456 <                                }
457 <                                copystruct(f->fi+j, f->fi+(j-1));
458 <                        }
459 <                                        /* coalesce adjacent fragments */
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 <                        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; )
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)
476 <                                        break;
477 <                if (k < 0) {            /* no fragment -- extend file */
478 <                        nfo = f->flen;
479 <                        f->flen += nrays*sizeof(RAYVAL);
480 <                } else {                /* else use fragment */
481 <                        nfo = f->fi[k].fo;
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 */
530 >        if (hp->bi[i].nrd)              /* relinquish old fragment */
531 >                hdfreefrag(hp->fd, &hp->bi[i]);
532 >        if (nrays) {                    /* get and write new fragment */
533 >                nfo = hdallocfrag(hp->fd, nrays);
534                  errno = 0;
535                  if (lseek(hp->fd, nfo, 0) < 0)
536                          error(SYSTEM, "cannot seek on holodeck file");
537                  n = hp->bl[i]->nrm * sizeof(RAYVAL);
538                  if (write(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) {
539 <                        hdfrag[hp->fd].writerr++;
540 <                        hdsync(hp, 0);          /* sync directory */
539 >                        hdfragl[hp->fd].writerr++;
540 >                        hdsync(NULL, 0);        /* sync directories */
541                          error(SYSTEM, "write error in hdsyncbeam");
542                  }
543 <        }
543 >                hp->bi[i].fo = nfo;
544 >        } else
545 >                hp->bi[i].fo = 0L;
546          biglob(hp)->nrd += nrays - hp->bi[i].nrd;
547          hp->bi[i].nrd = nrays;
548 <        hp->bi[i].fo = nfo;
510 <        markdirty(hp);          /* section directory now out of date */
548 >        markdirty(hp);                  /* section directory now out of date */
549          return(1);
550   }
551  
# Line 525 | Line 563 | register int   i;
563                          nchanged += hdfreebeam(hdlist[i], 0);
564                  return(nchanged);
565          }
566 <        if (hdfrag[hp->fd].writerr)     /* check for file error */
566 >        if (hdfragl[hp->fd].writerr)    /* check for file error */
567                  return(0);
568          if (i == 0) {                   /* clear entire holodeck */
569                  nchanged = 0;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines