Geert Uytterhoeven | 9d6dbe1 | 2015-01-29 15:40:25 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Resizable, Scalable, Concurrent Hash Table |
| 3 | * |
Thomas Graf | 1aa661f | 2015-04-30 22:37:41 +0000 | [diff] [blame] | 4 | * Copyright (c) 2014-2015 Thomas Graf <tgraf@suug.ch> |
Geert Uytterhoeven | 9d6dbe1 | 2015-01-29 15:40:25 +0100 | [diff] [blame] | 5 | * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net> |
| 6 | * |
Geert Uytterhoeven | 9d6dbe1 | 2015-01-29 15:40:25 +0100 | [diff] [blame] | 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> |
Phil Sutter | f4a3e90 | 2015-08-15 00:37:15 +0200 | [diff] [blame] | 19 | #include <linux/kthread.h> |
Geert Uytterhoeven | 9d6dbe1 | 2015-01-29 15:40:25 +0100 | [diff] [blame] | 20 | #include <linux/module.h> |
| 21 | #include <linux/rcupdate.h> |
| 22 | #include <linux/rhashtable.h> |
Phil Sutter | f4a3e90 | 2015-08-15 00:37:15 +0200 | [diff] [blame] | 23 | #include <linux/semaphore.h> |
Geert Uytterhoeven | 9d6dbe1 | 2015-01-29 15:40:25 +0100 | [diff] [blame] | 24 | #include <linux/slab.h> |
Thomas Graf | 685a015 | 2015-07-17 10:52:48 +0200 | [diff] [blame] | 25 | #include <linux/sched.h> |
Phil Sutter | f4a3e90 | 2015-08-15 00:37:15 +0200 | [diff] [blame] | 26 | #include <linux/vmalloc.h> |
Geert Uytterhoeven | 9d6dbe1 | 2015-01-29 15:40:25 +0100 | [diff] [blame] | 27 | |
Thomas Graf | 1aa661f | 2015-04-30 22:37:41 +0000 | [diff] [blame] | 28 | #define MAX_ENTRIES 1000000 |
Thomas Graf | 67b7cbf | 2015-04-30 22:37:45 +0000 | [diff] [blame] | 29 | #define TEST_INSERT_FAIL INT_MAX |
Thomas Graf | 1aa661f | 2015-04-30 22:37:41 +0000 | [diff] [blame] | 30 | |
| 31 | static int entries = 50000; |
| 32 | module_param(entries, int, 0); |
| 33 | MODULE_PARM_DESC(entries, "Number of entries to add (default: 50000)"); |
| 34 | |
| 35 | static int runs = 4; |
| 36 | module_param(runs, int, 0); |
| 37 | MODULE_PARM_DESC(runs, "Number of test runs per variant (default: 4)"); |
| 38 | |
| 39 | static int max_size = 65536; |
| 40 | module_param(max_size, int, 0); |
| 41 | MODULE_PARM_DESC(runs, "Maximum table size (default: 65536)"); |
| 42 | |
| 43 | static bool shrinking = false; |
| 44 | module_param(shrinking, bool, 0); |
| 45 | MODULE_PARM_DESC(shrinking, "Enable automatic shrinking (default: off)"); |
| 46 | |
| 47 | static int size = 8; |
| 48 | module_param(size, int, 0); |
| 49 | MODULE_PARM_DESC(size, "Initial size hint of table (default: 8)"); |
Geert Uytterhoeven | 9d6dbe1 | 2015-01-29 15:40:25 +0100 | [diff] [blame] | 50 | |
Phil Sutter | f4a3e90 | 2015-08-15 00:37:15 +0200 | [diff] [blame] | 51 | static int tcount = 10; |
| 52 | module_param(tcount, int, 0); |
| 53 | MODULE_PARM_DESC(tcount, "Number of threads to spawn (default: 10)"); |
| 54 | |
Geert Uytterhoeven | 9d6dbe1 | 2015-01-29 15:40:25 +0100 | [diff] [blame] | 55 | struct test_obj { |
Geert Uytterhoeven | 9d6dbe1 | 2015-01-29 15:40:25 +0100 | [diff] [blame] | 56 | int value; |
| 57 | struct rhash_head node; |
| 58 | }; |
| 59 | |
Phil Sutter | f4a3e90 | 2015-08-15 00:37:15 +0200 | [diff] [blame] | 60 | struct thread_data { |
| 61 | int id; |
| 62 | struct task_struct *task; |
| 63 | struct test_obj *objs; |
| 64 | }; |
| 65 | |
Thomas Graf | fcc5702 | 2015-04-30 22:37:43 +0000 | [diff] [blame] | 66 | static struct test_obj array[MAX_ENTRIES]; |
| 67 | |
Thomas Graf | 1aa661f | 2015-04-30 22:37:41 +0000 | [diff] [blame] | 68 | static struct rhashtable_params test_rht_params = { |
Herbert Xu | b182aa6 | 2015-03-20 21:57:04 +1100 | [diff] [blame] | 69 | .head_offset = offsetof(struct test_obj, node), |
| 70 | .key_offset = offsetof(struct test_obj, value), |
| 71 | .key_len = sizeof(int), |
| 72 | .hashfn = jhash, |
Herbert Xu | b182aa6 | 2015-03-20 21:57:04 +1100 | [diff] [blame] | 73 | .nulls_base = (3U << RHT_BASE_SHIFT), |
| 74 | }; |
| 75 | |
Phil Sutter | f4a3e90 | 2015-08-15 00:37:15 +0200 | [diff] [blame] | 76 | static struct semaphore prestart_sem; |
| 77 | static struct semaphore startup_sem = __SEMAPHORE_INITIALIZER(startup_sem, 0); |
| 78 | |
Geert Uytterhoeven | 9d6dbe1 | 2015-01-29 15:40:25 +0100 | [diff] [blame] | 79 | static int __init test_rht_lookup(struct rhashtable *ht) |
| 80 | { |
| 81 | unsigned int i; |
| 82 | |
Thomas Graf | 1aa661f | 2015-04-30 22:37:41 +0000 | [diff] [blame] | 83 | for (i = 0; i < entries * 2; i++) { |
Geert Uytterhoeven | 9d6dbe1 | 2015-01-29 15:40:25 +0100 | [diff] [blame] | 84 | struct test_obj *obj; |
| 85 | bool expected = !(i % 2); |
| 86 | u32 key = i; |
| 87 | |
Thomas Graf | 67b7cbf | 2015-04-30 22:37:45 +0000 | [diff] [blame] | 88 | if (array[i / 2].value == TEST_INSERT_FAIL) |
| 89 | expected = false; |
| 90 | |
Herbert Xu | b182aa6 | 2015-03-20 21:57:04 +1100 | [diff] [blame] | 91 | obj = rhashtable_lookup_fast(ht, &key, test_rht_params); |
Geert Uytterhoeven | 9d6dbe1 | 2015-01-29 15:40:25 +0100 | [diff] [blame] | 92 | |
| 93 | if (expected && !obj) { |
| 94 | pr_warn("Test failed: Could not find key %u\n", key); |
| 95 | return -ENOENT; |
| 96 | } else if (!expected && obj) { |
| 97 | pr_warn("Test failed: Unexpected entry found for key %u\n", |
| 98 | key); |
| 99 | return -EEXIST; |
| 100 | } else if (expected && obj) { |
Thomas Graf | c2c8a90 | 2015-04-30 22:37:42 +0000 | [diff] [blame] | 101 | if (obj->value != i) { |
| 102 | pr_warn("Test failed: Lookup value mismatch %u!=%u\n", |
| 103 | obj->value, i); |
Geert Uytterhoeven | 9d6dbe1 | 2015-01-29 15:40:25 +0100 | [diff] [blame] | 104 | return -EINVAL; |
| 105 | } |
| 106 | } |
Thomas Graf | 685a015 | 2015-07-17 10:52:48 +0200 | [diff] [blame] | 107 | |
| 108 | cond_resched_rcu(); |
Geert Uytterhoeven | 9d6dbe1 | 2015-01-29 15:40:25 +0100 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | |
Thomas Graf | 246b23a | 2015-04-30 22:37:44 +0000 | [diff] [blame] | 114 | static void test_bucket_stats(struct rhashtable *ht) |
Geert Uytterhoeven | 9d6dbe1 | 2015-01-29 15:40:25 +0100 | [diff] [blame] | 115 | { |
Thomas Graf | 246b23a | 2015-04-30 22:37:44 +0000 | [diff] [blame] | 116 | unsigned int err, total = 0, chain_len = 0; |
| 117 | struct rhashtable_iter hti; |
Geert Uytterhoeven | 9d6dbe1 | 2015-01-29 15:40:25 +0100 | [diff] [blame] | 118 | struct rhash_head *pos; |
Geert Uytterhoeven | 9d6dbe1 | 2015-01-29 15:40:25 +0100 | [diff] [blame] | 119 | |
Thomas Graf | 246b23a | 2015-04-30 22:37:44 +0000 | [diff] [blame] | 120 | err = rhashtable_walk_init(ht, &hti); |
| 121 | if (err) { |
| 122 | pr_warn("Test failed: allocation error"); |
| 123 | return; |
Geert Uytterhoeven | 9d6dbe1 | 2015-01-29 15:40:25 +0100 | [diff] [blame] | 124 | } |
| 125 | |
Thomas Graf | 246b23a | 2015-04-30 22:37:44 +0000 | [diff] [blame] | 126 | err = rhashtable_walk_start(&hti); |
| 127 | if (err && err != -EAGAIN) { |
| 128 | pr_warn("Test failed: iterator failed: %d\n", err); |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | while ((pos = rhashtable_walk_next(&hti))) { |
| 133 | if (PTR_ERR(pos) == -EAGAIN) { |
| 134 | pr_info("Info: encountered resize\n"); |
| 135 | chain_len++; |
| 136 | continue; |
| 137 | } else if (IS_ERR(pos)) { |
| 138 | pr_warn("Test failed: rhashtable_walk_next() error: %ld\n", |
| 139 | PTR_ERR(pos)); |
| 140 | break; |
| 141 | } |
| 142 | |
| 143 | total++; |
| 144 | } |
| 145 | |
| 146 | rhashtable_walk_stop(&hti); |
| 147 | rhashtable_walk_exit(&hti); |
| 148 | |
| 149 | pr_info(" Traversal complete: counted=%u, nelems=%u, entries=%d, table-jumps=%u\n", |
| 150 | total, atomic_read(&ht->nelems), entries, chain_len); |
Geert Uytterhoeven | 9d6dbe1 | 2015-01-29 15:40:25 +0100 | [diff] [blame] | 151 | |
Thomas Graf | 1aa661f | 2015-04-30 22:37:41 +0000 | [diff] [blame] | 152 | if (total != atomic_read(&ht->nelems) || total != entries) |
Geert Uytterhoeven | 9d6dbe1 | 2015-01-29 15:40:25 +0100 | [diff] [blame] | 153 | pr_warn("Test failed: Total count mismatch ^^^"); |
| 154 | } |
| 155 | |
Thomas Graf | 1aa661f | 2015-04-30 22:37:41 +0000 | [diff] [blame] | 156 | static s64 __init test_rhashtable(struct rhashtable *ht) |
Geert Uytterhoeven | 9d6dbe1 | 2015-01-29 15:40:25 +0100 | [diff] [blame] | 157 | { |
Geert Uytterhoeven | 9d6dbe1 | 2015-01-29 15:40:25 +0100 | [diff] [blame] | 158 | struct test_obj *obj; |
Geert Uytterhoeven | 9d6dbe1 | 2015-01-29 15:40:25 +0100 | [diff] [blame] | 159 | int err; |
Thomas Graf | 67b7cbf | 2015-04-30 22:37:45 +0000 | [diff] [blame] | 160 | unsigned int i, insert_fails = 0; |
Thomas Graf | 1aa661f | 2015-04-30 22:37:41 +0000 | [diff] [blame] | 161 | s64 start, end; |
Geert Uytterhoeven | 9d6dbe1 | 2015-01-29 15:40:25 +0100 | [diff] [blame] | 162 | |
| 163 | /* |
| 164 | * Insertion Test: |
Thomas Graf | 1aa661f | 2015-04-30 22:37:41 +0000 | [diff] [blame] | 165 | * Insert entries into table with all keys even numbers |
Geert Uytterhoeven | 9d6dbe1 | 2015-01-29 15:40:25 +0100 | [diff] [blame] | 166 | */ |
Thomas Graf | 1aa661f | 2015-04-30 22:37:41 +0000 | [diff] [blame] | 167 | pr_info(" Adding %d keys\n", entries); |
| 168 | start = ktime_get_ns(); |
| 169 | for (i = 0; i < entries; i++) { |
Thomas Graf | fcc5702 | 2015-04-30 22:37:43 +0000 | [diff] [blame] | 170 | struct test_obj *obj = &array[i]; |
Geert Uytterhoeven | 9d6dbe1 | 2015-01-29 15:40:25 +0100 | [diff] [blame] | 171 | |
Geert Uytterhoeven | 9d6dbe1 | 2015-01-29 15:40:25 +0100 | [diff] [blame] | 172 | obj->value = i * 2; |
| 173 | |
Herbert Xu | b182aa6 | 2015-03-20 21:57:04 +1100 | [diff] [blame] | 174 | err = rhashtable_insert_fast(ht, &obj->node, test_rht_params); |
Thomas Graf | 67b7cbf | 2015-04-30 22:37:45 +0000 | [diff] [blame] | 175 | if (err == -ENOMEM || err == -EBUSY) { |
| 176 | /* Mark failed inserts but continue */ |
| 177 | obj->value = TEST_INSERT_FAIL; |
| 178 | insert_fails++; |
| 179 | } else if (err) { |
Thomas Graf | fcc5702 | 2015-04-30 22:37:43 +0000 | [diff] [blame] | 180 | return err; |
Thomas Graf | 67b7cbf | 2015-04-30 22:37:45 +0000 | [diff] [blame] | 181 | } |
Thomas Graf | 685a015 | 2015-07-17 10:52:48 +0200 | [diff] [blame] | 182 | |
| 183 | cond_resched(); |
Geert Uytterhoeven | 9d6dbe1 | 2015-01-29 15:40:25 +0100 | [diff] [blame] | 184 | } |
| 185 | |
Thomas Graf | 67b7cbf | 2015-04-30 22:37:45 +0000 | [diff] [blame] | 186 | if (insert_fails) |
| 187 | pr_info(" %u insertions failed due to memory pressure\n", |
| 188 | insert_fails); |
| 189 | |
Thomas Graf | 246b23a | 2015-04-30 22:37:44 +0000 | [diff] [blame] | 190 | test_bucket_stats(ht); |
Geert Uytterhoeven | 9d6dbe1 | 2015-01-29 15:40:25 +0100 | [diff] [blame] | 191 | rcu_read_lock(); |
Geert Uytterhoeven | 9d6dbe1 | 2015-01-29 15:40:25 +0100 | [diff] [blame] | 192 | test_rht_lookup(ht); |
| 193 | rcu_read_unlock(); |
| 194 | |
Thomas Graf | 246b23a | 2015-04-30 22:37:44 +0000 | [diff] [blame] | 195 | test_bucket_stats(ht); |
Geert Uytterhoeven | 9d6dbe1 | 2015-01-29 15:40:25 +0100 | [diff] [blame] | 196 | |
Thomas Graf | 1aa661f | 2015-04-30 22:37:41 +0000 | [diff] [blame] | 197 | pr_info(" Deleting %d keys\n", entries); |
| 198 | for (i = 0; i < entries; i++) { |
Geert Uytterhoeven | 9d6dbe1 | 2015-01-29 15:40:25 +0100 | [diff] [blame] | 199 | u32 key = i * 2; |
| 200 | |
Thomas Graf | 67b7cbf | 2015-04-30 22:37:45 +0000 | [diff] [blame] | 201 | if (array[i].value != TEST_INSERT_FAIL) { |
| 202 | obj = rhashtable_lookup_fast(ht, &key, test_rht_params); |
| 203 | BUG_ON(!obj); |
Geert Uytterhoeven | 9d6dbe1 | 2015-01-29 15:40:25 +0100 | [diff] [blame] | 204 | |
Thomas Graf | 67b7cbf | 2015-04-30 22:37:45 +0000 | [diff] [blame] | 205 | rhashtable_remove_fast(ht, &obj->node, test_rht_params); |
| 206 | } |
Thomas Graf | 685a015 | 2015-07-17 10:52:48 +0200 | [diff] [blame] | 207 | |
| 208 | cond_resched(); |
Geert Uytterhoeven | 9d6dbe1 | 2015-01-29 15:40:25 +0100 | [diff] [blame] | 209 | } |
| 210 | |
Thomas Graf | 1aa661f | 2015-04-30 22:37:41 +0000 | [diff] [blame] | 211 | end = ktime_get_ns(); |
| 212 | pr_info(" Duration of test: %lld ns\n", end - start); |
| 213 | |
| 214 | return end - start; |
Geert Uytterhoeven | 9d6dbe1 | 2015-01-29 15:40:25 +0100 | [diff] [blame] | 215 | } |
| 216 | |
Daniel Borkmann | b7f5e5c | 2015-02-20 21:14:21 +0100 | [diff] [blame] | 217 | static struct rhashtable ht; |
| 218 | |
Phil Sutter | f4a3e90 | 2015-08-15 00:37:15 +0200 | [diff] [blame] | 219 | static int thread_lookup_test(struct thread_data *tdata) |
| 220 | { |
| 221 | int i, err = 0; |
| 222 | |
| 223 | for (i = 0; i < entries; i++) { |
| 224 | struct test_obj *obj; |
| 225 | int key = (tdata->id << 16) | i; |
| 226 | |
| 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); |
| 230 | err++; |
| 231 | } else if (!obj && (tdata->objs[i].value != TEST_INSERT_FAIL)) { |
| 232 | pr_err(" object %d not found!\n", key); |
| 233 | err++; |
| 234 | } else if (obj && (obj->value != key)) { |
| 235 | pr_err(" wrong object returned (got %d, expected %d)\n", |
| 236 | obj->value, key); |
| 237 | err++; |
| 238 | } |
Phil Sutter | cd5b318 | 2015-11-20 18:17:17 +0100 | [diff] [blame^] | 239 | |
| 240 | cond_resched(); |
Phil Sutter | f4a3e90 | 2015-08-15 00:37:15 +0200 | [diff] [blame] | 241 | } |
| 242 | return err; |
| 243 | } |
| 244 | |
| 245 | static int threadfunc(void *data) |
| 246 | { |
| 247 | int i, step, err = 0, insert_fails = 0; |
| 248 | struct thread_data *tdata = data; |
| 249 | |
| 250 | up(&prestart_sem); |
| 251 | if (down_interruptible(&startup_sem)) |
| 252 | pr_err(" thread[%d]: down_interruptible failed\n", tdata->id); |
| 253 | |
| 254 | for (i = 0; i < entries; i++) { |
| 255 | tdata->objs[i].value = (tdata->id << 16) | i; |
Phil Sutter | cd5b318 | 2015-11-20 18:17:17 +0100 | [diff] [blame^] | 256 | cond_resched(); |
Phil Sutter | f4a3e90 | 2015-08-15 00:37:15 +0200 | [diff] [blame] | 257 | err = rhashtable_insert_fast(&ht, &tdata->objs[i].node, |
| 258 | test_rht_params); |
| 259 | if (err == -ENOMEM || err == -EBUSY) { |
| 260 | tdata->objs[i].value = TEST_INSERT_FAIL; |
| 261 | insert_fails++; |
| 262 | } else if (err) { |
| 263 | pr_err(" thread[%d]: rhashtable_insert_fast failed\n", |
| 264 | tdata->id); |
| 265 | goto out; |
| 266 | } |
| 267 | } |
| 268 | if (insert_fails) |
| 269 | pr_info(" thread[%d]: %d insert failures\n", |
| 270 | tdata->id, insert_fails); |
| 271 | |
| 272 | err = thread_lookup_test(tdata); |
| 273 | if (err) { |
| 274 | pr_err(" thread[%d]: rhashtable_lookup_test failed\n", |
| 275 | tdata->id); |
| 276 | goto out; |
| 277 | } |
| 278 | |
| 279 | for (step = 10; step > 0; step--) { |
| 280 | for (i = 0; i < entries; i += step) { |
| 281 | if (tdata->objs[i].value == TEST_INSERT_FAIL) |
| 282 | continue; |
| 283 | err = rhashtable_remove_fast(&ht, &tdata->objs[i].node, |
| 284 | test_rht_params); |
| 285 | if (err) { |
| 286 | pr_err(" thread[%d]: rhashtable_remove_fast failed\n", |
| 287 | tdata->id); |
| 288 | goto out; |
| 289 | } |
| 290 | tdata->objs[i].value = TEST_INSERT_FAIL; |
Phil Sutter | cd5b318 | 2015-11-20 18:17:17 +0100 | [diff] [blame^] | 291 | |
| 292 | cond_resched(); |
Phil Sutter | f4a3e90 | 2015-08-15 00:37:15 +0200 | [diff] [blame] | 293 | } |
| 294 | err = thread_lookup_test(tdata); |
| 295 | if (err) { |
| 296 | pr_err(" thread[%d]: rhashtable_lookup_test (2) failed\n", |
| 297 | tdata->id); |
| 298 | goto out; |
| 299 | } |
| 300 | } |
| 301 | out: |
| 302 | while (!kthread_should_stop()) { |
| 303 | set_current_state(TASK_INTERRUPTIBLE); |
| 304 | schedule(); |
| 305 | } |
| 306 | return err; |
| 307 | } |
| 308 | |
Geert Uytterhoeven | 9d6dbe1 | 2015-01-29 15:40:25 +0100 | [diff] [blame] | 309 | static int __init test_rht_init(void) |
| 310 | { |
Phil Sutter | f4a3e90 | 2015-08-15 00:37:15 +0200 | [diff] [blame] | 311 | int i, err, started_threads = 0, failed_threads = 0; |
Thomas Graf | 1aa661f | 2015-04-30 22:37:41 +0000 | [diff] [blame] | 312 | u64 total_time = 0; |
Phil Sutter | f4a3e90 | 2015-08-15 00:37:15 +0200 | [diff] [blame] | 313 | struct thread_data *tdata; |
| 314 | struct test_obj *objs; |
Geert Uytterhoeven | 9d6dbe1 | 2015-01-29 15:40:25 +0100 | [diff] [blame] | 315 | |
Thomas Graf | 1aa661f | 2015-04-30 22:37:41 +0000 | [diff] [blame] | 316 | entries = min(entries, MAX_ENTRIES); |
Geert Uytterhoeven | 9d6dbe1 | 2015-01-29 15:40:25 +0100 | [diff] [blame] | 317 | |
Thomas Graf | 1aa661f | 2015-04-30 22:37:41 +0000 | [diff] [blame] | 318 | test_rht_params.automatic_shrinking = shrinking; |
| 319 | test_rht_params.max_size = max_size; |
| 320 | test_rht_params.nelem_hint = size; |
| 321 | |
| 322 | pr_info("Running rhashtable test nelem=%d, max_size=%d, shrinking=%d\n", |
| 323 | size, max_size, shrinking); |
| 324 | |
| 325 | for (i = 0; i < runs; i++) { |
| 326 | s64 time; |
| 327 | |
| 328 | pr_info("Test %02d:\n", i); |
Thomas Graf | fcc5702 | 2015-04-30 22:37:43 +0000 | [diff] [blame] | 329 | memset(&array, 0, sizeof(array)); |
Thomas Graf | 1aa661f | 2015-04-30 22:37:41 +0000 | [diff] [blame] | 330 | err = rhashtable_init(&ht, &test_rht_params); |
| 331 | if (err < 0) { |
| 332 | pr_warn("Test failed: Unable to initialize hashtable: %d\n", |
| 333 | err); |
| 334 | continue; |
| 335 | } |
| 336 | |
| 337 | time = test_rhashtable(&ht); |
| 338 | rhashtable_destroy(&ht); |
| 339 | if (time < 0) { |
| 340 | pr_warn("Test failed: return code %lld\n", time); |
| 341 | return -EINVAL; |
| 342 | } |
| 343 | |
| 344 | total_time += time; |
Geert Uytterhoeven | 9d6dbe1 | 2015-01-29 15:40:25 +0100 | [diff] [blame] | 345 | } |
| 346 | |
Thomas Graf | 6decd63 | 2015-05-05 02:27:02 +0200 | [diff] [blame] | 347 | do_div(total_time, runs); |
| 348 | pr_info("Average test time: %llu\n", total_time); |
Geert Uytterhoeven | 9d6dbe1 | 2015-01-29 15:40:25 +0100 | [diff] [blame] | 349 | |
Phil Sutter | f4a3e90 | 2015-08-15 00:37:15 +0200 | [diff] [blame] | 350 | if (!tcount) |
| 351 | return 0; |
| 352 | |
| 353 | pr_info("Testing concurrent rhashtable access from %d threads\n", |
| 354 | tcount); |
| 355 | sema_init(&prestart_sem, 1 - tcount); |
| 356 | tdata = vzalloc(tcount * sizeof(struct thread_data)); |
| 357 | if (!tdata) |
| 358 | return -ENOMEM; |
| 359 | objs = vzalloc(tcount * entries * sizeof(struct test_obj)); |
| 360 | if (!objs) { |
| 361 | vfree(tdata); |
| 362 | return -ENOMEM; |
| 363 | } |
| 364 | |
| 365 | err = rhashtable_init(&ht, &test_rht_params); |
| 366 | if (err < 0) { |
| 367 | pr_warn("Test failed: Unable to initialize hashtable: %d\n", |
| 368 | err); |
| 369 | vfree(tdata); |
| 370 | vfree(objs); |
| 371 | return -EINVAL; |
| 372 | } |
| 373 | for (i = 0; i < tcount; i++) { |
| 374 | tdata[i].id = i; |
| 375 | tdata[i].objs = objs + i * entries; |
| 376 | tdata[i].task = kthread_run(threadfunc, &tdata[i], |
| 377 | "rhashtable_thrad[%d]", i); |
| 378 | if (IS_ERR(tdata[i].task)) |
| 379 | pr_err(" kthread_run failed for thread %d\n", i); |
| 380 | else |
| 381 | started_threads++; |
| 382 | } |
| 383 | if (down_interruptible(&prestart_sem)) |
| 384 | pr_err(" down interruptible failed\n"); |
| 385 | for (i = 0; i < tcount; i++) |
| 386 | up(&startup_sem); |
| 387 | for (i = 0; i < tcount; i++) { |
| 388 | if (IS_ERR(tdata[i].task)) |
| 389 | continue; |
| 390 | if ((err = kthread_stop(tdata[i].task))) { |
| 391 | pr_warn("Test failed: thread %d returned: %d\n", |
| 392 | i, err); |
| 393 | failed_threads++; |
| 394 | } |
| 395 | } |
| 396 | pr_info("Started %d threads, %d failed\n", |
| 397 | started_threads, failed_threads); |
| 398 | rhashtable_destroy(&ht); |
| 399 | vfree(tdata); |
| 400 | vfree(objs); |
Thomas Graf | 1aa661f | 2015-04-30 22:37:41 +0000 | [diff] [blame] | 401 | return 0; |
Geert Uytterhoeven | 9d6dbe1 | 2015-01-29 15:40:25 +0100 | [diff] [blame] | 402 | } |
| 403 | |
Daniel Borkmann | 6dd0c16 | 2015-02-20 00:53:39 +0100 | [diff] [blame] | 404 | static void __exit test_rht_exit(void) |
| 405 | { |
| 406 | } |
| 407 | |
Geert Uytterhoeven | 9d6dbe1 | 2015-01-29 15:40:25 +0100 | [diff] [blame] | 408 | module_init(test_rht_init); |
Daniel Borkmann | 6dd0c16 | 2015-02-20 00:53:39 +0100 | [diff] [blame] | 409 | module_exit(test_rht_exit); |
Geert Uytterhoeven | 9d6dbe1 | 2015-01-29 15:40:25 +0100 | [diff] [blame] | 410 | |
| 411 | MODULE_LICENSE("GPL v2"); |