Use the hash from hash.c in the limiter 68/242868/1
authorMateusz Majewski <m.majewski2@samsung.com>
Tue, 1 Sep 2020 10:08:26 +0000 (12:08 +0200)
committerMateusz Majewski <m.majewski2@samsung.com>
Tue, 1 Sep 2020 10:17:25 +0000 (12:17 +0200)
We have a nicely tested hash implementation in hash.c. It makes little
sense for the loglimiter to have its own hash.

Change-Id: I09e8c8bb8e2dcc385a2063d794bf5c627fa12460

Makefile.am
include/loglimiter-internal.h
src/shared/loglimiter.c

index 9c47857..94ddad0 100644 (file)
@@ -185,6 +185,7 @@ dlogctl_SOURCES = \
        src/shared/logconfig.c \
        src/shared/parsers.c \
        src/shared/ptrs_list.c \
+       src/shared/hash.c \
        src/shared/loglimiter.c \
        src/logctl/logctl.c
 
@@ -380,11 +381,11 @@ src_tests_test_common_pos_SOURCES = src/tests/test_common_pos.c src/shared/logco
 src_tests_test_common_pos_CFLAGS = $(check_CFLAGS)
 src_tests_test_common_pos_LDFLAGS = $(AM_LDFLAGS) -Wl,--wrap=sendmsg,--wrap=recvmsg,--wrap=writev,--wrap=read,--wrap=poll,--wrap=fcntl,--wrap=fcntl64,--wrap=malloc,--wrap=calloc,--wrap=connect,--wrap=socket,--wrap=open,--wrap=open64,--wrap=ioctl
 
-src_tests_limiter_pos_SOURCES = src/tests/limiter_pos.c src/shared/loglimiter.c src/shared/logconfig.c src/shared/logcommon.c src/shared/parsers.c src/shared/ptrs_list.c
+src_tests_limiter_pos_SOURCES = src/tests/limiter_pos.c src/shared/hash.c src/shared/loglimiter.c src/shared/logconfig.c src/shared/logcommon.c src/shared/parsers.c src/shared/ptrs_list.c
 src_tests_limiter_pos_CFLAGS = $(check_CFLAGS)
 src_tests_limiter_pos_LDFLAGS = $(AM_LDFLAGS) -Wl,--wrap=snprintf,--wrap=malloc,--wrap=time
 
-src_tests_limiter_neg_SOURCES = src/tests/limiter_neg.c src/shared/loglimiter.c src/shared/logconfig.c src/shared/logcommon.c src/shared/parsers.c src/shared/ptrs_list.c
+src_tests_limiter_neg_SOURCES = src/tests/limiter_neg.c src/shared/hash.c src/shared/loglimiter.c src/shared/logconfig.c src/shared/logcommon.c src/shared/parsers.c src/shared/ptrs_list.c
 src_tests_limiter_neg_CFLAGS = $(check_CFLAGS)
 src_tests_limiter_neg_LDFLAGS = $(AM_LDFLAGS) -Wl,--wrap=snprintf,--wrap=malloc,--wrap=time
 
@@ -484,7 +485,7 @@ src_tests_queued_entry_monotonic_pos_SOURCES = src/tests/queued_entry_pos.c src/
 src_tests_queued_entry_monotonic_pos_CFLAGS = $(check_CFLAGS) -DUSE_ANDROID_MONOTONIC
 src_tests_queued_entry_monotonic_pos_LDFLAGS = $(AM_LDFLAGS)
 
-src_tests_logctl_SOURCES = src/tests/logctl.c src/logctl/logctl.c src/shared/parsers.c src/shared/logconfig.c src/shared/loglimiter.c src/shared/logcommon.c src/shared/ptrs_list.c
+src_tests_logctl_SOURCES = src/tests/logctl.c src/logctl/logctl.c src/shared/parsers.c src/shared/logconfig.c src/shared/hash.c src/shared/loglimiter.c src/shared/logcommon.c src/shared/ptrs_list.c
 src_tests_logctl_CFLAGS = $(check_CFLAGS)
 src_tests_logctl_LDFLAGS = $(AM_LDFLAGS) -Wl,--wrap=calloc,--wrap=asprintf,--wrap=open,--wrap=open64,--wrap=fdopen,--wrap=fdopen64,--wrap=mkstemp,--wrap=mkstemp64,--wrap=fchmod,--wrap=rename
 
@@ -615,6 +616,7 @@ src_tests_critical_log_CFLAGS = $(check_CFLAGS) -pthread
 src_tests_critical_log_LDFLAGS = $(AM_LDFLAGS) -lpthread -Wl,--wrap=execv,--wrap=clock_gettime
 
 src_tests_pid_limiter_SOURCES = src/tests/pid_limiter.c \
+       src/shared/hash.c \
        src/shared/loglimiter.c \
        src/shared/logcommon.c \
        src/shared/logconfig.c \
index 87a4570..ce69126 100644 (file)
@@ -22,6 +22,6 @@ struct rule {
 };
 
 int __log_limiter_initialize(struct rule *rules_table);
-unsigned util_hash_key(const char* s, int c);
+uint32_t util_hash_key(const char* s, int c);
 
 #endif // LOGLIMITER_INTERNAL_H_
index 5a3eb01..dc24a94 100644 (file)
@@ -40,9 +40,7 @@
 #include <logconfig.h>
 #include <ptrs_list.h>
 #include <qos_defaults.h>
-
-/* Some random big odd number to make hash more diverse */
-#define HASH_MAGIC_THINGY         5237231
+#include <hash.h>
 
 typedef int (*hash_cmp_func_t)(struct rule*, struct rule*);
 typedef int (*hash_match_func_t)(struct rule*, unsigned, const char*, int);
@@ -208,22 +206,15 @@ static int util_prio_to_char(int prio)
        }
 }
 
-/* The key is built from TAG and priority by DJB algorithm (Dan Bernstein).
- * Key is only 31 bits long. Negative values are keep to hold NULL bucket */
-unsigned util_hash_key(const char* s, int c)
+uint32_t util_hash_key(const char* s, int c)
 {
-       unsigned hash = (unsigned)c;
-
-       hash = ((hash << 5) + hash) + c;
-
-       if (s)
-               while ('\0' != (c = *s++))
-                       hash = ((hash << 5) + hash) + c;
+       if (!s)
+               return 0;
 
-       /* Makes the hash more diverse */
-       hash *= HASH_MAGIC_THINGY;
+       const uint32_t hash = create_hash(s, strlen(s));
 
-       return hash;
+       // Hardly random, but it doesn't have to be.
+       return hash ^ c;
 }
 
 /* Create hashmap, it's internal interface. */