Skip to contentSkip to conversation

sched: minor optimization

PATCH In review Nikanth Karthikesan 2 participants 2 messages Nov 23, 2007

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sched: minor optimization

As an optimization, if all tasks are in the fair class, the next task is directly picked from fair_sched_class. But, if it returns no task we go through again from sched_class_highest which could be avoided and instead return the idle task directly.

---
 kernel/sched.c |    2 ++
 1 file changed, 2 insertions(+)
kernel/sched.c +2 -0
Index: b/kernel/sched.c
===================================================================
@@ -3449,6 +3449,8 @@ pick_next_task(struct rq *rq, struct tas
 		p = fair_sched_class.pick_next_task(rq);
 		if (likely(p))
 			return p;
+		else /* rq->nr_running is zero */
+			return idle_sched_class.pick_next_task(rq);
 	}
 
 	class = sched_class_highest;
#1 lore raw
Quoted 4 lines · Nikanth Karthikesan

As an optimization, if all tasks are in the fair class, the next task is directly picked from fair_sched_class. But, if it returns no task we go through again from sched_class_highest which could be avoided and instead return the idle task directly.

The only legitimate possibility of having the fair_sched_class returning no task in this case is when 'rq->nr_running == rq->cfs.nr_running == 0'.

iow, a possible optimization would be just the following check :

if (rq->nr_running == 0)

        return idle_sched_class.pick_next_task(rq);

at the beginning of pick_next_task().

(or maybe put it at the beginning of the
if (likely(rq->nr_running == rq->cfs.nr_running)) {} block as we already have 'likely()' there).

Signature · 2 lines
Best regards,
Dmitry Adamushko