xref: /openbmc/linux/tools/testing/radix-tree/benchmark.c (revision 2956c6644bfd9aab9f6b21a12e1bd75876d9dd73)
1 /*
2  * benchmark.c:
3  * Author: Konstantin Khlebnikov <koct9i@gmail.com>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU General Public License,
7  * version 2, as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  */
14 #include <linux/radix-tree.h>
15 #include <linux/slab.h>
16 #include <linux/errno.h>
17 #include <time.h>
18 #include "test.h"
19 
20 #define for_each_index(i, base, order) \
21 	        for (i = base; i < base + (1 << order); i++)
22 
23 #define NSEC_PER_SEC	1000000000L
24 
25 static long long benchmark_iter(struct radix_tree_root *root, bool tagged)
26 {
27 	volatile unsigned long sink = 0;
28 	struct radix_tree_iter iter;
29 	struct timespec start, finish;
30 	long long nsec;
31 	int l, loops = 1;
32 	void **slot;
33 
34 #ifdef BENCHMARK
35 again:
36 #endif
37 	clock_gettime(CLOCK_MONOTONIC, &start);
38 	for (l = 0; l < loops; l++) {
39 		if (tagged) {
40 			radix_tree_for_each_tagged(slot, root, &iter, 0, 0)
41 				sink ^= (unsigned long)slot;
42 		} else {
43 			radix_tree_for_each_slot(slot, root, &iter, 0)
44 				sink ^= (unsigned long)slot;
45 		}
46 	}
47 	clock_gettime(CLOCK_MONOTONIC, &finish);
48 
49 	nsec = (finish.tv_sec - start.tv_sec) * NSEC_PER_SEC +
50 	       (finish.tv_nsec - start.tv_nsec);
51 
52 #ifdef BENCHMARK
53 	if (loops == 1 && nsec * 5 < NSEC_PER_SEC) {
54 		loops = NSEC_PER_SEC / nsec / 4 + 1;
55 		goto again;
56 	}
57 #endif
58 
59 	nsec /= loops;
60 	return nsec;
61 }
62 
63 static void benchmark_insert(struct radix_tree_root *root,
64 			     unsigned long size, unsigned long step, int order)
65 {
66 	struct timespec start, finish;
67 	unsigned long index;
68 	long long nsec;
69 
70 	clock_gettime(CLOCK_MONOTONIC, &start);
71 
72 	for (index = 0 ; index < size ; index += step)
73 		item_insert_order(root, index, order);
74 
75 	clock_gettime(CLOCK_MONOTONIC, &finish);
76 
77 	nsec = (finish.tv_sec - start.tv_sec) * NSEC_PER_SEC +
78 	       (finish.tv_nsec - start.tv_nsec);
79 
80 	printv(2, "Size: %8ld, step: %8ld, order: %d, insertion: %15lld ns\n",
81 		size, step, order, nsec);
82 }
83 
84 static void benchmark_tagging(struct radix_tree_root *root,
85 			     unsigned long size, unsigned long step, int order)
86 {
87 	struct timespec start, finish;
88 	unsigned long index;
89 	long long nsec;
90 
91 	clock_gettime(CLOCK_MONOTONIC, &start);
92 
93 	for (index = 0 ; index < size ; index += step)
94 		radix_tree_tag_set(root, index, 0);
95 
96 	clock_gettime(CLOCK_MONOTONIC, &finish);
97 
98 	nsec = (finish.tv_sec - start.tv_sec) * NSEC_PER_SEC +
99 	       (finish.tv_nsec - start.tv_nsec);
100 
101 	printv(2, "Size: %8ld, step: %8ld, order: %d, tagging: %17lld ns\n",
102 		size, step, order, nsec);
103 }
104 
105 static void benchmark_delete(struct radix_tree_root *root,
106 			     unsigned long size, unsigned long step, int order)
107 {
108 	struct timespec start, finish;
109 	unsigned long index, i;
110 	long long nsec;
111 
112 	clock_gettime(CLOCK_MONOTONIC, &start);
113 
114 	for (index = 0 ; index < size ; index += step)
115 		for_each_index(i, index, order)
116 			item_delete(root, i);
117 
118 	clock_gettime(CLOCK_MONOTONIC, &finish);
119 
120 	nsec = (finish.tv_sec - start.tv_sec) * NSEC_PER_SEC +
121 	       (finish.tv_nsec - start.tv_nsec);
122 
123 	printv(2, "Size: %8ld, step: %8ld, order: %d, deletion: %16lld ns\n",
124 		size, step, order, nsec);
125 }
126 
127 static void benchmark_size(unsigned long size, unsigned long step, int order)
128 {
129 	RADIX_TREE(tree, GFP_KERNEL);
130 	long long normal, tagged;
131 
132 	benchmark_insert(&tree, size, step, order);
133 	benchmark_tagging(&tree, size, step, order);
134 
135 	tagged = benchmark_iter(&tree, true);
136 	normal = benchmark_iter(&tree, false);
137 
138 	printv(2, "Size: %8ld, step: %8ld, order: %d, tagged iteration: %8lld ns\n",
139 		size, step, order, tagged);
140 	printv(2, "Size: %8ld, step: %8ld, order: %d, normal iteration: %8lld ns\n",
141 		size, step, order, normal);
142 
143 	benchmark_delete(&tree, size, step, order);
144 
145 	item_kill_tree(&tree);
146 	rcu_barrier();
147 }
148 
149 void benchmark(void)
150 {
151 	unsigned long size[] = {1 << 10, 1 << 20, 0};
152 	unsigned long step[] = {1, 2, 7, 15, 63, 64, 65,
153 				128, 256, 512, 12345, 0};
154 	int c, s;
155 
156 	printv(1, "starting benchmarks\n");
157 	printv(1, "RADIX_TREE_MAP_SHIFT = %d\n", RADIX_TREE_MAP_SHIFT);
158 
159 	for (c = 0; size[c]; c++)
160 		for (s = 0; step[s]; s++)
161 			benchmark_size(size[c], step[s], 0);
162 
163 	for (c = 0; size[c]; c++)
164 		for (s = 0; step[s]; s++)
165 			benchmark_size(size[c], step[s] << 9, 9);
166 }
167