benchmark.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 NSEC_PER_SEC 1000000000L
  20. static long long benchmark_iter(struct radix_tree_root *root, bool tagged)
  21. {
  22. volatile unsigned long sink = 0;
  23. struct radix_tree_iter iter;
  24. struct timespec start, finish;
  25. long long nsec;
  26. int l, loops = 1;
  27. void **slot;
  28. #ifdef BENCHMARK
  29. again:
  30. #endif
  31. clock_gettime(CLOCK_MONOTONIC, &start);
  32. for (l = 0; l < loops; l++) {
  33. if (tagged) {
  34. radix_tree_for_each_tagged(slot, root, &iter, 0, 0)
  35. sink ^= (unsigned long)slot;
  36. } else {
  37. radix_tree_for_each_slot(slot, root, &iter, 0)
  38. sink ^= (unsigned long)slot;
  39. }
  40. }
  41. clock_gettime(CLOCK_MONOTONIC, &finish);
  42. nsec = (finish.tv_sec - start.tv_sec) * NSEC_PER_SEC +
  43. (finish.tv_nsec - start.tv_nsec);
  44. #ifdef BENCHMARK
  45. if (loops == 1 && nsec * 5 < NSEC_PER_SEC) {
  46. loops = NSEC_PER_SEC / nsec / 4 + 1;
  47. goto again;
  48. }
  49. #endif
  50. nsec /= loops;
  51. return nsec;
  52. }
  53. static void benchmark_insert(struct radix_tree_root *root,
  54. unsigned long size, unsigned long step)
  55. {
  56. struct timespec start, finish;
  57. unsigned long index;
  58. long long nsec;
  59. clock_gettime(CLOCK_MONOTONIC, &start);
  60. for (index = 0 ; index < size ; index += step)
  61. item_insert(root, index);
  62. clock_gettime(CLOCK_MONOTONIC, &finish);
  63. nsec = (finish.tv_sec - start.tv_sec) * NSEC_PER_SEC +
  64. (finish.tv_nsec - start.tv_nsec);
  65. printv(2, "Size: %8ld, step: %8ld, insertion: %15lld ns\n",
  66. size, step, nsec);
  67. }
  68. static void benchmark_tagging(struct radix_tree_root *root,
  69. unsigned long size, unsigned long step)
  70. {
  71. struct timespec start, finish;
  72. unsigned long index;
  73. long long nsec;
  74. clock_gettime(CLOCK_MONOTONIC, &start);
  75. for (index = 0 ; index < size ; index += step)
  76. radix_tree_tag_set(root, index, 0);
  77. clock_gettime(CLOCK_MONOTONIC, &finish);
  78. nsec = (finish.tv_sec - start.tv_sec) * NSEC_PER_SEC +
  79. (finish.tv_nsec - start.tv_nsec);
  80. printv(2, "Size: %8ld, step: %8ld, tagging: %17lld ns\n",
  81. size, step, nsec);
  82. }
  83. static void benchmark_delete(struct radix_tree_root *root,
  84. unsigned long size, unsigned long step)
  85. {
  86. struct timespec start, finish;
  87. unsigned long index;
  88. long long nsec;
  89. clock_gettime(CLOCK_MONOTONIC, &start);
  90. for (index = 0 ; index < size ; index += step)
  91. item_delete(root, index);
  92. clock_gettime(CLOCK_MONOTONIC, &finish);
  93. nsec = (finish.tv_sec - start.tv_sec) * NSEC_PER_SEC +
  94. (finish.tv_nsec - start.tv_nsec);
  95. printv(2, "Size: %8ld, step: %8ld, deletion: %16lld ns\n",
  96. size, step, nsec);
  97. }
  98. static void benchmark_size(unsigned long size, unsigned long step)
  99. {
  100. RADIX_TREE(tree, GFP_KERNEL);
  101. long long normal, tagged;
  102. benchmark_insert(&tree, size, step);
  103. benchmark_tagging(&tree, size, step);
  104. tagged = benchmark_iter(&tree, true);
  105. normal = benchmark_iter(&tree, false);
  106. printv(2, "Size: %8ld, step: %8ld, tagged iteration: %8lld ns\n",
  107. size, step, tagged);
  108. printv(2, "Size: %8ld, step: %8ld, normal iteration: %8lld ns\n",
  109. size, step, normal);
  110. benchmark_delete(&tree, size, step);
  111. item_kill_tree(&tree);
  112. rcu_barrier();
  113. }
  114. void benchmark(void)
  115. {
  116. unsigned long size[] = {1 << 10, 1 << 20, 0};
  117. unsigned long step[] = {1, 2, 7, 15, 63, 64, 65,
  118. 128, 256, 512, 12345, 0};
  119. int c, s;
  120. printv(1, "starting benchmarks\n");
  121. printv(1, "RADIX_TREE_MAP_SHIFT = %d\n", RADIX_TREE_MAP_SHIFT);
  122. for (c = 0; size[c]; c++)
  123. for (s = 0; step[s]; s++)
  124. benchmark_size(size[c], step[s]);
  125. }