Merge branch 'dev' of https://github.com/smartxworks/fio
[fio.git] / lib / prio_tree.c
CommitLineData
b65c7ec4
JA
1/*
2 * lib/prio_tree.c - priority search tree
3 *
4 * Copyright (C) 2004, Rajesh Venkatasubramanian <vrajesh@umich.edu>
5 *
6 * This file is released under the GPL v2.
7 *
8 * Based on the radix priority search tree proposed by Edward M. McCreight
9 * SIAM Journal of Computing, vol. 14, no.2, pages 257-276, May 1985
10 *
11 * 02Feb2004 Initial version
12 */
13
3d2d14bc 14#include <assert.h>
b65c7ec4
JA
15#include <stdlib.h>
16#include <limits.h>
1f6ab977
TK
17
18#include "../compiler/compiler.h"
b65c7ec4
JA
19#include "prio_tree.h"
20
1f6ab977
TK
21#define ARRAY_SIZE(x) (sizeof((x)) / (sizeof((x)[0])))
22
b65c7ec4
JA
23/*
24 * A clever mix of heap and radix trees forms a radix priority search tree (PST)
25 * which is useful for storing intervals, e.g, we can consider a vma as a closed
26 * interval of file pages [offset_begin, offset_end], and store all vmas that
27 * map a file in a PST. Then, using the PST, we can answer a stabbing query,
28 * i.e., selecting a set of stored intervals (vmas) that overlap with (map) a
29 * given input interval X (a set of consecutive file pages), in "O(log n + m)"
30 * time where 'log n' is the height of the PST, and 'm' is the number of stored
31 * intervals (vmas) that overlap (map) with the input interval X (the set of
32 * consecutive file pages).
33 *
34 * In our implementation, we store closed intervals of the form [radix_index,
35 * heap_index]. We assume that always radix_index <= heap_index. McCreight's PST
36 * is designed for storing intervals with unique radix indices, i.e., each
37 * interval have different radix_index. However, this limitation can be easily
38 * overcome by using the size, i.e., heap_index - radix_index, as part of the
39 * index, so we index the tree using [(radix_index,size), heap_index].
40 *
41 * When the above-mentioned indexing scheme is used, theoretically, in a 32 bit
42 * machine, the maximum height of a PST can be 64. We can use a balanced version
43 * of the priority search tree to optimize the tree height, but the balanced
44 * tree proposed by McCreight is too complex and memory-hungry for our purpose.
45 */
46
47static void get_index(const struct prio_tree_node *node,
48 unsigned long *radix, unsigned long *heap)
49{
50 *radix = node->start;
51 *heap = node->last;
52}
53
54static unsigned long index_bits_to_maxindex[BITS_PER_LONG];
55
10aa136b 56static void fio_init prio_tree_init(void)
b65c7ec4
JA
57{
58 unsigned int i;
59
60 for (i = 0; i < ARRAY_SIZE(index_bits_to_maxindex) - 1; i++)
61 index_bits_to_maxindex[i] = (1UL << (i + 1)) - 1;
62 index_bits_to_maxindex[ARRAY_SIZE(index_bits_to_maxindex) - 1] = ~0UL;
63}
64
65/*
66 * Maximum heap_index that can be stored in a PST with index_bits bits
67 */
68static inline unsigned long prio_tree_maxindex(unsigned int bits)
69{
70 return index_bits_to_maxindex[bits - 1];
71}
72
73/*
74 * Extend a priority search tree so that it can store a node with heap_index
75 * max_heap_index. In the worst case, this algorithm takes O((log n)^2).
76 * However, this function is used rarely and the common case performance is
77 * not bad.
78 */
79static struct prio_tree_node *prio_tree_expand(struct prio_tree_root *root,
80 struct prio_tree_node *node, unsigned long max_heap_index)
81{
82 struct prio_tree_node *first = NULL, *prev, *last = NULL;
83
84 if (max_heap_index > prio_tree_maxindex(root->index_bits))
85 root->index_bits++;
86
87 while (max_heap_index > prio_tree_maxindex(root->index_bits)) {
88 root->index_bits++;
89
90 if (prio_tree_empty(root))
91 continue;
92
93 if (first == NULL) {
94 first = root->prio_tree_node;
95 prio_tree_remove(root, root->prio_tree_node);
96 INIT_PRIO_TREE_NODE(first);
97 last = first;
98 } else {
99 prev = last;
100 last = root->prio_tree_node;
101 prio_tree_remove(root, root->prio_tree_node);
102 INIT_PRIO_TREE_NODE(last);
103 prev->left = last;
104 last->parent = prev;
105 }
106 }
107
108 INIT_PRIO_TREE_NODE(node);
109
110 if (first) {
111 node->left = first;
112 first->parent = node;
113 } else
114 last = node;
115
116 if (!prio_tree_empty(root)) {
117 last->left = root->prio_tree_node;
118 last->left->parent = last;
119 }
120
121 root->prio_tree_node = node;
122 return node;
123}
124
125/*
126 * Replace a prio_tree_node with a new node and return the old node
127 */
128struct prio_tree_node *prio_tree_replace(struct prio_tree_root *root,
129 struct prio_tree_node *old, struct prio_tree_node *node)
130{
131 INIT_PRIO_TREE_NODE(node);
132
133 if (prio_tree_root(old)) {
134 assert(root->prio_tree_node == old);
135 /*
136 * We can reduce root->index_bits here. However, it is complex
137 * and does not help much to improve performance (IMO).
138 */
139 node->parent = node;
140 root->prio_tree_node = node;
141 } else {
142 node->parent = old->parent;
143 if (old->parent->left == old)
144 old->parent->left = node;
145 else
146 old->parent->right = node;
147 }
148
149 if (!prio_tree_left_empty(old)) {
150 node->left = old->left;
151 old->left->parent = node;
152 }
153
154 if (!prio_tree_right_empty(old)) {
155 node->right = old->right;
156 old->right->parent = node;
157 }
158
159 return old;
160}
161
162/*
163 * Insert a prio_tree_node @node into a radix priority search tree @root. The
164 * algorithm typically takes O(log n) time where 'log n' is the number of bits
165 * required to represent the maximum heap_index. In the worst case, the algo
166 * can take O((log n)^2) - check prio_tree_expand.
167 *
168 * If a prior node with same radix_index and heap_index is already found in
169 * the tree, then returns the address of the prior node. Otherwise, inserts
170 * @node into the tree and returns @node.
171 */
172struct prio_tree_node *prio_tree_insert(struct prio_tree_root *root,
173 struct prio_tree_node *node)
174{
175 struct prio_tree_node *cur, *res = node;
176 unsigned long radix_index, heap_index;
177 unsigned long r_index, h_index, index, mask;
178 int size_flag = 0;
179
180 get_index(node, &radix_index, &heap_index);
181
182 if (prio_tree_empty(root) ||
183 heap_index > prio_tree_maxindex(root->index_bits))
184 return prio_tree_expand(root, node, heap_index);
185
186 cur = root->prio_tree_node;
187 mask = 1UL << (root->index_bits - 1);
188
189 while (mask) {
190 get_index(cur, &r_index, &h_index);
191
192 if (r_index == radix_index && h_index == heap_index)
193 return cur;
194
195 if (h_index < heap_index ||
196 (h_index == heap_index && r_index > radix_index)) {
197 struct prio_tree_node *tmp = node;
198 node = prio_tree_replace(root, cur, node);
199 cur = tmp;
200 /* swap indices */
201 index = r_index;
202 r_index = radix_index;
203 radix_index = index;
204 index = h_index;
205 h_index = heap_index;
206 heap_index = index;
207 }
208
209 if (size_flag)
210 index = heap_index - radix_index;
211 else
212 index = radix_index;
213
214 if (index & mask) {
215 if (prio_tree_right_empty(cur)) {
216 INIT_PRIO_TREE_NODE(node);
217 cur->right = node;
218 node->parent = cur;
219 return res;
220 } else
221 cur = cur->right;
222 } else {
223 if (prio_tree_left_empty(cur)) {
224 INIT_PRIO_TREE_NODE(node);
225 cur->left = node;
226 node->parent = cur;
227 return res;
228 } else
229 cur = cur->left;
230 }
231
232 mask >>= 1;
233
234 if (!mask) {
235 mask = 1UL << (BITS_PER_LONG - 1);
236 size_flag = 1;
237 }
238 }
239 /* Should not reach here */
240 assert(0);
241 return NULL;
242}
243
244/*
245 * Remove a prio_tree_node @node from a radix priority search tree @root. The
246 * algorithm takes O(log n) time where 'log n' is the number of bits required
247 * to represent the maximum heap_index.
248 */
249void prio_tree_remove(struct prio_tree_root *root, struct prio_tree_node *node)
250{
251 struct prio_tree_node *cur;
252 unsigned long r_index, h_index_right, h_index_left;
253
254 cur = node;
255
256 while (!prio_tree_left_empty(cur) || !prio_tree_right_empty(cur)) {
257 if (!prio_tree_left_empty(cur))
258 get_index(cur->left, &r_index, &h_index_left);
259 else {
260 cur = cur->right;
261 continue;
262 }
263
264 if (!prio_tree_right_empty(cur))
265 get_index(cur->right, &r_index, &h_index_right);
266 else {
267 cur = cur->left;
268 continue;
269 }
270
271 /* both h_index_left and h_index_right cannot be 0 */
272 if (h_index_left >= h_index_right)
273 cur = cur->left;
274 else
275 cur = cur->right;
276 }
277
278 if (prio_tree_root(cur)) {
279 assert(root->prio_tree_node == cur);
280 INIT_PRIO_TREE_ROOT(root);
281 return;
282 }
283
284 if (cur->parent->right == cur)
285 cur->parent->right = cur->parent;
286 else
287 cur->parent->left = cur->parent;
288
289 while (cur != node)
290 cur = prio_tree_replace(root, cur->parent, cur);
291}
292
293/*
294 * Following functions help to enumerate all prio_tree_nodes in the tree that
295 * overlap with the input interval X [radix_index, heap_index]. The enumeration
296 * takes O(log n + m) time where 'log n' is the height of the tree (which is
297 * proportional to # of bits required to represent the maximum heap_index) and
298 * 'm' is the number of prio_tree_nodes that overlap the interval X.
299 */
300
301static struct prio_tree_node *prio_tree_left(struct prio_tree_iter *iter,
302 unsigned long *r_index, unsigned long *h_index)
303{
304 if (prio_tree_left_empty(iter->cur))
305 return NULL;
306
307 get_index(iter->cur->left, r_index, h_index);
308
309 if (iter->r_index <= *h_index) {
310 iter->cur = iter->cur->left;
311 iter->mask >>= 1;
312 if (iter->mask) {
313 if (iter->size_level)
314 iter->size_level++;
315 } else {
316 if (iter->size_level) {
317 assert(prio_tree_left_empty(iter->cur));
318 assert(prio_tree_right_empty(iter->cur));
319 iter->size_level++;
320 iter->mask = ULONG_MAX;
321 } else {
322 iter->size_level = 1;
323 iter->mask = 1UL << (BITS_PER_LONG - 1);
324 }
325 }
326 return iter->cur;
327 }
328
329 return NULL;
330}
331
332static struct prio_tree_node *prio_tree_right(struct prio_tree_iter *iter,
333 unsigned long *r_index, unsigned long *h_index)
334{
335 unsigned long value;
336
337 if (prio_tree_right_empty(iter->cur))
338 return NULL;
339
340 if (iter->size_level)
341 value = iter->value;
342 else
343 value = iter->value | iter->mask;
344
345 if (iter->h_index < value)
346 return NULL;
347
348 get_index(iter->cur->right, r_index, h_index);
349
350 if (iter->r_index <= *h_index) {
351 iter->cur = iter->cur->right;
352 iter->mask >>= 1;
353 iter->value = value;
354 if (iter->mask) {
355 if (iter->size_level)
356 iter->size_level++;
357 } else {
358 if (iter->size_level) {
359 assert(prio_tree_left_empty(iter->cur));
360 assert(prio_tree_right_empty(iter->cur));
361 iter->size_level++;
362 iter->mask = ULONG_MAX;
363 } else {
364 iter->size_level = 1;
365 iter->mask = 1UL << (BITS_PER_LONG - 1);
366 }
367 }
368 return iter->cur;
369 }
370
371 return NULL;
372}
373
374static struct prio_tree_node *prio_tree_parent(struct prio_tree_iter *iter)
375{
376 iter->cur = iter->cur->parent;
377 if (iter->mask == ULONG_MAX)
378 iter->mask = 1UL;
379 else if (iter->size_level == 1)
380 iter->mask = 1UL;
381 else
382 iter->mask <<= 1;
383 if (iter->size_level)
384 iter->size_level--;
385 if (!iter->size_level && (iter->value & iter->mask))
386 iter->value ^= iter->mask;
387 return iter->cur;
388}
389
390static inline int overlap(struct prio_tree_iter *iter,
391 unsigned long r_index, unsigned long h_index)
392{
393 return iter->h_index >= r_index && iter->r_index <= h_index;
394}
395
396/*
397 * prio_tree_first:
398 *
399 * Get the first prio_tree_node that overlaps with the interval [radix_index,
400 * heap_index]. Note that always radix_index <= heap_index. We do a pre-order
401 * traversal of the tree.
402 */
403static struct prio_tree_node *prio_tree_first(struct prio_tree_iter *iter)
404{
405 struct prio_tree_root *root;
406 unsigned long r_index, h_index;
407
408 INIT_PRIO_TREE_ITER(iter);
409
410 root = iter->root;
411 if (prio_tree_empty(root))
412 return NULL;
413
414 get_index(root->prio_tree_node, &r_index, &h_index);
415
416 if (iter->r_index > h_index)
417 return NULL;
418
419 iter->mask = 1UL << (root->index_bits - 1);
420 iter->cur = root->prio_tree_node;
421
422 while (1) {
423 if (overlap(iter, r_index, h_index))
424 return iter->cur;
425
426 if (prio_tree_left(iter, &r_index, &h_index))
427 continue;
428
429 if (prio_tree_right(iter, &r_index, &h_index))
430 continue;
431
432 break;
433 }
434 return NULL;
435}
436
437/*
438 * prio_tree_next:
439 *
440 * Get the next prio_tree_node that overlaps with the input interval in iter
441 */
442struct prio_tree_node *prio_tree_next(struct prio_tree_iter *iter)
443{
444 unsigned long r_index, h_index;
445
446 if (iter->cur == NULL)
447 return prio_tree_first(iter);
448
449repeat:
450 while (prio_tree_left(iter, &r_index, &h_index))
451 if (overlap(iter, r_index, h_index))
452 return iter->cur;
453
454 while (!prio_tree_right(iter, &r_index, &h_index)) {
455 while (!prio_tree_root(iter->cur) &&
456 iter->cur->parent->right == iter->cur)
457 prio_tree_parent(iter);
458
459 if (prio_tree_root(iter->cur))
460 return NULL;
461
462 prio_tree_parent(iter);
463 }
464
465 if (overlap(iter, r_index, h_index))
466 return iter->cur;
467
468 goto repeat;
469}