Skip to contentSkip to conversation

sched: Skip double execution of pick_next_task_fair

PATCH Superseded Tim Chen 3 participants 5 messages Apr 23, 2014 – May 8, 2014 Scheduler

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Tim Chen

author
#0 lore raw

PATCH 1/1 sched: Skip double execution of pick_next_task_fair

The current code will call pick_next_task_fair a second time in the slow path if we did not pull any task in our first try. This is really unnecessary as we already know no task can be pulled and it doubles the delay for the cpu to enter idle.

We instrumented some network workloads and that saw that pick_next_task_fair is frequently called twice before a cpu enters idle. The call to pick_next_task_fair can add
non trivial latency as it calls load_balance which runs find_busiest_group on an hierarchy of sched domains spanning the cpus for a large system. For some 4 socket systems, we saw almost 0.25 msec spent per call of pick_next_task_fair before a cpu can be idled.

This patch skips pick_next_task_fair in the slow path if it has already been invoked.

Tim

---
 kernel/sched/core.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
kernel/sched/core.c +7 -1
index 1d1b87b..4053437 100644
@@ -2583,6 +2583,7 @@ pick_next_task(struct rq *rq, struct task_struct *prev)
 {
 	const struct sched_class *class = &fair_sched_class;
 	struct task_struct *p;
+	int skip_fair = 0;
 
 	/*
 	 * Optimization: we know that if all tasks are in
@@ -2591,12 +2592,17 @@ pick_next_task(struct rq *rq, struct task_struct *prev)
 	if (likely(prev->sched_class == class &&
 		   rq->nr_running == rq->cfs.h_nr_running)) {
 		p = fair_sched_class.pick_next_task(rq, prev);
-		if (likely(p && p != RETRY_TASK))
+		if (!p)
+			skip_fair = 1;
+		else if (likely(p != RETRY_TASK))
 			return p;
 	}
 
 again:
 	for_each_class(class) {
+		/* if we have already failed to pull fair task, skip */
+		if (class == &fair_sched_class && skip_fair)
+			continue;
 		p = class->pick_next_task(rq, prev);
 		if (p) {
 			if (unlikely(p == RETRY_TASK))
Signature · 1 line
1.7.11.7
maintainer
#1 lore raw
Quoted 13 lines · Tim Chen

The current code will call pick_next_task_fair a second time in the slow path if we did not pull any task in our first try. This is really unnecessary as we already know no task can be pulled and it doubles the delay for the cpu to enter idle.

We instrumented some network workloads and that saw that pick_next_task_fair is frequently called twice before a cpu enters idle. The call to pick_next_task_fair can add
non trivial latency as it calls load_balance which runs find_busiest_group on an hierarchy of sched domains spanning the cpus for a large system. For some 4 socket systems, we saw almost 0.25 msec spent per call of pick_next_task_fair before a cpu can be idled.

Quoted 2 lines

This patch skips pick_next_task_fair in the slow path if it has already been invoked.

How about something like so?

Its a little more contained.

kernel/sched/core.c +8 -2
@@ -2636,8 +2636,14 @@ pick_next_task(struct rq *rq, struct tas
 	if (likely(prev->sched_class == class &&
 		   rq->nr_running == rq->cfs.h_nr_running)) {
 		p = fair_sched_class.pick_next_task(rq, prev);
-		if (likely(p && p != RETRY_TASK))
-			return p;
+		if (unlikely(p == RETRY_TASK))
+			goto again;
+
+		/* assumes fair_sched_class->next == idle_sched_class */
+		if (unlikely(!p))
+			p = pick_next_task_idle(rq, prev);
+
+		return p;
 	}
 
 again:
author
#2 lore raw
Quoted 14 lines · Peter Zijlstra
Quoted 13 lines · Tim Chen

The current code will call pick_next_task_fair a second time in the slow path if we did not pull any task in our first try. This is really unnecessary as we already know no task can be pulled and it doubles the delay for the cpu to enter idle.

We instrumented some network workloads and that saw that pick_next_task_fair is frequently called twice before a cpu enters idle. The call to pick_next_task_fair can add
non trivial latency as it calls load_balance which runs find_busiest_group on an hierarchy of sched domains spanning the cpus for a large system. For some 4 socket systems, we saw almost 0.25 msec spent per call of pick_next_task_fair before a cpu can be idled.

Quoted 4 lines
Quoted 2 lines

This patch skips pick_next_task_fair in the slow path if it has already been invoked.

How about something like so?

Yes, this version is more concise.

Quoted 7 lines

Its a little more contained.

kernel/sched/core.c +0 -0
@@ -2636,8 +2636,14 @@ pick_next_task(struct rq *rq, struct tas
 	if (likely(prev->sched_class == class &&

kernel/sched/core.c pick_next_task(struct rq *rq, struct tas · line 2644

Quoted 10 lines
kernel/sched/core.c +6 -2
 		   rq->nr_running == rq->cfs.h_nr_running)) {
 		p = fair_sched_class.pick_next_task(rq, prev);
-		if (likely(p && p != RETRY_TASK))
-			return p;
+		if (unlikely(p == RETRY_TASK))
+			goto again;
+
+		/* assumes fair_sched_class->next == idle_sched_class */
+		if (unlikely(!p))
+			p = pick_next_task_idle(rq, prev);

Should be

			  p = idle_sched_class.pick_next_task(rq, prev);

kernel/sched/core.c pick_next_task(struct rq *rq, struct tas · line 2649

Quoted 5 lines
kernel/sched/core.c +2 -0
+
+		return p;
 	}
 
 again:

I'll respin the patch with these changes.

Thanks.

Tim

maintainer
#3 lore raw
Quoted 20 lines · Tim Chen

Yes, this version is more concise.

Quoted 7 lines

Its a little more contained.

kernel/sched/core.c +0 -0
@@ -2636,8 +2636,14 @@ pick_next_task(struct rq *rq, struct tas
 	if (likely(prev->sched_class == class &&

kernel/sched/core.c pick_next_task(struct rq *rq, struct tas · line 2644

Quoted 10 lines
kernel/sched/core.c +6 -2
 		   rq->nr_running == rq->cfs.h_nr_running)) {
 		p = fair_sched_class.pick_next_task(rq, prev);
-		if (likely(p && p != RETRY_TASK))
-			return p;
+		if (unlikely(p == RETRY_TASK))
+			goto again;
+
+		/* assumes fair_sched_class->next == idle_sched_class */
+		if (unlikely(!p))
+			p = pick_next_task_idle(rq, prev);

Should be

Quoted 1 line
			  p = idle_sched_class.pick_next_task(rq, prev);

Indeed, already fixed that when my compiler complained.

Quoted 7 lines

kernel/sched/core.c pick_next_task(struct rq *rq, struct tas · line 2649

Quoted 5 lines
kernel/sched/core.c +2 -0
+
+		return p;
 	}
 
 again:

I'll respin the patch with these changes.

No need; I have the following queued:

---
Subject: sched: Skip double execution of pick_next_task_fair
From: Peter Zijlstra <[email protected]>
Date: Thu, 24 Apr 2014 12:00:47 +0200

Tim wrote:

The current code will call pick_next_task_fair a second time in the slow path if we did not pull any task in our first try. This is really unnecessary as we already know no task can be pulled and it doubles the delay for the cpu to enter idle.

We instrumented some network workloads and that saw that pick_next_task_fair is frequently called twice before a cpu enters idle. The call to pick_next_task_fair can add non trivial latency as it calls load_balance which runs find_busiest_group on an hierarchy of sched domains spanning the cpus for a large system. For some 4 socket systems, we saw almost 0.25 msec spent per call of pick_next_task_fair before a cpu can be idled.

---

---
 kernel/sched/core.c |   10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
kernel/sched/core.c +8 -2
@@ -2605,8 +2605,14 @@ pick_next_task(struct rq *rq, struct tas
 	if (likely(prev->sched_class == class &&
 		   rq->nr_running == rq->cfs.h_nr_running)) {
 		p = fair_sched_class.pick_next_task(rq, prev);
-		if (likely(p && p != RETRY_TASK))
-			return p;
+		if (unlikely(p == RETRY_TASK))
+			goto again;
+
+		/* assumes fair_sched_class->next == idle_sched_class */
+		if (unlikely(!p))
+			p = idle_sched_class.pick_next_task(rq, prev);
+
+		return p;
 	}
 
 again:
#4 lore raw

[tip:sched/core] sched: Skip double execution of pick_next_task_fair()

In reply to Peter Zijlstra
sched: Skip double execution of pick_next_task_fair()
Commit-ID:  6ccdc84b81a0a6c09a7f0427761d2f8cecfc2218
Gitweb:     http://git.kernel.org/tip/6ccdc84b81a0a6c09a7f0427761d2f8cecfc2218
Author:     Peter Zijlstra <[email protected]>
AuthorDate: Thu, 24 Apr 2014 12:00:47 +0200
Committer:  Ingo Molnar <[email protected]>
CommitDate: Wed, 7 May 2014 11:51:35 +0200

sched: Skip double execution of pick_next_task_fair()

Tim wrote:

"The current code will call pick_next_task_fair a second time in the
slow path if we did not pull any task in our first try. This is really unnecessary as we already know no task can be pulled and it doubles the delay for the cpu to enter idle.

We instrumented some network workloads and that saw that pick_next_task_fair is frequently called twice before a cpu enters idle. The call to pick_next_task_fair can add non trivial latency as it calls load_balance which runs find_busiest_group on an hierarchy of sched domains spanning the cpus for a large system. For some 4 socket systems, we saw almost 0.25 msec spent per call of pick_next_task_fair before a cpu can be idled."

Optimize the second call away for the common case and document the dependency.

---
 kernel/sched/core.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
kernel/sched/core.c +8 -2
index e62c65a..28921ec 100644
@@ -2592,8 +2592,14 @@ pick_next_task(struct rq *rq, struct task_struct *prev)
 	if (likely(prev->sched_class == class &&
 		   rq->nr_running == rq->cfs.h_nr_running)) {
 		p = fair_sched_class.pick_next_task(rq, prev);
-		if (likely(p && p != RETRY_TASK))
-			return p;
+		if (unlikely(p == RETRY_TASK))
+			goto again;
+
+		/* assumes fair_sched_class->next == idle_sched_class */
+		if (unlikely(!p))
+			p = idle_sched_class.pick_next_task(rq, prev);
+
+		return p;
 	}
 
 again: