2 * Resizable, Scalable, Concurrent Hash Table
4 * Copyright (c) 2014 Thomas Graf <tgraf@suug.ch>
5 * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net>
7 * Based on the following paper:
8 * https://www.usenix.org/legacy/event/atc11/tech/final_files/Triplett.pdf
10 * Code partially derived from nft_hash
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
17 /**************************************************************************
19 **************************************************************************/
21 #include <linux/init.h>
22 #include <linux/jhash.h>
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/rcupdate.h>
26 #include <linux/rhashtable.h>
27 #include <linux/slab.h>
30 #define TEST_HT_SIZE 8
31 #define TEST_ENTRIES 2048
32 #define TEST_PTR ((void *) 0xdeadbeef)
33 #define TEST_NEXPANDS 4
38 struct rhash_head node
;
41 static int __init
test_rht_lookup(struct rhashtable
*ht
)
45 for (i
= 0; i
< TEST_ENTRIES
* 2; i
++) {
47 bool expected
= !(i
% 2);
50 obj
= rhashtable_lookup(ht
, &key
);
52 if (expected
&& !obj
) {
53 pr_warn("Test failed: Could not find key %u\n", key
);
55 } else if (!expected
&& obj
) {
56 pr_warn("Test failed: Unexpected entry found for key %u\n",
59 } else if (expected
&& obj
) {
60 if (obj
->ptr
!= TEST_PTR
|| obj
->value
!= i
) {
61 pr_warn("Test failed: Lookup value mismatch %p!=%p, %u!=%u\n",
62 obj
->ptr
, TEST_PTR
, obj
->value
, i
);
71 static void test_bucket_stats(struct rhashtable
*ht
, bool quiet
)
73 unsigned int cnt
, rcu_cnt
, i
, total
= 0;
74 struct rhash_head
*pos
;
76 struct bucket_table
*tbl
;
78 tbl
= rht_dereference_rcu(ht
->tbl
, ht
);
79 for (i
= 0; i
< tbl
->size
; i
++) {
83 pr_info(" [%#4x/%zu]", i
, tbl
->size
);
85 rht_for_each_entry_rcu(obj
, pos
, tbl
, i
, node
) {
89 pr_cont(" [%p],", obj
);
92 rht_for_each_entry_rcu(obj
, pos
, tbl
, i
, node
)
96 pr_warn("Test failed: Chain count mismach %d != %d",
100 pr_cont("\n [%#x] first element: %p, chain length: %u\n",
101 i
, tbl
->buckets
[i
], cnt
);
104 pr_info(" Traversal complete: counted=%u, nelems=%u, entries=%d\n",
105 total
, atomic_read(&ht
->nelems
), TEST_ENTRIES
);
107 if (total
!= atomic_read(&ht
->nelems
) || total
!= TEST_ENTRIES
)
108 pr_warn("Test failed: Total count mismatch ^^^");
111 static int __init
test_rhashtable(struct rhashtable
*ht
)
113 struct bucket_table
*tbl
;
114 struct test_obj
*obj
;
115 struct rhash_head
*pos
, *next
;
121 * Insert TEST_ENTRIES into table with all keys even numbers
123 pr_info(" Adding %d keys\n", TEST_ENTRIES
);
124 for (i
= 0; i
< TEST_ENTRIES
; i
++) {
125 struct test_obj
*obj
;
127 obj
= kzalloc(sizeof(*obj
), GFP_KERNEL
);
136 rhashtable_insert(ht
, &obj
->node
);
140 test_bucket_stats(ht
, true);
144 for (i
= 0; i
< TEST_NEXPANDS
; i
++) {
145 pr_info(" Table expansion iteration %u...\n", i
);
146 mutex_lock(&ht
->mutex
);
147 rhashtable_expand(ht
);
148 mutex_unlock(&ht
->mutex
);
151 pr_info(" Verifying lookups...\n");
156 for (i
= 0; i
< TEST_NEXPANDS
; i
++) {
157 pr_info(" Table shrinkage iteration %u...\n", i
);
158 mutex_lock(&ht
->mutex
);
159 rhashtable_shrink(ht
);
160 mutex_unlock(&ht
->mutex
);
163 pr_info(" Verifying lookups...\n");
169 test_bucket_stats(ht
, true);
172 pr_info(" Deleting %d keys\n", TEST_ENTRIES
);
173 for (i
= 0; i
< TEST_ENTRIES
; i
++) {
176 obj
= rhashtable_lookup(ht
, &key
);
179 rhashtable_remove(ht
, &obj
->node
);
186 tbl
= rht_dereference_rcu(ht
->tbl
, ht
);
187 for (i
= 0; i
< tbl
->size
; i
++)
188 rht_for_each_entry_safe(obj
, pos
, next
, tbl
, i
, node
)
194 static struct rhashtable ht
;
196 static int __init
test_rht_init(void)
198 struct rhashtable_params params
= {
199 .nelem_hint
= TEST_HT_SIZE
,
200 .head_offset
= offsetof(struct test_obj
, node
),
201 .key_offset
= offsetof(struct test_obj
, value
),
202 .key_len
= sizeof(int),
204 .max_shift
= 1, /* we expand/shrink manually here */
205 .nulls_base
= (3U << RHT_BASE_SHIFT
),
209 pr_info("Running resizable hashtable tests...\n");
211 err
= rhashtable_init(&ht
, ¶ms
);
213 pr_warn("Test failed: Unable to initialize hashtable: %d\n",
218 err
= test_rhashtable(&ht
);
220 rhashtable_destroy(&ht
);
225 static void __exit
test_rht_exit(void)
229 module_init(test_rht_init
);
230 module_exit(test_rht_exit
);
232 MODULE_LICENSE("GPL v2");