1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
|
/*
* This file has been donated to Jam.
*/
# include "jam.h"
# include "lists.h"
# include "parse.h"
# include "rules.h"
# include "regexp.h"
# include "headers.h"
# include "newstr.h"
# include "hash.h"
# include "hcache.h"
# include "variable.h"
# include "search.h"
#ifdef OPT_HEADER_CACHE_EXT
/*
* Craig W. McPheeters, Alias|Wavefront.
*
* hcache.c hcache.h - handle cacheing of #includes in source files.
*
* Create a cache of files scanned for headers. When starting jam, look for the
* cache file and load it if present. When finished the binding phase, create a
* new header cache. The cache contains files, their timestamps and the header
* files found in their scan. During the binding phase of jam, look in the
* header cache first for the headers contained in a file. If the cache is
* present and valid, use its contents. This results in dramatic speedups with
* large projects (eg. 3min -> 1min startup for one project.)
*
* External routines:
* hcache_init() - read and parse the local .jamdeps file.
* hcache_done() - write a new .jamdeps file.
* hcache() - return list of headers on target. Use cache or do a scan.
*
* The dependency file format is an ASCII file with 1 line per target. Each line
* has the following fields:
* @boundname@ timestamp @file@ @file@ @file@ ... \n
*/
typedef struct hcachedata HCACHEDATA ;
struct hcachedata
{
char * boundname;
time_t time;
LIST * includes;
LIST * hdrscan; /* the HDRSCAN value for this target */
int age; /* if too old, we'll remove it from cache */
HCACHEDATA * next;
};
static struct hash * hcachehash = 0;
static HCACHEDATA * hcachelist = 0;
static int queries = 0;
static int hits = 0;
#define CACHE_FILE_VERSION "version 4"
#define CACHE_RECORD_HEADER "header"
#define CACHE_RECORD_END "end"
/*
* Return the name of the header cache file. May return NULL.
*
* The user sets this by setting the HCACHEFILE variable in a Jamfile. We cache
* the result so the user can not change the cache file during header scanning.
*/
static char * cache_name( void )
{
static char * name = 0;
if ( !name )
{
LIST * hcachevar = var_get( "HCACHEFILE" );
if ( hcachevar )
{
TARGET * t = bindtarget( hcachevar->string );
pushsettings( t->settings );
/* Do not expect the cache file to be generated, so pass 0 as the
* third argument to search. Expect the location to be specified via
* LOCATE, so pass 0 as the fourth arugment.
*/
t->boundname = search( t->name, &t->time, 0, 0 );
popsettings( t->settings );
if ( hcachevar )
name = copystr( t->boundname );
}
}
return name;
}
/*
* Return the maximum age a cache entry can have before it is purged ftom the
* cache.
*/
static int cache_maxage( void )
{
int age = 100;
LIST * var = var_get( "HCACHEMAXAGE" );
if ( var )
{
age = atoi( var->string );
if ( age < 0 )
age = 0;
}
return age;
}
/*
* Read a netstring. The caveat is that the string can not contain ASCII 0. The
* returned value is as returned by newstr(), so it need not be freed.
*/
char * read_netstring( FILE * f )
{
unsigned long len;
static char * buf = NULL;
static unsigned long buf_len = 0;
if ( fscanf( f, " %9lu", &len ) != 1 )
return NULL;
if ( fgetc( f ) != (int)'\t' )
return NULL;
if ( len > 1024 * 64 )
return NULL; /* sanity check */
if ( len > buf_len )
{
unsigned long new_len = buf_len * 2;
if ( new_len < len )
new_len = len;
buf = (char *)BJAM_REALLOC( buf, new_len + 1 );
if ( buf )
buf_len = new_len;
}
if ( !buf )
return NULL;
if ( fread( buf, 1, len, f ) != len )
return NULL;
if ( fgetc( f ) != (int)'\n' )
return NULL;
buf[ len ] = 0;
return newstr( buf );
}
/*
* Write a netstring.
*/
void write_netstring( FILE * f, char const * s )
{
if ( !s )
s = "";
fprintf( f, "%lu\t%s\n", (long unsigned)strlen( s ), s );
}
void hcache_init()
{
HCACHEDATA cachedata;
HCACHEDATA * c;
FILE * f;
char * version;
int header_count = 0;
char * hcachename;
hcachehash = hashinit( sizeof( HCACHEDATA ), "hcache" );
if ( !( hcachename = cache_name() ) )
return;
if ( !( f = fopen( hcachename, "rb" ) ) )
return;
version = read_netstring( f );
if ( !version || strcmp( version, CACHE_FILE_VERSION ) )
{
fclose( f );
return;
}
while ( 1 )
{
char * record_type;
char * time_str;
char * age_str;
char * includes_count_str;
char * hdrscan_count_str;
int i;
int count;
LIST * l;
record_type = read_netstring( f );
if ( !record_type )
{
fprintf( stderr, "invalid %s\n", hcachename );
goto bail;
}
if ( !strcmp( record_type, CACHE_RECORD_END ) )
break;
if ( strcmp( record_type, CACHE_RECORD_HEADER ) )
{
fprintf( stderr, "invalid %s with record separator <%s>\n",
hcachename, record_type ? record_type : "<null>" );
goto bail;
}
c = &cachedata;
c->boundname = read_netstring( f );
time_str = read_netstring( f );
age_str = read_netstring( f );
includes_count_str = read_netstring( f );
if ( !c->boundname || !time_str || !age_str || !includes_count_str )
{
fprintf( stderr, "invalid %s\n", hcachename );
goto bail;
}
c->time = atoi( time_str );
c->age = atoi( age_str ) + 1;
count = atoi( includes_count_str );
for ( l = 0, i = 0; i < count; ++i )
{
char * s = read_netstring( f );
if ( !s )
{
fprintf( stderr, "invalid %s\n", hcachename );
goto bail;
}
l = list_new( l, s );
}
c->includes = l;
hdrscan_count_str = read_netstring( f );
if ( !includes_count_str )
{
list_free( c->includes );
fprintf( stderr, "invalid %s\n", hcachename );
goto bail;
}
count = atoi( hdrscan_count_str );
for ( l = 0, i = 0; i < count; ++i )
{
char * s = read_netstring( f );
if ( !s )
{
fprintf( stderr, "invalid %s\n", hcachename );
goto bail;
}
l = list_new( l, s );
}
c->hdrscan = l;
if ( !hashenter( hcachehash, (HASHDATA * *)&c ) )
{
fprintf( stderr, "can't insert header cache item, bailing on %s\n",
hcachename );
goto bail;
}
c->next = hcachelist;
hcachelist = c;
++header_count;
}
if ( DEBUG_HEADER )
printf( "hcache read from file %s\n", hcachename );
bail:
fclose( f );
}
void hcache_done()
{
FILE * f;
HCACHEDATA * c;
int header_count = 0;
char * hcachename;
int maxage;
if ( !hcachehash )
return;
if ( !( hcachename = cache_name() ) )
return;
if ( !( f = fopen( hcachename, "wb" ) ) )
return;
maxage = cache_maxage();
/* Print out the version. */
write_netstring( f, CACHE_FILE_VERSION );
c = hcachelist;
for ( c = hcachelist; c; c = c->next )
{
LIST * l;
char time_str[ 30 ];
char age_str[ 30 ];
char includes_count_str[ 30 ];
char hdrscan_count_str[ 30 ];
if ( maxage == 0 )
c->age = 0;
else if ( c->age > maxage )
continue;
sprintf( includes_count_str, "%lu", (long unsigned) list_length( c->includes ) );
sprintf( hdrscan_count_str, "%lu", (long unsigned) list_length( c->hdrscan ) );
sprintf( time_str, "%lu", (long unsigned) c->time );
sprintf( age_str, "%lu", (long unsigned) c->age );
write_netstring( f, CACHE_RECORD_HEADER );
write_netstring( f, c->boundname );
write_netstring( f, time_str );
write_netstring( f, age_str );
write_netstring( f, includes_count_str );
for ( l = c->includes; l; l = list_next( l ) )
write_netstring( f, l->string );
write_netstring( f, hdrscan_count_str );
for ( l = c->hdrscan; l; l = list_next( l ) )
write_netstring( f, l->string );
fputs( "\n", f );
++header_count;
}
write_netstring( f, CACHE_RECORD_END );
if ( DEBUG_HEADER )
printf( "hcache written to %s. %d dependencies, %.0f%% hit rate\n",
hcachename, header_count, queries ? 100.0 * hits / queries : 0 );
fclose ( f );
}
LIST * hcache( TARGET * t, int rec, regexp * re[], LIST * hdrscan )
{
HCACHEDATA cachedata;
HCACHEDATA * c = &cachedata;
LIST * l = 0;
++queries;
c->boundname = t->boundname;
if (hashcheck (hcachehash, (HASHDATA **) &c))
{
if (c->time == t->time)
{
LIST *l1 = hdrscan, *l2 = c->hdrscan;
while (l1 && l2) {
if (l1->string != l2->string) {
l1 = NULL;
} else {
l1 = list_next(l1);
l2 = list_next(l2);
}
}
if (l1 || l2) {
if (DEBUG_HEADER)
printf("HDRSCAN out of date in cache for %s\n",
t->boundname);
printf("HDRSCAN out of date for %s\n", t->boundname);
printf(" real : ");
list_print(hdrscan);
printf("\n cached: ");
list_print(c->hdrscan);
printf("\n");
list_free(c->includes);
list_free(c->hdrscan);
c->includes = 0;
c->hdrscan = 0;
} else {
if (DEBUG_HEADER)
printf ("using header cache for %s\n", t->boundname);
c->age = 0;
++hits;
l = list_copy (0, c->includes);
return l;
}
} else {
if (DEBUG_HEADER)
printf ("header cache out of date for %s\n", t->boundname);
list_free (c->includes);
list_free(c->hdrscan);
c->includes = 0;
c->hdrscan = 0;
}
} else {
if (hashenter (hcachehash, (HASHDATA **)&c)) {
c->boundname = newstr (c->boundname);
c->next = hcachelist;
hcachelist = c;
}
}
/* 'c' points at the cache entry. Its out of date. */
l = headers1 (0, t->boundname, rec, re);
c->time = t->time;
c->age = 0;
c->includes = list_copy (0, l);
c->hdrscan = list_copy(0, hdrscan);
return l;
}
#endif
|