3 * AUTHOR: Alex Rousskov
5 * SQUID Web Proxy Cache http://www.squid-cache.org/
6 * ----------------------------------------------------------
8 * Squid is the result of efforts by numerous individuals from
9 * the Internet community; see the CONTRIBUTORS file for full
10 * details. Many organizations have provided support for Squid's
11 * development; see the SPONSORS file for full details. Squid is
12 * Copyrighted (C) 2001 by the Regents of the University of
13 * California; see the COPYRIGHT file for full details. Squid
14 * incorporates software developed and/or copyrighted by other
15 * sources; see the CREDITS file for full details.
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
34 * Test-suite for playing with cache digests
38 #include "CacheDigest.h"
39 #include "store_key_md5.h"
53 typedef struct _Cache Cache
;
60 CacheQueryStats qstats
;
61 int count
; /* #currently cached entries */
62 int req_count
; /* #requests to this cache */
63 int bad_add_count
; /* #duplicate adds */
64 int bad_del_count
; /* #dels with no prior add */
67 typedef struct _CacheEntry
{
70 struct _CacheEntry
*next
;
71 unsigned char key_arr
[SQUID_MD5_DIGEST_LENGTH
];
72 /* storeSwapLogData s; */
75 /* parsed access log entry */
78 cache_key key
[SQUID_MD5_DIGEST_LENGTH
];
80 short int use_icp
; /* true/false */
84 frError
= -2, frMore
= -1, frEof
= 0, frOk
= 1
87 typedef struct _FileIterator FileIterator
;
88 typedef fr_result(*FI_READER
) (FileIterator
* fi
);
90 struct _FileIterator
{
93 time_t inner_time
; /* timestamp of the current entry */
94 time_t time_offset
; /* to adjust time set by reader */
95 int line_count
; /* number of lines scanned */
96 int bad_line_count
; /* number of parsing errors */
97 int time_warp_count
; /* number of out-of-order entries in the file */
98 FI_READER reader
; /* reads next entry and updates inner_time */
99 void *entry
; /* buffer for the current entry, freed with xfree() */
103 static time_t cur_time
= -1; /* timestamp of the current log entry */
105 /* copied from url.c */
106 const char *RequestMethodStr
[] = {
117 /* copied from url.c */
118 static HttpRequestMethod
119 methodStrToId(const char *s
)
121 if (strcasecmp(s
, "GET") == 0) {
123 } else if (strcasecmp(s
, "POST") == 0) {
125 } else if (strcasecmp(s
, "PUT") == 0) {
127 } else if (strcasecmp(s
, "HEAD") == 0) {
129 } else if (strcasecmp(s
, "CONNECT") == 0) {
130 return METHOD_CONNECT
;
131 } else if (strcasecmp(s
, "TRACE") == 0) {
133 } else if (strcasecmp(s
, "PURGE") == 0) {
142 static void fileIteratorAdvance(FileIterator
* fi
);
144 static FileIterator
*
145 fileIteratorCreate(const char *fname
, FI_READER reader
)
147 FileIterator
*fi
= (FileIterator
*)xcalloc(1, sizeof(FileIterator
));
148 assert(fname
&& reader
);
151 fi
->file
= fopen(fname
, "r");
154 fprintf(stderr
, "cannot open %s: %s\n", fname
, strerror(errno
));
157 fprintf(stderr
, "opened %s\n", fname
);
159 fileIteratorAdvance(fi
);
165 fileIteratorDestroy(FileIterator
* fi
)
171 fprintf(stderr
, "closed %s\n", fi
->fname
);
179 fileIteratorSetCurTime(FileIterator
* fi
, time_t ct
)
182 assert(fi
->inner_time
> 0);
183 fi
->time_offset
= ct
- fi
->inner_time
;
187 fileIteratorAdvance(FileIterator
* fi
)
193 const time_t last_time
= fi
->inner_time
;
195 res
= fi
->reader(fi
);
198 if (fi
->inner_time
< 0)
199 fi
->inner_time
= last_time
;
201 fi
->inner_time
+= fi
->time_offset
;
204 ++ fi
->bad_line_count
;
205 else if (res
== frEof
) {
206 fprintf(stderr
, "exhausted %s (%d entries) at %s",
207 fi
->fname
, fi
->line_count
, ctime(&fi
->inner_time
));
209 } else if (fi
->inner_time
< last_time
) {
210 assert(last_time
>= 0);
211 ++ fi
->time_warp_count
;
212 fi
->inner_time
= last_time
;
215 /* report progress */
216 if (!(fi
->line_count
% 50000))
217 fprintf(stderr
, "%s scanned %d K entries (%d bad) at %s",
218 fi
->fname
, fi
->line_count
/ 1000, fi
->bad_line_count
,
219 ctime(&fi
->inner_time
));
226 cacheEntryCreate(const storeSwapLogData
* s
)
228 CacheEntry
*e
= (CacheEntry
*)xcalloc(1, sizeof(CacheEntry
));
231 memcpy(e
->key_arr
, s
->key
, SQUID_MD5_DIGEST_LENGTH
);
232 e
->key
= &e
->key_arr
[0];
237 cacheEntryDestroy(CacheEntry
* e
)
246 cacheCreate(const char *name
)
249 assert(name
&& strlen(name
));
250 c
= (Cache
*)xcalloc(1, sizeof(Cache
));
252 c
->hash
= hash_create(storeKeyHashCmp
, (int)2e6
, storeKeyHashHash
);
257 cacheDestroy(Cache
* cache
)
259 CacheEntry
*e
= NULL
;
263 /* destroy hash table contents */
266 while ((e
= (CacheEntry
*)hash_next(hash
))) {
267 hash_remove_link(hash
, (hash_link
*) e
);
268 cacheEntryDestroy(e
);
271 /* destroy the hash table itself */
272 hashFreeMemory(hash
);
275 cacheDigestDestroy(cache
->digest
);
280 /* re-digests currently hashed entries */
282 cacheResetDigest(Cache
* cache
)
284 CacheEntry
*e
= NULL
;
287 struct timeval t_start
, t_end
;
290 fprintf(stderr
, "%s: init-ing digest with %d entries\n", cache
->name
, cache
->count
);
293 cacheDigestDestroy(cache
->digest
);
297 cache
->digest
= cacheDigestCreate(cache
->count
+ 1, 6);
302 gettimeofday(&t_start
, NULL
);
306 while ((e
= (CacheEntry
*)hash_next(hash
))) {
307 cacheDigestAdd(cache
->digest
, e
->key
);
310 gettimeofday(&t_end
, NULL
);
311 assert(cache
->digest
->count
== cache
->count
);
312 fprintf(stderr
, "%s: init-ed digest with %d entries\n",
313 cache
->name
, cache
->digest
->count
);
314 fprintf(stderr
, "%s: init took: %f sec, %f sec/M\n",
316 tvSubDsec(t_start
, t_end
),
317 (double) 1e6
* tvSubDsec(t_start
, t_end
) / cache
->count
);
318 /* check how long it takes to traverse the hash */
319 gettimeofday(&t_start
, NULL
);
322 for (e
= (CacheEntry
*)hash_next(hash
); e
; e
= (CacheEntry
*)hash_next(hash
)) {}
324 gettimeofday(&t_end
, NULL
);
325 fprintf(stderr
, "%s: hash scan took: %f sec, %f sec/M\n",
327 tvSubDsec(t_start
, t_end
),
328 (double) 1e6
* tvSubDsec(t_start
, t_end
) / cache
->count
);
332 cacheQueryPeer(Cache
* cache
, const cache_key
* key
)
334 const int peer_has_it
= hash_lookup(cache
->peer
->hash
, key
) != NULL
;
335 const int we_think_we_have_it
= cacheDigestTest(cache
->digest
, key
);
337 ++ cache
->qstats
.query_count
;
340 if (we_think_we_have_it
)
341 ++ cache
->qstats
.true_hit_count
;
343 ++ cache
->qstats
.false_miss_count
;
345 if (we_think_we_have_it
)
346 ++ cache
->qstats
.false_hit_count
;
348 ++ cache
->qstats
.true_miss_count
;
353 cacheQueryReport(Cache
* cache
, CacheQueryStats
* stats
)
355 fprintf(stdout
, "%s: peer queries: %d (%d%%)\n",
357 stats
->query_count
, xpercentInt(stats
->query_count
, cache
->req_count
)
359 fprintf(stdout
, "%s: t-hit: %d (%d%%) t-miss: %d (%d%%) t-*: %d (%d%%)\n",
361 stats
->true_hit_count
, xpercentInt(stats
->true_hit_count
, stats
->query_count
),
362 stats
->true_miss_count
, xpercentInt(stats
->true_miss_count
, stats
->query_count
),
363 stats
->true_hit_count
+ stats
->true_miss_count
,
364 xpercentInt(stats
->true_hit_count
+ stats
->true_miss_count
, stats
->query_count
)
366 fprintf(stdout
, "%s: f-hit: %d (%d%%) f-miss: %d (%d%%) f-*: %d (%d%%)\n",
368 stats
->false_hit_count
, xpercentInt(stats
->false_hit_count
, stats
->query_count
),
369 stats
->false_miss_count
, xpercentInt(stats
->false_miss_count
, stats
->query_count
),
370 stats
->false_hit_count
+ stats
->false_miss_count
,
371 xpercentInt(stats
->false_hit_count
+ stats
->false_miss_count
, stats
->query_count
)
376 cacheReport(Cache
* cache
)
378 fprintf(stdout
, "%s: entries: %d reqs: %d bad-add: %d bad-del: %d\n",
379 cache
->name
, cache
->count
, cache
->req_count
,
380 cache
->bad_add_count
, cache
->bad_del_count
);
385 cacheFetch(Cache
* cache
, const RawAccessLogEntry
* e
)
391 cacheQueryPeer(cache
, e
->key
);
395 swapStateReader(FileIterator
* fi
)
397 storeSwapLogData
*entry
;
400 fi
->entry
= xcalloc(1, sizeof(storeSwapLogData
));
402 entry
= (storeSwapLogData
*)fi
->entry
;
404 if (fread(entry
, sizeof(*entry
), 1, fi
->file
) != 1)
407 fi
->inner_time
= entry
->lastref
;
409 if (entry
->op
!= SWAP_LOG_ADD
&& entry
->op
!= SWAP_LOG_DEL
) {
410 fprintf(stderr
, "%s:%d: unknown swap log action\n", fi
->fname
, fi
->line_count
);
418 accessLogReader(FileIterator
* fi
)
420 static char buf
[4096];
421 RawAccessLogEntry
*entry
;
424 HttpRequestMethod method_id
= METHOD_NONE
;
430 fi
->entry
= xcalloc(1, sizeof(RawAccessLogEntry
));
432 memset(fi
->entry
, 0, sizeof(RawAccessLogEntry
));
434 entry
= (RawAccessLogEntry
*)fi
->entry
;
436 if (!fgets(buf
, sizeof(buf
), fi
->file
))
437 return frEof
; /* eof */
439 entry
->timestamp
= fi
->inner_time
= (time_t) atoi(buf
);
441 url
= strstr(buf
, "://");
443 hier
= url
? strstr(url
, " - ") : NULL
;
446 /*fprintf(stderr, "%s:%d: strange access log entry '%s'\n",
447 * fname, scanned_count, buf); */
453 while (!xisdigit(*method
)) {
461 method_id
= methodStrToId(method
);
463 if (method_id
== METHOD_NONE
) {
464 /*fprintf(stderr, "%s:%d: invalid method %s in '%s'\n",
465 * fname, scanned_count, method, buf); */
478 *strchr(hier
, '/') = '\0';
480 /*fprintf(stdout, "%s:%d: %s %s %s\n",
481 * fname, count, method, url, hier); */
482 entry
->use_icp
= strcmp(hier
, "NONE");
484 /* no ICP lookup for these status codes */
485 /* strcmp(hier, "NONE") &&
486 * strcmp(hier, "DIRECT") &&
487 * strcmp(hier, "FIREWALL_IP_DIRECT") &&
488 * strcmp(hier, "LOCAL_IP_DIRECT") &&
489 * strcmp(hier, "NO_DIRECT_FAIL") &&
490 * strcmp(hier, "NO_PARENT_DIRECT") &&
491 * strcmp(hier, "SINGLE_PARENT") &&
492 * strcmp(hier, "PASSTHROUGH_PARENT") &&
493 * strcmp(hier, "SSL_PARENT_MISS") &&
494 * strcmp(hier, "DEFAULT_PARENT");
496 memcpy(entry
->key
, storeKeyPublic(url
, method_id
), sizeof(entry
->key
));
498 /*fprintf(stdout, "%s:%d: %s %s %s %s\n",
499 * fname, count, method, storeKeyText(entry->key), url, hier); */
504 cachePurge(Cache
* cache
, storeSwapLogData
* s
, int update_digest
)
506 CacheEntry
*olde
= (CacheEntry
*) hash_lookup(cache
->hash
, s
->key
);
509 ++ cache
->bad_del_count
;
511 assert(cache
->count
);
512 hash_remove_link(cache
->hash
, (hash_link
*) olde
);
515 cacheDigestDel(cache
->digest
, s
->key
);
517 cacheEntryDestroy(olde
);
524 cacheStore(Cache
* cache
, storeSwapLogData
* s
, int update_digest
)
526 CacheEntry
*olde
= (CacheEntry
*) hash_lookup(cache
->hash
, s
->key
);
529 ++ cache
->bad_add_count
;
531 CacheEntry
*e
= cacheEntryCreate(s
);
532 hash_join(cache
->hash
, (hash_link
*)&e
->key
);
536 cacheDigestAdd(cache
->digest
, e
->key
);
541 cacheUpdateStore(Cache
* cache
, storeSwapLogData
* s
, int update_digest
)
546 cacheStore(cache
, s
, update_digest
);
550 cachePurge(cache
, s
, update_digest
);
559 usage(const char *prg_name
)
561 fprintf(stderr
, "usage: %s <access_log> <swap_state> ...\n",
567 main(int argc
, char *argv
[])
569 FileIterator
**fis
= NULL
;
570 const int fi_count
= argc
- 1;
571 int active_fi_count
= 0;
577 return usage(argv
[0]);
579 them
= cacheCreate("them");
581 us
= cacheCreate("us");
587 fis
= (FileIterator
**)xcalloc(fi_count
, sizeof(FileIterator
*));
589 /* init iterators with files */
590 fis
[0] = fileIteratorCreate(argv
[1], accessLogReader
);
592 for (i
= 2; i
< argc
; ++i
)
593 fis
[i
- 1] = fileIteratorCreate(argv
[i
], swapStateReader
);
595 /* check that all files were found */
596 for (i
= 0; i
< fi_count
; ++i
)
600 /* read prefix to get start-up contents of the peer cache */
603 for (i
= 1; i
< fi_count
; ++i
) {
604 FileIterator
*fi
= fis
[i
];
606 while (fi
->inner_time
> 0) {
607 if (((storeSwapLogData
*) fi
->entry
)->op
== SWAP_LOG_DEL
) {
608 cachePurge(them
, (storeSwapLogData
*)fi
->entry
, 0);
611 ready_time
= fi
->inner_time
;
613 if (ready_time
> 0 && fi
->inner_time
> ready_time
)
616 cacheStore(them
, (storeSwapLogData
*)fi
->entry
, 0);
619 fileIteratorAdvance(fi
);
623 /* digest peer cache content */
624 cacheResetDigest(them
);
626 us
->digest
= cacheDigestClone(them
->digest
); /* @netw@ */
628 /* shift the time in access log to match ready_time */
629 fileIteratorSetCurTime(fis
[0], ready_time
);
631 /* iterate, use the iterator with the smallest positive inner_time */
636 time_t next_time
= -1;
639 for (i
= 0; i
< fi_count
; ++i
) {
640 if (fis
[i
]->inner_time
>= 0) {
641 if (!active_fi_count
|| fis
[i
]->inner_time
< next_time
) {
643 next_time
= fis
[i
]->inner_time
;
651 cur_time
= next_time
;
652 /*fprintf(stderr, "%2d time: %d %s", next_i, (int)cur_time, ctime(&cur_time)); */
655 cacheFetch(us
, (RawAccessLogEntry
*)fis
[next_i
]->entry
);
657 cacheUpdateStore(them
, (storeSwapLogData
*)fis
[next_i
]->entry
, 1);
659 fileIteratorAdvance(fis
[next_i
]);
661 } while (active_fi_count
);
668 cacheQueryReport(us
, &us
->qstats
);
671 for (i
= 0; i
< argc
- 1; ++i
) {
672 fileIteratorDestroy(fis
[i
]);