Skip to content

Commit e938b9c

Browse files
Wanpeng LiIngo Molnar
authored andcommitted
sched/nohz: Optimize get_nohz_timer_target()
On a machine, CPU 0 is used for housekeeping, the other 39 CPUs in the same socket are in nohz_full mode. We can observe huge time burn in the loop for seaching nearest busy housekeeper cpu by ftrace. 2) | get_nohz_timer_target() { 2) 0.240 us | housekeeping_test_cpu(); 2) 0.458 us | housekeeping_test_cpu(); ... 2) 0.292 us | housekeeping_test_cpu(); 2) 0.240 us | housekeeping_test_cpu(); 2) 0.227 us | housekeeping_any_cpu(); 2) + 43.460 us | } This patch optimizes the searching logic by finding a nearest housekeeper CPU in the housekeeping cpumask, it can minimize the worst searching time from ~44us to < 10us in my testing. In addition, the last iterated busy housekeeper can become a random candidate while current CPU is a better fallback if it is a housekeeper. Signed-off-by: Wanpeng Li <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Signed-off-by: Ingo Molnar <[email protected]> Reviewed-by: Frederic Weisbecker <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent b562d14 commit e938b9c

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

kernel/sched/core.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -552,27 +552,32 @@ void resched_cpu(int cpu)
552552
*/
553553
int get_nohz_timer_target(void)
554554
{
555-
int i, cpu = smp_processor_id();
555+
int i, cpu = smp_processor_id(), default_cpu = -1;
556556
struct sched_domain *sd;
557557

558-
if (!idle_cpu(cpu) && housekeeping_cpu(cpu, HK_FLAG_TIMER))
559-
return cpu;
558+
if (housekeeping_cpu(cpu, HK_FLAG_TIMER)) {
559+
if (!idle_cpu(cpu))
560+
return cpu;
561+
default_cpu = cpu;
562+
}
560563

561564
rcu_read_lock();
562565
for_each_domain(cpu, sd) {
563-
for_each_cpu(i, sched_domain_span(sd)) {
566+
for_each_cpu_and(i, sched_domain_span(sd),
567+
housekeeping_cpumask(HK_FLAG_TIMER)) {
564568
if (cpu == i)
565569
continue;
566570

567-
if (!idle_cpu(i) && housekeeping_cpu(i, HK_FLAG_TIMER)) {
571+
if (!idle_cpu(i)) {
568572
cpu = i;
569573
goto unlock;
570574
}
571575
}
572576
}
573577

574-
if (!housekeeping_cpu(cpu, HK_FLAG_TIMER))
575-
cpu = housekeeping_any_cpu(HK_FLAG_TIMER);
578+
if (default_cpu == -1)
579+
default_cpu = housekeeping_any_cpu(HK_FLAG_TIMER);
580+
cpu = default_cpu;
576581
unlock:
577582
rcu_read_unlock();
578583
return cpu;

0 commit comments

Comments
 (0)