interval_tree_test.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/module.h>
  3. #include <linux/moduleparam.h>
  4. #include <linux/interval_tree.h>
  5. #include <linux/prandom.h>
  6. #include <linux/slab.h>
  7. #include <asm/timex.h>
  8. #include <linux/bitmap.h>
  9. #include <linux/maple_tree.h>
  10. #define __param(type, name, init, msg) \
  11. static type name = init; \
  12. module_param(name, type, 0444); \
  13. MODULE_PARM_DESC(name, msg);
  14. __param(int, nnodes, 100, "Number of nodes in the interval tree");
  15. __param(int, perf_loops, 1000, "Number of iterations modifying the tree");
  16. __param(int, nsearches, 100, "Number of searches to the interval tree");
  17. __param(int, search_loops, 1000, "Number of iterations searching the tree");
  18. __param(bool, search_all, false, "Searches will iterate all nodes in the tree");
  19. __param(uint, max_endpoint, ~0, "Largest value for the interval's endpoint");
  20. __param(ullong, seed, 3141592653589793238ULL, "Random seed");
  21. static struct rb_root_cached root = RB_ROOT_CACHED;
  22. static struct interval_tree_node *nodes = NULL;
  23. static u32 *queries = NULL;
  24. static struct rnd_state rnd;
  25. static inline unsigned long
  26. search(struct rb_root_cached *root, unsigned long start, unsigned long last)
  27. {
  28. struct interval_tree_node *node;
  29. unsigned long results = 0;
  30. for (node = interval_tree_iter_first(root, start, last); node;
  31. node = interval_tree_iter_next(node, start, last))
  32. results++;
  33. return results;
  34. }
  35. static void init(void)
  36. {
  37. int i;
  38. for (i = 0; i < nnodes; i++) {
  39. u32 b = (prandom_u32_state(&rnd) >> 4) % max_endpoint;
  40. u32 a = (prandom_u32_state(&rnd) >> 4) % b;
  41. nodes[i].start = a;
  42. nodes[i].last = b;
  43. }
  44. /*
  45. * Limit the search scope to what the user defined.
  46. * Otherwise we are merely measuring empty walks,
  47. * which is pointless.
  48. */
  49. for (i = 0; i < nsearches; i++)
  50. queries[i] = (prandom_u32_state(&rnd) >> 4) % max_endpoint;
  51. }
  52. static int basic_check(void)
  53. {
  54. int i, j;
  55. cycles_t time1, time2, time;
  56. printk(KERN_ALERT "interval tree insert/remove");
  57. init();
  58. time1 = get_cycles();
  59. for (i = 0; i < perf_loops; i++) {
  60. for (j = 0; j < nnodes; j++)
  61. interval_tree_insert(nodes + j, &root);
  62. for (j = 0; j < nnodes; j++)
  63. interval_tree_remove(nodes + j, &root);
  64. }
  65. time2 = get_cycles();
  66. time = time2 - time1;
  67. time = div_u64(time, perf_loops);
  68. printk(" -> %llu cycles\n", (unsigned long long)time);
  69. return 0;
  70. }
  71. static int search_check(void)
  72. {
  73. int i, j;
  74. unsigned long results;
  75. cycles_t time1, time2, time;
  76. printk(KERN_ALERT "interval tree search");
  77. init();
  78. for (j = 0; j < nnodes; j++)
  79. interval_tree_insert(nodes + j, &root);
  80. time1 = get_cycles();
  81. results = 0;
  82. for (i = 0; i < search_loops; i++)
  83. for (j = 0; j < nsearches; j++) {
  84. unsigned long start = search_all ? 0 : queries[j];
  85. unsigned long last = search_all ? max_endpoint : queries[j];
  86. results += search(&root, start, last);
  87. }
  88. time2 = get_cycles();
  89. time = time2 - time1;
  90. time = div_u64(time, search_loops);
  91. results = div_u64(results, search_loops);
  92. printk(" -> %llu cycles (%lu results)\n",
  93. (unsigned long long)time, results);
  94. for (j = 0; j < nnodes; j++)
  95. interval_tree_remove(nodes + j, &root);
  96. return 0;
  97. }
  98. static int intersection_range_check(void)
  99. {
  100. int i, j, k;
  101. unsigned long start, last;
  102. struct interval_tree_node *node;
  103. unsigned long *intxn1;
  104. unsigned long *intxn2;
  105. printk(KERN_ALERT "interval tree iteration\n");
  106. intxn1 = bitmap_alloc(nnodes, GFP_KERNEL);
  107. if (!intxn1) {
  108. WARN_ON_ONCE("Failed to allocate intxn1\n");
  109. return -ENOMEM;
  110. }
  111. intxn2 = bitmap_alloc(nnodes, GFP_KERNEL);
  112. if (!intxn2) {
  113. WARN_ON_ONCE("Failed to allocate intxn2\n");
  114. bitmap_free(intxn1);
  115. return -ENOMEM;
  116. }
  117. for (i = 0; i < search_loops; i++) {
  118. /* Initialize interval tree for each round */
  119. init();
  120. for (j = 0; j < nnodes; j++)
  121. interval_tree_insert(nodes + j, &root);
  122. /* Let's try nsearches different ranges */
  123. for (k = 0; k < nsearches; k++) {
  124. /* Try whole range once */
  125. if (!k) {
  126. start = 0UL;
  127. last = ULONG_MAX;
  128. } else {
  129. last = (prandom_u32_state(&rnd) >> 4) % max_endpoint;
  130. start = (prandom_u32_state(&rnd) >> 4) % last;
  131. }
  132. /* Walk nodes to mark intersection nodes */
  133. bitmap_zero(intxn1, nnodes);
  134. for (j = 0; j < nnodes; j++) {
  135. node = nodes + j;
  136. if (start <= node->last && last >= node->start)
  137. bitmap_set(intxn1, j, 1);
  138. }
  139. /* Iterate tree to clear intersection nodes */
  140. bitmap_zero(intxn2, nnodes);
  141. for (node = interval_tree_iter_first(&root, start, last); node;
  142. node = interval_tree_iter_next(node, start, last))
  143. bitmap_set(intxn2, node - nodes, 1);
  144. WARN_ON_ONCE(!bitmap_equal(intxn1, intxn2, nnodes));
  145. }
  146. for (j = 0; j < nnodes; j++)
  147. interval_tree_remove(nodes + j, &root);
  148. }
  149. bitmap_free(intxn1);
  150. bitmap_free(intxn2);
  151. return 0;
  152. }
  153. #ifdef CONFIG_INTERVAL_TREE_SPAN_ITER
  154. /*
  155. * Helper function to get span of current position from maple tree point of
  156. * view.
  157. */
  158. static void mas_cur_span(struct ma_state *mas, struct interval_tree_span_iter *state)
  159. {
  160. unsigned long cur_start;
  161. unsigned long cur_last;
  162. int is_hole;
  163. if (mas->status == ma_overflow)
  164. return;
  165. /* walk to current position */
  166. state->is_hole = mas_walk(mas) ? 0 : 1;
  167. cur_start = mas->index < state->first_index ?
  168. state->first_index : mas->index;
  169. /* whether we have followers */
  170. do {
  171. cur_last = mas->last > state->last_index ?
  172. state->last_index : mas->last;
  173. is_hole = mas_next_range(mas, state->last_index) ? 0 : 1;
  174. } while (mas->status != ma_overflow && is_hole == state->is_hole);
  175. if (state->is_hole) {
  176. state->start_hole = cur_start;
  177. state->last_hole = cur_last;
  178. } else {
  179. state->start_used = cur_start;
  180. state->last_used = cur_last;
  181. }
  182. /* advance position for next round */
  183. if (mas->status != ma_overflow)
  184. mas_set(mas, cur_last + 1);
  185. }
  186. static int span_iteration_check(void)
  187. {
  188. int i, j, k;
  189. unsigned long start, last;
  190. struct interval_tree_span_iter span, mas_span;
  191. DEFINE_MTREE(tree);
  192. MA_STATE(mas, &tree, 0, 0);
  193. printk(KERN_ALERT "interval tree span iteration\n");
  194. for (i = 0; i < search_loops; i++) {
  195. /* Initialize interval tree for each round */
  196. init();
  197. for (j = 0; j < nnodes; j++)
  198. interval_tree_insert(nodes + j, &root);
  199. /* Put all the range into maple tree */
  200. mt_init_flags(&tree, MT_FLAGS_ALLOC_RANGE);
  201. mt_set_in_rcu(&tree);
  202. for (j = 0; j < nnodes; j++)
  203. WARN_ON_ONCE(mtree_store_range(&tree, nodes[j].start,
  204. nodes[j].last, nodes + j, GFP_KERNEL));
  205. /* Let's try nsearches different ranges */
  206. for (k = 0; k < nsearches; k++) {
  207. /* Try whole range once */
  208. if (!k) {
  209. start = 0UL;
  210. last = ULONG_MAX;
  211. } else {
  212. last = (prandom_u32_state(&rnd) >> 4) % max_endpoint;
  213. start = (prandom_u32_state(&rnd) >> 4) % last;
  214. }
  215. mas_span.first_index = start;
  216. mas_span.last_index = last;
  217. mas_span.is_hole = -1;
  218. mas_set(&mas, start);
  219. interval_tree_for_each_span(&span, &root, start, last) {
  220. mas_cur_span(&mas, &mas_span);
  221. WARN_ON_ONCE(span.is_hole != mas_span.is_hole);
  222. if (span.is_hole) {
  223. WARN_ON_ONCE(span.start_hole != mas_span.start_hole);
  224. WARN_ON_ONCE(span.last_hole != mas_span.last_hole);
  225. } else {
  226. WARN_ON_ONCE(span.start_used != mas_span.start_used);
  227. WARN_ON_ONCE(span.last_used != mas_span.last_used);
  228. }
  229. }
  230. }
  231. WARN_ON_ONCE(mas.status != ma_overflow);
  232. /* Cleanup maple tree for each round */
  233. mtree_destroy(&tree);
  234. /* Cleanup interval tree for each round */
  235. for (j = 0; j < nnodes; j++)
  236. interval_tree_remove(nodes + j, &root);
  237. }
  238. return 0;
  239. }
  240. #else
  241. static inline int span_iteration_check(void) {return 0; }
  242. #endif
  243. static int interval_tree_test_init(void)
  244. {
  245. nodes = kmalloc_objs(struct interval_tree_node, nnodes);
  246. if (!nodes)
  247. return -ENOMEM;
  248. queries = kmalloc_array(nsearches, sizeof(int), GFP_KERNEL);
  249. if (!queries) {
  250. kfree(nodes);
  251. return -ENOMEM;
  252. }
  253. prandom_seed_state(&rnd, seed);
  254. basic_check();
  255. search_check();
  256. intersection_range_check();
  257. span_iteration_check();
  258. kfree(queries);
  259. kfree(nodes);
  260. return -EAGAIN; /* Fail will directly unload the module */
  261. }
  262. static void interval_tree_test_exit(void)
  263. {
  264. printk(KERN_ALERT "test exit\n");
  265. }
  266. module_init(interval_tree_test_init)
  267. module_exit(interval_tree_test_exit)
  268. MODULE_LICENSE("GPL");
  269. MODULE_AUTHOR("Michel Lespinasse");
  270. MODULE_DESCRIPTION("Interval Tree test");