ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/ambient.c
Revision: 2.36
Committed: Wed Feb 14 15:43:22 1996 UTC (28 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.35: +3 -8 lines
Log Message:
added -aw option for ambient value weight

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