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
= 0;
40 module_param(max_size
, int, 0);
41 MODULE_PARM_DESC(max_size
, "Maximum table size (default: calculated)");
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)");
55 static bool enomem_retry
= false;
56 module_param(enomem_retry
, bool, 0);
57 MODULE_PARM_DESC(enomem_retry
, "Retry insert even if -ENOMEM was returned (default: off)");
65 struct test_obj_val value
;
66 struct rhash_head node
;
71 struct task_struct
*task
;
72 struct test_obj
*objs
;
75 static struct test_obj array
[MAX_ENTRIES
];
77 static struct rhashtable_params test_rht_params
= {
78 .head_offset
= offsetof(struct test_obj
, node
),
79 .key_offset
= offsetof(struct test_obj
, value
),
80 .key_len
= sizeof(struct test_obj_val
),
82 .nulls_base
= (3U << RHT_BASE_SHIFT
),
85 static struct semaphore prestart_sem
;
86 static struct semaphore startup_sem
= __SEMAPHORE_INITIALIZER(startup_sem
, 0);
88 static int insert_retry(struct rhashtable
*ht
, struct rhash_head
*obj
,
89 const struct rhashtable_params params
)
91 int err
, retries
= -1, enomem_retries
= 0;
96 err
= rhashtable_insert_fast(ht
, obj
, params
);
97 if (err
== -ENOMEM
&& enomem_retry
) {
101 } while (err
== -EBUSY
);
104 pr_info(" %u insertions retried after -ENOMEM\n",
107 return err
? : retries
;
110 static int __init
test_rht_lookup(struct rhashtable
*ht
)
114 for (i
= 0; i
< entries
* 2; i
++) {
115 struct test_obj
*obj
;
116 bool expected
= !(i
% 2);
117 struct test_obj_val key
= {
121 if (array
[i
/ 2].value
.id
== TEST_INSERT_FAIL
)
124 obj
= rhashtable_lookup_fast(ht
, &key
, test_rht_params
);
126 if (expected
&& !obj
) {
127 pr_warn("Test failed: Could not find key %u\n", key
.id
);
129 } else if (!expected
&& obj
) {
130 pr_warn("Test failed: Unexpected entry found for key %u\n",
133 } else if (expected
&& obj
) {
134 if (obj
->value
.id
!= i
) {
135 pr_warn("Test failed: Lookup value mismatch %u!=%u\n",
147 static void test_bucket_stats(struct rhashtable
*ht
)
149 unsigned int err
, total
= 0, chain_len
= 0;
150 struct rhashtable_iter hti
;
151 struct rhash_head
*pos
;
153 err
= rhashtable_walk_init(ht
, &hti
, GFP_KERNEL
);
155 pr_warn("Test failed: allocation error");
159 err
= rhashtable_walk_start(&hti
);
160 if (err
&& err
!= -EAGAIN
) {
161 pr_warn("Test failed: iterator failed: %d\n", err
);
165 while ((pos
= rhashtable_walk_next(&hti
))) {
166 if (PTR_ERR(pos
) == -EAGAIN
) {
167 pr_info("Info: encountered resize\n");
170 } else if (IS_ERR(pos
)) {
171 pr_warn("Test failed: rhashtable_walk_next() error: %ld\n",
179 rhashtable_walk_stop(&hti
);
180 rhashtable_walk_exit(&hti
);
182 pr_info(" Traversal complete: counted=%u, nelems=%u, entries=%d, table-jumps=%u\n",
183 total
, atomic_read(&ht
->nelems
), entries
, chain_len
);
185 if (total
!= atomic_read(&ht
->nelems
) || total
!= entries
)
186 pr_warn("Test failed: Total count mismatch ^^^");
189 static s64 __init
test_rhashtable(struct rhashtable
*ht
)
191 struct test_obj
*obj
;
193 unsigned int i
, insert_retries
= 0;
198 * Insert entries into table with all keys even numbers
200 pr_info(" Adding %d keys\n", entries
);
201 start
= ktime_get_ns();
202 for (i
= 0; i
< entries
; i
++) {
203 struct test_obj
*obj
= &array
[i
];
205 obj
->value
.id
= i
* 2;
206 err
= insert_retry(ht
, &obj
->node
, test_rht_params
);
208 insert_retries
+= err
;
214 pr_info(" %u insertions retried due to memory pressure\n",
217 test_bucket_stats(ht
);
222 test_bucket_stats(ht
);
224 pr_info(" Deleting %d keys\n", entries
);
225 for (i
= 0; i
< entries
; i
++) {
226 struct test_obj_val key
= {
230 if (array
[i
].value
.id
!= TEST_INSERT_FAIL
) {
231 obj
= rhashtable_lookup_fast(ht
, &key
, test_rht_params
);
234 rhashtable_remove_fast(ht
, &obj
->node
, test_rht_params
);
240 end
= ktime_get_ns();
241 pr_info(" Duration of test: %lld ns\n", end
- start
);
246 static struct rhashtable ht
;
248 static int thread_lookup_test(struct thread_data
*tdata
)
252 for (i
= 0; i
< entries
; i
++) {
253 struct test_obj
*obj
;
254 struct test_obj_val key
= {
259 obj
= rhashtable_lookup_fast(&ht
, &key
, test_rht_params
);
260 if (obj
&& (tdata
->objs
[i
].value
.id
== TEST_INSERT_FAIL
)) {
261 pr_err(" found unexpected object %d-%d\n", key
.tid
, key
.id
);
263 } else if (!obj
&& (tdata
->objs
[i
].value
.id
!= TEST_INSERT_FAIL
)) {
264 pr_err(" object %d-%d not found!\n", key
.tid
, key
.id
);
266 } else if (obj
&& memcmp(&obj
->value
, &key
, sizeof(key
))) {
267 pr_err(" wrong object returned (got %d-%d, expected %d-%d)\n",
268 obj
->value
.tid
, obj
->value
.id
, key
.tid
, key
.id
);
277 static int threadfunc(void *data
)
279 int i
, step
, err
= 0, insert_retries
= 0;
280 struct thread_data
*tdata
= data
;
283 if (down_interruptible(&startup_sem
))
284 pr_err(" thread[%d]: down_interruptible failed\n", tdata
->id
);
286 for (i
= 0; i
< entries
; i
++) {
287 tdata
->objs
[i
].value
.id
= i
;
288 tdata
->objs
[i
].value
.tid
= tdata
->id
;
289 err
= insert_retry(&ht
, &tdata
->objs
[i
].node
, test_rht_params
);
291 insert_retries
+= err
;
293 pr_err(" thread[%d]: rhashtable_insert_fast failed\n",
299 pr_info(" thread[%d]: %u insertions retried due to memory pressure\n",
300 tdata
->id
, insert_retries
);
302 err
= thread_lookup_test(tdata
);
304 pr_err(" thread[%d]: rhashtable_lookup_test failed\n",
309 for (step
= 10; step
> 0; step
--) {
310 for (i
= 0; i
< entries
; i
+= step
) {
311 if (tdata
->objs
[i
].value
.id
== TEST_INSERT_FAIL
)
313 err
= rhashtable_remove_fast(&ht
, &tdata
->objs
[i
].node
,
316 pr_err(" thread[%d]: rhashtable_remove_fast failed\n",
320 tdata
->objs
[i
].value
.id
= TEST_INSERT_FAIL
;
324 err
= thread_lookup_test(tdata
);
326 pr_err(" thread[%d]: rhashtable_lookup_test (2) failed\n",
332 while (!kthread_should_stop()) {
333 set_current_state(TASK_INTERRUPTIBLE
);
339 static int __init
test_rht_init(void)
341 int i
, err
, started_threads
= 0, failed_threads
= 0;
343 struct thread_data
*tdata
;
344 struct test_obj
*objs
;
346 entries
= min(entries
, MAX_ENTRIES
);
348 test_rht_params
.automatic_shrinking
= shrinking
;
349 test_rht_params
.max_size
= max_size
? : roundup_pow_of_two(entries
);
350 test_rht_params
.nelem_hint
= size
;
352 pr_info("Running rhashtable test nelem=%d, max_size=%d, shrinking=%d\n",
353 size
, max_size
, shrinking
);
355 for (i
= 0; i
< runs
; i
++) {
358 pr_info("Test %02d:\n", i
);
359 memset(&array
, 0, sizeof(array
));
360 err
= rhashtable_init(&ht
, &test_rht_params
);
362 pr_warn("Test failed: Unable to initialize hashtable: %d\n",
367 time
= test_rhashtable(&ht
);
368 rhashtable_destroy(&ht
);
370 pr_warn("Test failed: return code %lld\n", time
);
377 do_div(total_time
, runs
);
378 pr_info("Average test time: %llu\n", total_time
);
383 pr_info("Testing concurrent rhashtable access from %d threads\n",
385 sema_init(&prestart_sem
, 1 - tcount
);
386 tdata
= vzalloc(tcount
* sizeof(struct thread_data
));
389 objs
= vzalloc(tcount
* entries
* sizeof(struct test_obj
));
395 test_rht_params
.max_size
= max_size
? :
396 roundup_pow_of_two(tcount
* entries
);
397 err
= rhashtable_init(&ht
, &test_rht_params
);
399 pr_warn("Test failed: Unable to initialize hashtable: %d\n",
405 for (i
= 0; i
< tcount
; i
++) {
407 tdata
[i
].objs
= objs
+ i
* entries
;
408 tdata
[i
].task
= kthread_run(threadfunc
, &tdata
[i
],
409 "rhashtable_thrad[%d]", i
);
410 if (IS_ERR(tdata
[i
].task
))
411 pr_err(" kthread_run failed for thread %d\n", i
);
415 if (down_interruptible(&prestart_sem
))
416 pr_err(" down interruptible failed\n");
417 for (i
= 0; i
< tcount
; i
++)
419 for (i
= 0; i
< tcount
; i
++) {
420 if (IS_ERR(tdata
[i
].task
))
422 if ((err
= kthread_stop(tdata
[i
].task
))) {
423 pr_warn("Test failed: thread %d returned: %d\n",
428 pr_info("Started %d threads, %d failed\n",
429 started_threads
, failed_threads
);
430 rhashtable_destroy(&ht
);
436 static void __exit
test_rht_exit(void)
440 module_init(test_rht_init
);
441 module_exit(test_rht_exit
);
443 MODULE_LICENSE("GPL v2");