1 |
< |
/* Copyright (c) 1998 Silicon Graphics, Inc. */ |
1 |
> |
/* Copyright (c) 1999 Silicon Graphics, Inc. */ |
2 |
|
|
3 |
|
#ifndef lint |
4 |
|
static char SCCSid[] = "$SunId$ SGI"; |
13 |
|
#include "holo.h" |
14 |
|
|
15 |
|
#ifndef CACHESIZE |
16 |
< |
#define CACHESIZE 16 /* default cache size (Mbytes, 0==inf) */ |
16 |
> |
#ifdef BIGMEM |
17 |
> |
#define CACHESIZE 32 /* default cache size (Mbytes, 0==inf) */ |
18 |
> |
#else |
19 |
> |
#define CACHESIZE 12 |
20 |
|
#endif |
21 |
+ |
#endif |
22 |
|
#ifndef FREEBEAMS |
23 |
|
#define FREEBEAMS 1500 /* maximum beams to free at a time */ |
24 |
|
#endif |
28 |
|
#ifndef MAXFRAGB |
29 |
|
#define MAXFRAGB 16 /* fragment blocks/file to track (0==inf) */ |
30 |
|
#endif |
31 |
+ |
#ifndef FF_DEFAULT |
32 |
+ |
/* when to free a beam fragment */ |
33 |
+ |
#define FF_DEFAULT (FF_ALLOC|FF_WRITE|FF_KILL) |
34 |
+ |
#endif |
35 |
|
#ifndef MINDIRSEL |
36 |
|
/* minimum directory seek length */ |
37 |
|
#define MINDIRSEL (4*BUFSIZ/sizeof(BEAMI)) |
44 |
|
|
45 |
|
#define FRAGBLK 512 /* number of fragments to allocate at a time */ |
46 |
|
|
47 |
+ |
int hdfragflags = FF_DEFAULT; /* tells when to free fragments */ |
48 |
|
unsigned hdcachesize = CACHESIZE*1024*1024; /* target cache size */ |
49 |
< |
unsigned long hdclock; /* clock value */ |
49 |
> |
unsigned long hdclock; /* clock value */ |
50 |
|
|
51 |
|
HOLO *hdlist[HDMAX+1]; /* holodeck pointers (NULL term.) */ |
52 |
|
|
358 |
|
RAYVAL *p; |
359 |
|
int n; |
360 |
|
|
361 |
< |
if (nr <= 0) |
362 |
< |
return(NULL); |
363 |
< |
if (i < 1 | i > nbeams(hp)) |
355 |
< |
error(CONSISTENCY, "bad beam index given to hdnewrays"); |
361 |
> |
if (nr <= 0) return(NULL); |
362 |
> |
CHECK(i < 1 | i > nbeams(hp), |
363 |
> |
CONSISTENCY, "bad beam index given to hdnewrays"); |
364 |
|
if (hp->bl[i] != NULL) |
365 |
|
hp->bl[i]->tick = hdclock; /* preempt swap */ |
366 |
|
if (hdcachesize > 0 && hdmemuse(0) >= hdcachesize) |
369 |
|
n = hp->bi[i].nrd + nr; |
370 |
|
hp->bl[i] = (BEAM *)hdrealloc(NULL, hdbsiz(n), "hdnewrays"); |
371 |
|
blglob(hp)->nrm += n; |
372 |
< |
if (n = hp->bl[i]->nrm = hp->bi[i].nrd) { |
372 |
> |
if ((n = hp->bl[i]->nrm = hp->bi[i].nrd)) { |
373 |
|
errno = 0; |
374 |
|
if (lseek(hp->fd, hp->bi[i].fo, 0) < 0) |
375 |
|
error(SYSTEM, "seek error on holodeck file"); |
383 |
|
hdbsiz(hp->bl[i]->nrm + nr), "hdnewrays"); |
384 |
|
blglob(hp)->nrm += nr; |
385 |
|
} |
386 |
+ |
if (hdfragflags&FF_ALLOC && hp->bi[i].nrd) |
387 |
+ |
hdfreefrag(hp, i); /* relinquish old fragment */ |
388 |
|
p = hdbray(hp->bl[i]) + hp->bl[i]->nrm; |
389 |
|
hp->bl[i]->nrm += nr; /* update in-core structure */ |
390 |
|
bzero((char *)p, nr*sizeof(RAYVAL)); |
400 |
|
{ |
401 |
|
register int n; |
402 |
|
|
403 |
< |
if (i < 1 | i > nbeams(hp)) |
404 |
< |
error(CONSISTENCY, "bad beam index given to hdgetbeam"); |
403 |
> |
CHECK(i < 1 | i > nbeams(hp), |
404 |
> |
CONSISTENCY, "bad beam index given to hdgetbeam"); |
405 |
|
if (hp->bl[i] == NULL) { /* load from disk */ |
406 |
|
if (!(n = hp->bi[i].nrd)) |
407 |
|
return(NULL); |
415 |
|
n *= sizeof(RAYVAL); |
416 |
|
if (read(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) |
417 |
|
error(SYSTEM, "error reading beam from holodeck file"); |
418 |
+ |
if (hdfragflags&FF_READ) |
419 |
+ |
hdfreefrag(hp, i); /* relinquish old frag. */ |
420 |
|
} |
421 |
|
blglob(hp)->tick = hp->bl[i]->tick = hdclock++; /* update LRU clock */ |
422 |
|
return(hp->bl[i]); |
454 |
|
/* precheck consistency */ |
455 |
|
if (n <= 0) return; |
456 |
|
for (i = n; i--; ) |
457 |
< |
if (hb[i].h == NULL || hb[i].b < 1 | hb[i].b > nbeams(hb[i].h)) |
457 |
> |
if (hb[i].h==NULL || hb[i].b<1 | hb[i].b>nbeams(hb[i].h)) |
458 |
|
error(CONSISTENCY, "bad beam in hdloadbeams"); |
459 |
|
/* sort list for optimal access */ |
460 |
|
qsort((char *)hb, n, sizeof(HDBEAMI), hdfilord); |
487 |
|
} |
488 |
|
|
489 |
|
|
490 |
< |
hdfreefrag(fd, bi) /* free a file fragment */ |
491 |
< |
int fd; |
492 |
< |
register BEAMI *bi; |
490 |
> |
int |
491 |
> |
hdfreefrag(hp, i) /* free a file fragment */ |
492 |
> |
HOLO *hp; |
493 |
> |
int i; |
494 |
|
{ |
495 |
+ |
register BEAMI *bi = &hp->bi[i]; |
496 |
|
register struct fraglist *f; |
497 |
|
register int j, k; |
498 |
|
|
499 |
< |
if (bi->nrd == 0) |
500 |
< |
return; |
501 |
< |
#ifdef DEBUG |
502 |
< |
if (fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks) |
503 |
< |
error(CONSISTENCY, "bad file descriptor in hdfreefrag"); |
490 |
< |
#endif |
491 |
< |
f = &hdfragl[fd]; |
499 |
> |
if (bi->nrd <= 0) |
500 |
> |
return(0); |
501 |
> |
DCHECK(hp->fd < 0 | hp->fd >= nhdfragls || !hdfragl[hp->fd].nlinks, |
502 |
> |
CONSISTENCY, "bad file descriptor in hdfreefrag"); |
503 |
> |
f = &hdfragl[hp->fd]; |
504 |
|
if (f->nfrags % FRAGBLK == 0) { /* delete empty remnants */ |
505 |
|
for (j = k = 0; k < f->nfrags; j++, k++) { |
506 |
|
while (f->fi[k].nrd == 0) |
517 |
|
if (j >= MAXFRAGB*FRAGBLK) { |
518 |
|
f->nfrags = j--; /* stop list growth */ |
519 |
|
if (bi->nrd <= f->fi[j].nrd) |
520 |
< |
return; /* new one no better than discard */ |
520 |
> |
return(0); /* new one no better than discard */ |
521 |
|
} |
522 |
|
#endif |
523 |
< |
if (j % FRAGBLK == 0) { /* more free list space */ |
523 |
> |
if (j % FRAGBLK == 0) { /* more (or less) free list space */ |
524 |
|
register BEAMI *newp; |
525 |
|
if (f->fi == NULL) |
526 |
|
newp = (BEAMI *)malloc((j+FRAGBLK)*sizeof(BEAMI)); |
529 |
|
(j+FRAGBLK)*sizeof(BEAMI)); |
530 |
|
if (newp == NULL) { |
531 |
|
f->nfrags--; /* graceful failure */ |
532 |
< |
return; |
532 |
> |
return(0); |
533 |
|
} |
534 |
|
f->fi = newp; |
535 |
|
} |
556 |
|
} |
557 |
|
break; |
558 |
|
} |
559 |
+ |
biglob(hp)->nrd -= bi->nrd; /* tell fragment it's free */ |
560 |
+ |
bi->nrd = 0; |
561 |
+ |
bi->fo = 0L; |
562 |
+ |
return(1); |
563 |
|
} |
564 |
|
|
565 |
|
|
566 |
+ |
int |
567 |
+ |
hdfragOK(fd, listlen, listsiz) /* get fragment list status for file */ |
568 |
+ |
int fd; |
569 |
+ |
int *listlen; |
570 |
+ |
register int4 *listsiz; |
571 |
+ |
{ |
572 |
+ |
register struct fraglist *f; |
573 |
+ |
register int i; |
574 |
+ |
|
575 |
+ |
if (fd < 0 | fd >= nhdfragls || !(f = &hdfragl[fd])->nlinks) |
576 |
+ |
return(0); /* listless */ |
577 |
+ |
if (listlen != NULL) |
578 |
+ |
*listlen = f->nfrags; |
579 |
+ |
if (listsiz != NULL) |
580 |
+ |
for (i = f->nfrags, *listsiz = 0; i--; ) |
581 |
+ |
*listsiz += f->fi[i].nrd; |
582 |
+ |
#if MAXFRAGB |
583 |
+ |
return(f->nfrags < MAXFRAGB*FRAGBLK); |
584 |
+ |
#else |
585 |
+ |
return(1); /* list never fills up */ |
586 |
+ |
#endif |
587 |
+ |
} |
588 |
+ |
|
589 |
+ |
|
590 |
|
long |
591 |
|
hdallocfrag(fd, nrays) /* allocate a file fragment */ |
592 |
|
int fd; |
593 |
|
unsigned int4 nrays; |
594 |
|
{ |
595 |
|
register struct fraglist *f; |
596 |
< |
register int j, k; |
596 |
> |
register int j; |
597 |
|
long nfo; |
598 |
|
|
599 |
|
if (nrays == 0) |
600 |
|
return(-1L); |
601 |
< |
#ifdef DEBUG |
602 |
< |
if (fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks) |
563 |
< |
error(CONSISTENCY, "bad file descriptor in hdallocfrag"); |
564 |
< |
#endif |
601 |
> |
DCHECK(fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks, |
602 |
> |
CONSISTENCY, "bad file descriptor in hdallocfrag"); |
603 |
|
f = &hdfragl[fd]; |
604 |
< |
k = -1; /* find closest-sized fragment */ |
605 |
< |
for (j = f->nfrags; j-- > 0; ) |
606 |
< |
if (f->fi[j].nrd >= nrays && |
607 |
< |
(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 */ |
604 |
> |
for (j = f->nfrags; j-- > 0; ) /* first fit algorithm */ |
605 |
> |
if (f->fi[j].nrd >= nrays) |
606 |
> |
break; |
607 |
> |
if (j < 0) { /* no fragment -- extend file */ |
608 |
|
nfo = f->flen; |
609 |
|
f->flen += nrays*sizeof(RAYVAL); |
610 |
|
} else { /* else use fragment */ |
611 |
< |
nfo = f->fi[k].fo; |
612 |
< |
f->fi[k].fo += nrays*sizeof(RAYVAL); |
613 |
< |
f->fi[k].nrd -= nrays; |
611 |
> |
nfo = f->fi[j].fo; |
612 |
> |
f->fi[j].fo += nrays*sizeof(RAYVAL); |
613 |
> |
f->fi[j].nrd -= nrays; |
614 |
|
} |
615 |
|
return(nfo); |
616 |
|
} |
627 |
|
/* check file status */ |
628 |
|
if (hdfragl[hp->fd].writerr) |
629 |
|
return(-1); |
630 |
< |
#ifdef DEBUG |
631 |
< |
if (i < 1 | i > nbeams(hp)) |
597 |
< |
error(CONSISTENCY, "bad beam index in hdsyncbeam"); |
598 |
< |
#endif |
630 |
> |
DCHECK(i < 1 | i > nbeams(hp), |
631 |
> |
CONSISTENCY, "bad beam index in hdsyncbeam"); |
632 |
|
/* is current fragment OK? */ |
633 |
|
if (hp->bl[i] == NULL || (nrays = hp->bl[i]->nrm) == hp->bi[i].nrd) |
634 |
|
return(0); |
635 |
< |
if (hp->bi[i].nrd) /* relinquish old fragment */ |
636 |
< |
hdfreefrag(hp->fd, &hp->bi[i]); |
635 |
> |
if (hdfragflags&FF_WRITE && hp->bi[i].nrd) |
636 |
> |
hdfreefrag(hp, i); /* relinquish old fragment */ |
637 |
|
if (nrays) { /* get and write new fragment */ |
638 |
|
nfo = hdallocfrag(hp->fd, nrays); |
639 |
|
errno = 0; |
677 |
|
nchanged += hdfreebeam(hp, i); |
678 |
|
return(nchanged); |
679 |
|
} |
680 |
< |
#ifdef DEBUG |
681 |
< |
if (i < 1 | i > nbeams(hp)) |
649 |
< |
error(CONSISTENCY, "bad beam index to hdfreebeam"); |
650 |
< |
#endif |
680 |
> |
DCHECK(i < 1 | i > nbeams(hp), |
681 |
> |
CONSISTENCY, "bad beam index to hdfreebeam"); |
682 |
|
if (hp->bl[i] == NULL) |
683 |
|
return(0); |
684 |
|
/* check for additions */ |
697 |
|
register HOLO *hp; |
698 |
|
register int i; |
699 |
|
{ |
669 |
– |
static BEAM emptybeam; |
700 |
|
int nchanged; |
701 |
|
|
702 |
|
if (hp == NULL) { /* clobber all holodecks */ |
710 |
|
for (i = nbeams(hp); i > 0; i--) |
711 |
|
if (hp->bi[i].nrd > 0 || hp->bl[i] != NULL) |
712 |
|
nchanged += hdkillbeam(hp, i); |
713 |
< |
#ifdef DEBUG |
714 |
< |
if (biglob(hp)->nrd != 0 | blglob(hp)->nrm != 0) |
685 |
< |
error(CONSISTENCY, "bad beam count in hdkillbeam"); |
686 |
< |
#endif |
713 |
> |
DCHECK(biglob(hp)->nrd != 0 | blglob(hp)->nrm != 0, |
714 |
> |
CONSISTENCY, "bad beam count in hdkillbeam"); |
715 |
|
return(nchanged); |
716 |
|
} |
717 |
< |
#ifdef DEBUG |
718 |
< |
if (i < 1 | i > nbeams(hp)) |
691 |
< |
error(CONSISTENCY, "bad beam index to hdkillbeam"); |
692 |
< |
#endif |
717 |
> |
DCHECK(i < 1 | i > nbeams(hp), |
718 |
> |
CONSISTENCY, "bad beam index to hdkillbeam"); |
719 |
|
if (hp->bl[i] != NULL) { /* free memory */ |
720 |
|
blglob(hp)->nrm -= nchanged = hp->bl[i]->nrm; |
721 |
|
free((char *)hp->bl[i]); |
722 |
|
} else |
723 |
|
nchanged = hp->bi[i].nrd; |
724 |
< |
if (hp->bi[i].nrd) { /* free file fragment */ |
725 |
< |
hp->bl[i] = &emptybeam; |
726 |
< |
hdsyncbeam(hp, i); |
724 |
> |
if (hp->bi[i].nrd) { |
725 |
> |
if (hdfragflags&FF_KILL) |
726 |
> |
hdfreefrag(hp, i); |
727 |
> |
biglob(hp)->nrd -= hp->bi[i].nrd; |
728 |
> |
hp->bi[i].nrd = 0; /* make sure it's gone */ |
729 |
> |
hp->bi[i].fo = 0L; |
730 |
|
} |
731 |
|
hp->bl[i] = NULL; |
732 |
|
return(nchanged); |