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.2 by gregl, Fri Oct 31 15:48:32 1997 UTC vs.
Revision 3.29 by gwlarson, Wed Nov 11 10:56:07 1998 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1997 Silicon Graphics, Inc. */
1 > /* Copyright (c) 1998 Silicon Graphics, Inc. */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ SGI";
# Line 16 | Line 16 | static char SCCSid[] = "$SunId$ SGI";
16   #define CACHESIZE       16      /* default cache size (Mbytes, 0==inf) */
17   #endif
18   #ifndef FREEBEAMS
19 < #define FREEBEAMS       1024    /* maximum beams to free at a time */
19 > #define FREEBEAMS       1500    /* maximum beams to free at a time */
20   #endif
21   #ifndef PCTFREE
22 < #define PCTFREE         12      /* maximum fraction to free (%) */
22 > #define PCTFREE         15      /* maximum fraction to free (%) */
23   #endif
24 #ifndef FFDMAX
25 #define FFDMAX          64      /* max. file descriptor for tracking */
26 #endif
24   #ifndef MAXFRAG
25 < #define MAXFRAG         2048    /* maximum fragments tracked in each file */
25 > #define MAXFRAG         32767   /* maximum fragments/file to track (0==inf) */
26   #endif
27 + #ifndef MINDIRSEL
28 +                                /* minimum directory seek length */
29 + #define MINDIRSEL       (4*BUFSIZ/sizeof(BEAMI))
30 + #endif
31  
32 < int     hdcachesize = CACHESIZE*1024*1024;      /* target cache size (bytes) */
32 > #ifndef BSD
33 > #define write   writebuf        /* safe i/o routines */
34 > #define read    readbuf
35 > #endif
36 >
37 > #define FRAGBLK         256     /* number of fragments to allocate at a time */
38 >
39 > unsigned        hdcachesize = CACHESIZE*1024*1024;      /* target cache size */
40   unsigned long   hdclock;        /* clock value */
41  
42   HOLO    *hdlist[HDMAX+1];       /* holodeck pointers (NULL term.) */
43  
44 < static struct fragment {
44 > static struct fraglist {
45          short   nlinks;         /* number of holodeck sections using us */
46 <        short   nfrags;         /* number of known fragments */
47 <        BEAMI   fi[MAXFRAG];    /* fragments, descending file position */
46 >        short   writerr;        /* write error encountered */
47 >        int     nfrags;         /* number of known fragments */
48 >        BEAMI   *fi;            /* fragments, descending file position */
49          long    flen;           /* last known file length */
50 < } *hdfrag[FFDMAX];      /* fragment lists, indexed by file descriptor */
50 > } *hdfragl;             /* fragment lists, indexed by file descriptor */
51  
52 + static int      nhdfragls;      /* size of hdfragl array */
53  
54 +
55 + char *
56 + hdrealloc(ptr, siz, rout)       /* (re)allocate memory, retry then error */
57 + char    *ptr;
58 + unsigned        siz;
59 + char    *rout;
60 + {
61 +        register char   *newp;
62 +                                        /* call malloc/realloc */
63 +        if (ptr == NULL) newp = (char *)malloc(siz);
64 +        else newp = (char *)realloc(ptr, siz);
65 +                                        /* check success */
66 +        if (newp == NULL && rout != NULL) {
67 +                hdfreecache(25, NULL);  /* free some memory */
68 +                errno = 0;              /* retry */
69 +                newp = hdrealloc(ptr, siz, NULL);
70 +                if (newp == NULL) {     /* give up and report error */
71 +                        sprintf(errmsg, "out of memory in %s", rout);
72 +                        error(SYSTEM, errmsg);
73 +                }
74 +        }
75 +        return(newp);
76 + }
77 +
78 +
79   hdattach(fd)            /* start tracking file fragments for some section */
80   register int    fd;
81   {
82 <        if (fd >= FFDMAX)
83 <                return;                         /* descriptor out of range */
84 <        if (hdfrag[fd] == NULL) {
85 <                if ((hdfrag[fd] = (struct fragment *)
86 <                                malloc(sizeof(struct fragment))) == NULL)
87 <                        return;                 /* should flag error? */
53 <                hdfrag[fd]->nlinks = 0;
54 <                hdfrag[fd]->nfrags = 0;
82 >        if (fd >= nhdfragls) {
83 >                hdfragl = (struct fraglist *)hdrealloc((char *)hdfragl,
84 >                                (fd+1)*sizeof(struct fraglist), "hdattach");
85 >                bzero((char *)(hdfragl+nhdfragls),
86 >                                (fd+1-nhdfragls)*sizeof(struct fraglist));
87 >                nhdfragls = fd+1;
88          }
89 <        hdfrag[fd]->nlinks++;
90 <        hdfrag[fd]->flen = lseek(fd, 0L, 2);    /* get file length */
89 >        hdfragl[fd].nlinks++;
90 >        hdfragl[fd].flen = lseek(fd, 0L, 2);    /* get file length */
91   }
92  
93  
# Line 64 | Line 97 | register int   fd;
97   hdrelease(fd)           /* stop tracking file fragments for some section */
98   register int    fd;
99   {
100 <        if (fd >= FFDMAX || hdfrag[fd] == NULL)
100 >        if (fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks)
101                  return;
102 <        if (!--hdfrag[fd]->nlinks) {
103 <                free((char *)hdfrag[fd]);
104 <                hdfrag[fd] = NULL;
102 >        if (!--hdfragl[fd].nlinks && hdfragl[fd].nfrags) {
103 >                free((char *)hdfragl[fd].fi);
104 >                hdfragl[fd].fi = NULL;
105 >                hdfragl[fd].nfrags = 0;
106          }
107   }
108  
# Line 78 | Line 112 | hdinit(fd, hproto)     /* initialize a holodeck section in
112   int     fd;                     /* corresponding file descriptor */
113   HDGRID  *hproto;                /* holodeck section grid */
114   {
115 +        long    rtrunc;
116          long    fpos;
117          register HOLO   *hp;
118          register int    n;
# Line 97 | Line 132 | HDGRID *hproto;                /* holodeck section grid */
132                  n = nbeams(hp)*sizeof(BEAMI);
133                  if (read(fd, (char *)(hp->bi+1), n) != n)
134                          error(SYSTEM, "failure loading holodeck directory");
135 +                                                /* check that it's clean */
136 +                for (n = nbeams(hp); n > 0; n--)
137 +                        if (hp->bi[n].fo < 0) {
138 +                                hp->bi[n].fo = 0;
139 +                                error(WARNING, "dirty holodeck section");
140 +                                break;
141 +                        }
142          } else {                        /* assume we're creating it */
143                  if ((hp = hdalloc(hproto)) == NULL)
144                          goto memerr;
# Line 110 | Line 152 | HDGRID *hproto;                /* holodeck section grid */
152          hp->fd = fd;    
153          hp->dirty = 0;
154          biglob(hp)->fo = fpos + sizeof(HDGRID);
155 <        biglob(hp)->nrd = 0;            /* count rays on disk */
156 <        for (n = nbeams(hp); n > 0; n--)
157 <                biglob(hp)->nrd += hp->bi[n].nrd;
155 >                                        /* start tracking fragments */
156 >        hdattach(fd);
157 >                                        /* check rays on disk */
158 >        fpos = hdfilen(fd);
159 >        biglob(hp)->nrd = rtrunc = 0;
160 >        for (n = hproto == NULL ? nbeams(hp) : 0; n > 0; n--)
161 >                if (hp->bi[n].nrd)
162 >                        if (hp->bi[n].fo+hp->bi[n].nrd*sizeof(RAYVAL) > fpos) {
163 >                                rtrunc += hp->bi[n].nrd;
164 >                                hp->bi[n].nrd = 0;
165 >                        } else
166 >                                biglob(hp)->nrd += hp->bi[n].nrd;
167 >        if (rtrunc) {
168 >                sprintf(errmsg, "truncated section, %ld rays lost (%.1f%%)",
169 >                                rtrunc, 100.*rtrunc/(rtrunc+biglob(hp)->nrd));
170 >                error(WARNING, errmsg);
171 >        }
172                                          /* add to holodeck list */
173          for (n = 0; n < HDMAX; n++)
174                  if (hdlist[n] == NULL) {
175                          hdlist[n] = hp;
176                          break;
177                  }
122                                        /* start tracking fragments (last) */
123        hdattach(fd);
178                                          /* all done */
179          return(hp);
180   memerr:
# Line 128 | Line 182 | memerr:
182   }
183  
184  
185 + markdirty(hp, i)                /* mark holodeck directory position dirty */
186 + register HOLO   *hp;
187 + int     i;
188 + {
189 +        static BEAMI    smudge = {0, -1};
190 +        int     mindist, minpos;
191 +        register int    j;
192 +
193 +        if (!hp->dirty++) {                     /* write smudge first time */
194 +                if (lseek(hp->fd, biglob(hp)->fo+(i-1)*sizeof(BEAMI), 0) < 0
195 +                                || write(hp->fd, (char *)&smudge,
196 +                                        sizeof(BEAMI)) != sizeof(BEAMI))
197 +                        error(SYSTEM, "seek/write error in markdirty");
198 +                hp->dirseg[0].s = i;
199 +                hp->dirseg[0].n = 1;
200 +                return;
201 +        }
202 +                                                /* insert into segment list */
203 +        for (j = hp->dirty; j--; ) {
204 +                if (!j || hp->dirseg[j-1].s < i) {
205 +                        hp->dirseg[j].s = i;
206 +                        hp->dirseg[j].n = 1;
207 +                        break;
208 +                }
209 +                copystruct(hp->dirseg+j, hp->dirseg+(j-1));
210 +        }
211 +        do {                            /* check neighbors */
212 +                mindist = nbeams(hp);           /* find closest */
213 +                for (j = hp->dirty; --j; )
214 +                        if (hp->dirseg[j].s -
215 +                                        (hp->dirseg[j-1].s + hp->dirseg[j-1].n)
216 +                                        < mindist) {
217 +                                minpos = j;
218 +                                mindist = hp->dirseg[j].s -
219 +                                        (hp->dirseg[j-1].s + hp->dirseg[j-1].n);
220 +                        }
221 +                                                /* good enough? */
222 +                if (hp->dirty <= MAXDIRSE && mindist > MINDIRSEL)
223 +                        break;
224 +                j = minpos - 1;                 /* coalesce neighbors */
225 +                if (hp->dirseg[j].s + hp->dirseg[j].n <
226 +                                hp->dirseg[minpos].s + hp->dirseg[minpos].n)
227 +                        hp->dirseg[j].n = hp->dirseg[minpos].s +
228 +                                        hp->dirseg[minpos].n - hp->dirseg[j].s;
229 +                hp->dirty--;
230 +                while (++j < hp->dirty)         /* close the gap */
231 +                        copystruct(hp->dirseg+j, hp->dirseg+(j+1));
232 +        } while (mindist <= MINDIRSEL);
233 + }
234 +
235 +
236   int
237 < hdsync(hp)                      /* update directory on disk if necessary */
237 > hdsync(hp, all)                 /* update beams and directory on disk */
238   register HOLO   *hp;
239 + int     all;
240   {
241          register int    j, n;
242  
243 <        if (hp == NULL) {               /* do all */
243 >        if (hp == NULL) {               /* do all holodecks */
244                  n = 0;
245                  for (j = 0; hdlist[j] != NULL; j++)
246 <                        n += hdsync(hdlist[j]);
246 >                        n += hdsync(hdlist[j], all);
247                  return(n);
248          }
249 <        if (!hp->dirty)                 /* check first */
249 >                                        /* sync the beams */
250 >        for (j = (all ? nbeams(hp) : 0); j > 0; j--)
251 >                if (hp->bl[j] != NULL)
252 >                        hdsyncbeam(hp, j);
253 >        if (!hp->dirty)                 /* directory clean? */
254                  return(0);
255 <        if (lseek(hp->fd, biglob(hp)->fo, 0) < 0)
256 <                error(SYSTEM, "cannot seek on holodeck file");
257 <        n = nbeams(hp)*sizeof(BEAMI);
258 <        if (write(hp->fd, (char *)(hp->bi+1), n) != n)
259 <                error(SYSTEM, "cannot update holodeck section directory");
260 <        hp->dirty = 0;
255 >        errno = 0;                      /* write dirty segments */
256 >        for (j = 0; j < hp->dirty; j++) {
257 >                if (lseek(hp->fd, biglob(hp)->fo +
258 >                                (hp->dirseg[j].s-1)*sizeof(BEAMI), 0) < 0)
259 >                        error(SYSTEM, "cannot seek on holodeck file");
260 >                n = hp->dirseg[j].n * sizeof(BEAMI);
261 >                if (write(hp->fd, (char *)(hp->bi+hp->dirseg[j].s), n) != n)
262 >                        error(SYSTEM, "cannot update section directory");
263 >        }
264 >        hp->dirty = 0;                  /* all clean */
265          return(1);
266   }
267  
268  
269 < long
269 > unsigned
270   hdmemuse(all)           /* return memory usage (in bytes) */
271   int     all;                    /* include overhead (painful) */
272   {
# Line 171 | Line 285 | int    all;                    /* include overhead (painful) */
285                  }
286          }
287          if (all)
288 <                for (j = 0; j < FFDMAX; j++)
289 <                        if (hdfrag[j] != NULL)
290 <                                total += sizeof(struct fragment);
288 >                for (j = 0; j < nhdfragls; j++) {
289 >                        total += sizeof(struct fraglist);
290 >                        if (hdfragl[j].nfrags)
291 >                                total += FRAGBLK*sizeof(BEAMI) *
292 >                                        ((hdfragl[j].nfrags-1)/FRAGBLK + 1) ;
293 >                }
294          return(total);
295   }
296  
297  
298   long
299 + hdfilen(fd)             /* return file length for fd */
300 + int     fd;
301 + {
302 +        long    fpos, flen;
303 +
304 +        if (fd < 0)
305 +                return(-1);
306 +        if (fd >= nhdfragls || !hdfragl[fd].nlinks) {
307 +                if ((fpos = lseek(fd, 0L, 1)) < 0)
308 +                        return(-1);
309 +                flen = lseek(fd, 0L, 2);
310 +                lseek(fd, fpos, 0);
311 +                return(flen);
312 +        }
313 +        return(hdfragl[fd].flen);
314 + }
315 +
316 +
317 + long
318   hdfiluse(fd, all)       /* compute file usage (in bytes) */
319   int     fd;                     /* open file descriptor to check */
320   int     all;                    /* include overhead and unflushed data */
# Line 223 | Line 359 | int    nr;                     /* number of new rays desired */
359                  hdfreecache(PCTFREE, NULL);     /* free some space */
360          if (hp->bl[i] == NULL) {                /* allocate (and load) */
361                  n = hp->bi[i].nrd + nr;
362 <                if ((hp->bl[i] = (BEAM *)malloc(hdbsiz(n))) == NULL)
227 <                        goto memerr;
362 >                hp->bl[i] = (BEAM *)hdrealloc(NULL, hdbsiz(n), "hdnewrays");
363                  blglob(hp)->nrm += n;
364                  if (n = hp->bl[i]->nrm = hp->bi[i].nrd) {
365 +                        errno = 0;
366                          if (lseek(hp->fd, hp->bi[i].fo, 0) < 0)
367                                  error(SYSTEM, "seek error on holodeck file");
368                          n *= sizeof(RAYVAL);
# Line 235 | Line 371 | int    nr;                     /* number of new rays desired */
371                                  "error reading beam from holodeck file");
372                  }
373          } else {                                /* just grow in memory */
374 <                hp->bl[i] = (BEAM *)realloc( (char *)hp->bl[i],
375 <                                hdbsiz(hp->bl[i]->nrm + nr) );
240 <                if (hp->bl[i] == NULL)
241 <                        goto memerr;
374 >                hp->bl[i] = (BEAM *)hdrealloc((char *)hp->bl[i],
375 >                                hdbsiz(hp->bl[i]->nrm + nr), "hdnewrays");
376                  blglob(hp)->nrm += nr;
377          }
378          p = hdbray(hp->bl[i]) + hp->bl[i]->nrm;
379          hp->bl[i]->nrm += nr;                   /* update in-core structure */
380          bzero((char *)p, nr*sizeof(RAYVAL));
381 <        hp->bl[i]->tick = ++hdclock;            /* update LRU clock */
248 <        blglob(hp)->tick = hdclock;
381 >        blglob(hp)->tick = hp->bl[i]->tick = hdclock++; /* update LRU clock */
382          return(p);                              /* point to new rays */
250 memerr:
251        error(SYSTEM, "out of memory in hdnewrays");
383   }
384  
385  
# Line 266 | Line 397 | register int   i;
397                          return(NULL);
398                  if (hdcachesize > 0 && hdmemuse(0) >= hdcachesize)
399                          hdfreecache(PCTFREE, NULL);     /* get free space */
400 <                errno = 0;
270 <                if ((hp->bl[i] = (BEAM *)malloc(hdbsiz(n))) == NULL)
271 <                        error(SYSTEM, "cannot allocate memory for beam");
400 >                hp->bl[i] = (BEAM *)hdrealloc(NULL, hdbsiz(n), "hdgetbeam");
401                  blglob(hp)->nrm += hp->bl[i]->nrm = n;
402 +                errno = 0;
403                  if (lseek(hp->fd, hp->bi[i].fo, 0) < 0)
404                          error(SYSTEM, "seek error on holodeck file");
405                  n *= sizeof(RAYVAL);
406                  if (read(hp->fd, (char *)hdbray(hp->bl[i]), n) != n)
407                          error(SYSTEM, "error reading beam from holodeck file");
408          }
409 <        hp->bl[i]->tick = ++hdclock;    /* update LRU clock */
280 <        blglob(hp)->tick = hdclock;
409 >        blglob(hp)->tick = hp->bl[i]->tick = hdclock++; /* update LRU clock */
410          return(hp->bl[i]);
411   }
412  
413  
414   int
415 < hdgetbi(hp, i)                  /* allocate a file fragment */
416 < register HOLO   *hp;
288 < register int    i;
415 > hdfilord(hb1, hb2)      /* order beams for quick loading */
416 > register HDBEAMI        *hb1, *hb2;
417   {
418 <        int     nrays = hp->bl[i]->nrm;
418 >        register long   c;
419 >                                /* residents go first */
420 >        if (hb2->h->bl[hb2->b] != NULL)
421 >                return(hb1->h->bl[hb1->b] == NULL);
422 >        if (hb1->h->bl[hb1->b] != NULL)
423 >                return(-1);
424 >                                /* otherwise sort by file descriptor */
425 >        if ((c = hb1->h->fd - hb2->h->fd))
426 >                return(c);
427 >                                /* then by position in file */
428 >        c = hb1->h->bi[hb1->b].fo - hb2->h->bi[hb2->b].fo;
429 >        return(c > 0 ? 1 : c < 0 ? -1 : 0);
430 > }
431  
292        if (hp->bi[i].nrd == nrays)     /* current one will do? */
293                return(0);
432  
433 <        if (hp->fd >= FFDMAX || hdfrag[hp->fd] == NULL) /* untracked */
434 <                hp->bi[i].fo = lseek(hp->fd, 0L, 2);
433 > hdloadbeams(hb, n, bf)  /* load a list of beams in optimal order */
434 > register HDBEAMI        *hb;    /* list gets sorted by hdfilord() */
435 > int     n;                      /* list length */
436 > int     (*bf)();                /* callback function (optional) */
437 > {
438 >        unsigned        origcachesize, memuse;
439 >        int     bytesloaded, needbytes, bytes2free;
440 >        register BEAM   *bp;
441 >        register int    i;
442 >                                        /* precheck consistency */
443 >        if (n <= 0) return;
444 >        for (i = n; i--; )
445 >                if (hb[i].h == NULL || hb[i].b < 1 | hb[i].b > nbeams(hb[i].h))
446 >                        error(CONSISTENCY, "bad beam in hdloadbeams");
447 >                                        /* sort list for optimal access */
448 >        qsort((char *)hb, n, sizeof(HDBEAMI), hdfilord);
449 >        bytesloaded = 0;                /* run through loaded beams */
450 >        for ( ; n && (bp = hb->h->bl[hb->b]) != NULL; n--, hb++) {
451 >                bp->tick = hdclock;     /* preempt swap */
452 >                bytesloaded += bp->nrm;
453 >                if (bf != NULL)
454 >                        (*bf)(bp, hb);
455 >        }
456 >        bytesloaded *= sizeof(RAYVAL);
457 >        if ((origcachesize = hdcachesize) > 0) {
458 >                needbytes = 0;          /* figure out memory needs */
459 >                for (i = n; i--; )
460 >                        needbytes += hb[i].h->bi[hb[i].b].nrd;
461 >                needbytes *= sizeof(RAYVAL);
462 >                do {                            /* free enough memory */
463 >                        memuse = hdmemuse(0);
464 >                        bytes2free = needbytes - (int)(hdcachesize-memuse);
465 >                        if (bytes2free > (int)(memuse - bytesloaded))
466 >                                bytes2free = memuse - bytesloaded;
467 >                } while (bytes2free > 0 &&
468 >                                hdfreecache(100*bytes2free/memuse, NULL) < 0);
469 >                hdcachesize = 0;                /* load beams w/o swap */
470 >        }
471 >        for (i = 0; i < n; i++)
472 >                if ((bp = hdgetbeam(hb[i].h, hb[i].b)) != NULL && bf != NULL)
473 >                        (*bf)(bp, hb+i);
474 >        hdcachesize = origcachesize;    /* resume dynamic swapping */
475 > }
476  
298        else if (hp->bi[i].fo + hp->bi[i].nrd*sizeof(RAYVAL) ==
299                        hdfrag[hp->fd]->flen)           /* EOF special case */
300                hdfrag[hp->fd]->flen = hp->bi[i].fo + nrays*sizeof(RAYVAL);
477  
478 <        else {                                          /* general case */
479 <                register struct fragment        *f = hdfrag[hp->fd];
480 <                register int    j, k;
481 <                                        /* relinquish old fragment */
482 <                if (hp->bi[i].nrd) {
483 <                        if ((j = ++f->nfrags) >= MAXFRAG)
484 <                                f->nfrags--;
485 <                        for ( ; ; ) {   /* stick it in our descending list */
486 <                                if (!--j || hp->bi[i].fo < f->fi[j-1].fo) {
487 <                                        f->fi[j].fo = hp->bi[i].fo;
488 <                                        f->fi[j].nrd = hp->bi[i].nrd;
489 <                                        break;
490 <                                }
491 <                                copystruct(f->fi+j, f->fi+j-1);
492 <                        }
478 > hdfreefrag(fd, bi)                      /* free a file fragment */
479 > int     fd;
480 > register BEAMI  *bi;
481 > {
482 >        register struct fraglist        *f;
483 >        register int    j, k;
484 >
485 >        if (bi->nrd == 0)
486 >                return;
487 > #ifdef DEBUG
488 >        if (fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks)
489 >                error(CONSISTENCY, "bad file descriptor in hdfreefrag");
490 > #endif
491 >        f = &hdfragl[fd];
492 >        if (f->nfrags % FRAGBLK == 0) { /* delete empty remnants */
493 >                for (j = k = 0; k < f->nfrags; j++, k++) {
494 >                        while (f->fi[k].nrd == 0)
495 >                                if (++k >= f->nfrags)
496 >                                        goto endloop;
497 >                        if (k > j)
498 >                                copystruct(f->fi+j, f->fi+k);
499 >                }
500 >        endloop:
501 >                f->nfrags = j;
502 >        }
503 >        j = f->nfrags++;                /* allocate a slot in free list */
504 > #if MAXFRAG
505 >        if (j >= MAXFRAG-1)
506 >                f->nfrags--;
507 > #endif
508 >        if (j % FRAGBLK == 0) {         /* more free list space */
509 >                register BEAMI  *newp;
510 >                if (f->fi == NULL)
511 >                        newp = (BEAMI *)malloc((j+FRAGBLK)*sizeof(BEAMI));
512 >                else
513 >                        newp = (BEAMI *)realloc((char *)f->fi,
514 >                                        (j+FRAGBLK)*sizeof(BEAMI));
515 >                if (newp == NULL) {
516 >                        f->nfrags--;    /* graceful failure */
517 >                        return;
518 >                }
519 >                f->fi = newp;
520 >        }
521 >        for ( ; ; j--) {                /* insert in descending list */
522 >                if (!j || bi->fo < f->fi[j-1].fo) {
523 >                        f->fi[j].fo = bi->fo;
524 >                        f->fi[j].nrd = bi->nrd;
525 >                        break;
526 >                }
527 >                copystruct(f->fi+j, f->fi+(j-1));
528 >        }
529                                          /* coalesce adjacent fragments */
530 <                        for (j = k = 0; k < f->nfrags; j++, k++) {
531 <                                if (j != k)
532 <                                        copystruct(f->fi+j, f->fi+k);
533 <                                while (k+1 < f->nfrags &&
534 <                                f->fi[k+1].fo + f->fi[k+1].nrd*sizeof(RAYVAL)
535 <                                                == f->fi[j].fo)
536 <                                        f->fi[j].fo -=
537 <                                                f->fi[++k].nrd*sizeof(RAYVAL);
530 >                                                /* successors never empty */
531 >        if (j && f->fi[j-1].fo == f->fi[j].fo + f->fi[j].nrd*sizeof(RAYVAL)) {
532 >                f->fi[j].nrd += f->fi[j-1].nrd;
533 >                f->fi[j-1].nrd = 0;
534 >        }
535 >        for (k = j+1; k < f->nfrags; k++)       /* get non-empty predecessor */
536 >                if (f->fi[k].nrd) {
537 >                        if (f->fi[j].fo == f->fi[k].fo +
538 >                                        f->fi[k].nrd*sizeof(RAYVAL)) {
539 >                                f->fi[k].nrd += f->fi[j].nrd;
540 >                                f->fi[j].nrd = 0;
541                          }
542 <                        f->nfrags = j;
542 >                        break;
543                  }
544 <                k = -1;                 /* find closest-sized fragment */
545 <                for (j = f->nfrags; j-- > 0; )
546 <                        if (f->fi[j].nrd >= nrays &&
547 <                                        (k < 0 || f->fi[j].nrd < f->fi[k].nrd)
548 <                                        && f->fi[k=j].nrd == nrays)
544 > }
545 >
546 >
547 > long
548 > hdallocfrag(fd, nrays)          /* allocate a file fragment */
549 > int     fd;
550 > unsigned int4   nrays;
551 > {
552 >        register struct fraglist        *f;
553 >        register int    j, k;
554 >        long    nfo;
555 >
556 >        if (nrays == 0)
557 >                return(-1L);
558 > #ifdef DEBUG
559 >        if (fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks)
560 >                error(CONSISTENCY, "bad file descriptor in hdallocfrag");
561 > #endif
562 >        f = &hdfragl[fd];
563 >        k = -1;                         /* find closest-sized fragment */
564 >        for (j = f->nfrags; j-- > 0; )
565 >                if (f->fi[j].nrd >= nrays &&
566 >                                (k < 0 || f->fi[j].nrd < f->fi[k].nrd))
567 >                        if (f->fi[k=j].nrd == nrays)
568                                  break;
569 <                if (k < 0) {            /* no fragment -- extend file */
570 <                        hp->bi[i].fo = f->flen;
571 <                        f->flen += nrays*sizeof(RAYVAL);
572 <                } else {                /* else use fragment */
573 <                        hp->bi[i].fo = f->fi[k].fo;
574 <                        if (f->fi[k].nrd == nrays) {    /* delete fragment */
575 <                                f->nfrags--;
342 <                                for (j = k; j < f->nfrags; j++)
343 <                                        copystruct(f->fi+j, f->fi+j+1);
344 <                        } else {                        /* else shrink it */
345 <                                f->fi[k].fo += nrays*sizeof(RAYVAL);
346 <                                f->fi[k].nrd -= nrays;
347 <                        }
348 <                }
569 >        if (k < 0) {                    /* no fragment -- extend file */
570 >                nfo = f->flen;
571 >                f->flen += nrays*sizeof(RAYVAL);
572 >        } else {                        /* else use fragment */
573 >                nfo = f->fi[k].fo;
574 >                f->fi[k].fo += nrays*sizeof(RAYVAL);
575 >                f->fi[k].nrd -= nrays;
576          }
577 +        return(nfo);
578 + }
579 +
580 +
581 + int
582 + hdsyncbeam(hp, i)               /* sync beam in memory with beam on disk */
583 + register HOLO   *hp;
584 + register int    i;
585 + {
586 +        unsigned int4   nrays;
587 +        unsigned int    n;
588 +        long    nfo;
589 +                                        /* check file status */
590 +        if (hdfragl[hp->fd].writerr)
591 +                return(-1);
592 + #ifdef DEBUG
593 +        if (i < 1 | i > nbeams(hp))
594 +                error(CONSISTENCY, "bad beam index in hdsyncbeam");
595 + #endif
596 +                                        /* is current fragment OK? */
597 +        if (hp->bl[i] == NULL || (nrays = hp->bl[i]->nrm) == hp->bi[i].nrd)
598 +                return(0);
599 +        if (hp->bi[i].nrd)              /* relinquish old fragment */
600 +                hdfreefrag(hp->fd, &hp->bi[i]);
601 +        if (nrays) {                    /* get and write new fragment */
602 +                nfo = hdallocfrag(hp->fd, nrays);
603 +                errno = 0;
604 +                if (lseek(hp->fd, nfo, 0) < 0)
605 +                        error(SYSTEM, "cannot seek on holodeck file");
606 +                n = hp->bl[i]->nrm * sizeof(RAYVAL);
607 +                if (write(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) {
608 +                        hdfragl[hp->fd].writerr++;
609 +                        hdsync(NULL, 0);        /* sync directories */
610 +                        error(SYSTEM, "write error in hdsyncbeam");
611 +                }
612 +                hp->bi[i].fo = nfo;
613 +        } else
614 +                hp->bi[i].fo = 0L;
615          biglob(hp)->nrd += nrays - hp->bi[i].nrd;
616          hp->bi[i].nrd = nrays;
617 <        hp->dirty++;            /* section directory now out of date */
617 >        markdirty(hp, i);               /* section directory now out of date */
618          return(1);
619   }
620  
# Line 359 | Line 624 | hdfreebeam(hp, i)              /* free beam, writing if dirty */
624   register HOLO   *hp;
625   register int    i;
626   {
627 <        int     nchanged, n;
627 >        int     nchanged;
628  
629          if (hp == NULL) {               /* clear all holodecks */
630                  nchanged = 0;
# Line 367 | Line 632 | register int   i;
632                          nchanged += hdfreebeam(hdlist[i], 0);
633                  return(nchanged);
634          }
635 +        if (hdfragl[hp->fd].writerr)    /* check for file error */
636 +                return(0);
637          if (i == 0) {                   /* clear entire holodeck */
638                  nchanged = 0;
639 <                for (i = 1; i <= nbeams(hp); i++)
640 <                        nchanged += hdfreebeam(hp, i);
639 >                for (i = nbeams(hp); i > 0; i--)
640 >                        if (hp->bl[i] != NULL)
641 >                                nchanged += hdfreebeam(hp, i);
642                  return(nchanged);
643          }
644 + #ifdef DEBUG
645          if (i < 1 | i > nbeams(hp))
646                  error(CONSISTENCY, "bad beam index to hdfreebeam");
647 + #endif
648          if (hp->bl[i] == NULL)
649                  return(0);
650                                          /* check for additions */
651          nchanged = hp->bl[i]->nrm - hp->bi[i].nrd;
652 <        if (nchanged) {
653 <                hdgetbi(hp, i);                 /* allocate a file position */
384 <                errno = 0;
385 <                if (lseek(hp->fd, hp->bi[i].fo, 0) < 0)
386 <                        error(SYSTEM, "cannot seek on holodeck file");
387 <                n = hp->bl[i]->nrm * sizeof(RAYVAL);
388 <                if (write(hp->fd, (char *)hdbray(hp->bl[i]), n) != n)
389 <                        error(SYSTEM, "write error in hdfreebeam");
390 <        }
652 >        if (nchanged)
653 >                hdsyncbeam(hp, i);              /* write new fragment */
654          blglob(hp)->nrm -= hp->bl[i]->nrm;
655          free((char *)hp->bl[i]);                /* free memory */
656          hp->bl[i] = NULL;
# Line 395 | Line 658 | register int   i;
658   }
659  
660  
661 < hdlrulist(ha, ba, n, hp)        /* add beams from holodeck to LRU list */
662 < register HOLO   *ha[];                  /* section list (NULL terminated) */
663 < register int    ba[];                   /* beam index to go with section */
664 < int     n;                              /* length of arrays minus 1 */
661 > int
662 > hdkillbeam(hp, i)               /* delete beam from holodeck */
663 > register HOLO   *hp;
664 > register int    i;
665 > {
666 >        static BEAM     emptybeam;
667 >        int     nchanged;
668 >
669 >        if (hp == NULL) {               /* clobber all holodecks */
670 >                nchanged = 0;
671 >                for (i = 0; hdlist[i] != NULL; i++)
672 >                        nchanged += hdkillbeam(hdlist[i], 0);
673 >                return(nchanged);
674 >        }
675 >        if (i == 0) {                   /* clobber entire holodeck */
676 >                nchanged = 0;
677 >                for (i = nbeams(hp); i > 0; i--)
678 >                        if (hp->bi[i].nrd > 0 || hp->bl[i] != NULL)
679 >                                nchanged += hdkillbeam(hp, i);
680 > #ifdef DEBUG
681 >                if (biglob(hp)->nrd != 0 | blglob(hp)->nrm != 0)
682 >                        error(CONSISTENCY, "bad beam count in hdkillbeam");
683 > #endif
684 >                return(nchanged);
685 >        }
686 > #ifdef DEBUG
687 >        if (i < 1 | i > nbeams(hp))
688 >                error(CONSISTENCY, "bad beam index to hdkillbeam");
689 > #endif
690 >        if (hp->bl[i] != NULL) {        /* free memory */
691 >                blglob(hp)->nrm -= nchanged = hp->bl[i]->nrm;
692 >                free((char *)hp->bl[i]);
693 >        } else
694 >                nchanged = hp->bi[i].nrd;
695 >        if (hp->bi[i].nrd) {            /* free file fragment */
696 >                hp->bl[i] = &emptybeam;
697 >                hdsyncbeam(hp, i);
698 >        }
699 >        hp->bl[i] = NULL;
700 >        return(nchanged);
701 > }
702 >
703 >
704 > int
705 > hdlrulist(hb, nents, n, hp)     /* add beams from holodeck to LRU list */
706 > register HDBEAMI        *hb;            /* beam list */
707 > int     nents;                          /* current list length */
708 > int     n;                              /* maximum list length */
709   register HOLO   *hp;                    /* section we're adding from */
710   {
711          register int    i, j;
405        int     nents;
406                                        /* find last entry in LRU list */
407        for (j = 0; ha[j] != NULL; j++)
408                ;
409        nents = j;
712                                          /* insert each beam from hp */
713 <        for (i = nbeams(hp); i > 0; i-- ) {
713 >        for (i = 1; i <= nbeams(hp); i++) {
714                  if (hp->bl[i] == NULL)          /* check if loaded */
715                          continue;
716 <                if ((j = ++nents) > n)          /* grow list if we can */
716 > #if 0
717 >                if (hp->bl[i]->tick == hdclock) /* preempt swap? */
718 >                        continue;
719 > #endif
720 >                if ((j = ++nents) >= n)         /* grow list if we can */
721                          nents--;
722                  for ( ; ; ) {                   /* bubble into place */
723                          if (!--j || hp->bl[i]->tick >=
724 <                                        ha[j-1]->bl[ba[j-1]]->tick) {
725 <                                ha[j] = hp;
726 <                                ba[j] = i;
724 >                                        hb[j-1].h->bl[hb[j-1].b]->tick) {
725 >                                hb[j].h = hp;
726 >                                hb[j].b = i;
727                                  break;
728                          }
729 <                        ha[j] = ha[j-1];
424 <                        ba[j] = ba[j-1];
729 >                        copystruct(hb+j, hb+(j-1));
730                  }
731          }
732 <        ha[nents] = NULL;               /* all done */
428 <        ba[nents] = 0;
732 >        return(nents);                  /* return new list length */
733   }
734  
735  
736 + int
737   hdfreecache(pct, honly)         /* free up cache space, writing changes */
738   int     pct;                            /* maximum percentage to free */
739   register HOLO   *honly;                 /* NULL means check all */
740   {
741 <        HOLO    *hp[FREEBEAMS+1];
437 <        int     bn[FREEBEAMS+1];
741 >        HDBEAMI hb[FREEBEAMS];
742          int     freetarget;
743 +        int     n;
744          register int    i;
745 + #ifdef DEBUG
746 +        unsigned        membefore;
747 +
748 +        membefore = hdmemuse(0);
749 + #endif
750                                                  /* compute free target */
751          freetarget = (honly != NULL) ? blglob(honly)->nrm :
752                          hdmemuse(0)/sizeof(RAYVAL) ;
753          freetarget = freetarget*pct/100;
754 +        if (freetarget <= 0)
755 +                return(0);
756                                                  /* find least recently used */
757 <        hp[0] = NULL;
446 <        bn[0] = 0;
757 >        n = 0;
758          if (honly != NULL)
759 <                hdlrulist(hp, bn, FREEBEAMS, honly);
759 >                n = hdlrulist(hb, n, FREEBEAMS, honly);
760          else
761                  for (i = 0; hdlist[i] != NULL; i++)
762 <                        hdlrulist(hp, bn, FREEBEAMS, hdlist[i]);
762 >                        n = hdlrulist(hb, n, FREEBEAMS, hdlist[i]);
763                                                  /* free LRU beams */
764 <        for (i = 0; hp[i] != NULL; i++) {
765 <                hdfreebeam(hp[i], bn[i]);
766 <                if ((freetarget -= hp[i]->bi[bn[i]].nrd) <= 0)
764 >        for (i = 0; i < n; i++) {
765 >                hdfreebeam(hb[i].h, hb[i].b);
766 >                if ((freetarget -= hb[i].h->bi[hb[i].b].nrd) <= 0)
767                          break;
768          }
769 <        hdsync(honly);          /* synchronize directories as necessary */
769 >        hdsync(honly, 0);       /* synchronize directories as necessary */
770 > #ifdef DEBUG
771 >        sprintf(errmsg,
772 >        "%dK before, %dK after hdfreecache (%dK total), %d rays short\n",
773 >                membefore>>10, hdmemuse(0)>>10, hdmemuse(1)>>10, freetarget);
774 >        wputs(errmsg);
775 > #endif
776 >        return(-freetarget);    /* return how far past goal we went */
777   }
778  
779  
# Line 467 | Line 785 | register HOLO  *hp;            /* NULL means clean up all */
785          if (hp == NULL) {               /* NULL means clean up everything */
786                  while (hdlist[0] != NULL)
787                          hddone(hdlist[0]);
788 +                free((char *)hdfragl);
789 +                hdfragl = NULL; nhdfragls = 0;
790                  return;
791          }
792                                          /* flush all data and free memory */
793 <        hdflush(hp);
793 >        hdfreebeam(hp, 0);
794 >        hdsync(hp, 0);
795                                          /* release fragment resources */
796          hdrelease(hp->fd);
797                                          /* remove hp from active list */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines