ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/holofile.c
Revision: 3.33
Committed: Sat Jan 9 09:16:14 1999 UTC (25 years, 3 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 3.32: +13 -14 lines
Log Message:
changed to first-fit algorithm in hdallocfrag()

File Contents

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