test_rhashtable.c 9.7 KB

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