| 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 |
+ |
int chunkycmp = 0; /* clump beams together on disk */ |
| 30 |
+ |
|
| 31 |
|
static PACKHEAD *complist=NULL; /* list of beams to compute */ |
| 32 |
|
static int complen=0; /* length of complist */ |
| 33 |
|
static int listpos=0; /* current list position for next_packet */ |
| 38 |
|
beamcmp(b0, b1) /* comparison for compute order */ |
| 39 |
|
register PACKHEAD *b0, *b1; |
| 40 |
|
{ |
| 41 |
< |
return( b1->nr*(b0->nc+1) - b0->nr*(b1->nc+1) ); |
| 41 |
> |
BEAMI *bip0, *bip1; |
| 42 |
> |
register long c; |
| 43 |
> |
/* first check desired quantities */ |
| 44 |
> |
if (chunkycmp) |
| 45 |
> |
c = rchunk(b1->nr)*(rchunk(b0->nc)+1L) - |
| 46 |
> |
rchunk(b0->nr)*(rchunk(b1->nc)+1L); |
| 47 |
> |
else |
| 48 |
> |
c = b1->nr*(b0->nc+1L) - b0->nr*(b1->nc+1L); |
| 49 |
> |
if (c > 0) return(1); |
| 50 |
> |
if (c < 0) return(-1); |
| 51 |
> |
/* only one file, so skip the following: */ |
| 52 |
> |
#if 0 |
| 53 |
> |
/* next, check file descriptors */ |
| 54 |
> |
c = hdlist[b0->hd]->fd - hdlist[b1->hd]->fd; |
| 55 |
> |
if (c) return(c); |
| 56 |
> |
#endif |
| 57 |
> |
/* finally, check file positions */ |
| 58 |
> |
bip0 = &hdlist[b0->hd]->bi[b0->bi]; |
| 59 |
> |
bip1 = &hdlist[b1->hd]->bi[b1->bi]; |
| 60 |
> |
/* put diskless beams last */ |
| 61 |
> |
if (!bip0->nrd) |
| 62 |
> |
return(bip1->nrd > 0); |
| 63 |
> |
if (!bip1->nrd) |
| 64 |
> |
return(-1); |
| 65 |
> |
c = bip0->fo - bip1->fo; |
| 66 |
> |
return(c < 0 ? -1 : c > 0); |
| 67 |
|
} |
| 68 |
|
|
| 69 |
|
|
| 103 |
|
error(CONSISTENCY, "unregistered holodeck in dispbeam"); |
| 104 |
|
p->bi = hb->b; |
| 105 |
|
disp_packet(p); /* display it */ |
| 106 |
+ |
if (n >= 1024) { /* free ridiculous packets */ |
| 107 |
+ |
free((char *)p); |
| 108 |
+ |
p = NULL; n = 0; |
| 109 |
+ |
} |
| 110 |
|
} |
| 111 |
|
|
| 112 |
|
|
| 203 |
|
default: |
| 204 |
|
error(CONSISTENCY, "bundle_set called with unknown operation"); |
| 205 |
|
} |
| 206 |
< |
if (outdev == NULL) /* nothing to display? */ |
| 206 |
> |
if (outdev == NULL || !nents) /* nothing to display? */ |
| 207 |
|
return; |
| 208 |
|
/* load and display beams we have */ |
| 209 |
|
hbarr = (HDBEAMI *)malloc(nents*sizeof(HDBEAMI)); |
| 213 |
|
} |
| 214 |
|
hdloadbeams(hbarr, nents, dispbeam); |
| 215 |
|
free((char *)hbarr); |
| 216 |
+ |
if (hdfragflags&FF_READ) { |
| 217 |
+ |
listpos = 0; |
| 218 |
+ |
lastin = -1; /* need to re-sort list */ |
| 219 |
+ |
} |
| 220 |
|
return; |
| 221 |
|
memerr: |
| 222 |
|
error(SYSTEM, "out of memory in bundle_set"); |
| 267 |
|
free((char *)complist); |
| 268 |
|
done_packets(flush_queue()); |
| 269 |
|
} |
| 270 |
+ |
/* reseed random number generator */ |
| 271 |
+ |
srandom(time(NULL)); |
| 272 |
|
/* allocate beam list */ |
| 273 |
|
complen = 0; |
| 274 |
|
for (j = 0; hdlist[j] != NULL; j++) |
| 330 |
|
sortcomplist() /* fix our list order */ |
| 331 |
|
{ |
| 332 |
|
PACKHEAD *list2; |
| 333 |
+ |
int listlen; |
| 334 |
|
register int i; |
| 335 |
|
|
| 336 |
|
if (complen <= 0) /* check to see if there is even a list */ |
| 337 |
|
return; |
| 338 |
+ |
if (!chunkycmp) /* check to see if fragment list is full */ |
| 339 |
+ |
if (!hdfragOK(hdlist[0]->fd, &listlen, NULL) |
| 340 |
+ |
#if NFRAG2CHUNK |
| 341 |
+ |
|| listlen >= NFRAG2CHUNK |
| 342 |
+ |
#endif |
| 343 |
+ |
) { |
| 344 |
+ |
chunkycmp++; /* use "chunky" comparison */ |
| 345 |
+ |
lastin = -1; /* need to re-sort list */ |
| 346 |
+ |
#ifdef DEBUG |
| 347 |
+ |
error(WARNING, "using chunky comparison mode"); |
| 348 |
+ |
#endif |
| 349 |
+ |
} |
| 350 |
|
if (lastin < 0 || listpos*4 >= complen*3) |
| 351 |
|
qsort((char *)complist, complen, sizeof(PACKHEAD), beamcmp); |
| 352 |
|
else if (listpos) { /* else sort and merge sublist */ |
| 382 |
|
* more or less evenly distributed, such that computing a packet causes |
| 383 |
|
* a given bundle to move way down in the computation order. We keep |
| 384 |
|
* track of where the computed bundle with the highest priority would end |
| 385 |
< |
* up, and if we get further in our compute list than this, we resort the |
| 385 |
> |
* up, and if we get further in our compute list than this, we re-sort the |
| 386 |
|
* list and start again from the beginning. Since |
| 387 |
|
* a merge sort is used, the sorting costs are minimal. |
| 388 |
|
*/ |
| 402 |
|
p->nr = complist[listpos].nr - p->nc; |
| 403 |
|
if (p->nr <= 0) |
| 404 |
|
return(0); |
| 405 |
< |
#ifdef DEBUG |
| 406 |
< |
if (n < 1 | n > RPACKSIZ) |
| 344 |
< |
error(CONSISTENCY, "next_packet called with bad n value"); |
| 345 |
< |
#endif |
| 405 |
> |
DCHECK(n < 1 | n > RPACKSIZ, |
| 406 |
> |
CONSISTENCY, "next_packet called with bad n value"); |
| 407 |
|
if (p->nr > n) |
| 408 |
|
p->nr = n; |
| 409 |
|
complist[listpos].nc += p->nr; /* find where this one would go */ |
| 410 |
+ |
if (hdgetbeam(hdlist[p->hd], p->bi) != NULL) |
| 411 |
+ |
hdfreefrag(hdlist[p->hd], p->bi); |
| 412 |
|
while (lastin > listpos && |
| 413 |
|
beamcmp(complist+lastin, complist+listpos) > 0) |
| 414 |
|
lastin--; |