]> git.ipfire.org Git - thirdparty/squid.git/blob - src/test_cache_digest.cc
- fixed digest size report
[thirdparty/squid.git] / src / test_cache_digest.cc
1
2 /*
3 * $Id: test_cache_digest.cc,v 1.10 1998/04/01 07:07:17 rousskov Exp $
4 *
5 * AUTHOR: Alex Rousskov
6 *
7 * SQUID Internet Object Cache http://squid.nlanr.net/Squid/
8 * --------------------------------------------------------
9 *
10 * Squid is the result of efforts by numerous individuals from the
11 * Internet community. Development is led by Duane Wessels of the
12 * National Laboratory for Applied Network Research and funded by
13 * the National Science Foundation.
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 *
29 */
30
31 /*
32 * Test-suite for playing with cache digests
33 */
34
35 #include "squid.h"
36
37 typedef struct {
38 int query_count;
39 int true_hit_count;
40 int true_miss_count;
41 int false_hit_count;
42 int false_miss_count;
43 } CacheQueryStats;
44
45 typedef struct _Cache Cache;
46 struct _Cache {
47 const char *name;
48 hash_table *hash;
49 CacheDigest *digest;
50 Cache *peer;
51 CacheQueryStats qstats;
52 int count; /* #currently cached entries */
53 int req_count; /* #requests to this cache */
54 int bad_add_count; /* #duplicate adds */
55 int bad_del_count; /* #dels with no prior add */
56 };
57
58
59 typedef struct _CacheEntry {
60 const cache_key *key;
61 struct _CacheEntry *next;
62 unsigned char key_arr[MD5_DIGEST_CHARS];
63 /* storeSwapLogData s; */
64 } CacheEntry;
65
66 /* parsed access log entry */
67 typedef struct {
68 cache_key key[MD5_DIGEST_CHARS];
69 time_t timestamp;
70 short int use_icp; /* true/false */
71 } RawAccessLogEntry;
72
73 typedef enum { frError = -2, frMore = -1, frEof = 0, frOk = 1 } fr_result;
74 typedef struct _FileIterator FileIterator;
75 typedef fr_result (*FI_READER)(FileIterator *fi);
76
77 struct _FileIterator {
78 const char *fname;
79 FILE *file;
80 time_t inner_time; /* timestamp of the current entry */
81 time_t time_offset; /* to adjust time set by reader */
82 int line_count; /* number of lines scanned */
83 int bad_line_count; /* number of parsing errors */
84 int time_warp_count;/* number of out-of-order entries in the file */
85 FI_READER reader; /* reads next entry and updates inner_time */
86 void *entry; /* buffer for the current entry, freed with xfree() */
87 };
88
89 /* globals */
90 static time_t cur_time = -1; /* timestamp of the current log entry */
91
92 #if 0
93
94 static int cacheIndexScanCleanPrefix(CacheIndex * idx, const char *fname, FILE * file);
95 static int cacheIndexScanAccessLog(CacheIndex * idx, const char *fname, FILE * file);
96
97 #endif
98
99 /* copied from url.c */
100 const char *RequestMethodStr[] =
101 {
102 "NONE",
103 "GET",
104 "POST",
105 "PUT",
106 "HEAD",
107 "CONNECT",
108 "TRACE",
109 "PURGE"
110 };
111 /* copied from url.c */
112 static method_t
113 methodStrToId(const char *s)
114 {
115 if (strcasecmp(s, "GET") == 0) {
116 return METHOD_GET;
117 } else if (strcasecmp(s, "POST") == 0) {
118 return METHOD_POST;
119 } else if (strcasecmp(s, "PUT") == 0) {
120 return METHOD_PUT;
121 } else if (strcasecmp(s, "HEAD") == 0) {
122 return METHOD_HEAD;
123 } else if (strcasecmp(s, "CONNECT") == 0) {
124 return METHOD_CONNECT;
125 } else if (strcasecmp(s, "TRACE") == 0) {
126 return METHOD_TRACE;
127 } else if (strcasecmp(s, "PURGE") == 0) {
128 return METHOD_PURGE;
129 }
130 return METHOD_NONE;
131 }
132
133 /* FileIterator */
134
135 static void fileIteratorAdvance(FileIterator *fi);
136
137 static FileIterator *
138 fileIteratorCreate(const char *fname, FI_READER reader)
139 {
140 FileIterator *fi = xcalloc(1, sizeof(FileIterator));
141 assert(fname && reader);
142 fi->fname = fname;
143 fi->reader = reader;
144 fi->file = fopen(fname, "r");
145 if (!fi->file) {
146 fprintf(stderr, "cannot open %s: %s\n", fname, strerror(errno));
147 return NULL;
148 } else
149 fprintf(stderr, "opened %s\n", fname);
150 fileIteratorAdvance(fi);
151 return fi;
152 }
153
154 static void
155 fileIteratorDestroy(FileIterator *fi)
156 {
157 assert(fi);
158 if (fi->file) {
159 fclose(fi->file);
160 fprintf(stderr, "closed %s\n", fi->fname);
161 }
162 xfree(fi->entry);
163 xfree(fi);
164 }
165
166 static void
167 fileIteratorSetCurTime(FileIterator *fi, time_t ct)
168 {
169 assert(fi);
170 assert(fi->inner_time > 0);
171 fi->time_offset = ct - fi->inner_time;
172 }
173
174 static void
175 fileIteratorAdvance(FileIterator *fi)
176 {
177 int res;
178 assert(fi);
179 do {
180 const time_t last_time = fi->inner_time;
181 fi->inner_time = -1;
182 res = fi->reader(fi);
183 fi->line_count++;
184 if (fi->inner_time < 0)
185 fi->inner_time = last_time;
186 else
187 fi->inner_time += fi->time_offset;
188 if (res == frError)
189 fi->bad_line_count++;
190 else
191 if (res == frEof) {
192 fprintf(stderr, "exhausted %s (%d entries) at %s",
193 fi->fname, fi->line_count, ctime(&fi->inner_time));
194 fi->inner_time = -1;
195 } else
196 if (fi->inner_time < last_time) {
197 assert(last_time >= 0);
198 fi->time_warp_count++;
199 fi->inner_time = last_time;
200 }
201 /* report progress */
202 if (!(fi->line_count % 50000))
203 fprintf(stderr, "%s scanned %d K entries (%d bad) at %s",
204 fi->fname, fi->line_count / 1000, fi->bad_line_count,
205 ctime(&fi->inner_time));
206 } while (res < 0);
207 }
208
209 /* CacheEntry */
210
211 static CacheEntry *
212 cacheEntryCreate(const storeSwapLogData * s)
213 {
214 CacheEntry *e = xcalloc(1, sizeof(CacheEntry));
215 assert(s);
216 /* e->s = *s; */
217 xmemcpy(e->key_arr, s->key, MD5_DIGEST_CHARS);
218 e->key = &e->key_arr[0];
219 return e;
220 }
221
222 static void
223 cacheEntryDestroy(CacheEntry * e)
224 {
225 assert(e);
226 xfree(e);
227 }
228
229
230 /* Cache */
231
232 static Cache *
233 cacheCreate(const char *name)
234 {
235 Cache *c;
236 assert(name && strlen(name));
237 c = xcalloc(1, sizeof(Cache));
238 c->name = name;
239 c->hash = hash_create(storeKeyHashCmp, 2e6, storeKeyHashHash);
240 return c;
241 }
242
243 static void
244 cacheDestroy(Cache * cache)
245 {
246 CacheEntry *e = NULL;
247 hash_table *hash;
248 assert(cache);
249 hash = cache->hash;
250 /* destroy hash table contents */
251 for (e = hash_first(hash); e; e = hash_next(hash)) {
252 hash_remove_link(hash, (hash_link*)e);
253 cacheEntryDestroy(e);
254 }
255 /* destroy the hash table itself */
256 hashFreeMemory(hash);
257 if (cache->digest)
258 cacheDigestDestroy(cache->digest);
259 xfree(cache);
260 }
261
262 /* re-digests currently hashed entries */
263 static void
264 cacheResetDigest(Cache * cache)
265 {
266 CacheEntry *e = NULL;
267 hash_table *hash;
268 struct timeval t_start, t_end;
269
270 assert(cache);
271 fprintf(stderr, "%s: init-ing digest with %d entries\n", cache->name, cache->count);
272 if (cache->digest)
273 cacheDigestDestroy(cache->digest);
274 hash = cache->hash;
275 cache->digest = cacheDigestCreate(2 * cache->count + 1); /* 50% utilization */
276 if (!cache->count)
277 return;
278 gettimeofday(&t_start, NULL);
279 for (e = hash_first(hash); e; e = hash_next(hash)) {
280 cacheDigestAdd(cache->digest, e->key);
281 }
282 gettimeofday(&t_end, NULL);
283 assert(cache->digest->count == cache->count);
284 fprintf(stderr, "%s: init-ed digest with %d entries\n",
285 cache->name, cache->digest->count);
286 fprintf(stderr, "%s: init took: %f sec, %f sec/M\n",
287 cache->name,
288 tvSubDsec(t_start, t_end),
289 (double) 1e6 * tvSubDsec(t_start, t_end) / cache->count);
290 /* check how long it takes to traverse the hash */
291 gettimeofday(&t_start, NULL);
292 for (e = hash_first(hash); e; e = hash_next(hash)) {
293 }
294 gettimeofday(&t_end, NULL);
295 fprintf(stderr, "%s: hash scan took: %f sec, %f sec/M\n",
296 cache->name,
297 tvSubDsec(t_start, t_end),
298 (double) 1e6 * tvSubDsec(t_start, t_end) / cache->count);
299 }
300
301 static void
302 cacheQueryPeer(Cache * cache, const cache_key * key)
303 {
304 const int peer_has_it = hash_lookup(cache->peer->hash, key) != NULL;
305 const int we_think_we_have_it = cacheDigestTest(cache->digest, key);
306
307 cache->qstats.query_count++;
308 if (peer_has_it) {
309 if (we_think_we_have_it)
310 cache->qstats.true_hit_count++;
311 else
312 cache->qstats.false_miss_count++;
313 } else {
314 if (we_think_we_have_it)
315 cache->qstats.false_hit_count++;
316 else
317 cache->qstats.true_miss_count++;
318 }
319 }
320
321 static void
322 cacheQueryReport(Cache * cache, CacheQueryStats *stats)
323 {
324 fprintf(stdout, "%s: peer queries: %d (%d%%)\n",
325 cache->name,
326 stats->query_count, xpercentInt(stats->query_count, cache->req_count)
327 );
328 fprintf(stdout, "%s: t-hit: %d (%d%%) t-miss: %d (%d%%) t-*: %d (%d%%)\n",
329 cache->name,
330 stats->true_hit_count, xpercentInt(stats->true_hit_count, stats->query_count),
331 stats->true_miss_count, xpercentInt(stats->true_miss_count, stats->query_count),
332 stats->true_hit_count + stats->true_miss_count,
333 xpercentInt(stats->true_hit_count + stats->true_miss_count, stats->query_count)
334 );
335 fprintf(stdout, "%s: f-hit: %d (%d%%) f-miss: %d (%d%%) f-*: %d (%d%%)\n",
336 cache->name,
337 stats->false_hit_count, xpercentInt(stats->false_hit_count, stats->query_count),
338 stats->false_miss_count, xpercentInt(stats->false_miss_count, stats->query_count),
339 stats->false_hit_count + stats->false_miss_count,
340 xpercentInt(stats->false_hit_count + stats->false_miss_count, stats->query_count)
341 );
342 }
343
344 static void
345 cacheReport(Cache * cache)
346 {
347 fprintf(stdout, "%s: entries: %d reqs: %d bad-add: %d bad-del: %d\n",
348 cache->name, cache->count, cache->req_count,
349 cache->bad_add_count, cache->bad_del_count);
350
351 if (cache->digest) {
352 int bit_count, on_count;
353 cacheDigestUtil(cache->digest, &bit_count, &on_count);
354 fprintf(stdout, "%s: digest entries: cnt: %d cap: %d util: %d%% size: %d b\n",
355 cache->name,
356 cache->digest->count, cache->digest->capacity,
357 xpercentInt(cache->digest->count, cache->digest->capacity),
358 bit_count/8
359 );
360 fprintf(stdout, "%s: digest bits: on: %d cap: %d util: %d%%\n",
361 cache->name,
362 on_count, bit_count,
363 xpercentInt(on_count, bit_count)
364 );
365 }
366 }
367
368 static void
369 cacheFetch(Cache *cache, const RawAccessLogEntry *e)
370 {
371 assert(e);
372 cache->req_count++;
373 if (e->use_icp)
374 cacheQueryPeer(cache, e->key);
375 }
376
377 static fr_result
378 swapStateReader(FileIterator * fi)
379 {
380 storeSwapLogData *entry;
381 if (!fi->entry)
382 fi->entry = xcalloc(1, sizeof(storeSwapLogData));
383 entry = fi->entry;
384 if (fread(entry, sizeof(*entry), 1, fi->file) != 1)
385 return frEof;
386 fi->inner_time = entry->lastref;
387 if (entry->op != SWAP_LOG_ADD && entry->op != SWAP_LOG_DEL) {
388 fprintf(stderr, "%s:%d: unknown swap log action\n", fi->fname, fi->line_count);
389 exit(-3);
390 }
391 return frOk;
392 }
393
394 static fr_result
395 accessLogReader(FileIterator * fi)
396 {
397 static char buf[4096];
398 RawAccessLogEntry *entry;
399 char *url;
400 char *method;
401 int method_id = METHOD_NONE;
402 char *hier = NULL;
403
404 assert(fi);
405 if (!fi->entry)
406 fi->entry = xcalloc(1, sizeof(RawAccessLogEntry));
407 else
408 memset(fi->entry, 0, sizeof(RawAccessLogEntry));
409 entry = fi->entry;
410 if (!fgets(buf, sizeof(buf), fi->file))
411 return frEof; /* eof */
412 entry->timestamp = fi->inner_time = (time_t)atoi(buf);
413 url = strstr(buf, "://");
414 hier = url ? strstr(url, " - ") : NULL;
415
416 if (!url || !hier) {
417 /*fprintf(stderr, "%s:%d: strange access log entry '%s'\n",
418 * fname, scanned_count, buf); */
419 return frError;
420 }
421 method = url;
422 while (!isdigit(*method)) {
423 if (*method == ' ')
424 *method = '\0';
425 --method;
426 }
427 method += 2;
428 method_id = methodStrToId(method);
429 if (method_id == METHOD_NONE) {
430 /*fprintf(stderr, "%s:%d: invalid method %s in '%s'\n",
431 * fname, scanned_count, method, buf); */
432 return frError;
433 }
434 while (*url) url--;
435 url++;
436 *hier = '\0';
437 hier += 3;
438 *strchr(hier, '/') = '\0';
439 /*fprintf(stdout, "%s:%d: %s %s %s\n",
440 * fname, count, method, url, hier); */
441 entry->use_icp = /* no ICP lookup for these status codes */
442 strcmp(hier, "NONE") &&
443 strcmp(hier, "DIRECT") &&
444 strcmp(hier, "FIREWALL_IP_DIRECT") &&
445 strcmp(hier, "LOCAL_IP_DIRECT") &&
446 strcmp(hier, "NO_DIRECT_FAIL") &&
447 strcmp(hier, "NO_PARENT_DIRECT") &&
448 strcmp(hier, "SINGLE_PARENT") &&
449 strcmp(hier, "PASSTHROUGH_PARENT") &&
450 strcmp(hier, "SSL_PARENT_MISS") &&
451 strcmp(hier, "DEFAULT_PARENT");
452 memcpy(entry->key, storeKeyPublic(url, method_id), sizeof(entry->key));
453 /*fprintf(stdout, "%s:%d: %s %s %s %s\n",
454 fname, count, method, storeKeyText(entry->key), url, hier); */
455 return frOk;
456 }
457
458
459 static void
460 cachePurge(Cache *cache, storeSwapLogData *s)
461 {
462 CacheEntry *olde = (CacheEntry *) hash_lookup(cache->hash, s->key);
463 if (!olde) {
464 cache->bad_del_count++;
465 } else {
466 assert(cache->count);
467 hash_remove_link(cache->hash, (hash_link *) olde);
468 cacheEntryDestroy(olde);
469 cache->count--;
470 }
471 }
472
473 static void
474 cacheStore(Cache *cache, storeSwapLogData *s, int update_digest)
475 {
476 CacheEntry *olde = (CacheEntry *) hash_lookup(cache->hash, s->key);
477 if (olde) {
478 cache->bad_add_count++;
479 } else {
480 CacheEntry *e = cacheEntryCreate(s);
481 hash_join(cache->hash, (hash_link *) e);
482 cache->count++;
483 if (update_digest)
484 cacheDigestAdd(cache->digest, e->key);
485 }
486 }
487
488 static void
489 cacheUpdateStore(Cache *cache, storeSwapLogData *s, int update_digest)
490 {
491 switch (s->op) {
492 case SWAP_LOG_ADD:
493 cacheStore(cache, s, update_digest);
494 break;
495 case SWAP_LOG_DEL:
496 cachePurge(cache, s);
497 break;
498 default:
499 assert(0);
500 }
501 }
502
503 static int
504 usage(const char *prg_name)
505 {
506 fprintf(stderr, "usage: %s <access_log> <swap_state> ...\n",
507 prg_name);
508 return -1;
509 }
510
511 int
512 main(int argc, char *argv[])
513 {
514 FileIterator **fis = NULL;
515 const int fi_count = argc-1;
516 int active_fi_count = 0;
517 time_t ready_time;
518 Cache *them, *us;
519 int i;
520
521 if (argc < 3)
522 return usage(argv[0]);
523
524 them = cacheCreate("them");
525 us = cacheCreate("us");
526 them->peer = us;
527 us->peer = them;
528
529 fis = xcalloc(fi_count, sizeof(FileIterator *));
530 /* init iterators with files */
531 fis[0] = fileIteratorCreate(argv[1], accessLogReader);
532 for (i = 2; i < argc; ++i) {
533 fis[i-1] = fileIteratorCreate(argv[i], swapStateReader);
534 if (!fis[i-1]) return -2;
535 }
536 /* read prefix to get start-up contents of the peer cache */
537 ready_time = -1;
538 for (i = 1; i < fi_count; ++i) {
539 FileIterator *fi = fis[i];
540 while (fi->inner_time > 0) {
541 ready_time = fi->inner_time;
542 if (((storeSwapLogData*)fi->entry)->op != SWAP_LOG_ADD) {
543 break;
544 } else {
545 cacheStore(them, fi->entry, 0);
546 fileIteratorAdvance(fi);
547 }
548 }
549 }
550 /* digest peer cache content */
551 cacheResetDigest(them);
552 us->digest = cacheDigestClone(them->digest); /* @netw@ */
553
554 /* shift the time in access log to match ready_time */
555 fileIteratorSetCurTime(fis[0], ready_time);
556
557 /* iterate, use the iterator with the smallest positive inner_time */
558 cur_time = -1;
559 do {
560 int next_i = -1;
561 time_t next_time = -1;
562 active_fi_count = 0;
563 for (i = 0; i < fi_count; ++i) {
564 if (fis[i]->inner_time >= 0) {
565 if (!active_fi_count || fis[i]->inner_time < next_time) {
566 next_i = i;
567 next_time = fis[i]->inner_time;
568 }
569 active_fi_count++;
570 }
571 }
572 if (next_i >= 0) {
573 cur_time = next_time;
574 /*fprintf(stderr, "%2d time: %d %s", next_i, (int)cur_time, ctime(&cur_time));*/
575 if (next_i == 0)
576 cacheFetch(us, fis[next_i]->entry);
577 else
578 cacheUpdateStore(them, fis[next_i]->entry, 1);
579 fileIteratorAdvance(fis[next_i]);
580 }
581 } while (active_fi_count);
582
583 /* report */
584 cacheReport(them);
585 cacheReport(us);
586 cacheQueryReport(us, &us->qstats);
587
588 /* clean */
589 for (i = 0; i < argc-1; ++i) {
590 fileIteratorDestroy(fis[i]);
591 }
592 xfree(fis);
593 cacheDestroy(them);
594 cacheDestroy(us);
595 return 0;
596 }