]> git.ipfire.org Git - thirdparty/squid.git/blob - src/test_cache_digest.cc
gindent
[thirdparty/squid.git] / src / test_cache_digest.cc
1
2 /*
3 * $Id: test_cache_digest.cc,v 1.17 1998/04/06 22:32:22 wessels 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 {
74 frError = -2, frMore = -1, frEof = 0, frOk = 1
75 } fr_result;
76 typedef struct _FileIterator FileIterator;
77 typedef fr_result(*FI_READER) (FileIterator * fi);
78
79 struct _FileIterator {
80 const char *fname;
81 FILE *file;
82 time_t inner_time; /* timestamp of the current entry */
83 time_t time_offset; /* to adjust time set by reader */
84 int line_count; /* number of lines scanned */
85 int bad_line_count; /* number of parsing errors */
86 int time_warp_count; /* number of out-of-order entries in the file */
87 FI_READER reader; /* reads next entry and updates inner_time */
88 void *entry; /* buffer for the current entry, freed with xfree() */
89 };
90
91 /* globals */
92 static time_t cur_time = -1; /* timestamp of the current log entry */
93
94 #if 0
95
96 static int cacheIndexScanCleanPrefix(CacheIndex * idx, const char *fname, FILE * file);
97 static int cacheIndexScanAccessLog(CacheIndex * idx, const char *fname, FILE * file);
98
99 #endif
100
101 /* copied from url.c */
102 const char *RequestMethodStr[] =
103 {
104 "NONE",
105 "GET",
106 "POST",
107 "PUT",
108 "HEAD",
109 "CONNECT",
110 "TRACE",
111 "PURGE"
112 };
113 /* copied from url.c */
114 static method_t
115 methodStrToId(const char *s)
116 {
117 if (strcasecmp(s, "GET") == 0) {
118 return METHOD_GET;
119 } else if (strcasecmp(s, "POST") == 0) {
120 return METHOD_POST;
121 } else if (strcasecmp(s, "PUT") == 0) {
122 return METHOD_PUT;
123 } else if (strcasecmp(s, "HEAD") == 0) {
124 return METHOD_HEAD;
125 } else if (strcasecmp(s, "CONNECT") == 0) {
126 return METHOD_CONNECT;
127 } else if (strcasecmp(s, "TRACE") == 0) {
128 return METHOD_TRACE;
129 } else if (strcasecmp(s, "PURGE") == 0) {
130 return METHOD_PURGE;
131 }
132 return METHOD_NONE;
133 }
134
135 /* FileIterator */
136
137 static void fileIteratorAdvance(FileIterator * fi);
138
139 static FileIterator *
140 fileIteratorCreate(const char *fname, FI_READER reader)
141 {
142 FileIterator *fi = xcalloc(1, sizeof(FileIterator));
143 assert(fname && reader);
144 fi->fname = fname;
145 fi->reader = reader;
146 fi->file = fopen(fname, "r");
147 if (!fi->file) {
148 fprintf(stderr, "cannot open %s: %s\n", fname, strerror(errno));
149 return NULL;
150 } else
151 fprintf(stderr, "opened %s\n", fname);
152 fileIteratorAdvance(fi);
153 return fi;
154 }
155
156 static void
157 fileIteratorDestroy(FileIterator * fi)
158 {
159 assert(fi);
160 if (fi->file) {
161 fclose(fi->file);
162 fprintf(stderr, "closed %s\n", fi->fname);
163 }
164 xfree(fi->entry);
165 xfree(fi);
166 }
167
168 static void
169 fileIteratorSetCurTime(FileIterator * fi, time_t ct)
170 {
171 assert(fi);
172 assert(fi->inner_time > 0);
173 fi->time_offset = ct - fi->inner_time;
174 }
175
176 static void
177 fileIteratorAdvance(FileIterator * fi)
178 {
179 int res;
180 assert(fi);
181 do {
182 const time_t last_time = fi->inner_time;
183 fi->inner_time = -1;
184 res = fi->reader(fi);
185 fi->line_count++;
186 if (fi->inner_time < 0)
187 fi->inner_time = last_time;
188 else
189 fi->inner_time += fi->time_offset;
190 if (res == frError)
191 fi->bad_line_count++;
192 else if (res == frEof) {
193 fprintf(stderr, "exhausted %s (%d entries) at %s",
194 fi->fname, fi->line_count, ctime(&fi->inner_time));
195 fi->inner_time = -1;
196 } else 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 }
352
353 static void
354 cacheFetch(Cache * cache, const RawAccessLogEntry * e)
355 {
356 assert(e);
357 cache->req_count++;
358 if (e->use_icp)
359 cacheQueryPeer(cache, e->key);
360 }
361
362 static fr_result
363 swapStateReader(FileIterator * fi)
364 {
365 storeSwapLogData *entry;
366 if (!fi->entry)
367 fi->entry = xcalloc(1, sizeof(storeSwapLogData));
368 entry = fi->entry;
369 if (fread(entry, sizeof(*entry), 1, fi->file) != 1)
370 return frEof;
371 fi->inner_time = entry->lastref;
372 if (entry->op != SWAP_LOG_ADD && entry->op != SWAP_LOG_DEL) {
373 fprintf(stderr, "%s:%d: unknown swap log action\n", fi->fname, fi->line_count);
374 exit(-3);
375 }
376 return frOk;
377 }
378
379 static fr_result
380 accessLogReader(FileIterator * fi)
381 {
382 static char buf[4096];
383 RawAccessLogEntry *entry;
384 char *url;
385 char *method;
386 int method_id = METHOD_NONE;
387 char *hier = NULL;
388
389 assert(fi);
390 if (!fi->entry)
391 fi->entry = xcalloc(1, sizeof(RawAccessLogEntry));
392 else
393 memset(fi->entry, 0, sizeof(RawAccessLogEntry));
394 entry = fi->entry;
395 if (!fgets(buf, sizeof(buf), fi->file))
396 return frEof; /* eof */
397 entry->timestamp = fi->inner_time = (time_t) atoi(buf);
398 url = strstr(buf, "://");
399 hier = url ? strstr(url, " - ") : NULL;
400
401 if (!url || !hier) {
402 /*fprintf(stderr, "%s:%d: strange access log entry '%s'\n",
403 * fname, scanned_count, buf); */
404 return frError;
405 }
406 method = url;
407 while (!isdigit(*method)) {
408 if (*method == ' ')
409 *method = '\0';
410 --method;
411 }
412 method += 2;
413 method_id = methodStrToId(method);
414 if (method_id == METHOD_NONE) {
415 /*fprintf(stderr, "%s:%d: invalid method %s in '%s'\n",
416 * fname, scanned_count, method, buf); */
417 return frError;
418 }
419 while (*url)
420 url--;
421 url++;
422 *hier = '\0';
423 hier += 3;
424 *strchr(hier, '/') = '\0';
425 /*fprintf(stdout, "%s:%d: %s %s %s\n",
426 * fname, count, method, url, hier); */
427 entry->use_icp = strcmp(hier, "NONE");
428 /* no ICP lookup for these status codes */
429 /* strcmp(hier, "NONE") &&
430 * strcmp(hier, "DIRECT") &&
431 * strcmp(hier, "FIREWALL_IP_DIRECT") &&
432 * strcmp(hier, "LOCAL_IP_DIRECT") &&
433 * strcmp(hier, "NO_DIRECT_FAIL") &&
434 * strcmp(hier, "NO_PARENT_DIRECT") &&
435 * strcmp(hier, "SINGLE_PARENT") &&
436 * strcmp(hier, "PASSTHROUGH_PARENT") &&
437 * strcmp(hier, "SSL_PARENT_MISS") &&
438 * strcmp(hier, "DEFAULT_PARENT");
439 */
440 memcpy(entry->key, storeKeyPublic(url, method_id), sizeof(entry->key));
441 /*fprintf(stdout, "%s:%d: %s %s %s %s\n",
442 * fname, count, method, storeKeyText(entry->key), url, hier); */
443 return frOk;
444 }
445
446
447 static void
448 cachePurge(Cache * cache, storeSwapLogData * s, int update_digest)
449 {
450 CacheEntry *olde = (CacheEntry *) hash_lookup(cache->hash, s->key);
451 if (!olde) {
452 cache->bad_del_count++;
453 } else {
454 assert(cache->count);
455 hash_remove_link(cache->hash, (hash_link *) olde);
456 if (update_digest)
457 cacheDigestDel(cache->digest, s->key);
458 cacheEntryDestroy(olde);
459 cache->count--;
460 }
461 }
462
463 static void
464 cacheStore(Cache * cache, storeSwapLogData * s, int update_digest)
465 {
466 CacheEntry *olde = (CacheEntry *) hash_lookup(cache->hash, s->key);
467 if (olde) {
468 cache->bad_add_count++;
469 } else {
470 CacheEntry *e = cacheEntryCreate(s);
471 hash_join(cache->hash, (hash_link *) e);
472 cache->count++;
473 if (update_digest)
474 cacheDigestAdd(cache->digest, e->key);
475 }
476 }
477
478 static void
479 cacheUpdateStore(Cache * cache, storeSwapLogData * s, int update_digest)
480 {
481 switch (s->op) {
482 case SWAP_LOG_ADD:
483 cacheStore(cache, s, update_digest);
484 break;
485 case SWAP_LOG_DEL:
486 cachePurge(cache, s, update_digest);
487 break;
488 default:
489 assert(0);
490 }
491 }
492
493 static int
494 usage(const char *prg_name)
495 {
496 fprintf(stderr, "usage: %s <access_log> <swap_state> ...\n",
497 prg_name);
498 return -1;
499 }
500
501 int
502 main(int argc, char *argv[])
503 {
504 FileIterator **fis = NULL;
505 const int fi_count = argc - 1;
506 int active_fi_count = 0;
507 time_t ready_time;
508 Cache *them, *us;
509 int i;
510
511 if (argc < 3)
512 return usage(argv[0]);
513
514 them = cacheCreate("them");
515 us = cacheCreate("us");
516 them->peer = us;
517 us->peer = them;
518
519 fis = xcalloc(fi_count, sizeof(FileIterator *));
520 /* init iterators with files */
521 fis[0] = fileIteratorCreate(argv[1], accessLogReader);
522 for (i = 2; i < argc; ++i)
523 fis[i - 1] = fileIteratorCreate(argv[i], swapStateReader);
524 /* check that all files were found */
525 for (i = 0; i < fi_count; ++i)
526 if (!fis[i])
527 return -2;
528 /* read prefix to get start-up contents of the peer cache */
529 ready_time = -1;
530 for (i = 1; i < fi_count; ++i) {
531 FileIterator *fi = fis[i];
532 while (fi->inner_time > 0) {
533 if (((storeSwapLogData *) fi->entry)->op == SWAP_LOG_DEL) {
534 cachePurge(them, fi->entry, 0);
535 if (ready_time < 0)
536 ready_time = fi->inner_time;
537 } else {
538 if (ready_time > 0 && fi->inner_time > ready_time)
539 break;
540 cacheStore(them, fi->entry, 0);
541 }
542 fileIteratorAdvance(fi);
543 }
544 }
545 /* digest peer cache content */
546 cacheResetDigest(them);
547 us->digest = cacheDigestClone(them->digest); /* @netw@ */
548
549 /* shift the time in access log to match ready_time */
550 fileIteratorSetCurTime(fis[0], ready_time);
551
552 /* iterate, use the iterator with the smallest positive inner_time */
553 cur_time = -1;
554 do {
555 int next_i = -1;
556 time_t next_time = -1;
557 active_fi_count = 0;
558 for (i = 0; i < fi_count; ++i) {
559 if (fis[i]->inner_time >= 0) {
560 if (!active_fi_count || fis[i]->inner_time < next_time) {
561 next_i = i;
562 next_time = fis[i]->inner_time;
563 }
564 active_fi_count++;
565 }
566 }
567 if (next_i >= 0) {
568 cur_time = next_time;
569 /*fprintf(stderr, "%2d time: %d %s", next_i, (int)cur_time, ctime(&cur_time)); */
570 if (next_i == 0)
571 cacheFetch(us, fis[next_i]->entry);
572 else
573 cacheUpdateStore(them, fis[next_i]->entry, 1);
574 fileIteratorAdvance(fis[next_i]);
575 }
576 } while (active_fi_count);
577
578 /* report */
579 cacheReport(them);
580 cacheReport(us);
581 cacheQueryReport(us, &us->qstats);
582
583 /* clean */
584 for (i = 0; i < argc - 1; ++i) {
585 fileIteratorDestroy(fis[i]);
586 }
587 xfree(fis);
588 cacheDestroy(them);
589 cacheDestroy(us);
590 return 0;
591 }