ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/holofile.c
Revision: 3.12
Committed: Fri Dec 12 19:52:31 1997 UTC (26 years, 4 months ago) by gregl
Content type: text/plain
Branch: MAIN
Changes since 3.11: +25 -12 lines
Log Message:
modified hdsync() to optionally sync all unflushed beams

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 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 < 0 | 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, all) /* update beams and directory on disk */
161 register HOLO *hp;
162 int all;
163 {
164 register int j, n;
165
166 if (hp == NULL) { /* do all holodecks */
167 n = 0;
168 for (j = 0; hdlist[j] != NULL; j++)
169 n += hdsync(hdlist[j], all);
170 return(n);
171 }
172 /* sync the beams */
173 for (j = all ? nbeams(hp) : 0; j > 0; j--)
174 if (hp->bl[j] != NULL)
175 hdsyncbeam(hp, j);
176 if (!hp->dirty) /* directory dirty? */
177 return(0);
178 errno = 0;
179 if (lseek(hp->fd, biglob(hp)->fo, 0) < 0)
180 error(SYSTEM, "cannot seek on holodeck file");
181 n = nbeams(hp)*sizeof(BEAMI);
182 if (write(hp->fd, (char *)(hp->bi+1), n) != n)
183 error(SYSTEM, "cannot update holodeck section directory");
184 hp->dirty = 0;
185 return(1);
186 }
187
188
189 long
190 hdmemuse(all) /* return memory usage (in bytes) */
191 int all; /* include overhead (painful) */
192 {
193 long total = 0;
194 register int i, j;
195
196 for (j = 0; hdlist[j] != NULL; j++) {
197 total += blglob(hdlist[j])->nrm * sizeof(RAYVAL);
198 if (all) {
199 total += sizeof(HOLO) + sizeof(BEAM *) +
200 nbeams(hdlist[j]) *
201 (sizeof(BEAM *)+sizeof(BEAMI));
202 for (i = nbeams(hdlist[j]); i > 0; i--)
203 if (hdlist[j]->bl[i] != NULL)
204 total += sizeof(BEAM);
205 }
206 }
207 if (all)
208 for (j = 0; j < nhdfrags; j++) {
209 total += sizeof(struct fragment);
210 if (hdfrag[j].nfrags)
211 total += FRAGBLK*sizeof(BEAMI) *
212 ((hdfrag[j].nfrags-1)/FRAGBLK + 1) ;
213 }
214 return(total);
215 }
216
217
218 long
219 hdfilen(fd) /* return file length for fd */
220 int fd;
221 {
222 long fpos, flen;
223
224 if (fd < 0)
225 return(-1);
226 if (fd >= nhdfrags || !hdfrag[fd].nlinks) {
227 if ((fpos = lseek(fd, 0L, 1)) < 0)
228 return(-1);
229 flen = lseek(fd, 0L, 2);
230 lseek(fd, fpos, 0);
231 return(flen);
232 }
233 return(hdfrag[fd].flen);
234 }
235
236
237 long
238 hdfiluse(fd, all) /* compute file usage (in bytes) */
239 int fd; /* open file descriptor to check */
240 int all; /* include overhead and unflushed data */
241 {
242 long total = 0;
243 register int i, j;
244
245 for (j = 0; hdlist[j] != NULL; j++) {
246 if (hdlist[j]->fd != fd)
247 continue;
248 total += biglob(hdlist[j])->nrd * sizeof(RAYVAL);
249 if (all) {
250 for (i = nbeams(hdlist[j]); i > 0; i--)
251 if (hdlist[j]->bl[i] != NULL)
252 total += sizeof(RAYVAL) *
253 (hdlist[j]->bl[i]->nrm -
254 hdlist[j]->bi[i].nrd);
255 total += sizeof(HDGRID) +
256 nbeams(hdlist[j])*sizeof(BEAMI);
257 }
258 }
259 return(total); /* does not include fragments */
260 }
261
262
263 RAYVAL *
264 hdnewrays(hp, i, nr) /* allocate space for add'l rays and return pointer */
265 register HOLO *hp;
266 register int i;
267 int nr; /* number of new rays desired */
268 {
269 RAYVAL *p;
270 int n;
271
272 if (nr <= 0)
273 return(NULL);
274 if (i < 1 | i > nbeams(hp))
275 error(CONSISTENCY, "bad beam index given to hdnewrays");
276 if (hp->bl[i] != NULL)
277 hp->bl[i]->tick = hdclock; /* preempt swap */
278 if (hdcachesize > 0 && hdmemuse(0) >= hdcachesize)
279 hdfreecache(PCTFREE, NULL); /* free some space */
280 errno = 0;
281 if (hp->bl[i] == NULL) { /* allocate (and load) */
282 n = hp->bi[i].nrd + nr;
283 if ((hp->bl[i] = (BEAM *)malloc(hdbsiz(n))) == NULL)
284 goto memerr;
285 blglob(hp)->nrm += n;
286 if (n = hp->bl[i]->nrm = hp->bi[i].nrd) {
287 if (lseek(hp->fd, hp->bi[i].fo, 0) < 0)
288 error(SYSTEM, "seek error on holodeck file");
289 n *= sizeof(RAYVAL);
290 if (read(hp->fd, (char *)hdbray(hp->bl[i]), n) != n)
291 error(SYSTEM,
292 "error reading beam from holodeck file");
293 }
294 } else { /* just grow in memory */
295 hp->bl[i] = (BEAM *)realloc( (char *)hp->bl[i],
296 hdbsiz(hp->bl[i]->nrm + nr) );
297 if (hp->bl[i] == NULL)
298 goto memerr;
299 blglob(hp)->nrm += nr;
300 }
301 p = hdbray(hp->bl[i]) + hp->bl[i]->nrm;
302 hp->bl[i]->nrm += nr; /* update in-core structure */
303 bzero((char *)p, nr*sizeof(RAYVAL));
304 hp->bl[i]->tick = ++hdclock; /* update LRU clock */
305 blglob(hp)->tick = hdclock;
306 return(p); /* point to new rays */
307 memerr:
308 error(SYSTEM, "out of memory in hdnewrays");
309 }
310
311
312 BEAM *
313 hdgetbeam(hp, i) /* get beam (from file if necessary) */
314 register HOLO *hp;
315 register int i;
316 {
317 register int n;
318
319 if (i < 1 | i > nbeams(hp))
320 error(CONSISTENCY, "bad beam index given to hdgetbeam");
321 if (hp->bl[i] == NULL) { /* load from disk */
322 if (!(n = hp->bi[i].nrd))
323 return(NULL);
324 if (hdcachesize > 0 && hdmemuse(0) >= hdcachesize)
325 hdfreecache(PCTFREE, NULL); /* get free space */
326 errno = 0;
327 if ((hp->bl[i] = (BEAM *)malloc(hdbsiz(n))) == NULL)
328 error(SYSTEM, "cannot allocate memory for beam");
329 blglob(hp)->nrm += hp->bl[i]->nrm = n;
330 if (lseek(hp->fd, hp->bi[i].fo, 0) < 0)
331 error(SYSTEM, "seek error on holodeck file");
332 n *= sizeof(RAYVAL);
333 if (read(hp->fd, (char *)hdbray(hp->bl[i]), n) != n)
334 error(SYSTEM, "error reading beam from holodeck file");
335 }
336 hp->bl[i]->tick = ++hdclock; /* update LRU clock */
337 blglob(hp)->tick = hdclock;
338 return(hp->bl[i]);
339 }
340
341
342 int
343 hdsyncbeam(hp, i) /* sync beam in memory with beam on disk */
344 register HOLO *hp;
345 register int i;
346 {
347 unsigned int nrays;
348 long nfo;
349 unsigned int n;
350 #ifdef DEBUG
351 if (i < 1 | i > nbeams(hp))
352 error(CONSISTENCY, "bad beam index in hdsyncbeam");
353 #endif
354 /* is current fragment OK? */
355 if (hp->bl[i] == NULL || (nrays = hp->bl[i]->nrm) == hp->bi[i].nrd)
356 return(0);
357 /* check file status */
358 if (hp->dirty < 0)
359 return(-1);
360
361 if (hp->fd >= nhdfrags || !hdfrag[hp->fd].nlinks) /* untracked */
362 hp->bi[i].fo = lseek(hp->fd, 0L, 2);
363
364 else if (hp->bi[i].fo + hp->bi[i].nrd*sizeof(RAYVAL) ==
365 hdfrag[hp->fd].flen) /* EOF special case */
366 hdfrag[hp->fd].flen = (nfo=hp->bi[i].fo) + nrays*sizeof(RAYVAL);
367
368 else { /* general case */
369 register struct fragment *f = &hdfrag[hp->fd];
370 register int j, k;
371 /* relinquish old fragment */
372 if (hp->bi[i].nrd) {
373 j = f->nfrags++;
374 #ifdef MAXFRAG
375 if (j >= MAXFRAG-1)
376 f->nfrags--;
377 #endif
378 if (j % FRAGBLK == 0) { /* more frag. space */
379 if (f->fi == NULL)
380 f->fi = (BEAMI *)malloc(
381 FRAGBLK*sizeof(BEAMI));
382 else
383 f->fi = (BEAMI *)realloc((char *)f->fi,
384 (j+FRAGBLK)*sizeof(BEAMI));
385 if (f->fi == NULL)
386 error(SYSTEM,
387 "out of memory in hdsyncbeam");
388 }
389 for ( ; ; j--) { /* insert in descending list */
390 if (!j || hp->bi[i].fo < f->fi[j-1].fo) {
391 f->fi[j].fo = hp->bi[i].fo;
392 f->fi[j].nrd = hp->bi[i].nrd;
393 break;
394 }
395 copystruct(f->fi+j, f->fi+j-1);
396 }
397 /* coalesce adjacent fragments */
398 for (j = k = 0; k < f->nfrags; j++, k++) {
399 if (k > j)
400 copystruct(f->fi+j, f->fi+k);
401 while (k+1 < f->nfrags && f->fi[k+1].fo +
402 f->fi[k+1].nrd*sizeof(RAYVAL)
403 == f->fi[j].fo) {
404 f->fi[j].fo -=
405 f->fi[++k].nrd*sizeof(RAYVAL);
406 f->fi[j].nrd += f->fi[k].nrd;
407 }
408 }
409 f->nfrags = j;
410 }
411 k = -1; /* find closest-sized fragment */
412 for (j = nrays ? f->nfrags : 0; j-- > 0; )
413 if (f->fi[j].nrd >= nrays &&
414 (k < 0 || f->fi[j].nrd < f->fi[k].nrd))
415 if (f->fi[k=j].nrd == nrays)
416 break;
417 if (k < 0) { /* no fragment -- extend file */
418 nfo = f->flen;
419 f->flen += nrays*sizeof(RAYVAL);
420 } else { /* else use fragment */
421 nfo = f->fi[k].fo;
422 if (f->fi[k].nrd == nrays) { /* delete fragment */
423 f->nfrags--;
424 for (j = k; j < f->nfrags; j++)
425 copystruct(f->fi+j, f->fi+j+1);
426 } else { /* else shrink it */
427 f->fi[k].fo += nrays*sizeof(RAYVAL);
428 f->fi[k].nrd -= nrays;
429 }
430 }
431 }
432 if (nrays) { /* write the new fragment */
433 errno = 0;
434 if (lseek(hp->fd, nfo, 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 hp->dirty = -1; /* avoid recursive error */
439 error(SYSTEM, "write error in hdsyncbeam");
440 }
441 }
442 biglob(hp)->nrd += nrays - hp->bi[i].nrd;
443 hp->bi[i].nrd = nrays;
444 hp->bi[i].fo = nfo;
445 markdirty(hp); /* section directory now out of date */
446 return(1);
447 }
448
449
450 int
451 hdfreebeam(hp, i) /* free beam, writing if dirty */
452 register HOLO *hp;
453 register int i;
454 {
455 int nchanged;
456
457 if (hp == NULL) { /* clear all holodecks */
458 nchanged = 0;
459 for (i = 0; hdlist[i] != NULL; i++)
460 nchanged += hdfreebeam(hdlist[i], 0);
461 return(nchanged);
462 }
463 if (i == 0) { /* clear entire holodeck */
464 nchanged = 0;
465 for (i = nbeams(hp); i > 0; i--)
466 if (hp->bl[i] != NULL)
467 nchanged += hdfreebeam(hp, i);
468 return(nchanged);
469 }
470 #ifdef DEBUG
471 if (i < 1 | i > nbeams(hp))
472 error(CONSISTENCY, "bad beam index to hdfreebeam");
473 #endif
474 if (hp->bl[i] == NULL)
475 return(0);
476 /* check for additions */
477 nchanged = hp->bl[i]->nrm - hp->bi[i].nrd;
478 if (nchanged)
479 hdsyncbeam(hp, i); /* write new fragment */
480 blglob(hp)->nrm -= hp->bl[i]->nrm;
481 free((char *)hp->bl[i]); /* free memory */
482 hp->bl[i] = NULL;
483 return(nchanged);
484 }
485
486
487 int
488 hdkillbeam(hp, i) /* delete beam from holodeck */
489 register HOLO *hp;
490 register int i;
491 {
492 static BEAM emptybeam;
493 int nchanged;
494
495 if (hp == NULL) { /* clobber all holodecks */
496 nchanged = 0;
497 for (i = 0; hdlist[i] != NULL; i++)
498 nchanged += hdkillbeam(hdlist[i], 0);
499 return(nchanged);
500 }
501 if (i == 0) { /* clobber entire holodeck */
502 nchanged = 0;
503 for (i = nbeams(hp); i > 0; i--)
504 if (hp->bi[i].nrd > 0 || hp->bl[i] != NULL)
505 nchanged += hdkillbeam(hp, i);
506 #ifdef DEBUG
507 if (biglob(hp)->nrd != 0 | blglob(hp)->nrm != 0)
508 error(CONSISTENCY, "bad beam count in hdkillbeam");
509 #endif
510 return(nchanged);
511 }
512 #ifdef DEBUG
513 if (i < 1 | i > nbeams(hp))
514 error(CONSISTENCY, "bad beam index to hdkillbeam");
515 #endif
516 if (hp->bl[i] != NULL) { /* free memory */
517 blglob(hp)->nrm -= nchanged = hp->bl[i]->nrm;
518 free((char *)hp->bl[i]);
519 } else
520 nchanged = hp->bi[i].nrd;
521 if (hp->bi[i].nrd) { /* free file fragment */
522 hp->bl[i] = &emptybeam;
523 hdsyncbeam(hp, i);
524 }
525 hp->bl[i] = NULL;
526 return(nchanged);
527 }
528
529
530 hdlrulist(ha, ba, n, hp) /* add beams from holodeck to LRU list */
531 register HOLO *ha[]; /* section list (NULL terminated) */
532 register int ba[]; /* beam index to go with section */
533 int n; /* length of arrays minus 1 */
534 register HOLO *hp; /* section we're adding from */
535 {
536 register int i, j;
537 int nents;
538 /* find last entry in LRU list */
539 for (j = 0; ha[j] != NULL; j++)
540 ;
541 nents = j;
542 /* insert each beam from hp */
543 for (i = nbeams(hp); i > 0; i--) {
544 if (hp->bl[i] == NULL) /* check if loaded */
545 continue;
546 if ((j = ++nents) > n) /* grow list if we can */
547 nents--;
548 for ( ; ; ) { /* bubble into place */
549 if (!--j || hp->bl[i]->tick >=
550 ha[j-1]->bl[ba[j-1]]->tick) {
551 ha[j] = hp;
552 ba[j] = i;
553 break;
554 }
555 ha[j] = ha[j-1];
556 ba[j] = ba[j-1];
557 }
558 }
559 ha[nents] = NULL; /* all done */
560 ba[nents] = 0;
561 }
562
563
564 hdfreecache(pct, honly) /* free up cache space, writing changes */
565 int pct; /* maximum percentage to free */
566 register HOLO *honly; /* NULL means check all */
567 {
568 HOLO *hp[FREEBEAMS+1];
569 int bn[FREEBEAMS+1];
570 int freetarget;
571 register int i;
572 /* compute free target */
573 freetarget = (honly != NULL) ? blglob(honly)->nrm :
574 hdmemuse(0)/sizeof(RAYVAL) ;
575 freetarget = freetarget*pct/100;
576 /* find least recently used */
577 hp[0] = NULL;
578 bn[0] = 0;
579 if (honly != NULL)
580 hdlrulist(hp, bn, FREEBEAMS, honly);
581 else
582 for (i = 0; hdlist[i] != NULL; i++)
583 hdlrulist(hp, bn, FREEBEAMS, hdlist[i]);
584 /* free LRU beams */
585 for (i = 0; hp[i] != NULL; i++) {
586 hdfreebeam(hp[i], bn[i]);
587 if ((freetarget -= hp[i]->bi[bn[i]].nrd) <= 0)
588 break;
589 }
590 hdsync(honly, 0); /* synchronize directories as necessary */
591 }
592
593
594 hddone(hp) /* clean up holodeck section and free */
595 register HOLO *hp; /* NULL means clean up all */
596 {
597 register int i;
598
599 if (hp == NULL) { /* NULL means clean up everything */
600 while (hdlist[0] != NULL)
601 hddone(hdlist[0]);
602 return;
603 }
604 /* flush all data and free memory */
605 hdflush(hp);
606 /* release fragment resources */
607 hdrelease(hp->fd);
608 /* remove hp from active list */
609 for (i = 0; hdlist[i] != NULL; i++)
610 if (hdlist[i] == hp) {
611 while ((hdlist[i] = hdlist[i+1]) != NULL)
612 i++;
613 break;
614 }
615 free((char *)hp->bl); /* free beam list */
616 free((char *)hp); /* free holodeck struct */
617 }