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