blob: f37f4d44faa9075292f9acdb361dfabb098dc0dd [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Michel Lespinassefff3fd82012-10-08 16:31:23 -07002#include <linux/module.h>
Davidlohr Buesoa54dae02017-07-10 15:51:46 -07003#include <linux/moduleparam.h>
Michel Lespinassefff3fd82012-10-08 16:31:23 -07004#include <linux/interval_tree.h>
5#include <linux/random.h>
Davidlohr Buesoa54dae02017-07-10 15:51:46 -07006#include <linux/slab.h>
Michel Lespinassefff3fd82012-10-08 16:31:23 -07007#include <asm/timex.h>
8
Davidlohr Buesoa54dae02017-07-10 15:51:46 -07009#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");
Davidlohr Bueso0b548e32017-11-17 15:28:27 -080015__param(int, perf_loops, 1000, "Number of iterations modifying the tree");
Davidlohr Buesoa54dae02017-07-10 15:51:46 -070016
17__param(int, nsearches, 100, "Number of searches to the interval tree");
Davidlohr Bueso0b548e32017-11-17 15:28:27 -080018__param(int, search_loops, 1000, "Number of iterations searching the tree");
Davidlohr Buesoc46ecce2017-07-10 15:51:52 -070019__param(bool, search_all, false, "Searches will iterate all nodes in the tree");
Davidlohr Buesoa54dae02017-07-10 15:51:46 -070020
Davidlohr Buesoa8ec14d2017-07-10 15:51:49 -070021__param(uint, max_endpoint, ~0, "Largest value for the interval's endpoint");
Michel Lespinassefff3fd82012-10-08 16:31:23 -070022
Davidlohr Buesof808c132017-09-08 16:15:08 -070023static struct rb_root_cached root = RB_ROOT_CACHED;
Davidlohr Buesoa54dae02017-07-10 15:51:46 -070024static struct interval_tree_node *nodes = NULL;
25static u32 *queries = NULL;
Michel Lespinassefff3fd82012-10-08 16:31:23 -070026
27static struct rnd_state rnd;
28
29static inline unsigned long
Davidlohr Buesof808c132017-09-08 16:15:08 -070030search(struct rb_root_cached *root, unsigned long start, unsigned long last)
Michel Lespinassefff3fd82012-10-08 16:31:23 -070031{
32 struct interval_tree_node *node;
33 unsigned long results = 0;
34
Davidlohr Buesoc46ecce2017-07-10 15:51:52 -070035 for (node = interval_tree_iter_first(root, start, last); node;
36 node = interval_tree_iter_next(node, start, last))
Michel Lespinassefff3fd82012-10-08 16:31:23 -070037 results++;
38 return results;
39}
40
41static void init(void)
42{
43 int i;
Davidlohr Buesoa54dae02017-07-10 15:51:46 -070044
45 for (i = 0; i < nnodes; i++) {
Davidlohr Buesoa8ec14d2017-07-10 15:51:49 -070046 u32 b = (prandom_u32_state(&rnd) >> 4) % max_endpoint;
47 u32 a = (prandom_u32_state(&rnd) >> 4) % b;
48
49 nodes[i].start = a;
50 nodes[i].last = b;
Michel Lespinassefff3fd82012-10-08 16:31:23 -070051 }
Davidlohr Buesoa8ec14d2017-07-10 15:51:49 -070052
53 /*
54 * Limit the search scope to what the user defined.
55 * Otherwise we are merely measuring empty walks,
56 * which is pointless.
57 */
Davidlohr Buesoa54dae02017-07-10 15:51:46 -070058 for (i = 0; i < nsearches; i++)
Davidlohr Buesoa8ec14d2017-07-10 15:51:49 -070059 queries[i] = (prandom_u32_state(&rnd) >> 4) % max_endpoint;
Michel Lespinassefff3fd82012-10-08 16:31:23 -070060}
61
62static int interval_tree_test_init(void)
63{
64 int i, j;
65 unsigned long results;
66 cycles_t time1, time2, time;
67
Kees Cook6da2ec52018-06-12 13:55:00 -070068 nodes = kmalloc_array(nnodes, sizeof(struct interval_tree_node),
69 GFP_KERNEL);
Davidlohr Buesoa54dae02017-07-10 15:51:46 -070070 if (!nodes)
71 return -ENOMEM;
72
Kees Cook6da2ec52018-06-12 13:55:00 -070073 queries = kmalloc_array(nsearches, sizeof(int), GFP_KERNEL);
Davidlohr Buesoa54dae02017-07-10 15:51:46 -070074 if (!queries) {
75 kfree(nodes);
76 return -ENOMEM;
77 }
78
Michel Lespinassefff3fd82012-10-08 16:31:23 -070079 printk(KERN_ALERT "interval tree insert/remove");
80
Akinobu Mita496f2f92012-12-17 16:04:23 -080081 prandom_seed_state(&rnd, 3141592653589793238ULL);
Michel Lespinassefff3fd82012-10-08 16:31:23 -070082 init();
83
84 time1 = get_cycles();
85
Davidlohr Buesoa54dae02017-07-10 15:51:46 -070086 for (i = 0; i < perf_loops; i++) {
87 for (j = 0; j < nnodes; j++)
Michel Lespinassefff3fd82012-10-08 16:31:23 -070088 interval_tree_insert(nodes + j, &root);
Davidlohr Buesoa54dae02017-07-10 15:51:46 -070089 for (j = 0; j < nnodes; j++)
Michel Lespinassefff3fd82012-10-08 16:31:23 -070090 interval_tree_remove(nodes + j, &root);
91 }
92
93 time2 = get_cycles();
94 time = time2 - time1;
95
Davidlohr Buesoa54dae02017-07-10 15:51:46 -070096 time = div_u64(time, perf_loops);
Michel Lespinassefff3fd82012-10-08 16:31:23 -070097 printk(" -> %llu cycles\n", (unsigned long long)time);
98
99 printk(KERN_ALERT "interval tree search");
100
Davidlohr Buesoa54dae02017-07-10 15:51:46 -0700101 for (j = 0; j < nnodes; j++)
Michel Lespinassefff3fd82012-10-08 16:31:23 -0700102 interval_tree_insert(nodes + j, &root);
103
104 time1 = get_cycles();
105
106 results = 0;
Davidlohr Buesoa54dae02017-07-10 15:51:46 -0700107 for (i = 0; i < search_loops; i++)
Davidlohr Buesoc46ecce2017-07-10 15:51:52 -0700108 for (j = 0; j < nsearches; j++) {
109 unsigned long start = search_all ? 0 : queries[j];
110 unsigned long last = search_all ? max_endpoint : queries[j];
111
112 results += search(&root, start, last);
113 }
Michel Lespinassefff3fd82012-10-08 16:31:23 -0700114
115 time2 = get_cycles();
116 time = time2 - time1;
117
Davidlohr Buesoa54dae02017-07-10 15:51:46 -0700118 time = div_u64(time, search_loops);
119 results = div_u64(results, search_loops);
Michel Lespinassefff3fd82012-10-08 16:31:23 -0700120 printk(" -> %llu cycles (%lu results)\n",
121 (unsigned long long)time, results);
122
Davidlohr Buesoa54dae02017-07-10 15:51:46 -0700123 kfree(queries);
124 kfree(nodes);
125
Michel Lespinassefff3fd82012-10-08 16:31:23 -0700126 return -EAGAIN; /* Fail will directly unload the module */
127}
128
129static void interval_tree_test_exit(void)
130{
131 printk(KERN_ALERT "test exit\n");
132}
133
134module_init(interval_tree_test_init)
135module_exit(interval_tree_test_exit)
136
137MODULE_LICENSE("GPL");
138MODULE_AUTHOR("Michel Lespinasse");
139MODULE_DESCRIPTION("Interval Tree test");