9 |
|
*/ |
10 |
|
|
11 |
|
#include "rholo.h" |
12 |
+ |
#include <sys/types.h> |
13 |
|
|
14 |
+ |
#ifndef NFRAG2CHUNK |
15 |
+ |
#define NFRAG2CHUNK 4096 /* number of fragments to start chunking */ |
16 |
+ |
#endif |
17 |
+ |
|
18 |
+ |
#ifndef abs |
19 |
|
#define abs(x) ((x) > 0 ? (x) : -(x)) |
20 |
+ |
#endif |
21 |
+ |
#ifndef sgn |
22 |
|
#define sgn(x) ((x) > 0 ? 1 : (x) < 0 ? -1 : 0) |
23 |
+ |
#endif |
24 |
|
|
25 |
+ |
#define rchunk(n) (((n)+(RPACKSIZ/2))/RPACKSIZ) |
26 |
+ |
|
27 |
+ |
extern time_t time(); |
28 |
+ |
|
29 |
|
static PACKHEAD *complist=NULL; /* list of beams to compute */ |
30 |
|
static int complen=0; /* length of complist */ |
31 |
|
static int listpos=0; /* current list position for next_packet */ |
32 |
|
static int lastin= -1; /* last ordered position in list */ |
33 |
+ |
static int chunky=0; /* clump beams together on disk */ |
34 |
|
|
35 |
|
|
36 |
|
int |
37 |
|
beamcmp(b0, b1) /* comparison for compute order */ |
38 |
|
register PACKHEAD *b0, *b1; |
39 |
|
{ |
40 |
< |
return( b1->nr*(b0->nc+1) - b0->nr*(b1->nc+1) ); |
40 |
> |
BEAMI *bip0, *bip1; |
41 |
> |
register long c; |
42 |
> |
/* first check desired quantities */ |
43 |
> |
if (chunky) |
44 |
> |
c = rchunk(b1->nr)*(rchunk(b0->nc)+1L) - |
45 |
> |
rchunk(b0->nr)*(rchunk(b1->nc)+1L); |
46 |
> |
else |
47 |
> |
c = b1->nr*(b0->nc+1L) - b0->nr*(b1->nc+1L); |
48 |
> |
if (c > 0) return(1); |
49 |
> |
if (c < 0) return(-1); |
50 |
> |
/* only one file, so skip the following: */ |
51 |
> |
#if 0 |
52 |
> |
/* next, check file descriptors */ |
53 |
> |
c = hdlist[b0->hd]->fd - hdlist[b1->hd]->fd; |
54 |
> |
if (c) return(c); |
55 |
> |
#endif |
56 |
> |
/* finally, check file positions */ |
57 |
> |
bip0 = &hdlist[b0->hd]->bi[b0->bi]; |
58 |
> |
bip1 = &hdlist[b1->hd]->bi[b1->bi]; |
59 |
> |
/* put diskless beams last */ |
60 |
> |
if (!bip0->nrd) |
61 |
> |
return(bip1->nrd > 0); |
62 |
> |
if (!bip1->nrd) |
63 |
> |
return(-1); |
64 |
> |
c = bip0->fo - bip1->fo; |
65 |
> |
return(c < 0 ? -1 : c > 0); |
66 |
|
} |
67 |
|
|
68 |
|
|
102 |
|
error(CONSISTENCY, "unregistered holodeck in dispbeam"); |
103 |
|
p->bi = hb->b; |
104 |
|
disp_packet(p); /* display it */ |
105 |
+ |
if (n >= 1024) { /* free ridiculous packets */ |
106 |
+ |
free((char *)p); |
107 |
+ |
p = NULL; n = 0; |
108 |
+ |
} |
109 |
|
} |
110 |
|
|
111 |
|
|
202 |
|
default: |
203 |
|
error(CONSISTENCY, "bundle_set called with unknown operation"); |
204 |
|
} |
205 |
< |
if (outdev == NULL) /* nothing to display? */ |
205 |
> |
if (outdev == NULL || !nents) /* nothing to display? */ |
206 |
|
return; |
207 |
|
/* load and display beams we have */ |
208 |
|
hbarr = (HDBEAMI *)malloc(nents*sizeof(HDBEAMI)); |
212 |
|
} |
213 |
|
hdloadbeams(hbarr, nents, dispbeam); |
214 |
|
free((char *)hbarr); |
215 |
+ |
if (hdfragflags&FF_READ) { |
216 |
+ |
listpos = 0; |
217 |
+ |
lastin = -1; /* need to re-sort list */ |
218 |
+ |
} |
219 |
|
return; |
220 |
|
memerr: |
221 |
|
error(SYSTEM, "out of memory in bundle_set"); |
266 |
|
free((char *)complist); |
267 |
|
done_packets(flush_queue()); |
268 |
|
} |
269 |
+ |
/* reseed random number generator */ |
270 |
+ |
srandom(time(NULL)); |
271 |
|
/* allocate beam list */ |
272 |
|
complen = 0; |
273 |
|
for (j = 0; hdlist[j] != NULL; j++) |
329 |
|
sortcomplist() /* fix our list order */ |
330 |
|
{ |
331 |
|
PACKHEAD *list2; |
332 |
+ |
int listlen; |
333 |
|
register int i; |
334 |
|
|
335 |
|
if (complen <= 0) /* check to see if there is even a list */ |
336 |
|
return; |
337 |
+ |
if (!chunky) /* check to see if fragment list is full */ |
338 |
+ |
if (!hdfragOK(hdlist[0]->fd, &listlen, NULL) |
339 |
+ |
#if NFRAG2CHUNK |
340 |
+ |
|| listlen >= NFRAG2CHUNK |
341 |
+ |
#endif |
342 |
+ |
) { |
343 |
+ |
#ifdef DEBUG |
344 |
+ |
error(WARNING, "using chunky comparison mode"); |
345 |
+ |
#endif |
346 |
+ |
chunky++; /* use "chunky" comparison */ |
347 |
+ |
lastin = -1; /* need to re-sort list */ |
348 |
+ |
} |
349 |
+ |
#ifdef DEBUG |
350 |
+ |
else |
351 |
+ |
fprintf(stderr, "sortcomplist: %d fragments\n", |
352 |
+ |
listlen); |
353 |
+ |
#endif |
354 |
|
if (lastin < 0 || listpos*4 >= complen*3) |
355 |
|
qsort((char *)complist, complen, sizeof(PACKHEAD), beamcmp); |
356 |
|
else if (listpos) { /* else sort and merge sublist */ |
373 |
|
} else if (i < complen-1) { |
374 |
|
list2 = (PACKHEAD *)realloc((char *)complist, |
375 |
|
(i+1)*sizeof(PACKHEAD)); |
376 |
< |
if (list2 != NULL) { |
376 |
> |
if (list2 != NULL) |
377 |
|
complist = list2; |
378 |
< |
complen = i+1; |
312 |
< |
} |
378 |
> |
complen = i+1; |
379 |
|
} |
380 |
|
listpos = 0; lastin = i; |
381 |
|
} |
386 |
|
* more or less evenly distributed, such that computing a packet causes |
387 |
|
* a given bundle to move way down in the computation order. We keep |
388 |
|
* track of where the computed bundle with the highest priority would end |
389 |
< |
* up, and if we get further in our compute list than this, we resort the |
389 |
> |
* up, and if we get further in our compute list than this, we re-sort the |
390 |
|
* list and start again from the beginning. Since |
391 |
|
* a merge sort is used, the sorting costs are minimal. |
392 |
|
*/ |
406 |
|
p->nr = complist[listpos].nr - p->nc; |
407 |
|
if (p->nr <= 0) |
408 |
|
return(0); |
409 |
< |
#ifdef DEBUG |
410 |
< |
if (n < 1 | n > RPACKSIZ) |
345 |
< |
error(CONSISTENCY, "next_packet called with bad n value"); |
346 |
< |
#endif |
409 |
> |
DCHECK(n < 1 | n > RPACKSIZ, |
410 |
> |
CONSISTENCY, "next_packet called with bad n value"); |
411 |
|
if (p->nr > n) |
412 |
|
p->nr = n; |
413 |
|
complist[listpos].nc += p->nr; /* find where this one would go */ |
414 |
+ |
if (hdgetbeam(hdlist[p->hd], p->bi) != NULL) |
415 |
+ |
hdfreefrag(hdlist[p->hd], p->bi); |
416 |
|
while (lastin > listpos && |
417 |
|
beamcmp(complist+lastin, complist+listpos) > 0) |
418 |
|
lastin--; |