]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/cache_diff.cc
3 * AUTHOR: Alex Rousskov
5 * SQUID Web Proxy Cache http://www.squid-cache.org/
6 * ----------------------------------------------------------
8 * Squid is the result of efforts by numerous individuals from
9 * the Internet community; see the CONTRIBUTORS file for full
10 * details. Many organizations have provided support for Squid's
11 * development; see the SPONSORS file for full details. Squid is
12 * Copyrighted (C) 2001 by the Regents of the University of
13 * California; see the COPYRIGHT file for full details. Squid
14 * incorporates software developed and/or copyrighted by other
15 * sources; see the CREDITS file for full details.
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
34 * Computes the difference between the contents of two caches
36 * 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 */
51 typedef struct _CacheEntry
{
54 struct _CacheEntry
*next
;
55 /* StoreSwapLogData s; */
56 unsigned char key_arr
[SQUID_MD5_DIGEST_LENGTH
];
59 static int cacheIndexScan(CacheIndex
* idx
, const char *fname
, FILE * file
);
62 cacheEntryCreate(const StoreSwapLogData
* s
)
64 CacheEntry
*e
= xcalloc(1, sizeof(CacheEntry
));
67 memcpy(e
->key_arr
, s
->key
, SQUID_MD5_DIGEST_LENGTH
);
68 e
->key
= &e
->key_arr
[0];
73 cacheEntryDestroy(CacheEntry
* e
)
80 cacheIndexCreate(const char *name
)
84 if (!name
|| !strlen(name
))
87 idx
= xcalloc(1, sizeof(CacheIndex
));
91 idx
->hash
= hash_create(storeKeyHashCmp
, 2e6
, storeKeyHashHash
);
97 cacheIndexDestroy(CacheIndex
* idx
)
99 hash_link
*hashr
= NULL
;
102 /* destroy hash list contents */
103 hash_first(idx
->hash
);
105 while (hashr
= hash_next(idx
->hash
)) {
106 hash_remove_link(idx
->hash
, hashr
);
107 cacheEntryDestroy((CacheEntry
*) hashr
);
110 /* destroy the hash table itself */
111 hashFreeMemory(idx
->hash
);
118 cacheIndexAddLog(CacheIndex
* idx
, const char *fname
)
121 int scanned_count
= 0;
123 assert(fname
&& strlen(fname
));
125 file
= fopen(fname
, "r");
128 fprintf(stderr
, "cannot open %s: %s\n", fname
, strerror(errno
));
133 setmode(fileno(file
), O_BINARY
);
136 scanned_count
= cacheIndexScan(idx
, fname
, file
);
140 return scanned_count
;
144 cacheIndexInitReport(CacheIndex
* idx
)
147 fprintf(stderr
, "%s: bad swap_add: %d\n",
148 idx
->name
, idx
->bad_add_count
);
149 fprintf(stderr
, "%s: bad swap_del: %d\n",
150 idx
->name
, idx
->bad_del_count
);
151 fprintf(stderr
, "%s: scanned lines: %d\n",
152 idx
->name
, idx
->scanned_count
);
156 cacheIndexScan(CacheIndex
* idx
, const char *fname
, FILE * file
)
160 fprintf(stderr
, "%s scanning\n", fname
);
162 while (fread(&s
, sizeof(s
), 1, file
) == 1) {
164 ++ idx
->scanned_count
;
168 if (s
.op
== SWAP_LOG_ADD
) {
169 CacheEntry
*olde
= (CacheEntry
*) hash_lookup(idx
->hash
, s
.key
);
172 ++ idx
->bad_add_count
;
174 CacheEntry
*e
= cacheEntryCreate(&s
);
175 hash_join(idx
->hash
, &e
->hash
);
178 } else if (s
.op
== SWAP_LOG_DEL
) {
179 CacheEntry
*olde
= (CacheEntry
*) hash_lookup(idx
->hash
, s
.key
);
182 ++ idx
->bad_del_count
;
185 hash_remove_link(idx
->hash
, (hash_link
*) olde
);
186 cacheEntryDestroy(olde
);
190 fprintf(stderr
, "%s:%d: unknown swap log action\n", fname
, count
);
195 fprintf(stderr
, "%s:%d: scanned (size: %d bytes)\n",
196 fname
, count
, (int) (count
* sizeof(CacheEntry
)));
201 cacheIndexCmpReport(CacheIndex
* idx
, int shared_count
)
203 assert(idx
&& shared_count
<= idx
->count
);
205 printf("%s:\t %7d = %7d + %7d (%7.2f%% + %7.2f%%)\n",
208 idx
->count
- shared_count
,
210 xpercent(idx
->count
- shared_count
, idx
->count
),
211 xpercent(shared_count
, idx
->count
));
215 cacheIndexCmp(CacheIndex
* idx1
, CacheIndex
* idx2
)
217 int shared_count
= 0;
218 int hashed_count
= 0;
219 hash_link
*hashr
= NULL
;
220 CacheIndex
*small_idx
= idx1
;
221 CacheIndex
*large_idx
= idx2
;
222 assert(idx1
&& idx2
);
224 /* check our guess */
226 if (idx1
->count
> idx2
->count
) {
231 /* find shared_count */
232 hash_first(small_idx
->hash
);
234 for (hashr
= hash_next(small_idx
->hash
)) {
237 if (hash_lookup(large_idx
->hash
, hashr
->key
))
241 assert(hashed_count
== small_idx
->count
);
243 cacheIndexCmpReport(idx1
, shared_count
);
244 cacheIndexCmpReport(idx2
, shared_count
);
248 usage(const char *prg_name
)
250 fprintf(stderr
, "usage: %s <label1>: <swap_state>... <label2>: <swap_state>...\n",
256 main(int argc
, char *argv
[])
258 CacheIndex
*CacheIdx
[2];
259 CacheIndex
*idx
= NULL
;
264 return usage(argv
[0]);
266 for (i
= 1; i
< argc
; ++i
) {
267 const int len
= strlen(argv
[i
]);
270 return usage(argv
[0]);
272 if (argv
[i
][len
- 1] == ':') {
275 if (len
< 2 || idxCount
> 2)
276 return usage(argv
[0]);
278 idx
= cacheIndexCreate(argv
[i
]);
280 CacheIdx
[idxCount
- 1] = idx
;
283 return usage(argv
[0]);
285 cacheIndexAddLog(idx
, argv
[i
]);
290 return usage(argv
[0]);
292 cacheIndexInitReport(CacheIdx
[0]);
294 cacheIndexInitReport(CacheIdx
[1]);
296 cacheIndexCmp(CacheIdx
[0], CacheIdx
[1]);
298 cacheIndexDestroy(CacheIdx
[0]);
300 cacheIndexDestroy(CacheIdx
[1]);