test_rhashtable.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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/module.h>
  18. #include <linux/rcupdate.h>
  19. #include <linux/rhashtable.h>
  20. #include <linux/slab.h>
  21. #define MAX_ENTRIES 1000000
  22. #define TEST_INSERT_FAIL INT_MAX
  23. static int entries = 50000;
  24. module_param(entries, int, 0);
  25. MODULE_PARM_DESC(entries, "Number of entries to add (default: 50000)");
  26. static int runs = 4;
  27. module_param(runs, int, 0);
  28. MODULE_PARM_DESC(runs, "Number of test runs per variant (default: 4)");
  29. static int max_size = 65536;
  30. module_param(max_size, int, 0);
  31. MODULE_PARM_DESC(runs, "Maximum table size (default: 65536)");
  32. static bool shrinking = false;
  33. module_param(shrinking, bool, 0);
  34. MODULE_PARM_DESC(shrinking, "Enable automatic shrinking (default: off)");
  35. static int size = 8;
  36. module_param(size, int, 0);
  37. MODULE_PARM_DESC(size, "Initial size hint of table (default: 8)");
  38. struct test_obj {
  39. int value;
  40. struct rhash_head node;
  41. };
  42. static struct test_obj array[MAX_ENTRIES];
  43. static struct rhashtable_params test_rht_params = {
  44. .head_offset = offsetof(struct test_obj, node),
  45. .key_offset = offsetof(struct test_obj, value),
  46. .key_len = sizeof(int),
  47. .hashfn = jhash,
  48. .nulls_base = (3U << RHT_BASE_SHIFT),
  49. };
  50. static int __init test_rht_lookup(struct rhashtable *ht)
  51. {
  52. unsigned int i;
  53. for (i = 0; i < entries * 2; i++) {
  54. struct test_obj *obj;
  55. bool expected = !(i % 2);
  56. u32 key = i;
  57. if (array[i / 2].value == TEST_INSERT_FAIL)
  58. expected = false;
  59. obj = rhashtable_lookup_fast(ht, &key, test_rht_params);
  60. if (expected && !obj) {
  61. pr_warn("Test failed: Could not find key %u\n", key);
  62. return -ENOENT;
  63. } else if (!expected && obj) {
  64. pr_warn("Test failed: Unexpected entry found for key %u\n",
  65. key);
  66. return -EEXIST;
  67. } else if (expected && obj) {
  68. if (obj->value != i) {
  69. pr_warn("Test failed: Lookup value mismatch %u!=%u\n",
  70. obj->value, i);
  71. return -EINVAL;
  72. }
  73. }
  74. }
  75. return 0;
  76. }
  77. static void test_bucket_stats(struct rhashtable *ht)
  78. {
  79. unsigned int err, total = 0, chain_len = 0;
  80. struct rhashtable_iter hti;
  81. struct rhash_head *pos;
  82. err = rhashtable_walk_init(ht, &hti);
  83. if (err) {
  84. pr_warn("Test failed: allocation error");
  85. return;
  86. }
  87. err = rhashtable_walk_start(&hti);
  88. if (err && err != -EAGAIN) {
  89. pr_warn("Test failed: iterator failed: %d\n", err);
  90. return;
  91. }
  92. while ((pos = rhashtable_walk_next(&hti))) {
  93. if (PTR_ERR(pos) == -EAGAIN) {
  94. pr_info("Info: encountered resize\n");
  95. chain_len++;
  96. continue;
  97. } else if (IS_ERR(pos)) {
  98. pr_warn("Test failed: rhashtable_walk_next() error: %ld\n",
  99. PTR_ERR(pos));
  100. break;
  101. }
  102. total++;
  103. }
  104. rhashtable_walk_stop(&hti);
  105. rhashtable_walk_exit(&hti);
  106. pr_info(" Traversal complete: counted=%u, nelems=%u, entries=%d, table-jumps=%u\n",
  107. total, atomic_read(&ht->nelems), entries, chain_len);
  108. if (total != atomic_read(&ht->nelems) || total != entries)
  109. pr_warn("Test failed: Total count mismatch ^^^");
  110. }
  111. static s64 __init test_rhashtable(struct rhashtable *ht)
  112. {
  113. struct test_obj *obj;
  114. int err;
  115. unsigned int i, insert_fails = 0;
  116. s64 start, end;
  117. /*
  118. * Insertion Test:
  119. * Insert entries into table with all keys even numbers
  120. */
  121. pr_info(" Adding %d keys\n", entries);
  122. start = ktime_get_ns();
  123. for (i = 0; i < entries; i++) {
  124. struct test_obj *obj = &array[i];
  125. obj->value = i * 2;
  126. err = rhashtable_insert_fast(ht, &obj->node, test_rht_params);
  127. if (err == -ENOMEM || err == -EBUSY) {
  128. /* Mark failed inserts but continue */
  129. obj->value = TEST_INSERT_FAIL;
  130. insert_fails++;
  131. } else if (err) {
  132. return err;
  133. }
  134. }
  135. if (insert_fails)
  136. pr_info(" %u insertions failed due to memory pressure\n",
  137. insert_fails);
  138. test_bucket_stats(ht);
  139. rcu_read_lock();
  140. test_rht_lookup(ht);
  141. rcu_read_unlock();
  142. test_bucket_stats(ht);
  143. pr_info(" Deleting %d keys\n", entries);
  144. for (i = 0; i < entries; i++) {
  145. u32 key = i * 2;
  146. if (array[i].value != TEST_INSERT_FAIL) {
  147. obj = rhashtable_lookup_fast(ht, &key, test_rht_params);
  148. BUG_ON(!obj);
  149. rhashtable_remove_fast(ht, &obj->node, test_rht_params);
  150. }
  151. }
  152. end = ktime_get_ns();
  153. pr_info(" Duration of test: %lld ns\n", end - start);
  154. return end - start;
  155. }
  156. static struct rhashtable ht;
  157. static int __init test_rht_init(void)
  158. {
  159. int i, err;
  160. u64 total_time = 0;
  161. entries = min(entries, MAX_ENTRIES);
  162. test_rht_params.automatic_shrinking = shrinking;
  163. test_rht_params.max_size = max_size;
  164. test_rht_params.nelem_hint = size;
  165. pr_info("Running rhashtable test nelem=%d, max_size=%d, shrinking=%d\n",
  166. size, max_size, shrinking);
  167. for (i = 0; i < runs; i++) {
  168. s64 time;
  169. pr_info("Test %02d:\n", i);
  170. memset(&array, 0, sizeof(array));
  171. err = rhashtable_init(&ht, &test_rht_params);
  172. if (err < 0) {
  173. pr_warn("Test failed: Unable to initialize hashtable: %d\n",
  174. err);
  175. continue;
  176. }
  177. time = test_rhashtable(&ht);
  178. rhashtable_destroy(&ht);
  179. if (time < 0) {
  180. pr_warn("Test failed: return code %lld\n", time);
  181. return -EINVAL;
  182. }
  183. total_time += time;
  184. }
  185. do_div(total_time, runs);
  186. pr_info("Average test time: %llu\n", total_time);
  187. return 0;
  188. }
  189. static void __exit test_rht_exit(void)
  190. {
  191. }
  192. module_init(test_rht_init);
  193. module_exit(test_rht_exit);
  194. MODULE_LICENSE("GPL v2");