Radix Tree in Linux Kernel

本文介绍了计算机科学中的Radix树(又称Patricia树),并详细探讨了其在Linux内核中的实现方式与应用场景,特别是在页缓存搜索方面的高效表现。

1. Common Introduction to Radix Tree

(mainly from wiki)

In computer science, a radix tree (also patricia trie or radix trie or compact prefix tree) is a space-optimized trie data structure where each node with only one child is merged with its child.

(http://en.wikipedia.org/wiki/Radix_tree)

 

They find particular application in the area of IP routing, where the ability to contain large ranges of values with a few exceptions is particularly suited to the hierarchical organization of IP addresses. (also, the hierarchical organization of page addresses in page cache!)

 

Donald R. Morrison first described what he called "Patricia trees" in 1968;[3] the name comes from the acronym 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 (injection) to strings, they lack the full generality of balanced search trees, which apply to any data type with a total ordering.

 

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

http://lwn.net/Articles/175432/

http://en.wikipedia.org/wiki/Radix_tree

 

转载于:https://my.oschina.net/u/158589/blog/61887

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值