multiorder.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * multiorder.c: Multi-order radix tree entry testing
  3. * Copyright (c) 2016 Intel Corporation
  4. * Author: Ross Zwisler <ross.zwisler@linux.intel.com>
  5. * Author: Matthew Wilcox <matthew.r.wilcox@intel.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms and conditions of the GNU General Public License,
  9. * version 2, as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. */
  16. #include <linux/radix-tree.h>
  17. #include <linux/slab.h>
  18. #include <linux/errno.h>
  19. #include <pthread.h>
  20. #include "test.h"
  21. void multiorder_iteration(void)
  22. {
  23. RADIX_TREE(tree, GFP_KERNEL);
  24. struct radix_tree_iter iter;
  25. void **slot;
  26. int i, j, err;
  27. printv(1, "Multiorder iteration test\n");
  28. #define NUM_ENTRIES 11
  29. int index[NUM_ENTRIES] = {0, 2, 4, 8, 16, 32, 34, 36, 64, 72, 128};
  30. int order[NUM_ENTRIES] = {1, 1, 2, 3, 4, 1, 0, 1, 3, 0, 7};
  31. for (i = 0; i < NUM_ENTRIES; i++) {
  32. err = item_insert_order(&tree, index[i], order[i]);
  33. assert(!err);
  34. }
  35. for (j = 0; j < 256; j++) {
  36. for (i = 0; i < NUM_ENTRIES; i++)
  37. if (j <= (index[i] | ((1 << order[i]) - 1)))
  38. break;
  39. radix_tree_for_each_slot(slot, &tree, &iter, j) {
  40. int height = order[i] / RADIX_TREE_MAP_SHIFT;
  41. int shift = height * RADIX_TREE_MAP_SHIFT;
  42. unsigned long mask = (1UL << order[i]) - 1;
  43. struct item *item = *slot;
  44. assert((iter.index | mask) == (index[i] | mask));
  45. assert(iter.shift == shift);
  46. assert(!radix_tree_is_internal_node(item));
  47. assert((item->index | mask) == (index[i] | mask));
  48. assert(item->order == order[i]);
  49. i++;
  50. }
  51. }
  52. item_kill_tree(&tree);
  53. }
  54. void multiorder_tagged_iteration(void)
  55. {
  56. RADIX_TREE(tree, GFP_KERNEL);
  57. struct radix_tree_iter iter;
  58. void **slot;
  59. int i, j;
  60. printv(1, "Multiorder tagged iteration test\n");
  61. #define MT_NUM_ENTRIES 9
  62. int index[MT_NUM_ENTRIES] = {0, 2, 4, 16, 32, 40, 64, 72, 128};
  63. int order[MT_NUM_ENTRIES] = {1, 0, 2, 4, 3, 1, 3, 0, 7};
  64. #define TAG_ENTRIES 7
  65. int tag_index[TAG_ENTRIES] = {0, 4, 16, 40, 64, 72, 128};
  66. for (i = 0; i < MT_NUM_ENTRIES; i++)
  67. assert(!item_insert_order(&tree, index[i], order[i]));
  68. assert(!radix_tree_tagged(&tree, 1));
  69. for (i = 0; i < TAG_ENTRIES; i++)
  70. assert(radix_tree_tag_set(&tree, tag_index[i], 1));
  71. for (j = 0; j < 256; j++) {
  72. int k;
  73. for (i = 0; i < TAG_ENTRIES; i++) {
  74. for (k = i; index[k] < tag_index[i]; k++)
  75. ;
  76. if (j <= (index[k] | ((1 << order[k]) - 1)))
  77. break;
  78. }
  79. radix_tree_for_each_tagged(slot, &tree, &iter, j, 1) {
  80. unsigned long mask;
  81. struct item *item = *slot;
  82. for (k = i; index[k] < tag_index[i]; k++)
  83. ;
  84. mask = (1UL << order[k]) - 1;
  85. assert((iter.index | mask) == (tag_index[i] | mask));
  86. assert(!radix_tree_is_internal_node(item));
  87. assert((item->index | mask) == (tag_index[i] | mask));
  88. assert(item->order == order[k]);
  89. i++;
  90. }
  91. }
  92. assert(tag_tagged_items(&tree, 0, ~0UL, TAG_ENTRIES, XA_MARK_1,
  93. XA_MARK_2) == TAG_ENTRIES);
  94. for (j = 0; j < 256; j++) {
  95. int mask, k;
  96. for (i = 0; i < TAG_ENTRIES; i++) {
  97. for (k = i; index[k] < tag_index[i]; k++)
  98. ;
  99. if (j <= (index[k] | ((1 << order[k]) - 1)))
  100. break;
  101. }
  102. radix_tree_for_each_tagged(slot, &tree, &iter, j, 2) {
  103. struct item *item = *slot;
  104. for (k = i; index[k] < tag_index[i]; k++)
  105. ;
  106. mask = (1 << order[k]) - 1;
  107. assert((iter.index | mask) == (tag_index[i] | mask));
  108. assert(!radix_tree_is_internal_node(item));
  109. assert((item->index | mask) == (tag_index[i] | mask));
  110. assert(item->order == order[k]);
  111. i++;
  112. }
  113. }
  114. assert(tag_tagged_items(&tree, 1, ~0UL, MT_NUM_ENTRIES * 2, XA_MARK_1,
  115. XA_MARK_0) == TAG_ENTRIES);
  116. i = 0;
  117. radix_tree_for_each_tagged(slot, &tree, &iter, 0, 0) {
  118. assert(iter.index == tag_index[i]);
  119. i++;
  120. }
  121. item_kill_tree(&tree);
  122. }
  123. bool stop_iteration = false;
  124. static void *creator_func(void *ptr)
  125. {
  126. /* 'order' is set up to ensure we have sibling entries */
  127. unsigned int order = RADIX_TREE_MAP_SHIFT - 1;
  128. struct radix_tree_root *tree = ptr;
  129. int i;
  130. for (i = 0; i < 10000; i++) {
  131. item_insert_order(tree, 0, order);
  132. item_delete_rcu(tree, 0);
  133. }
  134. stop_iteration = true;
  135. return NULL;
  136. }
  137. static void *iterator_func(void *ptr)
  138. {
  139. struct radix_tree_root *tree = ptr;
  140. struct radix_tree_iter iter;
  141. struct item *item;
  142. void **slot;
  143. while (!stop_iteration) {
  144. rcu_read_lock();
  145. radix_tree_for_each_slot(slot, tree, &iter, 0) {
  146. item = radix_tree_deref_slot(slot);
  147. if (!item)
  148. continue;
  149. if (radix_tree_deref_retry(item)) {
  150. slot = radix_tree_iter_retry(&iter);
  151. continue;
  152. }
  153. item_sanity(item, iter.index);
  154. }
  155. rcu_read_unlock();
  156. }
  157. return NULL;
  158. }
  159. static void multiorder_iteration_race(void)
  160. {
  161. const int num_threads = sysconf(_SC_NPROCESSORS_ONLN);
  162. pthread_t worker_thread[num_threads];
  163. RADIX_TREE(tree, GFP_KERNEL);
  164. int i;
  165. pthread_create(&worker_thread[0], NULL, &creator_func, &tree);
  166. for (i = 1; i < num_threads; i++)
  167. pthread_create(&worker_thread[i], NULL, &iterator_func, &tree);
  168. for (i = 0; i < num_threads; i++)
  169. pthread_join(worker_thread[i], NULL);
  170. item_kill_tree(&tree);
  171. }
  172. void multiorder_checks(void)
  173. {
  174. multiorder_iteration();
  175. multiorder_tagged_iteration();
  176. multiorder_iteration_race();
  177. radix_tree_cpu_dead(0);
  178. }
  179. int __weak main(void)
  180. {
  181. radix_tree_init();
  182. multiorder_checks();
  183. return 0;
  184. }