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 <laoar.shao@gmail.com>
---
kernel/sched/cputime.c | 16 +++++++---------
kernel/sched/sched.h | 1 +
2 files changed, 8 insertions(+), 9 deletions(-)
diff --git a/kernel/sched/cputime.c b/kernel/sched/cputime.c
index 0bed0fa1acd9..d0b6ea737d04 100644
--- a/kernel/sched/cputime.c
+++ b/kernel/sched/cputime.c
@@ -7,6 +7,8 @@
#include <asm/cputime.h>
#endif
+DEFINE_STATIC_KEY_FALSE(sched_clock_irqtime);
+
#ifdef CONFIG_IRQ_TIME_ACCOUNTING
/*
@@ -22,16 +24,14 @@
*/
DEFINE_PER_CPU(struct irqtime, cpu_irqtime);
-static int sched_clock_irqtime;
-
void enable_sched_clock_irqtime(void)
{
- sched_clock_irqtime = 1;
+ static_branch_enable(&sched_clock_irqtime);
}
void disable_sched_clock_irqtime(void)
{
- sched_clock_irqtime = 0;
+ static_branch_disable(&sched_clock_irqtime);
}
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)
s64 delta;
int cpu;
- if (!sched_clock_irqtime)
+ if (!static_branch_likely(&sched_clock_irqtime))
return;
cpu = smp_processor_id();
@@ -90,8 +90,6 @@ static u64 irqtime_tick_accounted(u64 maxtime)
#else /* CONFIG_IRQ_TIME_ACCOUNTING */
-#define sched_clock_irqtime (0)
-
static u64 irqtime_tick_accounted(u64 dummy)
{
return 0;
@@ -478,7 +476,7 @@ void account_process_tick(struct task_struct *p, int user_tick)
if (vtime_accounting_enabled_this_cpu())
return;
- if (sched_clock_irqtime) {
+ if (static_branch_likely(&sched_clock_irqtime)) {
irqtime_account_process_tick(p, user_tick, 1);
return;
}
@@ -507,7 +505,7 @@ void account_idle_ticks(unsigned long ticks)
{
u64 cputime, steal;
- if (sched_clock_irqtime) {
+ if (static_branch_likely(&sched_clock_irqtime)) {
irqtime_account_idle_ticks(ticks);
return;
}
diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
index 081519ffab46..038ce65d6635 100644
--- a/kernel/sched/sched.h
+++ b/kernel/sched/sched.h
@@ -3179,6 +3179,7 @@ struct irqtime {
};
DECLARE_PER_CPU(struct irqtime, cpu_irqtime);
+DECLARE_STATIC_KEY_FALSE(sched_clock_irqtime);
/*
* Returns the irqtime minus the softirq time computed by ksoftirqd.
--
2.43.5
On Fri, Nov 01, 2024 at 11:17:47AM +0800, Yafang Shao wrote:
> 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 <laoar.shao@gmail.com>
> ---
> kernel/sched/cputime.c | 16 +++++++---------
> kernel/sched/sched.h | 1 +
> 2 files changed, 8 insertions(+), 9 deletions(-)
>
> diff --git a/kernel/sched/cputime.c b/kernel/sched/cputime.c
> index 0bed0fa1acd9..d0b6ea737d04 100644
> --- a/kernel/sched/cputime.c
> +++ b/kernel/sched/cputime.c
> @@ -7,6 +7,8 @@
> #include <asm/cputime.h>
> #endif
>
> +DEFINE_STATIC_KEY_FALSE(sched_clock_irqtime);
> +
> #ifdef CONFIG_IRQ_TIME_ACCOUNTING
>
> /*
> @@ -22,16 +24,14 @@
> */
> DEFINE_PER_CPU(struct irqtime, cpu_irqtime);
>
> -static int sched_clock_irqtime;
> -
> void enable_sched_clock_irqtime(void)
> {
> - sched_clock_irqtime = 1;
> + static_branch_enable(&sched_clock_irqtime);
> }
>
> void disable_sched_clock_irqtime(void)
> {
> - sched_clock_irqtime = 0;
> + static_branch_disable(&sched_clock_irqtime);
> }
>
> 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)
> s64 delta;
> int cpu;
>
> - if (!sched_clock_irqtime)
> + if (!static_branch_likely(&sched_clock_irqtime))
> return;
>
> cpu = smp_processor_id();
> @@ -90,8 +90,6 @@ static u64 irqtime_tick_accounted(u64 maxtime)
>
> #else /* CONFIG_IRQ_TIME_ACCOUNTING */
>
> -#define sched_clock_irqtime (0)
> -
> static u64 irqtime_tick_accounted(u64 dummy)
> {
> return 0;
This makes no sense... in the IRQ_TIME_ACCOUNTING=n case you shouldn't
be using the static key.
> @@ -478,7 +476,7 @@ void account_process_tick(struct task_struct *p, int user_tick)
> if (vtime_accounting_enabled_this_cpu())
> return;
>
> - if (sched_clock_irqtime) {
> + if (static_branch_likely(&sched_clock_irqtime)) {
> irqtime_account_process_tick(p, user_tick, 1);
> return;
> }
> @@ -507,7 +505,7 @@ void account_idle_ticks(unsigned long ticks)
> {
> u64 cputime, steal;
>
> - if (sched_clock_irqtime) {
> + if (static_branch_likely(&sched_clock_irqtime)) {
> irqtime_account_idle_ticks(ticks);
> return;
> }
> diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
> index 081519ffab46..038ce65d6635 100644
> --- a/kernel/sched/sched.h
> +++ b/kernel/sched/sched.h
> @@ -3179,6 +3179,7 @@ struct irqtime {
> };
>
> DECLARE_PER_CPU(struct irqtime, cpu_irqtime);
> +DECLARE_STATIC_KEY_FALSE(sched_clock_irqtime);
>
> /*
> * Returns the irqtime minus the softirq time computed by ksoftirqd.
> --
> 2.43.5
>
On Fri, Nov 1, 2024 at 6:06 PM Peter Zijlstra <peterz@infradead.org> wrote:
>
> On Fri, Nov 01, 2024 at 11:17:47AM +0800, Yafang Shao wrote:
> > 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 <laoar.shao@gmail.com>
> > ---
> > kernel/sched/cputime.c | 16 +++++++---------
> > kernel/sched/sched.h | 1 +
> > 2 files changed, 8 insertions(+), 9 deletions(-)
> >
> > diff --git a/kernel/sched/cputime.c b/kernel/sched/cputime.c
> > index 0bed0fa1acd9..d0b6ea737d04 100644
> > --- a/kernel/sched/cputime.c
> > +++ b/kernel/sched/cputime.c
> > @@ -7,6 +7,8 @@
> > #include <asm/cputime.h>
> > #endif
> >
> > +DEFINE_STATIC_KEY_FALSE(sched_clock_irqtime);
> > +
> > #ifdef CONFIG_IRQ_TIME_ACCOUNTING
> >
> > /*
> > @@ -22,16 +24,14 @@
> > */
> > DEFINE_PER_CPU(struct irqtime, cpu_irqtime);
> >
> > -static int sched_clock_irqtime;
> > -
> > void enable_sched_clock_irqtime(void)
> > {
> > - sched_clock_irqtime = 1;
> > + static_branch_enable(&sched_clock_irqtime);
> > }
> >
> > void disable_sched_clock_irqtime(void)
> > {
> > - sched_clock_irqtime = 0;
> > + static_branch_disable(&sched_clock_irqtime);
> > }
> >
> > 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)
> > s64 delta;
> > int cpu;
> >
> > - if (!sched_clock_irqtime)
> > + if (!static_branch_likely(&sched_clock_irqtime))
> > return;
> >
> > cpu = smp_processor_id();
> > @@ -90,8 +90,6 @@ static u64 irqtime_tick_accounted(u64 maxtime)
> >
> > #else /* CONFIG_IRQ_TIME_ACCOUNTING */
> >
> > -#define sched_clock_irqtime (0)
> > -
> > static u64 irqtime_tick_accounted(u64 dummy)
> > {
> > return 0;
>
> This makes no sense... in the IRQ_TIME_ACCOUNTING=n case you shouldn't
> be using the static key.
will change it.
--
Regards
Yafang
© 2016 - 2026 Red Hat, Inc.