Skip to content

Commit d34e798

Browse files
vax-rPeter Zijlstra
authored andcommitted
sched/fair: Refactor can_migrate_task() to elimate looping
The function "can_migrate_task()" utilize "for_each_cpu_and" with a "if" statement inside to find the destination cpu. It's the same logic to find the first set bit of the result of the bitwise-AND of "env->dst_grpmask", "env->cpus" and "p->cpus_ptr". Refactor it by using "cpumask_first_and_and()" to perform bitwise-AND for "env->dst_grpmask", "env->cpus" and "p->cpus_ptr" and pick the first cpu within the intersection as the destination cpu, so we can elimate the need of looping and multiple times of branch. After the refactoring this part of the code can speed up from ~115ns to ~54ns, according to the test below. Ran the test for 5 times and the result is showned in the following table, and the test script is paste in next section. ------------------------------------------------------- |Old method| 130| 118| 115| 109| 106| avg ~115ns| ------------------------------------------------------- |New method| 58| 55| 54| 48| 55| avg ~54ns| ------------------------------------------------------- Signed-off-by: I Hsin Cheng <richard120310@gmail.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Link: https://lkml.kernel.org/r/20250210103019.283824-1-richard120310@gmail.com
1 parent 563bc21 commit d34e798

1 file changed

Lines changed: 5 additions & 6 deletions

File tree

kernel/sched/fair.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9439,12 +9439,11 @@ int can_migrate_task(struct task_struct *p, struct lb_env *env)
94399439
return 0;
94409440

94419441
/* Prevent to re-select dst_cpu via env's CPUs: */
9442-
for_each_cpu_and(cpu, env->dst_grpmask, env->cpus) {
9443-
if (cpumask_test_cpu(cpu, p->cpus_ptr)) {
9444-
env->flags |= LBF_DST_PINNED;
9445-
env->new_dst_cpu = cpu;
9446-
break;
9447-
}
9442+
cpu = cpumask_first_and_and(env->dst_grpmask, env->cpus, p->cpus_ptr);
9443+
9444+
if (cpu < nr_cpu_ids) {
9445+
env->flags |= LBF_DST_PINNED;
9446+
env->new_dst_cpu = cpu;
94489447
}
94499448

94509449
return 0;

0 commit comments

Comments
 (0)