benchmark.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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_size(unsigned long size, unsigned long step, int order)
  54. {
  55. RADIX_TREE(tree, GFP_KERNEL);
  56. long long normal, tagged;
  57. unsigned long index;
  58. for (index = 0 ; index < size ; index += step) {
  59. item_insert_order(&tree, index, order);
  60. radix_tree_tag_set(&tree, index, 0);
  61. }
  62. tagged = benchmark_iter(&tree, true);
  63. normal = benchmark_iter(&tree, false);
  64. printf("Size %ld, step %6ld, order %d tagged %10lld ns, normal %10lld ns\n",
  65. size, step, order, tagged, normal);
  66. item_kill_tree(&tree);
  67. rcu_barrier();
  68. }
  69. void benchmark(void)
  70. {
  71. unsigned long size[] = {1 << 10, 1 << 20, 0};
  72. unsigned long step[] = {1, 2, 7, 15, 63, 64, 65,
  73. 128, 256, 512, 12345, 0};
  74. int c, s;
  75. printf("starting benchmarks\n");
  76. printf("RADIX_TREE_MAP_SHIFT = %d\n", RADIX_TREE_MAP_SHIFT);
  77. for (c = 0; size[c]; c++)
  78. for (s = 0; step[s]; s++)
  79. benchmark_size(size[c], step[s], 0);
  80. for (c = 0; size[c]; c++)
  81. for (s = 0; step[s]; s++)
  82. benchmark_size(size[c], step[s] << 9, 9);
  83. }