Merge tag 'batman-adv-for-davem' of git://git.open-mesh.org/linux-merge
[linux-block.git] / lib / test_rhashtable.c
CommitLineData
9d6dbe1b
GU
1/*
2 * Resizable, Scalable, Concurrent Hash Table
3 *
1aa661f5 4 * Copyright (c) 2014-2015 Thomas Graf <tgraf@suug.ch>
9d6dbe1b
GU
5 * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net>
6 *
9d6dbe1b
GU
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.
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>
685a015e 23#include <linux/sched.h>
9d6dbe1b 24
1aa661f5 25#define MAX_ENTRIES 1000000
67b7cbf4 26#define TEST_INSERT_FAIL INT_MAX
1aa661f5
TG
27
28static int entries = 50000;
29module_param(entries, int, 0);
30MODULE_PARM_DESC(entries, "Number of entries to add (default: 50000)");
31
32static int runs = 4;
33module_param(runs, int, 0);
34MODULE_PARM_DESC(runs, "Number of test runs per variant (default: 4)");
35
36static int max_size = 65536;
37module_param(max_size, int, 0);
38MODULE_PARM_DESC(runs, "Maximum table size (default: 65536)");
39
40static bool shrinking = false;
41module_param(shrinking, bool, 0);
42MODULE_PARM_DESC(shrinking, "Enable automatic shrinking (default: off)");
43
44static int size = 8;
45module_param(size, int, 0);
46MODULE_PARM_DESC(size, "Initial size hint of table (default: 8)");
9d6dbe1b
GU
47
48struct test_obj {
9d6dbe1b
GU
49 int value;
50 struct rhash_head node;
51};
52
fcc57020
TG
53static struct test_obj array[MAX_ENTRIES];
54
1aa661f5 55static struct rhashtable_params test_rht_params = {
b182aa6e
HX
56 .head_offset = offsetof(struct test_obj, node),
57 .key_offset = offsetof(struct test_obj, value),
58 .key_len = sizeof(int),
59 .hashfn = jhash,
b182aa6e
HX
60 .nulls_base = (3U << RHT_BASE_SHIFT),
61};
62
9d6dbe1b
GU
63static int __init test_rht_lookup(struct rhashtable *ht)
64{
65 unsigned int i;
66
1aa661f5 67 for (i = 0; i < entries * 2; i++) {
9d6dbe1b
GU
68 struct test_obj *obj;
69 bool expected = !(i % 2);
70 u32 key = i;
71
67b7cbf4
TG
72 if (array[i / 2].value == TEST_INSERT_FAIL)
73 expected = false;
74
b182aa6e 75 obj = rhashtable_lookup_fast(ht, &key, test_rht_params);
9d6dbe1b
GU
76
77 if (expected && !obj) {
78 pr_warn("Test failed: Could not find key %u\n", key);
79 return -ENOENT;
80 } else if (!expected && obj) {
81 pr_warn("Test failed: Unexpected entry found for key %u\n",
82 key);
83 return -EEXIST;
84 } else if (expected && obj) {
c2c8a901
TG
85 if (obj->value != i) {
86 pr_warn("Test failed: Lookup value mismatch %u!=%u\n",
87 obj->value, i);
9d6dbe1b
GU
88 return -EINVAL;
89 }
90 }
685a015e
TG
91
92 cond_resched_rcu();
9d6dbe1b
GU
93 }
94
95 return 0;
96}
97
246b23a7 98static void test_bucket_stats(struct rhashtable *ht)
9d6dbe1b 99{
246b23a7
TG
100 unsigned int err, total = 0, chain_len = 0;
101 struct rhashtable_iter hti;
9d6dbe1b 102 struct rhash_head *pos;
9d6dbe1b 103
246b23a7
TG
104 err = rhashtable_walk_init(ht, &hti);
105 if (err) {
106 pr_warn("Test failed: allocation error");
107 return;
108 }
9d6dbe1b 109
246b23a7
TG
110 err = rhashtable_walk_start(&hti);
111 if (err && err != -EAGAIN) {
112 pr_warn("Test failed: iterator failed: %d\n", err);
113 return;
114 }
9d6dbe1b 115
246b23a7
TG
116 while ((pos = rhashtable_walk_next(&hti))) {
117 if (PTR_ERR(pos) == -EAGAIN) {
118 pr_info("Info: encountered resize\n");
119 chain_len++;
120 continue;
121 } else if (IS_ERR(pos)) {
122 pr_warn("Test failed: rhashtable_walk_next() error: %ld\n",
123 PTR_ERR(pos));
124 break;
9d6dbe1b
GU
125 }
126
246b23a7 127 total++;
9d6dbe1b
GU
128 }
129
246b23a7
TG
130 rhashtable_walk_stop(&hti);
131 rhashtable_walk_exit(&hti);
132
133 pr_info(" Traversal complete: counted=%u, nelems=%u, entries=%d, table-jumps=%u\n",
134 total, atomic_read(&ht->nelems), entries, chain_len);
9d6dbe1b 135
1aa661f5 136 if (total != atomic_read(&ht->nelems) || total != entries)
9d6dbe1b
GU
137 pr_warn("Test failed: Total count mismatch ^^^");
138}
139
1aa661f5 140static s64 __init test_rhashtable(struct rhashtable *ht)
9d6dbe1b 141{
9d6dbe1b 142 struct test_obj *obj;
9d6dbe1b 143 int err;
67b7cbf4 144 unsigned int i, insert_fails = 0;
1aa661f5 145 s64 start, end;
9d6dbe1b
GU
146
147 /*
148 * Insertion Test:
1aa661f5 149 * Insert entries into table with all keys even numbers
9d6dbe1b 150 */
1aa661f5
TG
151 pr_info(" Adding %d keys\n", entries);
152 start = ktime_get_ns();
153 for (i = 0; i < entries; i++) {
fcc57020 154 struct test_obj *obj = &array[i];
9d6dbe1b 155
9d6dbe1b
GU
156 obj->value = i * 2;
157
b182aa6e 158 err = rhashtable_insert_fast(ht, &obj->node, test_rht_params);
67b7cbf4
TG
159 if (err == -ENOMEM || err == -EBUSY) {
160 /* Mark failed inserts but continue */
161 obj->value = TEST_INSERT_FAIL;
162 insert_fails++;
163 } else if (err) {
fcc57020 164 return err;
67b7cbf4 165 }
685a015e
TG
166
167 cond_resched();
9d6dbe1b
GU
168 }
169
67b7cbf4
TG
170 if (insert_fails)
171 pr_info(" %u insertions failed due to memory pressure\n",
172 insert_fails);
173
246b23a7 174 test_bucket_stats(ht);
9d6dbe1b 175 rcu_read_lock();
9d6dbe1b
GU
176 test_rht_lookup(ht);
177 rcu_read_unlock();
178
246b23a7 179 test_bucket_stats(ht);
9d6dbe1b 180
1aa661f5
TG
181 pr_info(" Deleting %d keys\n", entries);
182 for (i = 0; i < entries; i++) {
9d6dbe1b
GU
183 u32 key = i * 2;
184
67b7cbf4
TG
185 if (array[i].value != TEST_INSERT_FAIL) {
186 obj = rhashtable_lookup_fast(ht, &key, test_rht_params);
187 BUG_ON(!obj);
9d6dbe1b 188
67b7cbf4
TG
189 rhashtable_remove_fast(ht, &obj->node, test_rht_params);
190 }
685a015e
TG
191
192 cond_resched();
9d6dbe1b
GU
193 }
194
1aa661f5
TG
195 end = ktime_get_ns();
196 pr_info(" Duration of test: %lld ns\n", end - start);
197
198 return end - start;
9d6dbe1b
GU
199}
200
b7f5e5c7
DB
201static struct rhashtable ht;
202
9d6dbe1b
GU
203static int __init test_rht_init(void)
204{
1aa661f5
TG
205 int i, err;
206 u64 total_time = 0;
9d6dbe1b 207
1aa661f5 208 entries = min(entries, MAX_ENTRIES);
9d6dbe1b 209
1aa661f5
TG
210 test_rht_params.automatic_shrinking = shrinking;
211 test_rht_params.max_size = max_size;
212 test_rht_params.nelem_hint = size;
9d6dbe1b 213
1aa661f5
TG
214 pr_info("Running rhashtable test nelem=%d, max_size=%d, shrinking=%d\n",
215 size, max_size, shrinking);
9d6dbe1b 216
1aa661f5
TG
217 for (i = 0; i < runs; i++) {
218 s64 time;
9d6dbe1b 219
1aa661f5 220 pr_info("Test %02d:\n", i);
fcc57020 221 memset(&array, 0, sizeof(array));
1aa661f5
TG
222 err = rhashtable_init(&ht, &test_rht_params);
223 if (err < 0) {
224 pr_warn("Test failed: Unable to initialize hashtable: %d\n",
225 err);
226 continue;
227 }
228
229 time = test_rhashtable(&ht);
230 rhashtable_destroy(&ht);
231 if (time < 0) {
232 pr_warn("Test failed: return code %lld\n", time);
233 return -EINVAL;
234 }
235
236 total_time += time;
237 }
238
6decd63a
TG
239 do_div(total_time, runs);
240 pr_info("Average test time: %llu\n", total_time);
1aa661f5
TG
241
242 return 0;
9d6dbe1b
GU
243}
244
6dd0c165
DB
245static void __exit test_rht_exit(void)
246{
247}
248
9d6dbe1b 249module_init(test_rht_init);
6dd0c165 250module_exit(test_rht_exit);
9d6dbe1b
GU
251
252MODULE_LICENSE("GPL v2");