2 * Resizable, Scalable, Concurrent Hash Table
4 * Copyright (c) 2014-2015 Thomas Graf <tgraf@suug.ch>
5 * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 /**************************************************************************
14 **************************************************************************/
16 #include <linux/init.h>
17 #include <linux/jhash.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/rcupdate.h>
21 #include <linux/rhashtable.h>
22 #include <linux/slab.h>
23 #include <linux/sched.h>
25 #define MAX_ENTRIES 1000000
26 #define TEST_INSERT_FAIL INT_MAX
28 static int entries = 50000;
29 module_param(entries, int, 0);
30 MODULE_PARM_DESC(entries, "Number of entries to add (default: 50000)");
33 module_param(runs, int, 0);
34 MODULE_PARM_DESC(runs, "Number of test runs per variant (default: 4)");
36 static int max_size = 65536;
37 module_param(max_size, int, 0);
38 MODULE_PARM_DESC(runs, "Maximum table size (default: 65536)");
40 static bool shrinking = false;
41 module_param(shrinking, bool, 0);
42 MODULE_PARM_DESC(shrinking, "Enable automatic shrinking (default: off)");
45 module_param(size, int, 0);
46 MODULE_PARM_DESC(size, "Initial size hint of table (default: 8)");
50 struct rhash_head node;
53 static struct test_obj array[MAX_ENTRIES];
55 static struct rhashtable_params test_rht_params = {
56 .head_offset = offsetof(struct test_obj, node),
57 .key_offset = offsetof(struct test_obj, value),
58 .key_len = sizeof(int),
60 .nulls_base = (3U << RHT_BASE_SHIFT),
63 static int __init test_rht_lookup(struct rhashtable *ht)
67 for (i = 0; i < entries * 2; i++) {
69 bool expected = !(i % 2);
72 if (array[i / 2].value == TEST_INSERT_FAIL)
75 obj = rhashtable_lookup_fast(ht, &key, test_rht_params);
77 if (expected && !obj) {
78 pr_warn("Test failed: Could not find key %u\n", key);
80 } else if (!expected && obj) {
81 pr_warn("Test failed: Unexpected entry found for key %u\n",
84 } else if (expected && obj) {
85 if (obj->value != i) {
86 pr_warn("Test failed: Lookup value mismatch %u!=%u\n",
98 static void test_bucket_stats(struct rhashtable *ht)
100 unsigned int err, total = 0, chain_len = 0;
101 struct rhashtable_iter hti;
102 struct rhash_head *pos;
104 err = rhashtable_walk_init(ht, &hti);
106 pr_warn("Test failed: allocation error");
110 err = rhashtable_walk_start(&hti);
111 if (err && err != -EAGAIN) {
112 pr_warn("Test failed: iterator failed: %d\n", err);
116 while ((pos = rhashtable_walk_next(&hti))) {
117 if (PTR_ERR(pos) == -EAGAIN) {
118 pr_info("Info: encountered resize\n");
121 } else if (IS_ERR(pos)) {
122 pr_warn("Test failed: rhashtable_walk_next() error: %ld\n",
130 rhashtable_walk_stop(&hti);
131 rhashtable_walk_exit(&hti);
133 pr_info(" Traversal complete: counted=%u, nelems=%u, entries=%d, table-jumps=%u\n",
134 total, atomic_read(&ht->nelems), entries, chain_len);
136 if (total != atomic_read(&ht->nelems) || total != entries)
137 pr_warn("Test failed: Total count mismatch ^^^");
140 static s64 __init test_rhashtable(struct rhashtable *ht)
142 struct test_obj *obj;
144 unsigned int i, insert_fails = 0;
149 * Insert entries into table with all keys even numbers
151 pr_info(" Adding %d keys\n", entries);
152 start = ktime_get_ns();
153 for (i = 0; i < entries; i++) {
154 struct test_obj *obj = &array[i];
158 err = rhashtable_insert_fast(ht, &obj->node, test_rht_params);
159 if (err == -ENOMEM || err == -EBUSY) {
160 /* Mark failed inserts but continue */
161 obj->value = TEST_INSERT_FAIL;
171 pr_info(" %u insertions failed due to memory pressure\n",
174 test_bucket_stats(ht);
179 test_bucket_stats(ht);
181 pr_info(" Deleting %d keys\n", entries);
182 for (i = 0; i < entries; i++) {
185 if (array[i].value != TEST_INSERT_FAIL) {
186 obj = rhashtable_lookup_fast(ht, &key, test_rht_params);
189 rhashtable_remove_fast(ht, &obj->node, test_rht_params);
195 end = ktime_get_ns();
196 pr_info(" Duration of test: %lld ns\n", end - start);
201 static struct rhashtable ht;
203 static int __init test_rht_init(void)
208 entries = min(entries, MAX_ENTRIES);
210 test_rht_params.automatic_shrinking = shrinking;
211 test_rht_params.max_size = max_size;
212 test_rht_params.nelem_hint = size;
214 pr_info("Running rhashtable test nelem=%d, max_size=%d, shrinking=%d\n",
215 size, max_size, shrinking);
217 for (i = 0; i < runs; i++) {
220 pr_info("Test %02d:\n", i);
221 memset(&array, 0, sizeof(array));
222 err = rhashtable_init(&ht, &test_rht_params);
224 pr_warn("Test failed: Unable to initialize hashtable: %d\n",
229 time = test_rhashtable(&ht);
230 rhashtable_destroy(&ht);
232 pr_warn("Test failed: return code %lld\n", time);
239 do_div(total_time, runs);
240 pr_info("Average test time: %llu\n", total_time);
245 static void __exit test_rht_exit(void)
249 module_init(test_rht_init);
250 module_exit(test_rht_exit);
252 MODULE_LICENSE("GPL v2");