rhashtable-test: Get rid of ptr in test_obj structure
[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>
19#include <linux/module.h>
20#include <linux/rcupdate.h>
21#include <linux/rhashtable.h>
22#include <linux/slab.h>
23
24
1aa661f5
TG
25#define MAX_ENTRIES 1000000
26
27static int entries = 50000;
28module_param(entries, int, 0);
29MODULE_PARM_DESC(entries, "Number of entries to add (default: 50000)");
30
31static int runs = 4;
32module_param(runs, int, 0);
33MODULE_PARM_DESC(runs, "Number of test runs per variant (default: 4)");
34
35static int max_size = 65536;
36module_param(max_size, int, 0);
37MODULE_PARM_DESC(runs, "Maximum table size (default: 65536)");
38
39static bool shrinking = false;
40module_param(shrinking, bool, 0);
41MODULE_PARM_DESC(shrinking, "Enable automatic shrinking (default: off)");
42
43static int size = 8;
44module_param(size, int, 0);
45MODULE_PARM_DESC(size, "Initial size hint of table (default: 8)");
9d6dbe1b
GU
46
47struct test_obj {
9d6dbe1b
GU
48 int value;
49 struct rhash_head node;
50};
51
1aa661f5 52static struct rhashtable_params test_rht_params = {
b182aa6e
HX
53 .head_offset = offsetof(struct test_obj, node),
54 .key_offset = offsetof(struct test_obj, value),
55 .key_len = sizeof(int),
56 .hashfn = jhash,
b182aa6e
HX
57 .nulls_base = (3U << RHT_BASE_SHIFT),
58};
59
9d6dbe1b
GU
60static int __init test_rht_lookup(struct rhashtable *ht)
61{
62 unsigned int i;
63
1aa661f5 64 for (i = 0; i < entries * 2; i++) {
9d6dbe1b
GU
65 struct test_obj *obj;
66 bool expected = !(i % 2);
67 u32 key = i;
68
b182aa6e 69 obj = rhashtable_lookup_fast(ht, &key, test_rht_params);
9d6dbe1b
GU
70
71 if (expected && !obj) {
72 pr_warn("Test failed: Could not find key %u\n", key);
73 return -ENOENT;
74 } else if (!expected && obj) {
75 pr_warn("Test failed: Unexpected entry found for key %u\n",
76 key);
77 return -EEXIST;
78 } else if (expected && obj) {
c2c8a901
TG
79 if (obj->value != i) {
80 pr_warn("Test failed: Lookup value mismatch %u!=%u\n",
81 obj->value, i);
9d6dbe1b
GU
82 return -EINVAL;
83 }
84 }
85 }
86
87 return 0;
88}
89
90static void test_bucket_stats(struct rhashtable *ht, bool quiet)
91{
92 unsigned int cnt, rcu_cnt, i, total = 0;
93 struct rhash_head *pos;
94 struct test_obj *obj;
95 struct bucket_table *tbl;
96
97 tbl = rht_dereference_rcu(ht->tbl, ht);
98 for (i = 0; i < tbl->size; i++) {
99 rcu_cnt = cnt = 0;
100
101 if (!quiet)
63d512d0 102 pr_info(" [%#4x/%u]", i, tbl->size);
9d6dbe1b
GU
103
104 rht_for_each_entry_rcu(obj, pos, tbl, i, node) {
105 cnt++;
106 total++;
107 if (!quiet)
108 pr_cont(" [%p],", obj);
109 }
110
111 rht_for_each_entry_rcu(obj, pos, tbl, i, node)
112 rcu_cnt++;
113
114 if (rcu_cnt != cnt)
115 pr_warn("Test failed: Chain count mismach %d != %d",
116 cnt, rcu_cnt);
117
118 if (!quiet)
119 pr_cont("\n [%#x] first element: %p, chain length: %u\n",
120 i, tbl->buckets[i], cnt);
121 }
122
123 pr_info(" Traversal complete: counted=%u, nelems=%u, entries=%d\n",
1aa661f5 124 total, atomic_read(&ht->nelems), entries);
9d6dbe1b 125
1aa661f5 126 if (total != atomic_read(&ht->nelems) || total != entries)
9d6dbe1b
GU
127 pr_warn("Test failed: Total count mismatch ^^^");
128}
129
1aa661f5 130static s64 __init test_rhashtable(struct rhashtable *ht)
9d6dbe1b
GU
131{
132 struct bucket_table *tbl;
133 struct test_obj *obj;
134 struct rhash_head *pos, *next;
135 int err;
136 unsigned int i;
1aa661f5 137 s64 start, end;
9d6dbe1b
GU
138
139 /*
140 * Insertion Test:
1aa661f5 141 * Insert entries into table with all keys even numbers
9d6dbe1b 142 */
1aa661f5
TG
143 pr_info(" Adding %d keys\n", entries);
144 start = ktime_get_ns();
145 for (i = 0; i < entries; i++) {
9d6dbe1b
GU
146 struct test_obj *obj;
147
148 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
149 if (!obj) {
150 err = -ENOMEM;
151 goto error;
152 }
153
9d6dbe1b
GU
154 obj->value = i * 2;
155
b182aa6e
HX
156 err = rhashtable_insert_fast(ht, &obj->node, test_rht_params);
157 if (err) {
158 kfree(obj);
159 goto error;
160 }
9d6dbe1b
GU
161 }
162
163 rcu_read_lock();
164 test_bucket_stats(ht, true);
165 test_rht_lookup(ht);
166 rcu_read_unlock();
167
9d6dbe1b
GU
168 rcu_read_lock();
169 test_bucket_stats(ht, true);
170 rcu_read_unlock();
171
1aa661f5
TG
172 pr_info(" Deleting %d keys\n", entries);
173 for (i = 0; i < entries; i++) {
9d6dbe1b
GU
174 u32 key = i * 2;
175
b182aa6e 176 obj = rhashtable_lookup_fast(ht, &key, test_rht_params);
9d6dbe1b
GU
177 BUG_ON(!obj);
178
b182aa6e 179 rhashtable_remove_fast(ht, &obj->node, test_rht_params);
9d6dbe1b
GU
180 kfree(obj);
181 }
182
1aa661f5
TG
183 end = ktime_get_ns();
184 pr_info(" Duration of test: %lld ns\n", end - start);
185
186 return end - start;
9d6dbe1b
GU
187
188error:
189 tbl = rht_dereference_rcu(ht->tbl, ht);
190 for (i = 0; i < tbl->size; i++)
191 rht_for_each_entry_safe(obj, pos, next, tbl, i, node)
192 kfree(obj);
193
194 return err;
195}
196
b7f5e5c7
DB
197static struct rhashtable ht;
198
9d6dbe1b
GU
199static int __init test_rht_init(void)
200{
1aa661f5
TG
201 int i, err;
202 u64 total_time = 0;
9d6dbe1b 203
1aa661f5 204 entries = min(entries, MAX_ENTRIES);
9d6dbe1b 205
1aa661f5
TG
206 test_rht_params.automatic_shrinking = shrinking;
207 test_rht_params.max_size = max_size;
208 test_rht_params.nelem_hint = size;
9d6dbe1b 209
1aa661f5
TG
210 pr_info("Running rhashtable test nelem=%d, max_size=%d, shrinking=%d\n",
211 size, max_size, shrinking);
9d6dbe1b 212
1aa661f5
TG
213 for (i = 0; i < runs; i++) {
214 s64 time;
9d6dbe1b 215
1aa661f5
TG
216 pr_info("Test %02d:\n", i);
217 err = rhashtable_init(&ht, &test_rht_params);
218 if (err < 0) {
219 pr_warn("Test failed: Unable to initialize hashtable: %d\n",
220 err);
221 continue;
222 }
223
224 time = test_rhashtable(&ht);
225 rhashtable_destroy(&ht);
226 if (time < 0) {
227 pr_warn("Test failed: return code %lld\n", time);
228 return -EINVAL;
229 }
230
231 total_time += time;
232 }
233
234 pr_info("Average test time: %llu\n", total_time / runs);
235
236 return 0;
9d6dbe1b
GU
237}
238
6dd0c165
DB
239static void __exit test_rht_exit(void)
240{
241}
242
9d6dbe1b 243module_init(test_rht_init);
6dd0c165 244module_exit(test_rht_exit);
9d6dbe1b
GU
245
246MODULE_LICENSE("GPL v2");