ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/holofile.c
Revision: 3.25
Committed: Fri Sep 18 13:51:01 1998 UTC (25 years, 7 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 3.24: +15 -5 lines
Log Message:
added debugging statement to track hdfreecache() performance

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