1 |
/* Copyright (c) 1997 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 512 /* maximum beams to free at a time */ |
20 |
#endif |
21 |
#ifndef PCTFREE |
22 |
#define PCTFREE 20 /* maximum fraction to free (%) */ |
23 |
#endif |
24 |
|
25 |
/* define MAXFRAG if you want to limit fragment tracking memory */ |
26 |
|
27 |
#ifndef BSD |
28 |
#define write writebuf /* safe i/o routines */ |
29 |
#define read readbuf |
30 |
#endif |
31 |
|
32 |
#define FRAGBLK 64 /* number of fragments to allocate at a time */ |
33 |
|
34 |
int hdcachesize = CACHESIZE*1024*1024; /* target cache size (bytes) */ |
35 |
unsigned long hdclock; /* clock value */ |
36 |
|
37 |
HOLO *hdlist[HDMAX+1]; /* holodeck pointers (NULL term.) */ |
38 |
|
39 |
static struct fragment { |
40 |
short nlinks; /* number of holodeck sections using us */ |
41 |
short nfrags; /* number of known fragments */ |
42 |
BEAMI *fi; /* fragments, descending file position */ |
43 |
long flen; /* last known file length */ |
44 |
} *hdfrag; /* fragment lists, indexed by file descriptor */ |
45 |
|
46 |
static int nhdfrags; /* size of hdfrag array */ |
47 |
|
48 |
|
49 |
hdattach(fd) /* start tracking file fragments for some section */ |
50 |
register int fd; |
51 |
{ |
52 |
if (fd >= nhdfrags) { |
53 |
if (nhdfrags) |
54 |
hdfrag = (struct fragment *)realloc((char *)hdfrag, |
55 |
(fd+1)*sizeof(struct fragment)); |
56 |
else |
57 |
hdfrag = (struct fragment *)malloc( |
58 |
(fd+1)*sizeof(struct fragment)); |
59 |
if (hdfrag == NULL) |
60 |
error(SYSTEM, "out of memory in hdattach"); |
61 |
bzero((char *)(hdfrag+nhdfrags), |
62 |
(fd+1-nhdfrags)*sizeof(struct fragment)); |
63 |
nhdfrags = fd+1; |
64 |
} |
65 |
hdfrag[fd].nlinks++; |
66 |
hdfrag[fd].flen = lseek(fd, 0L, 2); /* get file length */ |
67 |
} |
68 |
|
69 |
|
70 |
/* Do we need a routine to locate file fragments given known occupants? */ |
71 |
|
72 |
|
73 |
hdrelease(fd) /* stop tracking file fragments for some section */ |
74 |
register int fd; |
75 |
{ |
76 |
if (fd >= nhdfrags || !hdfrag[fd].nlinks) |
77 |
return; |
78 |
if (!--hdfrag[fd].nlinks && hdfrag[fd].nfrags) { |
79 |
free((char *)hdfrag[fd].fi); |
80 |
hdfrag[fd].fi = NULL; |
81 |
hdfrag[fd].nfrags = 0; |
82 |
} |
83 |
} |
84 |
|
85 |
|
86 |
markdirty(hp) /* mark holodeck section directory dirty */ |
87 |
register HOLO *hp; |
88 |
{ |
89 |
static BEAMI smudge = {0, -1}; |
90 |
|
91 |
if (hp->dirty) /* already marked? */ |
92 |
return; |
93 |
hp->dirty = 1; |
94 |
if (lseek(hp->fd, biglob(hp)->fo+(nbeams(hp)-1)*sizeof(BEAMI), 0) < 0 |
95 |
|| write(hp->fd, (char *)&smudge, |
96 |
sizeof(BEAMI)) != sizeof(BEAMI)) |
97 |
error(SYSTEM, "seek/write error in markdirty"); |
98 |
} |
99 |
|
100 |
|
101 |
HOLO * |
102 |
hdinit(fd, hproto) /* initialize a holodeck section in a file */ |
103 |
int fd; /* corresponding file descriptor */ |
104 |
HDGRID *hproto; /* holodeck section grid */ |
105 |
{ |
106 |
long fpos; |
107 |
register HOLO *hp; |
108 |
register int n; |
109 |
/* prepare for system errors */ |
110 |
errno = 0; |
111 |
if ((fpos = lseek(fd, 0L, 1)) < 0) |
112 |
error(SYSTEM, "cannot determine holodeck file position"); |
113 |
if (hproto == NULL) { /* assume we're loading it */ |
114 |
HDGRID hpr; |
115 |
/* load header */ |
116 |
if (read(fd, (char *)&hpr, sizeof(HDGRID)) != sizeof(HDGRID)) |
117 |
error(SYSTEM, "cannot load holodeck header"); |
118 |
/* allocate grid */ |
119 |
if ((hp = hdalloc(&hpr)) == NULL) |
120 |
goto memerr; |
121 |
/* load beam directory */ |
122 |
n = nbeams(hp)*sizeof(BEAMI); |
123 |
if (read(fd, (char *)(hp->bi+1), n) != n) |
124 |
error(SYSTEM, "failure loading holodeck directory"); |
125 |
/* check that it's clean */ |
126 |
if (hp->bi[nbeams(hp)].fo < 0) |
127 |
error(USER, "dirty holodeck section"); |
128 |
} else { /* assume we're creating it */ |
129 |
if ((hp = hdalloc(hproto)) == NULL) |
130 |
goto memerr; |
131 |
/* write header and skeleton */ |
132 |
n = nbeams(hp)*sizeof(BEAMI); |
133 |
if (write(fd, (char *)hproto, sizeof(HDGRID)) != |
134 |
sizeof(HDGRID) || |
135 |
write(fd, (char *)(hp->bi+1), n) != n) |
136 |
error(SYSTEM, "cannot write header to holodeck file"); |
137 |
} |
138 |
hp->fd = fd; |
139 |
hp->dirty = 0; |
140 |
biglob(hp)->fo = fpos + sizeof(HDGRID); |
141 |
biglob(hp)->nrd = 0; /* count rays on disk */ |
142 |
for (n = nbeams(hp); n > 0; n--) |
143 |
biglob(hp)->nrd += hp->bi[n].nrd; |
144 |
/* add to holodeck list */ |
145 |
for (n = 0; n < HDMAX; n++) |
146 |
if (hdlist[n] == NULL) { |
147 |
hdlist[n] = hp; |
148 |
break; |
149 |
} |
150 |
/* start tracking fragments (last) */ |
151 |
hdattach(fd); |
152 |
/* all done */ |
153 |
return(hp); |
154 |
memerr: |
155 |
error(SYSTEM, "cannot allocate holodeck grid"); |
156 |
} |
157 |
|
158 |
|
159 |
int |
160 |
hdsync(hp) /* update directory on disk if necessary */ |
161 |
register HOLO *hp; |
162 |
{ |
163 |
register int j, n; |
164 |
|
165 |
if (hp == NULL) { /* do all */ |
166 |
n = 0; |
167 |
for (j = 0; hdlist[j] != NULL; j++) |
168 |
n += hdsync(hdlist[j]); |
169 |
return(n); |
170 |
} |
171 |
if (!hp->dirty) /* check first */ |
172 |
return(0); |
173 |
errno = 0; |
174 |
if (lseek(hp->fd, biglob(hp)->fo, 0) < 0) |
175 |
error(SYSTEM, "cannot seek on holodeck file"); |
176 |
n = nbeams(hp)*sizeof(BEAMI); |
177 |
if (write(hp->fd, (char *)(hp->bi+1), n) != n) |
178 |
error(SYSTEM, "cannot update holodeck section directory"); |
179 |
hp->dirty = 0; |
180 |
return(1); |
181 |
} |
182 |
|
183 |
|
184 |
long |
185 |
hdmemuse(all) /* return memory usage (in bytes) */ |
186 |
int all; /* include overhead (painful) */ |
187 |
{ |
188 |
long total = 0; |
189 |
register int i, j; |
190 |
|
191 |
for (j = 0; hdlist[j] != NULL; j++) { |
192 |
total += blglob(hdlist[j])->nrm * sizeof(RAYVAL); |
193 |
if (all) { |
194 |
total += sizeof(HOLO) + sizeof(BEAM *) + |
195 |
nbeams(hdlist[j]) * |
196 |
(sizeof(BEAM *)+sizeof(BEAMI)); |
197 |
for (i = nbeams(hdlist[j]); i > 0; i--) |
198 |
if (hdlist[j]->bl[i] != NULL) |
199 |
total += sizeof(BEAM); |
200 |
} |
201 |
} |
202 |
if (all) |
203 |
for (j = 0; j < nhdfrags; j++) { |
204 |
total += sizeof(struct fragment); |
205 |
if (hdfrag[j].nfrags) |
206 |
total += FRAGBLK*sizeof(BEAMI) * |
207 |
((hdfrag[j].nfrags-1)/FRAGBLK + 1) ; |
208 |
} |
209 |
return(total); |
210 |
} |
211 |
|
212 |
|
213 |
long |
214 |
hdfiluse(fd, all) /* compute file usage (in bytes) */ |
215 |
int fd; /* open file descriptor to check */ |
216 |
int all; /* include overhead and unflushed data */ |
217 |
{ |
218 |
long total = 0; |
219 |
register int i, j; |
220 |
|
221 |
for (j = 0; hdlist[j] != NULL; j++) { |
222 |
if (hdlist[j]->fd != fd) |
223 |
continue; |
224 |
total += biglob(hdlist[j])->nrd * sizeof(RAYVAL); |
225 |
if (all) { |
226 |
for (i = nbeams(hdlist[j]); i > 0; i--) |
227 |
if (hdlist[j]->bl[i] != NULL) |
228 |
total += sizeof(RAYVAL) * |
229 |
(hdlist[j]->bl[i]->nrm - |
230 |
hdlist[j]->bi[i].nrd); |
231 |
total += sizeof(HDGRID) + |
232 |
nbeams(hdlist[j])*sizeof(BEAMI); |
233 |
} |
234 |
} |
235 |
return(total); /* does not include fragments */ |
236 |
} |
237 |
|
238 |
|
239 |
RAYVAL * |
240 |
hdnewrays(hp, i, nr) /* allocate space for add'l rays and return pointer */ |
241 |
register HOLO *hp; |
242 |
register int i; |
243 |
int nr; /* number of new rays desired */ |
244 |
{ |
245 |
RAYVAL *p; |
246 |
int n; |
247 |
|
248 |
if (nr <= 0) |
249 |
return(NULL); |
250 |
if (i < 1 | i > nbeams(hp)) |
251 |
error(CONSISTENCY, "bad beam index given to hdnewrays"); |
252 |
if (hp->bl[i] != NULL) |
253 |
hp->bl[i]->tick = hdclock; /* preempt swap */ |
254 |
if (hdcachesize > 0 && hdmemuse(0) >= hdcachesize) |
255 |
hdfreecache(PCTFREE, NULL); /* free some space */ |
256 |
errno = 0; |
257 |
if (hp->bl[i] == NULL) { /* allocate (and load) */ |
258 |
n = hp->bi[i].nrd + nr; |
259 |
if ((hp->bl[i] = (BEAM *)malloc(hdbsiz(n))) == NULL) |
260 |
goto memerr; |
261 |
blglob(hp)->nrm += n; |
262 |
if (n = hp->bl[i]->nrm = hp->bi[i].nrd) { |
263 |
if (lseek(hp->fd, hp->bi[i].fo, 0) < 0) |
264 |
error(SYSTEM, "seek error on holodeck file"); |
265 |
n *= sizeof(RAYVAL); |
266 |
if (read(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) |
267 |
error(SYSTEM, |
268 |
"error reading beam from holodeck file"); |
269 |
} |
270 |
} else { /* just grow in memory */ |
271 |
hp->bl[i] = (BEAM *)realloc( (char *)hp->bl[i], |
272 |
hdbsiz(hp->bl[i]->nrm + nr) ); |
273 |
if (hp->bl[i] == NULL) |
274 |
goto memerr; |
275 |
blglob(hp)->nrm += nr; |
276 |
} |
277 |
p = hdbray(hp->bl[i]) + hp->bl[i]->nrm; |
278 |
hp->bl[i]->nrm += nr; /* update in-core structure */ |
279 |
bzero((char *)p, nr*sizeof(RAYVAL)); |
280 |
hp->bl[i]->tick = ++hdclock; /* update LRU clock */ |
281 |
blglob(hp)->tick = hdclock; |
282 |
return(p); /* point to new rays */ |
283 |
memerr: |
284 |
error(SYSTEM, "out of memory in hdnewrays"); |
285 |
} |
286 |
|
287 |
|
288 |
BEAM * |
289 |
hdgetbeam(hp, i) /* get beam (from file if necessary) */ |
290 |
register HOLO *hp; |
291 |
register int i; |
292 |
{ |
293 |
register int n; |
294 |
|
295 |
if (i < 1 | i > nbeams(hp)) |
296 |
error(CONSISTENCY, "bad beam index given to hdgetbeam"); |
297 |
if (hp->bl[i] == NULL) { /* load from disk */ |
298 |
if (!(n = hp->bi[i].nrd)) |
299 |
return(NULL); |
300 |
if (hdcachesize > 0 && hdmemuse(0) >= hdcachesize) |
301 |
hdfreecache(PCTFREE, NULL); /* get free space */ |
302 |
errno = 0; |
303 |
if ((hp->bl[i] = (BEAM *)malloc(hdbsiz(n))) == NULL) |
304 |
error(SYSTEM, "cannot allocate memory for beam"); |
305 |
blglob(hp)->nrm += hp->bl[i]->nrm = n; |
306 |
if (lseek(hp->fd, hp->bi[i].fo, 0) < 0) |
307 |
error(SYSTEM, "seek error on holodeck file"); |
308 |
n *= sizeof(RAYVAL); |
309 |
if (read(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) |
310 |
error(SYSTEM, "error reading beam from holodeck file"); |
311 |
} |
312 |
hp->bl[i]->tick = ++hdclock; /* update LRU clock */ |
313 |
blglob(hp)->tick = hdclock; |
314 |
return(hp->bl[i]); |
315 |
} |
316 |
|
317 |
|
318 |
int |
319 |
hdgetbi(hp, i) /* allocate a file fragment */ |
320 |
register HOLO *hp; |
321 |
register int i; |
322 |
{ |
323 |
int nrays = hp->bl[i]->nrm; |
324 |
|
325 |
if (hp->bi[i].nrd == nrays) /* current one will do? */ |
326 |
return(0); |
327 |
|
328 |
if (hp->fd >= nhdfrags || !hdfrag[hp->fd].nlinks) /* untracked */ |
329 |
hp->bi[i].fo = lseek(hp->fd, 0L, 2); |
330 |
|
331 |
else if (hp->bi[i].fo + hp->bi[i].nrd*sizeof(RAYVAL) == |
332 |
hdfrag[hp->fd].flen) /* EOF special case */ |
333 |
hdfrag[hp->fd].flen = hp->bi[i].fo + nrays*sizeof(RAYVAL); |
334 |
|
335 |
else { /* general case */ |
336 |
register struct fragment *f = &hdfrag[hp->fd]; |
337 |
register int j, k; |
338 |
/* relinquish old fragment */ |
339 |
if (hp->bi[i].nrd) { |
340 |
j = f->nfrags++; |
341 |
#ifdef MAXFRAG |
342 |
if (j >= MAXFRAG-1) |
343 |
f->nfrags--; |
344 |
#endif |
345 |
if (j % FRAGBLK == 0) { /* more frag. space */ |
346 |
if (f->fi == NULL) |
347 |
f->fi = (BEAMI *)malloc( |
348 |
FRAGBLK*sizeof(BEAMI)); |
349 |
else |
350 |
f->fi = (BEAMI *)realloc((char *)f->fi, |
351 |
(j+FRAGBLK)*sizeof(BEAMI)); |
352 |
if (f->fi == NULL) |
353 |
error(SYSTEM, |
354 |
"out of memory in hdgetbi"); |
355 |
} |
356 |
for ( ; ; j--) { /* insert in descending list */ |
357 |
if (!j || hp->bi[i].fo < f->fi[j-1].fo) { |
358 |
f->fi[j].fo = hp->bi[i].fo; |
359 |
f->fi[j].nrd = hp->bi[i].nrd; |
360 |
break; |
361 |
} |
362 |
copystruct(f->fi+j, f->fi+j-1); |
363 |
} |
364 |
/* coalesce adjacent fragments */ |
365 |
for (j = k = 0; k < f->nfrags; j++, k++) { |
366 |
if (k > j) |
367 |
copystruct(f->fi+j, f->fi+k); |
368 |
while (k+1 < f->nfrags && f->fi[k+1].fo + |
369 |
f->fi[k+1].nrd*sizeof(RAYVAL) |
370 |
== f->fi[j].fo) { |
371 |
f->fi[j].fo -= |
372 |
f->fi[++k].nrd*sizeof(RAYVAL); |
373 |
f->fi[j].nrd += f->fi[k].nrd; |
374 |
} |
375 |
} |
376 |
f->nfrags = j; |
377 |
} |
378 |
k = -1; /* find closest-sized fragment */ |
379 |
for (j = f->nfrags; j-- > 0; ) |
380 |
if (f->fi[j].nrd >= nrays && |
381 |
(k < 0 || f->fi[j].nrd < f->fi[k].nrd)) |
382 |
if (f->fi[k=j].nrd == nrays) |
383 |
break; |
384 |
if (k < 0) { /* no fragment -- extend file */ |
385 |
hp->bi[i].fo = f->flen; |
386 |
f->flen += nrays*sizeof(RAYVAL); |
387 |
} else { /* else use fragment */ |
388 |
hp->bi[i].fo = f->fi[k].fo; |
389 |
if (f->fi[k].nrd == nrays) { /* delete fragment */ |
390 |
f->nfrags--; |
391 |
for (j = k; j < f->nfrags; j++) |
392 |
copystruct(f->fi+j, f->fi+j+1); |
393 |
} else { /* else shrink it */ |
394 |
f->fi[k].fo += nrays*sizeof(RAYVAL); |
395 |
f->fi[k].nrd -= nrays; |
396 |
} |
397 |
} |
398 |
} |
399 |
biglob(hp)->nrd += nrays - hp->bi[i].nrd; |
400 |
hp->bi[i].nrd = nrays; |
401 |
markdirty(hp); /* section directory now out of date */ |
402 |
return(1); |
403 |
} |
404 |
|
405 |
|
406 |
int |
407 |
hdfreebeam(hp, i) /* free beam, writing if dirty */ |
408 |
register HOLO *hp; |
409 |
register int i; |
410 |
{ |
411 |
int nchanged, n; |
412 |
|
413 |
if (hp == NULL) { /* clear all holodecks */ |
414 |
nchanged = 0; |
415 |
for (i = 0; hdlist[i] != NULL; i++) |
416 |
nchanged += hdfreebeam(hdlist[i], 0); |
417 |
return(nchanged); |
418 |
} |
419 |
if (i == 0) { /* clear entire holodeck */ |
420 |
nchanged = 0; |
421 |
for (i = 1; i <= nbeams(hp); i++) |
422 |
nchanged += hdfreebeam(hp, i); |
423 |
return(nchanged); |
424 |
} |
425 |
if (i < 1 | i > nbeams(hp)) |
426 |
error(CONSISTENCY, "bad beam index to hdfreebeam"); |
427 |
if (hp->bl[i] == NULL) |
428 |
return(0); |
429 |
/* check for additions */ |
430 |
nchanged = hp->bl[i]->nrm - hp->bi[i].nrd; |
431 |
if (nchanged) { |
432 |
hdgetbi(hp, i); /* allocate a file position */ |
433 |
errno = 0; |
434 |
if (lseek(hp->fd, hp->bi[i].fo, 0) < 0) |
435 |
error(SYSTEM, "cannot seek on holodeck file"); |
436 |
n = hp->bl[i]->nrm * sizeof(RAYVAL); |
437 |
if (write(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) |
438 |
error(SYSTEM, "write error in hdfreebeam"); |
439 |
} |
440 |
blglob(hp)->nrm -= hp->bl[i]->nrm; |
441 |
free((char *)hp->bl[i]); /* free memory */ |
442 |
hp->bl[i] = NULL; |
443 |
return(nchanged); |
444 |
} |
445 |
|
446 |
|
447 |
hdlrulist(ha, ba, n, hp) /* add beams from holodeck to LRU list */ |
448 |
register HOLO *ha[]; /* section list (NULL terminated) */ |
449 |
register int ba[]; /* beam index to go with section */ |
450 |
int n; /* length of arrays minus 1 */ |
451 |
register HOLO *hp; /* section we're adding from */ |
452 |
{ |
453 |
register int i, j; |
454 |
int nents; |
455 |
/* find last entry in LRU list */ |
456 |
for (j = 0; ha[j] != NULL; j++) |
457 |
; |
458 |
nents = j; |
459 |
/* insert each beam from hp */ |
460 |
for (i = nbeams(hp); i > 0; i-- ) { |
461 |
if (hp->bl[i] == NULL) /* check if loaded */ |
462 |
continue; |
463 |
if ((j = ++nents) > n) /* grow list if we can */ |
464 |
nents--; |
465 |
for ( ; ; ) { /* bubble into place */ |
466 |
if (!--j || hp->bl[i]->tick >= |
467 |
ha[j-1]->bl[ba[j-1]]->tick) { |
468 |
ha[j] = hp; |
469 |
ba[j] = i; |
470 |
break; |
471 |
} |
472 |
ha[j] = ha[j-1]; |
473 |
ba[j] = ba[j-1]; |
474 |
} |
475 |
} |
476 |
ha[nents] = NULL; /* all done */ |
477 |
ba[nents] = 0; |
478 |
} |
479 |
|
480 |
|
481 |
hdfreecache(pct, honly) /* free up cache space, writing changes */ |
482 |
int pct; /* maximum percentage to free */ |
483 |
register HOLO *honly; /* NULL means check all */ |
484 |
{ |
485 |
HOLO *hp[FREEBEAMS+1]; |
486 |
int bn[FREEBEAMS+1]; |
487 |
int freetarget; |
488 |
register int i; |
489 |
/* compute free target */ |
490 |
freetarget = (honly != NULL) ? blglob(honly)->nrm : |
491 |
hdmemuse(0)/sizeof(RAYVAL) ; |
492 |
freetarget = freetarget*pct/100; |
493 |
/* find least recently used */ |
494 |
hp[0] = NULL; |
495 |
bn[0] = 0; |
496 |
if (honly != NULL) |
497 |
hdlrulist(hp, bn, FREEBEAMS, honly); |
498 |
else |
499 |
for (i = 0; hdlist[i] != NULL; i++) |
500 |
hdlrulist(hp, bn, FREEBEAMS, hdlist[i]); |
501 |
/* free LRU beams */ |
502 |
for (i = 0; hp[i] != NULL; i++) { |
503 |
hdfreebeam(hp[i], bn[i]); |
504 |
if ((freetarget -= hp[i]->bi[bn[i]].nrd) <= 0) |
505 |
break; |
506 |
} |
507 |
hdsync(honly); /* synchronize directories as necessary */ |
508 |
} |
509 |
|
510 |
|
511 |
hddone(hp) /* clean up holodeck section and free */ |
512 |
register HOLO *hp; /* NULL means clean up all */ |
513 |
{ |
514 |
register int i; |
515 |
|
516 |
if (hp == NULL) { /* NULL means clean up everything */ |
517 |
while (hdlist[0] != NULL) |
518 |
hddone(hdlist[0]); |
519 |
return; |
520 |
} |
521 |
/* flush all data and free memory */ |
522 |
hdflush(hp); |
523 |
/* release fragment resources */ |
524 |
hdrelease(hp->fd); |
525 |
/* remove hp from active list */ |
526 |
for (i = 0; hdlist[i] != NULL; i++) |
527 |
if (hdlist[i] == hp) { |
528 |
while ((hdlist[i] = hdlist[i+1]) != NULL) |
529 |
i++; |
530 |
break; |
531 |
} |
532 |
free((char *)hp->bl); /* free beam list */ |
533 |
free((char *)hp); /* free holodeck struct */ |
534 |
} |