Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 #include <linux/module.h>
0003 #include <linux/moduleparam.h>
0004 #include <linux/interval_tree.h>
0005 #include <linux/random.h>
0006 #include <linux/slab.h>
0007 #include <asm/timex.h>
0008 
0009 #define __param(type, name, init, msg)      \
0010     static type name = init;        \
0011     module_param(name, type, 0444);     \
0012     MODULE_PARM_DESC(name, msg);
0013 
0014 __param(int, nnodes, 100, "Number of nodes in the interval tree");
0015 __param(int, perf_loops, 1000, "Number of iterations modifying the tree");
0016 
0017 __param(int, nsearches, 100, "Number of searches to the interval tree");
0018 __param(int, search_loops, 1000, "Number of iterations searching the tree");
0019 __param(bool, search_all, false, "Searches will iterate all nodes in the tree");
0020 
0021 __param(uint, max_endpoint, ~0, "Largest value for the interval's endpoint");
0022 
0023 static struct rb_root_cached root = RB_ROOT_CACHED;
0024 static struct interval_tree_node *nodes = NULL;
0025 static u32 *queries = NULL;
0026 
0027 static struct rnd_state rnd;
0028 
0029 static inline unsigned long
0030 search(struct rb_root_cached *root, unsigned long start, unsigned long last)
0031 {
0032     struct interval_tree_node *node;
0033     unsigned long results = 0;
0034 
0035     for (node = interval_tree_iter_first(root, start, last); node;
0036          node = interval_tree_iter_next(node, start, last))
0037         results++;
0038     return results;
0039 }
0040 
0041 static void init(void)
0042 {
0043     int i;
0044 
0045     for (i = 0; i < nnodes; i++) {
0046         u32 b = (prandom_u32_state(&rnd) >> 4) % max_endpoint;
0047         u32 a = (prandom_u32_state(&rnd) >> 4) % b;
0048 
0049         nodes[i].start = a;
0050         nodes[i].last = b;
0051     }
0052 
0053     /*
0054      * Limit the search scope to what the user defined.
0055      * Otherwise we are merely measuring empty walks,
0056      * which is pointless.
0057      */
0058     for (i = 0; i < nsearches; i++)
0059         queries[i] = (prandom_u32_state(&rnd) >> 4) % max_endpoint;
0060 }
0061 
0062 static int interval_tree_test_init(void)
0063 {
0064     int i, j;
0065     unsigned long results;
0066     cycles_t time1, time2, time;
0067 
0068     nodes = kmalloc_array(nnodes, sizeof(struct interval_tree_node),
0069                   GFP_KERNEL);
0070     if (!nodes)
0071         return -ENOMEM;
0072 
0073     queries = kmalloc_array(nsearches, sizeof(int), GFP_KERNEL);
0074     if (!queries) {
0075         kfree(nodes);
0076         return -ENOMEM;
0077     }
0078 
0079     printk(KERN_ALERT "interval tree insert/remove");
0080 
0081     prandom_seed_state(&rnd, 3141592653589793238ULL);
0082     init();
0083 
0084     time1 = get_cycles();
0085 
0086     for (i = 0; i < perf_loops; i++) {
0087         for (j = 0; j < nnodes; j++)
0088             interval_tree_insert(nodes + j, &root);
0089         for (j = 0; j < nnodes; j++)
0090             interval_tree_remove(nodes + j, &root);
0091     }
0092 
0093     time2 = get_cycles();
0094     time = time2 - time1;
0095 
0096     time = div_u64(time, perf_loops);
0097     printk(" -> %llu cycles\n", (unsigned long long)time);
0098 
0099     printk(KERN_ALERT "interval tree search");
0100 
0101     for (j = 0; j < nnodes; j++)
0102         interval_tree_insert(nodes + j, &root);
0103 
0104     time1 = get_cycles();
0105 
0106     results = 0;
0107     for (i = 0; i < search_loops; i++)
0108         for (j = 0; j < nsearches; j++) {
0109             unsigned long start = search_all ? 0 : queries[j];
0110             unsigned long last = search_all ? max_endpoint : queries[j];
0111 
0112             results += search(&root, start, last);
0113         }
0114 
0115     time2 = get_cycles();
0116     time = time2 - time1;
0117 
0118     time = div_u64(time, search_loops);
0119     results = div_u64(results, search_loops);
0120     printk(" -> %llu cycles (%lu results)\n",
0121            (unsigned long long)time, results);
0122 
0123     kfree(queries);
0124     kfree(nodes);
0125 
0126     return -EAGAIN; /* Fail will directly unload the module */
0127 }
0128 
0129 static void interval_tree_test_exit(void)
0130 {
0131     printk(KERN_ALERT "test exit\n");
0132 }
0133 
0134 module_init(interval_tree_test_init)
0135 module_exit(interval_tree_test_exit)
0136 
0137 MODULE_LICENSE("GPL");
0138 MODULE_AUTHOR("Michel Lespinasse");
0139 MODULE_DESCRIPTION("Interval Tree test");