]> git.ipfire.org Git - thirdparty/squid.git/blob - src/cache_diff.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / cache_diff.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 * Computes the difference between the contents of two caches
37 * using swap logs
38 * Reports the percentage of common files and other stats
39 */
40
41 #include "squid.h"
42
43 #if HAVE_ERRNO_H
44 #include <errno.h>
45 #endif
46
47 typedef struct {
48 const char *name;
49 hash_table *hash;
50 int count; /* #currently cached entries */
51 int scanned_count; /* #scanned entries */
52 int bad_add_count; /* #duplicate adds */
53 int bad_del_count; /* #dels with no prior add */
54 } CacheIndex;
55
56 typedef struct _CacheEntry {
57 const cache_key *key;
58
59 struct _CacheEntry *next;
60 /* StoreSwapLogData s; */
61 unsigned char key_arr[SQUID_MD5_DIGEST_LENGTH];
62 } CacheEntry;
63
64 /* copied from url.c */
65 const char *RequestMethodStr[] = {
66 "NONE",
67 "GET",
68 "POST",
69 "PUT",
70 "HEAD",
71 "CONNECT",
72 "TRACE",
73 "PURGE"
74 };
75
76 static int cacheIndexScan(CacheIndex * idx, const char *fname, FILE * file);
77
78 static CacheEntry *
79 cacheEntryCreate(const StoreSwapLogData * s)
80 {
81 CacheEntry *e = xcalloc(1, sizeof(CacheEntry));
82 assert(s);
83 /* e->s = *s; */
84 memcpy(e->key_arr, s->key, SQUID_MD5_DIGEST_LENGTH);
85 e->key = &e->key_arr[0];
86 return e;
87 }
88
89 static void
90 cacheEntryDestroy(CacheEntry * e)
91 {
92 assert(e);
93 xfree(e);
94 }
95
96 static CacheIndex *
97 cacheIndexCreate(const char *name)
98 {
99 CacheIndex *idx;
100
101 if (!name || !strlen(name))
102 return NULL;
103
104 idx = xcalloc(1, sizeof(CacheIndex));
105
106 idx->name = name;
107
108 idx->hash = hash_create(storeKeyHashCmp, 2e6, storeKeyHashHash);
109
110 return idx;
111 }
112
113 static void
114 cacheIndexDestroy(CacheIndex * idx)
115 {
116 hash_link *hashr = NULL;
117
118 if (idx) {
119 /* destroy hash list contents */
120 hash_first(idx->hash);
121
122 while (hashr = hash_next(idx->hash)) {
123 hash_remove_link(idx->hash, hashr);
124 cacheEntryDestroy((CacheEntry *) hashr);
125 }
126
127 /* destroy the hash table itself */
128 hashFreeMemory(idx->hash);
129
130 xfree(idx);
131 }
132 }
133
134 static int
135 cacheIndexAddLog(CacheIndex * idx, const char *fname)
136 {
137 FILE *file;
138 int scanned_count = 0;
139 assert(idx);
140 assert(fname && strlen(fname));
141
142 file = fopen(fname, "r");
143
144 if (!file) {
145 fprintf(stderr, "cannot open %s: %s\n", fname, strerror(errno));
146 return 0;
147 }
148
149 #if _SQUID_WINDOWS_
150 setmode(fileno(file), O_BINARY);
151 #endif
152
153 scanned_count = cacheIndexScan(idx, fname, file);
154
155 fclose(file);
156
157 return scanned_count;
158 }
159
160 static void
161 cacheIndexInitReport(CacheIndex * idx)
162 {
163 assert(idx);
164 fprintf(stderr, "%s: bad swap_add: %d\n",
165 idx->name, idx->bad_add_count);
166 fprintf(stderr, "%s: bad swap_del: %d\n",
167 idx->name, idx->bad_del_count);
168 fprintf(stderr, "%s: scanned lines: %d\n",
169 idx->name, idx->scanned_count);
170 }
171
172 static int
173 cacheIndexScan(CacheIndex * idx, const char *fname, FILE * file)
174 {
175 int count = 0;
176 StoreSwapLogData s;
177 fprintf(stderr, "%s scanning\n", fname);
178
179 while (fread(&s, sizeof(s), 1, file) == 1) {
180 ++count;
181 ++ idx->scanned_count;
182 /* if (!s.sane())
183 * continue; */
184
185 if (s.op == SWAP_LOG_ADD) {
186 CacheEntry *olde = (CacheEntry *) hash_lookup(idx->hash, s.key);
187
188 if (olde) {
189 ++ idx->bad_add_count;
190 } else {
191 CacheEntry *e = cacheEntryCreate(&s);
192 hash_join(idx->hash, &e->hash);
193 ++ idx->count;
194 }
195 } else if (s.op == SWAP_LOG_DEL) {
196 CacheEntry *olde = (CacheEntry *) hash_lookup(idx->hash, s.key);
197
198 if (!olde)
199 ++ idx->bad_del_count;
200 else {
201 assert(idx->count);
202 hash_remove_link(idx->hash, (hash_link *) olde);
203 cacheEntryDestroy(olde);
204 -- idx->count;
205 }
206 } else {
207 fprintf(stderr, "%s:%d: unknown swap log action\n", fname, count);
208 exit(-3);
209 }
210 }
211
212 fprintf(stderr, "%s:%d: scanned (size: %d bytes)\n",
213 fname, count, (int) (count * sizeof(CacheEntry)));
214 return count;
215 }
216
217 static void
218 cacheIndexCmpReport(CacheIndex * idx, int shared_count)
219 {
220 assert(idx && shared_count <= idx->count);
221
222 printf("%s:\t %7d = %7d + %7d (%7.2f%% + %7.2f%%)\n",
223 idx->name,
224 idx->count,
225 idx->count - shared_count,
226 shared_count,
227 xpercent(idx->count - shared_count, idx->count),
228 xpercent(shared_count, idx->count));
229 }
230
231 static void
232 cacheIndexCmp(CacheIndex * idx1, CacheIndex * idx2)
233 {
234 int shared_count = 0;
235 int hashed_count = 0;
236 hash_link *hashr = NULL;
237 CacheIndex *small_idx = idx1;
238 CacheIndex *large_idx = idx2;
239 assert(idx1 && idx2);
240
241 /* check our guess */
242
243 if (idx1->count > idx2->count) {
244 small_idx = idx2;
245 large_idx = idx1;
246 }
247
248 /* find shared_count */
249 hash_first(small_idx->hash);
250
251 for (hashr = hash_next(small_idx->hash)) {
252 ++hashed_count;
253
254 if (hash_lookup(large_idx->hash, hashr->key))
255 ++shared_count;
256 }
257
258 assert(hashed_count == small_idx->count);
259
260 cacheIndexCmpReport(idx1, shared_count);
261 cacheIndexCmpReport(idx2, shared_count);
262 }
263
264 static int
265 usage(const char *prg_name)
266 {
267 fprintf(stderr, "usage: %s <label1>: <swap_state>... <label2>: <swap_state>...\n",
268 prg_name);
269 return -1;
270 }
271
272 int
273 main(int argc, char *argv[])
274 {
275 CacheIndex *CacheIdx[2];
276 CacheIndex *idx = NULL;
277 int idxCount = 0;
278 int i;
279
280 if (argc < 5)
281 return usage(argv[0]);
282
283 for (i = 1; i < argc; ++i) {
284 const int len = strlen(argv[i]);
285
286 if (!len)
287 return usage(argv[0]);
288
289 if (argv[i][len - 1] == ':') {
290 ++idxCount;
291
292 if (len < 2 || idxCount > 2)
293 return usage(argv[0]);
294
295 idx = cacheIndexCreate(argv[i]);
296
297 CacheIdx[idxCount - 1] = idx;
298 } else {
299 if (!idx)
300 return usage(argv[0]);
301
302 cacheIndexAddLog(idx, argv[i]);
303 }
304 }
305
306 if (idxCount != 2)
307 return usage(argv[0]);
308
309 cacheIndexInitReport(CacheIdx[0]);
310
311 cacheIndexInitReport(CacheIdx[1]);
312
313 cacheIndexCmp(CacheIdx[0], CacheIdx[1]);
314
315 cacheIndexDestroy(CacheIdx[0]);
316
317 cacheIndexDestroy(CacheIdx[1]);
318
319 return 1;
320 }