]> git.ipfire.org Git - thirdparty/squid.git/blob - src/test_cache_digest.cc
Add missing includes
[thirdparty/squid.git] / src / test_cache_digest.cc
1
2 /*
3 * $Id$
4 *
5 * AUTHOR: Alex Rousskov
6 *
7 * SQUID Web Proxy Cache http://www.squid-cache.org/
8 * ----------------------------------------------------------
9 *
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.
18 *
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.
23 *
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.
28 *
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.
32 *
33 */
34
35 /*
36 * Test-suite for playing with cache digests
37 */
38
39 #include "squid.h"
40
41 #if HAVE_ERRNO_H
42 #include <errno.h>
43 #endif
44
45 typedef struct {
46 int query_count;
47 int true_hit_count;
48 int true_miss_count;
49 int false_hit_count;
50 int false_miss_count;
51 } CacheQueryStats;
52
53 typedef struct _Cache Cache;
54
55 struct _Cache {
56 const char *name;
57 hash_table *hash;
58 CacheDigest *digest;
59 Cache *peer;
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 */
65 };
66
67
68 typedef struct _CacheEntry {
69 const cache_key *key;
70
71 struct _CacheEntry *next;
72 unsigned char key_arr[SQUID_MD5_DIGEST_LENGTH];
73 /* storeSwapLogData s; */
74 } CacheEntry;
75
76 /* parsed access log entry */
77
78 typedef struct {
79 cache_key key[SQUID_MD5_DIGEST_LENGTH];
80 time_t timestamp;
81 short int use_icp; /* true/false */
82 } RawAccessLogEntry;
83
84 typedef enum {
85 frError = -2, frMore = -1, frEof = 0, frOk = 1
86 } fr_result;
87
88 typedef struct _FileIterator FileIterator;
89 typedef fr_result(*FI_READER) (FileIterator * fi);
90
91 struct _FileIterator {
92 const char *fname;
93 FILE *file;
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() */
101 };
102
103 /* globals */
104 static time_t cur_time = -1; /* timestamp of the current log entry */
105
106 /* copied from url.c */
107 const char *RequestMethodStr[] = {
108 "NONE",
109 "GET",
110 "POST",
111 "PUT",
112 "HEAD",
113 "CONNECT",
114 "TRACE",
115 "PURGE"
116 };
117
118 /* copied from url.c */
119 static HttpRequestMethod
120 methodStrToId(const char *s)
121 {
122 if (strcasecmp(s, "GET") == 0) {
123 return METHOD_GET;
124 } else if (strcasecmp(s, "POST") == 0) {
125 return METHOD_POST;
126 } else if (strcasecmp(s, "PUT") == 0) {
127 return METHOD_PUT;
128 } else if (strcasecmp(s, "HEAD") == 0) {
129 return METHOD_HEAD;
130 } else if (strcasecmp(s, "CONNECT") == 0) {
131 return METHOD_CONNECT;
132 } else if (strcasecmp(s, "TRACE") == 0) {
133 return METHOD_TRACE;
134 } else if (strcasecmp(s, "PURGE") == 0) {
135 return METHOD_PURGE;
136 }
137
138 return METHOD_NONE;
139 }
140
141 /* FileIterator */
142
143 static void fileIteratorAdvance(FileIterator * fi);
144
145 static FileIterator *
146 fileIteratorCreate(const char *fname, FI_READER reader)
147 {
148 FileIterator *fi = (FileIterator *)xcalloc(1, sizeof(FileIterator));
149 assert(fname && reader);
150 fi->fname = fname;
151 fi->reader = reader;
152 fi->file = fopen(fname, "r");
153
154 if (!fi->file) {
155 fprintf(stderr, "cannot open %s: %s\n", fname, strerror(errno));
156 return NULL;
157 } else
158 fprintf(stderr, "opened %s\n", fname);
159
160 fileIteratorAdvance(fi);
161
162 return fi;
163 }
164
165 static void
166 fileIteratorDestroy(FileIterator * fi)
167 {
168 assert(fi);
169
170 if (fi->file) {
171 fclose(fi->file);
172 fprintf(stderr, "closed %s\n", fi->fname);
173 }
174
175 xfree(fi->entry);
176 xfree(fi);
177 }
178
179 static void
180 fileIteratorSetCurTime(FileIterator * fi, time_t ct)
181 {
182 assert(fi);
183 assert(fi->inner_time > 0);
184 fi->time_offset = ct - fi->inner_time;
185 }
186
187 static void
188 fileIteratorAdvance(FileIterator * fi)
189 {
190 int res;
191 assert(fi);
192
193 do {
194 const time_t last_time = fi->inner_time;
195 fi->inner_time = -1;
196 res = fi->reader(fi);
197 ++ fi->line_count;
198
199 if (fi->inner_time < 0)
200 fi->inner_time = last_time;
201 else
202 fi->inner_time += fi->time_offset;
203
204 if (res == frError)
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));
209 fi->inner_time = -1;
210 } else if (fi->inner_time < last_time) {
211 assert(last_time >= 0);
212 ++ fi->time_warp_count;
213 fi->inner_time = last_time;
214 }
215
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));
221 } while (res < 0);
222 }
223
224 /* CacheEntry */
225
226 static CacheEntry *
227 cacheEntryCreate(const storeSwapLogData * s)
228 {
229 CacheEntry *e = (CacheEntry *)xcalloc(1, sizeof(CacheEntry));
230 assert(s);
231 /* e->s = *s; */
232 memcpy(e->key_arr, s->key, SQUID_MD5_DIGEST_LENGTH);
233 e->key = &e->key_arr[0];
234 return e;
235 }
236
237 static void
238 cacheEntryDestroy(CacheEntry * e)
239 {
240 assert(e);
241 xfree(e);
242 }
243
244
245 /* Cache */
246
247 static Cache *
248 cacheCreate(const char *name)
249 {
250 Cache *c;
251 assert(name && strlen(name));
252 c = (Cache *)xcalloc(1, sizeof(Cache));
253 c->name = name;
254 c->hash = hash_create(storeKeyHashCmp, (int)2e6, storeKeyHashHash);
255 return c;
256 }
257
258 static void
259 cacheDestroy(Cache * cache)
260 {
261 CacheEntry *e = NULL;
262 hash_table *hash;
263 assert(cache);
264 hash = cache->hash;
265 /* destroy hash table contents */
266 hash_first(hash);
267
268 while ((e = (CacheEntry *)hash_next(hash))) {
269 hash_remove_link(hash, (hash_link *) e);
270 cacheEntryDestroy(e);
271 }
272
273 /* destroy the hash table itself */
274 hashFreeMemory(hash);
275
276 if (cache->digest)
277 cacheDigestDestroy(cache->digest);
278
279 xfree(cache);
280 }
281
282 /* re-digests currently hashed entries */
283 static void
284 cacheResetDigest(Cache * cache)
285 {
286 CacheEntry *e = NULL;
287 hash_table *hash;
288
289 struct timeval t_start, t_end;
290
291 assert(cache);
292 fprintf(stderr, "%s: init-ing digest with %d entries\n", cache->name, cache->count);
293
294 if (cache->digest)
295 cacheDigestDestroy(cache->digest);
296
297 hash = cache->hash;
298
299 cache->digest = cacheDigestCreate(cache->count + 1, 6);
300
301 if (!cache->count)
302 return;
303
304 gettimeofday(&t_start, NULL);
305
306 hash_first(hash);
307
308 while ((e = (CacheEntry *)hash_next(hash))) {
309 cacheDigestAdd(cache->digest, e->key);
310 }
311
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",
317 cache->name,
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);
322 hash_first(hash);
323
324 for (e = (CacheEntry *)hash_next(hash); e; e = (CacheEntry *)hash_next(hash)) {}
325
326 gettimeofday(&t_end, NULL);
327 fprintf(stderr, "%s: hash scan took: %f sec, %f sec/M\n",
328 cache->name,
329 tvSubDsec(t_start, t_end),
330 (double) 1e6 * tvSubDsec(t_start, t_end) / cache->count);
331 }
332
333 static void
334 cacheQueryPeer(Cache * cache, const cache_key * key)
335 {
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);
338
339 ++ cache->qstats.query_count;
340
341 if (peer_has_it) {
342 if (we_think_we_have_it)
343 ++ cache->qstats.true_hit_count;
344 else
345 ++ cache->qstats.false_miss_count;
346 } else {
347 if (we_think_we_have_it)
348 ++ cache->qstats.false_hit_count;
349 else
350 ++ cache->qstats.true_miss_count;
351 }
352 }
353
354 static void
355 cacheQueryReport(Cache * cache, CacheQueryStats * stats)
356 {
357 fprintf(stdout, "%s: peer queries: %d (%d%%)\n",
358 cache->name,
359 stats->query_count, xpercentInt(stats->query_count, cache->req_count)
360 );
361 fprintf(stdout, "%s: t-hit: %d (%d%%) t-miss: %d (%d%%) t-*: %d (%d%%)\n",
362 cache->name,
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)
367 );
368 fprintf(stdout, "%s: f-hit: %d (%d%%) f-miss: %d (%d%%) f-*: %d (%d%%)\n",
369 cache->name,
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)
374 );
375 }
376
377 static void
378 cacheReport(Cache * cache)
379 {
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);
383
384 }
385
386 static void
387 cacheFetch(Cache * cache, const RawAccessLogEntry * e)
388 {
389 assert(e);
390 ++ cache->req_count;
391
392 if (e->use_icp)
393 cacheQueryPeer(cache, e->key);
394 }
395
396 static fr_result
397 swapStateReader(FileIterator * fi)
398 {
399 storeSwapLogData *entry;
400
401 if (!fi->entry)
402 fi->entry = xcalloc(1, sizeof(storeSwapLogData));
403
404 entry = (storeSwapLogData *)fi->entry;
405
406 if (fread(entry, sizeof(*entry), 1, fi->file) != 1)
407 return frEof;
408
409 fi->inner_time = entry->lastref;
410
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);
413 exit(-3);
414 }
415
416 return frOk;
417 }
418
419 static fr_result
420 accessLogReader(FileIterator * fi)
421 {
422 static char buf[4096];
423 RawAccessLogEntry *entry;
424 char *url;
425 char *method;
426 HttpRequestMethod method_id = METHOD_NONE;
427 char *hier = NULL;
428
429 assert(fi);
430
431 if (!fi->entry)
432 fi->entry = xcalloc(1, sizeof(RawAccessLogEntry));
433 else
434 memset(fi->entry, 0, sizeof(RawAccessLogEntry));
435
436 entry = (RawAccessLogEntry*)fi->entry;
437
438 if (!fgets(buf, sizeof(buf), fi->file))
439 return frEof; /* eof */
440
441 entry->timestamp = fi->inner_time = (time_t) atoi(buf);
442
443 url = strstr(buf, "://");
444
445 hier = url ? strstr(url, " - ") : NULL;
446
447 if (!url || !hier) {
448 /*fprintf(stderr, "%s:%d: strange access log entry '%s'\n",
449 * fname, scanned_count, buf); */
450 return frError;
451 }
452
453 method = url;
454
455 while (!xisdigit(*method)) {
456 if (*method == ' ')
457 *method = '\0';
458
459 --method;
460 }
461
462 method += 2;
463 method_id = methodStrToId(method);
464
465 if (method_id == METHOD_NONE) {
466 /*fprintf(stderr, "%s:%d: invalid method %s in '%s'\n",
467 * fname, scanned_count, method, buf); */
468 return frError;
469 }
470
471 while (*url)
472 --url;
473
474 ++url;
475
476 *hier = '\0';
477
478 hier += 3;
479
480 *strchr(hier, '/') = '\0';
481
482 /*fprintf(stdout, "%s:%d: %s %s %s\n",
483 * fname, count, method, url, hier); */
484 entry->use_icp = strcmp(hier, "NONE");
485
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");
497 */
498 memcpy(entry->key, storeKeyPublic(url, method_id), sizeof(entry->key));
499
500 /*fprintf(stdout, "%s:%d: %s %s %s %s\n",
501 * fname, count, method, storeKeyText(entry->key), url, hier); */
502 return frOk;
503 }
504
505
506 static void
507 cachePurge(Cache * cache, storeSwapLogData * s, int update_digest)
508 {
509 CacheEntry *olde = (CacheEntry *) hash_lookup(cache->hash, s->key);
510
511 if (!olde) {
512 ++ cache->bad_del_count;
513 } else {
514 assert(cache->count);
515 hash_remove_link(cache->hash, (hash_link *) olde);
516
517 if (update_digest)
518 cacheDigestDel(cache->digest, s->key);
519
520 cacheEntryDestroy(olde);
521
522 -- cache->count;
523 }
524 }
525
526 static void
527 cacheStore(Cache * cache, storeSwapLogData * s, int update_digest)
528 {
529 CacheEntry *olde = (CacheEntry *) hash_lookup(cache->hash, s->key);
530
531 if (olde) {
532 ++ cache->bad_add_count;
533 } else {
534 CacheEntry *e = cacheEntryCreate(s);
535 hash_join(cache->hash, (hash_link *)&e->key);
536 ++ cache->count;
537
538 if (update_digest)
539 cacheDigestAdd(cache->digest, e->key);
540 }
541 }
542
543 static void
544 cacheUpdateStore(Cache * cache, storeSwapLogData * s, int update_digest)
545 {
546 switch (s->op) {
547
548 case SWAP_LOG_ADD:
549 cacheStore(cache, s, update_digest);
550 break;
551
552 case SWAP_LOG_DEL:
553 cachePurge(cache, s, update_digest);
554 break;
555
556 default:
557 assert(0);
558 }
559 }
560
561 static int
562 usage(const char *prg_name)
563 {
564 fprintf(stderr, "usage: %s <access_log> <swap_state> ...\n",
565 prg_name);
566 return -1;
567 }
568
569 int
570 main(int argc, char *argv[])
571 {
572 FileIterator **fis = NULL;
573 const int fi_count = argc - 1;
574 int active_fi_count = 0;
575 time_t ready_time;
576 Cache *them, *us;
577 int i;
578
579 if (argc < 3)
580 return usage(argv[0]);
581
582 them = cacheCreate("them");
583
584 us = cacheCreate("us");
585
586 them->peer = us;
587
588 us->peer = them;
589
590 fis = (FileIterator **)xcalloc(fi_count, sizeof(FileIterator *));
591
592 /* init iterators with files */
593 fis[0] = fileIteratorCreate(argv[1], accessLogReader);
594
595 for (i = 2; i < argc; ++i)
596 fis[i - 1] = fileIteratorCreate(argv[i], swapStateReader);
597
598 /* check that all files were found */
599 for (i = 0; i < fi_count; ++i)
600 if (!fis[i])
601 return -2;
602
603 /* read prefix to get start-up contents of the peer cache */
604 ready_time = -1;
605
606 for (i = 1; i < fi_count; ++i) {
607 FileIterator *fi = fis[i];
608
609 while (fi->inner_time > 0) {
610 if (((storeSwapLogData *) fi->entry)->op == SWAP_LOG_DEL) {
611 cachePurge(them, (storeSwapLogData *)fi->entry, 0);
612
613 if (ready_time < 0)
614 ready_time = fi->inner_time;
615 } else {
616 if (ready_time > 0 && fi->inner_time > ready_time)
617 break;
618
619 cacheStore(them, (storeSwapLogData *)fi->entry, 0);
620 }
621
622 fileIteratorAdvance(fi);
623 }
624 }
625
626 /* digest peer cache content */
627 cacheResetDigest(them);
628
629 us->digest = cacheDigestClone(them->digest); /* @netw@ */
630
631 /* shift the time in access log to match ready_time */
632 fileIteratorSetCurTime(fis[0], ready_time);
633
634 /* iterate, use the iterator with the smallest positive inner_time */
635 cur_time = -1;
636
637 do {
638 int next_i = -1;
639 time_t next_time = -1;
640 active_fi_count = 0;
641
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) {
645 next_i = i;
646 next_time = fis[i]->inner_time;
647 }
648
649 ++active_fi_count;
650 }
651 }
652
653 if (next_i >= 0) {
654 cur_time = next_time;
655 /*fprintf(stderr, "%2d time: %d %s", next_i, (int)cur_time, ctime(&cur_time)); */
656
657 if (next_i == 0)
658 cacheFetch(us, (RawAccessLogEntry *)fis[next_i]->entry);
659 else
660 cacheUpdateStore(them, (storeSwapLogData *)fis[next_i]->entry, 1);
661
662 fileIteratorAdvance(fis[next_i]);
663 }
664 } while (active_fi_count);
665
666 /* report */
667 cacheReport(them);
668
669 cacheReport(us);
670
671 cacheQueryReport(us, &us->qstats);
672
673 /* clean */
674 for (i = 0; i < argc - 1; ++i) {
675 fileIteratorDestroy(fis[i]);
676 }
677
678 xfree(fis);
679 cacheDestroy(them);
680 cacheDestroy(us);
681 return 0;
682 }