prio_tree_test.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include <linux/module.h>
  2. #include <linux/prio_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 prio_tree_root root;
  10. static struct prio_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 prio_tree_root *root)
  15. {
  16. struct prio_tree_iter iter;
  17. unsigned long results = 0;
  18. prio_tree_iter_init(&iter, root, query, query);
  19. while (prio_tree_next(&iter))
  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 prio_tree_test_init(void)
  40. {
  41. int i, j;
  42. unsigned long results;
  43. cycles_t time1, time2, time;
  44. printk(KERN_ALERT "prio tree insert/remove");
  45. prandom32_seed(&rnd, 3141592653589793238ULL);
  46. INIT_PRIO_TREE_ROOT(&root);
  47. init();
  48. time1 = get_cycles();
  49. for (i = 0; i < PERF_LOOPS; i++) {
  50. for (j = 0; j < NODES; j++)
  51. prio_tree_insert(&root, nodes + j);
  52. for (j = 0; j < NODES; j++)
  53. prio_tree_remove(&root, nodes + j);
  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 "prio tree search");
  60. for (j = 0; j < NODES; j++)
  61. prio_tree_insert(&root, nodes + j);
  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 prio_tree_test_exit(void)
  76. {
  77. printk(KERN_ALERT "test exit\n");
  78. }
  79. module_init(prio_tree_test_init)
  80. module_exit(prio_tree_test_exit)
  81. MODULE_LICENSE("GPL");
  82. MODULE_AUTHOR("Michel Lespinasse");
  83. MODULE_DESCRIPTION("Prio Tree test");