test_rhashtable.c 9.9 KB

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