]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - lib/test_rhashtable.c
rhashtable-test: Use walker to test bucket statistics
[mirror_ubuntu-artful-kernel.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
fcc57020
TG
52static struct test_obj array[MAX_ENTRIES];
53
1aa661f5 54static struct rhashtable_params test_rht_params = {
b182aa6e
HX
55 .head_offset = offsetof(struct test_obj, node),
56 .key_offset = offsetof(struct test_obj, value),
57 .key_len = sizeof(int),
58 .hashfn = jhash,
b182aa6e
HX
59 .nulls_base = (3U << RHT_BASE_SHIFT),
60};
61
9d6dbe1b
GU
62static int __init test_rht_lookup(struct rhashtable *ht)
63{
64 unsigned int i;
65
1aa661f5 66 for (i = 0; i < entries * 2; i++) {
9d6dbe1b
GU
67 struct test_obj *obj;
68 bool expected = !(i % 2);
69 u32 key = i;
70
b182aa6e 71 obj = rhashtable_lookup_fast(ht, &key, test_rht_params);
9d6dbe1b
GU
72
73 if (expected && !obj) {
74 pr_warn("Test failed: Could not find key %u\n", key);
75 return -ENOENT;
76 } else if (!expected && obj) {
77 pr_warn("Test failed: Unexpected entry found for key %u\n",
78 key);
79 return -EEXIST;
80 } else if (expected && obj) {
c2c8a901
TG
81 if (obj->value != i) {
82 pr_warn("Test failed: Lookup value mismatch %u!=%u\n",
83 obj->value, i);
9d6dbe1b
GU
84 return -EINVAL;
85 }
86 }
87 }
88
89 return 0;
90}
91
246b23a7 92static void test_bucket_stats(struct rhashtable *ht)
9d6dbe1b 93{
246b23a7
TG
94 unsigned int err, total = 0, chain_len = 0;
95 struct rhashtable_iter hti;
9d6dbe1b 96 struct rhash_head *pos;
9d6dbe1b 97
246b23a7
TG
98 err = rhashtable_walk_init(ht, &hti);
99 if (err) {
100 pr_warn("Test failed: allocation error");
101 return;
102 }
9d6dbe1b 103
246b23a7
TG
104 err = rhashtable_walk_start(&hti);
105 if (err && err != -EAGAIN) {
106 pr_warn("Test failed: iterator failed: %d\n", err);
107 return;
108 }
9d6dbe1b 109
246b23a7
TG
110 while ((pos = rhashtable_walk_next(&hti))) {
111 if (PTR_ERR(pos) == -EAGAIN) {
112 pr_info("Info: encountered resize\n");
113 chain_len++;
114 continue;
115 } else if (IS_ERR(pos)) {
116 pr_warn("Test failed: rhashtable_walk_next() error: %ld\n",
117 PTR_ERR(pos));
118 break;
9d6dbe1b
GU
119 }
120
246b23a7 121 total++;
9d6dbe1b
GU
122 }
123
246b23a7
TG
124 rhashtable_walk_stop(&hti);
125 rhashtable_walk_exit(&hti);
126
127 pr_info(" Traversal complete: counted=%u, nelems=%u, entries=%d, table-jumps=%u\n",
128 total, atomic_read(&ht->nelems), entries, chain_len);
9d6dbe1b 129
1aa661f5 130 if (total != atomic_read(&ht->nelems) || total != entries)
9d6dbe1b
GU
131 pr_warn("Test failed: Total count mismatch ^^^");
132}
133
1aa661f5 134static s64 __init test_rhashtable(struct rhashtable *ht)
9d6dbe1b 135{
9d6dbe1b 136 struct test_obj *obj;
9d6dbe1b
GU
137 int err;
138 unsigned int i;
1aa661f5 139 s64 start, end;
9d6dbe1b
GU
140
141 /*
142 * Insertion Test:
1aa661f5 143 * Insert entries into table with all keys even numbers
9d6dbe1b 144 */
1aa661f5
TG
145 pr_info(" Adding %d keys\n", entries);
146 start = ktime_get_ns();
147 for (i = 0; i < entries; i++) {
fcc57020 148 struct test_obj *obj = &array[i];
9d6dbe1b 149
9d6dbe1b
GU
150 obj->value = i * 2;
151
b182aa6e 152 err = rhashtable_insert_fast(ht, &obj->node, test_rht_params);
fcc57020
TG
153 if (err)
154 return err;
9d6dbe1b
GU
155 }
156
246b23a7 157 test_bucket_stats(ht);
9d6dbe1b 158 rcu_read_lock();
9d6dbe1b
GU
159 test_rht_lookup(ht);
160 rcu_read_unlock();
161
246b23a7 162 test_bucket_stats(ht);
9d6dbe1b 163
1aa661f5
TG
164 pr_info(" Deleting %d keys\n", entries);
165 for (i = 0; i < entries; i++) {
9d6dbe1b
GU
166 u32 key = i * 2;
167
b182aa6e 168 obj = rhashtable_lookup_fast(ht, &key, test_rht_params);
9d6dbe1b
GU
169 BUG_ON(!obj);
170
b182aa6e 171 rhashtable_remove_fast(ht, &obj->node, test_rht_params);
9d6dbe1b
GU
172 }
173
1aa661f5
TG
174 end = ktime_get_ns();
175 pr_info(" Duration of test: %lld ns\n", end - start);
176
177 return end - start;
9d6dbe1b
GU
178}
179
b7f5e5c7
DB
180static struct rhashtable ht;
181
9d6dbe1b
GU
182static int __init test_rht_init(void)
183{
1aa661f5
TG
184 int i, err;
185 u64 total_time = 0;
9d6dbe1b 186
1aa661f5 187 entries = min(entries, MAX_ENTRIES);
9d6dbe1b 188
1aa661f5
TG
189 test_rht_params.automatic_shrinking = shrinking;
190 test_rht_params.max_size = max_size;
191 test_rht_params.nelem_hint = size;
9d6dbe1b 192
1aa661f5
TG
193 pr_info("Running rhashtable test nelem=%d, max_size=%d, shrinking=%d\n",
194 size, max_size, shrinking);
9d6dbe1b 195
1aa661f5
TG
196 for (i = 0; i < runs; i++) {
197 s64 time;
9d6dbe1b 198
1aa661f5 199 pr_info("Test %02d:\n", i);
fcc57020 200 memset(&array, 0, sizeof(array));
1aa661f5
TG
201 err = rhashtable_init(&ht, &test_rht_params);
202 if (err < 0) {
203 pr_warn("Test failed: Unable to initialize hashtable: %d\n",
204 err);
205 continue;
206 }
207
208 time = test_rhashtable(&ht);
209 rhashtable_destroy(&ht);
210 if (time < 0) {
211 pr_warn("Test failed: return code %lld\n", time);
212 return -EINVAL;
213 }
214
215 total_time += time;
216 }
217
218 pr_info("Average test time: %llu\n", total_time / runs);
219
220 return 0;
9d6dbe1b
GU
221}
222
6dd0c165
DB
223static void __exit test_rht_exit(void)
224{
225}
226
9d6dbe1b 227module_init(test_rht_init);
6dd0c165 228module_exit(test_rht_exit);
9d6dbe1b
GU
229
230MODULE_LICENSE("GPL v2");