Skip to contentSkip to conversation

sched/fair: Remove the cost of a redundant cpumask_next_wrap in select_idle_cpu

PATCH Superseded Barry Song 2 participants 3 messages Nov 23, 2021 – Nov 24, 2021 Scheduler

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Barry Song

author
#0 lore raw

PATCH 1/1 sched/fair: Remove the cost of a redundant cpumask_next_wrap in select_idle_cpu

From: Barry Song <[email protected]>

This patch keeps the same scanning amount, but drops a redundant loop of cpumask_next_wrap.
The original code did for_each_cpu_wrap(cpu, cpus, target + 1), then checked --nr; this patch does --nr before doing the next loop, thus, it can remove a cpumask_next_wrap() which costs a little bit.

---
 kernel/sched/fair.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
kernel/sched/fair.c +4 -4
index ff69f24..e2fb3e0 100644
@@ -6298,9 +6298,9 @@ static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, bool
 
 		span_avg = sd->span_weight * avg_idle;
 		if (span_avg > 4*avg_cost)
-			nr = div_u64(span_avg, avg_cost);
+			nr = div_u64(span_avg, avg_cost) - 1;
 		else
-			nr = 4;
+			nr = 3;
 
 		time = cpu_clock(this);
 	}
@@ -6312,11 +6312,11 @@ static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, bool
 				return i;
 
 		} else {
-			if (!--nr)
-				return -1;
 			idle_cpu = __select_idle_cpu(cpu, p);
 			if ((unsigned int)idle_cpu < nr_cpumask_bits)
 				break;
+			if (!--nr)
+				return -1;
 		}
 	}
 
Signature · 1 line
1.8.3.1
maintainer
#1 lore raw
Quoted 32 lines · Barry Song

From: Barry Song <[email protected]>

This patch keeps the same scanning amount, but drops a redundant loop of cpumask_next_wrap.
The original code did for_each_cpu_wrap(cpu, cpus, target + 1), then checked --nr; this patch does --nr before doing the next loop, thus, it can remove a cpumask_next_wrap() which costs a little bit.

---
 kernel/sched/fair.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
kernel/sched/fair.c +2 -2
index ff69f24..e2fb3e0 100644
@@ -6298,9 +6298,9 @@ static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, bool
 
 		span_avg = sd->span_weight * avg_idle;
 		if (span_avg > 4*avg_cost)
-			nr = div_u64(span_avg, avg_cost);
+			nr = div_u64(span_avg, avg_cost) - 1;
 		else
-			nr = 4;
+			nr = 3;
 
 		time = cpu_clock(this);
 	}
@@ -6312,11 +6312,11 @@ static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, bool
 				return i;
 

kernel/sched/fair.c static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, bool · line 6321

Quoted 10 lines
kernel/sched/fair.c +2 -2
 		} else {
-			if (!--nr)
-				return -1;
 			idle_cpu = __select_idle_cpu(cpu, p);
 			if ((unsigned int)idle_cpu < nr_cpumask_bits)
 				break;
+			if (!--nr)
+				return -1;
 		}
 	}

That's just confusing code. Isn't it much clearer to write the whole thing like so ?

	nr--;
	for_each_cpu_wrap(cpu, cpus, target+1) {
		...
		if (!nr--)
			return -1;
	}
author
#2 lore raw
Quoted 48 lines · Peter Zijlstra
Quoted 32 lines · Barry Song

From: Barry Song <[email protected]>

This patch keeps the same scanning amount, but drops a redundant loop of cpumask_next_wrap.
The original code did for_each_cpu_wrap(cpu, cpus, target + 1), then checked --nr; this patch does --nr before doing the next loop, thus, it can remove a cpumask_next_wrap() which costs a little bit.

---
 kernel/sched/fair.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
kernel/sched/fair.c +2 -2
index ff69f24..e2fb3e0 100644
@@ -6298,9 +6298,9 @@ static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, bool
 
              span_avg = sd->span_weight * avg_idle;
              if (span_avg > 4*avg_cost)
-                     nr = div_u64(span_avg, avg_cost);
+                     nr = div_u64(span_avg, avg_cost) - 1;
              else
-                     nr = 4;
+                     nr = 3;
 
              time = cpu_clock(this);
      }
@@ -6312,11 +6312,11 @@ static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, bool
                              return i;
 

kernel/sched/fair.c static int select_idle_cpu(struct task_struct *p, struct sched_domain *sd, bool · line 6321

Quoted 10 lines
kernel/sched/fair.c +2 -2
              } else {
-                     if (!--nr)
-                             return -1;
                      idle_cpu = __select_idle_cpu(cpu, p);
                      if ((unsigned int)idle_cpu < nr_cpumask_bits)
                              break;
+                     if (!--nr)
+                             return -1;
              }
      }

That's just confusing code. Isn't it much clearer to write the whole thing like so ?

Quoted 1 line
        nr--;

this is fine to avoid the code of setting 4 to 3 and setting div_u64(span_avg, avg_cost) to
div_u64(span_avg, avg_cost) - 1;

Quoted 3 lines
        for_each_cpu_wrap(cpu, cpus, target+1) {
                ...
                if (!nr--)

I guess you mean if(!--nr).

For example, if nr=4, the original code will only check 3 cpus for __select_idle_cpu.
the new code "nr--" will check 4 cpus for __select_idle_cpu. to keep the amount untouched, the code should be --nr.

Quoted 3 lines
                        return -1;
        }

Thanks
Barry