blob: 935693ed7ae51e45e85aa40314d36b281cb6beee [file] [log] [blame]
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +01001/*
2 * Resizable, Scalable, Concurrent Hash Table
3 *
Thomas Graf1aa661f2015-04-30 22:37:41 +00004 * Copyright (c) 2014-2015 Thomas Graf <tgraf@suug.ch>
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +01005 * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net>
6 *
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +01007 * 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.
10 */
11
12/**************************************************************************
13 * Self Test
14 **************************************************************************/
15
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
24
Thomas Graf1aa661f2015-04-30 22:37:41 +000025#define MAX_ENTRIES 1000000
26
27static int entries = 50000;
28module_param(entries, int, 0);
29MODULE_PARM_DESC(entries, "Number of entries to add (default: 50000)");
30
31static int runs = 4;
32module_param(runs, int, 0);
33MODULE_PARM_DESC(runs, "Number of test runs per variant (default: 4)");
34
35static int max_size = 65536;
36module_param(max_size, int, 0);
37MODULE_PARM_DESC(runs, "Maximum table size (default: 65536)");
38
39static bool shrinking = false;
40module_param(shrinking, bool, 0);
41MODULE_PARM_DESC(shrinking, "Enable automatic shrinking (default: off)");
42
43static int size = 8;
44module_param(size, int, 0);
45MODULE_PARM_DESC(size, "Initial size hint of table (default: 8)");
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +010046
47struct test_obj {
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +010048 int value;
49 struct rhash_head node;
50};
51
Thomas Graffcc57022015-04-30 22:37:43 +000052static struct test_obj array[MAX_ENTRIES];
53
Thomas Graf1aa661f2015-04-30 22:37:41 +000054static struct rhashtable_params test_rht_params = {
Herbert Xub182aa62015-03-20 21:57:04 +110055 .head_offset = offsetof(struct test_obj, node),
56 .key_offset = offsetof(struct test_obj, value),
57 .key_len = sizeof(int),
58 .hashfn = jhash,
Herbert Xub182aa62015-03-20 21:57:04 +110059 .nulls_base = (3U << RHT_BASE_SHIFT),
60};
61
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +010062static int __init test_rht_lookup(struct rhashtable *ht)
63{
64 unsigned int i;
65
Thomas Graf1aa661f2015-04-30 22:37:41 +000066 for (i = 0; i < entries * 2; i++) {
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +010067 struct test_obj *obj;
68 bool expected = !(i % 2);
69 u32 key = i;
70
Herbert Xub182aa62015-03-20 21:57:04 +110071 obj = rhashtable_lookup_fast(ht, &key, test_rht_params);
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +010072
73 if (expected && !obj) {
74 pr_warn("Test failed: Could not find key %u\n", key);
75 return -ENOENT;
76 } else if (!expected && obj) {
77 pr_warn("Test failed: Unexpected entry found for key %u\n",
78 key);
79 return -EEXIST;
80 } else if (expected && obj) {
Thomas Grafc2c8a902015-04-30 22:37:42 +000081 if (obj->value != i) {
82 pr_warn("Test failed: Lookup value mismatch %u!=%u\n",
83 obj->value, i);
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +010084 return -EINVAL;
85 }
86 }
87 }
88
89 return 0;
90}
91
92static void test_bucket_stats(struct rhashtable *ht, bool quiet)
93{
94 unsigned int cnt, rcu_cnt, i, total = 0;
95 struct rhash_head *pos;
96 struct test_obj *obj;
97 struct bucket_table *tbl;
98
99 tbl = rht_dereference_rcu(ht->tbl, ht);
100 for (i = 0; i < tbl->size; i++) {
101 rcu_cnt = cnt = 0;
102
103 if (!quiet)
Herbert Xu63d512d2015-03-14 13:57:24 +1100104 pr_info(" [%#4x/%u]", i, tbl->size);
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100105
106 rht_for_each_entry_rcu(obj, pos, tbl, i, node) {
107 cnt++;
108 total++;
109 if (!quiet)
110 pr_cont(" [%p],", obj);
111 }
112
113 rht_for_each_entry_rcu(obj, pos, tbl, i, node)
114 rcu_cnt++;
115
116 if (rcu_cnt != cnt)
117 pr_warn("Test failed: Chain count mismach %d != %d",
118 cnt, rcu_cnt);
119
120 if (!quiet)
121 pr_cont("\n [%#x] first element: %p, chain length: %u\n",
122 i, tbl->buckets[i], cnt);
123 }
124
125 pr_info(" Traversal complete: counted=%u, nelems=%u, entries=%d\n",
Thomas Graf1aa661f2015-04-30 22:37:41 +0000126 total, atomic_read(&ht->nelems), entries);
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100127
Thomas Graf1aa661f2015-04-30 22:37:41 +0000128 if (total != atomic_read(&ht->nelems) || total != entries)
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100129 pr_warn("Test failed: Total count mismatch ^^^");
130}
131
Thomas Graf1aa661f2015-04-30 22:37:41 +0000132static s64 __init test_rhashtable(struct rhashtable *ht)
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100133{
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100134 struct test_obj *obj;
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100135 int err;
136 unsigned int i;
Thomas Graf1aa661f2015-04-30 22:37:41 +0000137 s64 start, end;
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100138
139 /*
140 * Insertion Test:
Thomas Graf1aa661f2015-04-30 22:37:41 +0000141 * Insert entries into table with all keys even numbers
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100142 */
Thomas Graf1aa661f2015-04-30 22:37:41 +0000143 pr_info(" Adding %d keys\n", entries);
144 start = ktime_get_ns();
145 for (i = 0; i < entries; i++) {
Thomas Graffcc57022015-04-30 22:37:43 +0000146 struct test_obj *obj = &array[i];
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100147
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100148 obj->value = i * 2;
149
Herbert Xub182aa62015-03-20 21:57:04 +1100150 err = rhashtable_insert_fast(ht, &obj->node, test_rht_params);
Thomas Graffcc57022015-04-30 22:37:43 +0000151 if (err)
152 return err;
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100153 }
154
155 rcu_read_lock();
156 test_bucket_stats(ht, true);
157 test_rht_lookup(ht);
158 rcu_read_unlock();
159
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100160 rcu_read_lock();
161 test_bucket_stats(ht, true);
162 rcu_read_unlock();
163
Thomas Graf1aa661f2015-04-30 22:37:41 +0000164 pr_info(" Deleting %d keys\n", entries);
165 for (i = 0; i < entries; i++) {
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100166 u32 key = i * 2;
167
Herbert Xub182aa62015-03-20 21:57:04 +1100168 obj = rhashtable_lookup_fast(ht, &key, test_rht_params);
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100169 BUG_ON(!obj);
170
Herbert Xub182aa62015-03-20 21:57:04 +1100171 rhashtable_remove_fast(ht, &obj->node, test_rht_params);
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100172 }
173
Thomas Graf1aa661f2015-04-30 22:37:41 +0000174 end = ktime_get_ns();
175 pr_info(" Duration of test: %lld ns\n", end - start);
176
177 return end - start;
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100178}
179
Daniel Borkmannb7f5e5c2015-02-20 21:14:21 +0100180static struct rhashtable ht;
181
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100182static int __init test_rht_init(void)
183{
Thomas Graf1aa661f2015-04-30 22:37:41 +0000184 int i, err;
185 u64 total_time = 0;
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100186
Thomas Graf1aa661f2015-04-30 22:37:41 +0000187 entries = min(entries, MAX_ENTRIES);
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100188
Thomas Graf1aa661f2015-04-30 22:37:41 +0000189 test_rht_params.automatic_shrinking = shrinking;
190 test_rht_params.max_size = max_size;
191 test_rht_params.nelem_hint = size;
192
193 pr_info("Running rhashtable test nelem=%d, max_size=%d, shrinking=%d\n",
194 size, max_size, shrinking);
195
196 for (i = 0; i < runs; i++) {
197 s64 time;
198
199 pr_info("Test %02d:\n", i);
Thomas Graffcc57022015-04-30 22:37:43 +0000200 memset(&array, 0, sizeof(array));
Thomas Graf1aa661f2015-04-30 22:37:41 +0000201 err = rhashtable_init(&ht, &test_rht_params);
202 if (err < 0) {
203 pr_warn("Test failed: Unable to initialize hashtable: %d\n",
204 err);
205 continue;
206 }
207
208 time = test_rhashtable(&ht);
209 rhashtable_destroy(&ht);
210 if (time < 0) {
211 pr_warn("Test failed: return code %lld\n", time);
212 return -EINVAL;
213 }
214
215 total_time += time;
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100216 }
217
Thomas Graf1aa661f2015-04-30 22:37:41 +0000218 pr_info("Average test time: %llu\n", total_time / runs);
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100219
Thomas Graf1aa661f2015-04-30 22:37:41 +0000220 return 0;
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100221}
222
Daniel Borkmann6dd0c162015-02-20 00:53:39 +0100223static void __exit test_rht_exit(void)
224{
225}
226
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100227module_init(test_rht_init);
Daniel Borkmann6dd0c162015-02-20 00:53:39 +0100228module_exit(test_rht_exit);
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100229
230MODULE_LICENSE("GPL v2");