Skip to content

Commit 8722903

Browse files
laoarPeter Zijlstra
authored andcommitted
sched: Define sched_clock_irqtime as static key
Since CPU time accounting is a performance-critical path, let's define sched_clock_irqtime as a static key to minimize potential overhead. Signed-off-by: Yafang Shao <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Reviewed-by: Michal Koutný <[email protected]> Reviewed-by: Vincent Guittot <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 3229adb commit 8722903

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

kernel/sched/cputime.c

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
#ifdef CONFIG_IRQ_TIME_ACCOUNTING
1111

12+
DEFINE_STATIC_KEY_FALSE(sched_clock_irqtime);
13+
1214
/*
1315
* There are no locks covering percpu hardirq/softirq time.
1416
* They are only modified in vtime_account, on corresponding CPU
@@ -22,16 +24,14 @@
2224
*/
2325
DEFINE_PER_CPU(struct irqtime, cpu_irqtime);
2426

25-
static int sched_clock_irqtime;
26-
2727
void enable_sched_clock_irqtime(void)
2828
{
29-
sched_clock_irqtime = 1;
29+
static_branch_enable(&sched_clock_irqtime);
3030
}
3131

3232
void disable_sched_clock_irqtime(void)
3333
{
34-
sched_clock_irqtime = 0;
34+
static_branch_disable(&sched_clock_irqtime);
3535
}
3636

3737
static void irqtime_account_delta(struct irqtime *irqtime, u64 delta,
@@ -57,7 +57,7 @@ void irqtime_account_irq(struct task_struct *curr, unsigned int offset)
5757
s64 delta;
5858
int cpu;
5959

60-
if (!sched_clock_irqtime)
60+
if (!irqtime_enabled())
6161
return;
6262

6363
cpu = smp_processor_id();
@@ -90,8 +90,6 @@ static u64 irqtime_tick_accounted(u64 maxtime)
9090

9191
#else /* CONFIG_IRQ_TIME_ACCOUNTING */
9292

93-
#define sched_clock_irqtime (0)
94-
9593
static u64 irqtime_tick_accounted(u64 dummy)
9694
{
9795
return 0;
@@ -478,7 +476,7 @@ void account_process_tick(struct task_struct *p, int user_tick)
478476
if (vtime_accounting_enabled_this_cpu())
479477
return;
480478

481-
if (sched_clock_irqtime) {
479+
if (irqtime_enabled()) {
482480
irqtime_account_process_tick(p, user_tick, 1);
483481
return;
484482
}
@@ -507,7 +505,7 @@ void account_idle_ticks(unsigned long ticks)
507505
{
508506
u64 cputime, steal;
509507

510-
if (sched_clock_irqtime) {
508+
if (irqtime_enabled()) {
511509
irqtime_account_idle_ticks(ticks);
512510
return;
513511
}

kernel/sched/sched.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3233,6 +3233,12 @@ struct irqtime {
32333233
};
32343234

32353235
DECLARE_PER_CPU(struct irqtime, cpu_irqtime);
3236+
DECLARE_STATIC_KEY_FALSE(sched_clock_irqtime);
3237+
3238+
static inline int irqtime_enabled(void)
3239+
{
3240+
return static_branch_likely(&sched_clock_irqtime);
3241+
}
32363242

32373243
/*
32383244
* Returns the irqtime minus the softirq time computed by ksoftirqd.
@@ -3253,6 +3259,13 @@ static inline u64 irq_time_read(int cpu)
32533259
return total;
32543260
}
32553261

3262+
#else
3263+
3264+
static inline int irqtime_enabled(void)
3265+
{
3266+
return 0;
3267+
}
3268+
32563269
#endif /* CONFIG_IRQ_TIME_ACCOUNTING */
32573270

32583271
#ifdef CONFIG_CPU_FREQ

0 commit comments

Comments
 (0)