Skip to contentSkip to conversation

waitpid: traverse pid task list instead of children or ptraced list

PATCH New Min Zhang 1 participant 1 message Nov 22, 2011

lore
Share & tools
lore

Min Zhang

author
#0 lore raw

PATCH 1/1 waitpid: traverse pid task list instead of children or ptraced list

Shorten waitpid system call time by traversing pid task list intead of entire children or ptraced list when waiting for specific PIDTYPE_PID/PGID/SID.

Tested by fork thousands child processes, PTRACE_ATTACH on each, then waitpid on each. Result shows saving 2 order of magnitude of time.

---
 kernel/exit.c |   26 ++++++++++++++++++++++++++
 1 files changed, 26 insertions(+), 0 deletions(-)
kernel/exit.c +26 -0
index 2913b35..89c5d51 100644
@@ -1634,6 +1634,19 @@ static int do_wait_thread(struct wait_opts *wo, struct task_struct *tsk)
 {
 	struct task_struct *p;
 
+	/* traverse pid task list for a specific child pid */
+	if (wo->wo_type != PIDTYPE_MAX) {
+		do_each_pid_task(wo->wo_pid, wo->wo_type, p) {
+			if (p->parent == tsk) {
+				int ret = wait_consider_task(wo, 0, p);
+				if (ret)
+					return ret;
+			}
+		} while_each_pid_task(wo->wo_pid, wo->wo_type, p);
+		return 0;
+	}
+
+	/* traverse entire children list for any child pid */
 	list_for_each_entry(p, &tsk->children, sibling) {
 		int ret = wait_consider_task(wo, 0, p);
 		if (ret)
@@ -1647,6 +1660,19 @@ static int ptrace_do_wait(struct wait_opts *wo, struct task_struct *tsk)
 {
 	struct task_struct *p;
 
+	/* traverse pid task list for a specific child pid */
+	if (wo->wo_type != PIDTYPE_MAX) {
+		do_each_pid_task(wo->wo_pid, wo->wo_type, p) {
+			if (task_is_traced(p) && p->parent == tsk) {
+				int ret = wait_consider_task(wo, 1, p);
+				if (ret)
+					return ret;
+			}
+		} while_each_pid_task(wo->wo_pid, wo->wo_type, p);
+		return 0;
+	}
+
+	/* traverse entire children list for any child pid */
 	list_for_each_entry(p, &tsk->ptraced, ptrace_entry) {
 		int ret = wait_consider_task(wo, 1, p);
 		if (ret)
Signature · 1 line
1.6.4.2