test_rhashtable: don't allocate huge static array
[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>
f4a3e90b 19#include <linux/kthread.h>
9d6dbe1b
GU
20#include <linux/module.h>
21#include <linux/rcupdate.h>
22#include <linux/rhashtable.h>
f4a3e90b 23#include <linux/semaphore.h>
9d6dbe1b 24#include <linux/slab.h>
685a015e 25#include <linux/sched.h>
f4a3e90b 26#include <linux/vmalloc.h>
9d6dbe1b 27
1aa661f5 28#define MAX_ENTRIES 1000000
67b7cbf4 29#define TEST_INSERT_FAIL INT_MAX
1aa661f5
TG
30
31static int entries = 50000;
32module_param(entries, int, 0);
33MODULE_PARM_DESC(entries, "Number of entries to add (default: 50000)");
34
35static int runs = 4;
36module_param(runs, int, 0);
37MODULE_PARM_DESC(runs, "Number of test runs per variant (default: 4)");
38
95e435af 39static int max_size = 0;
1aa661f5 40module_param(max_size, int, 0);
3b3bf80b 41MODULE_PARM_DESC(max_size, "Maximum table size (default: calculated)");
1aa661f5
TG
42
43static bool shrinking = false;
44module_param(shrinking, bool, 0);
45MODULE_PARM_DESC(shrinking, "Enable automatic shrinking (default: off)");
46
47static int size = 8;
48module_param(size, int, 0);
49MODULE_PARM_DESC(size, "Initial size hint of table (default: 8)");
9d6dbe1b 50
f4a3e90b
PS
51static int tcount = 10;
52module_param(tcount, int, 0);
53MODULE_PARM_DESC(tcount, "Number of threads to spawn (default: 10)");
54
d662e037
PS
55static bool enomem_retry = false;
56module_param(enomem_retry, bool, 0);
57MODULE_PARM_DESC(enomem_retry, "Retry insert even if -ENOMEM was returned (default: off)");
58
e859afe1
PS
59struct test_obj_val {
60 int id;
61 int tid;
62};
63
9d6dbe1b 64struct test_obj {
e859afe1 65 struct test_obj_val value;
9d6dbe1b
GU
66 struct rhash_head node;
67};
68
f4a3e90b
PS
69struct thread_data {
70 int id;
71 struct task_struct *task;
72 struct test_obj *objs;
73};
74
1aa661f5 75static struct rhashtable_params test_rht_params = {
b182aa6e
HX
76 .head_offset = offsetof(struct test_obj, node),
77 .key_offset = offsetof(struct test_obj, value),
e859afe1 78 .key_len = sizeof(struct test_obj_val),
b182aa6e 79 .hashfn = jhash,
b182aa6e
HX
80 .nulls_base = (3U << RHT_BASE_SHIFT),
81};
82
f4a3e90b
PS
83static struct semaphore prestart_sem;
84static struct semaphore startup_sem = __SEMAPHORE_INITIALIZER(startup_sem, 0);
85
7e936bd7 86static int insert_retry(struct rhashtable *ht, struct test_obj *obj,
9e9089e5
PS
87 const struct rhashtable_params params)
88{
d662e037 89 int err, retries = -1, enomem_retries = 0;
9e9089e5
PS
90
91 do {
92 retries++;
93 cond_resched();
7e936bd7 94 err = rhashtable_insert_fast(ht, &obj->node, params);
d662e037
PS
95 if (err == -ENOMEM && enomem_retry) {
96 enomem_retries++;
97 err = -EBUSY;
98 }
9e9089e5
PS
99 } while (err == -EBUSY);
100
d662e037
PS
101 if (enomem_retries)
102 pr_info(" %u insertions retried after -ENOMEM\n",
103 enomem_retries);
104
9e9089e5
PS
105 return err ? : retries;
106}
107
7e936bd7 108static int __init test_rht_lookup(struct rhashtable *ht, struct test_obj *array)
9d6dbe1b
GU
109{
110 unsigned int i;
111
1aa661f5 112 for (i = 0; i < entries * 2; i++) {
9d6dbe1b
GU
113 struct test_obj *obj;
114 bool expected = !(i % 2);
e859afe1
PS
115 struct test_obj_val key = {
116 .id = i,
117 };
9d6dbe1b 118
e859afe1 119 if (array[i / 2].value.id == TEST_INSERT_FAIL)
67b7cbf4
TG
120 expected = false;
121
b182aa6e 122 obj = rhashtable_lookup_fast(ht, &key, test_rht_params);
9d6dbe1b
GU
123
124 if (expected && !obj) {
e859afe1 125 pr_warn("Test failed: Could not find key %u\n", key.id);
9d6dbe1b
GU
126 return -ENOENT;
127 } else if (!expected && obj) {
128 pr_warn("Test failed: Unexpected entry found for key %u\n",
e859afe1 129 key.id);
9d6dbe1b
GU
130 return -EEXIST;
131 } else if (expected && obj) {
e859afe1 132 if (obj->value.id != i) {
c2c8a901 133 pr_warn("Test failed: Lookup value mismatch %u!=%u\n",
e859afe1 134 obj->value.id, i);
9d6dbe1b
GU
135 return -EINVAL;
136 }
137 }
685a015e
TG
138
139 cond_resched_rcu();
9d6dbe1b
GU
140 }
141
142 return 0;
143}
144
246b23a7 145static void test_bucket_stats(struct rhashtable *ht)
9d6dbe1b 146{
246b23a7
TG
147 unsigned int err, total = 0, chain_len = 0;
148 struct rhashtable_iter hti;
9d6dbe1b 149 struct rhash_head *pos;
9d6dbe1b 150
8f6fd83c 151 err = rhashtable_walk_init(ht, &hti, GFP_KERNEL);
246b23a7
TG
152 if (err) {
153 pr_warn("Test failed: allocation error");
154 return;
155 }
9d6dbe1b 156
246b23a7
TG
157 err = rhashtable_walk_start(&hti);
158 if (err && err != -EAGAIN) {
159 pr_warn("Test failed: iterator failed: %d\n", err);
160 return;
161 }
9d6dbe1b 162
246b23a7
TG
163 while ((pos = rhashtable_walk_next(&hti))) {
164 if (PTR_ERR(pos) == -EAGAIN) {
165 pr_info("Info: encountered resize\n");
166 chain_len++;
167 continue;
168 } else if (IS_ERR(pos)) {
169 pr_warn("Test failed: rhashtable_walk_next() error: %ld\n",
170 PTR_ERR(pos));
171 break;
9d6dbe1b
GU
172 }
173
246b23a7 174 total++;
9d6dbe1b
GU
175 }
176
246b23a7
TG
177 rhashtable_walk_stop(&hti);
178 rhashtable_walk_exit(&hti);
179
180 pr_info(" Traversal complete: counted=%u, nelems=%u, entries=%d, table-jumps=%u\n",
181 total, atomic_read(&ht->nelems), entries, chain_len);
9d6dbe1b 182
1aa661f5 183 if (total != atomic_read(&ht->nelems) || total != entries)
9d6dbe1b
GU
184 pr_warn("Test failed: Total count mismatch ^^^");
185}
186
7e936bd7 187static s64 __init test_rhashtable(struct rhashtable *ht, struct test_obj *array)
9d6dbe1b 188{
9d6dbe1b 189 struct test_obj *obj;
9d6dbe1b 190 int err;
9e9089e5 191 unsigned int i, insert_retries = 0;
1aa661f5 192 s64 start, end;
9d6dbe1b
GU
193
194 /*
195 * Insertion Test:
1aa661f5 196 * Insert entries into table with all keys even numbers
9d6dbe1b 197 */
1aa661f5
TG
198 pr_info(" Adding %d keys\n", entries);
199 start = ktime_get_ns();
200 for (i = 0; i < entries; i++) {
fcc57020 201 struct test_obj *obj = &array[i];
9d6dbe1b 202
e859afe1 203 obj->value.id = i * 2;
7e936bd7 204 err = insert_retry(ht, obj, test_rht_params);
9e9089e5
PS
205 if (err > 0)
206 insert_retries += err;
207 else if (err)
fcc57020 208 return err;
9d6dbe1b
GU
209 }
210
9e9089e5
PS
211 if (insert_retries)
212 pr_info(" %u insertions retried due to memory pressure\n",
213 insert_retries);
67b7cbf4 214
246b23a7 215 test_bucket_stats(ht);
9d6dbe1b 216 rcu_read_lock();
7e936bd7 217 test_rht_lookup(ht, array);
9d6dbe1b
GU
218 rcu_read_unlock();
219
246b23a7 220 test_bucket_stats(ht);
9d6dbe1b 221
1aa661f5
TG
222 pr_info(" Deleting %d keys\n", entries);
223 for (i = 0; i < entries; i++) {
78369255
PS
224 struct test_obj_val key = {
225 .id = i * 2,
226 };
9d6dbe1b 227
e859afe1 228 if (array[i].value.id != TEST_INSERT_FAIL) {
67b7cbf4
TG
229 obj = rhashtable_lookup_fast(ht, &key, test_rht_params);
230 BUG_ON(!obj);
9d6dbe1b 231
67b7cbf4
TG
232 rhashtable_remove_fast(ht, &obj->node, test_rht_params);
233 }
685a015e
TG
234
235 cond_resched();
9d6dbe1b
GU
236 }
237
1aa661f5
TG
238 end = ktime_get_ns();
239 pr_info(" Duration of test: %lld ns\n", end - start);
240
241 return end - start;
9d6dbe1b
GU
242}
243
b7f5e5c7
DB
244static struct rhashtable ht;
245
f4a3e90b
PS
246static int thread_lookup_test(struct thread_data *tdata)
247{
248 int i, err = 0;
249
250 for (i = 0; i < entries; i++) {
251 struct test_obj *obj;
e859afe1
PS
252 struct test_obj_val key = {
253 .id = i,
254 .tid = tdata->id,
255 };
f4a3e90b
PS
256
257 obj = rhashtable_lookup_fast(&ht, &key, test_rht_params);
e859afe1
PS
258 if (obj && (tdata->objs[i].value.id == TEST_INSERT_FAIL)) {
259 pr_err(" found unexpected object %d-%d\n", key.tid, key.id);
f4a3e90b 260 err++;
e859afe1
PS
261 } else if (!obj && (tdata->objs[i].value.id != TEST_INSERT_FAIL)) {
262 pr_err(" object %d-%d not found!\n", key.tid, key.id);
f4a3e90b 263 err++;
e859afe1
PS
264 } else if (obj && memcmp(&obj->value, &key, sizeof(key))) {
265 pr_err(" wrong object returned (got %d-%d, expected %d-%d)\n",
266 obj->value.tid, obj->value.id, key.tid, key.id);
f4a3e90b
PS
267 err++;
268 }
cd5b318d
PS
269
270 cond_resched();
f4a3e90b
PS
271 }
272 return err;
273}
274
275static int threadfunc(void *data)
276{
9e9089e5 277 int i, step, err = 0, insert_retries = 0;
f4a3e90b
PS
278 struct thread_data *tdata = data;
279
280 up(&prestart_sem);
281 if (down_interruptible(&startup_sem))
282 pr_err(" thread[%d]: down_interruptible failed\n", tdata->id);
283
284 for (i = 0; i < entries; i++) {
e859afe1
PS
285 tdata->objs[i].value.id = i;
286 tdata->objs[i].value.tid = tdata->id;
7e936bd7 287 err = insert_retry(&ht, &tdata->objs[i], test_rht_params);
9e9089e5
PS
288 if (err > 0) {
289 insert_retries += err;
f4a3e90b
PS
290 } else if (err) {
291 pr_err(" thread[%d]: rhashtable_insert_fast failed\n",
292 tdata->id);
293 goto out;
294 }
295 }
9e9089e5
PS
296 if (insert_retries)
297 pr_info(" thread[%d]: %u insertions retried due to memory pressure\n",
298 tdata->id, insert_retries);
f4a3e90b
PS
299
300 err = thread_lookup_test(tdata);
301 if (err) {
302 pr_err(" thread[%d]: rhashtable_lookup_test failed\n",
303 tdata->id);
304 goto out;
305 }
306
307 for (step = 10; step > 0; step--) {
308 for (i = 0; i < entries; i += step) {
e859afe1 309 if (tdata->objs[i].value.id == TEST_INSERT_FAIL)
f4a3e90b
PS
310 continue;
311 err = rhashtable_remove_fast(&ht, &tdata->objs[i].node,
312 test_rht_params);
313 if (err) {
314 pr_err(" thread[%d]: rhashtable_remove_fast failed\n",
315 tdata->id);
316 goto out;
317 }
e859afe1 318 tdata->objs[i].value.id = TEST_INSERT_FAIL;
cd5b318d
PS
319
320 cond_resched();
f4a3e90b
PS
321 }
322 err = thread_lookup_test(tdata);
323 if (err) {
324 pr_err(" thread[%d]: rhashtable_lookup_test (2) failed\n",
325 tdata->id);
326 goto out;
327 }
328 }
329out:
330 while (!kthread_should_stop()) {
331 set_current_state(TASK_INTERRUPTIBLE);
332 schedule();
333 }
334 return err;
335}
336
9d6dbe1b
GU
337static int __init test_rht_init(void)
338{
f4a3e90b 339 int i, err, started_threads = 0, failed_threads = 0;
1aa661f5 340 u64 total_time = 0;
f4a3e90b
PS
341 struct thread_data *tdata;
342 struct test_obj *objs;
9d6dbe1b 343
1aa661f5 344 entries = min(entries, MAX_ENTRIES);
9d6dbe1b 345
1aa661f5 346 test_rht_params.automatic_shrinking = shrinking;
95e435af 347 test_rht_params.max_size = max_size ? : roundup_pow_of_two(entries);
1aa661f5 348 test_rht_params.nelem_hint = size;
9d6dbe1b 349
7e936bd7
FW
350 objs = vzalloc((test_rht_params.max_size + 1) * sizeof(struct test_obj));
351 if (!objs)
352 return -ENOMEM;
353
1aa661f5
TG
354 pr_info("Running rhashtable test nelem=%d, max_size=%d, shrinking=%d\n",
355 size, max_size, shrinking);
9d6dbe1b 356
1aa661f5
TG
357 for (i = 0; i < runs; i++) {
358 s64 time;
9d6dbe1b 359
1aa661f5 360 pr_info("Test %02d:\n", i);
7e936bd7
FW
361 memset(objs, 0, test_rht_params.max_size * sizeof(struct test_obj));
362
1aa661f5
TG
363 err = rhashtable_init(&ht, &test_rht_params);
364 if (err < 0) {
365 pr_warn("Test failed: Unable to initialize hashtable: %d\n",
366 err);
367 continue;
368 }
369
7e936bd7 370 time = test_rhashtable(&ht, objs);
1aa661f5
TG
371 rhashtable_destroy(&ht);
372 if (time < 0) {
7e936bd7 373 vfree(objs);
1aa661f5
TG
374 pr_warn("Test failed: return code %lld\n", time);
375 return -EINVAL;
376 }
377
378 total_time += time;
379 }
380
7e936bd7 381 vfree(objs);
6decd63a
TG
382 do_div(total_time, runs);
383 pr_info("Average test time: %llu\n", total_time);
1aa661f5 384
f4a3e90b
PS
385 if (!tcount)
386 return 0;
387
388 pr_info("Testing concurrent rhashtable access from %d threads\n",
389 tcount);
390 sema_init(&prestart_sem, 1 - tcount);
391 tdata = vzalloc(tcount * sizeof(struct thread_data));
392 if (!tdata)
393 return -ENOMEM;
394 objs = vzalloc(tcount * entries * sizeof(struct test_obj));
395 if (!objs) {
396 vfree(tdata);
397 return -ENOMEM;
398 }
399
95e435af
PS
400 test_rht_params.max_size = max_size ? :
401 roundup_pow_of_two(tcount * entries);
f4a3e90b
PS
402 err = rhashtable_init(&ht, &test_rht_params);
403 if (err < 0) {
404 pr_warn("Test failed: Unable to initialize hashtable: %d\n",
405 err);
406 vfree(tdata);
407 vfree(objs);
408 return -EINVAL;
409 }
410 for (i = 0; i < tcount; i++) {
411 tdata[i].id = i;
412 tdata[i].objs = objs + i * entries;
413 tdata[i].task = kthread_run(threadfunc, &tdata[i],
414 "rhashtable_thrad[%d]", i);
415 if (IS_ERR(tdata[i].task))
416 pr_err(" kthread_run failed for thread %d\n", i);
417 else
418 started_threads++;
419 }
420 if (down_interruptible(&prestart_sem))
421 pr_err(" down interruptible failed\n");
422 for (i = 0; i < tcount; i++)
423 up(&startup_sem);
424 for (i = 0; i < tcount; i++) {
425 if (IS_ERR(tdata[i].task))
426 continue;
427 if ((err = kthread_stop(tdata[i].task))) {
428 pr_warn("Test failed: thread %d returned: %d\n",
429 i, err);
430 failed_threads++;
431 }
432 }
433 pr_info("Started %d threads, %d failed\n",
434 started_threads, failed_threads);
435 rhashtable_destroy(&ht);
436 vfree(tdata);
437 vfree(objs);
1aa661f5 438 return 0;
9d6dbe1b
GU
439}
440
6dd0c165
DB
441static void __exit test_rht_exit(void)
442{
443}
444
9d6dbe1b 445module_init(test_rht_init);
6dd0c165 446module_exit(test_rht_exit);
9d6dbe1b
GU
447
448MODULE_LICENSE("GPL v2");