Skip to contentSkip to conversation

sched/fair: micro-optimize pick_next_entity()

PATCH RESEND In review Li RongQing 2 participants 4 messages Aug 25, 2021 – Sep 3, 2021 Scheduler

lore
Share & tools
lore
#0 lore raw

PATCH RESEND 1/1 sched/fair: micro-optimize pick_next_entity()

Only check the skip buddy when next buddy and last buddy are not picked up, this can save the cycles of checking the skip buddy and computation of the second buddy, when next and last buddy will be picked up
for example, yield_to_task_fair() set both next and skip buddy

---
 kernel/sched/fair.c | 33 ++++++++++++++++-----------------
 1 file changed, 16 insertions(+), 17 deletions(-)
kernel/sched/fair.c +16 -17
index 44c452072a1b..9c6569ddf3eb 100644
@@ -4503,11 +4503,22 @@ pick_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *curr)
 
 	se = left; /* ideally we run the leftmost entity */
 
-	/*
-	 * Avoid running the skip buddy, if running something else can
-	 * be done without getting too unfair.
-	 */
-	if (cfs_rq->skip && cfs_rq->skip == se) {
+
+	if (cfs_rq->next && wakeup_preempt_entity(cfs_rq->next, left) < 1) {
+		/*
+		 * Someone really wants this to run. If it's not unfair, run it.
+		 */
+		se = cfs_rq->next;
+	} else if (cfs_rq->last && wakeup_preempt_entity(cfs_rq->last, left) < 1) {
+		/*
+		 * Prefer last buddy, try to return the CPU to a preempted task.
+		 */
+		se = cfs_rq->last;
+	} else if (cfs_rq->skip && cfs_rq->skip == se) {
+		/*
+		 * Avoid running the skip buddy, if running something else can
+		 * be done without getting too unfair.
+		 */
 		struct sched_entity *second;
 
 		if (se == curr) {
@@ -4522,18 +4533,6 @@ pick_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *curr)
 			se = second;
 	}
 
-	if (cfs_rq->next && wakeup_preempt_entity(cfs_rq->next, left) < 1) {
-		/*
-		 * Someone really wants this to run. If it's not unfair, run it.
-		 */
-		se = cfs_rq->next;
-	} else if (cfs_rq->last && wakeup_preempt_entity(cfs_rq->last, left) < 1) {
-		/*
-		 * Prefer last buddy, try to return the CPU to a preempted task.
-		 */
-		se = cfs_rq->last;
-	}
-
 	return se;
 }
 
Signature · 1 line
2.33.0.69.gc420321.dirty
maintainer
#1 lore raw
Quoted 6 lines · Li RongQing

Only check the skip buddy when next buddy and last buddy are not picked up, this can save the cycles of checking the skip buddy and computation of the second buddy, when next and last buddy will be picked up
for example, yield_to_task_fair() set both next and skip buddy

Is that actually measurable?

But looking at it, should we not, instead, move the whole ->skip thing to the bottom, so we unconditionally check it vs the result of ->next/->last ?

Imagine ->next == ->skip, then we want to avoid running it and not have ->next win.

#2 lore raw
Quoted 11 lines

-----邮件原件-----
发件人: Peter Zijlstra <[email protected]>
发送时间: 2021年8月31日 22:10
收件人: Li,Rongqing <[email protected]>
抄送: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected] 主题: Re: [Resend][PATCH] sched/fair: micro-optimize pick_next_entity()

Quoted 9 lines
Quoted 5 lines · Li RongQing

Only check the skip buddy when next buddy and last buddy are not picked up, this can save the cycles of checking the skip buddy and computation of the second buddy, when next and last buddy will be picked up for example, yield_to_task_fair() set both next and skip buddy

Is that actually measurable?

No measurable

Quoted 6 lines

But looking at it, should we not, instead, move the whole ->skip thing to the bottom, so we unconditionally check it vs the result of ->next/->last ?

Imagine ->next == ->skip, then we want to avoid running it and not have ->next win.

True, next/last may be equal to skip

-Li

#3 lore raw
In reply to Peter Zijlstra
Quoted 18 lines

-----邮件原件-----
发件人: Peter Zijlstra <[email protected]>
发送时间: 2021年8月31日 22:10
收件人: Li,Rongqing <[email protected]>
抄送: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected] 主题: Re: [Resend][PATCH] sched/fair: micro-optimize pick_next_entity()

Quoted 5 lines · Li RongQing

Only check the skip buddy when next buddy and last buddy are not picked up, this can save the cycles of checking the skip buddy and computation of the second buddy, when next and last buddy will be picked up for example, yield_to_task_fair() set both next and skip buddy

Quoted 8 lines

Is that actually measurable?

But looking at it, should we not, instead, move the whole ->skip thing to the bottom, so we unconditionally check it vs the result of ->next/->last ?

Imagine ->next == ->skip, then we want to avoid running it and not have ->next win.

skip means this task is running on cpu, next is the blocked task and will be running.

I donot know how they can be same.

-Li