xref: /linux/lib/interval_tree_test.c (revision 16b1936ae6d1858252413bf4bae8bcf247eb4b4c)
1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/module.h>
3 #include <linux/moduleparam.h>
4 #include <linux/interval_tree.h>
5 #include <linux/prandom.h>
6 #include <linux/slab.h>
7 #include <asm/timex.h>
8 
9 #define __param(type, name, init, msg)		\
10 	static type name = init;		\
11 	module_param(name, type, 0444);		\
12 	MODULE_PARM_DESC(name, msg);
13 
14 __param(int, nnodes, 100, "Number of nodes in the interval tree");
15 __param(int, perf_loops, 1000, "Number of iterations modifying the tree");
16 
17 __param(int, nsearches, 100, "Number of searches to the interval tree");
18 __param(int, search_loops, 1000, "Number of iterations searching the tree");
19 __param(bool, search_all, false, "Searches will iterate all nodes in the tree");
20 
21 __param(uint, max_endpoint, ~0, "Largest value for the interval's endpoint");
22 __param(ullong, seed, 3141592653589793238ULL, "Random seed");
23 
24 static struct rb_root_cached root = RB_ROOT_CACHED;
25 static struct interval_tree_node *nodes = NULL;
26 static u32 *queries = NULL;
27 
28 static struct rnd_state rnd;
29 
30 static inline unsigned long
31 search(struct rb_root_cached *root, unsigned long start, unsigned long last)
32 {
33 	struct interval_tree_node *node;
34 	unsigned long results = 0;
35 
36 	for (node = interval_tree_iter_first(root, start, last); node;
37 	     node = interval_tree_iter_next(node, start, last))
38 		results++;
39 	return results;
40 }
41 
42 static void init(void)
43 {
44 	int i;
45 
46 	for (i = 0; i < nnodes; i++) {
47 		u32 b = (prandom_u32_state(&rnd) >> 4) % max_endpoint;
48 		u32 a = (prandom_u32_state(&rnd) >> 4) % b;
49 
50 		nodes[i].start = a;
51 		nodes[i].last = b;
52 	}
53 
54 	/*
55 	 * Limit the search scope to what the user defined.
56 	 * Otherwise we are merely measuring empty walks,
57 	 * which is pointless.
58 	 */
59 	for (i = 0; i < nsearches; i++)
60 		queries[i] = (prandom_u32_state(&rnd) >> 4) % max_endpoint;
61 }
62 
63 static int basic_check(void)
64 {
65 	int i, j;
66 	cycles_t time1, time2, time;
67 
68 	printk(KERN_ALERT "interval tree insert/remove");
69 
70 	init();
71 
72 	time1 = get_cycles();
73 
74 	for (i = 0; i < perf_loops; i++) {
75 		for (j = 0; j < nnodes; j++)
76 			interval_tree_insert(nodes + j, &root);
77 		for (j = 0; j < nnodes; j++)
78 			interval_tree_remove(nodes + j, &root);
79 	}
80 
81 	time2 = get_cycles();
82 	time = time2 - time1;
83 
84 	time = div_u64(time, perf_loops);
85 	printk(" -> %llu cycles\n", (unsigned long long)time);
86 
87 	return 0;
88 }
89 
90 static int search_check(void)
91 {
92 	int i, j;
93 	unsigned long results;
94 	cycles_t time1, time2, time;
95 
96 	printk(KERN_ALERT "interval tree search");
97 
98 	init();
99 
100 	for (j = 0; j < nnodes; j++)
101 		interval_tree_insert(nodes + j, &root);
102 
103 	time1 = get_cycles();
104 
105 	results = 0;
106 	for (i = 0; i < search_loops; i++)
107 		for (j = 0; j < nsearches; j++) {
108 			unsigned long start = search_all ? 0 : queries[j];
109 			unsigned long last = search_all ? max_endpoint : queries[j];
110 
111 			results += search(&root, start, last);
112 		}
113 
114 	time2 = get_cycles();
115 	time = time2 - time1;
116 
117 	time = div_u64(time, search_loops);
118 	results = div_u64(results, search_loops);
119 	printk(" -> %llu cycles (%lu results)\n",
120 	       (unsigned long long)time, results);
121 
122 	for (j = 0; j < nnodes; j++)
123 		interval_tree_remove(nodes + j, &root);
124 
125 	return 0;
126 }
127 
128 static int interval_tree_test_init(void)
129 {
130 	nodes = kmalloc_array(nnodes, sizeof(struct interval_tree_node),
131 			      GFP_KERNEL);
132 	if (!nodes)
133 		return -ENOMEM;
134 
135 	queries = kmalloc_array(nsearches, sizeof(int), GFP_KERNEL);
136 	if (!queries) {
137 		kfree(nodes);
138 		return -ENOMEM;
139 	}
140 
141 	prandom_seed_state(&rnd, seed);
142 
143 	basic_check();
144 	search_check();
145 
146 	kfree(queries);
147 	kfree(nodes);
148 
149 	return -EAGAIN; /* Fail will directly unload the module */
150 }
151 
152 static void interval_tree_test_exit(void)
153 {
154 	printk(KERN_ALERT "test exit\n");
155 }
156 
157 module_init(interval_tree_test_init)
158 module_exit(interval_tree_test_exit)
159 
160 MODULE_LICENSE("GPL");
161 MODULE_AUTHOR("Michel Lespinasse");
162 MODULE_DESCRIPTION("Interval Tree test");
163