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
40 #include "CacheDigest.h"
41 #include "store_key_md5.h"
55 typedef struct _Cache Cache
;
62 CacheQueryStats qstats
;
63 int count
; /* #currently cached entries */
64 int req_count
; /* #requests to this cache */
65 int bad_add_count
; /* #duplicate adds */
66 int bad_del_count
; /* #dels with no prior add */
69 typedef struct _CacheEntry
{
72 struct _CacheEntry
*next
;
73 unsigned char key_arr
[SQUID_MD5_DIGEST_LENGTH
];
74 /* storeSwapLogData s; */
77 /* parsed access log entry */
80 cache_key key
[SQUID_MD5_DIGEST_LENGTH
];
82 short int use_icp
; /* true/false */
86 frError
= -2, frMore
= -1, frEof
= 0, frOk
= 1
89 typedef struct _FileIterator FileIterator
;
90 typedef fr_result(*FI_READER
) (FileIterator
* fi
);
92 struct _FileIterator
{
95 time_t inner_time
; /* timestamp of the current entry */
96 time_t time_offset
; /* to adjust time set by reader */
97 int line_count
; /* number of lines scanned */
98 int bad_line_count
; /* number of parsing errors */
99 int time_warp_count
; /* number of out-of-order entries in the file */
100 FI_READER reader
; /* reads next entry and updates inner_time */
101 void *entry
; /* buffer for the current entry, freed with xfree() */
105 static time_t cur_time
= -1; /* timestamp of the current log entry */
107 /* copied from url.c */
108 const char *RequestMethodStr
[] = {
119 /* copied from url.c */
120 static HttpRequestMethod
121 methodStrToId(const char *s
)
123 if (strcasecmp(s
, "GET") == 0) {
125 } else if (strcasecmp(s
, "POST") == 0) {
127 } else if (strcasecmp(s
, "PUT") == 0) {
129 } else if (strcasecmp(s
, "HEAD") == 0) {
131 } else if (strcasecmp(s
, "CONNECT") == 0) {
132 return METHOD_CONNECT
;
133 } else if (strcasecmp(s
, "TRACE") == 0) {
135 } else if (strcasecmp(s
, "PURGE") == 0) {
144 static void fileIteratorAdvance(FileIterator
* fi
);
146 static FileIterator
*
147 fileIteratorCreate(const char *fname
, FI_READER reader
)
149 FileIterator
*fi
= (FileIterator
*)xcalloc(1, sizeof(FileIterator
));
150 assert(fname
&& reader
);
153 fi
->file
= fopen(fname
, "r");
156 fprintf(stderr
, "cannot open %s: %s\n", fname
, strerror(errno
));
159 fprintf(stderr
, "opened %s\n", fname
);
161 fileIteratorAdvance(fi
);
167 fileIteratorDestroy(FileIterator
* fi
)
173 fprintf(stderr
, "closed %s\n", fi
->fname
);
181 fileIteratorSetCurTime(FileIterator
* fi
, time_t ct
)
184 assert(fi
->inner_time
> 0);
185 fi
->time_offset
= ct
- fi
->inner_time
;
189 fileIteratorAdvance(FileIterator
* fi
)
195 const time_t last_time
= fi
->inner_time
;
197 res
= fi
->reader(fi
);
200 if (fi
->inner_time
< 0)
201 fi
->inner_time
= last_time
;
203 fi
->inner_time
+= fi
->time_offset
;
206 ++ fi
->bad_line_count
;
207 else if (res
== frEof
) {
208 fprintf(stderr
, "exhausted %s (%d entries) at %s",
209 fi
->fname
, fi
->line_count
, ctime(&fi
->inner_time
));
211 } else if (fi
->inner_time
< last_time
) {
212 assert(last_time
>= 0);
213 ++ fi
->time_warp_count
;
214 fi
->inner_time
= last_time
;
217 /* report progress */
218 if (!(fi
->line_count
% 50000))
219 fprintf(stderr
, "%s scanned %d K entries (%d bad) at %s",
220 fi
->fname
, fi
->line_count
/ 1000, fi
->bad_line_count
,
221 ctime(&fi
->inner_time
));
228 cacheEntryCreate(const storeSwapLogData
* s
)
230 CacheEntry
*e
= (CacheEntry
*)xcalloc(1, sizeof(CacheEntry
));
233 memcpy(e
->key_arr
, s
->key
, SQUID_MD5_DIGEST_LENGTH
);
234 e
->key
= &e
->key_arr
[0];
239 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); */
506 cachePurge(Cache
* cache
, storeSwapLogData
* s
, int update_digest
)
508 CacheEntry
*olde
= (CacheEntry
*) hash_lookup(cache
->hash
, s
->key
);
511 ++ cache
->bad_del_count
;
513 assert(cache
->count
);
514 hash_remove_link(cache
->hash
, (hash_link
*) olde
);
517 cacheDigestDel(cache
->digest
, s
->key
);
519 cacheEntryDestroy(olde
);
526 cacheStore(Cache
* cache
, storeSwapLogData
* s
, int update_digest
)
528 CacheEntry
*olde
= (CacheEntry
*) hash_lookup(cache
->hash
, s
->key
);
531 ++ cache
->bad_add_count
;
533 CacheEntry
*e
= cacheEntryCreate(s
);
534 hash_join(cache
->hash
, (hash_link
*)&e
->key
);
538 cacheDigestAdd(cache
->digest
, e
->key
);
543 cacheUpdateStore(Cache
* cache
, storeSwapLogData
* s
, int update_digest
)
548 cacheStore(cache
, s
, update_digest
);
552 cachePurge(cache
, s
, update_digest
);
561 usage(const char *prg_name
)
563 fprintf(stderr
, "usage: %s <access_log> <swap_state> ...\n",
569 main(int argc
, char *argv
[])
571 FileIterator
**fis
= NULL
;
572 const int fi_count
= argc
- 1;
573 int active_fi_count
= 0;
579 return usage(argv
[0]);
581 them
= cacheCreate("them");
583 us
= cacheCreate("us");
589 fis
= (FileIterator
**)xcalloc(fi_count
, sizeof(FileIterator
*));
591 /* init iterators with files */
592 fis
[0] = fileIteratorCreate(argv
[1], accessLogReader
);
594 for (i
= 2; i
< argc
; ++i
)
595 fis
[i
- 1] = fileIteratorCreate(argv
[i
], swapStateReader
);
597 /* check that all files were found */
598 for (i
= 0; i
< fi_count
; ++i
)
602 /* read prefix to get start-up contents of the peer cache */
605 for (i
= 1; i
< fi_count
; ++i
) {
606 FileIterator
*fi
= fis
[i
];
608 while (fi
->inner_time
> 0) {
609 if (((storeSwapLogData
*) fi
->entry
)->op
== SWAP_LOG_DEL
) {
610 cachePurge(them
, (storeSwapLogData
*)fi
->entry
, 0);
613 ready_time
= fi
->inner_time
;
615 if (ready_time
> 0 && fi
->inner_time
> ready_time
)
618 cacheStore(them
, (storeSwapLogData
*)fi
->entry
, 0);
621 fileIteratorAdvance(fi
);
625 /* digest peer cache content */
626 cacheResetDigest(them
);
628 us
->digest
= cacheDigestClone(them
->digest
); /* @netw@ */
630 /* shift the time in access log to match ready_time */
631 fileIteratorSetCurTime(fis
[0], ready_time
);
633 /* iterate, use the iterator with the smallest positive inner_time */
638 time_t next_time
= -1;
641 for (i
= 0; i
< fi_count
; ++i
) {
642 if (fis
[i
]->inner_time
>= 0) {
643 if (!active_fi_count
|| fis
[i
]->inner_time
< next_time
) {
645 next_time
= fis
[i
]->inner_time
;
653 cur_time
= next_time
;
654 /*fprintf(stderr, "%2d time: %d %s", next_i, (int)cur_time, ctime(&cur_time)); */
657 cacheFetch(us
, (RawAccessLogEntry
*)fis
[next_i
]->entry
);
659 cacheUpdateStore(them
, (storeSwapLogData
*)fis
[next_i
]->entry
, 1);
661 fileIteratorAdvance(fis
[next_i
]);
663 } while (active_fi_count
);
670 cacheQueryReport(us
, &us
->qstats
);
673 for (i
= 0; i
< argc
- 1; ++i
) {
674 fileIteratorDestroy(fis
[i
]);