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>
25 #define MAX_ENTRIES 1000000
27 static int entries = 50000;
28 module_param(entries, int, 0);
29 MODULE_PARM_DESC(entries, "Number of entries to add (default: 50000)");
32 module_param(runs, int, 0);
33 MODULE_PARM_DESC(runs, "Number of test runs per variant (default: 4)");
35 static int max_size = 65536;
36 module_param(max_size, int, 0);
37 MODULE_PARM_DESC(runs, "Maximum table size (default: 65536)");
39 static bool shrinking = false;
40 module_param(shrinking, bool, 0);
41 MODULE_PARM_DESC(shrinking, "Enable automatic shrinking (default: off)");
44 module_param(size, int, 0);
45 MODULE_PARM_DESC(size, "Initial size hint of table (default: 8)");
49 struct rhash_head node;
52 static struct rhashtable_params test_rht_params = {
53 .head_offset = offsetof(struct test_obj, node),
54 .key_offset = offsetof(struct test_obj, value),
55 .key_len = sizeof(int),
57 .nulls_base = (3U << RHT_BASE_SHIFT),
60 static int __init test_rht_lookup(struct rhashtable *ht)
64 for (i = 0; i < entries * 2; i++) {
66 bool expected = !(i % 2);
69 obj = rhashtable_lookup_fast(ht, &key, test_rht_params);
71 if (expected && !obj) {
72 pr_warn("Test failed: Could not find key %u\n", key);
74 } else if (!expected && obj) {
75 pr_warn("Test failed: Unexpected entry found for key %u\n",
78 } else if (expected && obj) {
79 if (obj->value != i) {
80 pr_warn("Test failed: Lookup value mismatch %u!=%u\n",
90 static void test_bucket_stats(struct rhashtable *ht, bool quiet)
92 unsigned int cnt, rcu_cnt, i, total = 0;
93 struct rhash_head *pos;
95 struct bucket_table *tbl;
97 tbl = rht_dereference_rcu(ht->tbl, ht);
98 for (i = 0; i < tbl->size; i++) {
102 pr_info(" [%#4x/%u]", i, tbl->size);
104 rht_for_each_entry_rcu(obj, pos, tbl, i, node) {
108 pr_cont(" [%p],", obj);
111 rht_for_each_entry_rcu(obj, pos, tbl, i, node)
115 pr_warn("Test failed: Chain count mismach %d != %d",
119 pr_cont("\n [%#x] first element: %p, chain length: %u\n",
120 i, tbl->buckets[i], cnt);
123 pr_info(" Traversal complete: counted=%u, nelems=%u, entries=%d\n",
124 total, atomic_read(&ht->nelems), entries);
126 if (total != atomic_read(&ht->nelems) || total != entries)
127 pr_warn("Test failed: Total count mismatch ^^^");
130 static s64 __init test_rhashtable(struct rhashtable *ht)
132 struct bucket_table *tbl;
133 struct test_obj *obj;
134 struct rhash_head *pos, *next;
141 * Insert entries into table with all keys even numbers
143 pr_info(" Adding %d keys\n", entries);
144 start = ktime_get_ns();
145 for (i = 0; i < entries; i++) {
146 struct test_obj *obj;
148 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
156 err = rhashtable_insert_fast(ht, &obj->node, test_rht_params);
164 test_bucket_stats(ht, true);
169 test_bucket_stats(ht, true);
172 pr_info(" Deleting %d keys\n", entries);
173 for (i = 0; i < entries; i++) {
176 obj = rhashtable_lookup_fast(ht, &key, test_rht_params);
179 rhashtable_remove_fast(ht, &obj->node, test_rht_params);
183 end = ktime_get_ns();
184 pr_info(" Duration of test: %lld ns\n", end - start);
189 tbl = rht_dereference_rcu(ht->tbl, ht);
190 for (i = 0; i < tbl->size; i++)
191 rht_for_each_entry_safe(obj, pos, next, tbl, i, node)
197 static struct rhashtable ht;
199 static int __init test_rht_init(void)
204 entries = min(entries, MAX_ENTRIES);
206 test_rht_params.automatic_shrinking = shrinking;
207 test_rht_params.max_size = max_size;
208 test_rht_params.nelem_hint = size;
210 pr_info("Running rhashtable test nelem=%d, max_size=%d, shrinking=%d\n",
211 size, max_size, shrinking);
213 for (i = 0; i < runs; i++) {
216 pr_info("Test %02d:\n", i);
217 err = rhashtable_init(&ht, &test_rht_params);
219 pr_warn("Test failed: Unable to initialize hashtable: %d\n",
224 time = test_rhashtable(&ht);
225 rhashtable_destroy(&ht);
227 pr_warn("Test failed: return code %lld\n", time);
234 pr_info("Average test time: %llu\n", total_time / runs);
239 static void __exit test_rht_exit(void)
243 module_init(test_rht_init);
244 module_exit(test_rht_exit);
246 MODULE_LICENSE("GPL v2");