test_rhashtable.c 10 KB

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