--- ray/src/hd/holofile.c 1997/10/31 15:48:32 3.2 +++ ray/src/hd/holofile.c 1997/12/29 15:17:45 3.15 @@ -16,45 +16,56 @@ static char SCCSid[] = "$SunId$ SGI"; #define CACHESIZE 16 /* default cache size (Mbytes, 0==inf) */ #endif #ifndef FREEBEAMS -#define FREEBEAMS 1024 /* maximum beams to free at a time */ +#define FREEBEAMS 512 /* maximum beams to free at a time */ #endif #ifndef PCTFREE -#define PCTFREE 12 /* maximum fraction to free (%) */ +#define PCTFREE 20 /* maximum fraction to free (%) */ #endif -#ifndef FFDMAX -#define FFDMAX 64 /* max. file descriptor for tracking */ -#endif #ifndef MAXFRAG -#define MAXFRAG 2048 /* maximum fragments tracked in each file */ +#define MAXFRAG 131000 /* maximum fragments/file to track (0==inf) */ #endif -int hdcachesize = CACHESIZE*1024*1024; /* target cache size (bytes) */ +#ifndef BSD +#define write writebuf /* safe i/o routines */ +#define read readbuf +#endif + +#define FRAGBLK 64 /* number of fragments to allocate at a time */ + +unsigned hdcachesize = CACHESIZE*1024*1024; /* target cache size */ unsigned long hdclock; /* clock value */ HOLO *hdlist[HDMAX+1]; /* holodeck pointers (NULL term.) */ static struct fragment { short nlinks; /* number of holodeck sections using us */ - short nfrags; /* number of known fragments */ - BEAMI fi[MAXFRAG]; /* fragments, descending file position */ + short writerr; /* write error encountered */ + int nfrags; /* number of known fragments */ + BEAMI *fi; /* fragments, descending file position */ long flen; /* last known file length */ -} *hdfrag[FFDMAX]; /* fragment lists, indexed by file descriptor */ +} *hdfrag; /* fragment lists, indexed by file descriptor */ +static int nhdfrags; /* size of hdfrag array */ + hdattach(fd) /* start tracking file fragments for some section */ register int fd; { - if (fd >= FFDMAX) - return; /* descriptor out of range */ - if (hdfrag[fd] == NULL) { - if ((hdfrag[fd] = (struct fragment *) - malloc(sizeof(struct fragment))) == NULL) - return; /* should flag error? */ - hdfrag[fd]->nlinks = 0; - hdfrag[fd]->nfrags = 0; + if (fd >= nhdfrags) { + if (nhdfrags) + hdfrag = (struct fragment *)realloc((char *)hdfrag, + (fd+1)*sizeof(struct fragment)); + else + hdfrag = (struct fragment *)malloc( + (fd+1)*sizeof(struct fragment)); + if (hdfrag == NULL) + error(SYSTEM, "out of memory in hdattach"); + bzero((char *)(hdfrag+nhdfrags), + (fd+1-nhdfrags)*sizeof(struct fragment)); + nhdfrags = fd+1; } - hdfrag[fd]->nlinks++; - hdfrag[fd]->flen = lseek(fd, 0L, 2); /* get file length */ + hdfrag[fd].nlinks++; + hdfrag[fd].flen = lseek(fd, 0L, 2); /* get file length */ } @@ -64,15 +75,31 @@ register int fd; hdrelease(fd) /* stop tracking file fragments for some section */ register int fd; { - if (fd >= FFDMAX || hdfrag[fd] == NULL) + if (fd < 0 | fd >= nhdfrags || !hdfrag[fd].nlinks) return; - if (!--hdfrag[fd]->nlinks) { - free((char *)hdfrag[fd]); - hdfrag[fd] = NULL; + if (!--hdfrag[fd].nlinks && hdfrag[fd].nfrags) { + free((char *)hdfrag[fd].fi); + hdfrag[fd].fi = NULL; + hdfrag[fd].nfrags = 0; } } +markdirty(hp) /* mark holodeck section directory dirty */ +register HOLO *hp; +{ + static BEAMI smudge = {0, -1}; + + if (hp->dirty) /* already marked? */ + return; + hp->dirty = 1; + if (lseek(hp->fd, biglob(hp)->fo+(nbeams(hp)-1)*sizeof(BEAMI), 0) < 0 + || write(hp->fd, (char *)&smudge, + sizeof(BEAMI)) != sizeof(BEAMI)) + error(SYSTEM, "seek/write error in markdirty"); +} + + HOLO * hdinit(fd, hproto) /* initialize a holodeck section in a file */ int fd; /* corresponding file descriptor */ @@ -97,6 +124,9 @@ HDGRID *hproto; /* holodeck section grid */ n = nbeams(hp)*sizeof(BEAMI); if (read(fd, (char *)(hp->bi+1), n) != n) error(SYSTEM, "failure loading holodeck directory"); + /* check that it's clean */ + if (hp->bi[nbeams(hp)].fo < 0) + error(USER, "dirty holodeck section"); } else { /* assume we're creating it */ if ((hp = hdalloc(hproto)) == NULL) goto memerr; @@ -110,17 +140,23 @@ HDGRID *hproto; /* holodeck section grid */ hp->fd = fd; hp->dirty = 0; biglob(hp)->fo = fpos + sizeof(HDGRID); - biglob(hp)->nrd = 0; /* count rays on disk */ - for (n = nbeams(hp); n > 0; n--) - biglob(hp)->nrd += hp->bi[n].nrd; + /* start tracking fragments */ + hdattach(fd); + /* check rays on disk */ + fpos = hdfilen(fd); + biglob(hp)->nrd = 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 + biglob(hp)->nrd += hp->bi[n].nrd; /* add to holodeck list */ for (n = 0; n < HDMAX; n++) if (hdlist[n] == NULL) { hdlist[n] = hp; break; } - /* start tracking fragments (last) */ - hdattach(fd); /* all done */ return(hp); memerr: @@ -129,19 +165,25 @@ memerr: int -hdsync(hp) /* update directory on disk if necessary */ +hdsync(hp, all) /* update beams and directory on disk */ register HOLO *hp; +int all; { register int j, n; - if (hp == NULL) { /* do all */ + if (hp == NULL) { /* do all holodecks */ n = 0; for (j = 0; hdlist[j] != NULL; j++) - n += hdsync(hdlist[j]); + n += hdsync(hdlist[j], all); return(n); } - if (!hp->dirty) /* check first */ + /* sync the beams */ + for (j = (all ? nbeams(hp) : 0); j > 0; j--) + if (hp->bl[j] != NULL) + hdsyncbeam(hp, j); + if (!hp->dirty) /* directory clean? */ return(0); + errno = 0; if (lseek(hp->fd, biglob(hp)->fo, 0) < 0) error(SYSTEM, "cannot seek on holodeck file"); n = nbeams(hp)*sizeof(BEAMI); @@ -152,7 +194,7 @@ register HOLO *hp; } -long +unsigned hdmemuse(all) /* return memory usage (in bytes) */ int all; /* include overhead (painful) */ { @@ -171,14 +213,36 @@ int all; /* include overhead (painful) */ } } if (all) - for (j = 0; j < FFDMAX; j++) - if (hdfrag[j] != NULL) - total += sizeof(struct fragment); + for (j = 0; j < nhdfrags; j++) { + total += sizeof(struct fragment); + if (hdfrag[j].nfrags) + total += FRAGBLK*sizeof(BEAMI) * + ((hdfrag[j].nfrags-1)/FRAGBLK + 1) ; + } return(total); } long +hdfilen(fd) /* return file length for fd */ +int fd; +{ + long fpos, flen; + + if (fd < 0) + return(-1); + if (fd >= nhdfrags || !hdfrag[fd].nlinks) { + if ((fpos = lseek(fd, 0L, 1)) < 0) + return(-1); + flen = lseek(fd, 0L, 2); + lseek(fd, fpos, 0); + return(flen); + } + return(hdfrag[fd].flen); +} + + +long hdfiluse(fd, all) /* compute file usage (in bytes) */ int fd; /* open file descriptor to check */ int all; /* include overhead and unflushed data */ @@ -221,6 +285,7 @@ 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) @@ -244,8 +309,8 @@ int nr; /* number of new rays desired */ 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; + hp->bl[i]->tick = hdclock; /* update LRU clock */ + blglob(hp)->tick = hdclock++; return(p); /* point to new rays */ memerr: error(SYSTEM, "out of memory in hdnewrays"); @@ -276,80 +341,173 @@ register int i; 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; + hp->bl[i]->tick = hdclock; /* update LRU clock */ + blglob(hp)->tick = hdclock++; return(hp->bl[i]); } int -hdgetbi(hp, i) /* allocate a file fragment */ +hdfilord(hb1, hb2) /* order beams for optimal loading */ +register HDBEAMI *hb1, *hb2; +{ + register int c; + /* sort by file descriptor first */ + if ((c = hb1->h->fd - hb2->h->fd)) + return(c); + /* then by position in file */ + return(hb1->h->bi[hb1->b].fo - hb2->h->bi[hb2->b].fo); +} + + +hdloadbeams(hb, n, bf) /* load a list of beams in optimal order */ +register HDBEAMI *hb; /* list gets sorted by hdfilord() */ +int n; /* list length */ +int (*bf)(); /* callback function (optional) */ +{ + unsigned origcachesize, memuse; + register BEAM *bp; + int bytesloaded, needbytes, bytes2free; + register int i; + /* precheck consistency */ + 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"); + /* sort list for optimal access */ + qsort((char *)hb, n, sizeof(HDBEAMI), hdfilord); + bytesloaded = needbytes = 0; /* figure out memory needs */ + if ((origcachesize = hdcachesize) > 0) { + for (i = n; i--; ) + if ((bp = hb[i].h->bl[hb[i].b]) != NULL) { + bp->tick = hdclock; /* preempt swap */ + bytesloaded += bp->nrm; + } else /* prepare to load */ + needbytes += hb[i].h->bi[hb[i].b].nrd; + bytesloaded *= sizeof(RAYVAL); + needbytes *= sizeof(RAYVAL); + do { /* free enough memory */ + memuse = hdmemuse(0); + bytes2free = needbytes - (signed)(hdcachesize-memuse); + if (bytes2free > (signed)(memuse - bytesloaded)) + bytes2free = memuse - bytesloaded; + } while (bytes2free > 0 && + hdfreecache(100*bytes2free/memuse, NULL) < 0); + } + hdcachesize = 0; /* load the ordered beams w/o swap */ + 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); + hdcachesize = origcachesize; /* resume dynamic swapping */ +} + + +int +hdsyncbeam(hp, i) /* sync beam in memory with beam on disk */ register HOLO *hp; register int i; { - int nrays = hp->bl[i]->nrm; - - if (hp->bi[i].nrd == nrays) /* current one will do? */ + unsigned int nrays; + long nfo; + unsigned int n; + /* check file status */ + if (hdfrag[hp->fd].writerr) + return(-1); +#ifdef DEBUG + if (i < 1 | i > nbeams(hp)) + error(CONSISTENCY, "bad beam index in hdsyncbeam"); +#endif + /* is current fragment OK? */ + if (hp->bl[i] == NULL || (nrays = hp->bl[i]->nrm) == hp->bi[i].nrd) return(0); - - if (hp->fd >= FFDMAX || hdfrag[hp->fd] == NULL) /* untracked */ + /* locate fragment */ + if (hp->fd >= nhdfrags || !hdfrag[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) == - hdfrag[hp->fd]->flen) /* EOF special case */ - hdfrag[hp->fd]->flen = hp->bi[i].fo + nrays*sizeof(RAYVAL); + hdfrag[hp->fd].flen) /* EOF special case */ + hdfrag[hp->fd].flen = (nfo=hp->bi[i].fo) + nrays*sizeof(RAYVAL); else { /* general case */ - register struct fragment *f = hdfrag[hp->fd]; + register struct fragment *f = &hdfrag[hp->fd]; register int j, k; /* relinquish old fragment */ if (hp->bi[i].nrd) { - if ((j = ++f->nfrags) >= MAXFRAG) + j = f->nfrags++; +#if MAXFRAG + if (j >= MAXFRAG-1) f->nfrags--; - for ( ; ; ) { /* stick it in our descending list */ - if (!--j || hp->bi[i].fo < f->fi[j-1].fo) { +#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); + copystruct(f->fi+j, f->fi+(j-1)); } /* coalesce adjacent fragments */ - for (j = k = 0; k < f->nfrags; j++, k++) { - if (j != k) - copystruct(f->fi+j, f->fi+k); - while (k+1 < f->nfrags && - f->fi[k+1].fo + f->fi[k+1].nrd*sizeof(RAYVAL) - == f->fi[j].fo) - f->fi[j].fo -= - f->fi[++k].nrd*sizeof(RAYVAL); + 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; } - f->nfrags = j; + 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; + } } k = -1; /* find closest-sized fragment */ - for (j = f->nfrags; j-- > 0; ) + for (j = (nrays ? f->nfrags : 0); j-- > 0; ) if (f->fi[j].nrd >= nrays && - (k < 0 || f->fi[j].nrd < f->fi[k].nrd) - && f->fi[k=j].nrd == nrays) - break; + (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 */ - hp->bi[i].fo = f->flen; + nfo = f->flen; f->flen += nrays*sizeof(RAYVAL); } else { /* else use fragment */ - hp->bi[i].fo = f->fi[k].fo; - if (f->fi[k].nrd == nrays) { /* delete fragment */ - f->nfrags--; - for (j = k; j < f->nfrags; j++) - copystruct(f->fi+j, f->fi+j+1); - } else { /* else shrink it */ - f->fi[k].fo += nrays*sizeof(RAYVAL); - f->fi[k].nrd -= nrays; - } + nfo = f->fi[k].fo; + f->fi[k].fo += nrays*sizeof(RAYVAL); + f->fi[k].nrd -= nrays; } + /* 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; } + if (nrays) { /* write the new fragment */ + 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) { + hdfrag[hp->fd].writerr++; + hdsync(hp, 0); /* sync directory */ + error(SYSTEM, "write error in hdsyncbeam"); + } + } biglob(hp)->nrd += nrays - hp->bi[i].nrd; hp->bi[i].nrd = nrays; - hp->dirty++; /* section directory now out of date */ + hp->bi[i].fo = nfo; + markdirty(hp); /* section directory now out of date */ return(1); } @@ -359,7 +517,7 @@ hdfreebeam(hp, i) /* free beam, writing if dirty */ register HOLO *hp; register int i; { - int nchanged, n; + int nchanged; if (hp == NULL) { /* clear all holodecks */ nchanged = 0; @@ -367,27 +525,25 @@ register int i; nchanged += hdfreebeam(hdlist[i], 0); return(nchanged); } + if (hdfrag[hp->fd].writerr) /* check for file error */ + return(0); if (i == 0) { /* clear entire holodeck */ nchanged = 0; - for (i = 1; i <= nbeams(hp); i++) - nchanged += hdfreebeam(hp, i); + for (i = nbeams(hp); i > 0; i--) + if (hp->bl[i] != NULL) + nchanged += hdfreebeam(hp, i); return(nchanged); } +#ifdef DEBUG if (i < 1 | i > nbeams(hp)) error(CONSISTENCY, "bad beam index to hdfreebeam"); +#endif if (hp->bl[i] == NULL) return(0); /* check for additions */ nchanged = hp->bl[i]->nrm - hp->bi[i].nrd; - if (nchanged) { - hdgetbi(hp, i); /* allocate a file position */ - errno = 0; - if (lseek(hp->fd, hp->bi[i].fo, 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) - error(SYSTEM, "write error in hdfreebeam"); - } + if (nchanged) + hdsyncbeam(hp, i); /* write new fragment */ blglob(hp)->nrm -= hp->bl[i]->nrm; free((char *)hp->bl[i]); /* free memory */ hp->bl[i] = NULL; @@ -395,67 +551,111 @@ register int i; } -hdlrulist(ha, ba, n, hp) /* add beams from holodeck to LRU list */ -register HOLO *ha[]; /* section list (NULL terminated) */ -register int ba[]; /* beam index to go with section */ -int n; /* length of arrays minus 1 */ +int +hdkillbeam(hp, i) /* delete beam from holodeck */ +register HOLO *hp; +register int i; +{ + static BEAM emptybeam; + int nchanged; + + if (hp == NULL) { /* clobber all holodecks */ + nchanged = 0; + for (i = 0; hdlist[i] != NULL; i++) + nchanged += hdkillbeam(hdlist[i], 0); + return(nchanged); + } + if (i == 0) { /* clobber entire holodeck */ + nchanged = 0; + for (i = nbeams(hp); i > 0; i--) + if (hp->bi[i].nrd > 0 || hp->bl[i] != NULL) + nchanged += hdkillbeam(hp, i); +#ifdef DEBUG + if (biglob(hp)->nrd != 0 | blglob(hp)->nrm != 0) + error(CONSISTENCY, "bad beam count in hdkillbeam"); +#endif + return(nchanged); + } +#ifdef DEBUG + if (i < 1 | i > nbeams(hp)) + error(CONSISTENCY, "bad beam index to hdkillbeam"); +#endif + if (hp->bl[i] != NULL) { /* free memory */ + blglob(hp)->nrm -= nchanged = hp->bl[i]->nrm; + free((char *)hp->bl[i]); + } else + nchanged = hp->bi[i].nrd; + if (hp->bi[i].nrd) { /* free file fragment */ + hp->bl[i] = &emptybeam; + hdsyncbeam(hp, i); + } + hp->bl[i] = NULL; + return(nchanged); +} + + +int +hdlrulist(hb, nents, n, hp) /* add beams from holodeck to LRU list */ +register HDBEAMI *hb; /* beam list */ +int nents; /* current list length */ +int n; /* maximum list length */ register HOLO *hp; /* section we're adding from */ { register int i, j; - int nents; - /* find last entry in LRU list */ - for (j = 0; ha[j] != NULL; j++) - ; - nents = j; /* insert each beam from hp */ - for (i = nbeams(hp); i > 0; i-- ) { + for (i = nbeams(hp); i > 0; i--) { if (hp->bl[i] == NULL) /* check if loaded */ continue; - if ((j = ++nents) > n) /* grow list if we can */ +#if 0 + if (hp->bl[i]->tick == hdclock) /* preempt swap? */ + continue; +#endif + if ((j = ++nents) >= n) /* grow list if we can */ nents--; for ( ; ; ) { /* bubble into place */ if (!--j || hp->bl[i]->tick >= - ha[j-1]->bl[ba[j-1]]->tick) { - ha[j] = hp; - ba[j] = i; + hb[j-1].h->bl[hb[j-1].b]->tick) { + hb[j].h = hp; + hb[j].b = i; break; } - ha[j] = ha[j-1]; - ba[j] = ba[j-1]; + copystruct(hb+j, hb+(j-1)); } } - ha[nents] = NULL; /* all done */ - ba[nents] = 0; + return(nents); /* return new list length */ } +int hdfreecache(pct, honly) /* free up cache space, writing changes */ int pct; /* maximum percentage to free */ register HOLO *honly; /* NULL means check all */ { - HOLO *hp[FREEBEAMS+1]; - int bn[FREEBEAMS+1]; + HDBEAMI hb[FREEBEAMS]; int freetarget; + int n; register int i; /* compute free target */ freetarget = (honly != NULL) ? blglob(honly)->nrm : hdmemuse(0)/sizeof(RAYVAL) ; freetarget = freetarget*pct/100; + if (freetarget <= 0) + return(0); /* find least recently used */ - hp[0] = NULL; - bn[0] = 0; + n = 0; if (honly != NULL) - hdlrulist(hp, bn, FREEBEAMS, honly); + n = hdlrulist(hb, n, FREEBEAMS, honly); else for (i = 0; hdlist[i] != NULL; i++) - hdlrulist(hp, bn, FREEBEAMS, hdlist[i]); + n = hdlrulist(hb, n, FREEBEAMS, hdlist[i]); /* free LRU beams */ - for (i = 0; hp[i] != NULL; i++) { - hdfreebeam(hp[i], bn[i]); - if ((freetarget -= hp[i]->bi[bn[i]].nrd) <= 0) + for (i = 0; i < n; i++) { + hdfreebeam(hb[i].h, hb[i].b); + if ((freetarget -= hb[i].h->bi[hb[i].b].nrd) <= 0) break; } - hdsync(honly); /* synchronize directories as necessary */ + hdsync(honly, 0); /* synchronize directories as necessary */ + return(-freetarget); /* return how far past goal we went */ }