2 * Copyright (C) 1996-2014 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
10 * Test-suite for playing with cache digests
14 #include "CacheDigest.h"
15 #include "store_key_md5.h"
27 typedef struct _Cache Cache
;
34 CacheQueryStats qstats
;
35 int count
; /* #currently cached entries */
36 int req_count
; /* #requests to this cache */
37 int bad_add_count
; /* #duplicate adds */
38 int bad_del_count
; /* #dels with no prior add */
41 typedef struct _CacheEntry
{
44 struct _CacheEntry
*next
;
45 unsigned char key_arr
[SQUID_MD5_DIGEST_LENGTH
];
46 /* storeSwapLogData s; */
49 /* parsed access log entry */
52 cache_key key
[SQUID_MD5_DIGEST_LENGTH
];
54 short int use_icp
; /* true/false */
58 frError
= -2, frMore
= -1, frEof
= 0, frOk
= 1
61 typedef struct _FileIterator FileIterator
;
62 typedef fr_result(*FI_READER
) (FileIterator
* fi
);
64 struct _FileIterator
{
67 time_t inner_time
; /* timestamp of the current entry */
68 time_t time_offset
; /* to adjust time set by reader */
69 int line_count
; /* number of lines scanned */
70 int bad_line_count
; /* number of parsing errors */
71 int time_warp_count
; /* number of out-of-order entries in the file */
72 FI_READER reader
; /* reads next entry and updates inner_time */
73 void *entry
; /* buffer for the current entry, freed with xfree() */
77 static time_t cur_time
= -1; /* timestamp of the current log entry */
79 /* copied from url.c */
80 static HttpRequestMethod
81 methodStrToId(const char *s
)
83 if (strcasecmp(s
, "GET") == 0) {
85 } else if (strcasecmp(s
, "POST") == 0) {
87 } else if (strcasecmp(s
, "PUT") == 0) {
89 } else if (strcasecmp(s
, "HEAD") == 0) {
91 } else if (strcasecmp(s
, "CONNECT") == 0) {
92 return METHOD_CONNECT
;
93 } else if (strcasecmp(s
, "TRACE") == 0) {
95 } else if (strcasecmp(s
, "PURGE") == 0) {
104 static void fileIteratorAdvance(FileIterator
* fi
);
106 static FileIterator
*
107 fileIteratorCreate(const char *fname
, FI_READER reader
)
109 FileIterator
*fi
= (FileIterator
*)xcalloc(1, sizeof(FileIterator
));
110 assert(fname
&& reader
);
113 fi
->file
= fopen(fname
, "r");
116 fprintf(stderr
, "cannot open %s: %s\n", fname
, strerror(errno
));
119 fprintf(stderr
, "opened %s\n", fname
);
121 fileIteratorAdvance(fi
);
127 fileIteratorDestroy(FileIterator
* fi
)
133 fprintf(stderr
, "closed %s\n", fi
->fname
);
141 fileIteratorSetCurTime(FileIterator
* fi
, time_t ct
)
144 assert(fi
->inner_time
> 0);
145 fi
->time_offset
= ct
- fi
->inner_time
;
149 fileIteratorAdvance(FileIterator
* fi
)
155 const time_t last_time
= fi
->inner_time
;
157 res
= fi
->reader(fi
);
160 if (fi
->inner_time
< 0)
161 fi
->inner_time
= last_time
;
163 fi
->inner_time
+= fi
->time_offset
;
166 ++ fi
->bad_line_count
;
167 else if (res
== frEof
) {
168 fprintf(stderr
, "exhausted %s (%d entries) at %s",
169 fi
->fname
, fi
->line_count
, ctime(&fi
->inner_time
));
171 } else if (fi
->inner_time
< last_time
) {
172 assert(last_time
>= 0);
173 ++ fi
->time_warp_count
;
174 fi
->inner_time
= last_time
;
177 /* report progress */
178 if (!(fi
->line_count
% 50000))
179 fprintf(stderr
, "%s scanned %d K entries (%d bad) at %s",
180 fi
->fname
, fi
->line_count
/ 1000, fi
->bad_line_count
,
181 ctime(&fi
->inner_time
));
188 cacheEntryCreate(const storeSwapLogData
* s
)
190 CacheEntry
*e
= (CacheEntry
*)xcalloc(1, sizeof(CacheEntry
));
193 memcpy(e
->key_arr
, s
->key
, SQUID_MD5_DIGEST_LENGTH
);
194 e
->key
= &e
->key_arr
[0];
199 cacheEntryDestroy(CacheEntry
* e
)
208 cacheCreate(const char *name
)
211 assert(name
&& strlen(name
));
212 c
= (Cache
*)xcalloc(1, sizeof(Cache
));
214 c
->hash
= hash_create(storeKeyHashCmp
, (int)2e6
, storeKeyHashHash
);
219 cacheDestroy(Cache
* cache
)
221 CacheEntry
*e
= NULL
;
225 /* destroy hash table contents */
228 while ((e
= (CacheEntry
*)hash_next(hash
))) {
229 hash_remove_link(hash
, (hash_link
*) e
);
230 cacheEntryDestroy(e
);
233 /* destroy the hash table itself */
234 hashFreeMemory(hash
);
237 cacheDigestDestroy(cache
->digest
);
242 /* re-digests currently hashed entries */
244 cacheResetDigest(Cache
* cache
)
246 CacheEntry
*e
= NULL
;
249 struct timeval t_start
, t_end
;
252 fprintf(stderr
, "%s: init-ing digest with %d entries\n", cache
->name
, cache
->count
);
255 cacheDigestDestroy(cache
->digest
);
259 cache
->digest
= cacheDigestCreate(cache
->count
+ 1, 6);
264 gettimeofday(&t_start
, NULL
);
268 while ((e
= (CacheEntry
*)hash_next(hash
))) {
269 cacheDigestAdd(cache
->digest
, e
->key
);
272 gettimeofday(&t_end
, NULL
);
273 assert(cache
->digest
->count
== cache
->count
);
274 fprintf(stderr
, "%s: init-ed digest with %d entries\n",
275 cache
->name
, cache
->digest
->count
);
276 fprintf(stderr
, "%s: init took: %f sec, %f sec/M\n",
278 tvSubDsec(t_start
, t_end
),
279 (double) 1e6
* tvSubDsec(t_start
, t_end
) / cache
->count
);
280 /* check how long it takes to traverse the hash */
281 gettimeofday(&t_start
, NULL
);
284 for (e
= (CacheEntry
*)hash_next(hash
); e
; e
= (CacheEntry
*)hash_next(hash
)) {}
286 gettimeofday(&t_end
, NULL
);
287 fprintf(stderr
, "%s: hash scan took: %f sec, %f sec/M\n",
289 tvSubDsec(t_start
, t_end
),
290 (double) 1e6
* tvSubDsec(t_start
, t_end
) / cache
->count
);
294 cacheQueryPeer(Cache
* cache
, const cache_key
* key
)
296 const int peer_has_it
= hash_lookup(cache
->peer
->hash
, key
) != NULL
;
297 const int we_think_we_have_it
= cacheDigestTest(cache
->digest
, key
);
299 ++ cache
->qstats
.query_count
;
302 if (we_think_we_have_it
)
303 ++ cache
->qstats
.true_hit_count
;
305 ++ cache
->qstats
.false_miss_count
;
307 if (we_think_we_have_it
)
308 ++ cache
->qstats
.false_hit_count
;
310 ++ cache
->qstats
.true_miss_count
;
315 cacheQueryReport(Cache
* cache
, CacheQueryStats
* stats
)
317 fprintf(stdout
, "%s: peer queries: %d (%d%%)\n",
319 stats
->query_count
, xpercentInt(stats
->query_count
, cache
->req_count
)
321 fprintf(stdout
, "%s: t-hit: %d (%d%%) t-miss: %d (%d%%) t-*: %d (%d%%)\n",
323 stats
->true_hit_count
, xpercentInt(stats
->true_hit_count
, stats
->query_count
),
324 stats
->true_miss_count
, xpercentInt(stats
->true_miss_count
, stats
->query_count
),
325 stats
->true_hit_count
+ stats
->true_miss_count
,
326 xpercentInt(stats
->true_hit_count
+ stats
->true_miss_count
, stats
->query_count
)
328 fprintf(stdout
, "%s: f-hit: %d (%d%%) f-miss: %d (%d%%) f-*: %d (%d%%)\n",
330 stats
->false_hit_count
, xpercentInt(stats
->false_hit_count
, stats
->query_count
),
331 stats
->false_miss_count
, xpercentInt(stats
->false_miss_count
, stats
->query_count
),
332 stats
->false_hit_count
+ stats
->false_miss_count
,
333 xpercentInt(stats
->false_hit_count
+ stats
->false_miss_count
, stats
->query_count
)
338 cacheReport(Cache
* cache
)
340 fprintf(stdout
, "%s: entries: %d reqs: %d bad-add: %d bad-del: %d\n",
341 cache
->name
, cache
->count
, cache
->req_count
,
342 cache
->bad_add_count
, cache
->bad_del_count
);
347 cacheFetch(Cache
* cache
, const RawAccessLogEntry
* e
)
353 cacheQueryPeer(cache
, e
->key
);
357 swapStateReader(FileIterator
* fi
)
359 storeSwapLogData
*entry
;
362 fi
->entry
= xcalloc(1, sizeof(storeSwapLogData
));
364 entry
= (storeSwapLogData
*)fi
->entry
;
366 if (fread(entry
, sizeof(*entry
), 1, fi
->file
) != 1)
369 fi
->inner_time
= entry
->lastref
;
371 if (entry
->op
!= SWAP_LOG_ADD
&& entry
->op
!= SWAP_LOG_DEL
) {
372 fprintf(stderr
, "%s:%d: unknown swap log action\n", fi
->fname
, fi
->line_count
);
380 accessLogReader(FileIterator
* fi
)
382 static char buf
[4096];
383 RawAccessLogEntry
*entry
;
386 HttpRequestMethod method_id
= METHOD_NONE
;
392 fi
->entry
= xcalloc(1, sizeof(RawAccessLogEntry
));
394 memset(fi
->entry
, 0, sizeof(RawAccessLogEntry
));
396 entry
= (RawAccessLogEntry
*)fi
->entry
;
398 if (!fgets(buf
, sizeof(buf
), fi
->file
))
399 return frEof
; /* eof */
401 entry
->timestamp
= fi
->inner_time
= (time_t) atoi(buf
);
403 url
= strstr(buf
, "://");
405 hier
= url
? strstr(url
, " - ") : NULL
;
408 /*fprintf(stderr, "%s:%d: strange access log entry '%s'\n",
409 * fname, scanned_count, buf); */
415 while (!xisdigit(*method
)) {
423 method_id
= methodStrToId(method
);
425 if (method_id
== METHOD_NONE
) {
426 /*fprintf(stderr, "%s:%d: invalid method %s in '%s'\n",
427 * fname, scanned_count, method, buf); */
440 *strchr(hier
, '/') = '\0';
442 /*fprintf(stdout, "%s:%d: %s %s %s\n",
443 * fname, count, method, url, hier); */
444 entry
->use_icp
= strcmp(hier
, "NONE");
446 /* no ICP lookup for these status codes */
447 /* strcmp(hier, "NONE") &&
448 * strcmp(hier, "DIRECT") &&
449 * strcmp(hier, "FIREWALL_IP_DIRECT") &&
450 * strcmp(hier, "LOCAL_IP_DIRECT") &&
451 * strcmp(hier, "NO_DIRECT_FAIL") &&
452 * strcmp(hier, "NO_PARENT_DIRECT") &&
453 * strcmp(hier, "SINGLE_PARENT") &&
454 * strcmp(hier, "PASSTHROUGH_PARENT") &&
455 * strcmp(hier, "SSL_PARENT_MISS") &&
456 * strcmp(hier, "DEFAULT_PARENT");
458 memcpy(entry
->key
, storeKeyPublic(url
, method_id
), sizeof(entry
->key
));
460 /*fprintf(stdout, "%s:%d: %s %s %s %s\n",
461 * fname, count, method, storeKeyText(entry->key), url, hier); */
466 cachePurge(Cache
* cache
, storeSwapLogData
* s
, int update_digest
)
468 CacheEntry
*olde
= (CacheEntry
*) hash_lookup(cache
->hash
, s
->key
);
471 ++ cache
->bad_del_count
;
473 assert(cache
->count
);
474 hash_remove_link(cache
->hash
, (hash_link
*) olde
);
477 cacheDigestDel(cache
->digest
, s
->key
);
479 cacheEntryDestroy(olde
);
486 cacheStore(Cache
* cache
, storeSwapLogData
* s
, int update_digest
)
488 CacheEntry
*olde
= (CacheEntry
*) hash_lookup(cache
->hash
, s
->key
);
491 ++ cache
->bad_add_count
;
493 CacheEntry
*e
= cacheEntryCreate(s
);
494 hash_join(cache
->hash
, (hash_link
*)&e
->key
);
498 cacheDigestAdd(cache
->digest
, e
->key
);
503 cacheUpdateStore(Cache
* cache
, storeSwapLogData
* s
, int update_digest
)
508 cacheStore(cache
, s
, update_digest
);
512 cachePurge(cache
, s
, update_digest
);
521 usage(const char *prg_name
)
523 fprintf(stderr
, "usage: %s <access_log> <swap_state> ...\n",
529 main(int argc
, char *argv
[])
531 FileIterator
**fis
= NULL
;
532 const int fi_count
= argc
- 1;
533 int active_fi_count
= 0;
539 return usage(argv
[0]);
541 them
= cacheCreate("them");
543 us
= cacheCreate("us");
549 fis
= (FileIterator
**)xcalloc(fi_count
, sizeof(FileIterator
*));
551 /* init iterators with files */
552 fis
[0] = fileIteratorCreate(argv
[1], accessLogReader
);
554 for (i
= 2; i
< argc
; ++i
)
555 fis
[i
- 1] = fileIteratorCreate(argv
[i
], swapStateReader
);
557 /* check that all files were found */
558 for (i
= 0; i
< fi_count
; ++i
)
562 /* read prefix to get start-up contents of the peer cache */
565 for (i
= 1; i
< fi_count
; ++i
) {
566 FileIterator
*fi
= fis
[i
];
568 while (fi
->inner_time
> 0) {
569 if (((storeSwapLogData
*) fi
->entry
)->op
== SWAP_LOG_DEL
) {
570 cachePurge(them
, (storeSwapLogData
*)fi
->entry
, 0);
573 ready_time
= fi
->inner_time
;
575 if (ready_time
> 0 && fi
->inner_time
> ready_time
)
578 cacheStore(them
, (storeSwapLogData
*)fi
->entry
, 0);
581 fileIteratorAdvance(fi
);
585 /* digest peer cache content */
586 cacheResetDigest(them
);
588 us
->digest
= cacheDigestClone(them
->digest
); /* @netw@ */
590 /* shift the time in access log to match ready_time */
591 fileIteratorSetCurTime(fis
[0], ready_time
);
593 /* iterate, use the iterator with the smallest positive inner_time */
598 time_t next_time
= -1;
601 for (i
= 0; i
< fi_count
; ++i
) {
602 if (fis
[i
]->inner_time
>= 0) {
603 if (!active_fi_count
|| fis
[i
]->inner_time
< next_time
) {
605 next_time
= fis
[i
]->inner_time
;
613 cur_time
= next_time
;
614 /*fprintf(stderr, "%2d time: %d %s", next_i, (int)cur_time, ctime(&cur_time)); */
617 cacheFetch(us
, (RawAccessLogEntry
*)fis
[next_i
]->entry
);
619 cacheUpdateStore(them
, (storeSwapLogData
*)fis
[next_i
]->entry
, 1);
621 fileIteratorAdvance(fis
[next_i
]);
623 } while (active_fi_count
);
630 cacheQueryReport(us
, &us
->qstats
);
633 for (i
= 0; i
< argc
- 1; ++i
) {
634 fileIteratorDestroy(fis
[i
]);