>From 2458dd29af2ce79a67d3b302e77769d6e322a11a Mon Sep 17 00:00:00 2001
From: qiaonuohan <qiaonuohan@cn.fujitsu.com>
Date: Tue, 26 Feb 2013 09:38:27 +0800
Subject: [PATCH 1/2] make rbtree manipulation functions global

rbtree manipulation functions were only used by runq cmd. Now make
them global and available to other commands.
---
 defs.h  |   20 +++++++++++++
 task.c  |   94 --------------------------------------------------------------
 tools.c |   95 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 115 insertions(+), 94 deletions(-)

diff --git a/defs.h b/defs.h
index 1f693c3..2993f2b 100755
--- a/defs.h
+++ b/defs.h
@@ -2256,6 +2256,20 @@ struct alias_data {                 /* command alias storage */
 	char argbuf[1];
 };
 
+struct rb_node
+{
+        unsigned long  rb_parent_color;
+#define RB_RED          0
+#define RB_BLACK        1
+        struct rb_node *rb_right;
+        struct rb_node *rb_left;
+};
+
+struct rb_root
+{
+        struct rb_node *rb_node;
+};
+
 #define NUMBER_STACKFRAMES 4
 
 #define SAVE_RETURN_ADDRESS(retaddr) \
@@ -4082,6 +4096,12 @@ uint16_t swap16(uint16_t, int);
 uint32_t swap32(uint32_t, int);
 int make_cpumask(char *, ulong *, int, int *);
 size_t strlcpy(char *, char *, size_t);
+struct rb_node *rb_first(struct rb_root *);
+struct rb_node *rb_parent(struct rb_node *, struct rb_node *);
+struct rb_node *rb_right(struct rb_node *, struct rb_node *);
+struct rb_node *rb_left(struct rb_node *, struct rb_node *);
+struct rb_node *rb_next(struct rb_node *);
+struct rb_node *rb_last(struct rb_root *);
 
 /* 
  *  symbols.c 
diff --git a/task.c b/task.c
index fec9f6a..6fb2b4e 100755
--- a/task.c
+++ b/task.c
@@ -56,13 +56,6 @@ static void dump_runq(void);
 static void dump_on_rq_timestamp(void);
 static void dump_runqueues(void);
 static void dump_prio_array(int, ulong, char *);
-struct rb_root;
-static struct rb_node *rb_first(struct rb_root *);
-struct rb_node;
-static struct rb_node *rb_next(struct rb_node *);
-static struct rb_node *rb_parent(struct rb_node *, struct rb_node *);
-static struct rb_node *rb_right(struct rb_node *, struct rb_node *);
-static struct rb_node *rb_left(struct rb_node *, struct rb_node *);
 static void dump_task_runq_entry(struct task_context *);
 static void print_group_header_fair(int, ulong, void *);
 static void print_parent_task_group_fair(void *, int);
@@ -7529,93 +7522,6 @@ dump_prio_array(int which, ulong k_prio_array, char *u_prio_array)
 	}
 }
 
-/*
- *  CFS scheduler uses Red-Black trees to maintain run queue.
- */
-struct rb_node
-{
-        unsigned long  rb_parent_color;
-#define RB_RED          0
-#define RB_BLACK        1
-        struct rb_node *rb_right;
-        struct rb_node *rb_left;
-};
-
-struct rb_root
-{
-        struct rb_node *rb_node;
-};
-
-static struct rb_node *
-rb_first(struct rb_root *root)
-{
-        struct rb_root rloc;
-        struct rb_node *n;
-	struct rb_node nloc;
-
-	readmem((ulong)root, KVADDR, &rloc, sizeof(struct rb_root), 
-		"rb_root", FAULT_ON_ERROR);
-
-        n = rloc.rb_node;
-        if (!n)
-                return NULL;
-        while (rb_left(n, &nloc))
-		n = nloc.rb_left;
-
-        return n;
-}
-
-static struct rb_node *
-rb_parent(struct rb_node *node, struct rb_node *nloc)
-{
-	readmem((ulong)node, KVADDR, nloc, sizeof(struct rb_node), 
-		"rb_node", FAULT_ON_ERROR);
-
-	return (struct rb_node *)(nloc->rb_parent_color & ~3);
-}
-
-static struct rb_node *
-rb_right(struct rb_node *node, struct rb_node *nloc)
-{
-	readmem((ulong)node, KVADDR, nloc, sizeof(struct rb_node), 
-		"rb_node", FAULT_ON_ERROR);
-
-	return nloc->rb_right;
-}
-
-static struct rb_node *
-rb_left(struct rb_node *node, struct rb_node *nloc)
-{
-	readmem((ulong)node, KVADDR, nloc, sizeof(struct rb_node), 
-		"rb_node", FAULT_ON_ERROR);
-
-	return nloc->rb_left;
-}
-
-static struct rb_node *
-rb_next(struct rb_node *node)
-{
-	struct rb_node nloc;
-        struct rb_node *parent;
-
-	parent = rb_parent(node, &nloc);
-
-	if (parent == node)
-		return NULL;
-
-        if (nloc.rb_right) {
-		node = nloc.rb_right;
-		while (rb_left(node, &nloc))
-			node = nloc.rb_left;
-		return node;
-	}
-
-        while ((parent = rb_parent(node, &nloc)) && (node == rb_right(parent, &nloc)))
-                node = parent;
-
-        return parent;
-}
-
 #define MAX_GROUP_NUM 200
 struct task_group_info {
 	int use;
diff --git a/tools.c b/tools.c
index d8dd04a..e9dd2b1 100755
--- a/tools.c
+++ b/tools.c
@@ -5555,3 +5555,98 @@ strlcpy(char *dest, char *src, size_t size)
 	return ret;
 }
 
+struct rb_node *
+rb_first(struct rb_root *root)
+{
+        struct rb_root rloc;
+        struct rb_node *n;
+	struct rb_node nloc;
+
+	readmem((ulong)root, KVADDR, &rloc, sizeof(struct rb_root), 
+		"rb_root", FAULT_ON_ERROR);
+
+        n = rloc.rb_node;
+        if (!n)
+                return NULL;
+        while (rb_left(n, &nloc))
+		n = nloc.rb_left;
+
+        return n;
+}
+
+struct rb_node *
+rb_parent(struct rb_node *node, struct rb_node *nloc)
+{
+	readmem((ulong)node, KVADDR, nloc, sizeof(struct rb_node), 
+		"rb_node", FAULT_ON_ERROR);
+
+	return (struct rb_node *)(nloc->rb_parent_color & ~3);
+}
+
+struct rb_node *
+rb_right(struct rb_node *node, struct rb_node *nloc)
+{
+	readmem((ulong)node, KVADDR, nloc, sizeof(struct rb_node), 
+		"rb_node", FAULT_ON_ERROR);
+
+	return nloc->rb_right;
+}
+
+struct rb_node *
+rb_left(struct rb_node *node, struct rb_node *nloc)
+{
+	readmem((ulong)node, KVADDR, nloc, sizeof(struct rb_node), 
+		"rb_node", FAULT_ON_ERROR);
+
+	return nloc->rb_left;
+}
+
+struct rb_node *
+rb_next(struct rb_node *node)
+{
+	struct rb_node nloc;
+        struct rb_node *parent;
+
+	parent = rb_parent(node, &nloc);
+
+	if (parent == node)
+		return NULL;
+
+        if (nloc.rb_right) {
+		node = nloc.rb_right;
+		while (rb_left(node, &nloc))
+			node = nloc.rb_left;
+		return node;
+	}
+
+        while ((parent = rb_parent(node, &nloc)) && (node == rb_right(parent, &nloc)))
+                node = parent;
+
+        return parent;
+}
+
+struct rb_node *
+rb_last(struct rb_root *root)
+{
+	struct rb_node *node;
+	struct rb_node nloc;
+
+	readmem((ulong)root, KVADDR, &node, sizeof(node), "rb_root node",
+		FAULT_ON_ERROR);
+
+	while (1) {
+		if (!node)
+			break;
+
+		readmem((ulong)node, KVADDR, &nloc, sizeof(struct rb_node),
+		"rb_node last", FAULT_ON_ERROR);
+
+		/*  meet the last one  */
+		if (!nloc.rb_right)
+			break;
+
+		node = nloc.rb_right;
+	}
+
+	return node;
+}
-- 
1.7.1

