]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/cache_diff.cc
2 * $Id: cache_diff.cc,v 1.11 2000/03/06 16:23:29 wessels Exp $
4 * AUTHOR: Alex Rousskov
6 * SQUID Internet Object Cache http://squid.nlanr.net/Squid/
7 * ----------------------------------------------------------
9 * Squid is the result of efforts by numerous individuals from the
10 * Internet community. Development is led by Duane Wessels of the
11 * National Laboratory for Applied Network Research and funded by the
12 * National Science Foundation. Squid is Copyrighted (C) 1998 by
13 * the Regents of the University of California. Please see the
14 * COPYRIGHT file for full details. Squid incorporates software
15 * developed and/or copyrighted by other sources. Please see the
16 * CREDITS file for full details.
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
35 * Computes the difference between the contents of two caches
37 * Reports the percentage of common files and other stats
45 int count
; /* #currently cached entries */
46 int scanned_count
; /* #scanned entries */
47 int bad_add_count
; /* #duplicate adds */
48 int bad_del_count
; /* #dels with no prior add */
52 typedef struct _CacheEntry
{
54 struct _CacheEntry
*next
;
55 /* storeSwapLogData s; */
56 unsigned char key_arr
[MD5_DIGEST_CHARS
];
60 /* copied from url.c */
61 const char *RequestMethodStr
[] =
74 static int cacheIndexScan(CacheIndex
* idx
, const char *fname
, FILE * file
);
78 cacheEntryCreate(const storeSwapLogData
* s
)
80 CacheEntry
*e
= xcalloc(1, sizeof(CacheEntry
));
83 xmemcpy(e
->key_arr
, s
->key
, MD5_DIGEST_CHARS
);
84 e
->key
= &e
->key_arr
[0];
89 cacheEntryDestroy(CacheEntry
* e
)
96 cacheIndexCreate(const char *name
)
99 if (!name
|| !strlen(name
))
102 idx
= xcalloc(1, sizeof(CacheIndex
));
104 idx
->hash
= hash_create(storeKeyHashCmp
, 2e6
, storeKeyHashHash
);
110 cacheIndexDestroy(CacheIndex
* idx
)
112 hash_link
*hashr
= NULL
;
114 /* destroy hash list contents */
115 hash_first(idx
->hash
);
116 while (hashr
= hash_next(idx
->hash
)) {
117 hash_remove_link(idx
->hash
, hashr
);
118 cacheEntryDestroy((CacheEntry
*) hashr
);
120 /* destroy the hash table itself */
121 hashFreeMemory(idx
->hash
);
127 cacheIndexAddLog(CacheIndex
* idx
, const char *fname
)
130 int scanned_count
= 0;
132 assert(fname
&& strlen(fname
));
134 file
= fopen(fname
, "r");
136 fprintf(stderr
, "cannot open %s: %s\n", fname
, strerror(errno
));
139 scanned_count
= cacheIndexScan(idx
, fname
, file
);
142 return scanned_count
;
146 cacheIndexInitReport(CacheIndex
* idx
)
149 fprintf(stderr
, "%s: bad swap_add: %d\n",
150 idx
->name
, idx
->bad_add_count
);
151 fprintf(stderr
, "%s: bad swap_del: %d\n",
152 idx
->name
, idx
->bad_del_count
);
153 fprintf(stderr
, "%s: scanned lines: %d\n",
154 idx
->name
, idx
->scanned_count
);
158 cacheIndexScan(CacheIndex
* idx
, const char *fname
, FILE * file
)
162 fprintf(stderr
, "%s scanning\n", fname
);
163 while (fread(&s
, sizeof(s
), 1, file
) == 1) {
165 idx
->scanned_count
++;
166 /* if (s.op <= SWAP_LOG_NOP || s.op >= SWAP_LOG_MAX)
168 if (s
.op
== SWAP_LOG_ADD
) {
169 CacheEntry
*olde
= (CacheEntry
*) hash_lookup(idx
->hash
, s
.key
);
171 idx
->bad_add_count
++;
173 CacheEntry
*e
= cacheEntryCreate(&s
);
174 hash_join(idx
->hash
, (hash_link
*) e
);
177 } else if (s
.op
== SWAP_LOG_DEL
) {
178 CacheEntry
*olde
= (CacheEntry
*) hash_lookup(idx
->hash
, s
.key
);
180 idx
->bad_del_count
++;
183 hash_remove_link(idx
->hash
, (hash_link
*) olde
);
184 cacheEntryDestroy(olde
);
188 fprintf(stderr
, "%s:%d: unknown swap log action\n", fname
, count
);
192 fprintf(stderr
, "%s:%d: scanned (size: %d bytes)\n",
193 fname
, count
, (int) (count
* sizeof(CacheEntry
)));
198 cacheIndexCmpReport(CacheIndex
* idx
, int shared_count
)
200 assert(idx
&& shared_count
<= idx
->count
);
202 printf("%s:\t %7d = %7d + %7d (%7.2f%% + %7.2f%%)\n",
205 idx
->count
- shared_count
,
207 xpercent(idx
->count
- shared_count
, idx
->count
),
208 xpercent(shared_count
, idx
->count
));
212 cacheIndexCmp(CacheIndex
* idx1
, CacheIndex
* idx2
)
214 int shared_count
= 0;
215 int hashed_count
= 0;
216 hash_link
*hashr
= NULL
;
217 CacheIndex
*small_idx
= idx1
;
218 CacheIndex
*large_idx
= idx2
;
219 assert(idx1
&& idx2
);
221 /* check our guess */
222 if (idx1
->count
> idx2
->count
) {
226 /* find shared_count */
227 hash_first(small_idx
->hash
);
228 for (hashr
= hash_next(small_idx
->hash
)) {
230 if (hash_lookup(large_idx
->hash
, hashr
->key
))
233 assert(hashed_count
== small_idx
->count
);
235 cacheIndexCmpReport(idx1
, shared_count
);
236 cacheIndexCmpReport(idx2
, shared_count
);
241 usage(const char *prg_name
)
243 fprintf(stderr
, "usage: %s <label1>: <swap_state>... <label2>: <swap_state>...\n",
249 main(int argc
, char *argv
[])
251 CacheIndex
*CacheIdx
[2];
252 CacheIndex
*idx
= NULL
;
257 return usage(argv
[0]);
259 for (i
= 1; i
< argc
; ++i
) {
260 const int len
= strlen(argv
[i
]);
262 return usage(argv
[0]);
263 if (argv
[i
][len
- 1] == ':') {
265 if (len
< 2 || idxCount
> 2)
266 return usage(argv
[0]);
267 idx
= cacheIndexCreate(argv
[i
]);
268 CacheIdx
[idxCount
- 1] = idx
;
271 return usage(argv
[0]);
272 cacheIndexAddLog(idx
, argv
[i
]);
277 return usage(argv
[0]);
279 cacheIndexInitReport(CacheIdx
[0]);
280 cacheIndexInitReport(CacheIdx
[1]);
282 cacheIndexCmp(CacheIdx
[0], CacheIdx
[1]);
284 cacheIndexDestroy(CacheIdx
[0]);
285 cacheIndexDestroy(CacheIdx
[1]);