ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/holofile.c
Revision: 3.1
Committed: Fri Oct 31 10:23:29 1997 UTC (26 years, 6 months ago) by gregl
Content type: text/plain
Branch: MAIN
Log Message:
Initial revision

File Contents

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