ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/ambient.c
Revision: 2.35
Committed: Tue Nov 21 14:28:22 1995 UTC (28 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.34: +6 -3 lines
Log Message:
improved logic for when to track time on multiprocessors

File Contents

# User Rev Content
1 greg 2.27 /* Copyright (c) 1995 Regents of the University of California */
2 greg 1.1
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ LBL";
5     #endif
6    
7     /*
8     * ambient.c - routines dealing with ambient (inter-reflected) component.
9     */
10    
11     #include "ray.h"
12    
13     #include "octree.h"
14    
15 greg 1.8 #include "otypes.h"
16    
17 greg 1.14 #include "ambient.h"
18    
19 greg 1.1 #include "random.h"
20    
21 greg 2.32 #ifndef OCTSCALE
22 greg 2.29 #define OCTSCALE 1.0 /* ceil((valid rad.)/(cube size)) */
23 greg 2.32 #endif
24     #ifndef AMBVWT
25     #define AMBVWT 250 /* relative ambient value weight (# calcs) */
26     #endif
27 greg 1.1
28 greg 1.14 typedef struct ambtree {
29 greg 2.12 AMBVAL *alist; /* ambient value list */
30     struct ambtree *kid; /* 8 child nodes */
31 greg 1.14 } AMBTREE; /* ambient octree */
32    
33 greg 1.1 extern CUBE thescene; /* contains space boundaries */
34    
35 greg 2.27 extern char *shm_boundary; /* memory sharing boundary */
36 greg 2.26
37 greg 2.12 #define MAXASET 511 /* maximum number of elements in ambient set */
38     OBJECT ambset[MAXASET+1]={0}; /* ambient include/exclude set */
39 greg 1.1
40 greg 2.12 double maxarad; /* maximum ambient radius */
41     double minarad; /* minimum ambient radius */
42 greg 1.1
43 greg 2.12 static AMBTREE atrunk; /* our ambient trunk node */
44 greg 1.1
45     static FILE *ambfp = NULL; /* ambient file pointer */
46 greg 2.16 static int nunflshed = 0; /* number of unflushed ambient values */
47 greg 1.1
48 greg 2.26 #ifndef SORT_THRESH
49     #ifdef BIGMEM
50 greg 2.27 #define SORT_THRESH ((9L<<20)/sizeof(AMBVAL))
51 greg 2.26 #else
52 greg 2.27 #define SORT_THRESH ((3L<<20)/sizeof(AMBVAL))
53 greg 2.26 #endif
54     #endif
55     #ifndef SORT_INTVL
56 greg 2.29 #define SORT_INTVL (SORT_THRESH*256)
57 greg 2.26 #endif
58 greg 2.28 #ifndef MAX_SORT_INTVL
59 greg 2.29 #define MAX_SORT_INTVL (SORT_INTVL<<4)
60 greg 2.28 #endif
61 greg 2.26
62 greg 2.32 static COLOR avsum = BLKCOLOR; /* computed ambient value sum */
63 greg 2.35 static unsigned int nambvals = 0; /* total number of indirect values */
64     static unsigned int nambshare = 0; /* number of values from file */
65 greg 2.27 static unsigned long ambclock = 0; /* ambient access clock */
66     static unsigned long lastsort = 0; /* time of last value sort */
67 greg 2.26 static long sortintvl = SORT_INTVL; /* time until next sort */
68    
69     #define MAXACLOCK (1L<<30) /* clock turnover value */
70 greg 2.27 /*
71     * Track access times unless we are sharing ambient values
72     * through memory on a multiprocessor, when we want to avoid
73 greg 2.35 * claiming our own memory (copy on write). Go ahead anyway
74     * if more than two thirds of our values are unshared.
75 greg 2.27 */
76 greg 2.35 #define tracktime (shm_boundary == NULL || nambvals > 3*nambshare)
77 greg 2.26
78 greg 2.12 #define AMBFLUSH (BUFSIZ/AMBVALSIZ)
79 greg 2.6
80 greg 2.12 #define newambval() (AMBVAL *)bmalloc(sizeof(AMBVAL))
81 greg 1.1
82 greg 2.7 extern long ftell(), lseek();
83 greg 2.26 static int initambfile(), avsave(), avinsert(), sortambvals();
84 greg 2.20 static AMBVAL *avstore();
85 greg 2.19 #ifdef F_SETLKW
86     static aflock();
87     #endif
88 greg 1.1
89 greg 2.7
90 greg 2.3 setambres(ar) /* set ambient resolution */
91     int ar;
92     {
93 greg 2.21 ambres = ar < 0 ? 0 : ar; /* may be done already */
94 greg 2.3 /* set min & max radii */
95     if (ar <= 0) {
96 greg 2.25 minarad = 0;
97 greg 2.3 maxarad = thescene.cusize / 2.0;
98     } else {
99     minarad = thescene.cusize / ar;
100 greg 2.30 maxarad = 64 * minarad; /* heuristic */
101 greg 2.3 if (maxarad > thescene.cusize / 2.0)
102     maxarad = thescene.cusize / 2.0;
103     }
104 greg 2.25 if (minarad <= FTINY)
105     minarad = 10*FTINY;
106     if (maxarad <= minarad)
107     maxarad = 64 * minarad;
108 greg 2.3 }
109    
110    
111 greg 2.21 setambacc(newa) /* set ambient accuracy */
112 greg 2.20 double newa;
113     {
114 greg 2.33 double ambdiff;
115 greg 2.20
116 greg 2.33 if (newa < 0.0)
117     newa = 0.0;
118     ambdiff = fabs(newa - ambacc);
119     if (ambdiff >= .01 && (ambacc = newa) > FTINY && nambvals > 0)
120     sortambvals(1); /* rebuild tree */
121 greg 2.20 }
122    
123    
124 greg 1.1 setambient(afile) /* initialize calculation */
125     char *afile;
126     {
127 greg 2.9 long headlen;
128 greg 2.12 AMBVAL amb;
129 greg 2.3 /* init ambient limits */
130     setambres(ambres);
131 greg 2.21 setambacc(ambacc);
132 greg 2.19 if (afile == NULL)
133     return;
134 greg 2.20 if (ambacc <= FTINY) {
135 greg 2.21 sprintf(errmsg, "zero ambient accuracy so \"%s\" not opened",
136 greg 2.20 afile);
137     error(WARNING, errmsg);
138     return;
139     }
140 greg 2.3 /* open ambient file */
141 greg 2.19 if ((ambfp = fopen(afile, "r+")) != NULL) {
142     initambfile(0);
143     headlen = ftell(ambfp);
144     while (readambval(&amb, ambfp))
145 greg 2.21 avinsert(avstore(&amb));
146 greg 2.19 /* align */
147     fseek(ambfp, -((ftell(ambfp)-headlen)%AMBVALSIZ), 1);
148 greg 2.35 nambshare = nambvals;
149 greg 2.19 } else if ((ambfp = fopen(afile, "w+")) != NULL)
150     initambfile(1);
151     else {
152     sprintf(errmsg, "cannot open ambient file \"%s\"", afile);
153     error(SYSTEM, errmsg);
154 greg 2.16 }
155 greg 2.19 nunflshed++; /* lie */
156     ambsync();
157 greg 1.8 }
158    
159    
160     ambnotify(obj) /* record new modifier */
161 greg 2.12 OBJECT obj;
162 greg 1.8 {
163 greg 1.11 static int hitlimit = 0;
164 greg 2.12 register OBJREC *o = objptr(obj);
165 greg 1.8 register char **amblp;
166    
167 greg 1.11 if (hitlimit || !ismodifier(o->otype))
168 greg 1.8 return;
169     for (amblp = amblist; *amblp != NULL; amblp++)
170     if (!strcmp(o->oname, *amblp)) {
171 greg 1.11 if (ambset[0] >= MAXASET) {
172     error(WARNING, "too many modifiers in ambient list");
173     hitlimit++;
174     return; /* should this be fatal? */
175     }
176 greg 1.8 insertelem(ambset, obj);
177     return;
178 greg 1.1 }
179     }
180    
181    
182 greg 2.34 ambient(acol, r, nrm) /* compute ambient component for ray */
183 greg 1.1 COLOR acol;
184     register RAY *r;
185 greg 2.34 FVECT nrm;
186 greg 1.1 {
187     static int rdepth = 0; /* ambient recursion */
188 greg 2.12 double d;
189 greg 1.1
190     if (ambdiv <= 0) /* no ambient calculation */
191     goto dumbamb;
192     /* check number of bounces */
193 greg 1.16 if (rdepth >= ambounce)
194 greg 1.1 goto dumbamb;
195     /* check ambient list */
196     if (ambincl != -1 && r->ro != NULL &&
197     ambincl != inset(ambset, r->ro->omod))
198     goto dumbamb;
199    
200     if (ambacc <= FTINY) { /* no ambient storage */
201 greg 1.16 rdepth++;
202     d = doambient(acol, r, r->rweight, NULL, NULL);
203     rdepth--;
204 greg 2.32 if (d <= FTINY)
205 greg 1.1 goto dumbamb;
206 greg 1.16 return;
207 greg 1.1 }
208 greg 2.27 /* resort memory? */
209 greg 2.28 sortambvals(0);
210 greg 1.1 /* get ambient value */
211     setcolor(acol, 0.0, 0.0, 0.0);
212 greg 2.34 d = sumambient(acol, r, nrm, rdepth,
213 greg 1.16 &atrunk, thescene.cuorg, thescene.cusize);
214 greg 2.32 if (d > FTINY) {
215 greg 1.16 scalecolor(acol, 1.0/d);
216 greg 2.32 return;
217 greg 1.16 }
218 greg 2.33 rdepth++; /* need to cache new value */
219 greg 2.34 d = makeambient(acol, r, nrm, rdepth-1);
220 greg 2.32 rdepth--;
221 greg 1.16 if (d > FTINY)
222     return;
223 greg 1.1 dumbamb: /* return global value */
224     copycolor(acol, ambval);
225 greg 2.32 #if AMBVWT
226     if (nambvals == 0)
227     return;
228     scalecolor(acol, (double)AMBVWT);
229     addcolor(acol, avsum); /* average in computations */
230     d = 1.0/(AMBVWT+nambvals);
231     scalecolor(acol, d);
232     #endif
233 greg 1.1 }
234    
235    
236     double
237 greg 2.34 sumambient(acol, r, rn, al, at, c0, s) /* get interpolated ambient value */
238 greg 1.1 COLOR acol;
239     register RAY *r;
240 greg 2.34 FVECT rn;
241 greg 1.16 int al;
242 greg 2.12 AMBTREE *at;
243 greg 1.1 FVECT c0;
244 greg 2.12 double s;
245 greg 1.1 {
246 greg 2.12 double d, e1, e2, wt, wsum;
247 greg 1.1 COLOR ct;
248     FVECT ck0;
249     int i;
250     register int j;
251 greg 2.12 register AMBVAL *av;
252 greg 2.29
253     wsum = 0.0;
254 greg 1.7 /* do this node */
255 greg 1.1 for (av = at->alist; av != NULL; av = av->next) {
256 greg 2.26 if (tracktime)
257     av->latick = ambclock++;
258 greg 1.1 /*
259 greg 1.16 * Ambient level test.
260 greg 1.1 */
261 greg 2.23 if (av->lvl > al) /* list sorted, so this works */
262     break;
263     if (av->weight < r->rweight-FTINY)
264 greg 1.1 continue;
265     /*
266     * Ambient radius test.
267     */
268 greg 2.29 d = av->pos[0] - r->rop[0];
269     e1 = d * d;
270     d = av->pos[1] - r->rop[1];
271     e1 += d * d;
272     d = av->pos[2] - r->rop[2];
273     e1 += d * d;
274 greg 1.1 e1 /= av->rad * av->rad;
275     if (e1 > ambacc*ambacc*1.21)
276     continue;
277     /*
278     * Normal direction test.
279     */
280     e2 = (1.0 - DOT(av->dir, r->ron)) * r->rweight;
281     if (e2 < 0.0) e2 = 0.0;
282     if (e1 + e2 > ambacc*ambacc*1.21)
283     continue;
284     /*
285     * Ray behind test.
286     */
287     d = 0.0;
288     for (j = 0; j < 3; j++)
289     d += (r->rop[j] - av->pos[j]) *
290     (av->dir[j] + r->ron[j]);
291 greg 1.18 if (d*0.5 < -minarad*ambacc-.001)
292 greg 1.1 continue;
293     /*
294     * Jittering final test reduces image artifacts.
295     */
296     wt = sqrt(e1) + sqrt(e2);
297 greg 2.31 if (wt > ambacc*(.9+.2*urand(9015+samplendx)))
298 greg 1.1 continue;
299     if (wt <= 1e-3)
300     wt = 1e3;
301     else
302     wt = 1.0 / wt;
303     wsum += wt;
304 greg 2.34 extambient(ct, av, r->rop, rn);
305 greg 1.1 scalecolor(ct, wt);
306     addcolor(acol, ct);
307 greg 1.7 }
308     if (at->kid == NULL)
309     return(wsum);
310     /* do children */
311     s *= 0.5;
312     for (i = 0; i < 8; i++) {
313     for (j = 0; j < 3; j++) {
314     ck0[j] = c0[j];
315     if (1<<j & i)
316     ck0[j] += s;
317     if (r->rop[j] < ck0[j] - OCTSCALE*s)
318     break;
319     if (r->rop[j] > ck0[j] + (1.0+OCTSCALE)*s)
320     break;
321     }
322     if (j == 3)
323 greg 2.34 wsum += sumambient(acol, r, rn, al, at->kid+i, ck0, s);
324 greg 1.1 }
325     return(wsum);
326     }
327    
328    
329     double
330 greg 2.34 makeambient(acol, r, rn, al) /* make a new ambient value */
331 greg 1.1 COLOR acol;
332     register RAY *r;
333 greg 2.34 FVECT rn;
334 greg 1.16 int al;
335 greg 1.1 {
336 greg 2.12 AMBVAL amb;
337 greg 1.14 FVECT gp, gd;
338 greg 1.16 /* compute weight */
339     amb.weight = pow(AVGREFL, (double)al);
340 greg 2.33 if (r->rweight < 0.1*amb.weight) /* heuristic */
341 greg 1.16 amb.weight = r->rweight;
342     /* compute ambient */
343     amb.rad = doambient(acol, r, amb.weight, gp, gd);
344 greg 2.33 if (amb.rad <= FTINY)
345 greg 1.1 return(0.0);
346     /* store it */
347     VCOPY(amb.pos, r->rop);
348     VCOPY(amb.dir, r->ron);
349 greg 1.16 amb.lvl = al;
350 greg 1.1 copycolor(amb.val, acol);
351 greg 1.14 VCOPY(amb.gpos, gp);
352     VCOPY(amb.gdir, gd);
353 greg 1.1 /* insert into tree */
354 greg 2.7 avsave(&amb); /* and save to file */
355 greg 2.34 if (rn != r->ron)
356     extambient(acol, &amb, r->rop, rn); /* texture */
357 greg 1.1 return(amb.rad);
358 greg 1.15 }
359    
360    
361     extambient(cr, ap, pv, nv) /* extrapolate value at pv, nv */
362     COLOR cr;
363 greg 2.12 register AMBVAL *ap;
364 greg 1.15 FVECT pv, nv;
365     {
366     FVECT v1, v2;
367     register int i;
368 greg 2.12 double d;
369 greg 1.15
370     d = 1.0; /* zeroeth order */
371     /* gradient due to translation */
372     for (i = 0; i < 3; i++)
373     d += ap->gpos[i]*(pv[i]-ap->pos[i]);
374     /* gradient due to rotation */
375     VCOPY(v1, ap->dir);
376     fcross(v2, v1, nv);
377     d += DOT(ap->gdir, v2);
378     if (d <= 0.0) {
379     setcolor(cr, 0.0, 0.0, 0.0);
380     return;
381     }
382     copycolor(cr, ap->val);
383     scalecolor(cr, d);
384 greg 1.1 }
385    
386    
387     static
388 greg 2.9 initambfile(creat) /* initialize ambient file */
389     int creat;
390     {
391     extern char *progname, *octname, VersionID[];
392    
393 greg 2.19 #ifdef F_SETLKW
394     aflock(creat ? F_WRLCK : F_RDLCK);
395     #endif
396 greg 2.12 #ifdef MSDOS
397     setmode(fileno(ambfp), O_BINARY);
398     #endif
399 greg 2.21 setbuf(ambfp, bmalloc(BUFSIZ+8));
400 greg 2.9 if (creat) { /* new file */
401 greg 2.24 newheader("RADIANCE", ambfp);
402 greg 2.9 fprintf(ambfp, "%s -av %g %g %g -ab %d -aa %g ",
403     progname, colval(ambval,RED),
404     colval(ambval,GRN), colval(ambval,BLU),
405     ambounce, ambacc);
406     fprintf(ambfp, "-ad %d -as %d -ar %d %s\n",
407     ambdiv, ambssamp, ambres,
408     octname==NULL ? "" : octname);
409     fprintf(ambfp, "SOFTWARE= %s\n", VersionID);
410     fputformat(AMBFMT, ambfp);
411     putc('\n', ambfp);
412     putambmagic(ambfp);
413 greg 2.17 } else if (checkheader(ambfp, AMBFMT, NULL) < 0 || !hasambmagic(ambfp))
414     error(USER, "bad ambient file");
415 greg 2.9 }
416    
417    
418     static
419 greg 2.7 avsave(av) /* insert and save an ambient value */
420 greg 2.12 AMBVAL *av;
421 greg 1.1 {
422 greg 2.21 avinsert(avstore(av));
423 greg 1.1 if (ambfp == NULL)
424     return;
425 greg 2.5 if (writambval(av, ambfp) < 0)
426 greg 1.1 goto writerr;
427 greg 2.16 if (++nunflshed >= AMBFLUSH)
428 greg 2.7 if (ambsync() == EOF)
429 greg 1.1 goto writerr;
430     return;
431     writerr:
432 greg 2.17 error(SYSTEM, "error writing ambient file");
433 greg 1.1 }
434    
435    
436 greg 2.20 static AMBVAL *
437     avstore(aval) /* allocate memory and store aval */
438     register AMBVAL *aval;
439     {
440     register AMBVAL *av;
441    
442     if ((av = newambval()) == NULL)
443     error(SYSTEM, "out of memory in avstore");
444     copystruct(av, aval);
445 greg 2.26 av->latick = ambclock;
446     av->next = NULL;
447 greg 2.32 addcolor(avsum, av->val); /* add to sum for averaging */
448 greg 2.26 nambvals++;
449 greg 2.20 return(av);
450     }
451    
452    
453 greg 2.26 #define ATALLOCSZ 512 /* #/8 trees to allocate at once */
454    
455     static AMBTREE *atfreelist = NULL; /* free ambient tree structures */
456    
457    
458 greg 1.1 static
459 greg 2.26 AMBTREE *
460     newambtree() /* allocate 8 ambient tree structs */
461     {
462     register AMBTREE *atp, *upperlim;
463    
464     if (atfreelist == NULL) { /* get more nodes */
465     atfreelist = (AMBTREE *)bmalloc(ATALLOCSZ*8*sizeof(AMBTREE));
466     if (atfreelist == NULL)
467     return(NULL);
468     /* link new free list */
469     upperlim = atfreelist + 8*(ATALLOCSZ-1);
470     for (atp = atfreelist; atp < upperlim; atp += 8)
471     atp->kid = atp + 8;
472     atp->kid = NULL;
473     }
474     atp = atfreelist;
475     atfreelist = atp->kid;
476     bzero((char *)atp, 8*sizeof(AMBTREE));
477     return(atp);
478     }
479    
480    
481     static
482     freeambtree(atp) /* free 8 ambient tree structs */
483     AMBTREE *atp;
484     {
485     atp->kid = atfreelist;
486     atfreelist = atp;
487     }
488    
489    
490     static
491 greg 2.21 avinsert(av) /* insert ambient value in our tree */
492 greg 2.20 register AMBVAL *av;
493 greg 1.1 {
494 greg 2.21 register AMBTREE *at;
495 greg 2.23 register AMBVAL *ap;
496     AMBVAL avh;
497 greg 1.1 FVECT ck0;
498 greg 2.21 double s;
499 greg 1.1 int branch;
500     register int i;
501    
502 greg 2.20 if (av->rad <= FTINY)
503     error(CONSISTENCY, "zero ambient radius in avinsert");
504 greg 2.21 at = &atrunk;
505     VCOPY(ck0, thescene.cuorg);
506     s = thescene.cusize;
507 greg 1.1 while (s*(OCTSCALE/2) > av->rad*ambacc) {
508     if (at->kid == NULL)
509     if ((at->kid = newambtree()) == NULL)
510 greg 2.20 error(SYSTEM, "out of memory in avinsert");
511 greg 1.1 s *= 0.5;
512     branch = 0;
513     for (i = 0; i < 3; i++)
514     if (av->pos[i] > ck0[i] + s) {
515     ck0[i] += s;
516     branch |= 1 << i;
517     }
518     at = at->kid + branch;
519     }
520 greg 2.23 avh.next = at->alist; /* order by increasing level */
521     for (ap = &avh; ap->next != NULL; ap = ap->next)
522     if (ap->next->lvl >= av->lvl)
523     break;
524     av->next = ap->next;
525     ap->next = av;
526     at->alist = avh.next;
527 greg 2.7 }
528    
529    
530 greg 2.20 static
531 greg 2.26 unloadatree(at, f) /* unload an ambient value tree */
532 greg 2.20 register AMBTREE *at;
533 greg 2.26 int (*f)();
534 greg 2.20 {
535     register AMBVAL *av;
536     register int i;
537     /* transfer values at this node */
538     for (av = at->alist; av != NULL; av = at->alist) {
539     at->alist = av->next;
540 greg 2.26 (*f)(av);
541 greg 2.20 }
542 greg 2.21 if (at->kid == NULL)
543     return;
544 greg 2.20 for (i = 0; i < 8; i++) /* transfer and free children */
545 greg 2.26 unloadatree(at->kid+i, f);
546 greg 2.20 freeambtree(at->kid);
547 greg 2.26 at->kid = NULL;
548     }
549    
550    
551     static AMBVAL **avlist1, **avlist2; /* ambient value lists for sorting */
552     static int i_avlist; /* index for lists */
553    
554    
555     static
556     av2list(av)
557     AMBVAL *av;
558     {
559 greg 2.27 #ifdef DEBUG
560 greg 2.26 if (i_avlist >= nambvals)
561     error(CONSISTENCY, "too many ambient values in av2list1");
562 greg 2.27 #endif
563 greg 2.26 avlist1[i_avlist] = avlist2[i_avlist] = av;
564     i_avlist++;
565     }
566    
567    
568     static int
569     alatcmp(avp1, avp2) /* compare ambient values for MRA */
570     AMBVAL **avp1, **avp2;
571     {
572     return((**avp2).latick - (**avp1).latick);
573     }
574    
575    
576     static int
577     aposcmp(avp1, avp2) /* compare ambient value positions */
578     AMBVAL **avp1, **avp2;
579     {
580     return(*avp1 - *avp2);
581     }
582    
583    
584 greg 2.27 #ifdef DEBUG
585     static int
586     avlmemi(avaddr) /* find list position from address */
587     AMBVAL *avaddr;
588     {
589     register AMBVAL **avlpp;
590    
591     avlpp = (AMBVAL **)bsearch((char *)&avaddr, (char *)avlist2,
592     nambvals, sizeof(AMBVAL *), aposcmp);
593     if (avlpp == NULL)
594     error(CONSISTENCY, "address not found in avlmemi");
595     return(avlpp - avlist2);
596     }
597     #else
598     #define avlmemi(avaddr) ((AMBVAL **)bsearch((char *)&avaddr,(char *)avlist2, \
599     nambvals,sizeof(AMBVAL *),aposcmp) - avlist2)
600     #endif
601    
602    
603 greg 2.26 static
604     sortambvals(always) /* resort ambient values */
605     int always;
606     {
607     AMBTREE oldatrunk;
608     AMBVAL tav, *tap, *pnext;
609     register int i, j;
610 greg 2.28 /* see if it's time yet */
611     if (!always && (ambclock < lastsort+sortintvl ||
612     nambvals < SORT_THRESH))
613     return;
614 greg 2.26 /*
615     * The idea here is to minimize memory thrashing
616     * in VM systems by improving reference locality.
617     * We do this by periodically sorting our stored ambient
618     * values in memory in order of most recently to least
619     * recently accessed. This ordering was chosen so that new
620     * ambient values (which tend to be less important) go into
621     * higher memory with the infrequently accessed values.
622     * Since we expect our values to need sorting less
623     * frequently as the process continues, we double our
624     * waiting interval after each call.
625     * This routine is also called by setambacc() with
626     * the "always" parameter set to 1 so that the ambient
627     * tree will be rebuilt with the new accuracy parameter.
628     */
629 greg 2.27 if (tracktime) { /* allocate pointer arrays to sort */
630 greg 2.26 avlist1 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *));
631     avlist2 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *));
632     } else
633     avlist1 = avlist2 = NULL;
634 greg 2.27 if (avlist2 == NULL) { /* no time tracking -- rebuild tree? */
635 greg 2.26 if (avlist1 != NULL)
636     free((char *)avlist1);
637 greg 2.27 if (always) { /* rebuild without sorting */
638     copystruct(&oldatrunk, &atrunk);
639     atrunk.alist = NULL;
640     atrunk.kid = NULL;
641     unloadatree(&oldatrunk, avinsert);
642     }
643 greg 2.26 } else { /* sort memory by last access time */
644     /*
645     * Sorting memory is tricky because it isn't contiguous.
646     * We have to sort an array of pointers by MRA and also
647     * by memory position. We then copy values in "loops"
648     * to minimize memory hits. Nevertheless, we will visit
649 greg 2.27 * everyone at least twice, and this is an expensive process
650 greg 2.26 * when we're thrashing, which is when we need to do it.
651     */
652 greg 2.27 #ifdef DEBUG
653 greg 2.29 sprintf(errmsg, "sorting %u ambient values at ambclock=%lu...",
654     nambvals, ambclock);
655 greg 2.27 eputs(errmsg);
656     #endif
657     i_avlist = 0;
658     unloadatree(&atrunk, av2list); /* empty current tree */
659     #ifdef DEBUG
660     if (i_avlist < nambvals)
661     error(CONSISTENCY, "missing ambient values in sortambvals");
662     #endif
663 greg 2.26 qsort((char *)avlist1, nambvals, sizeof(AMBVAL *), alatcmp);
664     qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), aposcmp);
665     for (i = 0; i < nambvals; i++) {
666 greg 2.27 if (avlist1[i] == NULL)
667 greg 2.26 continue;
668     tap = avlist2[i];
669     copystruct(&tav, tap);
670     for (j = i; (pnext = avlist1[j]) != tap;
671 greg 2.27 j = avlmemi(pnext)) {
672 greg 2.26 copystruct(avlist2[j], pnext);
673     avinsert(avlist2[j]);
674     avlist1[j] = NULL;
675     }
676     copystruct(avlist2[j], &tav);
677     avinsert(avlist2[j]);
678     avlist1[j] = NULL;
679     }
680     free((char *)avlist1);
681     free((char *)avlist2);
682 greg 2.28 /* compute new sort interval */
683     sortintvl = ambclock - lastsort;
684 greg 2.32 if (sortintvl >= MAX_SORT_INTVL/2)
685 greg 2.28 sortintvl = MAX_SORT_INTVL;
686     else
687 greg 2.26 sortintvl <<= 1; /* wait twice as long next */
688 greg 2.27 #ifdef DEBUG
689     eputs("done\n");
690     #endif
691 greg 2.26 }
692     if (ambclock >= MAXACLOCK)
693     ambclock = MAXACLOCK/2;
694     lastsort = ambclock;
695 greg 2.20 }
696    
697    
698 greg 2.18 #ifdef F_SETLKW
699 greg 2.10
700 greg 2.19 static
701     aflock(typ) /* lock/unlock ambient file */
702     int typ;
703     {
704     static struct flock fls; /* static so initialized to zeroes */
705    
706     fls.l_type = typ;
707     if (fcntl(fileno(ambfp), F_SETLKW, &fls) < 0)
708     error(SYSTEM, "cannot (un)lock ambient file");
709     }
710    
711    
712 greg 2.16 int
713 greg 2.7 ambsync() /* synchronize ambient file */
714     {
715     static FILE *ambinp = NULL;
716 greg 2.15 static long lastpos = -1;
717     long flen;
718 greg 2.12 AMBVAL avs;
719 greg 2.7 register int n;
720 greg 2.16
721     if (nunflshed == 0)
722     return(0);
723 greg 2.19 if (lastpos < 0) /* initializing (locked in initambfile) */
724     goto syncend;
725 greg 2.7 /* gain exclusive access */
726 greg 2.19 aflock(F_WRLCK);
727 greg 2.7 /* see if file has grown */
728 greg 2.15 if ((flen = lseek(fileno(ambfp), 0L, 2)) < 0)
729 greg 2.21 goto seekerr;
730 greg 2.15 if (n = flen - lastpos) { /* file has grown */
731 greg 2.17 if (ambinp == NULL) { /* use duplicate filedes */
732     ambinp = fdopen(dup(fileno(ambfp)), "r");
733 greg 2.14 if (ambinp == NULL)
734 greg 2.17 error(SYSTEM, "fdopen failed in ambsync");
735 greg 2.14 }
736 greg 2.15 if (fseek(ambinp, lastpos, 0) < 0)
737 greg 2.21 goto seekerr;
738 greg 2.15 while (n >= AMBVALSIZ) { /* load contributed values */
739 greg 2.7 readambval(&avs, ambinp);
740 greg 2.21 avinsert(avstore(&avs));
741 greg 2.15 n -= AMBVALSIZ;
742     }
743 greg 2.22 /*** seek always as safety measure
744     if (n) ***/ /* alignment */
745 greg 2.21 if (lseek(fileno(ambfp), flen-n, 0) < 0)
746     goto seekerr;
747 greg 2.7 }
748 greg 2.21 #ifdef DEBUG
749     if (ambfp->_ptr - ambfp->_base != nunflshed*AMBVALSIZ) {
750     sprintf(errmsg, "ambient file buffer at %d rather than %d",
751     ambfp->_ptr - ambfp->_base,
752     nunflshed*AMBVALSIZ);
753     error(CONSISTENCY, errmsg);
754     }
755     #endif
756 greg 2.15 syncend:
757 greg 2.7 n = fflush(ambfp); /* calls write() at last */
758 greg 2.21 if ((lastpos = lseek(fileno(ambfp), 0L, 1)) < 0)
759     goto seekerr;
760 greg 2.19 aflock(F_UNLCK); /* release file */
761 greg 2.16 nunflshed = 0;
762 greg 2.7 return(n);
763 greg 2.21 seekerr:
764     error(SYSTEM, "seek failed in ambsync");
765 greg 2.18 }
766    
767     #else
768    
769     int
770     ambsync() /* flush ambient file */
771     {
772     if (nunflshed == 0)
773     return(0);
774     nunflshed = 0;
775     return(fflush(ambfp));
776 greg 1.1 }
777 greg 2.10
778     #endif