interval_tree_test_main.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 = prandom32(&rnd), b = prandom32(&rnd);
  28. if (a <= b) {
  29. nodes[i].start = a;
  30. nodes[i].last = b;
  31. } else {
  32. nodes[i].start = b;
  33. nodes[i].last = a;
  34. }
  35. }
  36. for (i = 0; i < SEARCHES; i++)
  37. queries[i] = prandom32(&rnd);
  38. }
  39. static int interval_tree_test_init(void)
  40. {
  41. int i, j;
  42. unsigned long results;
  43. cycles_t time1, time2, time;
  44. printk(KERN_ALERT "interval tree insert/remove");
  45. prandom32_seed(&rnd, 3141592653589793238ULL);
  46. init();
  47. time1 = get_cycles();
  48. for (i = 0; i < PERF_LOOPS; i++) {
  49. for (j = 0; j < NODES; j++)
  50. interval_tree_insert(nodes + j, &root);
  51. for (j = 0; j < NODES; j++)
  52. interval_tree_remove(nodes + j, &root);
  53. }
  54. time2 = get_cycles();
  55. time = time2 - time1;
  56. time = div_u64(time, PERF_LOOPS);
  57. printk(" -> %llu cycles\n", (unsigned long long)time);
  58. printk(KERN_ALERT "interval tree search");
  59. for (j = 0; j < NODES; j++)
  60. interval_tree_insert(nodes + j, &root);
  61. time1 = get_cycles();
  62. results = 0;
  63. for (i = 0; i < SEARCH_LOOPS; i++)
  64. for (j = 0; j < SEARCHES; j++)
  65. results += search(queries[j], &root);
  66. time2 = get_cycles();
  67. time = time2 - time1;
  68. time = div_u64(time, SEARCH_LOOPS);
  69. results = div_u64(results, SEARCH_LOOPS);
  70. printk(" -> %llu cycles (%lu results)\n",
  71. (unsigned long long)time, results);
  72. return -EAGAIN; /* Fail will directly unload the module */
  73. }
  74. static void interval_tree_test_exit(void)
  75. {
  76. printk(KERN_ALERT "test exit\n");
  77. }
  78. module_init(interval_tree_test_init)
  79. module_exit(interval_tree_test_exit)
  80. MODULE_LICENSE("GPL");
  81. MODULE_AUTHOR("Michel Lespinasse");
  82. MODULE_DESCRIPTION("Interval Tree test");