31 |
|
|
32 |
|
#define FRAGBLK 64 /* number of fragments to allocate at a time */ |
33 |
|
|
34 |
< |
int hdcachesize = CACHESIZE*1024*1024; /* target cache size (bytes) */ |
34 |
> |
unsigned hdcachesize = CACHESIZE*1024*1024; /* target cache size */ |
35 |
|
unsigned long hdclock; /* clock value */ |
36 |
|
|
37 |
|
HOLO *hdlist[HDMAX+1]; /* holodeck pointers (NULL term.) */ |
193 |
|
} |
194 |
|
|
195 |
|
|
196 |
< |
long |
196 |
> |
unsigned |
197 |
|
hdmemuse(all) /* return memory usage (in bytes) */ |
198 |
|
int all; /* include overhead (painful) */ |
199 |
|
{ |
308 |
|
p = hdbray(hp->bl[i]) + hp->bl[i]->nrm; |
309 |
|
hp->bl[i]->nrm += nr; /* update in-core structure */ |
310 |
|
bzero((char *)p, nr*sizeof(RAYVAL)); |
311 |
< |
hp->bl[i]->tick = ++hdclock; /* update LRU clock */ |
312 |
< |
blglob(hp)->tick = hdclock; |
311 |
> |
hp->bl[i]->tick = hdclock; /* update LRU clock */ |
312 |
> |
blglob(hp)->tick = hdclock++; |
313 |
|
return(p); /* point to new rays */ |
314 |
|
memerr: |
315 |
|
error(SYSTEM, "out of memory in hdnewrays"); |
340 |
|
if (read(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) |
341 |
|
error(SYSTEM, "error reading beam from holodeck file"); |
342 |
|
} |
343 |
< |
hp->bl[i]->tick = ++hdclock; /* update LRU clock */ |
344 |
< |
blglob(hp)->tick = hdclock; |
343 |
> |
hp->bl[i]->tick = hdclock; /* update LRU clock */ |
344 |
> |
blglob(hp)->tick = hdclock++; |
345 |
|
return(hp->bl[i]); |
346 |
|
} |
347 |
|
|
348 |
|
|
349 |
|
int |
350 |
+ |
hdfilord(hb1, hb2) /* order beams for optimal loading */ |
351 |
+ |
register HDBEAMI *hb1, *hb2; |
352 |
+ |
{ |
353 |
+ |
register int c; |
354 |
+ |
/* sort by file descriptor first */ |
355 |
+ |
if ((c = hb1->h->fd - hb2->h->fd)) |
356 |
+ |
return(c); |
357 |
+ |
/* then by position in file */ |
358 |
+ |
return(hb1->h->bi[hb1->b].fo - hb2->h->bi[hb2->b].fo); |
359 |
+ |
} |
360 |
+ |
|
361 |
+ |
|
362 |
+ |
hdloadbeams(hb, n, bf) /* load a list of beams in optimal order */ |
363 |
+ |
register HDBEAMI *hb; /* list gets sorted by hdfilord() */ |
364 |
+ |
int n; /* list length */ |
365 |
+ |
int (*bf)(); /* callback function (optional) */ |
366 |
+ |
{ |
367 |
+ |
unsigned origcachesize, memuse; |
368 |
+ |
register BEAM *bp; |
369 |
+ |
int bytesloaded, needbytes, bytes2free; |
370 |
+ |
register int i; |
371 |
+ |
/* precheck consistency */ |
372 |
+ |
for (i = n; i--; ) |
373 |
+ |
if (hb[i].h == NULL || hb[i].b < 1 | hb[i].b > nbeams(hb[i].h)) |
374 |
+ |
error(CONSISTENCY, "bad beam in hdloadbeams"); |
375 |
+ |
/* sort list for optimal access */ |
376 |
+ |
qsort((char *)hb, n, sizeof(HDBEAMI), hdfilord); |
377 |
+ |
if ((origcachesize = hdcachesize) == 0) |
378 |
+ |
goto loadbeams; |
379 |
+ |
bytesloaded = needbytes = 0; /* figure out memory needs */ |
380 |
+ |
for (i = 0; i < n; i++) |
381 |
+ |
if ((bp = hb[i].h->bl[hb[i].b]) != NULL) { |
382 |
+ |
bp->tick = hdclock; /* preempt swap */ |
383 |
+ |
bytesloaded += bp->nrm; |
384 |
+ |
} else /* prepare to load */ |
385 |
+ |
needbytes += hb[i].h->bi[hb[i].b].nrd; |
386 |
+ |
bytesloaded *= sizeof(RAYVAL); |
387 |
+ |
needbytes *= sizeof(RAYVAL); |
388 |
+ |
do { /* free enough memory */ |
389 |
+ |
memuse = hdmemuse(0); |
390 |
+ |
bytes2free = needbytes - (signed)(hdcachesize-memuse); |
391 |
+ |
if (bytes2free > (signed)(memuse - bytesloaded)) |
392 |
+ |
bytes2free = memuse - bytesloaded; |
393 |
+ |
} while (bytes2free > 0 && |
394 |
+ |
hdfreecache(100*bytes2free/memuse, NULL) < 0); |
395 |
+ |
loadbeams: |
396 |
+ |
hdcachesize = 0; /* load the ordered beams w/o swap */ |
397 |
+ |
for (i = 0; i < n; i++) |
398 |
+ |
if ((bp = hdgetbeam(hb[i].h, hb[i].b)) != NULL && bf != NULL) |
399 |
+ |
(*bf)(bp, hb[i].h, hb[i].b); |
400 |
+ |
hdcachesize = origcachesize; /* resume dynamic swapping */ |
401 |
+ |
} |
402 |
+ |
|
403 |
+ |
|
404 |
+ |
int |
405 |
|
hdsyncbeam(hp, i) /* sync beam in memory with beam on disk */ |
406 |
|
register HOLO *hp; |
407 |
|
register int i; |
454 |
|
f->fi[j].nrd = hp->bi[i].nrd; |
455 |
|
break; |
456 |
|
} |
457 |
< |
copystruct(f->fi+j, f->fi+j-1); |
457 |
> |
copystruct(f->fi+j, f->fi+(j-1)); |
458 |
|
} |
459 |
|
/* coalesce adjacent fragments */ |
460 |
|
for (j = k = 0; k < f->nfrags; j++, k++) { |
484 |
|
if (f->fi[k].nrd == nrays) { /* delete fragment */ |
485 |
|
f->nfrags--; |
486 |
|
for (j = k; j < f->nfrags; j++) |
487 |
< |
copystruct(f->fi+j, f->fi+j+1); |
487 |
> |
copystruct(f->fi+j, f->fi+(j+1)); |
488 |
|
} else { /* else shrink it */ |
489 |
|
f->fi[k].fo += nrays*sizeof(RAYVAL); |
490 |
|
f->fi[k].nrd -= nrays; |
592 |
|
} |
593 |
|
|
594 |
|
|
595 |
< |
hdlrulist(ha, ba, n, hp) /* add beams from holodeck to LRU list */ |
596 |
< |
register HOLO *ha[]; /* section list (NULL terminated) */ |
597 |
< |
register int ba[]; /* beam index to go with section */ |
598 |
< |
int n; /* length of arrays minus 1 */ |
595 |
> |
int |
596 |
> |
hdlrulist(hb, nents, n, hp) /* add beams from holodeck to LRU list */ |
597 |
> |
register HDBEAMI *hb; /* beam list */ |
598 |
> |
int nents; /* current list length */ |
599 |
> |
int n; /* maximum list length */ |
600 |
|
register HOLO *hp; /* section we're adding from */ |
601 |
|
{ |
602 |
|
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; |
603 |
|
/* insert each beam from hp */ |
604 |
|
for (i = nbeams(hp); i > 0; i--) { |
605 |
|
if (hp->bl[i] == NULL) /* check if loaded */ |
606 |
|
continue; |
607 |
< |
if ((j = ++nents) > n) /* grow list if we can */ |
607 |
> |
#if 0 |
608 |
> |
if (hp->bl[i]->tick == hdclock) /* preempt swap? */ |
609 |
> |
continue; |
610 |
> |
#endif |
611 |
> |
if ((j = ++nents) >= n) /* grow list if we can */ |
612 |
|
nents--; |
613 |
|
for ( ; ; ) { /* bubble into place */ |
614 |
|
if (!--j || hp->bl[i]->tick >= |
615 |
< |
ha[j-1]->bl[ba[j-1]]->tick) { |
616 |
< |
ha[j] = hp; |
617 |
< |
ba[j] = i; |
615 |
> |
hb[j-1].h->bl[hb[j-1].b]->tick) { |
616 |
> |
hb[j].h = hp; |
617 |
> |
hb[j].b = i; |
618 |
|
break; |
619 |
|
} |
620 |
< |
ha[j] = ha[j-1]; |
566 |
< |
ba[j] = ba[j-1]; |
620 |
> |
copystruct(hb+j, hb+(j-1)); |
621 |
|
} |
622 |
|
} |
623 |
< |
ha[nents] = NULL; /* all done */ |
570 |
< |
ba[nents] = 0; |
623 |
> |
return(nents); /* return new list length */ |
624 |
|
} |
625 |
|
|
626 |
|
|
627 |
+ |
int |
628 |
|
hdfreecache(pct, honly) /* free up cache space, writing changes */ |
629 |
|
int pct; /* maximum percentage to free */ |
630 |
|
register HOLO *honly; /* NULL means check all */ |
631 |
|
{ |
632 |
< |
HOLO *hp[FREEBEAMS+1]; |
579 |
< |
int bn[FREEBEAMS+1]; |
632 |
> |
HDBEAMI hb[FREEBEAMS]; |
633 |
|
int freetarget; |
634 |
+ |
int n; |
635 |
|
register int i; |
636 |
|
/* compute free target */ |
637 |
|
freetarget = (honly != NULL) ? blglob(honly)->nrm : |
638 |
|
hdmemuse(0)/sizeof(RAYVAL) ; |
639 |
|
freetarget = freetarget*pct/100; |
640 |
+ |
if (freetarget <= 0) |
641 |
+ |
return(0); |
642 |
|
/* find least recently used */ |
643 |
< |
hp[0] = NULL; |
588 |
< |
bn[0] = 0; |
643 |
> |
n = 0; |
644 |
|
if (honly != NULL) |
645 |
< |
hdlrulist(hp, bn, FREEBEAMS, honly); |
645 |
> |
n = hdlrulist(hb, n, FREEBEAMS, honly); |
646 |
|
else |
647 |
|
for (i = 0; hdlist[i] != NULL; i++) |
648 |
< |
hdlrulist(hp, bn, FREEBEAMS, hdlist[i]); |
648 |
> |
n = hdlrulist(hb, n, FREEBEAMS, hdlist[i]); |
649 |
|
/* free LRU beams */ |
650 |
< |
for (i = 0; hp[i] != NULL; i++) { |
651 |
< |
hdfreebeam(hp[i], bn[i]); |
652 |
< |
if ((freetarget -= hp[i]->bi[bn[i]].nrd) <= 0) |
650 |
> |
for (i = 0; i < n; i++) { |
651 |
> |
hdfreebeam(hb[i].h, hb[i].b); |
652 |
> |
if ((freetarget -= hb[i].h->bi[hb[i].b].nrd) <= 0) |
653 |
|
break; |
654 |
|
} |
655 |
|
hdsync(honly, 0); /* synchronize directories as necessary */ |
656 |
+ |
return(-freetarget); /* return how far past goal we went */ |
657 |
|
} |
658 |
|
|
659 |
|
|