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