--- ray/src/hd/holofile.c 1997/11/03 13:40:22 3.3 +++ ray/src/hd/holofile.c 1997/12/11 11:29:35 3.10 @@ -16,18 +16,21 @@ 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 */ + +/* define MAXFRAG if you want to limit fragment tracking memory */ + +#ifndef BSD +#define write writebuf /* safe i/o routines */ +#define read readbuf #endif -#ifndef MAXFRAG -#define MAXFRAG 2048 /* maximum fragments tracked in each file */ -#endif +#define FRAGBLK 64 /* number of fragments to allocate at a time */ + int hdcachesize = CACHESIZE*1024*1024; /* target cache size (bytes) */ unsigned long hdclock; /* clock value */ @@ -36,25 +39,31 @@ 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 */ + 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 +73,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 +122,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; @@ -172,14 +200,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 */ @@ -294,22 +344,36 @@ register int i; if (hp->bi[i].nrd == nrays) /* current one will do? */ return(0); - if (hp->fd >= FFDMAX || hdfrag[hp->fd] == NULL) /* untracked */ + 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 = 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++; +#ifdef 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 hdgetbi"); + } + 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; @@ -353,7 +417,7 @@ register int i; } biglob(hp)->nrd += nrays - hp->bi[i].nrd; hp->bi[i].nrd = nrays; - hp->dirty++; /* section directory now out of date */ + markdirty(hp); /* section directory now out of date */ return(1); } @@ -371,10 +435,13 @@ register int i; nchanged += hdfreebeam(hdlist[i], 0); return(nchanged); } + if (hp->fd < 0) /* check for recursive error */ + return(-1); 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); } if (i < 1 | i > nbeams(hp)) @@ -389,8 +456,10 @@ register int i; 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) + if (write(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) { + hp->fd = -1; /* avoid recursive error */ error(SYSTEM, "write error in hdfreebeam"); + } } blglob(hp)->nrm -= hp->bl[i]->nrm; free((char *)hp->bl[i]); /* free memory */ @@ -399,6 +468,47 @@ register int i; } +int +hdkillbeam(hp, i) /* delete beam from holodeck */ +register HOLO *hp; +register int i; +{ + static BEAM emptybeam; + int nchanged, n; + + 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); + } + if (i < 1 | i > nbeams(hp)) + error(CONSISTENCY, "bad beam index to hdkillbeam"); + 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; + hdgetbi(hp, i); + } + hp->bl[i] = NULL; + return(nchanged); +} + + 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 */ @@ -412,7 +522,7 @@ register HOLO *hp; /* section we're adding from */ ; 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 */