5 * AUTHOR: Alex Rousskov
7 * SQUID Web Proxy Cache http://www.squid-cache.org/
8 * ----------------------------------------------------------
10 * Squid is the result of efforts by numerous individuals from
11 * the Internet community; see the CONTRIBUTORS file for full
12 * details. Many organizations have provided support for Squid's
13 * development; see the SPONSORS file for full details. Squid is
14 * Copyrighted (C) 2001 by the Regents of the University of
15 * California; see the COPYRIGHT file for full details. Squid
16 * incorporates software developed and/or copyrighted by other
17 * sources; see the CREDITS file for full details.
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
36 * Test-suite for playing with cache digests
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 */
68 typedef struct _CacheEntry
{
71 struct _CacheEntry
*next
;
72 unsigned char key_arr
[SQUID_MD5_DIGEST_LENGTH
];
73 /* storeSwapLogData s; */
76 /* parsed access log entry */
79 cache_key key
[SQUID_MD5_DIGEST_LENGTH
];
81 short int use_icp
; /* true/false */
85 frError
= -2, frMore
= -1, frEof
= 0, frOk
= 1
88 typedef struct _FileIterator FileIterator
;
89 typedef fr_result(*FI_READER
) (FileIterator
* fi
);
91 struct _FileIterator
{
94 time_t inner_time
; /* timestamp of the current entry */
95 time_t time_offset
; /* to adjust time set by reader */
96 int line_count
; /* number of lines scanned */
97 int bad_line_count
; /* number of parsing errors */
98 int time_warp_count
; /* number of out-of-order entries in the file */
99 FI_READER reader
; /* reads next entry and updates inner_time */
100 void *entry
; /* buffer for the current entry, freed with xfree() */
104 static time_t cur_time
= -1; /* timestamp of the current log entry */
106 /* copied from url.c */
107 const char *RequestMethodStr
[] = {
118 /* copied from url.c */
119 static HttpRequestMethod
120 methodStrToId(const char *s
)
122 if (strcasecmp(s
, "GET") == 0) {
124 } else if (strcasecmp(s
, "POST") == 0) {
126 } else if (strcasecmp(s
, "PUT") == 0) {
128 } else if (strcasecmp(s
, "HEAD") == 0) {
130 } else if (strcasecmp(s
, "CONNECT") == 0) {
131 return METHOD_CONNECT
;
132 } else if (strcasecmp(s
, "TRACE") == 0) {
134 } else if (strcasecmp(s
, "PURGE") == 0) {
143 static void fileIteratorAdvance(FileIterator
* fi
);
145 static FileIterator
*
146 fileIteratorCreate(const char *fname
, FI_READER reader
)
148 FileIterator
*fi
= (FileIterator
*)xcalloc(1, sizeof(FileIterator
));
149 assert(fname
&& reader
);
152 fi
->file
= fopen(fname
, "r");
155 fprintf(stderr
, "cannot open %s: %s\n", fname
, strerror(errno
));
158 fprintf(stderr
, "opened %s\n", fname
);
160 fileIteratorAdvance(fi
);
166 fileIteratorDestroy(FileIterator
* fi
)
172 fprintf(stderr
, "closed %s\n", fi
->fname
);
180 fileIteratorSetCurTime(FileIterator
* fi
, time_t ct
)
183 assert(fi
->inner_time
> 0);
184 fi
->time_offset
= ct
- fi
->inner_time
;
188 fileIteratorAdvance(FileIterator
* fi
)
194 const time_t last_time
= fi
->inner_time
;
196 res
= fi
->reader(fi
);
199 if (fi
->inner_time
< 0)
200 fi
->inner_time
= last_time
;
202 fi
->inner_time
+= fi
->time_offset
;
205 ++ fi
->bad_line_count
;
206 else if (res
== frEof
) {
207 fprintf(stderr
, "exhausted %s (%d entries) at %s",
208 fi
->fname
, fi
->line_count
, ctime(&fi
->inner_time
));
210 } else if (fi
->inner_time
< last_time
) {
211 assert(last_time
>= 0);
212 ++ fi
->time_warp_count
;
213 fi
->inner_time
= last_time
;
216 /* report progress */
217 if (!(fi
->line_count
% 50000))
218 fprintf(stderr
, "%s scanned %d K entries (%d bad) at %s",
219 fi
->fname
, fi
->line_count
/ 1000, fi
->bad_line_count
,
220 ctime(&fi
->inner_time
));
227 cacheEntryCreate(const storeSwapLogData
* s
)
229 CacheEntry
*e
= (CacheEntry
*)xcalloc(1, sizeof(CacheEntry
));
232 memcpy(e
->key_arr
, s
->key
, SQUID_MD5_DIGEST_LENGTH
);
233 e
->key
= &e
->key_arr
[0];
238 cacheEntryDestroy(CacheEntry
* e
)
248 cacheCreate(const char *name
)
251 assert(name
&& strlen(name
));
252 c
= (Cache
*)xcalloc(1, sizeof(Cache
));
254 c
->hash
= hash_create(storeKeyHashCmp
, (int)2e6
, storeKeyHashHash
);
259 cacheDestroy(Cache
* cache
)
261 CacheEntry
*e
= NULL
;
265 /* destroy hash table contents */
268 while ((e
= (CacheEntry
*)hash_next(hash
))) {
269 hash_remove_link(hash
, (hash_link
*) e
);
270 cacheEntryDestroy(e
);
273 /* destroy the hash table itself */
274 hashFreeMemory(hash
);
277 cacheDigestDestroy(cache
->digest
);
282 /* re-digests currently hashed entries */
284 cacheResetDigest(Cache
* cache
)
286 CacheEntry
*e
= NULL
;
289 struct timeval t_start
, t_end
;
292 fprintf(stderr
, "%s: init-ing digest with %d entries\n", cache
->name
, cache
->count
);
295 cacheDigestDestroy(cache
->digest
);
299 cache
->digest
= cacheDigestCreate(cache
->count
+ 1, 6);
304 gettimeofday(&t_start
, NULL
);
308 while ((e
= (CacheEntry
*)hash_next(hash
))) {
309 cacheDigestAdd(cache
->digest
, e
->key
);
312 gettimeofday(&t_end
, NULL
);
313 assert(cache
->digest
->count
== cache
->count
);
314 fprintf(stderr
, "%s: init-ed digest with %d entries\n",
315 cache
->name
, cache
->digest
->count
);
316 fprintf(stderr
, "%s: init took: %f sec, %f sec/M\n",
318 tvSubDsec(t_start
, t_end
),
319 (double) 1e6
* tvSubDsec(t_start
, t_end
) / cache
->count
);
320 /* check how long it takes to traverse the hash */
321 gettimeofday(&t_start
, NULL
);
324 for (e
= (CacheEntry
*)hash_next(hash
); e
; e
= (CacheEntry
*)hash_next(hash
)) {}
326 gettimeofday(&t_end
, NULL
);
327 fprintf(stderr
, "%s: hash scan took: %f sec, %f sec/M\n",
329 tvSubDsec(t_start
, t_end
),
330 (double) 1e6
* tvSubDsec(t_start
, t_end
) / cache
->count
);
334 cacheQueryPeer(Cache
* cache
, const cache_key
* key
)
336 const int peer_has_it
= hash_lookup(cache
->peer
->hash
, key
) != NULL
;
337 const int we_think_we_have_it
= cacheDigestTest(cache
->digest
, key
);
339 ++ cache
->qstats
.query_count
;
342 if (we_think_we_have_it
)
343 ++ cache
->qstats
.true_hit_count
;
345 ++ cache
->qstats
.false_miss_count
;
347 if (we_think_we_have_it
)
348 ++ cache
->qstats
.false_hit_count
;
350 ++ cache
->qstats
.true_miss_count
;
355 cacheQueryReport(Cache
* cache
, CacheQueryStats
* stats
)
357 fprintf(stdout
, "%s: peer queries: %d (%d%%)\n",
359 stats
->query_count
, xpercentInt(stats
->query_count
, cache
->req_count
)
361 fprintf(stdout
, "%s: t-hit: %d (%d%%) t-miss: %d (%d%%) t-*: %d (%d%%)\n",
363 stats
->true_hit_count
, xpercentInt(stats
->true_hit_count
, stats
->query_count
),
364 stats
->true_miss_count
, xpercentInt(stats
->true_miss_count
, stats
->query_count
),
365 stats
->true_hit_count
+ stats
->true_miss_count
,
366 xpercentInt(stats
->true_hit_count
+ stats
->true_miss_count
, stats
->query_count
)
368 fprintf(stdout
, "%s: f-hit: %d (%d%%) f-miss: %d (%d%%) f-*: %d (%d%%)\n",
370 stats
->false_hit_count
, xpercentInt(stats
->false_hit_count
, stats
->query_count
),
371 stats
->false_miss_count
, xpercentInt(stats
->false_miss_count
, stats
->query_count
),
372 stats
->false_hit_count
+ stats
->false_miss_count
,
373 xpercentInt(stats
->false_hit_count
+ stats
->false_miss_count
, stats
->query_count
)
378 cacheReport(Cache
* cache
)
380 fprintf(stdout
, "%s: entries: %d reqs: %d bad-add: %d bad-del: %d\n",
381 cache
->name
, cache
->count
, cache
->req_count
,
382 cache
->bad_add_count
, cache
->bad_del_count
);
387 cacheFetch(Cache
* cache
, const RawAccessLogEntry
* e
)
393 cacheQueryPeer(cache
, e
->key
);
397 swapStateReader(FileIterator
* fi
)
399 storeSwapLogData
*entry
;
402 fi
->entry
= xcalloc(1, sizeof(storeSwapLogData
));
404 entry
= (storeSwapLogData
*)fi
->entry
;
406 if (fread(entry
, sizeof(*entry
), 1, fi
->file
) != 1)
409 fi
->inner_time
= entry
->lastref
;
411 if (entry
->op
!= SWAP_LOG_ADD
&& entry
->op
!= SWAP_LOG_DEL
) {
412 fprintf(stderr
, "%s:%d: unknown swap log action\n", fi
->fname
, fi
->line_count
);
420 accessLogReader(FileIterator
* fi
)
422 static char buf
[4096];
423 RawAccessLogEntry
*entry
;
426 HttpRequestMethod method_id
= METHOD_NONE
;
432 fi
->entry
= xcalloc(1, sizeof(RawAccessLogEntry
));
434 memset(fi
->entry
, 0, sizeof(RawAccessLogEntry
));
436 entry
= (RawAccessLogEntry
*)fi
->entry
;
438 if (!fgets(buf
, sizeof(buf
), fi
->file
))
439 return frEof
; /* eof */
441 entry
->timestamp
= fi
->inner_time
= (time_t) atoi(buf
);
443 url
= strstr(buf
, "://");
445 hier
= url
? strstr(url
, " - ") : NULL
;
448 /*fprintf(stderr, "%s:%d: strange access log entry '%s'\n",
449 * fname, scanned_count, buf); */
455 while (!xisdigit(*method
)) {
463 method_id
= methodStrToId(method
);
465 if (method_id
== METHOD_NONE
) {
466 /*fprintf(stderr, "%s:%d: invalid method %s in '%s'\n",
467 * fname, scanned_count, method, buf); */
480 *strchr(hier
, '/') = '\0';
482 /*fprintf(stdout, "%s:%d: %s %s %s\n",
483 * fname, count, method, url, hier); */
484 entry
->use_icp
= strcmp(hier
, "NONE");
486 /* no ICP lookup for these status codes */
487 /* strcmp(hier, "NONE") &&
488 * strcmp(hier, "DIRECT") &&
489 * strcmp(hier, "FIREWALL_IP_DIRECT") &&
490 * strcmp(hier, "LOCAL_IP_DIRECT") &&
491 * strcmp(hier, "NO_DIRECT_FAIL") &&
492 * strcmp(hier, "NO_PARENT_DIRECT") &&
493 * strcmp(hier, "SINGLE_PARENT") &&
494 * strcmp(hier, "PASSTHROUGH_PARENT") &&
495 * strcmp(hier, "SSL_PARENT_MISS") &&
496 * strcmp(hier, "DEFAULT_PARENT");
498 memcpy(entry
->key
, storeKeyPublic(url
, method_id
), sizeof(entry
->key
));
500 /*fprintf(stdout, "%s:%d: %s %s %s %s\n",
501 * fname, count, method, storeKeyText(entry->key), url, hier); */
507 cachePurge(Cache
* cache
, storeSwapLogData
* s
, int update_digest
)
509 CacheEntry
*olde
= (CacheEntry
*) hash_lookup(cache
->hash
, s
->key
);
512 ++ cache
->bad_del_count
;
514 assert(cache
->count
);
515 hash_remove_link(cache
->hash
, (hash_link
*) olde
);
518 cacheDigestDel(cache
->digest
, s
->key
);
520 cacheEntryDestroy(olde
);
527 cacheStore(Cache
* cache
, storeSwapLogData
* s
, int update_digest
)
529 CacheEntry
*olde
= (CacheEntry
*) hash_lookup(cache
->hash
, s
->key
);
532 ++ cache
->bad_add_count
;
534 CacheEntry
*e
= cacheEntryCreate(s
);
535 hash_join(cache
->hash
, (hash_link
*)&e
->key
);
539 cacheDigestAdd(cache
->digest
, e
->key
);
544 cacheUpdateStore(Cache
* cache
, storeSwapLogData
* s
, int update_digest
)
549 cacheStore(cache
, s
, update_digest
);
553 cachePurge(cache
, s
, update_digest
);
562 usage(const char *prg_name
)
564 fprintf(stderr
, "usage: %s <access_log> <swap_state> ...\n",
570 main(int argc
, char *argv
[])
572 FileIterator
**fis
= NULL
;
573 const int fi_count
= argc
- 1;
574 int active_fi_count
= 0;
580 return usage(argv
[0]);
582 them
= cacheCreate("them");
584 us
= cacheCreate("us");
590 fis
= (FileIterator
**)xcalloc(fi_count
, sizeof(FileIterator
*));
592 /* init iterators with files */
593 fis
[0] = fileIteratorCreate(argv
[1], accessLogReader
);
595 for (i
= 2; i
< argc
; ++i
)
596 fis
[i
- 1] = fileIteratorCreate(argv
[i
], swapStateReader
);
598 /* check that all files were found */
599 for (i
= 0; i
< fi_count
; ++i
)
603 /* read prefix to get start-up contents of the peer cache */
606 for (i
= 1; i
< fi_count
; ++i
) {
607 FileIterator
*fi
= fis
[i
];
609 while (fi
->inner_time
> 0) {
610 if (((storeSwapLogData
*) fi
->entry
)->op
== SWAP_LOG_DEL
) {
611 cachePurge(them
, (storeSwapLogData
*)fi
->entry
, 0);
614 ready_time
= fi
->inner_time
;
616 if (ready_time
> 0 && fi
->inner_time
> ready_time
)
619 cacheStore(them
, (storeSwapLogData
*)fi
->entry
, 0);
622 fileIteratorAdvance(fi
);
626 /* digest peer cache content */
627 cacheResetDigest(them
);
629 us
->digest
= cacheDigestClone(them
->digest
); /* @netw@ */
631 /* shift the time in access log to match ready_time */
632 fileIteratorSetCurTime(fis
[0], ready_time
);
634 /* iterate, use the iterator with the smallest positive inner_time */
639 time_t next_time
= -1;
642 for (i
= 0; i
< fi_count
; ++i
) {
643 if (fis
[i
]->inner_time
>= 0) {
644 if (!active_fi_count
|| fis
[i
]->inner_time
< next_time
) {
646 next_time
= fis
[i
]->inner_time
;
654 cur_time
= next_time
;
655 /*fprintf(stderr, "%2d time: %d %s", next_i, (int)cur_time, ctime(&cur_time)); */
658 cacheFetch(us
, (RawAccessLogEntry
*)fis
[next_i
]->entry
);
660 cacheUpdateStore(them
, (storeSwapLogData
*)fis
[next_i
]->entry
, 1);
662 fileIteratorAdvance(fis
[next_i
]);
664 } while (active_fi_count
);
671 cacheQueryReport(us
, &us
->qstats
);
674 for (i
= 0; i
< argc
- 1; ++i
) {
675 fileIteratorDestroy(fis
[i
]);