1. Common Introduction to Radix Tree
(mainly from wiki)
In , a tree (also patricia trie or radix trie or compact prefix tree) is a space-optimized where each node with only one child is merged with its child.
()
They find particular application in the area of , where the ability to contain large ranges of values with a few exceptions is particularly suited to the hierarchical organization of . (also, the hierarchical organization of page addresses in page cache!)
Donald R. Morrison first described what he called "Patricia trees" in 1968; the name comes from the PATRICIA, which stands for "Practical Algorithm To Retrieve Information Coded In Alphanumeric".
Radix trees also share the disadvantages of tries, however: as they can only be applied to strings of elements or elements with an efficiently reversible mapping () to strings, they lack the full generality of balanced search trees, which apply to any data type with a .
2. Radix Tree in Linux Kernel
Problem: In Linux, file can be quite large, even a few terabytes. When accessing the file, the page cache may become filled with so many pages of the file that sequentially scanning all of them would be too time-consuming.
Goal: Perform page cache searching efficiently
Method: Linux 2.6 makes use of a large set of search trees, one for each page cache. These trees are radix trees.
A Linux radix tree is a mechanism by which a (pointer) value can be associated with a (long) integer key. It is reasonably efficient in terms of storage, and is quite quick on lookups.
The address_space structure used to keep track of backing store contains a radix tree which tracks in-core pages tied to that mapping. Among other things, this tree allows the memory management code to quickly find pages which are dirty or under writeback.
In radix tree, the equivalent of the linear address is the page’s index.
In a 32-bit architecture, the height of this radix tree never exceeds 6, that is 2+6+6+6+6+6. The corresponding slot numbers are: 4, 64, 64, 64, 64, 64.
If the highest index of a radix tree is smaller than the page index of a page to be added, then the kernel increases the tree height correspondingly.
data structures
struct address_space { struct radix_tree_root page_tree; /* radix tree of all pages */};/* root tags are stored in gfp_mask, shifted by __GFP_BITS_SHIFT */struct radix_tree_root { unsigned int height; gfp_t gfp_mask; struct radix_tree_node *rnode;};struct radix_tree_node { unsigned int height; /* Height from the bottom */ unsigned int count; struct rcu_head rcu_head; void *slots[RADIX_TREE_MAP_SIZE]; unsigned long tags[RADIX_TREE_MAX_TAGS][RADIX_TREE_TAG_LONGS];};/* root tags are stored in gfp_mask, shifted by __GFP_BITS_SHIFT */struct radix_tree_root { unsigned int height; gfp_t gfp_mask; struct radix_tree_node *rnode;};struct radix_tree_path { struct radix_tree_node *node; int offset;};
3. References