]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/cache_diff.cc
3 * $Id: cache_diff.cc,v 1.19 2003/04/24 06:35:08 hno Exp $
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 * Computes the difference between the contents of two caches
38 * Reports the percentage of common files and other stats
47 int count
; /* #currently cached entries */
48 int scanned_count
; /* #scanned entries */
49 int bad_add_count
; /* #duplicate adds */
50 int bad_del_count
; /* #dels with no prior add */
56 typedef struct _CacheEntry
60 struct _CacheEntry
*next
;
61 /* storeSwapLogData s; */
62 unsigned char key_arr
[MD5_DIGEST_CHARS
];
68 /* copied from url.c */
69 const char *RequestMethodStr
[] =
82 static int cacheIndexScan(CacheIndex
* idx
, const char *fname
, FILE * file
);
86 cacheEntryCreate(const storeSwapLogData
* s
)
88 CacheEntry
*e
= xcalloc(1, sizeof(CacheEntry
));
91 xmemcpy(e
->key_arr
, s
->key
, MD5_DIGEST_CHARS
);
92 e
->key
= &e
->key_arr
[0];
97 cacheEntryDestroy(CacheEntry
* e
)
104 cacheIndexCreate(const char *name
)
108 if (!name
|| !strlen(name
))
111 idx
= xcalloc(1, sizeof(CacheIndex
));
115 idx
->hash
= hash_create(storeKeyHashCmp
, 2e6
, storeKeyHashHash
);
121 cacheIndexDestroy(CacheIndex
* idx
)
123 hash_link
*hashr
= NULL
;
126 /* destroy hash list contents */
127 hash_first(idx
->hash
);
129 while (hashr
= hash_next(idx
->hash
)) {
130 hash_remove_link(idx
->hash
, hashr
);
131 cacheEntryDestroy((CacheEntry
*) hashr
);
134 /* destroy the hash table itself */
135 hashFreeMemory(idx
->hash
);
142 cacheIndexAddLog(CacheIndex
* idx
, const char *fname
)
145 int scanned_count
= 0;
147 assert(fname
&& strlen(fname
));
149 file
= fopen(fname
, "r");
152 fprintf(stderr
, "cannot open %s: %s\n", fname
, strerror(errno
));
157 setmode(fileno(file
), O_BINARY
);
161 scanned_count
= cacheIndexScan(idx
, fname
, file
);
165 return scanned_count
;
169 cacheIndexInitReport(CacheIndex
* idx
)
172 fprintf(stderr
, "%s: bad swap_add: %d\n",
173 idx
->name
, idx
->bad_add_count
);
174 fprintf(stderr
, "%s: bad swap_del: %d\n",
175 idx
->name
, idx
->bad_del_count
);
176 fprintf(stderr
, "%s: scanned lines: %d\n",
177 idx
->name
, idx
->scanned_count
);
181 cacheIndexScan(CacheIndex
* idx
, const char *fname
, FILE * file
)
185 fprintf(stderr
, "%s scanning\n", fname
);
187 while (fread(&s
, sizeof(s
), 1, file
) == 1) {
189 idx
->scanned_count
++;
190 /* if (s.op <= SWAP_LOG_NOP || s.op >= SWAP_LOG_MAX)
193 if (s
.op
== SWAP_LOG_ADD
) {
194 CacheEntry
*olde
= (CacheEntry
*) hash_lookup(idx
->hash
, s
.key
);
197 idx
->bad_add_count
++;
199 CacheEntry
*e
= cacheEntryCreate(&s
);
200 hash_join(idx
->hash
, &e
->hash
);
203 } else if (s
.op
== SWAP_LOG_DEL
) {
204 CacheEntry
*olde
= (CacheEntry
*) hash_lookup(idx
->hash
, s
.key
);
207 idx
->bad_del_count
++;
210 hash_remove_link(idx
->hash
, (hash_link
*) olde
);
211 cacheEntryDestroy(olde
);
215 fprintf(stderr
, "%s:%d: unknown swap log action\n", fname
, count
);
220 fprintf(stderr
, "%s:%d: scanned (size: %d bytes)\n",
221 fname
, count
, (int) (count
* sizeof(CacheEntry
)));
226 cacheIndexCmpReport(CacheIndex
* idx
, int shared_count
)
228 assert(idx
&& shared_count
<= idx
->count
);
230 printf("%s:\t %7d = %7d + %7d (%7.2f%% + %7.2f%%)\n",
233 idx
->count
- shared_count
,
235 xpercent(idx
->count
- shared_count
, idx
->count
),
236 xpercent(shared_count
, idx
->count
));
240 cacheIndexCmp(CacheIndex
* idx1
, CacheIndex
* idx2
)
242 int shared_count
= 0;
243 int hashed_count
= 0;
244 hash_link
*hashr
= NULL
;
245 CacheIndex
*small_idx
= idx1
;
246 CacheIndex
*large_idx
= idx2
;
247 assert(idx1
&& idx2
);
249 /* check our guess */
251 if (idx1
->count
> idx2
->count
) {
256 /* find shared_count */
257 hash_first(small_idx
->hash
);
259 for (hashr
= hash_next(small_idx
->hash
)) {
262 if (hash_lookup(large_idx
->hash
, hashr
->key
))
266 assert(hashed_count
== small_idx
->count
);
268 cacheIndexCmpReport(idx1
, shared_count
);
269 cacheIndexCmpReport(idx2
, shared_count
);
274 usage(const char *prg_name
)
276 fprintf(stderr
, "usage: %s <label1>: <swap_state>... <label2>: <swap_state>...\n",
282 main(int argc
, char *argv
[])
284 CacheIndex
*CacheIdx
[2];
285 CacheIndex
*idx
= NULL
;
290 return usage(argv
[0]);
292 for (i
= 1; i
< argc
; ++i
) {
293 const int len
= strlen(argv
[i
]);
296 return usage(argv
[0]);
298 if (argv
[i
][len
- 1] == ':') {
301 if (len
< 2 || idxCount
> 2)
302 return usage(argv
[0]);
304 idx
= cacheIndexCreate(argv
[i
]);
306 CacheIdx
[idxCount
- 1] = idx
;
309 return usage(argv
[0]);
311 cacheIndexAddLog(idx
, argv
[i
]);
316 return usage(argv
[0]);
318 cacheIndexInitReport(CacheIdx
[0]);
320 cacheIndexInitReport(CacheIdx
[1]);
322 cacheIndexCmp(CacheIdx
[0], CacheIdx
[1]);
324 cacheIndexDestroy(CacheIdx
[0]);
326 cacheIndexDestroy(CacheIdx
[1]);