--- ray/src/hd/holofile.c 1998/01/01 13:00:14 3.17 +++ ray/src/hd/holofile.c 1998/09/18 16:47:27 3.26 @@ -1,4 +1,4 @@ -/* Copyright (c) 1997 Silicon Graphics, Inc. */ +/* Copyright (c) 1998 Silicon Graphics, Inc. */ #ifndef lint static char SCCSid[] = "$SunId$ SGI"; @@ -16,10 +16,10 @@ static char SCCSid[] = "$SunId$ SGI"; #define CACHESIZE 16 /* default cache size (Mbytes, 0==inf) */ #endif #ifndef FREEBEAMS -#define FREEBEAMS 512 /* maximum beams to free at a time */ +#define FREEBEAMS 1500 /* maximum beams to free at a time */ #endif #ifndef PCTFREE -#define PCTFREE 20 /* maximum fraction to free (%) */ +#define PCTFREE 15 /* maximum fraction to free (%) */ #endif #ifndef MAXFRAG #define MAXFRAG 32767 /* maximum fragments/file to track (0==inf) */ @@ -48,18 +48,36 @@ static struct fraglist { static int nhdfragls; /* size of hdfragl array */ +char * +hdrealloc(ptr, siz, rout) /* (re)allocate memory, retry then error */ +char *ptr; +unsigned siz; +char *rout; +{ + register char *newp; + /* call malloc/realloc */ + if (ptr == NULL) newp = (char *)malloc(siz); + else newp = (char *)realloc(ptr, siz); + /* check success */ + if (newp == NULL && rout != NULL) { + hdfreecache(25, NULL); /* free some memory */ + errno = 0; /* retry */ + newp = hdrealloc(ptr, siz, NULL); + if (newp == NULL) { /* give up and report error */ + sprintf(errmsg, "out of memory in %s", rout); + error(SYSTEM, errmsg); + } + } + return(newp); +} + + hdattach(fd) /* start tracking file fragments for some section */ register int fd; { if (fd >= nhdfragls) { - if (nhdfragls) - hdfragl = (struct fraglist *)realloc((char *)hdfragl, - (fd+1)*sizeof(struct fraglist)); - else - hdfragl = (struct fraglist *)malloc( - (fd+1)*sizeof(struct fraglist)); - if (hdfragl == NULL) - error(SYSTEM, "out of memory in hdattach"); + hdfragl = (struct fraglist *)hdrealloc((char *)hdfragl, + (fd+1)*sizeof(struct fraglist), "hdattach"); bzero((char *)(hdfragl+nhdfragls), (fd+1-nhdfragls)*sizeof(struct fraglist)); nhdfragls = fd+1; @@ -105,6 +123,7 @@ hdinit(fd, hproto) /* initialize a holodeck section in int fd; /* corresponding file descriptor */ HDGRID *hproto; /* holodeck section grid */ { + long rtrunc; long fpos; register HOLO *hp; register int n; @@ -126,7 +145,7 @@ HDGRID *hproto; /* holodeck section grid */ error(SYSTEM, "failure loading holodeck directory"); /* check that it's clean */ if (hp->bi[nbeams(hp)].fo < 0) - error(USER, "dirty holodeck section"); + error(WARNING, "dirty holodeck section"); } else { /* assume we're creating it */ if ((hp = hdalloc(hproto)) == NULL) goto memerr; @@ -144,13 +163,19 @@ HDGRID *hproto; /* holodeck section grid */ hdattach(fd); /* check rays on disk */ fpos = hdfilen(fd); - biglob(hp)->nrd = 0; + biglob(hp)->nrd = rtrunc = 0; for (n = hproto == NULL ? nbeams(hp) : 0; n > 0; n--) if (hp->bi[n].nrd) - if (hp->bi[n].fo + hp->bi[n].nrd > fpos) - hp->bi[n].nrd = 0; /* off end */ - else + if (hp->bi[n].fo + hp->bi[n].nrd > fpos) { + rtrunc += hp->bi[n].nrd; + hp->bi[n].nrd = 0; + } else biglob(hp)->nrd += hp->bi[n].nrd; + if (rtrunc) { + sprintf(errmsg, "truncated section, %ld rays lost (%.1f%%)", + rtrunc, 100.*rtrunc/(rtrunc+biglob(hp)->nrd)); + error(WARNING, errmsg); + } /* add to holodeck list */ for (n = 0; n < HDMAX; n++) if (hdlist[n] == NULL) { @@ -285,13 +310,12 @@ int nr; /* number of new rays desired */ hp->bl[i]->tick = hdclock; /* preempt swap */ if (hdcachesize > 0 && hdmemuse(0) >= hdcachesize) hdfreecache(PCTFREE, NULL); /* free some space */ - errno = 0; if (hp->bl[i] == NULL) { /* allocate (and load) */ n = hp->bi[i].nrd + nr; - if ((hp->bl[i] = (BEAM *)malloc(hdbsiz(n))) == NULL) - goto memerr; + hp->bl[i] = (BEAM *)hdrealloc(NULL, hdbsiz(n), "hdnewrays"); blglob(hp)->nrm += n; if (n = hp->bl[i]->nrm = hp->bi[i].nrd) { + errno = 0; if (lseek(hp->fd, hp->bi[i].fo, 0) < 0) error(SYSTEM, "seek error on holodeck file"); n *= sizeof(RAYVAL); @@ -300,20 +324,15 @@ int nr; /* number of new rays desired */ "error reading beam from holodeck file"); } } else { /* just grow in memory */ - hp->bl[i] = (BEAM *)realloc( (char *)hp->bl[i], - hdbsiz(hp->bl[i]->nrm + nr) ); - if (hp->bl[i] == NULL) - goto memerr; + hp->bl[i] = (BEAM *)hdrealloc((char *)hp->bl[i], + hdbsiz(hp->bl[i]->nrm + nr), "hdnewrays"); blglob(hp)->nrm += nr; } p = hdbray(hp->bl[i]) + hp->bl[i]->nrm; hp->bl[i]->nrm += nr; /* update in-core structure */ bzero((char *)p, nr*sizeof(RAYVAL)); - hp->bl[i]->tick = hdclock; /* update LRU clock */ - blglob(hp)->tick = hdclock++; + blglob(hp)->tick = hp->bl[i]->tick = hdclock++; /* update LRU clock */ return(p); /* point to new rays */ -memerr: - error(SYSTEM, "out of memory in hdnewrays"); } @@ -331,18 +350,16 @@ register int i; return(NULL); if (hdcachesize > 0 && hdmemuse(0) >= hdcachesize) hdfreecache(PCTFREE, NULL); /* get free space */ - errno = 0; - if ((hp->bl[i] = (BEAM *)malloc(hdbsiz(n))) == NULL) - error(SYSTEM, "cannot allocate memory for beam"); + hp->bl[i] = (BEAM *)hdrealloc(NULL, hdbsiz(n), "hdgetbeam"); blglob(hp)->nrm += hp->bl[i]->nrm = n; + errno = 0; if (lseek(hp->fd, hp->bi[i].fo, 0) < 0) error(SYSTEM, "seek error on holodeck file"); n *= sizeof(RAYVAL); if (read(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) error(SYSTEM, "error reading beam from holodeck file"); } - hp->bl[i]->tick = hdclock; /* update LRU clock */ - blglob(hp)->tick = hdclock++; + blglob(hp)->tick = hp->bl[i]->tick = hdclock++; /* update LRU clock */ return(hp->bl[i]); } @@ -372,10 +389,11 @@ int n; /* list length */ int (*bf)(); /* callback function (optional) */ { unsigned origcachesize, memuse; - register BEAM *bp; int bytesloaded, needbytes, bytes2free; + register BEAM *bp; register int i; /* precheck consistency */ + if (n <= 0) return; for (i = n; i--; ) if (hb[i].h == NULL || hb[i].b < 1 | hb[i].b > nbeams(hb[i].h)) error(CONSISTENCY, "bad beam in hdloadbeams"); @@ -383,10 +401,10 @@ int (*bf)(); /* callback function (optional) */ qsort((char *)hb, n, sizeof(HDBEAMI), hdfilord); bytesloaded = 0; /* run through loaded beams */ for ( ; n && (bp = hb->h->bl[hb->b]) != NULL; n--, hb++) { - bp->tick = hdclock; /* preempt swap */ + bp->tick = hdclock; /* preempt swap */ bytesloaded += bp->nrm; if (bf != NULL) - (*bf)(bp, hb->h, hb->b); + (*bf)(bp, hb); } bytesloaded *= sizeof(RAYVAL); if ((origcachesize = hdcachesize) > 0) { @@ -405,19 +423,122 @@ int (*bf)(); /* callback function (optional) */ } for (i = 0; i < n; i++) if ((bp = hdgetbeam(hb[i].h, hb[i].b)) != NULL && bf != NULL) - (*bf)(bp, hb[i].h, hb[i].b); + (*bf)(bp, hb+i); hdcachesize = origcachesize; /* resume dynamic swapping */ } +hdfreefrag(fd, bi) /* free a file fragment */ +int fd; +register BEAMI *bi; +{ + register struct fraglist *f; + register int j, k; + + if (bi->nrd == 0) + return; +#ifdef DEBUG + if (fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks) + error(CONSISTENCY, "bad file descriptor in hdfreefrag"); +#endif + f = &hdfragl[fd]; + if (f->nfrags % FRAGBLK == 0) { /* delete empty remnants */ + for (j = k = 0; k < f->nfrags; j++, k++) { + while (f->fi[k].nrd == 0) + if (++k >= f->nfrags) + goto endloop; + if (k > j) + copystruct(f->fi+j, f->fi+k); + } + endloop: + f->nfrags = j; + } + j = f->nfrags++; /* allocate a slot in free list */ +#if MAXFRAG + if (j >= MAXFRAG-1) + f->nfrags--; +#endif + if (j % FRAGBLK == 0) { /* more free list space */ + register BEAMI *newp; + if (f->fi == NULL) + newp = (BEAMI *)malloc((j+FRAGBLK)*sizeof(BEAMI)); + else + newp = (BEAMI *)realloc((char *)f->fi, + (j+FRAGBLK)*sizeof(BEAMI)); + if (newp == NULL) { + f->nfrags--; /* graceful failure */ + return; + } + f->fi = newp; + } + for ( ; ; j--) { /* insert in descending list */ + if (!j || bi->fo < f->fi[j-1].fo) { + f->fi[j].fo = bi->fo; + f->fi[j].nrd = bi->nrd; + break; + } + copystruct(f->fi+j, f->fi+(j-1)); + } + /* coalesce adjacent fragments */ + /* successors never empty */ + if (j && f->fi[j-1].fo == f->fi[j].fo + f->fi[j].nrd*sizeof(RAYVAL)) { + f->fi[j].nrd += f->fi[j-1].nrd; + f->fi[j-1].nrd = 0; + } + for (k = j+1; k < f->nfrags; k++) /* get non-empty predecessor */ + if (f->fi[k].nrd) { + if (f->fi[j].fo == f->fi[k].fo + + f->fi[k].nrd*sizeof(RAYVAL)) { + f->fi[k].nrd += f->fi[j].nrd; + f->fi[j].nrd = 0; + } + break; + } +} + + +long +hdallocfrag(fd, nrays) /* allocate a file fragment */ +int fd; +unsigned int4 nrays; +{ + register struct fraglist *f; + register int j, k; + long nfo; + + if (nrays == 0) + return(-1L); +#ifdef DEBUG + if (fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks) + error(CONSISTENCY, "bad file descriptor in hdallocfrag"); +#endif + f = &hdfragl[fd]; + k = -1; /* find closest-sized fragment */ + for (j = f->nfrags; j-- > 0; ) + if (f->fi[j].nrd >= nrays && + (k < 0 || f->fi[j].nrd < f->fi[k].nrd)) + if (f->fi[k=j].nrd == nrays) + break; + if (k < 0) { /* no fragment -- extend file */ + nfo = f->flen; + f->flen += nrays*sizeof(RAYVAL); + } else { /* else use fragment */ + nfo = f->fi[k].fo; + f->fi[k].fo += nrays*sizeof(RAYVAL); + f->fi[k].nrd -= nrays; + } + return(nfo); +} + + int hdsyncbeam(hp, i) /* sync beam in memory with beam on disk */ register HOLO *hp; register int i; { - unsigned int nrays; - long nfo; + unsigned int4 nrays; unsigned int n; + long nfo; /* check file status */ if (hdfragl[hp->fd].writerr) return(-1); @@ -428,98 +549,25 @@ register int i; /* is current fragment OK? */ if (hp->bl[i] == NULL || (nrays = hp->bl[i]->nrm) == hp->bi[i].nrd) return(0); - /* locate fragment */ - if (hp->fd >= nhdfragls || !hdfragl[hp->fd].nlinks) /* untracked */ - hp->bi[i].fo = lseek(hp->fd, 0L, 2); - - else if (hp->bi[i].fo + hp->bi[i].nrd*sizeof(RAYVAL) == - hdfragl[hp->fd].flen) /* EOF special case */ - hdfragl[hp->fd].flen = (nfo=hp->bi[i].fo) + nrays*sizeof(RAYVAL); - - else { /* general case */ - register struct fraglist *f = &hdfragl[hp->fd]; - register int j, k; - n = f->nfrags; /* relinquish old fragment */ - if (hp->bi[i].nrd) { - j = f->nfrags++; -#if MAXFRAG - if (j >= MAXFRAG-1) - f->nfrags--; -#endif - if (j % FRAGBLK == 0) { /* more frag. space */ - if (f->fi == NULL) - f->fi = (BEAMI *)malloc( - FRAGBLK*sizeof(BEAMI)); - else - f->fi = (BEAMI *)realloc((char *)f->fi, - (j+FRAGBLK)*sizeof(BEAMI)); - if (f->fi == NULL) - error(SYSTEM, - "out of memory in hdsyncbeam"); - } - for ( ; ; j--) { /* insert in descending list */ - if (!j || hp->bi[i].fo < f->fi[j-1].fo) { - f->fi[j].fo = hp->bi[i].fo; - f->fi[j].nrd = hp->bi[i].nrd; - break; - } - copystruct(f->fi+j, f->fi+(j-1)); - } - /* coalesce adjacent fragments */ - if (j && f->fi[j-1].fo == f->fi[j].fo + - f->fi[j].nrd*sizeof(RAYVAL)) { - f->fi[j].nrd += f->fi[j-1].nrd; - f->fi[j-1].nrd = 0; - n = j-1; - } - if (j+1 < f->nfrags && f->fi[j].fo == f->fi[j+1].fo + - f->fi[j+1].nrd*sizeof(RAYVAL)) { - f->fi[j+1].nrd += f->fi[j].nrd; - f->fi[j].nrd = 0; - if (j < n) n = j; - } - } - k = -1; /* find closest-sized fragment */ - for (j = (nrays ? f->nfrags : 0); j-- > 0; ) - if (f->fi[j].nrd >= nrays && - (k < 0 || f->fi[j].nrd < f->fi[k].nrd)) - if (f->fi[k=j].nrd == nrays) - break; - if (k < 0) { /* no fragment -- extend file */ - nfo = f->flen; - f->flen += nrays*sizeof(RAYVAL); - } else { /* else use fragment */ - nfo = f->fi[k].fo; - f->fi[k].fo += nrays*sizeof(RAYVAL); - if (!(f->fi[k].nrd -= nrays) && k < n) - n = k; - } - /* delete empty remnants */ - for (j = k = n; k < f->nfrags; j++, k++) { - while (f->fi[k].nrd == 0) - if (++k >= f->nfrags) - goto endloop; - if (k > j) - copystruct(f->fi+j, f->fi+k); - } - endloop: - f->nfrags = j; - } - if (nrays) { /* write the new fragment */ + if (hp->bi[i].nrd) /* relinquish old fragment */ + hdfreefrag(hp->fd, &hp->bi[i]); + if (nrays) { /* get and write new fragment */ + nfo = hdallocfrag(hp->fd, nrays); errno = 0; if (lseek(hp->fd, nfo, 0) < 0) error(SYSTEM, "cannot seek on holodeck file"); n = hp->bl[i]->nrm * sizeof(RAYVAL); if (write(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) { hdfragl[hp->fd].writerr++; - hdsync(hp, 0); /* sync directory */ + hdsync(NULL, 0); /* sync directories */ error(SYSTEM, "write error in hdsyncbeam"); } - } + hp->bi[i].fo = nfo; + } else + hp->bi[i].fo = 0L; biglob(hp)->nrd += nrays - hp->bi[i].nrd; hp->bi[i].nrd = nrays; - hp->bi[i].fo = nfo; - markdirty(hp); /* section directory now out of date */ + markdirty(hp); /* section directory now out of date */ return(1); } @@ -615,7 +663,7 @@ register HOLO *hp; /* section we're adding from */ { register int i, j; /* insert each beam from hp */ - for (i = nbeams(hp); i > 0; i--) { + for (i = 1; i <= nbeams(hp); i++) { if (hp->bl[i] == NULL) /* check if loaded */ continue; #if 0 @@ -647,6 +695,11 @@ register HOLO *honly; /* NULL means check all */ int freetarget; int n; register int i; +#ifdef DEBUG + unsigned membefore; + + membefore = hdmemuse(0); +#endif /* compute free target */ freetarget = (honly != NULL) ? blglob(honly)->nrm : hdmemuse(0)/sizeof(RAYVAL) ; @@ -667,6 +720,12 @@ register HOLO *honly; /* NULL means check all */ break; } hdsync(honly, 0); /* synchronize directories as necessary */ +#ifdef DEBUG + sprintf(errmsg, + "%dK before, %dK after hdfreecache (%dK total), %d rays short\n", + membefore>>10, hdmemuse(0)>>10, hdmemuse(1)>>10, freetarget); + wputs(errmsg); +#endif return(-freetarget); /* return how far past goal we went */ } @@ -679,6 +738,8 @@ register HOLO *hp; /* NULL means clean up all */ if (hp == NULL) { /* NULL means clean up everything */ while (hdlist[0] != NULL) hddone(hdlist[0]); + free((char *)hdfragl); + hdfragl = NULL; nhdfragls = 0; return; } /* flush all data and free memory */