Thomas Gleixner | 09c434b | 2019-05-19 13:08:20 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Michel Lespinasse | fff3fd8 | 2012-10-08 16:31:23 -0700 | [diff] [blame] | 2 | #include <linux/module.h> |
Davidlohr Bueso | a54dae0 | 2017-07-10 15:51:46 -0700 | [diff] [blame] | 3 | #include <linux/moduleparam.h> |
Michel Lespinasse | fff3fd8 | 2012-10-08 16:31:23 -0700 | [diff] [blame] | 4 | #include <linux/interval_tree.h> |
| 5 | #include <linux/random.h> |
Davidlohr Bueso | a54dae0 | 2017-07-10 15:51:46 -0700 | [diff] [blame] | 6 | #include <linux/slab.h> |
Michel Lespinasse | fff3fd8 | 2012-10-08 16:31:23 -0700 | [diff] [blame] | 7 | #include <asm/timex.h> |
| 8 | |
Davidlohr Bueso | a54dae0 | 2017-07-10 15:51:46 -0700 | [diff] [blame] | 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"); |
Davidlohr Bueso | 0b548e3 | 2017-11-17 15:28:27 -0800 | [diff] [blame] | 15 | __param(int, perf_loops, 1000, "Number of iterations modifying the tree"); |
Davidlohr Bueso | a54dae0 | 2017-07-10 15:51:46 -0700 | [diff] [blame] | 16 | |
| 17 | __param(int, nsearches, 100, "Number of searches to the interval tree"); |
Davidlohr Bueso | 0b548e3 | 2017-11-17 15:28:27 -0800 | [diff] [blame] | 18 | __param(int, search_loops, 1000, "Number of iterations searching the tree"); |
Davidlohr Bueso | c46ecce | 2017-07-10 15:51:52 -0700 | [diff] [blame] | 19 | __param(bool, search_all, false, "Searches will iterate all nodes in the tree"); |
Davidlohr Bueso | a54dae0 | 2017-07-10 15:51:46 -0700 | [diff] [blame] | 20 | |
Davidlohr Bueso | a8ec14d | 2017-07-10 15:51:49 -0700 | [diff] [blame] | 21 | __param(uint, max_endpoint, ~0, "Largest value for the interval's endpoint"); |
Michel Lespinasse | fff3fd8 | 2012-10-08 16:31:23 -0700 | [diff] [blame] | 22 | |
Davidlohr Bueso | f808c13 | 2017-09-08 16:15:08 -0700 | [diff] [blame] | 23 | static struct rb_root_cached root = RB_ROOT_CACHED; |
Davidlohr Bueso | a54dae0 | 2017-07-10 15:51:46 -0700 | [diff] [blame] | 24 | static struct interval_tree_node *nodes = NULL; |
| 25 | static u32 *queries = NULL; |
Michel Lespinasse | fff3fd8 | 2012-10-08 16:31:23 -0700 | [diff] [blame] | 26 | |
| 27 | static struct rnd_state rnd; |
| 28 | |
| 29 | static inline unsigned long |
Davidlohr Bueso | f808c13 | 2017-09-08 16:15:08 -0700 | [diff] [blame] | 30 | search(struct rb_root_cached *root, unsigned long start, unsigned long last) |
Michel Lespinasse | fff3fd8 | 2012-10-08 16:31:23 -0700 | [diff] [blame] | 31 | { |
| 32 | struct interval_tree_node *node; |
| 33 | unsigned long results = 0; |
| 34 | |
Davidlohr Bueso | c46ecce | 2017-07-10 15:51:52 -0700 | [diff] [blame] | 35 | for (node = interval_tree_iter_first(root, start, last); node; |
| 36 | node = interval_tree_iter_next(node, start, last)) |
Michel Lespinasse | fff3fd8 | 2012-10-08 16:31:23 -0700 | [diff] [blame] | 37 | results++; |
| 38 | return results; |
| 39 | } |
| 40 | |
| 41 | static void init(void) |
| 42 | { |
| 43 | int i; |
Davidlohr Bueso | a54dae0 | 2017-07-10 15:51:46 -0700 | [diff] [blame] | 44 | |
| 45 | for (i = 0; i < nnodes; i++) { |
Davidlohr Bueso | a8ec14d | 2017-07-10 15:51:49 -0700 | [diff] [blame] | 46 | 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 Lespinasse | fff3fd8 | 2012-10-08 16:31:23 -0700 | [diff] [blame] | 51 | } |
Davidlohr Bueso | a8ec14d | 2017-07-10 15:51:49 -0700 | [diff] [blame] | 52 | |
| 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 Bueso | a54dae0 | 2017-07-10 15:51:46 -0700 | [diff] [blame] | 58 | for (i = 0; i < nsearches; i++) |
Davidlohr Bueso | a8ec14d | 2017-07-10 15:51:49 -0700 | [diff] [blame] | 59 | queries[i] = (prandom_u32_state(&rnd) >> 4) % max_endpoint; |
Michel Lespinasse | fff3fd8 | 2012-10-08 16:31:23 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | static int interval_tree_test_init(void) |
| 63 | { |
| 64 | int i, j; |
| 65 | unsigned long results; |
| 66 | cycles_t time1, time2, time; |
| 67 | |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 68 | nodes = kmalloc_array(nnodes, sizeof(struct interval_tree_node), |
| 69 | GFP_KERNEL); |
Davidlohr Bueso | a54dae0 | 2017-07-10 15:51:46 -0700 | [diff] [blame] | 70 | if (!nodes) |
| 71 | return -ENOMEM; |
| 72 | |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 73 | queries = kmalloc_array(nsearches, sizeof(int), GFP_KERNEL); |
Davidlohr Bueso | a54dae0 | 2017-07-10 15:51:46 -0700 | [diff] [blame] | 74 | if (!queries) { |
| 75 | kfree(nodes); |
| 76 | return -ENOMEM; |
| 77 | } |
| 78 | |
Michel Lespinasse | fff3fd8 | 2012-10-08 16:31:23 -0700 | [diff] [blame] | 79 | printk(KERN_ALERT "interval tree insert/remove"); |
| 80 | |
Akinobu Mita | 496f2f9 | 2012-12-17 16:04:23 -0800 | [diff] [blame] | 81 | prandom_seed_state(&rnd, 3141592653589793238ULL); |
Michel Lespinasse | fff3fd8 | 2012-10-08 16:31:23 -0700 | [diff] [blame] | 82 | init(); |
| 83 | |
| 84 | time1 = get_cycles(); |
| 85 | |
Davidlohr Bueso | a54dae0 | 2017-07-10 15:51:46 -0700 | [diff] [blame] | 86 | for (i = 0; i < perf_loops; i++) { |
| 87 | for (j = 0; j < nnodes; j++) |
Michel Lespinasse | fff3fd8 | 2012-10-08 16:31:23 -0700 | [diff] [blame] | 88 | interval_tree_insert(nodes + j, &root); |
Davidlohr Bueso | a54dae0 | 2017-07-10 15:51:46 -0700 | [diff] [blame] | 89 | for (j = 0; j < nnodes; j++) |
Michel Lespinasse | fff3fd8 | 2012-10-08 16:31:23 -0700 | [diff] [blame] | 90 | interval_tree_remove(nodes + j, &root); |
| 91 | } |
| 92 | |
| 93 | time2 = get_cycles(); |
| 94 | time = time2 - time1; |
| 95 | |
Davidlohr Bueso | a54dae0 | 2017-07-10 15:51:46 -0700 | [diff] [blame] | 96 | time = div_u64(time, perf_loops); |
Michel Lespinasse | fff3fd8 | 2012-10-08 16:31:23 -0700 | [diff] [blame] | 97 | printk(" -> %llu cycles\n", (unsigned long long)time); |
| 98 | |
| 99 | printk(KERN_ALERT "interval tree search"); |
| 100 | |
Davidlohr Bueso | a54dae0 | 2017-07-10 15:51:46 -0700 | [diff] [blame] | 101 | for (j = 0; j < nnodes; j++) |
Michel Lespinasse | fff3fd8 | 2012-10-08 16:31:23 -0700 | [diff] [blame] | 102 | interval_tree_insert(nodes + j, &root); |
| 103 | |
| 104 | time1 = get_cycles(); |
| 105 | |
| 106 | results = 0; |
Davidlohr Bueso | a54dae0 | 2017-07-10 15:51:46 -0700 | [diff] [blame] | 107 | for (i = 0; i < search_loops; i++) |
Davidlohr Bueso | c46ecce | 2017-07-10 15:51:52 -0700 | [diff] [blame] | 108 | 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 Lespinasse | fff3fd8 | 2012-10-08 16:31:23 -0700 | [diff] [blame] | 114 | |
| 115 | time2 = get_cycles(); |
| 116 | time = time2 - time1; |
| 117 | |
Davidlohr Bueso | a54dae0 | 2017-07-10 15:51:46 -0700 | [diff] [blame] | 118 | time = div_u64(time, search_loops); |
| 119 | results = div_u64(results, search_loops); |
Michel Lespinasse | fff3fd8 | 2012-10-08 16:31:23 -0700 | [diff] [blame] | 120 | printk(" -> %llu cycles (%lu results)\n", |
| 121 | (unsigned long long)time, results); |
| 122 | |
Davidlohr Bueso | a54dae0 | 2017-07-10 15:51:46 -0700 | [diff] [blame] | 123 | kfree(queries); |
| 124 | kfree(nodes); |
| 125 | |
Michel Lespinasse | fff3fd8 | 2012-10-08 16:31:23 -0700 | [diff] [blame] | 126 | return -EAGAIN; /* Fail will directly unload the module */ |
| 127 | } |
| 128 | |
| 129 | static void interval_tree_test_exit(void) |
| 130 | { |
| 131 | printk(KERN_ALERT "test exit\n"); |
| 132 | } |
| 133 | |
| 134 | module_init(interval_tree_test_init) |
| 135 | module_exit(interval_tree_test_exit) |
| 136 | |
| 137 | MODULE_LICENSE("GPL"); |
| 138 | MODULE_AUTHOR("Michel Lespinasse"); |
| 139 | MODULE_DESCRIPTION("Interval Tree test"); |