ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/holofile.c
Revision: 3.5
Committed: Wed Nov 5 17:28:49 1997 UTC (26 years, 5 months ago) by gregl
Content type: text/plain
Branch: MAIN
Changes since 3.4: +9 -10 lines
Log Message:
changed some constants related to paging

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 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 (128*FRAGBLK) /* maximum fragments per 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-1)
320 f->nfrags--;
321 #endif
322 if (j % FRAGBLK == 0) { /* more frag. space */
323 if (f->fi == NULL)
324 f->fi = (BEAMI *)malloc(
325 FRAGBLK*sizeof(BEAMI));
326 else
327 f->fi = (BEAMI *)realloc((char *)f->fi,
328 (j+FRAGBLK)*sizeof(BEAMI));
329 if (f->fi == NULL)
330 error(SYSTEM,
331 "out of memory in hdgetbi");
332 }
333 for ( ; ; j--) { /* insert in descending list */
334 if (!j || hp->bi[i].fo < f->fi[j-1].fo) {
335 f->fi[j].fo = hp->bi[i].fo;
336 f->fi[j].nrd = hp->bi[i].nrd;
337 break;
338 }
339 copystruct(f->fi+j, f->fi+j-1);
340 }
341 /* coalesce adjacent fragments */
342 for (j = k = 0; k < f->nfrags; j++, k++) {
343 if (k > j)
344 copystruct(f->fi+j, f->fi+k);
345 while (k+1 < f->nfrags && f->fi[k+1].fo +
346 f->fi[k+1].nrd*sizeof(RAYVAL)
347 == f->fi[j].fo) {
348 f->fi[j].fo -=
349 f->fi[++k].nrd*sizeof(RAYVAL);
350 f->fi[j].nrd += f->fi[k].nrd;
351 }
352 }
353 f->nfrags = j;
354 }
355 k = -1; /* find closest-sized fragment */
356 for (j = f->nfrags; j-- > 0; )
357 if (f->fi[j].nrd >= nrays &&
358 (k < 0 || f->fi[j].nrd < f->fi[k].nrd))
359 if (f->fi[k=j].nrd == nrays)
360 break;
361 if (k < 0) { /* no fragment -- extend file */
362 hp->bi[i].fo = f->flen;
363 f->flen += nrays*sizeof(RAYVAL);
364 } else { /* else use fragment */
365 hp->bi[i].fo = f->fi[k].fo;
366 if (f->fi[k].nrd == nrays) { /* delete fragment */
367 f->nfrags--;
368 for (j = k; j < f->nfrags; j++)
369 copystruct(f->fi+j, f->fi+j+1);
370 } else { /* else shrink it */
371 f->fi[k].fo += nrays*sizeof(RAYVAL);
372 f->fi[k].nrd -= nrays;
373 }
374 }
375 }
376 biglob(hp)->nrd += nrays - hp->bi[i].nrd;
377 hp->bi[i].nrd = nrays;
378 hp->dirty++; /* section directory now out of date */
379 return(1);
380 }
381
382
383 int
384 hdfreebeam(hp, i) /* free beam, writing if dirty */
385 register HOLO *hp;
386 register int i;
387 {
388 int nchanged, n;
389
390 if (hp == NULL) { /* clear all holodecks */
391 nchanged = 0;
392 for (i = 0; hdlist[i] != NULL; i++)
393 nchanged += hdfreebeam(hdlist[i], 0);
394 return(nchanged);
395 }
396 if (i == 0) { /* clear entire holodeck */
397 nchanged = 0;
398 for (i = 1; i <= nbeams(hp); i++)
399 nchanged += hdfreebeam(hp, i);
400 return(nchanged);
401 }
402 if (i < 1 | i > nbeams(hp))
403 error(CONSISTENCY, "bad beam index to hdfreebeam");
404 if (hp->bl[i] == NULL)
405 return(0);
406 /* check for additions */
407 nchanged = hp->bl[i]->nrm - hp->bi[i].nrd;
408 if (nchanged) {
409 hdgetbi(hp, i); /* allocate a file position */
410 errno = 0;
411 if (lseek(hp->fd, hp->bi[i].fo, 0) < 0)
412 error(SYSTEM, "cannot seek on holodeck file");
413 n = hp->bl[i]->nrm * sizeof(RAYVAL);
414 if (write(hp->fd, (char *)hdbray(hp->bl[i]), n) != n)
415 error(SYSTEM, "write error in hdfreebeam");
416 }
417 blglob(hp)->nrm -= hp->bl[i]->nrm;
418 free((char *)hp->bl[i]); /* free memory */
419 hp->bl[i] = NULL;
420 return(nchanged);
421 }
422
423
424 hdlrulist(ha, ba, n, hp) /* add beams from holodeck to LRU list */
425 register HOLO *ha[]; /* section list (NULL terminated) */
426 register int ba[]; /* beam index to go with section */
427 int n; /* length of arrays minus 1 */
428 register HOLO *hp; /* section we're adding from */
429 {
430 register int i, j;
431 int nents;
432 /* find last entry in LRU list */
433 for (j = 0; ha[j] != NULL; j++)
434 ;
435 nents = j;
436 /* insert each beam from hp */
437 for (i = nbeams(hp); i > 0; i-- ) {
438 if (hp->bl[i] == NULL) /* check if loaded */
439 continue;
440 if ((j = ++nents) > n) /* grow list if we can */
441 nents--;
442 for ( ; ; ) { /* bubble into place */
443 if (!--j || hp->bl[i]->tick >=
444 ha[j-1]->bl[ba[j-1]]->tick) {
445 ha[j] = hp;
446 ba[j] = i;
447 break;
448 }
449 ha[j] = ha[j-1];
450 ba[j] = ba[j-1];
451 }
452 }
453 ha[nents] = NULL; /* all done */
454 ba[nents] = 0;
455 }
456
457
458 hdfreecache(pct, honly) /* free up cache space, writing changes */
459 int pct; /* maximum percentage to free */
460 register HOLO *honly; /* NULL means check all */
461 {
462 HOLO *hp[FREEBEAMS+1];
463 int bn[FREEBEAMS+1];
464 int freetarget;
465 register int i;
466 /* compute free target */
467 freetarget = (honly != NULL) ? blglob(honly)->nrm :
468 hdmemuse(0)/sizeof(RAYVAL) ;
469 freetarget = freetarget*pct/100;
470 /* find least recently used */
471 hp[0] = NULL;
472 bn[0] = 0;
473 if (honly != NULL)
474 hdlrulist(hp, bn, FREEBEAMS, honly);
475 else
476 for (i = 0; hdlist[i] != NULL; i++)
477 hdlrulist(hp, bn, FREEBEAMS, hdlist[i]);
478 /* free LRU beams */
479 for (i = 0; hp[i] != NULL; i++) {
480 hdfreebeam(hp[i], bn[i]);
481 if ((freetarget -= hp[i]->bi[bn[i]].nrd) <= 0)
482 break;
483 }
484 hdsync(honly); /* synchronize directories as necessary */
485 }
486
487
488 hddone(hp) /* clean up holodeck section and free */
489 register HOLO *hp; /* NULL means clean up all */
490 {
491 register int i;
492
493 if (hp == NULL) { /* NULL means clean up everything */
494 while (hdlist[0] != NULL)
495 hddone(hdlist[0]);
496 return;
497 }
498 /* flush all data and free memory */
499 hdflush(hp);
500 /* release fragment resources */
501 hdrelease(hp->fd);
502 /* remove hp from active list */
503 for (i = 0; hdlist[i] != NULL; i++)
504 if (hdlist[i] == hp) {
505 while ((hdlist[i] = hdlist[i+1]) != NULL)
506 i++;
507 break;
508 }
509 free((char *)hp->bl); /* free beam list */
510 free((char *)hp); /* free holodeck struct */
511 }