linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] sched: Optimize pick_next_task for idle_sched_class too
@ 2017-01-19 15:17 Steven Rostedt
  2017-01-19 17:44 ` Peter Zijlstra
  0 siblings, 1 reply; 19+ messages in thread
From: Steven Rostedt @ 2017-01-19 15:17 UTC (permalink / raw)
  To: LKML; +Cc: Peter Zijlstra, Ingo Molnar, Andrew Morton

When running my likely/unlikely profiler, I noticed that the
SCHED_DEADLINE's pick_next_task_dl() unlikely case of
(!dl_rq->dl_nr_running) was always being hit. There's two cases where
this can happen.

First, there's an optimization in pick_next_task() for the likely case
that the only tasks running on the run queue are SCHED_OTHER tasks. In
a normal system, this is the case most of the time. When this is true,
only the pick_next_task() of the fair_sched_class is called. If an RT or
DEADLINE task is queued, then the other pick_next_task()s of the other
sched classes are called in sequence.

The SCHED_DEADLINE pick_next_task() is called first, and that
unlikely() case is hit if there's no deadline tasks available. This
happens when an RT task is queued (first case). But tracing revealed
that this happens in another very common case. The case where the
system goes from idle to running any task, including SCHED_OTHER. This
is because the idle task has a different sched class than the
fair_sched_class.

The optimization has:

	if (prev->sched_class == fair_sched_class &&
	    rq->nr_running == rq->cfs.h_nr_running) {

When going from SCHED_OTHER to idle, this optimization is hit, because
the SCHED_OTHER task is of the fair_sched_class, and rq->nr_running and
rq->cfs.h_nr_running are both zero. But when we go from idle to
SCHED_OTHER, the first test fails. prev->sched_class is equal to
idle_sched_class, and this causes both the pick_next_task() of deadline
and RT sched classes to be called unnecessarily.

Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
---
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 154fd68..e2c6d3b 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -3259,13 +3259,15 @@ static inline struct task_struct *
 pick_next_task(struct rq *rq, struct task_struct *prev, struct pin_cookie cookie)
 {
 	const struct sched_class *class = &fair_sched_class;
+	const struct sched_class *idle_class = &idle_sched_class;
 	struct task_struct *p;
 
 	/*
 	 * Optimization: we know that if all tasks are in
 	 * the fair class we can call that function directly:
 	 */
-	if (likely(prev->sched_class == class &&
+	if (likely((prev->sched_class == class ||
+		    prev->sched_class == idle_class) &&
 		   rq->nr_running == rq->cfs.h_nr_running)) {
 		p = fair_sched_class.pick_next_task(rq, prev, cookie);
 		if (unlikely(p == RETRY_TASK))

^ permalink raw reply related	[flat|nested] 19+ messages in thread

end of thread, other threads:[~2017-03-01 16:45 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-01-19 15:17 [PATCH] sched: Optimize pick_next_task for idle_sched_class too Steven Rostedt
2017-01-19 17:44 ` Peter Zijlstra
2017-01-20 16:14   ` Steven Rostedt
2017-01-20 16:16     ` Steven Rostedt
2017-01-20 16:52     ` Peter Zijlstra
2017-01-30 11:54   ` [tip:sched/core] sched/core: Optimize pick_next_task() for idle_sched_class tip-bot for Peter Zijlstra
2017-02-23 10:34   ` [PATCH] sched: Optimize pick_next_task for idle_sched_class too Pavan Kondeti
2017-02-23 13:54     ` Peter Zijlstra
2017-02-23 15:15       ` Pavan Kondeti
2017-02-23 15:25         ` Peter Zijlstra
2017-02-23 16:37           ` Peter Zijlstra
2017-02-23 17:29             ` Pavan Kondeti
2017-02-23 17:45               ` Peter Zijlstra
2017-02-23 17:54                 ` Peter Zijlstra
2017-02-27 17:54                   ` Steven Rostedt
2017-03-01 15:53         ` Steven Rostedt
2017-03-01 16:03           ` Peter Zijlstra
2017-03-01 16:19             ` Steven Rostedt
2017-03-01 16:22               ` Peter Zijlstra

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).