1 |
< |
/* Copyright (c) 1997 Silicon Graphics, Inc. */ |
1 |
> |
/* Copyright (c) 1998 Silicon Graphics, Inc. */ |
2 |
|
|
3 |
|
#ifndef lint |
4 |
|
static char SCCSid[] = "$SunId$ SGI"; |
16 |
|
#define CACHESIZE 16 /* default cache size (Mbytes, 0==inf) */ |
17 |
|
#endif |
18 |
|
#ifndef FREEBEAMS |
19 |
< |
#define FREEBEAMS 512 /* 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 20 /* maximum fraction to free (%) */ |
22 |
> |
#define PCTFREE 15 /* maximum fraction to free (%) */ |
23 |
|
#endif |
24 |
+ |
#ifndef MAXFRAG |
25 |
+ |
#define MAXFRAG 32767 /* maximum fragments/file to track (0==inf) */ |
26 |
+ |
#endif |
27 |
|
|
25 |
– |
/* define MAXFRAG if you want to limit fragment tracking memory */ |
26 |
– |
|
28 |
|
#ifndef BSD |
29 |
|
#define write writebuf /* safe i/o routines */ |
30 |
|
#define read readbuf |
31 |
|
#endif |
32 |
|
|
33 |
< |
#define FRAGBLK 64 /* number of fragments to allocate at a time */ |
33 |
> |
#define FRAGBLK 256 /* number of fragments to allocate at a time */ |
34 |
|
|
35 |
< |
int hdcachesize = CACHESIZE*1024*1024; /* target cache size (bytes) */ |
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 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; /* fragment lists, indexed by file descriptor */ |
46 |
> |
} *hdfragl; /* fragment lists, indexed by file descriptor */ |
47 |
|
|
48 |
< |
static int nhdfrags; /* size of hdfrag array */ |
48 |
> |
static int nhdfragls; /* size of hdfragl array */ |
49 |
|
|
50 |
|
|
51 |
|
hdattach(fd) /* start tracking file fragments for some section */ |
52 |
|
register int fd; |
53 |
|
{ |
54 |
< |
if (fd >= nhdfrags) { |
55 |
< |
if (nhdfrags) |
56 |
< |
hdfrag = (struct fragment *)realloc((char *)hdfrag, |
57 |
< |
(fd+1)*sizeof(struct fragment)); |
54 |
> |
if (fd >= nhdfragls) { |
55 |
> |
if (nhdfragls) |
56 |
> |
hdfragl = (struct fraglist *)realloc((char *)hdfragl, |
57 |
> |
(fd+1)*sizeof(struct fraglist)); |
58 |
|
else |
59 |
< |
hdfrag = (struct fragment *)malloc( |
60 |
< |
(fd+1)*sizeof(struct fragment)); |
61 |
< |
if (hdfrag == NULL) |
59 |
> |
hdfragl = (struct fraglist *)malloc( |
60 |
> |
(fd+1)*sizeof(struct fraglist)); |
61 |
> |
if (hdfragl == NULL) |
62 |
|
error(SYSTEM, "out of memory in hdattach"); |
63 |
< |
bzero((char *)(hdfrag+nhdfrags), |
64 |
< |
(fd+1-nhdfrags)*sizeof(struct fragment)); |
65 |
< |
nhdfrags = fd+1; |
63 |
> |
bzero((char *)(hdfragl+nhdfragls), |
64 |
> |
(fd+1-nhdfragls)*sizeof(struct fraglist)); |
65 |
> |
nhdfragls = fd+1; |
66 |
|
} |
67 |
< |
hdfrag[fd].nlinks++; |
68 |
< |
hdfrag[fd].flen = lseek(fd, 0L, 2); /* get file length */ |
67 |
> |
hdfragl[fd].nlinks++; |
68 |
> |
hdfragl[fd].flen = lseek(fd, 0L, 2); /* get file length */ |
69 |
|
} |
70 |
|
|
71 |
|
|
75 |
|
hdrelease(fd) /* stop tracking file fragments for some section */ |
76 |
|
register int fd; |
77 |
|
{ |
78 |
< |
if (fd < 0 | fd >= nhdfrags || !hdfrag[fd].nlinks) |
78 |
> |
if (fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks) |
79 |
|
return; |
80 |
< |
if (!--hdfrag[fd].nlinks && hdfrag[fd].nfrags) { |
81 |
< |
free((char *)hdfrag[fd].fi); |
82 |
< |
hdfrag[fd].fi = NULL; |
83 |
< |
hdfrag[fd].nfrags = 0; |
80 |
> |
if (!--hdfragl[fd].nlinks && hdfragl[fd].nfrags) { |
81 |
> |
free((char *)hdfragl[fd].fi); |
82 |
> |
hdfragl[fd].fi = NULL; |
83 |
> |
hdfragl[fd].nfrags = 0; |
84 |
|
} |
85 |
|
} |
86 |
|
|
105 |
|
int fd; /* corresponding file descriptor */ |
106 |
|
HDGRID *hproto; /* holodeck section grid */ |
107 |
|
{ |
108 |
+ |
long rtrunc; |
109 |
|
long fpos; |
110 |
|
register HOLO *hp; |
111 |
|
register int n; |
127 |
|
error(SYSTEM, "failure loading holodeck directory"); |
128 |
|
/* check that it's clean */ |
129 |
|
if (hp->bi[nbeams(hp)].fo < 0) |
130 |
< |
error(USER, "dirty holodeck section"); |
130 |
> |
error(WARNING, "dirty holodeck section"); |
131 |
|
} else { /* assume we're creating it */ |
132 |
|
if ((hp = hdalloc(hproto)) == NULL) |
133 |
|
goto memerr; |
145 |
|
hdattach(fd); |
146 |
|
/* check rays on disk */ |
147 |
|
fpos = hdfilen(fd); |
148 |
< |
biglob(hp)->nrd = 0; |
148 |
> |
biglob(hp)->nrd = rtrunc = 0; |
149 |
|
for (n = hproto == NULL ? nbeams(hp) : 0; n > 0; n--) |
150 |
|
if (hp->bi[n].nrd) |
151 |
< |
if (hp->bi[n].fo + hp->bi[n].nrd > fpos) |
152 |
< |
hp->bi[n].nrd = 0; /* off end */ |
153 |
< |
else |
151 |
> |
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 |
|
biglob(hp)->nrd += hp->bi[n].nrd; |
156 |
+ |
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 |
|
/* add to holodeck list */ |
162 |
|
for (n = 0; n < HDMAX; n++) |
163 |
|
if (hdlist[n] == NULL) { |
185 |
|
return(n); |
186 |
|
} |
187 |
|
/* sync the beams */ |
188 |
< |
for (j = all ? nbeams(hp) : 0; j > 0; j--) |
188 |
> |
for (j = (all ? nbeams(hp) : 0); j > 0; j--) |
189 |
|
if (hp->bl[j] != NULL) |
190 |
|
hdsyncbeam(hp, j); |
191 |
|
if (!hp->dirty) /* directory clean? */ |
201 |
|
} |
202 |
|
|
203 |
|
|
204 |
< |
long |
204 |
> |
unsigned |
205 |
|
hdmemuse(all) /* return memory usage (in bytes) */ |
206 |
|
int all; /* include overhead (painful) */ |
207 |
|
{ |
220 |
|
} |
221 |
|
} |
222 |
|
if (all) |
223 |
< |
for (j = 0; j < nhdfrags; j++) { |
224 |
< |
total += sizeof(struct fragment); |
225 |
< |
if (hdfrag[j].nfrags) |
223 |
> |
for (j = 0; j < nhdfragls; j++) { |
224 |
> |
total += sizeof(struct fraglist); |
225 |
> |
if (hdfragl[j].nfrags) |
226 |
|
total += FRAGBLK*sizeof(BEAMI) * |
227 |
< |
((hdfrag[j].nfrags-1)/FRAGBLK + 1) ; |
227 |
> |
((hdfragl[j].nfrags-1)/FRAGBLK + 1) ; |
228 |
|
} |
229 |
|
return(total); |
230 |
|
} |
238 |
|
|
239 |
|
if (fd < 0) |
240 |
|
return(-1); |
241 |
< |
if (fd >= nhdfrags || !hdfrag[fd].nlinks) { |
241 |
> |
if (fd >= nhdfragls || !hdfragl[fd].nlinks) { |
242 |
|
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 |
< |
return(hdfrag[fd].flen); |
248 |
> |
return(hdfragl[fd].flen); |
249 |
|
} |
250 |
|
|
251 |
|
|
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 |
< |
hp->bl[i]->tick = ++hdclock; /* update LRU clock */ |
312 |
< |
blglob(hp)->tick = hdclock; |
319 |
> |
blglob(hp)->tick = hp->bl[i]->tick = hdclock++; /* update LRU clock */ |
320 |
|
return(p); /* point to new rays */ |
321 |
|
memerr: |
322 |
|
error(SYSTEM, "out of memory in hdnewrays"); |
347 |
|
if (read(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) |
348 |
|
error(SYSTEM, "error reading beam from holodeck file"); |
349 |
|
} |
350 |
< |
hp->bl[i]->tick = ++hdclock; /* update LRU clock */ |
344 |
< |
blglob(hp)->tick = hdclock; |
350 |
> |
blglob(hp)->tick = hp->bl[i]->tick = hdclock++; /* update LRU clock */ |
351 |
|
return(hp->bl[i]); |
352 |
|
} |
353 |
|
|
354 |
|
|
355 |
|
int |
356 |
+ |
hdfilord(hb1, hb2) /* order beams for quick loading */ |
357 |
+ |
register HDBEAMI *hb1, *hb2; |
358 |
+ |
{ |
359 |
+ |
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 |
+ |
if ((c = hb1->h->fd - hb2->h->fd)) |
367 |
+ |
return(c); |
368 |
+ |
/* then by position in file */ |
369 |
+ |
c = hb1->h->bi[hb1->b].fo - hb2->h->bi[hb2->b].fo; |
370 |
+ |
return(c > 0 ? 1 : c < 0 ? -1 : 0); |
371 |
+ |
} |
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 |
+ |
int bytesloaded, needbytes, bytes2free; |
381 |
+ |
register BEAM *bp; |
382 |
+ |
register int i; |
383 |
+ |
/* precheck consistency */ |
384 |
+ |
if (n <= 0) return; |
385 |
+ |
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 |
+ |
bytesloaded = 0; /* run through loaded beams */ |
391 |
+ |
for ( ; n && (bp = hb->h->bl[hb->b]) != NULL; n--, hb++) { |
392 |
+ |
bp->tick = hdclock; /* preempt swap */ |
393 |
+ |
bytesloaded += bp->nrm; |
394 |
+ |
if (bf != NULL) |
395 |
+ |
(*bf)(bp, hb); |
396 |
+ |
} |
397 |
+ |
bytesloaded *= sizeof(RAYVAL); |
398 |
+ |
if ((origcachesize = hdcachesize) > 0) { |
399 |
+ |
needbytes = 0; /* figure out memory needs */ |
400 |
+ |
for (i = n; i--; ) |
401 |
+ |
needbytes += hb[i].h->bi[hb[i].b].nrd; |
402 |
+ |
needbytes *= sizeof(RAYVAL); |
403 |
+ |
do { /* free enough memory */ |
404 |
+ |
memuse = hdmemuse(0); |
405 |
+ |
bytes2free = needbytes - (int)(hdcachesize-memuse); |
406 |
+ |
if (bytes2free > (int)(memuse - bytesloaded)) |
407 |
+ |
bytes2free = memuse - bytesloaded; |
408 |
+ |
} while (bytes2free > 0 && |
409 |
+ |
hdfreecache(100*bytes2free/memuse, NULL) < 0); |
410 |
+ |
hdcachesize = 0; /* load beams w/o swap */ |
411 |
+ |
} |
412 |
+ |
for (i = 0; i < n; i++) |
413 |
+ |
if ((bp = hdgetbeam(hb[i].h, hb[i].b)) != NULL && bf != NULL) |
414 |
+ |
(*bf)(bp, hb+i); |
415 |
+ |
hdcachesize = origcachesize; /* resume dynamic swapping */ |
416 |
+ |
} |
417 |
+ |
|
418 |
+ |
|
419 |
+ |
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 |
+ |
int |
519 |
|
hdsyncbeam(hp, i) /* sync beam in memory with beam on disk */ |
520 |
|
register HOLO *hp; |
521 |
|
register int i; |
522 |
|
{ |
523 |
< |
unsigned int nrays; |
355 |
< |
long nfo; |
523 |
> |
unsigned int4 nrays; |
524 |
|
unsigned int n; |
525 |
+ |
long nfo; |
526 |
|
/* check file status */ |
527 |
< |
if (hdfrag[hp->fd].writerr) |
527 |
> |
if (hdfragl[hp->fd].writerr) |
528 |
|
return(-1); |
529 |
|
#ifdef DEBUG |
530 |
|
if (i < 1 | i > nbeams(hp)) |
533 |
|
/* is current fragment OK? */ |
534 |
|
if (hp->bl[i] == NULL || (nrays = hp->bl[i]->nrm) == hp->bi[i].nrd) |
535 |
|
return(0); |
536 |
< |
/* locate fragment */ |
537 |
< |
if (hp->fd >= nhdfrags || !hdfrag[hp->fd].nlinks) /* untracked */ |
538 |
< |
hp->bi[i].fo = lseek(hp->fd, 0L, 2); |
539 |
< |
|
371 |
< |
else if (hp->bi[i].fo + hp->bi[i].nrd*sizeof(RAYVAL) == |
372 |
< |
hdfrag[hp->fd].flen) /* EOF special case */ |
373 |
< |
hdfrag[hp->fd].flen = (nfo=hp->bi[i].fo) + nrays*sizeof(RAYVAL); |
374 |
< |
|
375 |
< |
else { /* general case */ |
376 |
< |
register struct fragment *f = &hdfrag[hp->fd]; |
377 |
< |
register int j, k; |
378 |
< |
/* relinquish old fragment */ |
379 |
< |
if (hp->bi[i].nrd) { |
380 |
< |
j = f->nfrags++; |
381 |
< |
#ifdef MAXFRAG |
382 |
< |
if (j >= MAXFRAG-1) |
383 |
< |
f->nfrags--; |
384 |
< |
#endif |
385 |
< |
if (j % FRAGBLK == 0) { /* more frag. space */ |
386 |
< |
if (f->fi == NULL) |
387 |
< |
f->fi = (BEAMI *)malloc( |
388 |
< |
FRAGBLK*sizeof(BEAMI)); |
389 |
< |
else |
390 |
< |
f->fi = (BEAMI *)realloc((char *)f->fi, |
391 |
< |
(j+FRAGBLK)*sizeof(BEAMI)); |
392 |
< |
if (f->fi == NULL) |
393 |
< |
error(SYSTEM, |
394 |
< |
"out of memory in hdsyncbeam"); |
395 |
< |
} |
396 |
< |
for ( ; ; j--) { /* insert in descending list */ |
397 |
< |
if (!j || hp->bi[i].fo < f->fi[j-1].fo) { |
398 |
< |
f->fi[j].fo = hp->bi[i].fo; |
399 |
< |
f->fi[j].nrd = hp->bi[i].nrd; |
400 |
< |
break; |
401 |
< |
} |
402 |
< |
copystruct(f->fi+j, f->fi+j-1); |
403 |
< |
} |
404 |
< |
/* coalesce adjacent fragments */ |
405 |
< |
for (j = k = 0; k < f->nfrags; j++, k++) { |
406 |
< |
if (k > j) |
407 |
< |
copystruct(f->fi+j, f->fi+k); |
408 |
< |
while (k+1 < f->nfrags && f->fi[k+1].fo + |
409 |
< |
f->fi[k+1].nrd*sizeof(RAYVAL) |
410 |
< |
== f->fi[j].fo) { |
411 |
< |
f->fi[j].fo -= |
412 |
< |
f->fi[++k].nrd*sizeof(RAYVAL); |
413 |
< |
f->fi[j].nrd += f->fi[k].nrd; |
414 |
< |
} |
415 |
< |
} |
416 |
< |
f->nfrags = j; |
417 |
< |
} |
418 |
< |
k = -1; /* find closest-sized fragment */ |
419 |
< |
for (j = nrays ? f->nfrags : 0; j-- > 0; ) |
420 |
< |
if (f->fi[j].nrd >= nrays && |
421 |
< |
(k < 0 || f->fi[j].nrd < f->fi[k].nrd)) |
422 |
< |
if (f->fi[k=j].nrd == nrays) |
423 |
< |
break; |
424 |
< |
if (k < 0) { /* no fragment -- extend file */ |
425 |
< |
nfo = f->flen; |
426 |
< |
f->flen += nrays*sizeof(RAYVAL); |
427 |
< |
} else { /* else use fragment */ |
428 |
< |
nfo = f->fi[k].fo; |
429 |
< |
if (f->fi[k].nrd == nrays) { /* delete fragment */ |
430 |
< |
f->nfrags--; |
431 |
< |
for (j = k; j < f->nfrags; j++) |
432 |
< |
copystruct(f->fi+j, f->fi+j+1); |
433 |
< |
} else { /* else shrink it */ |
434 |
< |
f->fi[k].fo += nrays*sizeof(RAYVAL); |
435 |
< |
f->fi[k].nrd -= nrays; |
436 |
< |
} |
437 |
< |
} |
438 |
< |
} |
439 |
< |
if (nrays) { /* write the new fragment */ |
536 |
> |
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 |
|
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 |
< |
hdfrag[hp->fd].writerr++; |
546 |
< |
hdsync(hp, 0); /* sync directory */ |
545 |
> |
hdfragl[hp->fd].writerr++; |
546 |
> |
hdsync(NULL, 0); /* sync directories */ |
547 |
|
error(SYSTEM, "write error in hdsyncbeam"); |
548 |
|
} |
549 |
< |
} |
549 |
> |
hp->bi[i].fo = nfo; |
550 |
> |
} else |
551 |
> |
hp->bi[i].fo = 0L; |
552 |
|
biglob(hp)->nrd += nrays - hp->bi[i].nrd; |
553 |
|
hp->bi[i].nrd = nrays; |
554 |
< |
hp->bi[i].fo = nfo; |
453 |
< |
markdirty(hp); /* section directory now out of date */ |
554 |
> |
markdirty(hp); /* section directory now out of date */ |
555 |
|
return(1); |
556 |
|
} |
557 |
|
|
569 |
|
nchanged += hdfreebeam(hdlist[i], 0); |
570 |
|
return(nchanged); |
571 |
|
} |
572 |
< |
if (hdfrag[hp->fd].writerr) /* check for file error */ |
572 |
> |
if (hdfragl[hp->fd].writerr) /* check for file error */ |
573 |
|
return(0); |
574 |
|
if (i == 0) { /* clear entire holodeck */ |
575 |
|
nchanged = 0; |
638 |
|
} |
639 |
|
|
640 |
|
|
641 |
< |
hdlrulist(ha, ba, n, hp) /* add beams from holodeck to LRU list */ |
642 |
< |
register HOLO *ha[]; /* section list (NULL terminated) */ |
643 |
< |
register int ba[]; /* beam index to go with section */ |
644 |
< |
int n; /* length of arrays minus 1 */ |
641 |
> |
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 |
|
register HOLO *hp; /* section we're adding from */ |
647 |
|
{ |
648 |
|
register int i, j; |
547 |
– |
int nents; |
548 |
– |
/* find last entry in LRU list */ |
549 |
– |
for (j = 0; ha[j] != NULL; j++) |
550 |
– |
; |
551 |
– |
nents = j; |
649 |
|
/* insert each beam from hp */ |
650 |
< |
for (i = nbeams(hp); i > 0; i--) { |
650 |
> |
for (i = 1; i <= nbeams(hp); i++) { |
651 |
|
if (hp->bl[i] == NULL) /* check if loaded */ |
652 |
|
continue; |
653 |
< |
if ((j = ++nents) > n) /* grow list if we can */ |
653 |
> |
#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 |
|
nents--; |
659 |
|
for ( ; ; ) { /* bubble into place */ |
660 |
|
if (!--j || hp->bl[i]->tick >= |
661 |
< |
ha[j-1]->bl[ba[j-1]]->tick) { |
662 |
< |
ha[j] = hp; |
663 |
< |
ba[j] = i; |
661 |
> |
hb[j-1].h->bl[hb[j-1].b]->tick) { |
662 |
> |
hb[j].h = hp; |
663 |
> |
hb[j].b = i; |
664 |
|
break; |
665 |
|
} |
666 |
< |
ha[j] = ha[j-1]; |
566 |
< |
ba[j] = ba[j-1]; |
666 |
> |
copystruct(hb+j, hb+(j-1)); |
667 |
|
} |
668 |
|
} |
669 |
< |
ha[nents] = NULL; /* all done */ |
570 |
< |
ba[nents] = 0; |
669 |
> |
return(nents); /* return new list length */ |
670 |
|
} |
671 |
|
|
672 |
|
|
673 |
+ |
int |
674 |
|
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 |
< |
HOLO *hp[FREEBEAMS+1]; |
579 |
< |
int bn[FREEBEAMS+1]; |
678 |
> |
HDBEAMI hb[FREEBEAMS]; |
679 |
|
int freetarget; |
680 |
+ |
int n; |
681 |
|
register int i; |
682 |
+ |
#ifdef DEBUG |
683 |
+ |
unsigned membefore; |
684 |
+ |
|
685 |
+ |
membefore = hdmemuse(0); |
686 |
+ |
#endif |
687 |
|
/* compute free target */ |
688 |
|
freetarget = (honly != NULL) ? blglob(honly)->nrm : |
689 |
|
hdmemuse(0)/sizeof(RAYVAL) ; |
690 |
|
freetarget = freetarget*pct/100; |
691 |
+ |
if (freetarget <= 0) |
692 |
+ |
return(0); |
693 |
|
/* find least recently used */ |
694 |
< |
hp[0] = NULL; |
588 |
< |
bn[0] = 0; |
694 |
> |
n = 0; |
695 |
|
if (honly != NULL) |
696 |
< |
hdlrulist(hp, bn, FREEBEAMS, honly); |
696 |
> |
n = hdlrulist(hb, n, FREEBEAMS, honly); |
697 |
|
else |
698 |
|
for (i = 0; hdlist[i] != NULL; i++) |
699 |
< |
hdlrulist(hp, bn, FREEBEAMS, hdlist[i]); |
699 |
> |
n = hdlrulist(hb, n, FREEBEAMS, hdlist[i]); |
700 |
|
/* free LRU beams */ |
701 |
< |
for (i = 0; hp[i] != NULL; i++) { |
702 |
< |
hdfreebeam(hp[i], bn[i]); |
703 |
< |
if ((freetarget -= hp[i]->bi[bn[i]].nrd) <= 0) |
701 |
> |
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 |
|
break; |
705 |
|
} |
706 |
|
hdsync(honly, 0); /* synchronize directories as necessary */ |
707 |
+ |
#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 |
+ |
return(-freetarget); /* return how far past goal we went */ |
714 |
|
} |
715 |
|
|
716 |
|
|
722 |
|
if (hp == NULL) { /* NULL means clean up everything */ |
723 |
|
while (hdlist[0] != NULL) |
724 |
|
hddone(hdlist[0]); |
725 |
+ |
free((char *)hdfragl); |
726 |
+ |
hdfragl = NULL; nhdfragls = 0; |
727 |
|
return; |
728 |
|
} |
729 |
|
/* flush all data and free memory */ |