interval_tree_test.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include <linux/module.h>
  2. #include <linux/interval_tree.h>
  3. #include <linux/random.h>
  4. #include <asm/timex.h>
  5. #define NODES 100
  6. #define PERF_LOOPS 100000
  7. #define SEARCHES 100
  8. #define SEARCH_LOOPS 10000
  9. static struct rb_root root = RB_ROOT;
  10. static struct interval_tree_node nodes[NODES];
  11. static u32 queries[SEARCHES];
  12. static struct rnd_state rnd;
  13. static inline unsigned long
  14. search(unsigned long query, struct rb_root *root)
  15. {
  16. struct interval_tree_node *node;
  17. unsigned long results = 0;
  18. for (node = interval_tree_iter_first(root, query, query); node;
  19. node = interval_tree_iter_next(node, query, query))
  20. results++;
  21. return results;
  22. }
  23. static void init(void)
  24. {
  25. int i;
  26. for (i = 0; i < NODES; i++) {
  27. u32 a = prandom_u32_state(&rnd);
  28. u32 b = prandom_u32_state(&rnd);
  29. if (a <= b) {
  30. nodes[i].start = a;
  31. nodes[i].last = b;
  32. } else {
  33. nodes[i].start = b;
  34. nodes[i].last = a;
  35. }
  36. }
  37. for (i = 0; i < SEARCHES; i++)
  38. queries[i] = prandom_u32_state(&rnd);
  39. }
  40. static int interval_tree_test_init(void)
  41. {
  42. int i, j;
  43. unsigned long results;
  44. cycles_t time1, time2, time;
  45. printk(KERN_ALERT "interval tree insert/remove");
  46. prandom_seed_state(&rnd, 3141592653589793238ULL);
  47. init();
  48. time1 = get_cycles();
  49. for (i = 0; i < PERF_LOOPS; i++) {
  50. for (j = 0; j < NODES; j++)
  51. interval_tree_insert(nodes + j, &root);
  52. for (j = 0; j < NODES; j++)
  53. interval_tree_remove(nodes + j, &root);
  54. }
  55. time2 = get_cycles();
  56. time = time2 - time1;
  57. time = div_u64(time, PERF_LOOPS);
  58. printk(" -> %llu cycles\n", (unsigned long long)time);
  59. printk(KERN_ALERT "interval tree search");
  60. for (j = 0; j < NODES; j++)
  61. interval_tree_insert(nodes + j, &root);
  62. time1 = get_cycles();
  63. results = 0;
  64. for (i = 0; i < SEARCH_LOOPS; i++)
  65. for (j = 0; j < SEARCHES; j++)
  66. results += search(queries[j], &root);
  67. time2 = get_cycles();
  68. time = time2 - time1;
  69. time = div_u64(time, SEARCH_LOOPS);
  70. results = div_u64(results, SEARCH_LOOPS);
  71. printk(" -> %llu cycles (%lu results)\n",
  72. (unsigned long long)time, results);
  73. return -EAGAIN; /* Fail will directly unload the module */
  74. }
  75. static void interval_tree_test_exit(void)
  76. {
  77. printk(KERN_ALERT "test exit\n");
  78. }
  79. module_init(interval_tree_test_init)
  80. module_exit(interval_tree_test_exit)
  81. MODULE_LICENSE("GPL");
  82. MODULE_AUTHOR("Michel Lespinasse");
  83. MODULE_DESCRIPTION("Interval Tree test");