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

Pavel Emelyanov xemul at virtuozzo.com
Wed Mar 9 08:56:26 PST 2016


On 03/09/2016 07:50 PM, Andrew Vagin wrote:
> On Wed, Mar 09, 2016 at 02:03:34PM +0300, Pavel Emelyanov wrote:
>> On 02/19/2016 09:13 PM, Andrey Vagin wrote:
>>> 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)
>>
>> We already have pstree_item_by_virt and pstree_item_by_real. Why do we need one more?
> 
> "struct pid" is used for threads too

Hm... OK, fix the pstree_item_by_<whatever_appropriate> to use this helper.
And how about renaming the lookup_pid into pid_by_virt/_real to make symmetrical
naming?

>>
>>> +{
>>> +	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;
>>>
>>
> .
> 



More information about the CRIU mailing list