sched/fair: Refactor can_migrate_task() to elimate looping
RFC PATCH v2 1/1 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|
-------------------------------------------------------
Also compare code generation before and after the change using script/bloat-o-meter, result shown below
$ ./scripts/bloat-o-meter fair_old.o fair_new.o add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-85 (-85) Function old new delta can_migrate_task 519 434 -85 Total: Before=45778, After=45693, chg -0.19%
---
v1 -> v2:
- Use cpumask_first_and_and()
- Remove additional cpumask
- Compare code generation with bloat-o-meter
Test is done on Linux 6.9.0-0-generic x86_64 with
Intel(R) Core(TM) i7-2600 CPU @ 3.40GHz
Test is executed in the form of kernel module.
Test script:
int init_module(void)
{
struct cpumask cur_mask, custom_mask;
struct task_struct *p = current;
int cpu, cpu1 = nr_cpu_ids, cpu2 = nr_cpu_ids;
unsigned tmp = 0;
cpumask_copy(&cur_mask, cpu_online_mask);
/* Self-implemented function, didn't paste here because the length */
generate_random_cpumask(&custom_mask);
ktime_t start_1 = ktime_get();
for_each_cpu_and(cpu, &cur_mask, &custom_mask) {
if (cpumask_test_cpu(cpu, p->cpus_ptr)) {
/* imitate load balance operation */
tmp |= 0x01010101;
cpu1 = cpu;
break;
}
}
ktime_t end_1 = ktime_get();
ktime_t start_2 = ktime_get();
cpu = cpumask_first_and_and(&cur_mask, &custom_mask, p->cpus_ptr);
if (cpu < nr_cpu_ids) {
/* imitate load balance operation */
tmp |= 0x01010101;
cpu2 = cpu;
}
ktime_t end_2 = ktime_get();
if (cpu1 != cpu2) {
pr_err("Failed Assertion, cpu1 = %d, cpu2 = %d\n", cpu1, cpu2);
return 0;
}
pr_info("Old method spend time : %lld\n", ktime_to_ns(end_1 - start_1));
pr_info("New method spend time : %lld\n", ktime_to_ns(end_2 - start_2));
return 0;
}
---
kernel/sched/fair.c | 11 +++++------
1 file changed, 5 insertions(+), 6 deletions(-)kernel/sched/fair.c +5 -6
@@ -9404,12 +9404,11 @@ int can_migrate_task(struct task_struct *p, struct lb_env *env)return 0 ;/* Prevent to re-select dst_cpu via env's CPUs: */ -for_each_cpu_and (cpu, env->dst_grpmask, env->cpus) { -if (cpumask_test_cpu (cpu, p->cpus_ptr)) { - env->flags |=LBF_DST_PINNED ; - env->new_dst_cpu = cpu; -break ; - } + cpu =cpumask_first_and_and (env->dst_grpmask, env->cpus, p->cpus_ptr); + +if (cpu < nr_cpu_ids) { + env->flags |=LBF_DST_PINNED ; + env->new_dst_cpu = cpu; }return 0 ;
Signature · 1 line
2.43.0
The following commit has been merged into the sched/core branch of tip:
The following commit has been merged into the sched/core branch of tip:
Commit-ID: d34e798094ca7be935b629a42f8b237d4d5b7f1d Gitweb: https://git.kernel.org/tip/d34e798094ca7be935b629a42f8b237d4d5b7f1d Author: I Hsin Cheng <[email protected]> AuthorDate: Mon, 10 Feb 2025 18:30:18 +08:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Fri, 14 Feb 2025 10:32:01 +01:00
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|
-------------------------------------------------------
--- kernel/sched/fair.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-)
kernel/sched/fair.c +5 -6
@@ -9439,12 +9439,11 @@ int can_migrate_task(struct task_struct *p, struct lb_env *env)return 0 ;/* Prevent to re-select dst_cpu via env's CPUs: */ -for_each_cpu_and (cpu, env->dst_grpmask, env->cpus) { -if (cpumask_test_cpu (cpu, p->cpus_ptr)) { - env->flags |=LBF_DST_PINNED ; - env->new_dst_cpu = cpu; -break ; - } + cpu =cpumask_first_and_and (env->dst_grpmask, env->cpus, p->cpus_ptr); + +if (cpu < nr_cpu_ids) { + env->flags |=LBF_DST_PINNED ; + env->new_dst_cpu = cpu; }return 0 ;