mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
synced 2026-06-21 15:43:21 +02:00
cf6444c3e1
The non-vtime dynticks-idle cputime accounting is a big mess that
accumulates within two concurrent statistics, each having their own
shortcomings:
* The accounting for online CPUs which is based on the delta between
tick_nohz_start_idle() and tick_nohz_stop_idle().
Pros:
- Works when the tick is off
- Has nsecs granularity
Cons:
- Account idle steal time but doesn't substract it from idle
cputime.
- Assumes CONFIG_IRQ_TIME_ACCOUNTING by not accounting IRQs but
the IRQ time is simply ignored when
CONFIG_IRQ_TIME_ACCOUNTING=n
- The windows between 1) idle task scheduling and the first call
to tick_nohz_start_idle() and 2) idle task between the last
tick_nohz_stop_idle() and the rest of the idle time are
blindspots wrt. cputime accounting (though mostly insignificant
amount)
- Relies on private fields outside of kernel stats, with specific
accessors.
* The accounting for offline CPUs which is based on ticks and the
jiffies delta during which the tick was stopped.
Pros:
- Handles steal time correctly
- Handle CONFIG_IRQ_TIME_ACCOUNTING=y and
CONFIG_IRQ_TIME_ACCOUNTING=n correctly.
- Handles the whole idle task
- Accounts directly to kernel stats, without midlayer accumulator.
Cons:
- Doesn't elapse when the tick is off, which doesn't make it
suitable for online CPUs.
- Has TICK_NSEC granularity (jiffies)
- Needs to track the dyntick-idle ticks that were accounted and
substract them from the total jiffies time spent while the tick
was stopped. This is an ugly workaround.
Having two different accounting for a single context is not the only
problem: since those accountings are of different natures, it is
possible to observe the global idle time going backward after a CPU goes
offline.
Clean up the situation with introducing a hybrid approach that stays
coherent and works for both online and offline CPUs:
* Tick based or native vtime accounting operate before the idle loop
is entered and resume once the idle loop prepares to exit.
* When the idle loop starts, switch to dynticks-idle accounting as is
done currently, except that the statistics accumulate directly to the
relevant kernel stat fields.
* Private dyntick cputime accounting fields are removed.
* Works on both online and offline case.
Further improvement will include:
* Only switch to dynticks-idle cputime accounting when the tick actually
goes in dynticks mode.
* Handle CONFIG_IRQ_TIME_ACCOUNTING=n correctly such that the
dynticks-idle accounting still elapses while on IRQs.
* Correctly substract idle steal cputime from idle time
Reported-by: Xin Zhao <jackzxcui1989@163.com>
Signed-off-by: Frederic Weisbecker <frederic@kernel.org>
Signed-off-by: Thomas Gleixner <tglx@kernel.org>
Tested-by: Shrikanth Hegde <sshegde@linux.ibm.com>
Link: https://patch.msgid.link/20260508131647.43868-8-frederic@kernel.org
174 lines
4.9 KiB
C
174 lines
4.9 KiB
C
/* SPDX-License-Identifier: GPL-2.0 */
|
|
#ifndef _LINUX_KERNEL_VTIME_H
|
|
#define _LINUX_KERNEL_VTIME_H
|
|
|
|
#include <linux/context_tracking_state.h>
|
|
#include <linux/sched.h>
|
|
|
|
/*
|
|
* Common vtime APIs
|
|
*/
|
|
#ifdef CONFIG_VIRT_CPU_ACCOUNTING
|
|
extern void vtime_account_kernel(struct task_struct *tsk);
|
|
#endif /* !CONFIG_VIRT_CPU_ACCOUNTING */
|
|
|
|
#ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
|
|
extern void vtime_user_enter(struct task_struct *tsk);
|
|
extern void vtime_user_exit(struct task_struct *tsk);
|
|
extern void vtime_guest_enter(struct task_struct *tsk);
|
|
extern void vtime_guest_exit(struct task_struct *tsk);
|
|
extern void vtime_init_idle(struct task_struct *tsk, int cpu);
|
|
#else /* !CONFIG_VIRT_CPU_ACCOUNTING_GEN */
|
|
static inline void vtime_user_enter(struct task_struct *tsk) { }
|
|
static inline void vtime_user_exit(struct task_struct *tsk) { }
|
|
static inline void vtime_guest_enter(struct task_struct *tsk) { }
|
|
static inline void vtime_guest_exit(struct task_struct *tsk) { }
|
|
static inline void vtime_init_idle(struct task_struct *tsk, int cpu) { }
|
|
#endif
|
|
|
|
static inline bool vtime_generic_enabled_cpu(int cpu)
|
|
{
|
|
return context_tracking_enabled_cpu(cpu);
|
|
}
|
|
|
|
static inline bool vtime_generic_enabled_this_cpu(void)
|
|
{
|
|
return context_tracking_enabled_this_cpu();
|
|
}
|
|
|
|
#ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
|
|
extern void vtime_account_idle(struct task_struct *tsk);
|
|
extern void vtime_account_irq(struct task_struct *tsk, unsigned int offset);
|
|
extern void vtime_account_softirq(struct task_struct *tsk);
|
|
extern void vtime_account_hardirq(struct task_struct *tsk);
|
|
extern void vtime_flush(struct task_struct *tsk);
|
|
extern void vtime_reset(void);
|
|
extern void vtime_dyntick_start(void);
|
|
extern void vtime_dyntick_stop(void);
|
|
#else /* !CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
|
|
static inline void vtime_account_irq(struct task_struct *tsk, unsigned int offset) { }
|
|
static inline void vtime_account_softirq(struct task_struct *tsk) { }
|
|
static inline void vtime_account_hardirq(struct task_struct *tsk) { }
|
|
static inline void vtime_flush(struct task_struct *tsk) { }
|
|
static inline void vtime_reset(void) { }
|
|
static inline void vtime_dyntick_start(void) { }
|
|
static inline void vtime_dyntick_stop(void) { }
|
|
#endif
|
|
|
|
/*
|
|
* vtime_accounting_enabled_this_cpu() definitions/declarations
|
|
*/
|
|
#if defined(CONFIG_VIRT_CPU_ACCOUNTING_NATIVE)
|
|
|
|
static inline bool vtime_accounting_enabled_this_cpu(void) { return true; }
|
|
extern void vtime_task_switch(struct task_struct *prev);
|
|
|
|
static __always_inline void vtime_account_guest_enter(void)
|
|
{
|
|
vtime_account_kernel(current);
|
|
current->flags |= PF_VCPU;
|
|
}
|
|
|
|
static __always_inline void vtime_account_guest_exit(void)
|
|
{
|
|
vtime_account_kernel(current);
|
|
current->flags &= ~PF_VCPU;
|
|
}
|
|
|
|
#elif defined(CONFIG_VIRT_CPU_ACCOUNTING_GEN)
|
|
|
|
/*
|
|
* Checks if vtime is enabled on some CPU. Cputime readers want to be careful
|
|
* in that case and compute the tickless cputime.
|
|
* For now vtime state is tied to context tracking. We might want to decouple
|
|
* those later if necessary.
|
|
*/
|
|
static inline bool vtime_accounting_enabled(void)
|
|
{
|
|
return context_tracking_enabled();
|
|
}
|
|
|
|
static inline bool vtime_accounting_enabled_cpu(int cpu)
|
|
{
|
|
return vtime_generic_enabled_cpu(cpu);
|
|
}
|
|
|
|
static inline bool vtime_accounting_enabled_this_cpu(void)
|
|
{
|
|
return vtime_generic_enabled_this_cpu();
|
|
}
|
|
|
|
extern void vtime_task_switch_generic(struct task_struct *prev);
|
|
|
|
static inline void vtime_task_switch(struct task_struct *prev)
|
|
{
|
|
if (vtime_accounting_enabled_this_cpu())
|
|
vtime_task_switch_generic(prev);
|
|
}
|
|
|
|
static __always_inline void vtime_account_guest_enter(void)
|
|
{
|
|
if (vtime_accounting_enabled_this_cpu())
|
|
vtime_guest_enter(current);
|
|
else
|
|
current->flags |= PF_VCPU;
|
|
}
|
|
|
|
static __always_inline void vtime_account_guest_exit(void)
|
|
{
|
|
if (vtime_accounting_enabled_this_cpu())
|
|
vtime_guest_exit(current);
|
|
else
|
|
current->flags &= ~PF_VCPU;
|
|
}
|
|
|
|
#else /* !CONFIG_VIRT_CPU_ACCOUNTING */
|
|
|
|
static inline bool vtime_accounting_enabled_this_cpu(void) { return false; }
|
|
static inline void vtime_task_switch(struct task_struct *prev) { }
|
|
|
|
static __always_inline void vtime_account_guest_enter(void)
|
|
{
|
|
current->flags |= PF_VCPU;
|
|
}
|
|
|
|
static __always_inline void vtime_account_guest_exit(void)
|
|
{
|
|
current->flags &= ~PF_VCPU;
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
#ifdef CONFIG_IRQ_TIME_ACCOUNTING
|
|
extern void irqtime_account_irq(struct task_struct *tsk, unsigned int offset);
|
|
#else
|
|
static inline void irqtime_account_irq(struct task_struct *tsk, unsigned int offset) { }
|
|
#endif
|
|
|
|
static inline void account_softirq_enter(struct task_struct *tsk)
|
|
{
|
|
vtime_account_irq(tsk, SOFTIRQ_OFFSET);
|
|
irqtime_account_irq(tsk, SOFTIRQ_OFFSET);
|
|
}
|
|
|
|
static inline void account_softirq_exit(struct task_struct *tsk)
|
|
{
|
|
vtime_account_softirq(tsk);
|
|
irqtime_account_irq(tsk, 0);
|
|
}
|
|
|
|
static inline void account_hardirq_enter(struct task_struct *tsk)
|
|
{
|
|
vtime_account_irq(tsk, HARDIRQ_OFFSET);
|
|
irqtime_account_irq(tsk, HARDIRQ_OFFSET);
|
|
}
|
|
|
|
static inline void account_hardirq_exit(struct task_struct *tsk)
|
|
{
|
|
vtime_account_hardirq(tsk);
|
|
irqtime_account_irq(tsk, 0);
|
|
}
|
|
|
|
#endif /* _LINUX_KERNEL_VTIME_H */
|