1 #include <linux/module.h>
2 #include <linux/interval_tree.h>
3 #include <linux/random.h>
7 #define PERF_LOOPS 100000
9 #define SEARCH_LOOPS 10000
11 static struct rb_root root = RB_ROOT;
12 static struct interval_tree_node nodes[NODES];
13 static u32 queries[SEARCHES];
15 static struct rnd_state rnd;
17 static inline unsigned long
18 search(unsigned long query, struct rb_root *root)
20 struct interval_tree_node *node;
21 unsigned long results = 0;
23 for (node = interval_tree_iter_first(root, query, query); node;
24 node = interval_tree_iter_next(node, query, query))
29 static void init(void)
32 for (i = 0; i < NODES; i++) {
33 u32 a = prandom32(&rnd), b = prandom32(&rnd);
42 for (i = 0; i < SEARCHES; i++)
43 queries[i] = prandom32(&rnd);
46 static int interval_tree_test_init(void)
49 unsigned long results;
50 cycles_t time1, time2, time;
52 printk(KERN_ALERT "interval tree insert/remove");
54 prandom32_seed(&rnd, 3141592653589793238ULL);
59 for (i = 0; i < PERF_LOOPS; i++) {
60 for (j = 0; j < NODES; j++)
61 interval_tree_insert(nodes + j, &root);
62 for (j = 0; j < NODES; j++)
63 interval_tree_remove(nodes + j, &root);
69 time = div_u64(time, PERF_LOOPS);
70 printk(" -> %llu cycles\n", (unsigned long long)time);
72 printk(KERN_ALERT "interval tree search");
74 for (j = 0; j < NODES; j++)
75 interval_tree_insert(nodes + j, &root);
80 for (i = 0; i < SEARCH_LOOPS; i++)
81 for (j = 0; j < SEARCHES; j++)
82 results += search(queries[j], &root);
87 time = div_u64(time, SEARCH_LOOPS);
88 results = div_u64(results, SEARCH_LOOPS);
89 printk(" -> %llu cycles (%lu results)\n",
90 (unsigned long long)time, results);
92 return -EAGAIN; /* Fail will directly unload the module */
95 static void interval_tree_test_exit(void)
97 printk(KERN_ALERT "test exit\n");
100 module_init(interval_tree_test_init)
101 module_exit(interval_tree_test_exit)
103 MODULE_LICENSE("GPL");
104 MODULE_AUTHOR("Michel Lespinasse");
105 MODULE_DESCRIPTION("Interval Tree test");