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