ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/holofile.c
Revision: 3.31
Committed: Wed Dec 30 11:30:04 1998 UTC (25 years, 4 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 3.30: +9 -6 lines
Log Message:
made it so we don't toss off large fragment for smaller one
fixed accumulation of empty fragments when maximum list size reached

File Contents

# User Rev Content
1 gregl 3.20 /* Copyright (c) 1998 Silicon Graphics, Inc. */
2 gregl 3.1
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ SGI";
5     #endif
6    
7     /*
8     * Routines for managing holodeck files
9     *
10     * 9/30/97 GWLarson
11     */
12    
13     #include "holo.h"
14    
15     #ifndef CACHESIZE
16     #define CACHESIZE 16 /* default cache size (Mbytes, 0==inf) */
17     #endif
18     #ifndef FREEBEAMS
19 gwlarson 3.25 #define FREEBEAMS 1500 /* maximum beams to free at a time */
20 gregl 3.1 #endif
21     #ifndef PCTFREE
22 gregl 3.21 #define PCTFREE 15 /* maximum fraction to free (%) */
23 gregl 3.1 #endif
24 gwlarson 3.31 #ifndef MAXFRAGB
25     #define MAXFRAGB 16 /* fragment blocks/file to track (0==inf) */
26 gregl 3.15 #endif
27 gwlarson 3.29 #ifndef MINDIRSEL
28     /* minimum directory seek length */
29     #define MINDIRSEL (4*BUFSIZ/sizeof(BEAMI))
30     #endif
31 gregl 3.1
32 gregl 3.6 #ifndef BSD
33     #define write writebuf /* safe i/o routines */
34     #define read readbuf
35     #endif
36 gregl 3.4
37 gwlarson 3.31 #define FRAGBLK 512 /* number of fragments to allocate at a time */
38 gregl 3.4
39 gregl 3.14 unsigned hdcachesize = CACHESIZE*1024*1024; /* target cache size */
40 gregl 3.1 unsigned long hdclock; /* clock value */
41    
42     HOLO *hdlist[HDMAX+1]; /* holodeck pointers (NULL term.) */
43    
44 gregl 3.16 static struct fraglist {
45 gregl 3.1 short nlinks; /* number of holodeck sections using us */
46 gregl 3.13 short writerr; /* write error encountered */
47     int nfrags; /* number of known fragments */
48 gregl 3.4 BEAMI *fi; /* fragments, descending file position */
49 gregl 3.1 long flen; /* last known file length */
50 gregl 3.16 } *hdfragl; /* fragment lists, indexed by file descriptor */
51 gregl 3.1
52 gregl 3.16 static int nhdfragls; /* size of hdfragl array */
53 gregl 3.1
54 gregl 3.4
55 gwlarson 3.26 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 gregl 3.1 hdattach(fd) /* start tracking file fragments for some section */
80     register int fd;
81     {
82 gregl 3.16 if (fd >= nhdfragls) {
83 gwlarson 3.26 hdfragl = (struct fraglist *)hdrealloc((char *)hdfragl,
84     (fd+1)*sizeof(struct fraglist), "hdattach");
85 gregl 3.16 bzero((char *)(hdfragl+nhdfragls),
86     (fd+1-nhdfragls)*sizeof(struct fraglist));
87     nhdfragls = fd+1;
88 gregl 3.1 }
89 gregl 3.16 hdfragl[fd].nlinks++;
90     hdfragl[fd].flen = lseek(fd, 0L, 2); /* get file length */
91 gregl 3.1 }
92    
93    
94     /* Do we need a routine to locate file fragments given known occupants? */
95    
96    
97     hdrelease(fd) /* stop tracking file fragments for some section */
98     register int fd;
99     {
100 gregl 3.16 if (fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks)
101 gregl 3.1 return;
102 gregl 3.16 if (!--hdfragl[fd].nlinks && hdfragl[fd].nfrags) {
103     free((char *)hdfragl[fd].fi);
104     hdfragl[fd].fi = NULL;
105     hdfragl[fd].nfrags = 0;
106 gregl 3.1 }
107     }
108    
109    
110     HOLO *
111     hdinit(fd, hproto) /* initialize a holodeck section in a file */
112     int fd; /* corresponding file descriptor */
113     HDGRID *hproto; /* holodeck section grid */
114     {
115 gwlarson 3.22 long rtrunc;
116 gregl 3.1 long fpos;
117     register HOLO *hp;
118     register int n;
119     /* prepare for system errors */
120     errno = 0;
121     if ((fpos = lseek(fd, 0L, 1)) < 0)
122     error(SYSTEM, "cannot determine holodeck file position");
123     if (hproto == NULL) { /* assume we're loading it */
124     HDGRID hpr;
125     /* load header */
126     if (read(fd, (char *)&hpr, sizeof(HDGRID)) != sizeof(HDGRID))
127     error(SYSTEM, "cannot load holodeck header");
128     /* allocate grid */
129     if ((hp = hdalloc(&hpr)) == NULL)
130     goto memerr;
131     /* load beam directory */
132     n = nbeams(hp)*sizeof(BEAMI);
133     if (read(fd, (char *)(hp->bi+1), n) != n)
134     error(SYSTEM, "failure loading holodeck directory");
135 gregl 3.6 /* check that it's clean */
136 gwlarson 3.28 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 gregl 3.1 } else { /* assume we're creating it */
143     if ((hp = hdalloc(hproto)) == NULL)
144     goto memerr;
145     /* write header and skeleton */
146     n = nbeams(hp)*sizeof(BEAMI);
147     if (write(fd, (char *)hproto, sizeof(HDGRID)) !=
148     sizeof(HDGRID) ||
149     write(fd, (char *)(hp->bi+1), n) != n)
150     error(SYSTEM, "cannot write header to holodeck file");
151     }
152     hp->fd = fd;
153     hp->dirty = 0;
154     biglob(hp)->fo = fpos + sizeof(HDGRID);
155 gregl 3.13 /* start tracking fragments */
156     hdattach(fd);
157     /* check rays on disk */
158     fpos = hdfilen(fd);
159 gwlarson 3.22 biglob(hp)->nrd = rtrunc = 0;
160 gregl 3.13 for (n = hproto == NULL ? nbeams(hp) : 0; n > 0; n--)
161     if (hp->bi[n].nrd)
162 gwlarson 3.27 if (hp->bi[n].fo+hp->bi[n].nrd*sizeof(RAYVAL) > fpos) {
163 gwlarson 3.22 rtrunc += hp->bi[n].nrd;
164     hp->bi[n].nrd = 0;
165     } else
166 gregl 3.13 biglob(hp)->nrd += hp->bi[n].nrd;
167 gwlarson 3.22 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 gregl 3.1 /* add to holodeck list */
173     for (n = 0; n < HDMAX; n++)
174     if (hdlist[n] == NULL) {
175     hdlist[n] = hp;
176     break;
177     }
178     /* all done */
179     return(hp);
180     memerr:
181     error(SYSTEM, "cannot allocate holodeck grid");
182     }
183    
184    
185 gwlarson 3.28 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 gwlarson 3.29 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 gwlarson 3.28 (hp->dirseg[j-1].s + hp->dirseg[j-1].n);
220 gwlarson 3.29 }
221     /* good enough? */
222     if (hp->dirty <= MAXDIRSE && mindist > MINDIRSEL)
223     break;
224 gwlarson 3.28 j = minpos - 1; /* coalesce neighbors */
225     if (hp->dirseg[j].s + hp->dirseg[j].n <
226 gwlarson 3.29 hp->dirseg[minpos].s + hp->dirseg[minpos].n)
227 gwlarson 3.28 hp->dirseg[j].n = hp->dirseg[minpos].s +
228     hp->dirseg[minpos].n - hp->dirseg[j].s;
229 gwlarson 3.29 hp->dirty--;
230     while (++j < hp->dirty) /* close the gap */
231 gwlarson 3.28 copystruct(hp->dirseg+j, hp->dirseg+(j+1));
232 gwlarson 3.29 } while (mindist <= MINDIRSEL);
233 gwlarson 3.28 }
234    
235    
236 gregl 3.1 int
237 gregl 3.12 hdsync(hp, all) /* update beams and directory on disk */
238 gregl 3.1 register HOLO *hp;
239 gregl 3.12 int all;
240 gregl 3.1 {
241     register int j, n;
242    
243 gregl 3.12 if (hp == NULL) { /* do all holodecks */
244 gregl 3.1 n = 0;
245     for (j = 0; hdlist[j] != NULL; j++)
246 gregl 3.12 n += hdsync(hdlist[j], all);
247 gregl 3.1 return(n);
248     }
249 gregl 3.12 /* sync the beams */
250 gregl 3.15 for (j = (all ? nbeams(hp) : 0); j > 0; j--)
251 gregl 3.12 if (hp->bl[j] != NULL)
252     hdsyncbeam(hp, j);
253 gregl 3.13 if (!hp->dirty) /* directory clean? */
254 gregl 3.1 return(0);
255 gwlarson 3.28 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 gregl 3.1 return(1);
266     }
267    
268    
269 gregl 3.14 unsigned
270 gregl 3.1 hdmemuse(all) /* return memory usage (in bytes) */
271     int all; /* include overhead (painful) */
272     {
273     long total = 0;
274     register int i, j;
275    
276     for (j = 0; hdlist[j] != NULL; j++) {
277     total += blglob(hdlist[j])->nrm * sizeof(RAYVAL);
278     if (all) {
279     total += sizeof(HOLO) + sizeof(BEAM *) +
280     nbeams(hdlist[j]) *
281     (sizeof(BEAM *)+sizeof(BEAMI));
282     for (i = nbeams(hdlist[j]); i > 0; i--)
283     if (hdlist[j]->bl[i] != NULL)
284     total += sizeof(BEAM);
285     }
286     }
287     if (all)
288 gregl 3.16 for (j = 0; j < nhdfragls; j++) {
289     total += sizeof(struct fraglist);
290     if (hdfragl[j].nfrags)
291 gregl 3.4 total += FRAGBLK*sizeof(BEAMI) *
292 gregl 3.16 ((hdfragl[j].nfrags-1)/FRAGBLK + 1) ;
293 gregl 3.4 }
294 gregl 3.1 return(total);
295 gregl 3.8 }
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 gregl 3.16 if (fd >= nhdfragls || !hdfragl[fd].nlinks) {
307 gregl 3.8 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 gregl 3.16 return(hdfragl[fd].flen);
314 gregl 3.1 }
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 */
321     {
322     long total = 0;
323     register int i, j;
324    
325     for (j = 0; hdlist[j] != NULL; j++) {
326     if (hdlist[j]->fd != fd)
327     continue;
328     total += biglob(hdlist[j])->nrd * sizeof(RAYVAL);
329     if (all) {
330     for (i = nbeams(hdlist[j]); i > 0; i--)
331     if (hdlist[j]->bl[i] != NULL)
332     total += sizeof(RAYVAL) *
333     (hdlist[j]->bl[i]->nrm -
334     hdlist[j]->bi[i].nrd);
335     total += sizeof(HDGRID) +
336     nbeams(hdlist[j])*sizeof(BEAMI);
337     }
338     }
339     return(total); /* does not include fragments */
340     }
341    
342    
343     RAYVAL *
344     hdnewrays(hp, i, nr) /* allocate space for add'l rays and return pointer */
345     register HOLO *hp;
346     register int i;
347     int nr; /* number of new rays desired */
348     {
349     RAYVAL *p;
350     int n;
351    
352     if (nr <= 0)
353     return(NULL);
354     if (i < 1 | i > nbeams(hp))
355     error(CONSISTENCY, "bad beam index given to hdnewrays");
356     if (hp->bl[i] != NULL)
357     hp->bl[i]->tick = hdclock; /* preempt swap */
358     if (hdcachesize > 0 && hdmemuse(0) >= hdcachesize)
359     hdfreecache(PCTFREE, NULL); /* free some space */
360     if (hp->bl[i] == NULL) { /* allocate (and load) */
361     n = hp->bi[i].nrd + nr;
362 gwlarson 3.26 hp->bl[i] = (BEAM *)hdrealloc(NULL, hdbsiz(n), "hdnewrays");
363 gregl 3.1 blglob(hp)->nrm += n;
364     if (n = hp->bl[i]->nrm = hp->bi[i].nrd) {
365 gwlarson 3.26 errno = 0;
366 gregl 3.1 if (lseek(hp->fd, hp->bi[i].fo, 0) < 0)
367     error(SYSTEM, "seek error on holodeck file");
368     n *= sizeof(RAYVAL);
369     if (read(hp->fd, (char *)hdbray(hp->bl[i]), n) != n)
370     error(SYSTEM,
371     "error reading beam from holodeck file");
372     }
373     } else { /* just grow in memory */
374 gwlarson 3.26 hp->bl[i] = (BEAM *)hdrealloc((char *)hp->bl[i],
375     hdbsiz(hp->bl[i]->nrm + nr), "hdnewrays");
376 gregl 3.1 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 gwlarson 3.25 blglob(hp)->tick = hp->bl[i]->tick = hdclock++; /* update LRU clock */
382 gregl 3.1 return(p); /* point to new rays */
383     }
384    
385    
386     BEAM *
387     hdgetbeam(hp, i) /* get beam (from file if necessary) */
388     register HOLO *hp;
389     register int i;
390     {
391     register int n;
392    
393     if (i < 1 | i > nbeams(hp))
394     error(CONSISTENCY, "bad beam index given to hdgetbeam");
395     if (hp->bl[i] == NULL) { /* load from disk */
396     if (!(n = hp->bi[i].nrd))
397     return(NULL);
398     if (hdcachesize > 0 && hdmemuse(0) >= hdcachesize)
399     hdfreecache(PCTFREE, NULL); /* get free space */
400 gwlarson 3.26 hp->bl[i] = (BEAM *)hdrealloc(NULL, hdbsiz(n), "hdgetbeam");
401     blglob(hp)->nrm += hp->bl[i]->nrm = n;
402 gregl 3.1 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 gwlarson 3.25 blglob(hp)->tick = hp->bl[i]->tick = hdclock++; /* update LRU clock */
410 gregl 3.1 return(hp->bl[i]);
411     }
412    
413    
414     int
415 gregl 3.17 hdfilord(hb1, hb2) /* order beams for quick loading */
416 gregl 3.14 register HDBEAMI *hb1, *hb2;
417     {
418 gregl 3.17 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 gregl 3.14 if ((c = hb1->h->fd - hb2->h->fd))
426     return(c);
427     /* then by position in file */
428 gregl 3.17 c = hb1->h->bi[hb1->b].fo - hb2->h->bi[hb2->b].fo;
429     return(c > 0 ? 1 : c < 0 ? -1 : 0);
430 gregl 3.14 }
431    
432    
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 gwlarson 3.23 int bytesloaded, needbytes, bytes2free;
440 gregl 3.14 register BEAM *bp;
441     register int i;
442     /* precheck consistency */
443 gwlarson 3.25 if (n <= 0) return;
444 gregl 3.14 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 gregl 3.17 bytesloaded = 0; /* run through loaded beams */
450     for ( ; n && (bp = hb->h->bl[hb->b]) != NULL; n--, hb++) {
451 gwlarson 3.23 bp->tick = hdclock; /* preempt swap */
452 gregl 3.17 bytesloaded += bp->nrm;
453     if (bf != NULL)
454 gwlarson 3.23 (*bf)(bp, hb);
455 gregl 3.17 }
456     bytesloaded *= sizeof(RAYVAL);
457 gregl 3.15 if ((origcachesize = hdcachesize) > 0) {
458 gregl 3.17 needbytes = 0; /* figure out memory needs */
459 gregl 3.15 for (i = n; i--; )
460 gregl 3.17 needbytes += hb[i].h->bi[hb[i].b].nrd;
461 gregl 3.15 needbytes *= sizeof(RAYVAL);
462     do { /* free enough memory */
463     memuse = hdmemuse(0);
464 gregl 3.17 bytes2free = needbytes - (int)(hdcachesize-memuse);
465     if (bytes2free > (int)(memuse - bytesloaded))
466 gregl 3.15 bytes2free = memuse - bytesloaded;
467     } while (bytes2free > 0 &&
468     hdfreecache(100*bytes2free/memuse, NULL) < 0);
469 gregl 3.17 hdcachesize = 0; /* load beams w/o swap */
470 gregl 3.15 }
471 gregl 3.14 for (i = 0; i < n; i++)
472     if ((bp = hdgetbeam(hb[i].h, hb[i].b)) != NULL && bf != NULL)
473 gwlarson 3.23 (*bf)(bp, hb+i);
474 gregl 3.14 hdcachesize = origcachesize; /* resume dynamic swapping */
475     }
476    
477    
478 gregl 3.19 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 gwlarson 3.31 #if MAXFRAGB
505     if (j >= MAXFRAGB*FRAGBLK) {
506     f->nfrags = j--; /* stop list growth */
507     if (bi->nrd <= f->fi[j].nrd)
508     return; /* new one no better than discard */
509     }
510 gregl 3.19 #endif
511     if (j % FRAGBLK == 0) { /* more free list space */
512 gwlarson 3.26 register BEAMI *newp;
513 gregl 3.19 if (f->fi == NULL)
514 gwlarson 3.26 newp = (BEAMI *)malloc((j+FRAGBLK)*sizeof(BEAMI));
515 gregl 3.19 else
516 gwlarson 3.26 newp = (BEAMI *)realloc((char *)f->fi,
517 gregl 3.19 (j+FRAGBLK)*sizeof(BEAMI));
518 gwlarson 3.26 if (newp == NULL) {
519     f->nfrags--; /* graceful failure */
520     return;
521     }
522     f->fi = newp;
523 gregl 3.19 }
524     for ( ; ; j--) { /* insert in descending list */
525     if (!j || bi->fo < f->fi[j-1].fo) {
526     f->fi[j].fo = bi->fo;
527     f->fi[j].nrd = bi->nrd;
528     break;
529     }
530     copystruct(f->fi+j, f->fi+(j-1));
531     }
532     /* coalesce adjacent fragments */
533     /* successors never empty */
534     if (j && f->fi[j-1].fo == f->fi[j].fo + f->fi[j].nrd*sizeof(RAYVAL)) {
535     f->fi[j].nrd += f->fi[j-1].nrd;
536     f->fi[j-1].nrd = 0;
537     }
538     for (k = j+1; k < f->nfrags; k++) /* get non-empty predecessor */
539     if (f->fi[k].nrd) {
540     if (f->fi[j].fo == f->fi[k].fo +
541     f->fi[k].nrd*sizeof(RAYVAL)) {
542     f->fi[k].nrd += f->fi[j].nrd;
543     f->fi[j].nrd = 0;
544     }
545     break;
546     }
547     }
548    
549    
550     long
551     hdallocfrag(fd, nrays) /* allocate a file fragment */
552     int fd;
553     unsigned int4 nrays;
554     {
555     register struct fraglist *f;
556     register int j, k;
557     long nfo;
558    
559     if (nrays == 0)
560     return(-1L);
561     #ifdef DEBUG
562     if (fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks)
563     error(CONSISTENCY, "bad file descriptor in hdallocfrag");
564     #endif
565     f = &hdfragl[fd];
566     k = -1; /* find closest-sized fragment */
567     for (j = f->nfrags; j-- > 0; )
568     if (f->fi[j].nrd >= nrays &&
569     (k < 0 || f->fi[j].nrd < f->fi[k].nrd))
570     if (f->fi[k=j].nrd == nrays)
571     break;
572     if (k < 0) { /* no fragment -- extend file */
573     nfo = f->flen;
574     f->flen += nrays*sizeof(RAYVAL);
575     } else { /* else use fragment */
576     nfo = f->fi[k].fo;
577     f->fi[k].fo += nrays*sizeof(RAYVAL);
578     f->fi[k].nrd -= nrays;
579     }
580     return(nfo);
581     }
582    
583    
584 gregl 3.14 int
585 gregl 3.12 hdsyncbeam(hp, i) /* sync beam in memory with beam on disk */
586 gregl 3.1 register HOLO *hp;
587     register int i;
588     {
589 gregl 3.19 unsigned int4 nrays;
590     unsigned int n;
591 gregl 3.11 long nfo;
592 gregl 3.13 /* check file status */
593 gregl 3.16 if (hdfragl[hp->fd].writerr)
594 gregl 3.13 return(-1);
595 gregl 3.12 #ifdef DEBUG
596     if (i < 1 | i > nbeams(hp))
597     error(CONSISTENCY, "bad beam index in hdsyncbeam");
598     #endif
599 gregl 3.11 /* is current fragment OK? */
600 gregl 3.12 if (hp->bl[i] == NULL || (nrays = hp->bl[i]->nrm) == hp->bi[i].nrd)
601 gregl 3.1 return(0);
602 gregl 3.19 if (hp->bi[i].nrd) /* relinquish old fragment */
603     hdfreefrag(hp->fd, &hp->bi[i]);
604     if (nrays) { /* get and write new fragment */
605     nfo = hdallocfrag(hp->fd, nrays);
606 gregl 3.11 errno = 0;
607     if (lseek(hp->fd, nfo, 0) < 0)
608     error(SYSTEM, "cannot seek on holodeck file");
609     n = hp->bl[i]->nrm * sizeof(RAYVAL);
610     if (write(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) {
611 gregl 3.16 hdfragl[hp->fd].writerr++;
612 gregl 3.18 hdsync(NULL, 0); /* sync directories */
613 gregl 3.12 error(SYSTEM, "write error in hdsyncbeam");
614 gregl 3.11 }
615 gregl 3.19 hp->bi[i].fo = nfo;
616     } else
617     hp->bi[i].fo = 0L;
618 gregl 3.1 biglob(hp)->nrd += nrays - hp->bi[i].nrd;
619     hp->bi[i].nrd = nrays;
620 gwlarson 3.28 markdirty(hp, i); /* section directory now out of date */
621 gregl 3.1 return(1);
622     }
623    
624    
625     int
626     hdfreebeam(hp, i) /* free beam, writing if dirty */
627     register HOLO *hp;
628     register int i;
629     {
630 gregl 3.11 int nchanged;
631 gregl 3.1
632     if (hp == NULL) { /* clear all holodecks */
633     nchanged = 0;
634     for (i = 0; hdlist[i] != NULL; i++)
635     nchanged += hdfreebeam(hdlist[i], 0);
636     return(nchanged);
637     }
638 gregl 3.16 if (hdfragl[hp->fd].writerr) /* check for file error */
639 gregl 3.13 return(0);
640 gregl 3.1 if (i == 0) { /* clear entire holodeck */
641     nchanged = 0;
642 gregl 3.10 for (i = nbeams(hp); i > 0; i--)
643     if (hp->bl[i] != NULL)
644     nchanged += hdfreebeam(hp, i);
645 gregl 3.1 return(nchanged);
646     }
647 gregl 3.12 #ifdef DEBUG
648 gregl 3.1 if (i < 1 | i > nbeams(hp))
649     error(CONSISTENCY, "bad beam index to hdfreebeam");
650 gregl 3.12 #endif
651 gregl 3.1 if (hp->bl[i] == NULL)
652     return(0);
653     /* check for additions */
654     nchanged = hp->bl[i]->nrm - hp->bi[i].nrd;
655 gregl 3.11 if (nchanged)
656 gregl 3.12 hdsyncbeam(hp, i); /* write new fragment */
657 gregl 3.1 blglob(hp)->nrm -= hp->bl[i]->nrm;
658     free((char *)hp->bl[i]); /* free memory */
659     hp->bl[i] = NULL;
660     return(nchanged);
661     }
662    
663    
664 gregl 3.10 int
665     hdkillbeam(hp, i) /* delete beam from holodeck */
666     register HOLO *hp;
667     register int i;
668     {
669     static BEAM emptybeam;
670 gregl 3.11 int nchanged;
671 gregl 3.10
672     if (hp == NULL) { /* clobber all holodecks */
673     nchanged = 0;
674     for (i = 0; hdlist[i] != NULL; i++)
675     nchanged += hdkillbeam(hdlist[i], 0);
676     return(nchanged);
677     }
678     if (i == 0) { /* clobber entire holodeck */
679     nchanged = 0;
680     for (i = nbeams(hp); i > 0; i--)
681     if (hp->bi[i].nrd > 0 || hp->bl[i] != NULL)
682     nchanged += hdkillbeam(hp, i);
683     #ifdef DEBUG
684     if (biglob(hp)->nrd != 0 | blglob(hp)->nrm != 0)
685     error(CONSISTENCY, "bad beam count in hdkillbeam");
686     #endif
687     return(nchanged);
688     }
689 gregl 3.12 #ifdef DEBUG
690 gregl 3.10 if (i < 1 | i > nbeams(hp))
691     error(CONSISTENCY, "bad beam index to hdkillbeam");
692 gregl 3.12 #endif
693 gregl 3.10 if (hp->bl[i] != NULL) { /* free memory */
694     blglob(hp)->nrm -= nchanged = hp->bl[i]->nrm;
695     free((char *)hp->bl[i]);
696     } else
697     nchanged = hp->bi[i].nrd;
698     if (hp->bi[i].nrd) { /* free file fragment */
699     hp->bl[i] = &emptybeam;
700 gregl 3.12 hdsyncbeam(hp, i);
701 gregl 3.10 }
702     hp->bl[i] = NULL;
703     return(nchanged);
704     }
705    
706    
707 gregl 3.14 int
708     hdlrulist(hb, nents, n, hp) /* add beams from holodeck to LRU list */
709     register HDBEAMI *hb; /* beam list */
710     int nents; /* current list length */
711     int n; /* maximum list length */
712 gregl 3.1 register HOLO *hp; /* section we're adding from */
713     {
714     register int i, j;
715     /* insert each beam from hp */
716 gregl 3.21 for (i = 1; i <= nbeams(hp); i++) {
717 gregl 3.1 if (hp->bl[i] == NULL) /* check if loaded */
718     continue;
719 gregl 3.14 #if 0
720     if (hp->bl[i]->tick == hdclock) /* preempt swap? */
721     continue;
722     #endif
723     if ((j = ++nents) >= n) /* grow list if we can */
724 gregl 3.1 nents--;
725     for ( ; ; ) { /* bubble into place */
726     if (!--j || hp->bl[i]->tick >=
727 gregl 3.14 hb[j-1].h->bl[hb[j-1].b]->tick) {
728     hb[j].h = hp;
729     hb[j].b = i;
730 gregl 3.1 break;
731     }
732 gregl 3.14 copystruct(hb+j, hb+(j-1));
733 gregl 3.1 }
734     }
735 gregl 3.14 return(nents); /* return new list length */
736 gregl 3.1 }
737    
738    
739 gregl 3.14 int
740 gregl 3.1 hdfreecache(pct, honly) /* free up cache space, writing changes */
741     int pct; /* maximum percentage to free */
742     register HOLO *honly; /* NULL means check all */
743     {
744 gregl 3.14 HDBEAMI hb[FREEBEAMS];
745 gregl 3.1 int freetarget;
746 gregl 3.14 int n;
747 gregl 3.1 register int i;
748 gwlarson 3.25 #ifdef DEBUG
749     unsigned membefore;
750    
751     membefore = hdmemuse(0);
752     #endif
753 gregl 3.1 /* compute free target */
754     freetarget = (honly != NULL) ? blglob(honly)->nrm :
755     hdmemuse(0)/sizeof(RAYVAL) ;
756     freetarget = freetarget*pct/100;
757 gregl 3.14 if (freetarget <= 0)
758     return(0);
759 gregl 3.1 /* find least recently used */
760 gregl 3.14 n = 0;
761 gregl 3.1 if (honly != NULL)
762 gregl 3.14 n = hdlrulist(hb, n, FREEBEAMS, honly);
763 gregl 3.1 else
764     for (i = 0; hdlist[i] != NULL; i++)
765 gregl 3.14 n = hdlrulist(hb, n, FREEBEAMS, hdlist[i]);
766 gregl 3.1 /* free LRU beams */
767 gregl 3.14 for (i = 0; i < n; i++) {
768     hdfreebeam(hb[i].h, hb[i].b);
769     if ((freetarget -= hb[i].h->bi[hb[i].b].nrd) <= 0)
770 gregl 3.1 break;
771     }
772 gregl 3.12 hdsync(honly, 0); /* synchronize directories as necessary */
773 gwlarson 3.25 #ifdef DEBUG
774     sprintf(errmsg,
775     "%dK before, %dK after hdfreecache (%dK total), %d rays short\n",
776     membefore>>10, hdmemuse(0)>>10, hdmemuse(1)>>10, freetarget);
777     wputs(errmsg);
778     #endif
779 gregl 3.14 return(-freetarget); /* return how far past goal we went */
780 gregl 3.1 }
781    
782    
783     hddone(hp) /* clean up holodeck section and free */
784     register HOLO *hp; /* NULL means clean up all */
785     {
786     register int i;
787    
788     if (hp == NULL) { /* NULL means clean up everything */
789     while (hdlist[0] != NULL)
790     hddone(hdlist[0]);
791 gwlarson 3.24 free((char *)hdfragl);
792     hdfragl = NULL; nhdfragls = 0;
793 gregl 3.1 return;
794     }
795     /* flush all data and free memory */
796 gwlarson 3.27 hdfreebeam(hp, 0);
797     hdsync(hp, 0);
798 gregl 3.1 /* release fragment resources */
799     hdrelease(hp->fd);
800     /* remove hp from active list */
801     for (i = 0; hdlist[i] != NULL; i++)
802     if (hdlist[i] == hp) {
803     while ((hdlist[i] = hdlist[i+1]) != NULL)
804     i++;
805     break;
806     }
807     free((char *)hp->bl); /* free beam list */
808     free((char *)hp); /* free holodeck struct */
809     }