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 test_obj array[MAX_ENTRIES];
54 static struct rhashtable_params test_rht_params = {
55 .head_offset = offsetof(struct test_obj, node),
56 .key_offset = offsetof(struct test_obj, value),
57 .key_len = sizeof(int),
59 .nulls_base = (3U << RHT_BASE_SHIFT),
62 static int __init test_rht_lookup(struct rhashtable *ht)
66 for (i = 0; i < entries * 2; i++) {
68 bool expected = !(i % 2);
71 obj = rhashtable_lookup_fast(ht, &key, test_rht_params);
73 if (expected && !obj) {
74 pr_warn("Test failed: Could not find key %u\n", key);
76 } else if (!expected && obj) {
77 pr_warn("Test failed: Unexpected entry found for key %u\n",
80 } else if (expected && obj) {
81 if (obj->value != i) {
82 pr_warn("Test failed: Lookup value mismatch %u!=%u\n",
92 static void test_bucket_stats(struct rhashtable *ht)
94 unsigned int err, total = 0, chain_len = 0;
95 struct rhashtable_iter hti;
96 struct rhash_head *pos;
98 err = rhashtable_walk_init(ht, &hti);
100 pr_warn("Test failed: allocation error");
104 err = rhashtable_walk_start(&hti);
105 if (err && err != -EAGAIN) {
106 pr_warn("Test failed: iterator failed: %d\n", err);
110 while ((pos = rhashtable_walk_next(&hti))) {
111 if (PTR_ERR(pos) == -EAGAIN) {
112 pr_info("Info: encountered resize\n");
115 } else if (IS_ERR(pos)) {
116 pr_warn("Test failed: rhashtable_walk_next() error: %ld\n",
124 rhashtable_walk_stop(&hti);
125 rhashtable_walk_exit(&hti);
127 pr_info(" Traversal complete: counted=%u, nelems=%u, entries=%d, table-jumps=%u\n",
128 total, atomic_read(&ht->nelems), entries, chain_len);
130 if (total != atomic_read(&ht->nelems) || total != entries)
131 pr_warn("Test failed: Total count mismatch ^^^");
134 static s64 __init test_rhashtable(struct rhashtable *ht)
136 struct test_obj *obj;
143 * Insert entries into table with all keys even numbers
145 pr_info(" Adding %d keys\n", entries);
146 start = ktime_get_ns();
147 for (i = 0; i < entries; i++) {
148 struct test_obj *obj = &array[i];
152 err = rhashtable_insert_fast(ht, &obj->node, test_rht_params);
157 test_bucket_stats(ht);
162 test_bucket_stats(ht);
164 pr_info(" Deleting %d keys\n", entries);
165 for (i = 0; i < entries; i++) {
168 obj = rhashtable_lookup_fast(ht, &key, test_rht_params);
171 rhashtable_remove_fast(ht, &obj->node, test_rht_params);
174 end = ktime_get_ns();
175 pr_info(" Duration of test: %lld ns\n", end - start);
180 static struct rhashtable ht;
182 static int __init test_rht_init(void)
187 entries = min(entries, MAX_ENTRIES);
189 test_rht_params.automatic_shrinking = shrinking;
190 test_rht_params.max_size = max_size;
191 test_rht_params.nelem_hint = size;
193 pr_info("Running rhashtable test nelem=%d, max_size=%d, shrinking=%d\n",
194 size, max_size, shrinking);
196 for (i = 0; i < runs; i++) {
199 pr_info("Test %02d:\n", i);
200 memset(&array, 0, sizeof(array));
201 err = rhashtable_init(&ht, &test_rht_params);
203 pr_warn("Test failed: Unable to initialize hashtable: %d\n",
208 time = test_rhashtable(&ht);
209 rhashtable_destroy(&ht);
211 pr_warn("Test failed: return code %lld\n", time);
218 pr_info("Average test time: %llu\n", total_time / runs);
223 static void __exit test_rht_exit(void)
227 module_init(test_rht_init);
228 module_exit(test_rht_exit);
230 MODULE_LICENSE("GPL v2");