[CRIU] [PATCH 5/5] pstree: use rbtree to find a specified pid

Andrey Vagin avagin at openvz.org
Fri Feb 19 10:13:31 PST 2016


From: Andrew Vagin <avagin at virtuozzo.com>

Signed-off-by: Andrew Vagin <avagin at virtuozzo.com>
---
 criu/pstree.c | 36 +++++++++++++++++++++++++-----------
 1 file changed, 25 insertions(+), 11 deletions(-)

diff --git a/criu/pstree.c b/criu/pstree.c
index 61fd5d3..f82a3de 100644
--- a/criu/pstree.c
+++ b/criu/pstree.c
@@ -383,6 +383,23 @@ static int prepare_pstree_for_shell_job(void)
 	return 0;
 }
 
+static struct pid *lookup_pid(pid_t pid)
+{
+	struct rb_node *node = root_rb.rb_node;
+
+	while (node) {
+		struct pid *this = rb_entry(node, struct pid, node);
+
+		if (pid < this->virt)
+			node = node->rb_left;
+		else if (pid > this->virt)
+			node = node->rb_right;
+		else
+			return this;
+	}
+	return NULL;
+}
+
 static struct pid *insert_pid(pid_t pid, struct pid *pid_node)
 {
 	struct rb_node *node = root_rb.rb_node;
@@ -686,18 +703,15 @@ static int prepare_pstree_ids(void)
 
 	/* Add a process group leader if it is absent  */
 	for_each_pstree_item(item) {
-		struct pstree_item *gleader;
+		struct pid *pid;
 
 		if (!item->pgid || item->pid.virt == item->pgid)
 			continue;
 
-		for_each_pstree_item(gleader) {
-			if (gleader->pid.virt == item->pgid)
-				break;
-		}
-
-		if (gleader) {
-			rsti(item)->pgrp_leader = gleader;
+		pid = lookup_pid(item->pgid);
+		if (pid->state != TASK_UNDEF) {
+			BUG_ON(pid->state == TASK_THREAD);
+			rsti(item)->pgrp_leader = container_of(pid, struct pstree_item, pid);
 			continue;
 		}
 
@@ -709,9 +723,9 @@ static int prepare_pstree_ids(void)
 		if (current_pgid == item->pgid)
 			continue;
 
-		helper = alloc_pstree_helper();
-		if (helper == NULL)
-			return -1;
+		helper = container_of(pid, struct pstree_item, pid);
+		init_pstree_helper(helper);
+
 		helper->sid = item->sid;
 		helper->pgid = item->pgid;
 		helper->pid.virt = item->pgid;
-- 
2.5.0



More information about the CRIU mailing list