public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Wu Fengguang <wfg@mail.ustc.edu.cn>
To: Andrew Morton <akpm@osdl.org>
Cc: linux-kernel@vger.kernel.org, Wu Fengguang <wfg@mail.ustc.edu.cn>,
	Nick Piggin <nickpiggin@yahoo.com.au>,
	Christoph Lameter <clameter@sgi.com>
Subject: [PATCH 02/33] radixtree: look-aside cache
Date: Wed, 24 May 2006 19:12:48 +0800	[thread overview]
Message-ID: <348469536.15678@ustc.edu.cn> (raw)
Message-ID: <20060524111857.983845462@localhost.localdomain> (raw)
In-Reply-To: 20060524111246.420010595@localhost.localdomain

[-- Attachment #1: radixtree-lookaside-cache.patch --]
[-- Type: text/plain, Size: 9264 bytes --]

Introduce a set of lookup functions to radix tree for the read-ahead logic.
Other access patterns with high locality may also benefit from them.

- radix_tree_lookup_parent(root, index, level)
	Perform partial lookup, return the @level'th parent of the slot at
	@index.

- radix_tree_cache_xxx()
	Init/Query the cache.
- radix_tree_cache_lookup(root, cache, index)
	Perform lookup with the aid of a look-aside cache.
	For sequential scans, it has a time complexity of 64*O(1) + 1*O(logN).

	Typical usage:

   void func() {
  +       struct radix_tree_cache cache;
  +
  +       radix_tree_cache_init(&cache);
          read_lock_irq(&mapping->tree_lock);
          for(;;) {
  -               page = radix_tree_lookup(&mapping->page_tree, index);
  +               page = radix_tree_cache_lookup(&mapping->page_tree, &cache, index);
          }
          read_unlock_irq(&mapping->tree_lock);
   }                                                                                                                       	

Acked-by: Nick Piggin <nickpiggin@yahoo.com.au>
Signed-off-by: Christoph Lameter <clameter@sgi.com>
Signed-off-by: Wu Fengguang <wfg@mail.ustc.edu.cn>
---

 include/linux/radix-tree.h |   83 +++++++++++++++++++++++++++++++++++
 lib/radix-tree.c           |  104 ++++++++++++++++++++++++++++++++++-----------
 2 files changed, 161 insertions(+), 26 deletions(-)

--- linux-2.6.17-rc4-mm3.orig/include/linux/radix-tree.h
+++ linux-2.6.17-rc4-mm3/include/linux/radix-tree.h
@@ -26,12 +26,29 @@
 #define RADIX_TREE_MAX_TAGS 2
 
 /* root tags are stored in gfp_mask, shifted by __GFP_BITS_SHIFT */
+#ifdef __KERNEL__
+#define RADIX_TREE_MAP_SHIFT	(CONFIG_BASE_SMALL ? 4 : 6)
+#else
+#define RADIX_TREE_MAP_SHIFT	3	/* For more stressful testing */
+#endif
+
+#define RADIX_TREE_MAP_SIZE	(1UL << RADIX_TREE_MAP_SHIFT)
+#define RADIX_TREE_MAP_MASK	(RADIX_TREE_MAP_SIZE-1)
+
 struct radix_tree_root {
 	unsigned int		height;
 	gfp_t			gfp_mask;
 	struct radix_tree_node	*rnode;
 };
 
+/*
+ * Lookaside cache to support access patterns with strong locality.
+ */
+struct radix_tree_cache {
+	unsigned long first_index;
+	struct radix_tree_node *tree_node;
+};
+
 #define RADIX_TREE_INIT(mask)	{					\
 	.height = 0,							\
 	.gfp_mask = (mask),						\
@@ -49,9 +66,14 @@ do {									\
 } while (0)
 
 int radix_tree_insert(struct radix_tree_root *, unsigned long, void *);
-void *radix_tree_lookup(struct radix_tree_root *, unsigned long);
-void **radix_tree_lookup_slot(struct radix_tree_root *, unsigned long);
+void *radix_tree_lookup_parent(struct radix_tree_root *, unsigned long,
+							unsigned int);
+void **radix_tree_lookup_slot(struct radix_tree_root *root, unsigned long);
 void *radix_tree_delete(struct radix_tree_root *, unsigned long);
+unsigned int radix_tree_cache_count(struct radix_tree_cache *cache);
+void *radix_tree_cache_lookup_parent(struct radix_tree_root *root,
+				struct radix_tree_cache *cache,
+				unsigned long index, unsigned int level);
 unsigned int
 radix_tree_gang_lookup(struct radix_tree_root *root, void **results,
 			unsigned long first_index, unsigned int max_items);
@@ -74,4 +96,61 @@ static inline void radix_tree_preload_en
 	preempt_enable();
 }
 
+/**
+ *	radix_tree_lookup    -    perform lookup operation on a radix tree
+ *	@root:		radix tree root
+ *	@index:		index key
+ *
+ *	Lookup the item at the position @index in the radix tree @root.
+ */
+static inline void *radix_tree_lookup(struct radix_tree_root *root,
+							unsigned long index)
+{
+	return radix_tree_lookup_parent(root, index, 0);
+}
+
+/**
+ *	radix_tree_cache_init    -    init a look-aside cache
+ *	@cache:		look-aside cache
+ *
+ *	Init the radix tree look-aside cache @cache.
+ */
+static inline void radix_tree_cache_init(struct radix_tree_cache *cache)
+{
+	cache->first_index = RADIX_TREE_MAP_MASK;
+	cache->tree_node = NULL;
+}
+
+/**
+ *	radix_tree_cache_lookup    -    cached lookup on a radix tree
+ *	@root:		radix tree root
+ *	@cache:		look-aside cache
+ *	@index:		index key
+ *
+ *	Lookup the item at the position @index in the radix tree @root,
+ *	and make use of @cache to speedup the lookup process.
+ */
+static inline void *radix_tree_cache_lookup(struct radix_tree_root *root,
+						struct radix_tree_cache *cache,
+						unsigned long index)
+{
+	return radix_tree_cache_lookup_parent(root, cache, index, 0);
+}
+
+static inline unsigned int radix_tree_cache_size(struct radix_tree_cache *cache)
+{
+	return RADIX_TREE_MAP_SIZE;
+}
+
+static inline int radix_tree_cache_full(struct radix_tree_cache *cache)
+{
+	return radix_tree_cache_count(cache) == radix_tree_cache_size(cache);
+}
+
+static inline unsigned long
+radix_tree_cache_first_index(struct radix_tree_cache *cache)
+{
+	return cache->first_index;
+}
+
 #endif /* _LINUX_RADIX_TREE_H */
--- linux-2.6.17-rc4-mm3.orig/lib/radix-tree.c
+++ linux-2.6.17-rc4-mm3/lib/radix-tree.c
@@ -309,36 +309,90 @@ int radix_tree_insert(struct radix_tree_
 }
 EXPORT_SYMBOL(radix_tree_insert);
 
-static inline void **__lookup_slot(struct radix_tree_root *root,
-				   unsigned long index)
+/**
+ *	radix_tree_lookup_parent    -    low level lookup routine
+ *	@root:		radix tree root
+ *	@index:		index key
+ *	@level:		stop at that many levels from the tree leaf
+ *
+ *	Lookup the @level'th parent of the slot at @index in radix tree @root.
+ *	The return value is:
+ *	@level == 0:      page at @index;
+ *	@level == 1:      the corresponding bottom level tree node;
+ *	@level < height:  (@level-1)th parent node of the bottom node
+ *			  that contains @index;
+ *	@level >= height: the root node.
+ */
+void *radix_tree_lookup_parent(struct radix_tree_root *root,
+				unsigned long index, unsigned int level)
 {
 	unsigned int height, shift;
-	struct radix_tree_node **slot;
+	struct radix_tree_node *slot;
 
 	height = root->height;
 
 	if (index > radix_tree_maxindex(height))
 		return NULL;
 
-	if (height == 0 && root->rnode)
-		return (void **)&root->rnode;
-
 	shift = (height-1) * RADIX_TREE_MAP_SHIFT;
-	slot = &root->rnode;
+	slot = root->rnode;
 
-	while (height > 0) {
-		if (*slot == NULL)
+	while (height > level) {
+		if (slot == NULL)
 			return NULL;
 
-		slot = (struct radix_tree_node **)
-			((*slot)->slots +
-				((index >> shift) & RADIX_TREE_MAP_MASK));
+		slot = slot->slots[(index >> shift) & RADIX_TREE_MAP_MASK];
 		shift -= RADIX_TREE_MAP_SHIFT;
 		height--;
 	}
 
-	return (void **)slot;
+	return slot;
+}
+EXPORT_SYMBOL(radix_tree_lookup_parent);
+
+/**
+ *	radix_tree_cache_lookup_parent    -    cached lookup node
+ *	@root:		radix tree root
+ *	@cache:		look-aside cache
+ *	@index:		index key
+ *
+ *	Lookup the item at the position @index in the radix tree @root,
+ *	and return the node @level levels from the bottom in the search path.
+ *
+ *	@cache stores the last accessed upper level tree node by this
+ *	function, and is always checked first before searching in the tree.
+ *	It can improve speed for access patterns with strong locality.
+ *
+ *	NOTE:
+ *	- The cache becomes invalid on leaving the lock;
+ *	- Do not intermix calls with different @level.
+ */
+void *radix_tree_cache_lookup_parent(struct radix_tree_root *root,
+				struct radix_tree_cache *cache,
+				unsigned long index, unsigned int level)
+{
+	struct radix_tree_node *node;
+        unsigned long i;
+        unsigned long mask;
+
+        if (level >= root->height)
+                return radix_tree_lookup_parent(root, index, level);
+
+        i = (index >> (level * RADIX_TREE_MAP_SHIFT)) & RADIX_TREE_MAP_MASK;
+        mask = (~0UL) << ((level + 1) * RADIX_TREE_MAP_SHIFT);
+
+	if ((index & mask) == cache->first_index)
+                return cache->tree_node->slots[i];
+
+	node = radix_tree_lookup_parent(root, index, level + 1);
+	if (!node)
+		return 0;
+
+	cache->tree_node = node;
+	cache->first_index = (index & mask);
+        return node->slots[i];
 }
+EXPORT_SYMBOL(radix_tree_cache_lookup_parent);
 
 /**
  *	radix_tree_lookup_slot    -    lookup a slot in a radix tree
@@ -350,25 +404,27 @@ static inline void **__lookup_slot(struc
  */
 void **radix_tree_lookup_slot(struct radix_tree_root *root, unsigned long index)
 {
-	return __lookup_slot(root, index);
+	struct radix_tree_node *node;
+
+	node = radix_tree_lookup_parent(root, index, 1);
+	return node->slots + (index & RADIX_TREE_MAP_MASK);
 }
 EXPORT_SYMBOL(radix_tree_lookup_slot);
 
 /**
- *	radix_tree_lookup    -    perform lookup operation on a radix tree
- *	@root:		radix tree root
- *	@index:		index key
+ *	radix_tree_cache_count    -    items in the cached node
+ *	@cache:      radix tree look-aside cache
  *
- *	Lookup the item at the position @index in the radix tree @root.
+ *      Query the number of items contained in the cached node.
  */
-void *radix_tree_lookup(struct radix_tree_root *root, unsigned long index)
+unsigned int radix_tree_cache_count(struct radix_tree_cache *cache)
 {
-	void **slot;
-
-	slot = __lookup_slot(root, index);
-	return slot != NULL ? *slot : NULL;
+	if (!(cache->first_index & RADIX_TREE_MAP_MASK))
+		return cache->tree_node->count;
+	else
+		return 0;
 }
-EXPORT_SYMBOL(radix_tree_lookup);
+EXPORT_SYMBOL(radix_tree_cache_count);
 
 /**
  *	radix_tree_tag_set - set a tag on a radix tree node

--

  parent reply	other threads:[~2006-05-24 11:19 UTC|newest]

Thread overview: 107+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20060524111246.420010595@localhost.localdomain>
2006-05-24 11:12 ` [PATCH 00/33] Adaptive read-ahead V12 Wu Fengguang
2006-05-25 15:44   ` Andrew Morton
2006-05-25 19:26     ` Michael Stone
2006-05-25 19:40     ` David Lang
2006-05-25 22:01       ` Andrew Morton
2006-05-25 20:28         ` David Lang
2006-05-26  0:48         ` Michael Stone
     [not found]     ` <20060526011939.GA6220@mail.ustc.edu.cn>
2006-05-26  1:19       ` Wu Fengguang
2006-05-26  2:10     ` Jon Smirl
2006-05-26  3:14       ` Nick Piggin
2006-05-26 14:00     ` Andi Kleen
2006-05-26 16:25       ` Andrew Morton
2006-05-26 23:54       ` Folkert van Heusden
2006-05-27  0:00         ` Con Kolivas
2006-05-27  0:08           ` Con Kolivas
2006-05-28 22:20             ` Diego Calleja
2006-05-28 22:31               ` kernel
     [not found]                 ` <20060529030445.GB5994@mail.ustc.edu.cn>
2006-05-29  3:04                   ` Wu Fengguang
     [not found] ` <20060524111857.983845462@localhost.localdomain>
2006-05-24 11:12   ` Wu Fengguang [this message]
     [not found] ` <20060524111858.357709745@localhost.localdomain>
2006-05-24 11:12   ` [PATCH 03/33] radixtree: hole scanning functions Wu Fengguang
2006-05-25 16:19     ` Andrew Morton
     [not found]       ` <20060526070416.GB5135@mail.ustc.edu.cn>
2006-05-26  7:04         ` Wu Fengguang
     [not found]       ` <20060526110559.GA14398@mail.ustc.edu.cn>
2006-05-26 11:05         ` Wu Fengguang
2006-05-26 16:19           ` Andrew Morton
     [not found] ` <20060524111859.540640819@localhost.localdomain>
2006-05-24 11:12   ` [PATCH 05/33] readahead: refactor do_generic_mapping_read() Wu Fengguang
     [not found] ` <20060524111859.909928820@localhost.localdomain>
2006-05-24 11:12   ` [PATCH 06/33] readahead: refactor __do_page_cache_readahead() Wu Fengguang
2006-05-25 16:30     ` Andrew Morton
2006-05-25 22:33       ` Paul Mackerras
2006-05-25 22:40         ` Andrew Morton
     [not found]       ` <20060526071339.GE5135@mail.ustc.edu.cn>
2006-05-26  7:13         ` Wu Fengguang
     [not found] ` <20060524111900.419314658@localhost.localdomain>
2006-05-24 11:12   ` [PATCH 07/33] readahead: insert cond_resched() calls Wu Fengguang
     [not found] ` <20060524111900.970898174@localhost.localdomain>
2006-05-24 11:12   ` [PATCH 08/33] readahead: common macros Wu Fengguang
2006-05-25  5:56     ` Nick Piggin
     [not found]       ` <20060525104117.GE4996@mail.ustc.edu.cn>
2006-05-25 10:41         ` Wu Fengguang
2006-05-26  3:33           ` Nick Piggin
     [not found]             ` <20060526065906.GA5135@mail.ustc.edu.cn>
2006-05-26  6:59               ` Wu Fengguang
     [not found]       ` <20060525134224.GJ4996@mail.ustc.edu.cn>
2006-05-25 13:42         ` Wu Fengguang
2006-05-25 14:38           ` Andrew Morton
2006-05-25 16:33     ` Andrew Morton
     [not found] ` <20060524111901.581603095@localhost.localdomain>
2006-05-24 11:12   ` [PATCH 09/33] readahead: events accounting Wu Fengguang
2006-05-25 16:36     ` Andrew Morton
     [not found]       ` <20060526070943.GD5135@mail.ustc.edu.cn>
2006-05-26  7:09         ` Wu Fengguang
     [not found]       ` <20060527132002.GA4814@mail.ustc.edu.cn>
2006-05-27 13:20         ` Wu Fengguang
2006-05-29  8:19           ` Martin Peschke
     [not found] ` <20060524111901.976888971@localhost.localdomain>
2006-05-24 11:12   ` [PATCH 10/33] readahead: support functions Wu Fengguang
2006-05-25  5:13     ` Nick Piggin
     [not found]       ` <20060525111318.GH4996@mail.ustc.edu.cn>
2006-05-25 11:13         ` Wu Fengguang
2006-05-25 16:48     ` Andrew Morton
     [not found]       ` <20060526073114.GH5135@mail.ustc.edu.cn>
2006-05-26  7:31         ` Wu Fengguang
     [not found] ` <20060524111902.491708692@localhost.localdomain>
2006-05-24 11:12   ` [PATCH 11/33] readahead: sysctl parameters Wu Fengguang
2006-05-25  4:50     ` [PATCH 12/33] readahead: min/max sizes Nick Piggin
     [not found]       ` <20060525121206.GI4996@mail.ustc.edu.cn>
2006-05-25 12:12         ` Wu Fengguang
     [not found] ` <20060524111903.510268987@localhost.localdomain>
2006-05-24 11:12   ` [PATCH 13/33] readahead: state based method - aging accounting Wu Fengguang
2006-05-26 17:04     ` Andrew Morton
     [not found]       ` <20060527062234.GB4991@mail.ustc.edu.cn>
2006-05-27  6:22         ` Wu Fengguang
2006-05-27  7:00           ` Andrew Morton
     [not found]             ` <20060527072201.GA5284@mail.ustc.edu.cn>
2006-05-27  7:22               ` Wu Fengguang
     [not found] ` <20060524111904.019763011@localhost.localdomain>
2006-05-24 11:13   ` [PATCH 14/33] readahead: state based method - data structure Wu Fengguang
2006-05-25  6:03     ` Nick Piggin
     [not found]       ` <20060525104353.GF4996@mail.ustc.edu.cn>
2006-05-25 10:43         ` Wu Fengguang
2006-05-26 17:05     ` Andrew Morton
     [not found]       ` <20060527070248.GD4991@mail.ustc.edu.cn>
2006-05-27  7:02         ` Wu Fengguang
     [not found]       ` <20060527082758.GF4991@mail.ustc.edu.cn>
2006-05-27  8:27         ` Wu Fengguang
     [not found] ` <20060524111904.683513683@localhost.localdomain>
2006-05-24 11:13   ` [PATCH 15/33] readahead: state based method - routines Wu Fengguang
2006-05-26 17:15     ` Andrew Morton
     [not found]       ` <20060527020616.GA7418@mail.ustc.edu.cn>
2006-05-27  2:06         ` Wu Fengguang
     [not found] ` <20060524111906.245276338@localhost.localdomain>
2006-05-24 11:13   ` [PATCH 18/33] readahead: initial method - guiding sizes Wu Fengguang
     [not found] ` <20060524111906.588647885@localhost.localdomain>
2006-05-24 11:13   ` [PATCH 19/33] readahead: initial method - thrashing guard size Wu Fengguang
     [not found] ` <20060524111907.134685550@localhost.localdomain>
2006-05-24 11:13   ` [PATCH 20/33] readahead: initial method - expected read size Wu Fengguang
2006-05-25  5:34     ` [PATCH 22/33] readahead: initial method Nick Piggin
     [not found]       ` <20060525085957.GC4996@mail.ustc.edu.cn>
2006-05-25  8:59         ` Wu Fengguang
2006-05-26 17:29     ` [PATCH 20/33] readahead: initial method - expected read size Andrew Morton
     [not found]       ` <20060527063826.GC4991@mail.ustc.edu.cn>
2006-05-27  6:38         ` Wu Fengguang
     [not found] ` <20060524111908.569533741@localhost.localdomain>
2006-05-24 11:13   ` [PATCH 23/33] readahead: backward prefetching method Wu Fengguang
2006-05-26 17:37     ` Nate Diller
2006-05-26 19:22       ` Nathan Scott
     [not found]         ` <20060528123006.GC6478@mail.ustc.edu.cn>
2006-05-28 12:30           ` Wu Fengguang
     [not found] ` <20060524111909.147416866@localhost.localdomain>
2006-05-24 11:13   ` [PATCH 24/33] readahead: seeking reads method Wu Fengguang
     [not found] ` <20060524111909.635589701@localhost.localdomain>
2006-05-24 11:13   ` [PATCH 25/33] readahead: thrashing recovery method Wu Fengguang
     [not found] ` <20060524111910.207894375@localhost.localdomain>
2006-05-24 11:13   ` [PATCH 26/33] readahead: call scheme Wu Fengguang
     [not found] ` <20060524111910.544274094@localhost.localdomain>
2006-05-24 11:13   ` [PATCH 27/33] readahead: laptop mode Wu Fengguang
2006-05-26 17:38     ` Andrew Morton
     [not found] ` <20060524111911.032100160@localhost.localdomain>
2006-05-24 11:13   ` [PATCH 28/33] readahead: loop case Wu Fengguang
2006-05-24 14:01   ` Limin Wang
     [not found]     ` <20060525154846.GA6907@mail.ustc.edu.cn>
2006-05-25 15:48       ` wfg
     [not found] ` <20060524111911.607080495@localhost.localdomain>
2006-05-24 11:13   ` [PATCH 29/33] readahead: nfsd case Wu Fengguang
     [not found] ` <20060524111912.156646847@localhost.localdomain>
2006-05-24 11:13   ` [PATCH 30/33] readahead: turn on by default Wu Fengguang
     [not found] ` <20060524111912.485160282@localhost.localdomain>
2006-05-24 11:13   ` [PATCH 31/33] readahead: debug radix tree new functions Wu Fengguang
     [not found] ` <20060524111912.967392912@localhost.localdomain>
2006-05-24 11:13   ` [PATCH 32/33] readahead: debug traces showing accessed file names Wu Fengguang
     [not found] ` <20060524111913.603476893@localhost.localdomain>
2006-05-24 11:13   ` [PATCH 33/33] readahead: debug traces showing read patterns Wu Fengguang
     [not found] ` <20060524111858.869793445@localhost.localdomain>
2006-05-24 11:12   ` [PATCH 04/33] readahead: page flag PG_readahead Wu Fengguang
2006-05-25 16:23     ` Andrew Morton
     [not found]       ` <20060526070646.GC5135@mail.ustc.edu.cn>
2006-05-26  7:06         ` Wu Fengguang
2006-05-24 12:27   ` Peter Zijlstra
     [not found]     ` <20060524123740.GA16304@mail.ustc.edu.cn>
2006-05-24 12:37       ` Wu Fengguang
2006-05-24 12:48       ` Peter Zijlstra
     [not found] ` <20060524111905.586110688@localhost.localdomain>
2006-05-24 11:13   ` [PATCH 17/33] readahead: context based method Wu Fengguang
2006-05-25  5:26     ` Nick Piggin
     [not found]       ` <20060525080308.GB4996@mail.ustc.edu.cn>
2006-05-25  8:03         ` Wu Fengguang
2006-05-26 17:23     ` Andrew Morton
     [not found]       ` <20060527021252.GB7418@mail.ustc.edu.cn>
2006-05-27  2:12         ` Wu Fengguang
2006-05-26 17:27     ` Andrew Morton
     [not found]       ` <20060527080443.GE4991@mail.ustc.edu.cn>
2006-05-27  8:04         ` Wu Fengguang
2006-05-24 12:37   ` Peter Zijlstra
     [not found]     ` <20060524133353.GA16508@mail.ustc.edu.cn>
2006-05-24 13:33       ` Wu Fengguang
2006-05-24 15:53       ` Peter Zijlstra
     [not found]         ` <20060525012556.GA6111@mail.ustc.edu.cn>
2006-05-25  1:25           ` Wu Fengguang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=348469536.15678@ustc.edu.cn \
    --to=wfg@mail.ustc.edu.cn \
    --cc=akpm@osdl.org \
    --cc=clameter@sgi.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nickpiggin@yahoo.com.au \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox