Add a hashing function (incl. tests) 90/235890/8
authorAgnieszka Baumann <a.baumann@samsung.com>
Wed, 10 Jun 2020 15:45:00 +0000 (17:45 +0200)
committerAgnieszka Baumann <a.baumann@samsung.com>
Wed, 15 Jul 2020 10:55:52 +0000 (12:55 +0200)
To be used for libdlog deduplication

Change-Id: I1cea213fcb191b1b306fe6a021366f18f1716dfe

Makefile.am
include/hash.h [new file with mode: 0644]
src/shared/hash.c [new file with mode: 0644]
src/tests/hash_test.c [new file with mode: 0644]

index c8594ae..0422a0e 100644 (file)
@@ -300,6 +300,7 @@ check_PROGRAMS = \
        src/tests/logutil_neg \
        src/tests/critical_log \
        src/tests/salvage_pipe_entry \
+       src/tests/hash_test \
        src/tests/filters
 
 check_CFLAGS = $(AM_CFLAGS) -O0 -fprofile-arcs -DUNIT_TEST \
@@ -562,6 +563,10 @@ src_tests_filters_SOURCES = src/tests/filters.c src/shared/ptrs_list.c src/share
 src_tests_filters_CFLAGS = $(check_CFLAGS)
 src_tests_filters_LDFLAGS = $(AM_LDFLAGS)
 
+src_tests_hash_test_SOURCES = src/tests/hash_test.c src/shared/hash.c
+src_tests_hash_test_CFLAGS = $(check_CFLAGS)
+src_tests_hash_test_LDFLAGS = $(AM_LDFLAGS)
+
 # conf file
 usrlibtmpfilesddir = /usr/lib/tmpfiles.d
 usrlibtmpfilesd_DATA = configs/dlog-run.conf
diff --git a/include/hash.h b/include/hash.h
new file mode 100644 (file)
index 0000000..db8ecdb
--- /dev/null
@@ -0,0 +1,31 @@
+/*  MIT License
+ *
+ * Copyright (c) 2020 Samsung Electronics Co., Ltd
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is furnished
+ * to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE. */
+
+#ifndef _HASH_H_
+#define _HASH_H_
+
+// C
+#include <stdint.h>
+#include <stdlib.h>
+
+uint32_t create_hash(const char *msg, size_t len);
+#endif /* _HASH_H_ */
diff --git a/src/shared/hash.c b/src/shared/hash.c
new file mode 100644 (file)
index 0000000..6a498f6
--- /dev/null
@@ -0,0 +1,25 @@
+/* Copyright 2006 Bob Jenkins - Public Domain
+ * https://www.burtleburtle.net/bob/hash/doobs.html */
+
+#include "hash.h"
+
+static uint32_t jenkins_one_at_the_time_hash(const char *msg, size_t len)
+{
+       uint32_t hash = 0;
+
+       for (size_t i = 0; i < len; ++i) {
+               hash += msg[i];
+               hash += (hash << 10);
+               hash ^= (hash >> 6);
+       }
+       hash += (hash << 3);
+       hash ^= (hash >> 11);
+       hash += (hash << 15);
+
+       return hash;
+}
+
+uint32_t create_hash(const char *msg, size_t len)
+{
+    return jenkins_one_at_the_time_hash(msg, len);
+}
diff --git a/src/tests/hash_test.c b/src/tests/hash_test.c
new file mode 100644 (file)
index 0000000..261a973
--- /dev/null
@@ -0,0 +1,114 @@
+/* Copyright (c) 2020, Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License. */
+
+// C
+#include <assert.h>
+#include <inttypes.h>
+#include <stdio.h>
+
+// DLog
+#include "hash.h"
+
+#define NUM_BUCKETS 10
+#define NELEMS(array) (sizeof array / sizeof *array)
+#define RANDOM_ITERATIONS 5000
+#define SEED 0
+
+static size_t index = 0;
+static char printables[36]; // a-z 0-9
+static uint32_t hash_values[(sizeof printables * sizeof printables * sizeof printables) + RANDOM_ITERATIONS * 3];
+static size_t collisions = 0;
+
+static void add_hash(const char *msg, size_t msg_size)
+{
+       const uint32_t new_hash = create_hash(msg, msg_size);
+
+       for (size_t i = 0; i < index; i++) {
+               if (hash_values[i] != new_hash)
+                       continue;
+               collisions++;
+               fprintf(stderr, "collided hash: [ %"PRIu32" ], from message: [ %.*s ]\n", hash_values[i], msg_size, msg);
+               return;
+       }
+
+       hash_values[index] = new_hash;
+       index++;
+}
+
+static void validate_hash()
+{
+       static const size_t BUCKET_SIZE = UINT32_MAX / NUM_BUCKETS + 1;
+       static const size_t BUCKET_AVG_COUNT = NELEMS(hash_values) / NUM_BUCKETS;
+       size_t buckets[NUM_BUCKETS] = {0};
+
+       for (size_t i = 0; i < index; i++) {
+               const int bucket = hash_values[i] / BUCKET_SIZE;
+               buckets[bucket]++;
+       }
+
+       /* One collision may appear, because of large number
+        * of elements, but if more, it should be checked. */
+       assert(collisions < 2);
+
+       for (size_t i = 0; i < NUM_BUCKETS; i++) {
+               assert(buckets[i] < (1.1 * BUCKET_AVG_COUNT));
+               assert(buckets[i] > (0.9 * BUCKET_AVG_COUNT));
+       }
+}
+
+static void test_permutations()
+{
+       char hash[3];
+
+       for (size_t i = 0; i < sizeof printables; i++)
+               for (size_t j = 0; j < sizeof printables; j++)
+                       for (size_t k = 0; k < sizeof printables; k++) {
+                               hash[0] = printables[i];
+                               hash[1] = printables[j];
+                               hash[2] = printables[k];
+                               add_hash(hash, sizeof hash);
+                       }
+}
+
+static void test_random(size_t size)
+{
+       char hash[size];
+
+       for (int i = 0; i < sizeof hash; i++)
+               hash[i] = printables[rand() % sizeof printables];
+
+       add_hash(hash, sizeof hash);
+}
+
+int main()
+{
+       srand(SEED);
+
+       for (char c = 'a'; c <= 'z'; ++c)
+               printables[c - 'a'] = c;
+
+       for (char c = '0'; c <= '9'; ++c)
+               printables[c - '0' + 'z' - 'a' + 1] = c;
+
+       test_permutations();
+
+       for (size_t i = 0; i < RANDOM_ITERATIONS; i++) {
+               test_random(10);
+               test_random(30);
+               test_random(200);
+       }
+
+       validate_hash();
+       return 0;
+}