prio_tree.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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. /*
  77. * Extend a priority search tree so that it can store a node with heap_index
  78. * max_heap_index. In the worst case, this algorithm takes O((log n)^2).
  79. * However, this function is used rarely and the common case performance is
  80. * not bad.
  81. */
  82. static struct prio_tree_node *prio_tree_expand(struct prio_tree_root *root,
  83. struct prio_tree_node *node, unsigned long max_heap_index)
  84. {
  85. struct prio_tree_node *first = NULL, *prev, *last = NULL;
  86. if (max_heap_index > prio_tree_maxindex(root->index_bits))
  87. root->index_bits++;
  88. while (max_heap_index > prio_tree_maxindex(root->index_bits)) {
  89. root->index_bits++;
  90. if (prio_tree_empty(root))
  91. continue;
  92. if (first == NULL) {
  93. first = root->prio_tree_node;
  94. prio_tree_remove(root, root->prio_tree_node);
  95. INIT_PRIO_TREE_NODE(first);
  96. last = first;
  97. } else {
  98. prev = last;
  99. last = root->prio_tree_node;
  100. prio_tree_remove(root, root->prio_tree_node);
  101. INIT_PRIO_TREE_NODE(last);
  102. prev->left = last;
  103. last->parent = prev;
  104. }
  105. }
  106. INIT_PRIO_TREE_NODE(node);
  107. if (first) {
  108. node->left = first;
  109. first->parent = node;
  110. } else
  111. last = node;
  112. if (!prio_tree_empty(root)) {
  113. last->left = root->prio_tree_node;
  114. last->left->parent = last;
  115. }
  116. root->prio_tree_node = node;
  117. return node;
  118. }
  119. /*
  120. * Replace a prio_tree_node with a new node and return the old node
  121. */
  122. struct prio_tree_node *prio_tree_replace(struct prio_tree_root *root,
  123. struct prio_tree_node *old, struct prio_tree_node *node)
  124. {
  125. INIT_PRIO_TREE_NODE(node);
  126. if (prio_tree_root(old)) {
  127. BUG_ON(root->prio_tree_node != old);
  128. /*
  129. * We can reduce root->index_bits here. However, it is complex
  130. * and does not help much to improve performance (IMO).
  131. */
  132. root->prio_tree_node = node;
  133. } else {
  134. node->parent = old->parent;
  135. if (old->parent->left == old)
  136. old->parent->left = node;
  137. else
  138. old->parent->right = node;
  139. }
  140. if (!prio_tree_left_empty(old)) {
  141. node->left = old->left;
  142. old->left->parent = node;
  143. }
  144. if (!prio_tree_right_empty(old)) {
  145. node->right = old->right;
  146. old->right->parent = node;
  147. }
  148. return old;
  149. }
  150. /*
  151. * Insert a prio_tree_node @node into a radix priority search tree @root. The
  152. * algorithm typically takes O(log n) time where 'log n' is the number of bits
  153. * required to represent the maximum heap_index. In the worst case, the algo
  154. * can take O((log n)^2) - check prio_tree_expand.
  155. *
  156. * If a prior node with same radix_index and heap_index is already found in
  157. * the tree, then returns the address of the prior node. Otherwise, inserts
  158. * @node into the tree and returns @node.
  159. */
  160. struct prio_tree_node *prio_tree_insert(struct prio_tree_root *root,
  161. struct prio_tree_node *node)
  162. {
  163. struct prio_tree_node *cur, *res = node;
  164. unsigned long radix_index, heap_index;
  165. unsigned long r_index, h_index, index, mask;
  166. int size_flag = 0;
  167. get_index(root, node, &radix_index, &heap_index);
  168. if (prio_tree_empty(root) ||
  169. heap_index > prio_tree_maxindex(root->index_bits))
  170. return prio_tree_expand(root, node, heap_index);
  171. cur = root->prio_tree_node;
  172. mask = 1UL << (root->index_bits - 1);
  173. while (mask) {
  174. get_index(root, cur, &r_index, &h_index);
  175. if (r_index == radix_index && h_index == heap_index)
  176. return cur;
  177. if (h_index < heap_index ||
  178. (h_index == heap_index && r_index > radix_index)) {
  179. struct prio_tree_node *tmp = node;
  180. node = prio_tree_replace(root, cur, node);
  181. cur = tmp;
  182. /* swap indices */
  183. index = r_index;
  184. r_index = radix_index;
  185. radix_index = index;
  186. index = h_index;
  187. h_index = heap_index;
  188. heap_index = index;
  189. }
  190. if (size_flag)
  191. index = heap_index - radix_index;
  192. else
  193. index = radix_index;
  194. if (index & mask) {
  195. if (prio_tree_right_empty(cur)) {
  196. INIT_PRIO_TREE_NODE(node);
  197. cur->right = node;
  198. node->parent = cur;
  199. return res;
  200. } else
  201. cur = cur->right;
  202. } else {
  203. if (prio_tree_left_empty(cur)) {
  204. INIT_PRIO_TREE_NODE(node);
  205. cur->left = node;
  206. node->parent = cur;
  207. return res;
  208. } else
  209. cur = cur->left;
  210. }
  211. mask >>= 1;
  212. if (!mask) {
  213. mask = 1UL << (BITS_PER_LONG - 1);
  214. size_flag = 1;
  215. }
  216. }
  217. /* Should not reach here */
  218. BUG();
  219. return NULL;
  220. }
  221. /*
  222. * Remove a prio_tree_node @node from a radix priority search tree @root. The
  223. * algorithm takes O(log n) time where 'log n' is the number of bits required
  224. * to represent the maximum heap_index.
  225. */
  226. void prio_tree_remove(struct prio_tree_root *root, struct prio_tree_node *node)
  227. {
  228. struct prio_tree_node *cur;
  229. unsigned long r_index, h_index_right, h_index_left;
  230. cur = node;
  231. while (!prio_tree_left_empty(cur) || !prio_tree_right_empty(cur)) {
  232. if (!prio_tree_left_empty(cur))
  233. get_index(root, cur->left, &r_index, &h_index_left);
  234. else {
  235. cur = cur->right;
  236. continue;
  237. }
  238. if (!prio_tree_right_empty(cur))
  239. get_index(root, cur->right, &r_index, &h_index_right);
  240. else {
  241. cur = cur->left;
  242. continue;
  243. }
  244. /* both h_index_left and h_index_right cannot be 0 */
  245. if (h_index_left >= h_index_right)
  246. cur = cur->left;
  247. else
  248. cur = cur->right;
  249. }
  250. if (prio_tree_root(cur)) {
  251. BUG_ON(root->prio_tree_node != cur);
  252. __INIT_PRIO_TREE_ROOT(root, root->raw);
  253. return;
  254. }
  255. if (cur->parent->right == cur)
  256. cur->parent->right = cur->parent;
  257. else
  258. cur->parent->left = cur->parent;
  259. while (cur != node)
  260. cur = prio_tree_replace(root, cur->parent, cur);
  261. }
  262. static void iter_walk_down(struct prio_tree_iter *iter)
  263. {
  264. iter->mask >>= 1;
  265. if (iter->mask) {
  266. if (iter->size_level)
  267. iter->size_level++;
  268. return;
  269. }
  270. if (iter->size_level) {
  271. BUG_ON(!prio_tree_left_empty(iter->cur));
  272. BUG_ON(!prio_tree_right_empty(iter->cur));
  273. iter->size_level++;
  274. iter->mask = ULONG_MAX;
  275. } else {
  276. iter->size_level = 1;
  277. iter->mask = 1UL << (BITS_PER_LONG - 1);
  278. }
  279. }
  280. static void iter_walk_up(struct prio_tree_iter *iter)
  281. {
  282. if (iter->mask == ULONG_MAX)
  283. iter->mask = 1UL;
  284. else if (iter->size_level == 1)
  285. iter->mask = 1UL;
  286. else
  287. iter->mask <<= 1;
  288. if (iter->size_level)
  289. iter->size_level--;
  290. if (!iter->size_level && (iter->value & iter->mask))
  291. iter->value ^= iter->mask;
  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. static struct prio_tree_node *prio_tree_left(struct prio_tree_iter *iter,
  301. unsigned long *r_index, unsigned long *h_index)
  302. {
  303. if (prio_tree_left_empty(iter->cur))
  304. return NULL;
  305. get_index(iter->root, iter->cur->left, r_index, h_index);
  306. if (iter->r_index <= *h_index) {
  307. iter->cur = iter->cur->left;
  308. iter_walk_down(iter);
  309. return iter->cur;
  310. }
  311. return NULL;
  312. }
  313. static struct prio_tree_node *prio_tree_right(struct prio_tree_iter *iter,
  314. unsigned long *r_index, unsigned long *h_index)
  315. {
  316. unsigned long value;
  317. if (prio_tree_right_empty(iter->cur))
  318. return NULL;
  319. if (iter->size_level)
  320. value = iter->value;
  321. else
  322. value = iter->value | iter->mask;
  323. if (iter->h_index < value)
  324. return NULL;
  325. get_index(iter->root, iter->cur->right, r_index, h_index);
  326. if (iter->r_index <= *h_index) {
  327. iter->cur = iter->cur->right;
  328. iter_walk_down(iter);
  329. return iter->cur;
  330. }
  331. return NULL;
  332. }
  333. static struct prio_tree_node *prio_tree_parent(struct prio_tree_iter *iter)
  334. {
  335. iter->cur = iter->cur->parent;
  336. iter_walk_up(iter);
  337. return iter->cur;
  338. }
  339. static inline int overlap(struct prio_tree_iter *iter,
  340. unsigned long r_index, unsigned long h_index)
  341. {
  342. return iter->h_index >= r_index && iter->r_index <= h_index;
  343. }
  344. /*
  345. * prio_tree_first:
  346. *
  347. * Get the first prio_tree_node that overlaps with the interval [radix_index,
  348. * heap_index]. Note that always radix_index <= heap_index. We do a pre-order
  349. * traversal of the tree.
  350. */
  351. static struct prio_tree_node *prio_tree_first(struct prio_tree_iter *iter)
  352. {
  353. struct prio_tree_root *root;
  354. unsigned long r_index, h_index;
  355. INIT_PRIO_TREE_ITER(iter);
  356. root = iter->root;
  357. if (prio_tree_empty(root))
  358. return NULL;
  359. get_index(root, root->prio_tree_node, &r_index, &h_index);
  360. if (iter->r_index > h_index)
  361. return NULL;
  362. iter->mask = 1UL << (root->index_bits - 1);
  363. iter->cur = root->prio_tree_node;
  364. while (1) {
  365. if (overlap(iter, r_index, h_index))
  366. return iter->cur;
  367. if (prio_tree_left(iter, &r_index, &h_index))
  368. continue;
  369. if (prio_tree_right(iter, &r_index, &h_index))
  370. continue;
  371. break;
  372. }
  373. return NULL;
  374. }
  375. /*
  376. * prio_tree_next:
  377. *
  378. * Get the next prio_tree_node that overlaps with the input interval in iter
  379. */
  380. struct prio_tree_node *prio_tree_next(struct prio_tree_iter *iter)
  381. {
  382. unsigned long r_index, h_index;
  383. if (iter->cur == NULL)
  384. return prio_tree_first(iter);
  385. repeat:
  386. while (prio_tree_left(iter, &r_index, &h_index))
  387. if (overlap(iter, r_index, h_index))
  388. return iter->cur;
  389. while (!prio_tree_right(iter, &r_index, &h_index)) {
  390. while (!prio_tree_root(iter->cur) &&
  391. iter->cur->parent->right == iter->cur)
  392. prio_tree_parent(iter);
  393. if (prio_tree_root(iter->cur))
  394. return NULL;
  395. prio_tree_parent(iter);
  396. }
  397. if (overlap(iter, r_index, h_index))
  398. return iter->cur;
  399. goto repeat;
  400. }