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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines