-
/*
- * $Id: test_cache_digest.cc,v 1.35 2008/01/20 08:54:28 amosjeffries Exp $
- *
- * AUTHOR: Alex Rousskov
- *
- * SQUID Web Proxy Cache http://www.squid-cache.org/
- * ----------------------------------------------------------
- *
- * Squid is the result of efforts by numerous individuals from
- * the Internet community; see the CONTRIBUTORS file for full
- * details. Many organizations have provided support for Squid's
- * development; see the SPONSORS file for full details. Squid is
- * Copyrighted (C) 2001 by the Regents of the University of
- * California; see the COPYRIGHT file for full details. Squid
- * incorporates software developed and/or copyrighted by other
- * sources; see the CREDITS file for full details.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
+ * Copyright (C) 1996-2018 The Squid Software Foundation and contributors
*
+ * Squid software is distributed under GPLv2+ license and includes
+ * contributions from numerous individuals and organizations.
+ * Please see the COPYING and CONTRIBUTORS files for details.
*/
/*
*/
#include "squid.h"
+#include "CacheDigest.h"
+#include "store_key_md5.h"
-typedef struct
-{
+#include <cerrno>
+
+typedef struct {
int query_count;
int true_hit_count;
int true_miss_count;
int false_hit_count;
int false_miss_count;
-}
-
-CacheQueryStats;
+} CacheQueryStats;
typedef struct _Cache Cache;
-struct _Cache
-{
+struct _Cache {
const char *name;
hash_table *hash;
CacheDigest *digest;
Cache *peer;
CacheQueryStats qstats;
- int count; /* #currently cached entries */
- int req_count; /* #requests to this cache */
- int bad_add_count; /* #duplicate adds */
- int bad_del_count; /* #dels with no prior add */
+ int count; /* #currently cached entries */
+ int req_count; /* #requests to this cache */
+ int bad_add_count; /* #duplicate adds */
+ int bad_del_count; /* #dels with no prior add */
};
-
-typedef struct _CacheEntry
-{
+typedef struct _CacheEntry {
const cache_key *key;
struct _CacheEntry *next;
unsigned char key_arr[SQUID_MD5_DIGEST_LENGTH];
/* storeSwapLogData s; */
-}
-
-CacheEntry;
+} CacheEntry;
/* parsed access log entry */
-typedef struct
-{
+typedef struct {
cache_key key[SQUID_MD5_DIGEST_LENGTH];
time_t timestamp;
- short int use_icp; /* true/false */
-}
-
-RawAccessLogEntry;
+ short int use_icp; /* true/false */
+} RawAccessLogEntry;
typedef enum {
frError = -2, frMore = -1, frEof = 0, frOk = 1
typedef struct _FileIterator FileIterator;
typedef fr_result(*FI_READER) (FileIterator * fi);
-struct _FileIterator
-{
+struct _FileIterator {
const char *fname;
FILE *file;
- time_t inner_time; /* timestamp of the current entry */
- time_t time_offset; /* to adjust time set by reader */
- int line_count; /* number of lines scanned */
- int bad_line_count; /* number of parsing errors */
- int time_warp_count; /* number of out-of-order entries in the file */
- FI_READER reader; /* reads next entry and updates inner_time */
- void *entry; /* buffer for the current entry, freed with xfree() */
+ time_t inner_time; /* timestamp of the current entry */
+ time_t time_offset; /* to adjust time set by reader */
+ int line_count; /* number of lines scanned */
+ int bad_line_count; /* number of parsing errors */
+ int time_warp_count; /* number of out-of-order entries in the file */
+ FI_READER reader; /* reads next entry and updates inner_time */
+ void *entry; /* buffer for the current entry, freed with xfree() */
};
/* globals */
-static time_t cur_time = -1; /* timestamp of the current log entry */
-
-/* copied from url.c */
-const char *RequestMethodStr[] =
- {
- "NONE",
- "GET",
- "POST",
- "PUT",
- "HEAD",
- "CONNECT",
- "TRACE",
- "PURGE"
- };
+static time_t cur_time = -1; /* timestamp of the current log entry */
/* copied from url.c */
static HttpRequestMethod
const time_t last_time = fi->inner_time;
fi->inner_time = -1;
res = fi->reader(fi);
- fi->line_count++;
+ ++ fi->line_count;
if (fi->inner_time < 0)
fi->inner_time = last_time;
fi->inner_time += fi->time_offset;
if (res == frError)
- fi->bad_line_count++;
+ ++ fi->bad_line_count;
else if (res == frEof) {
fprintf(stderr, "exhausted %s (%d entries) at %s",
fi->fname, fi->line_count, ctime(&fi->inner_time));
fi->inner_time = -1;
} else if (fi->inner_time < last_time) {
assert(last_time >= 0);
- fi->time_warp_count++;
+ ++ fi->time_warp_count;
fi->inner_time = last_time;
}
CacheEntry *e = (CacheEntry *)xcalloc(1, sizeof(CacheEntry));
assert(s);
/* e->s = *s; */
- xmemcpy(e->key_arr, s->key, SQUID_MD5_DIGEST_LENGTH);
+ memcpy(e->key_arr, s->key, SQUID_MD5_DIGEST_LENGTH);
e->key = &e->key_arr[0];
return e;
}
xfree(e);
}
-
/* Cache */
static Cache *
/* destroy the hash table itself */
hashFreeMemory(hash);
- if (cache->digest)
- cacheDigestDestroy(cache->digest);
-
+ delete cache->digest;
xfree(cache);
}
assert(cache);
fprintf(stderr, "%s: init-ing digest with %d entries\n", cache->name, cache->count);
- if (cache->digest)
- cacheDigestDestroy(cache->digest);
-
hash = cache->hash;
- cache->digest = cacheDigestCreate(cache->count + 1, 6);
+ delete cache->digest;
+ cache->digest = new CacheDigest(cache->count + 1, 6);
if (!cache->count)
return;
hash_first(hash);
while ((e = (CacheEntry *)hash_next(hash))) {
- cacheDigestAdd(cache->digest, e->key);
+ cache->digest->add(e->key);
}
gettimeofday(&t_end, NULL);
cacheQueryPeer(Cache * cache, const cache_key * key)
{
const int peer_has_it = hash_lookup(cache->peer->hash, key) != NULL;
- const int we_think_we_have_it = cacheDigestTest(cache->digest, key);
+ const int we_think_we_have_it = cache->digest->test(key);
- cache->qstats.query_count++;
+ ++ cache->qstats.query_count;
if (peer_has_it) {
if (we_think_we_have_it)
- cache->qstats.true_hit_count++;
+ ++ cache->qstats.true_hit_count;
else
- cache->qstats.false_miss_count++;
+ ++ cache->qstats.false_miss_count;
} else {
if (we_think_we_have_it)
- cache->qstats.false_hit_count++;
+ ++ cache->qstats.false_hit_count;
else
- cache->qstats.true_miss_count++;
+ ++ cache->qstats.true_miss_count;
}
}
cacheFetch(Cache * cache, const RawAccessLogEntry * e)
{
assert(e);
- cache->req_count++;
+ ++ cache->req_count;
if (e->use_icp)
cacheQueryPeer(cache, e->key);
entry = (RawAccessLogEntry*)fi->entry;
if (!fgets(buf, sizeof(buf), fi->file))
- return frEof; /* eof */
+ return frEof; /* eof */
entry->timestamp = fi->inner_time = (time_t) atoi(buf);
}
while (*url)
- url--;
+ --url;
- url++;
+ ++url;
*hier = '\0';
* strcmp(hier, "SSL_PARENT_MISS") &&
* strcmp(hier, "DEFAULT_PARENT");
*/
- xmemcpy(entry->key, storeKeyPublic(url, method_id), sizeof(entry->key));
+ memcpy(entry->key, storeKeyPublic(url, method_id), sizeof(entry->key));
/*fprintf(stdout, "%s:%d: %s %s %s %s\n",
* fname, count, method, storeKeyText(entry->key), url, hier); */
return frOk;
}
-
static void
cachePurge(Cache * cache, storeSwapLogData * s, int update_digest)
{
CacheEntry *olde = (CacheEntry *) hash_lookup(cache->hash, s->key);
if (!olde) {
- cache->bad_del_count++;
+ ++ cache->bad_del_count;
} else {
assert(cache->count);
hash_remove_link(cache->hash, (hash_link *) olde);
if (update_digest)
- cacheDigestDel(cache->digest, s->key);
+ cache->digest->remove(s->key);
cacheEntryDestroy(olde);
- cache->count--;
+ -- cache->count;
}
}
CacheEntry *olde = (CacheEntry *) hash_lookup(cache->hash, s->key);
if (olde) {
- cache->bad_add_count++;
+ ++ cache->bad_add_count;
} else {
CacheEntry *e = cacheEntryCreate(s);
hash_join(cache->hash, (hash_link *)&e->key);
- cache->count++;
+ ++ cache->count;
if (update_digest)
- cacheDigestAdd(cache->digest, e->key);
+ cache->digest->add(e->key);
}
}
{
fprintf(stderr, "usage: %s <access_log> <swap_state> ...\n",
prg_name);
- return -1;
+ return EXIT_FAILURE;
}
int
/* digest peer cache content */
cacheResetDigest(them);
- us->digest = cacheDigestClone(them->digest); /* @netw@ */
+ us->digest = them->digest->clone();
/* shift the time in access log to match ready_time */
fileIteratorSetCurTime(fis[0], ready_time);
next_time = fis[i]->inner_time;
}
- active_fi_count++;
+ ++active_fi_count;
}
}
xfree(fis);
cacheDestroy(them);
cacheDestroy(us);
- return 0;
+ return EXIT_SUCCESS;
}
+