prio_tree.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. #include <linux/init.h>
  14. #include <linux/mm.h>
  15. #include <linux/prio_tree.h>
  16. /*
  17. * A clever mix of heap and radix trees forms a radix priority search tree (PST)
  18. * which is useful for storing intervals, e.g, we can consider a vma as a closed
  19. * interval of file pages [offset_begin, offset_end], and store all vmas that
  20. * map a file in a PST. Then, using the PST, we can answer a stabbing query,
  21. * i.e., selecting a set of stored intervals (vmas) that overlap with (map) a
  22. * given input interval X (a set of consecutive file pages), in "O(log n + m)"
  23. * time where 'log n' is the height of the PST, and 'm' is the number of stored
  24. * intervals (vmas) that overlap (map) with the input interval X (the set of
  25. * consecutive file pages).
  26. *
  27. * In our implementation, we store closed intervals of the form [radix_index,
  28. * heap_index]. We assume that always radix_index <= heap_index. McCreight's PST
  29. * is designed for storing intervals with unique radix indices, i.e., each
  30. * interval have different radix_index. However, this limitation can be easily
  31. * overcome by using the size, i.e., heap_index - radix_index, as part of the
  32. * index, so we index the tree using [(radix_index,size), heap_index].
  33. *
  34. * When the above-mentioned indexing scheme is used, theoretically, in a 32 bit
  35. * machine, the maximum height of a PST can be 64. We can use a balanced version
  36. * of the priority search tree to optimize the tree height, but the balanced
  37. * tree proposed by McCreight is too complex and memory-hungry for our purpose.
  38. */
  39. /*
  40. * The following macros are used for implementing prio_tree for i_mmap
  41. */
  42. #define RADIX_INDEX(vma) ((vma)->vm_pgoff)
  43. #define VMA_SIZE(vma) (((vma)->vm_end - (vma)->vm_start) >> PAGE_SHIFT)
  44. /* avoid overflow */
  45. #define HEAP_INDEX(vma) ((vma)->vm_pgoff + (VMA_SIZE(vma) - 1))
  46. static void get_index(const struct prio_tree_root *root,
  47. const struct prio_tree_node *node,
  48. unsigned long *radix, unsigned long *heap)
  49. {
  50. if (root->raw) {
  51. struct vm_area_struct *vma = prio_tree_entry(
  52. node, struct vm_area_struct, shared.prio_tree_node);
  53. *radix = RADIX_INDEX(vma);
  54. *heap = HEAP_INDEX(vma);
  55. }
  56. else {
  57. *radix = node->start;
  58. *heap = node->last;
  59. }
  60. }
  61. static unsigned long index_bits_to_maxindex[BITS_PER_LONG];
  62. void __init prio_tree_init(void)
  63. {
  64. unsigned int i;
  65. for (i = 0; i < ARRAY_SIZE(index_bits_to_maxindex) - 1; i++)
  66. index_bits_to_maxindex[i] = (1UL << (i + 1)) - 1;
  67. index_bits_to_maxindex[ARRAY_SIZE(index_bits_to_maxindex) - 1] = ~0UL;
  68. }
  69. /*
  70. * Maximum heap_index that can be stored in a PST with index_bits bits
  71. */
  72. static inline unsigned long prio_tree_maxindex(unsigned int bits)
  73. {
  74. return index_bits_to_maxindex[bits - 1];
  75. }
  76. static void prio_set_parent(struct prio_tree_node *parent,
  77. struct prio_tree_node *child, bool left)
  78. {
  79. if (left)
  80. parent->left = child;
  81. else
  82. parent->right = child;
  83. child->parent = parent;
  84. }
  85. /*
  86. * Extend a priority search tree so that it can store a node with heap_index
  87. * max_heap_index. In the worst case, this algorithm takes O((log n)^2).
  88. * However, this function is used rarely and the common case performance is
  89. * not bad.
  90. */
  91. static struct prio_tree_node *prio_tree_expand(struct prio_tree_root *root,
  92. struct prio_tree_node *node, unsigned long max_heap_index)
  93. {
  94. struct prio_tree_node *prev;
  95. if (max_heap_index > prio_tree_maxindex(root->index_bits))
  96. root->index_bits++;
  97. prev = node;
  98. INIT_PRIO_TREE_NODE(node);
  99. while (max_heap_index > prio_tree_maxindex(root->index_bits)) {
  100. struct prio_tree_node *tmp = root->prio_tree_node;
  101. root->index_bits++;
  102. if (prio_tree_empty(root))
  103. continue;
  104. prio_tree_remove(root, root->prio_tree_node);
  105. INIT_PRIO_TREE_NODE(tmp);
  106. prio_set_parent(prev, tmp, true);
  107. prev = tmp;
  108. }
  109. if (!prio_tree_empty(root))
  110. prio_set_parent(prev, root->prio_tree_node, true);
  111. root->prio_tree_node = node;
  112. return node;
  113. }
  114. /*
  115. * Replace a prio_tree_node with a new node and return the old node
  116. */
  117. struct prio_tree_node *prio_tree_replace(struct prio_tree_root *root,
  118. struct prio_tree_node *old, struct prio_tree_node *node)
  119. {
  120. INIT_PRIO_TREE_NODE(node);
  121. if (prio_tree_root(old)) {
  122. BUG_ON(root->prio_tree_node != old);
  123. /*
  124. * We can reduce root->index_bits here. However, it is complex
  125. * and does not help much to improve performance (IMO).
  126. */
  127. root->prio_tree_node = node;
  128. } else
  129. prio_set_parent(old->parent, node, old->parent->left == old);
  130. if (!prio_tree_left_empty(old))
  131. prio_set_parent(node, old->left, true);
  132. if (!prio_tree_right_empty(old))
  133. prio_set_parent(node, old->right, false);
  134. return old;
  135. }
  136. /*
  137. * Insert a prio_tree_node @node into a radix priority search tree @root. The
  138. * algorithm typically takes O(log n) time where 'log n' is the number of bits
  139. * required to represent the maximum heap_index. In the worst case, the algo
  140. * can take O((log n)^2) - check prio_tree_expand.
  141. *
  142. * If a prior node with same radix_index and heap_index is already found in
  143. * the tree, then returns the address of the prior node. Otherwise, inserts
  144. * @node into the tree and returns @node.
  145. */
  146. struct prio_tree_node *prio_tree_insert(struct prio_tree_root *root,
  147. struct prio_tree_node *node)
  148. {
  149. struct prio_tree_node *cur, *res = node;
  150. unsigned long radix_index, heap_index;
  151. unsigned long r_index, h_index, index, mask;
  152. int size_flag = 0;
  153. get_index(root, node, &radix_index, &heap_index);
  154. if (prio_tree_empty(root) ||
  155. heap_index > prio_tree_maxindex(root->index_bits))
  156. return prio_tree_expand(root, node, heap_index);
  157. cur = root->prio_tree_node;
  158. mask = 1UL << (root->index_bits - 1);
  159. while (mask) {
  160. get_index(root, cur, &r_index, &h_index);
  161. if (r_index == radix_index && h_index == heap_index)
  162. return cur;
  163. if (h_index < heap_index ||
  164. (h_index == heap_index && r_index > radix_index)) {
  165. struct prio_tree_node *tmp = node;
  166. node = prio_tree_replace(root, cur, node);
  167. cur = tmp;
  168. /* swap indices */
  169. index = r_index;
  170. r_index = radix_index;
  171. radix_index = index;
  172. index = h_index;
  173. h_index = heap_index;
  174. heap_index = index;
  175. }
  176. if (size_flag)
  177. index = heap_index - radix_index;
  178. else
  179. index = radix_index;
  180. if (index & mask) {
  181. if (prio_tree_right_empty(cur)) {
  182. INIT_PRIO_TREE_NODE(node);
  183. prio_set_parent(cur, node, false);
  184. return res;
  185. } else
  186. cur = cur->right;
  187. } else {
  188. if (prio_tree_left_empty(cur)) {
  189. INIT_PRIO_TREE_NODE(node);
  190. prio_set_parent(cur, node, true);
  191. return res;
  192. } else
  193. cur = cur->left;
  194. }
  195. mask >>= 1;
  196. if (!mask) {
  197. mask = 1UL << (BITS_PER_LONG - 1);
  198. size_flag = 1;
  199. }
  200. }
  201. /* Should not reach here */
  202. BUG();
  203. return NULL;
  204. }
  205. /*
  206. * Remove a prio_tree_node @node from a radix priority search tree @root. The
  207. * algorithm takes O(log n) time where 'log n' is the number of bits required
  208. * to represent the maximum heap_index.
  209. */
  210. void prio_tree_remove(struct prio_tree_root *root, struct prio_tree_node *node)
  211. {
  212. struct prio_tree_node *cur;
  213. unsigned long r_index, h_index_right, h_index_left;
  214. cur = node;
  215. while (!prio_tree_left_empty(cur) || !prio_tree_right_empty(cur)) {
  216. if (!prio_tree_left_empty(cur))
  217. get_index(root, cur->left, &r_index, &h_index_left);
  218. else {
  219. cur = cur->right;
  220. continue;
  221. }
  222. if (!prio_tree_right_empty(cur))
  223. get_index(root, cur->right, &r_index, &h_index_right);
  224. else {
  225. cur = cur->left;
  226. continue;
  227. }
  228. /* both h_index_left and h_index_right cannot be 0 */
  229. if (h_index_left >= h_index_right)
  230. cur = cur->left;
  231. else
  232. cur = cur->right;
  233. }
  234. if (prio_tree_root(cur)) {
  235. BUG_ON(root->prio_tree_node != cur);
  236. __INIT_PRIO_TREE_ROOT(root, root->raw);
  237. return;
  238. }
  239. if (cur->parent->right == cur)
  240. cur->parent->right = cur->parent;
  241. else
  242. cur->parent->left = cur->parent;
  243. while (cur != node)
  244. cur = prio_tree_replace(root, cur->parent, cur);
  245. }
  246. static void iter_walk_down(struct prio_tree_iter *iter)
  247. {
  248. iter->mask >>= 1;
  249. if (iter->mask) {
  250. if (iter->size_level)
  251. iter->size_level++;
  252. return;
  253. }
  254. if (iter->size_level) {
  255. BUG_ON(!prio_tree_left_empty(iter->cur));
  256. BUG_ON(!prio_tree_right_empty(iter->cur));
  257. iter->size_level++;
  258. iter->mask = ULONG_MAX;
  259. } else {
  260. iter->size_level = 1;
  261. iter->mask = 1UL << (BITS_PER_LONG - 1);
  262. }
  263. }
  264. static void iter_walk_up(struct prio_tree_iter *iter)
  265. {
  266. if (iter->mask == ULONG_MAX)
  267. iter->mask = 1UL;
  268. else if (iter->size_level == 1)
  269. iter->mask = 1UL;
  270. else
  271. iter->mask <<= 1;
  272. if (iter->size_level)
  273. iter->size_level--;
  274. if (!iter->size_level && (iter->value & iter->mask))
  275. iter->value ^= iter->mask;
  276. }
  277. /*
  278. * Following functions help to enumerate all prio_tree_nodes in the tree that
  279. * overlap with the input interval X [radix_index, heap_index]. The enumeration
  280. * takes O(log n + m) time where 'log n' is the height of the tree (which is
  281. * proportional to # of bits required to represent the maximum heap_index) and
  282. * 'm' is the number of prio_tree_nodes that overlap the interval X.
  283. */
  284. static struct prio_tree_node *prio_tree_left(struct prio_tree_iter *iter,
  285. unsigned long *r_index, unsigned long *h_index)
  286. {
  287. if (prio_tree_left_empty(iter->cur))
  288. return NULL;
  289. get_index(iter->root, iter->cur->left, r_index, h_index);
  290. if (iter->r_index <= *h_index) {
  291. iter->cur = iter->cur->left;
  292. iter_walk_down(iter);
  293. return iter->cur;
  294. }
  295. return NULL;
  296. }
  297. static struct prio_tree_node *prio_tree_right(struct prio_tree_iter *iter,
  298. unsigned long *r_index, unsigned long *h_index)
  299. {
  300. unsigned long value;
  301. if (prio_tree_right_empty(iter->cur))
  302. return NULL;
  303. if (iter->size_level)
  304. value = iter->value;
  305. else
  306. value = iter->value | iter->mask;
  307. if (iter->h_index < value)
  308. return NULL;
  309. get_index(iter->root, iter->cur->right, r_index, h_index);
  310. if (iter->r_index <= *h_index) {
  311. iter->cur = iter->cur->right;
  312. iter_walk_down(iter);
  313. return iter->cur;
  314. }
  315. return NULL;
  316. }
  317. static struct prio_tree_node *prio_tree_parent(struct prio_tree_iter *iter)
  318. {
  319. iter->cur = iter->cur->parent;
  320. iter_walk_up(iter);
  321. return iter->cur;
  322. }
  323. static inline int overlap(struct prio_tree_iter *iter,
  324. unsigned long r_index, unsigned long h_index)
  325. {
  326. return iter->h_index >= r_index && iter->r_index <= h_index;
  327. }
  328. /*
  329. * prio_tree_first:
  330. *
  331. * Get the first prio_tree_node that overlaps with the interval [radix_index,
  332. * heap_index]. Note that always radix_index <= heap_index. We do a pre-order
  333. * traversal of the tree.
  334. */
  335. static struct prio_tree_node *prio_tree_first(struct prio_tree_iter *iter)
  336. {
  337. struct prio_tree_root *root;
  338. unsigned long r_index, h_index;
  339. INIT_PRIO_TREE_ITER(iter);
  340. root = iter->root;
  341. if (prio_tree_empty(root))
  342. return NULL;
  343. get_index(root, root->prio_tree_node, &r_index, &h_index);
  344. if (iter->r_index > h_index)
  345. return NULL;
  346. iter->mask = 1UL << (root->index_bits - 1);
  347. iter->cur = root->prio_tree_node;
  348. while (1) {
  349. if (overlap(iter, r_index, h_index))
  350. return iter->cur;
  351. if (prio_tree_left(iter, &r_index, &h_index))
  352. continue;
  353. if (prio_tree_right(iter, &r_index, &h_index))
  354. continue;
  355. break;
  356. }
  357. return NULL;
  358. }
  359. /*
  360. * prio_tree_next:
  361. *
  362. * Get the next prio_tree_node that overlaps with the input interval in iter
  363. */
  364. struct prio_tree_node *prio_tree_next(struct prio_tree_iter *iter)
  365. {
  366. unsigned long r_index, h_index;
  367. if (iter->cur == NULL)
  368. return prio_tree_first(iter);
  369. repeat:
  370. while (prio_tree_left(iter, &r_index, &h_index))
  371. if (overlap(iter, r_index, h_index))
  372. return iter->cur;
  373. while (!prio_tree_right(iter, &r_index, &h_index)) {
  374. while (!prio_tree_root(iter->cur) &&
  375. iter->cur->parent->right == iter->cur)
  376. prio_tree_parent(iter);
  377. if (prio_tree_root(iter->cur))
  378. return NULL;
  379. prio_tree_parent(iter);
  380. }
  381. if (overlap(iter, r_index, h_index))
  382. return iter->cur;
  383. goto repeat;
  384. }