From: Francesco Chemolli Date: Fri, 28 May 2021 18:19:54 +0000 (+0000) Subject: Refactor Via stats to replace hash_table with std::unordered_map (#829) X-Git-Tag: SQUID_6_0_1~329 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=cf1f23eea5cf4bc3053f6153ac0b96c415ad6033;p=thirdparty%2Fsquid.git Refactor Via stats to replace hash_table with std::unordered_map (#829) --- diff --git a/src/client_side_request.cc b/src/client_side_request.cc index bd1c3e4170..6b94783201 100644 --- a/src/client_side_request.cc +++ b/src/client_side_request.cc @@ -51,6 +51,7 @@ #include "proxyp/Header.h" #include "redirect.h" #include "rfc1738.h" +#include "sbuf/StringConvert.h" #include "SquidConfig.h" #include "SquidTime.h" #include "Store.h" @@ -1111,7 +1112,7 @@ clientInterpretRequestHeaders(ClientHttpRequest * http) } #if USE_FORW_VIA_DB - fvdbCountVia(s.termedBuf()); + fvdbCountVia(StringToSBuf(s)); #endif diff --git a/src/log/access_log.cc b/src/log/access_log.cc index 6a1621a8af..00b1efcb04 100644 --- a/src/log/access_log.cc +++ b/src/log/access_log.cc @@ -11,9 +11,11 @@ #include "squid.h" #include "AccessLogEntry.h" #include "acl/Checklist.h" +#include "sbuf/Algorithms.h" #if USE_ADAPTATION #include "adaptation/Config.h" #endif +#include "base/PackableStream.h" #include "CachePeer.h" #include "error/Detail.h" #include "errorpage.h" @@ -30,6 +32,7 @@ #include "MemBuf.h" #include "mgr/Registration.h" #include "rfc1738.h" +#include "sbuf/SBuf.h" #include "SquidConfig.h" #include "SquidTime.h" #include "Store.h" @@ -39,6 +42,8 @@ #include "eui/Eui64.h" #endif +#include + #if HEADERS_LOG static Logfile *headerslog = NULL; #endif @@ -56,7 +61,14 @@ typedef struct { hash_link hash; int n; } fvdb_entry; -static hash_table *via_table = NULL; + +using HeaderValueCountsElement = std::pair; +/// counts the number of header field value occurrences +using HeaderValueCounts = std::unordered_map, std::equal_to, PoolingAllocator >; + +/// counts the number of HTTP Via header field value occurrences +static HeaderValueCounts TheViaCounts; + static hash_table *forw_table = NULL; static void fvdbInit(); static void fvdbDumpTable(StoreEntry * e, hash_table * hash); @@ -443,7 +455,6 @@ accessLogInit(void) static void fvdbInit(void) { - via_table = hash_create((HASHCMP *) strcmp, 977, hash4); forw_table = hash_create((HASHCMP *) strcmp, 977, hash4); } @@ -475,9 +486,9 @@ fvdbCount(hash_table * hash, const char *key) } void -fvdbCountVia(const char *key) +fvdbCountVia(const SBuf &headerValue) { - fvdbCount(via_table, key); + ++TheViaCounts[headerValue]; } void @@ -503,10 +514,19 @@ fvdbDumpTable(StoreEntry * e, hash_table * hash) } } +static void +fvdbDumpCounts(StoreEntry &e, const HeaderValueCounts &counts) +{ + PackableStream os(e); + for (const auto &i : counts) + os << std::setw(9) << i.second << ' ' << i.first << "\n"; +} + static void fvdbDumpVia(StoreEntry * e) { - fvdbDumpTable(e, via_table); + assert(e); + fvdbDumpCounts(*e, TheViaCounts); } static void @@ -527,9 +547,7 @@ fvdbFreeEntry(void *data) static void fvdbClear(void) { - hashFreeItems(via_table, fvdbFreeEntry); - hashFreeMemory(via_table); - via_table = hash_create((HASHCMP *) strcmp, 977, hash4); + TheViaCounts.clear(); hashFreeItems(forw_table, fvdbFreeEntry); hashFreeMemory(forw_table); forw_table = hash_create((HASHCMP *) strcmp, 977, hash4); diff --git a/src/log/access_log.h b/src/log/access_log.h index 51f42cf443..4f97386503 100644 --- a/src/log/access_log.h +++ b/src/log/access_log.h @@ -12,8 +12,12 @@ #define SQUID_LOG_ACCESS_LOG_H_ #include "LogTags.h" +#include "sbuf/forward.h" + +/// count occurrences of the given Via header value +/// XXX: this function preserves all counted values until the next log rotation +void fvdbCountVia(const SBuf &); -void fvdbCountVia(const char *key); void fvdbCountForw(const char *key); #if HEADERS_LOG diff --git a/src/tests/stub_liblog.cc b/src/tests/stub_liblog.cc index a07b333484..acc3623837 100644 --- a/src/tests/stub_liblog.cc +++ b/src/tests/stub_liblog.cc @@ -30,7 +30,7 @@ void accessLogInit(void) STUB const char *accessLogTime(time_t) STUB_RETVAL(nullptr) #include "log/access_log.h" -void fvdbCountVia(const char *) STUB +void fvdbCountVia(const SBuf &) STUB void fvdbCountForw(const char *) STUB #if HEADERS_LOG void headersLog(int, int, const HttpRequestMethod &, void *) STUB