interval_tree_test.c 3.38 KB
Newer Older
1
#include <linux/module.h>
2
#include <linux/moduleparam.h>
3 4
#include <linux/interval_tree.h>
#include <linux/random.h>
5
#include <linux/slab.h>
6 7
#include <asm/timex.h>

8 9 10 11 12 13
#define __param(type, name, init, msg)		\
	static type name = init;		\
	module_param(name, type, 0444);		\
	MODULE_PARM_DESC(name, msg);

__param(int, nnodes, 100, "Number of nodes in the interval tree");
14
__param(int, perf_loops, 1000, "Number of iterations modifying the tree");
15 16

__param(int, nsearches, 100, "Number of searches to the interval tree");
17
__param(int, search_loops, 1000, "Number of iterations searching the tree");
18
__param(bool, search_all, false, "Searches will iterate all nodes in the tree");
19

20
__param(uint, max_endpoint, ~0, "Largest value for the interval's endpoint");
21

22
static struct rb_root_cached root = RB_ROOT_CACHED;
23 24
static struct interval_tree_node *nodes = NULL;
static u32 *queries = NULL;
25 26 27 28

static struct rnd_state rnd;

static inline unsigned long
29
search(struct rb_root_cached *root, unsigned long start, unsigned long last)
30 31 32 33
{
	struct interval_tree_node *node;
	unsigned long results = 0;

34 35
	for (node = interval_tree_iter_first(root, start, last); node;
	     node = interval_tree_iter_next(node, start, last))
36 37 38 39 40 41 42
		results++;
	return results;
}

static void init(void)
{
	int i;
43 44

	for (i = 0; i < nnodes; i++) {
45 46 47 48 49
		u32 b = (prandom_u32_state(&rnd) >> 4) % max_endpoint;
		u32 a = (prandom_u32_state(&rnd) >> 4) % b;

		nodes[i].start = a;
		nodes[i].last = b;
50
	}
51 52 53 54 55 56

	/*
	 * Limit the search scope to what the user defined.
	 * Otherwise we are merely measuring empty walks,
	 * which is pointless.
	 */
57
	for (i = 0; i < nsearches; i++)
58
		queries[i] = (prandom_u32_state(&rnd) >> 4) % max_endpoint;
59 60 61 62 63 64 65 66
}

static int interval_tree_test_init(void)
{
	int i, j;
	unsigned long results;
	cycles_t time1, time2, time;

67 68
	nodes = kmalloc_array(nnodes, sizeof(struct interval_tree_node),
			      GFP_KERNEL);
69 70 71
	if (!nodes)
		return -ENOMEM;

72
	queries = kmalloc_array(nsearches, sizeof(int), GFP_KERNEL);
73 74 75 76 77
	if (!queries) {
		kfree(nodes);
		return -ENOMEM;
	}

78 79
	printk(KERN_ALERT "interval tree insert/remove");

80
	prandom_seed_state(&rnd, 3141592653589793238ULL);
81 82 83 84
	init();

	time1 = get_cycles();

85 86
	for (i = 0; i < perf_loops; i++) {
		for (j = 0; j < nnodes; j++)
87
			interval_tree_insert(nodes + j, &root);
88
		for (j = 0; j < nnodes; j++)
89 90 91 92 93 94
			interval_tree_remove(nodes + j, &root);
	}

	time2 = get_cycles();
	time = time2 - time1;

95
	time = div_u64(time, perf_loops);
96 97 98 99
	printk(" -> %llu cycles\n", (unsigned long long)time);

	printk(KERN_ALERT "interval tree search");

100
	for (j = 0; j < nnodes; j++)
101 102 103 104 105
		interval_tree_insert(nodes + j, &root);

	time1 = get_cycles();

	results = 0;
106
	for (i = 0; i < search_loops; i++)
107 108 109 110 111 112
		for (j = 0; j < nsearches; j++) {
			unsigned long start = search_all ? 0 : queries[j];
			unsigned long last = search_all ? max_endpoint : queries[j];

			results += search(&root, start, last);
		}
113 114 115 116

	time2 = get_cycles();
	time = time2 - time1;

117 118
	time = div_u64(time, search_loops);
	results = div_u64(results, search_loops);
119 120 121
	printk(" -> %llu cycles (%lu results)\n",
	       (unsigned long long)time, results);

122 123 124
	kfree(queries);
	kfree(nodes);

125 126 127 128 129 130 131 132 133 134 135 136 137 138
	return -EAGAIN; /* Fail will directly unload the module */
}

static void interval_tree_test_exit(void)
{
	printk(KERN_ALERT "test exit\n");
}

module_init(interval_tree_test_init)
module_exit(interval_tree_test_exit)

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Michel Lespinasse");
MODULE_DESCRIPTION("Interval Tree test");