]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Refactor Via stats to replace hash_table with std::unordered_map (#829)
authorFrancesco Chemolli <kinkie@squid-cache.org>
Fri, 28 May 2021 18:19:54 +0000 (18:19 +0000)
committerSquid Anubis <squid-anubis@squid-cache.org>
Sat, 29 May 2021 02:25:09 +0000 (02:25 +0000)
src/client_side_request.cc
src/log/access_log.cc
src/log/access_log.h
src/tests/stub_liblog.cc

index bd1c3e4170ff29f4b1f330651ffc215a6e96fe9a..6b9478320129cd1db54e6d3a1b3def3a1cb751fd 100644 (file)
@@ -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
 
index 6a1621a8af509c32ba69c606e9a9bd2d2fbe39c8..00b1efcb049b5fa6b98fa7b429fcdb768335ed2e 100644 (file)
 #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 <unordered_map>
+
 #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<SBuf, uint64_t>;
+/// counts the number of header field value occurrences
+using HeaderValueCounts = std::unordered_map<SBuf, uint64_t, std::hash<SBuf>, std::equal_to<SBuf>, PoolingAllocator<HeaderValueCountsElement> >;
+
+/// 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);
index 51f42cf443bda19e58205fca9ac7d068065eaa4a..4f973865032863e533d48f0445c1e2816071e96e 100644 (file)
 #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
index a07b333484515ae792c8a6ba8bc60a87c88c8a3d..acc36238371b1844a42da4e60937c0a3b7af2d4a 100644 (file)
@@ -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