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 |
gregl |
3.5 |
#define FREEBEAMS 512 /* maximum beams to free at a time */ |
20 |
gregl |
3.1 |
#endif |
21 |
|
|
#ifndef PCTFREE |
22 |
gregl |
3.5 |
#define PCTFREE 20 /* maximum fraction to free (%) */ |
23 |
gregl |
3.1 |
#endif |
24 |
gregl |
3.15 |
#ifndef MAXFRAG |
25 |
gregl |
3.16 |
#define MAXFRAG 32767 /* maximum fragments/file to track (0==inf) */ |
26 |
gregl |
3.15 |
#endif |
27 |
gregl |
3.1 |
|
28 |
gregl |
3.6 |
#ifndef BSD |
29 |
|
|
#define write writebuf /* safe i/o routines */ |
30 |
|
|
#define read readbuf |
31 |
|
|
#endif |
32 |
gregl |
3.4 |
|
33 |
gregl |
3.16 |
#define FRAGBLK 256 /* number of fragments to allocate at a time */ |
34 |
gregl |
3.4 |
|
35 |
gregl |
3.14 |
unsigned hdcachesize = CACHESIZE*1024*1024; /* target cache size */ |
36 |
gregl |
3.1 |
unsigned long hdclock; /* clock value */ |
37 |
|
|
|
38 |
|
|
HOLO *hdlist[HDMAX+1]; /* holodeck pointers (NULL term.) */ |
39 |
|
|
|
40 |
gregl |
3.16 |
static struct fraglist { |
41 |
gregl |
3.1 |
short nlinks; /* number of holodeck sections using us */ |
42 |
gregl |
3.13 |
short writerr; /* write error encountered */ |
43 |
|
|
int nfrags; /* number of known fragments */ |
44 |
gregl |
3.4 |
BEAMI *fi; /* fragments, descending file position */ |
45 |
gregl |
3.1 |
long flen; /* last known file length */ |
46 |
gregl |
3.16 |
} *hdfragl; /* fragment lists, indexed by file descriptor */ |
47 |
gregl |
3.1 |
|
48 |
gregl |
3.16 |
static int nhdfragls; /* size of hdfragl array */ |
49 |
gregl |
3.1 |
|
50 |
gregl |
3.4 |
|
51 |
gregl |
3.1 |
hdattach(fd) /* start tracking file fragments for some section */ |
52 |
|
|
register int fd; |
53 |
|
|
{ |
54 |
gregl |
3.16 |
if (fd >= nhdfragls) { |
55 |
|
|
if (nhdfragls) |
56 |
|
|
hdfragl = (struct fraglist *)realloc((char *)hdfragl, |
57 |
|
|
(fd+1)*sizeof(struct fraglist)); |
58 |
gregl |
3.4 |
else |
59 |
gregl |
3.16 |
hdfragl = (struct fraglist *)malloc( |
60 |
|
|
(fd+1)*sizeof(struct fraglist)); |
61 |
|
|
if (hdfragl == NULL) |
62 |
gregl |
3.4 |
error(SYSTEM, "out of memory in hdattach"); |
63 |
gregl |
3.16 |
bzero((char *)(hdfragl+nhdfragls), |
64 |
|
|
(fd+1-nhdfragls)*sizeof(struct fraglist)); |
65 |
|
|
nhdfragls = fd+1; |
66 |
gregl |
3.1 |
} |
67 |
gregl |
3.16 |
hdfragl[fd].nlinks++; |
68 |
|
|
hdfragl[fd].flen = lseek(fd, 0L, 2); /* get file length */ |
69 |
gregl |
3.1 |
} |
70 |
|
|
|
71 |
|
|
|
72 |
|
|
/* Do we need a routine to locate file fragments given known occupants? */ |
73 |
|
|
|
74 |
|
|
|
75 |
|
|
hdrelease(fd) /* stop tracking file fragments for some section */ |
76 |
|
|
register int fd; |
77 |
|
|
{ |
78 |
gregl |
3.16 |
if (fd < 0 | fd >= nhdfragls || !hdfragl[fd].nlinks) |
79 |
gregl |
3.1 |
return; |
80 |
gregl |
3.16 |
if (!--hdfragl[fd].nlinks && hdfragl[fd].nfrags) { |
81 |
|
|
free((char *)hdfragl[fd].fi); |
82 |
|
|
hdfragl[fd].fi = NULL; |
83 |
|
|
hdfragl[fd].nfrags = 0; |
84 |
gregl |
3.1 |
} |
85 |
|
|
} |
86 |
|
|
|
87 |
|
|
|
88 |
gregl |
3.6 |
markdirty(hp) /* mark holodeck section directory dirty */ |
89 |
|
|
register HOLO *hp; |
90 |
|
|
{ |
91 |
|
|
static BEAMI smudge = {0, -1}; |
92 |
|
|
|
93 |
|
|
if (hp->dirty) /* already marked? */ |
94 |
|
|
return; |
95 |
|
|
hp->dirty = 1; |
96 |
|
|
if (lseek(hp->fd, biglob(hp)->fo+(nbeams(hp)-1)*sizeof(BEAMI), 0) < 0 |
97 |
|
|
|| write(hp->fd, (char *)&smudge, |
98 |
|
|
sizeof(BEAMI)) != sizeof(BEAMI)) |
99 |
|
|
error(SYSTEM, "seek/write error in markdirty"); |
100 |
|
|
} |
101 |
|
|
|
102 |
|
|
|
103 |
gregl |
3.1 |
HOLO * |
104 |
|
|
hdinit(fd, hproto) /* initialize a holodeck section in a file */ |
105 |
|
|
int fd; /* corresponding file descriptor */ |
106 |
|
|
HDGRID *hproto; /* holodeck section grid */ |
107 |
|
|
{ |
108 |
|
|
long fpos; |
109 |
|
|
register HOLO *hp; |
110 |
|
|
register int n; |
111 |
|
|
/* prepare for system errors */ |
112 |
|
|
errno = 0; |
113 |
|
|
if ((fpos = lseek(fd, 0L, 1)) < 0) |
114 |
|
|
error(SYSTEM, "cannot determine holodeck file position"); |
115 |
|
|
if (hproto == NULL) { /* assume we're loading it */ |
116 |
|
|
HDGRID hpr; |
117 |
|
|
/* load header */ |
118 |
|
|
if (read(fd, (char *)&hpr, sizeof(HDGRID)) != sizeof(HDGRID)) |
119 |
|
|
error(SYSTEM, "cannot load holodeck header"); |
120 |
|
|
/* allocate grid */ |
121 |
|
|
if ((hp = hdalloc(&hpr)) == NULL) |
122 |
|
|
goto memerr; |
123 |
|
|
/* load beam directory */ |
124 |
|
|
n = nbeams(hp)*sizeof(BEAMI); |
125 |
|
|
if (read(fd, (char *)(hp->bi+1), n) != n) |
126 |
|
|
error(SYSTEM, "failure loading holodeck directory"); |
127 |
gregl |
3.6 |
/* check that it's clean */ |
128 |
|
|
if (hp->bi[nbeams(hp)].fo < 0) |
129 |
|
|
error(USER, "dirty holodeck section"); |
130 |
gregl |
3.1 |
} else { /* assume we're creating it */ |
131 |
|
|
if ((hp = hdalloc(hproto)) == NULL) |
132 |
|
|
goto memerr; |
133 |
|
|
/* write header and skeleton */ |
134 |
|
|
n = nbeams(hp)*sizeof(BEAMI); |
135 |
|
|
if (write(fd, (char *)hproto, sizeof(HDGRID)) != |
136 |
|
|
sizeof(HDGRID) || |
137 |
|
|
write(fd, (char *)(hp->bi+1), n) != n) |
138 |
|
|
error(SYSTEM, "cannot write header to holodeck file"); |
139 |
|
|
} |
140 |
|
|
hp->fd = fd; |
141 |
|
|
hp->dirty = 0; |
142 |
|
|
biglob(hp)->fo = fpos + sizeof(HDGRID); |
143 |
gregl |
3.13 |
/* start tracking fragments */ |
144 |
|
|
hdattach(fd); |
145 |
|
|
/* check rays on disk */ |
146 |
|
|
fpos = hdfilen(fd); |
147 |
|
|
biglob(hp)->nrd = 0; |
148 |
|
|
for (n = hproto == NULL ? nbeams(hp) : 0; n > 0; n--) |
149 |
|
|
if (hp->bi[n].nrd) |
150 |
|
|
if (hp->bi[n].fo + hp->bi[n].nrd > fpos) |
151 |
|
|
hp->bi[n].nrd = 0; /* off end */ |
152 |
|
|
else |
153 |
|
|
biglob(hp)->nrd += hp->bi[n].nrd; |
154 |
gregl |
3.1 |
/* add to holodeck list */ |
155 |
|
|
for (n = 0; n < HDMAX; n++) |
156 |
|
|
if (hdlist[n] == NULL) { |
157 |
|
|
hdlist[n] = hp; |
158 |
|
|
break; |
159 |
|
|
} |
160 |
|
|
/* all done */ |
161 |
|
|
return(hp); |
162 |
|
|
memerr: |
163 |
|
|
error(SYSTEM, "cannot allocate holodeck grid"); |
164 |
|
|
} |
165 |
|
|
|
166 |
|
|
|
167 |
|
|
int |
168 |
gregl |
3.12 |
hdsync(hp, all) /* update beams and directory on disk */ |
169 |
gregl |
3.1 |
register HOLO *hp; |
170 |
gregl |
3.12 |
int all; |
171 |
gregl |
3.1 |
{ |
172 |
|
|
register int j, n; |
173 |
|
|
|
174 |
gregl |
3.12 |
if (hp == NULL) { /* do all holodecks */ |
175 |
gregl |
3.1 |
n = 0; |
176 |
|
|
for (j = 0; hdlist[j] != NULL; j++) |
177 |
gregl |
3.12 |
n += hdsync(hdlist[j], all); |
178 |
gregl |
3.1 |
return(n); |
179 |
|
|
} |
180 |
gregl |
3.12 |
/* sync the beams */ |
181 |
gregl |
3.15 |
for (j = (all ? nbeams(hp) : 0); j > 0; j--) |
182 |
gregl |
3.12 |
if (hp->bl[j] != NULL) |
183 |
|
|
hdsyncbeam(hp, j); |
184 |
gregl |
3.13 |
if (!hp->dirty) /* directory clean? */ |
185 |
gregl |
3.1 |
return(0); |
186 |
gregl |
3.3 |
errno = 0; |
187 |
gregl |
3.1 |
if (lseek(hp->fd, biglob(hp)->fo, 0) < 0) |
188 |
|
|
error(SYSTEM, "cannot seek on holodeck file"); |
189 |
|
|
n = nbeams(hp)*sizeof(BEAMI); |
190 |
|
|
if (write(hp->fd, (char *)(hp->bi+1), n) != n) |
191 |
|
|
error(SYSTEM, "cannot update holodeck section directory"); |
192 |
|
|
hp->dirty = 0; |
193 |
|
|
return(1); |
194 |
|
|
} |
195 |
|
|
|
196 |
|
|
|
197 |
gregl |
3.14 |
unsigned |
198 |
gregl |
3.1 |
hdmemuse(all) /* return memory usage (in bytes) */ |
199 |
|
|
int all; /* include overhead (painful) */ |
200 |
|
|
{ |
201 |
|
|
long total = 0; |
202 |
|
|
register int i, j; |
203 |
|
|
|
204 |
|
|
for (j = 0; hdlist[j] != NULL; j++) { |
205 |
|
|
total += blglob(hdlist[j])->nrm * sizeof(RAYVAL); |
206 |
|
|
if (all) { |
207 |
|
|
total += sizeof(HOLO) + sizeof(BEAM *) + |
208 |
|
|
nbeams(hdlist[j]) * |
209 |
|
|
(sizeof(BEAM *)+sizeof(BEAMI)); |
210 |
|
|
for (i = nbeams(hdlist[j]); i > 0; i--) |
211 |
|
|
if (hdlist[j]->bl[i] != NULL) |
212 |
|
|
total += sizeof(BEAM); |
213 |
|
|
} |
214 |
|
|
} |
215 |
|
|
if (all) |
216 |
gregl |
3.16 |
for (j = 0; j < nhdfragls; j++) { |
217 |
|
|
total += sizeof(struct fraglist); |
218 |
|
|
if (hdfragl[j].nfrags) |
219 |
gregl |
3.4 |
total += FRAGBLK*sizeof(BEAMI) * |
220 |
gregl |
3.16 |
((hdfragl[j].nfrags-1)/FRAGBLK + 1) ; |
221 |
gregl |
3.4 |
} |
222 |
gregl |
3.1 |
return(total); |
223 |
gregl |
3.8 |
} |
224 |
|
|
|
225 |
|
|
|
226 |
|
|
long |
227 |
|
|
hdfilen(fd) /* return file length for fd */ |
228 |
|
|
int fd; |
229 |
|
|
{ |
230 |
|
|
long fpos, flen; |
231 |
|
|
|
232 |
|
|
if (fd < 0) |
233 |
|
|
return(-1); |
234 |
gregl |
3.16 |
if (fd >= nhdfragls || !hdfragl[fd].nlinks) { |
235 |
gregl |
3.8 |
if ((fpos = lseek(fd, 0L, 1)) < 0) |
236 |
|
|
return(-1); |
237 |
|
|
flen = lseek(fd, 0L, 2); |
238 |
|
|
lseek(fd, fpos, 0); |
239 |
|
|
return(flen); |
240 |
|
|
} |
241 |
gregl |
3.16 |
return(hdfragl[fd].flen); |
242 |
gregl |
3.1 |
} |
243 |
|
|
|
244 |
|
|
|
245 |
|
|
long |
246 |
|
|
hdfiluse(fd, all) /* compute file usage (in bytes) */ |
247 |
|
|
int fd; /* open file descriptor to check */ |
248 |
|
|
int all; /* include overhead and unflushed data */ |
249 |
|
|
{ |
250 |
|
|
long total = 0; |
251 |
|
|
register int i, j; |
252 |
|
|
|
253 |
|
|
for (j = 0; hdlist[j] != NULL; j++) { |
254 |
|
|
if (hdlist[j]->fd != fd) |
255 |
|
|
continue; |
256 |
|
|
total += biglob(hdlist[j])->nrd * sizeof(RAYVAL); |
257 |
|
|
if (all) { |
258 |
|
|
for (i = nbeams(hdlist[j]); i > 0; i--) |
259 |
|
|
if (hdlist[j]->bl[i] != NULL) |
260 |
|
|
total += sizeof(RAYVAL) * |
261 |
|
|
(hdlist[j]->bl[i]->nrm - |
262 |
|
|
hdlist[j]->bi[i].nrd); |
263 |
|
|
total += sizeof(HDGRID) + |
264 |
|
|
nbeams(hdlist[j])*sizeof(BEAMI); |
265 |
|
|
} |
266 |
|
|
} |
267 |
|
|
return(total); /* does not include fragments */ |
268 |
|
|
} |
269 |
|
|
|
270 |
|
|
|
271 |
|
|
RAYVAL * |
272 |
|
|
hdnewrays(hp, i, nr) /* allocate space for add'l rays and return pointer */ |
273 |
|
|
register HOLO *hp; |
274 |
|
|
register int i; |
275 |
|
|
int nr; /* number of new rays desired */ |
276 |
|
|
{ |
277 |
|
|
RAYVAL *p; |
278 |
|
|
int n; |
279 |
|
|
|
280 |
|
|
if (nr <= 0) |
281 |
|
|
return(NULL); |
282 |
|
|
if (i < 1 | i > nbeams(hp)) |
283 |
|
|
error(CONSISTENCY, "bad beam index given to hdnewrays"); |
284 |
|
|
if (hp->bl[i] != NULL) |
285 |
|
|
hp->bl[i]->tick = hdclock; /* preempt swap */ |
286 |
|
|
if (hdcachesize > 0 && hdmemuse(0) >= hdcachesize) |
287 |
|
|
hdfreecache(PCTFREE, NULL); /* free some space */ |
288 |
gregl |
3.3 |
errno = 0; |
289 |
gregl |
3.1 |
if (hp->bl[i] == NULL) { /* allocate (and load) */ |
290 |
|
|
n = hp->bi[i].nrd + nr; |
291 |
|
|
if ((hp->bl[i] = (BEAM *)malloc(hdbsiz(n))) == NULL) |
292 |
|
|
goto memerr; |
293 |
|
|
blglob(hp)->nrm += n; |
294 |
|
|
if (n = hp->bl[i]->nrm = hp->bi[i].nrd) { |
295 |
|
|
if (lseek(hp->fd, hp->bi[i].fo, 0) < 0) |
296 |
|
|
error(SYSTEM, "seek error on holodeck file"); |
297 |
|
|
n *= sizeof(RAYVAL); |
298 |
|
|
if (read(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) |
299 |
|
|
error(SYSTEM, |
300 |
|
|
"error reading beam from holodeck file"); |
301 |
|
|
} |
302 |
|
|
} else { /* just grow in memory */ |
303 |
|
|
hp->bl[i] = (BEAM *)realloc( (char *)hp->bl[i], |
304 |
|
|
hdbsiz(hp->bl[i]->nrm + nr) ); |
305 |
|
|
if (hp->bl[i] == NULL) |
306 |
|
|
goto memerr; |
307 |
|
|
blglob(hp)->nrm += nr; |
308 |
|
|
} |
309 |
|
|
p = hdbray(hp->bl[i]) + hp->bl[i]->nrm; |
310 |
|
|
hp->bl[i]->nrm += nr; /* update in-core structure */ |
311 |
|
|
bzero((char *)p, nr*sizeof(RAYVAL)); |
312 |
gregl |
3.14 |
hp->bl[i]->tick = hdclock; /* update LRU clock */ |
313 |
|
|
blglob(hp)->tick = hdclock++; |
314 |
gregl |
3.1 |
return(p); /* point to new rays */ |
315 |
|
|
memerr: |
316 |
|
|
error(SYSTEM, "out of memory in hdnewrays"); |
317 |
|
|
} |
318 |
|
|
|
319 |
|
|
|
320 |
|
|
BEAM * |
321 |
|
|
hdgetbeam(hp, i) /* get beam (from file if necessary) */ |
322 |
|
|
register HOLO *hp; |
323 |
|
|
register int i; |
324 |
|
|
{ |
325 |
|
|
register int n; |
326 |
|
|
|
327 |
|
|
if (i < 1 | i > nbeams(hp)) |
328 |
|
|
error(CONSISTENCY, "bad beam index given to hdgetbeam"); |
329 |
|
|
if (hp->bl[i] == NULL) { /* load from disk */ |
330 |
|
|
if (!(n = hp->bi[i].nrd)) |
331 |
|
|
return(NULL); |
332 |
|
|
if (hdcachesize > 0 && hdmemuse(0) >= hdcachesize) |
333 |
|
|
hdfreecache(PCTFREE, NULL); /* get free space */ |
334 |
|
|
errno = 0; |
335 |
|
|
if ((hp->bl[i] = (BEAM *)malloc(hdbsiz(n))) == NULL) |
336 |
|
|
error(SYSTEM, "cannot allocate memory for beam"); |
337 |
|
|
blglob(hp)->nrm += hp->bl[i]->nrm = n; |
338 |
|
|
if (lseek(hp->fd, hp->bi[i].fo, 0) < 0) |
339 |
|
|
error(SYSTEM, "seek error on holodeck file"); |
340 |
|
|
n *= sizeof(RAYVAL); |
341 |
|
|
if (read(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) |
342 |
|
|
error(SYSTEM, "error reading beam from holodeck file"); |
343 |
|
|
} |
344 |
gregl |
3.14 |
hp->bl[i]->tick = hdclock; /* update LRU clock */ |
345 |
|
|
blglob(hp)->tick = hdclock++; |
346 |
gregl |
3.1 |
return(hp->bl[i]); |
347 |
|
|
} |
348 |
|
|
|
349 |
|
|
|
350 |
|
|
int |
351 |
gregl |
3.17 |
hdfilord(hb1, hb2) /* order beams for quick loading */ |
352 |
gregl |
3.14 |
register HDBEAMI *hb1, *hb2; |
353 |
|
|
{ |
354 |
gregl |
3.17 |
register long c; |
355 |
|
|
/* residents go first */ |
356 |
|
|
if (hb2->h->bl[hb2->b] != NULL) |
357 |
|
|
return(hb1->h->bl[hb1->b] == NULL); |
358 |
|
|
if (hb1->h->bl[hb1->b] != NULL) |
359 |
|
|
return(-1); |
360 |
|
|
/* otherwise sort by file descriptor */ |
361 |
gregl |
3.14 |
if ((c = hb1->h->fd - hb2->h->fd)) |
362 |
|
|
return(c); |
363 |
|
|
/* then by position in file */ |
364 |
gregl |
3.17 |
c = hb1->h->bi[hb1->b].fo - hb2->h->bi[hb2->b].fo; |
365 |
|
|
return(c > 0 ? 1 : c < 0 ? -1 : 0); |
366 |
gregl |
3.14 |
} |
367 |
|
|
|
368 |
|
|
|
369 |
|
|
hdloadbeams(hb, n, bf) /* load a list of beams in optimal order */ |
370 |
|
|
register HDBEAMI *hb; /* list gets sorted by hdfilord() */ |
371 |
|
|
int n; /* list length */ |
372 |
|
|
int (*bf)(); /* callback function (optional) */ |
373 |
|
|
{ |
374 |
|
|
unsigned origcachesize, memuse; |
375 |
|
|
register BEAM *bp; |
376 |
|
|
int bytesloaded, needbytes, bytes2free; |
377 |
|
|
register int i; |
378 |
|
|
/* precheck consistency */ |
379 |
|
|
for (i = n; i--; ) |
380 |
|
|
if (hb[i].h == NULL || hb[i].b < 1 | hb[i].b > nbeams(hb[i].h)) |
381 |
|
|
error(CONSISTENCY, "bad beam in hdloadbeams"); |
382 |
|
|
/* sort list for optimal access */ |
383 |
|
|
qsort((char *)hb, n, sizeof(HDBEAMI), hdfilord); |
384 |
gregl |
3.17 |
bytesloaded = 0; /* run through loaded beams */ |
385 |
|
|
for ( ; n && (bp = hb->h->bl[hb->b]) != NULL; n--, hb++) { |
386 |
|
|
bp->tick = hdclock; /* preempt swap */ |
387 |
|
|
bytesloaded += bp->nrm; |
388 |
|
|
if (bf != NULL) |
389 |
|
|
(*bf)(bp, hb->h, hb->b); |
390 |
|
|
} |
391 |
|
|
bytesloaded *= sizeof(RAYVAL); |
392 |
gregl |
3.15 |
if ((origcachesize = hdcachesize) > 0) { |
393 |
gregl |
3.17 |
needbytes = 0; /* figure out memory needs */ |
394 |
gregl |
3.15 |
for (i = n; i--; ) |
395 |
gregl |
3.17 |
needbytes += hb[i].h->bi[hb[i].b].nrd; |
396 |
gregl |
3.15 |
needbytes *= sizeof(RAYVAL); |
397 |
|
|
do { /* free enough memory */ |
398 |
|
|
memuse = hdmemuse(0); |
399 |
gregl |
3.17 |
bytes2free = needbytes - (int)(hdcachesize-memuse); |
400 |
|
|
if (bytes2free > (int)(memuse - bytesloaded)) |
401 |
gregl |
3.15 |
bytes2free = memuse - bytesloaded; |
402 |
|
|
} while (bytes2free > 0 && |
403 |
|
|
hdfreecache(100*bytes2free/memuse, NULL) < 0); |
404 |
gregl |
3.17 |
hdcachesize = 0; /* load beams w/o swap */ |
405 |
gregl |
3.15 |
} |
406 |
gregl |
3.14 |
for (i = 0; i < n; i++) |
407 |
|
|
if ((bp = hdgetbeam(hb[i].h, hb[i].b)) != NULL && bf != NULL) |
408 |
|
|
(*bf)(bp, hb[i].h, hb[i].b); |
409 |
|
|
hdcachesize = origcachesize; /* resume dynamic swapping */ |
410 |
|
|
} |
411 |
|
|
|
412 |
|
|
|
413 |
|
|
int |
414 |
gregl |
3.12 |
hdsyncbeam(hp, i) /* sync beam in memory with beam on disk */ |
415 |
gregl |
3.1 |
register HOLO *hp; |
416 |
|
|
register int i; |
417 |
|
|
{ |
418 |
gregl |
3.12 |
unsigned int nrays; |
419 |
gregl |
3.11 |
long nfo; |
420 |
|
|
unsigned int n; |
421 |
gregl |
3.13 |
/* check file status */ |
422 |
gregl |
3.16 |
if (hdfragl[hp->fd].writerr) |
423 |
gregl |
3.13 |
return(-1); |
424 |
gregl |
3.12 |
#ifdef DEBUG |
425 |
|
|
if (i < 1 | i > nbeams(hp)) |
426 |
|
|
error(CONSISTENCY, "bad beam index in hdsyncbeam"); |
427 |
|
|
#endif |
428 |
gregl |
3.11 |
/* is current fragment OK? */ |
429 |
gregl |
3.12 |
if (hp->bl[i] == NULL || (nrays = hp->bl[i]->nrm) == hp->bi[i].nrd) |
430 |
gregl |
3.1 |
return(0); |
431 |
gregl |
3.13 |
/* locate fragment */ |
432 |
gregl |
3.18 |
if (hp->bi[i].fo + hp->bi[i].nrd*sizeof(RAYVAL) == |
433 |
gregl |
3.16 |
hdfragl[hp->fd].flen) /* EOF special case */ |
434 |
|
|
hdfragl[hp->fd].flen = (nfo=hp->bi[i].fo) + nrays*sizeof(RAYVAL); |
435 |
gregl |
3.1 |
|
436 |
|
|
else { /* general case */ |
437 |
gregl |
3.16 |
register struct fraglist *f = &hdfragl[hp->fd]; |
438 |
gregl |
3.1 |
register int j, k; |
439 |
gregl |
3.16 |
n = f->nfrags; /* relinquish old fragment */ |
440 |
gregl |
3.2 |
if (hp->bi[i].nrd) { |
441 |
gregl |
3.5 |
j = f->nfrags++; |
442 |
gregl |
3.15 |
#if MAXFRAG |
443 |
gregl |
3.5 |
if (j >= MAXFRAG-1) |
444 |
gregl |
3.2 |
f->nfrags--; |
445 |
gregl |
3.4 |
#endif |
446 |
gregl |
3.5 |
if (j % FRAGBLK == 0) { /* more frag. space */ |
447 |
gregl |
3.4 |
if (f->fi == NULL) |
448 |
|
|
f->fi = (BEAMI *)malloc( |
449 |
|
|
FRAGBLK*sizeof(BEAMI)); |
450 |
|
|
else |
451 |
|
|
f->fi = (BEAMI *)realloc((char *)f->fi, |
452 |
gregl |
3.5 |
(j+FRAGBLK)*sizeof(BEAMI)); |
453 |
gregl |
3.4 |
if (f->fi == NULL) |
454 |
|
|
error(SYSTEM, |
455 |
gregl |
3.12 |
"out of memory in hdsyncbeam"); |
456 |
gregl |
3.4 |
} |
457 |
gregl |
3.5 |
for ( ; ; j--) { /* insert in descending list */ |
458 |
|
|
if (!j || hp->bi[i].fo < f->fi[j-1].fo) { |
459 |
gregl |
3.2 |
f->fi[j].fo = hp->bi[i].fo; |
460 |
|
|
f->fi[j].nrd = hp->bi[i].nrd; |
461 |
|
|
break; |
462 |
|
|
} |
463 |
gregl |
3.14 |
copystruct(f->fi+j, f->fi+(j-1)); |
464 |
gregl |
3.1 |
} |
465 |
|
|
/* coalesce adjacent fragments */ |
466 |
gregl |
3.15 |
if (j && f->fi[j-1].fo == f->fi[j].fo + |
467 |
|
|
f->fi[j].nrd*sizeof(RAYVAL)) { |
468 |
|
|
f->fi[j].nrd += f->fi[j-1].nrd; |
469 |
|
|
f->fi[j-1].nrd = 0; |
470 |
gregl |
3.16 |
n = j-1; |
471 |
gregl |
3.2 |
} |
472 |
gregl |
3.15 |
if (j+1 < f->nfrags && f->fi[j].fo == f->fi[j+1].fo + |
473 |
|
|
f->fi[j+1].nrd*sizeof(RAYVAL)) { |
474 |
|
|
f->fi[j+1].nrd += f->fi[j].nrd; |
475 |
|
|
f->fi[j].nrd = 0; |
476 |
gregl |
3.16 |
if (j < n) n = j; |
477 |
gregl |
3.15 |
} |
478 |
gregl |
3.1 |
} |
479 |
|
|
k = -1; /* find closest-sized fragment */ |
480 |
gregl |
3.15 |
for (j = (nrays ? f->nfrags : 0); j-- > 0; ) |
481 |
gregl |
3.1 |
if (f->fi[j].nrd >= nrays && |
482 |
gregl |
3.3 |
(k < 0 || f->fi[j].nrd < f->fi[k].nrd)) |
483 |
|
|
if (f->fi[k=j].nrd == nrays) |
484 |
|
|
break; |
485 |
gregl |
3.1 |
if (k < 0) { /* no fragment -- extend file */ |
486 |
gregl |
3.11 |
nfo = f->flen; |
487 |
gregl |
3.1 |
f->flen += nrays*sizeof(RAYVAL); |
488 |
|
|
} else { /* else use fragment */ |
489 |
gregl |
3.11 |
nfo = f->fi[k].fo; |
490 |
gregl |
3.15 |
f->fi[k].fo += nrays*sizeof(RAYVAL); |
491 |
gregl |
3.16 |
if (!(f->fi[k].nrd -= nrays) && k < n) |
492 |
|
|
n = k; |
493 |
gregl |
3.1 |
} |
494 |
gregl |
3.15 |
/* delete empty remnants */ |
495 |
gregl |
3.16 |
for (j = k = n; k < f->nfrags; j++, k++) { |
496 |
gregl |
3.15 |
while (f->fi[k].nrd == 0) |
497 |
|
|
if (++k >= f->nfrags) |
498 |
|
|
goto endloop; |
499 |
|
|
if (k > j) |
500 |
|
|
copystruct(f->fi+j, f->fi+k); |
501 |
|
|
} |
502 |
|
|
endloop: |
503 |
|
|
f->nfrags = j; |
504 |
gregl |
3.1 |
} |
505 |
gregl |
3.11 |
if (nrays) { /* write the new fragment */ |
506 |
|
|
errno = 0; |
507 |
|
|
if (lseek(hp->fd, nfo, 0) < 0) |
508 |
|
|
error(SYSTEM, "cannot seek on holodeck file"); |
509 |
|
|
n = hp->bl[i]->nrm * sizeof(RAYVAL); |
510 |
|
|
if (write(hp->fd, (char *)hdbray(hp->bl[i]), n) != n) { |
511 |
gregl |
3.16 |
hdfragl[hp->fd].writerr++; |
512 |
gregl |
3.18 |
hdsync(NULL, 0); /* sync directories */ |
513 |
gregl |
3.12 |
error(SYSTEM, "write error in hdsyncbeam"); |
514 |
gregl |
3.11 |
} |
515 |
|
|
} |
516 |
gregl |
3.1 |
biglob(hp)->nrd += nrays - hp->bi[i].nrd; |
517 |
|
|
hp->bi[i].nrd = nrays; |
518 |
gregl |
3.11 |
hp->bi[i].fo = nfo; |
519 |
gregl |
3.6 |
markdirty(hp); /* section directory now out of date */ |
520 |
gregl |
3.1 |
return(1); |
521 |
|
|
} |
522 |
|
|
|
523 |
|
|
|
524 |
|
|
int |
525 |
|
|
hdfreebeam(hp, i) /* free beam, writing if dirty */ |
526 |
|
|
register HOLO *hp; |
527 |
|
|
register int i; |
528 |
|
|
{ |
529 |
gregl |
3.11 |
int nchanged; |
530 |
gregl |
3.1 |
|
531 |
|
|
if (hp == NULL) { /* clear all holodecks */ |
532 |
|
|
nchanged = 0; |
533 |
|
|
for (i = 0; hdlist[i] != NULL; i++) |
534 |
|
|
nchanged += hdfreebeam(hdlist[i], 0); |
535 |
|
|
return(nchanged); |
536 |
|
|
} |
537 |
gregl |
3.16 |
if (hdfragl[hp->fd].writerr) /* check for file error */ |
538 |
gregl |
3.13 |
return(0); |
539 |
gregl |
3.1 |
if (i == 0) { /* clear entire holodeck */ |
540 |
|
|
nchanged = 0; |
541 |
gregl |
3.10 |
for (i = nbeams(hp); i > 0; i--) |
542 |
|
|
if (hp->bl[i] != NULL) |
543 |
|
|
nchanged += hdfreebeam(hp, i); |
544 |
gregl |
3.1 |
return(nchanged); |
545 |
|
|
} |
546 |
gregl |
3.12 |
#ifdef DEBUG |
547 |
gregl |
3.1 |
if (i < 1 | i > nbeams(hp)) |
548 |
|
|
error(CONSISTENCY, "bad beam index to hdfreebeam"); |
549 |
gregl |
3.12 |
#endif |
550 |
gregl |
3.1 |
if (hp->bl[i] == NULL) |
551 |
|
|
return(0); |
552 |
|
|
/* check for additions */ |
553 |
|
|
nchanged = hp->bl[i]->nrm - hp->bi[i].nrd; |
554 |
gregl |
3.11 |
if (nchanged) |
555 |
gregl |
3.12 |
hdsyncbeam(hp, i); /* write new fragment */ |
556 |
gregl |
3.1 |
blglob(hp)->nrm -= hp->bl[i]->nrm; |
557 |
|
|
free((char *)hp->bl[i]); /* free memory */ |
558 |
|
|
hp->bl[i] = NULL; |
559 |
|
|
return(nchanged); |
560 |
|
|
} |
561 |
|
|
|
562 |
|
|
|
563 |
gregl |
3.10 |
int |
564 |
|
|
hdkillbeam(hp, i) /* delete beam from holodeck */ |
565 |
|
|
register HOLO *hp; |
566 |
|
|
register int i; |
567 |
|
|
{ |
568 |
|
|
static BEAM emptybeam; |
569 |
gregl |
3.11 |
int nchanged; |
570 |
gregl |
3.10 |
|
571 |
|
|
if (hp == NULL) { /* clobber all holodecks */ |
572 |
|
|
nchanged = 0; |
573 |
|
|
for (i = 0; hdlist[i] != NULL; i++) |
574 |
|
|
nchanged += hdkillbeam(hdlist[i], 0); |
575 |
|
|
return(nchanged); |
576 |
|
|
} |
577 |
|
|
if (i == 0) { /* clobber entire holodeck */ |
578 |
|
|
nchanged = 0; |
579 |
|
|
for (i = nbeams(hp); i > 0; i--) |
580 |
|
|
if (hp->bi[i].nrd > 0 || hp->bl[i] != NULL) |
581 |
|
|
nchanged += hdkillbeam(hp, i); |
582 |
|
|
#ifdef DEBUG |
583 |
|
|
if (biglob(hp)->nrd != 0 | blglob(hp)->nrm != 0) |
584 |
|
|
error(CONSISTENCY, "bad beam count in hdkillbeam"); |
585 |
|
|
#endif |
586 |
|
|
return(nchanged); |
587 |
|
|
} |
588 |
gregl |
3.12 |
#ifdef DEBUG |
589 |
gregl |
3.10 |
if (i < 1 | i > nbeams(hp)) |
590 |
|
|
error(CONSISTENCY, "bad beam index to hdkillbeam"); |
591 |
gregl |
3.12 |
#endif |
592 |
gregl |
3.10 |
if (hp->bl[i] != NULL) { /* free memory */ |
593 |
|
|
blglob(hp)->nrm -= nchanged = hp->bl[i]->nrm; |
594 |
|
|
free((char *)hp->bl[i]); |
595 |
|
|
} else |
596 |
|
|
nchanged = hp->bi[i].nrd; |
597 |
|
|
if (hp->bi[i].nrd) { /* free file fragment */ |
598 |
|
|
hp->bl[i] = &emptybeam; |
599 |
gregl |
3.12 |
hdsyncbeam(hp, i); |
600 |
gregl |
3.10 |
} |
601 |
|
|
hp->bl[i] = NULL; |
602 |
|
|
return(nchanged); |
603 |
|
|
} |
604 |
|
|
|
605 |
|
|
|
606 |
gregl |
3.14 |
int |
607 |
|
|
hdlrulist(hb, nents, n, hp) /* add beams from holodeck to LRU list */ |
608 |
|
|
register HDBEAMI *hb; /* beam list */ |
609 |
|
|
int nents; /* current list length */ |
610 |
|
|
int n; /* maximum list length */ |
611 |
gregl |
3.1 |
register HOLO *hp; /* section we're adding from */ |
612 |
|
|
{ |
613 |
|
|
register int i, j; |
614 |
|
|
/* insert each beam from hp */ |
615 |
gregl |
3.10 |
for (i = nbeams(hp); i > 0; i--) { |
616 |
gregl |
3.1 |
if (hp->bl[i] == NULL) /* check if loaded */ |
617 |
|
|
continue; |
618 |
gregl |
3.14 |
#if 0 |
619 |
|
|
if (hp->bl[i]->tick == hdclock) /* preempt swap? */ |
620 |
|
|
continue; |
621 |
|
|
#endif |
622 |
|
|
if ((j = ++nents) >= n) /* grow list if we can */ |
623 |
gregl |
3.1 |
nents--; |
624 |
|
|
for ( ; ; ) { /* bubble into place */ |
625 |
|
|
if (!--j || hp->bl[i]->tick >= |
626 |
gregl |
3.14 |
hb[j-1].h->bl[hb[j-1].b]->tick) { |
627 |
|
|
hb[j].h = hp; |
628 |
|
|
hb[j].b = i; |
629 |
gregl |
3.1 |
break; |
630 |
|
|
} |
631 |
gregl |
3.14 |
copystruct(hb+j, hb+(j-1)); |
632 |
gregl |
3.1 |
} |
633 |
|
|
} |
634 |
gregl |
3.14 |
return(nents); /* return new list length */ |
635 |
gregl |
3.1 |
} |
636 |
|
|
|
637 |
|
|
|
638 |
gregl |
3.14 |
int |
639 |
gregl |
3.1 |
hdfreecache(pct, honly) /* free up cache space, writing changes */ |
640 |
|
|
int pct; /* maximum percentage to free */ |
641 |
|
|
register HOLO *honly; /* NULL means check all */ |
642 |
|
|
{ |
643 |
gregl |
3.14 |
HDBEAMI hb[FREEBEAMS]; |
644 |
gregl |
3.1 |
int freetarget; |
645 |
gregl |
3.14 |
int n; |
646 |
gregl |
3.1 |
register int i; |
647 |
|
|
/* compute free target */ |
648 |
|
|
freetarget = (honly != NULL) ? blglob(honly)->nrm : |
649 |
|
|
hdmemuse(0)/sizeof(RAYVAL) ; |
650 |
|
|
freetarget = freetarget*pct/100; |
651 |
gregl |
3.14 |
if (freetarget <= 0) |
652 |
|
|
return(0); |
653 |
gregl |
3.1 |
/* find least recently used */ |
654 |
gregl |
3.14 |
n = 0; |
655 |
gregl |
3.1 |
if (honly != NULL) |
656 |
gregl |
3.14 |
n = hdlrulist(hb, n, FREEBEAMS, honly); |
657 |
gregl |
3.1 |
else |
658 |
|
|
for (i = 0; hdlist[i] != NULL; i++) |
659 |
gregl |
3.14 |
n = hdlrulist(hb, n, FREEBEAMS, hdlist[i]); |
660 |
gregl |
3.1 |
/* free LRU beams */ |
661 |
gregl |
3.14 |
for (i = 0; i < n; i++) { |
662 |
|
|
hdfreebeam(hb[i].h, hb[i].b); |
663 |
|
|
if ((freetarget -= hb[i].h->bi[hb[i].b].nrd) <= 0) |
664 |
gregl |
3.1 |
break; |
665 |
|
|
} |
666 |
gregl |
3.12 |
hdsync(honly, 0); /* synchronize directories as necessary */ |
667 |
gregl |
3.14 |
return(-freetarget); /* return how far past goal we went */ |
668 |
gregl |
3.1 |
} |
669 |
|
|
|
670 |
|
|
|
671 |
|
|
hddone(hp) /* clean up holodeck section and free */ |
672 |
|
|
register HOLO *hp; /* NULL means clean up all */ |
673 |
|
|
{ |
674 |
|
|
register int i; |
675 |
|
|
|
676 |
|
|
if (hp == NULL) { /* NULL means clean up everything */ |
677 |
|
|
while (hdlist[0] != NULL) |
678 |
|
|
hddone(hdlist[0]); |
679 |
|
|
return; |
680 |
|
|
} |
681 |
|
|
/* flush all data and free memory */ |
682 |
|
|
hdflush(hp); |
683 |
|
|
/* release fragment resources */ |
684 |
|
|
hdrelease(hp->fd); |
685 |
|
|
/* remove hp from active list */ |
686 |
|
|
for (i = 0; hdlist[i] != NULL; i++) |
687 |
|
|
if (hdlist[i] == hp) { |
688 |
|
|
while ((hdlist[i] = hdlist[i+1]) != NULL) |
689 |
|
|
i++; |
690 |
|
|
break; |
691 |
|
|
} |
692 |
|
|
free((char *)hp->bl); /* free beam list */ |
693 |
|
|
free((char *)hp); /* free holodeck struct */ |
694 |
|
|
} |