benchmark.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * benchmark.c:
  3. * Author: Konstantin Khlebnikov <koct9i@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. */
  14. #include <linux/radix-tree.h>
  15. #include <linux/slab.h>
  16. #include <linux/errno.h>
  17. #include <time.h>
  18. #include "test.h"
  19. #define for_each_index(i, base, order) \
  20. for (i = base; i < base + (1 << order); i++)
  21. #define NSEC_PER_SEC 1000000000L
  22. static long long benchmark_iter(struct radix_tree_root *root, bool tagged)
  23. {
  24. volatile unsigned long sink = 0;
  25. struct radix_tree_iter iter;
  26. struct timespec start, finish;
  27. long long nsec;
  28. int l, loops = 1;
  29. void **slot;
  30. #ifdef BENCHMARK
  31. again:
  32. #endif
  33. clock_gettime(CLOCK_MONOTONIC, &start);
  34. for (l = 0; l < loops; l++) {
  35. if (tagged) {
  36. radix_tree_for_each_tagged(slot, root, &iter, 0, 0)
  37. sink ^= (unsigned long)slot;
  38. } else {
  39. radix_tree_for_each_slot(slot, root, &iter, 0)
  40. sink ^= (unsigned long)slot;
  41. }
  42. }
  43. clock_gettime(CLOCK_MONOTONIC, &finish);
  44. nsec = (finish.tv_sec - start.tv_sec) * NSEC_PER_SEC +
  45. (finish.tv_nsec - start.tv_nsec);
  46. #ifdef BENCHMARK
  47. if (loops == 1 && nsec * 5 < NSEC_PER_SEC) {
  48. loops = NSEC_PER_SEC / nsec / 4 + 1;
  49. goto again;
  50. }
  51. #endif
  52. nsec /= loops;
  53. return nsec;
  54. }
  55. static void benchmark_insert(struct radix_tree_root *root,
  56. unsigned long size, unsigned long step, int order)
  57. {
  58. struct timespec start, finish;
  59. unsigned long index;
  60. long long nsec;
  61. clock_gettime(CLOCK_MONOTONIC, &start);
  62. for (index = 0 ; index < size ; index += step)
  63. item_insert_order(root, index, order);
  64. clock_gettime(CLOCK_MONOTONIC, &finish);
  65. nsec = (finish.tv_sec - start.tv_sec) * NSEC_PER_SEC +
  66. (finish.tv_nsec - start.tv_nsec);
  67. printv(2, "Size: %8ld, step: %8ld, order: %d, insertion: %15lld ns\n",
  68. size, step, order, nsec);
  69. }
  70. static void benchmark_tagging(struct radix_tree_root *root,
  71. unsigned long size, unsigned long step, int order)
  72. {
  73. struct timespec start, finish;
  74. unsigned long index;
  75. long long nsec;
  76. clock_gettime(CLOCK_MONOTONIC, &start);
  77. for (index = 0 ; index < size ; index += step)
  78. radix_tree_tag_set(root, index, 0);
  79. clock_gettime(CLOCK_MONOTONIC, &finish);
  80. nsec = (finish.tv_sec - start.tv_sec) * NSEC_PER_SEC +
  81. (finish.tv_nsec - start.tv_nsec);
  82. printv(2, "Size: %8ld, step: %8ld, order: %d, tagging: %17lld ns\n",
  83. size, step, order, nsec);
  84. }
  85. static void benchmark_delete(struct radix_tree_root *root,
  86. unsigned long size, unsigned long step, int order)
  87. {
  88. struct timespec start, finish;
  89. unsigned long index, i;
  90. long long nsec;
  91. clock_gettime(CLOCK_MONOTONIC, &start);
  92. for (index = 0 ; index < size ; index += step)
  93. for_each_index(i, index, order)
  94. item_delete(root, i);
  95. clock_gettime(CLOCK_MONOTONIC, &finish);
  96. nsec = (finish.tv_sec - start.tv_sec) * NSEC_PER_SEC +
  97. (finish.tv_nsec - start.tv_nsec);
  98. printv(2, "Size: %8ld, step: %8ld, order: %d, deletion: %16lld ns\n",
  99. size, step, order, nsec);
  100. }
  101. static void benchmark_size(unsigned long size, unsigned long step, int order)
  102. {
  103. RADIX_TREE(tree, GFP_KERNEL);
  104. long long normal, tagged;
  105. benchmark_insert(&tree, size, step, order);
  106. benchmark_tagging(&tree, size, step, order);
  107. tagged = benchmark_iter(&tree, true);
  108. normal = benchmark_iter(&tree, false);
  109. printv(2, "Size: %8ld, step: %8ld, order: %d, tagged iteration: %8lld ns\n",
  110. size, step, order, tagged);
  111. printv(2, "Size: %8ld, step: %8ld, order: %d, normal iteration: %8lld ns\n",
  112. size, step, order, normal);
  113. benchmark_delete(&tree, size, step, order);
  114. item_kill_tree(&tree);
  115. rcu_barrier();
  116. }
  117. static long long __benchmark_split(unsigned long index,
  118. int old_order, int new_order)
  119. {
  120. struct timespec start, finish;
  121. long long nsec;
  122. RADIX_TREE(tree, GFP_ATOMIC);
  123. item_insert_order(&tree, index, old_order);
  124. clock_gettime(CLOCK_MONOTONIC, &start);
  125. radix_tree_split(&tree, index, new_order);
  126. clock_gettime(CLOCK_MONOTONIC, &finish);
  127. nsec = (finish.tv_sec - start.tv_sec) * NSEC_PER_SEC +
  128. (finish.tv_nsec - start.tv_nsec);
  129. item_kill_tree(&tree);
  130. return nsec;
  131. }
  132. static void benchmark_split(unsigned long size, unsigned long step)
  133. {
  134. int i, j, idx;
  135. long long nsec = 0;
  136. for (idx = 0; idx < size; idx += step) {
  137. for (i = 3; i < 11; i++) {
  138. for (j = 0; j < i; j++) {
  139. nsec += __benchmark_split(idx, i, j);
  140. }
  141. }
  142. }
  143. printv(2, "Size %8ld, step %8ld, split time %10lld ns\n",
  144. size, step, nsec);
  145. }
  146. static long long __benchmark_join(unsigned long index,
  147. unsigned order1, unsigned order2)
  148. {
  149. unsigned long loc;
  150. struct timespec start, finish;
  151. long long nsec;
  152. void *item, *item2 = item_create(index + 1, order1);
  153. RADIX_TREE(tree, GFP_KERNEL);
  154. item_insert_order(&tree, index, order2);
  155. item = radix_tree_lookup(&tree, index);
  156. clock_gettime(CLOCK_MONOTONIC, &start);
  157. radix_tree_join(&tree, index + 1, order1, item2);
  158. clock_gettime(CLOCK_MONOTONIC, &finish);
  159. nsec = (finish.tv_sec - start.tv_sec) * NSEC_PER_SEC +
  160. (finish.tv_nsec - start.tv_nsec);
  161. loc = find_item(&tree, item);
  162. if (loc == -1)
  163. free(item);
  164. item_kill_tree(&tree);
  165. return nsec;
  166. }
  167. static void benchmark_join(unsigned long step)
  168. {
  169. int i, j, idx;
  170. long long nsec = 0;
  171. for (idx = 0; idx < 1 << 10; idx += step) {
  172. for (i = 1; i < 15; i++) {
  173. for (j = 0; j < i; j++) {
  174. nsec += __benchmark_join(idx, i, j);
  175. }
  176. }
  177. }
  178. printv(2, "Size %8d, step %8ld, join time %10lld ns\n",
  179. 1 << 10, step, nsec);
  180. }
  181. void benchmark(void)
  182. {
  183. unsigned long size[] = {1 << 10, 1 << 20, 0};
  184. unsigned long step[] = {1, 2, 7, 15, 63, 64, 65,
  185. 128, 256, 512, 12345, 0};
  186. int c, s;
  187. printv(1, "starting benchmarks\n");
  188. printv(1, "RADIX_TREE_MAP_SHIFT = %d\n", RADIX_TREE_MAP_SHIFT);
  189. for (c = 0; size[c]; c++)
  190. for (s = 0; step[s]; s++)
  191. benchmark_size(size[c], step[s], 0);
  192. for (c = 0; size[c]; c++)
  193. for (s = 0; step[s]; s++)
  194. benchmark_size(size[c], step[s] << 9, 9);
  195. for (c = 0; size[c]; c++)
  196. for (s = 0; step[s]; s++)
  197. benchmark_split(size[c], step[s]);
  198. for (s = 0; step[s]; s++)
  199. benchmark_join(step[s]);
  200. }