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/kthread.h>
20 #include <linux/module.h>
21 #include <linux/rcupdate.h>
22 #include <linux/rhashtable.h>
23 #include <linux/semaphore.h>
24 #include <linux/slab.h>
25 #include <linux/sched.h>
26 #include <linux/vmalloc.h>
28 #define MAX_ENTRIES 1000000
29 #define TEST_INSERT_FAIL INT_MAX
31 static int entries
= 50000;
32 module_param(entries
, int, 0);
33 MODULE_PARM_DESC(entries
, "Number of entries to add (default: 50000)");
36 module_param(runs
, int, 0);
37 MODULE_PARM_DESC(runs
, "Number of test runs per variant (default: 4)");
39 static int max_size
= 65536;
40 module_param(max_size
, int, 0);
41 MODULE_PARM_DESC(runs
, "Maximum table size (default: 65536)");
43 static bool shrinking
= false;
44 module_param(shrinking
, bool, 0);
45 MODULE_PARM_DESC(shrinking
, "Enable automatic shrinking (default: off)");
48 module_param(size
, int, 0);
49 MODULE_PARM_DESC(size
, "Initial size hint of table (default: 8)");
51 static int tcount
= 10;
52 module_param(tcount
, int, 0);
53 MODULE_PARM_DESC(tcount
, "Number of threads to spawn (default: 10)");
57 struct rhash_head node
;
62 struct task_struct
*task
;
63 struct test_obj
*objs
;
66 static struct test_obj array
[MAX_ENTRIES
];
68 static struct rhashtable_params test_rht_params
= {
69 .head_offset
= offsetof(struct test_obj
, node
),
70 .key_offset
= offsetof(struct test_obj
, value
),
71 .key_len
= sizeof(int),
73 .nulls_base
= (3U << RHT_BASE_SHIFT
),
76 static struct semaphore prestart_sem
;
77 static struct semaphore startup_sem
= __SEMAPHORE_INITIALIZER(startup_sem
, 0);
79 static int __init
test_rht_lookup(struct rhashtable
*ht
)
83 for (i
= 0; i
< entries
* 2; i
++) {
85 bool expected
= !(i
% 2);
88 if (array
[i
/ 2].value
== TEST_INSERT_FAIL
)
91 obj
= rhashtable_lookup_fast(ht
, &key
, test_rht_params
);
93 if (expected
&& !obj
) {
94 pr_warn("Test failed: Could not find key %u\n", key
);
96 } else if (!expected
&& obj
) {
97 pr_warn("Test failed: Unexpected entry found for key %u\n",
100 } else if (expected
&& obj
) {
101 if (obj
->value
!= i
) {
102 pr_warn("Test failed: Lookup value mismatch %u!=%u\n",
114 static void test_bucket_stats(struct rhashtable
*ht
)
116 unsigned int err
, total
= 0, chain_len
= 0;
117 struct rhashtable_iter hti
;
118 struct rhash_head
*pos
;
120 err
= rhashtable_walk_init(ht
, &hti
);
122 pr_warn("Test failed: allocation error");
126 err
= rhashtable_walk_start(&hti
);
127 if (err
&& err
!= -EAGAIN
) {
128 pr_warn("Test failed: iterator failed: %d\n", err
);
132 while ((pos
= rhashtable_walk_next(&hti
))) {
133 if (PTR_ERR(pos
) == -EAGAIN
) {
134 pr_info("Info: encountered resize\n");
137 } else if (IS_ERR(pos
)) {
138 pr_warn("Test failed: rhashtable_walk_next() error: %ld\n",
146 rhashtable_walk_stop(&hti
);
147 rhashtable_walk_exit(&hti
);
149 pr_info(" Traversal complete: counted=%u, nelems=%u, entries=%d, table-jumps=%u\n",
150 total
, atomic_read(&ht
->nelems
), entries
, chain_len
);
152 if (total
!= atomic_read(&ht
->nelems
) || total
!= entries
)
153 pr_warn("Test failed: Total count mismatch ^^^");
156 static s64 __init
test_rhashtable(struct rhashtable
*ht
)
158 struct test_obj
*obj
;
160 unsigned int i
, insert_fails
= 0;
165 * Insert entries into table with all keys even numbers
167 pr_info(" Adding %d keys\n", entries
);
168 start
= ktime_get_ns();
169 for (i
= 0; i
< entries
; i
++) {
170 struct test_obj
*obj
= &array
[i
];
174 err
= rhashtable_insert_fast(ht
, &obj
->node
, test_rht_params
);
175 if (err
== -ENOMEM
|| err
== -EBUSY
) {
176 /* Mark failed inserts but continue */
177 obj
->value
= TEST_INSERT_FAIL
;
187 pr_info(" %u insertions failed due to memory pressure\n",
190 test_bucket_stats(ht
);
195 test_bucket_stats(ht
);
197 pr_info(" Deleting %d keys\n", entries
);
198 for (i
= 0; i
< entries
; i
++) {
201 if (array
[i
].value
!= TEST_INSERT_FAIL
) {
202 obj
= rhashtable_lookup_fast(ht
, &key
, test_rht_params
);
205 rhashtable_remove_fast(ht
, &obj
->node
, test_rht_params
);
211 end
= ktime_get_ns();
212 pr_info(" Duration of test: %lld ns\n", end
- start
);
217 static struct rhashtable ht
;
219 static int thread_lookup_test(struct thread_data
*tdata
)
223 for (i
= 0; i
< entries
; i
++) {
224 struct test_obj
*obj
;
225 int key
= (tdata
->id
<< 16) | i
;
227 obj
= rhashtable_lookup_fast(&ht
, &key
, test_rht_params
);
228 if (obj
&& (tdata
->objs
[i
].value
== TEST_INSERT_FAIL
)) {
229 pr_err(" found unexpected object %d\n", key
);
231 } else if (!obj
&& (tdata
->objs
[i
].value
!= TEST_INSERT_FAIL
)) {
232 pr_err(" object %d not found!\n", key
);
234 } else if (obj
&& (obj
->value
!= key
)) {
235 pr_err(" wrong object returned (got %d, expected %d)\n",
243 static int threadfunc(void *data
)
245 int i
, step
, err
= 0, insert_fails
= 0;
246 struct thread_data
*tdata
= data
;
249 if (down_interruptible(&startup_sem
))
250 pr_err(" thread[%d]: down_interruptible failed\n", tdata
->id
);
252 for (i
= 0; i
< entries
; i
++) {
253 tdata
->objs
[i
].value
= (tdata
->id
<< 16) | i
;
254 err
= rhashtable_insert_fast(&ht
, &tdata
->objs
[i
].node
,
256 if (err
== -ENOMEM
|| err
== -EBUSY
) {
257 tdata
->objs
[i
].value
= TEST_INSERT_FAIL
;
260 pr_err(" thread[%d]: rhashtable_insert_fast failed\n",
266 pr_info(" thread[%d]: %d insert failures\n",
267 tdata
->id
, insert_fails
);
269 err
= thread_lookup_test(tdata
);
271 pr_err(" thread[%d]: rhashtable_lookup_test failed\n",
276 for (step
= 10; step
> 0; step
--) {
277 for (i
= 0; i
< entries
; i
+= step
) {
278 if (tdata
->objs
[i
].value
== TEST_INSERT_FAIL
)
280 err
= rhashtable_remove_fast(&ht
, &tdata
->objs
[i
].node
,
283 pr_err(" thread[%d]: rhashtable_remove_fast failed\n",
287 tdata
->objs
[i
].value
= TEST_INSERT_FAIL
;
289 err
= thread_lookup_test(tdata
);
291 pr_err(" thread[%d]: rhashtable_lookup_test (2) failed\n",
297 while (!kthread_should_stop()) {
298 set_current_state(TASK_INTERRUPTIBLE
);
304 static int __init
test_rht_init(void)
306 int i
, err
, started_threads
= 0, failed_threads
= 0;
308 struct thread_data
*tdata
;
309 struct test_obj
*objs
;
311 entries
= min(entries
, MAX_ENTRIES
);
313 test_rht_params
.automatic_shrinking
= shrinking
;
314 test_rht_params
.max_size
= max_size
;
315 test_rht_params
.nelem_hint
= size
;
317 pr_info("Running rhashtable test nelem=%d, max_size=%d, shrinking=%d\n",
318 size
, max_size
, shrinking
);
320 for (i
= 0; i
< runs
; i
++) {
323 pr_info("Test %02d:\n", i
);
324 memset(&array
, 0, sizeof(array
));
325 err
= rhashtable_init(&ht
, &test_rht_params
);
327 pr_warn("Test failed: Unable to initialize hashtable: %d\n",
332 time
= test_rhashtable(&ht
);
333 rhashtable_destroy(&ht
);
335 pr_warn("Test failed: return code %lld\n", time
);
342 do_div(total_time
, runs
);
343 pr_info("Average test time: %llu\n", total_time
);
348 pr_info("Testing concurrent rhashtable access from %d threads\n",
350 sema_init(&prestart_sem
, 1 - tcount
);
351 tdata
= vzalloc(tcount
* sizeof(struct thread_data
));
354 objs
= vzalloc(tcount
* entries
* sizeof(struct test_obj
));
360 err
= rhashtable_init(&ht
, &test_rht_params
);
362 pr_warn("Test failed: Unable to initialize hashtable: %d\n",
368 for (i
= 0; i
< tcount
; i
++) {
370 tdata
[i
].objs
= objs
+ i
* entries
;
371 tdata
[i
].task
= kthread_run(threadfunc
, &tdata
[i
],
372 "rhashtable_thrad[%d]", i
);
373 if (IS_ERR(tdata
[i
].task
))
374 pr_err(" kthread_run failed for thread %d\n", i
);
378 if (down_interruptible(&prestart_sem
))
379 pr_err(" down interruptible failed\n");
380 for (i
= 0; i
< tcount
; i
++)
382 for (i
= 0; i
< tcount
; i
++) {
383 if (IS_ERR(tdata
[i
].task
))
385 if ((err
= kthread_stop(tdata
[i
].task
))) {
386 pr_warn("Test failed: thread %d returned: %d\n",
391 pr_info("Started %d threads, %d failed\n",
392 started_threads
, failed_threads
);
393 rhashtable_destroy(&ht
);
399 static void __exit
test_rht_exit(void)
403 module_init(test_rht_init
);
404 module_exit(test_rht_exit
);
406 MODULE_LICENSE("GPL v2");