ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/holofile.c
Revision: 3.4
Committed: Tue Nov 4 10:42:03 1997 UTC (26 years, 5 months ago) by gregl
Content type: text/plain
Branch: MAIN
Changes since 3.3: +53 -30 lines
Log Message:
dynamically allocate fragment lists

File Contents

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