1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
21da177e4SLinus Torvalds #ifndef _LINUX_KERNEL_STAT_H
31da177e4SLinus Torvalds #define _LINUX_KERNEL_STAT_H
41da177e4SLinus Torvalds
51da177e4SLinus Torvalds #include <linux/smp.h>
61da177e4SLinus Torvalds #include <linux/threads.h>
71da177e4SLinus Torvalds #include <linux/percpu.h>
828ef3584SIngo Molnar #include <linux/cpumask.h>
9aa0ce5bbSKeika Kobayashi #include <linux/interrupt.h>
103292beb3SGlauber Costa #include <linux/sched.h>
11dcbf832eSFrederic Weisbecker #include <linux/vtime.h>
126859a840SAlan Mayer #include <asm/irq.h>
131da177e4SLinus Torvalds
141da177e4SLinus Torvalds /*
151da177e4SLinus Torvalds * 'kernel_stat.h' contains the definitions needed for doing
161da177e4SLinus Torvalds * some kernel statistics (CPU usage, context switches ...),
171da177e4SLinus Torvalds * used by rstatd/perfmeter
181da177e4SLinus Torvalds */
191da177e4SLinus Torvalds
203292beb3SGlauber Costa enum cpu_usage_stat {
213292beb3SGlauber Costa CPUTIME_USER,
223292beb3SGlauber Costa CPUTIME_NICE,
233292beb3SGlauber Costa CPUTIME_SYSTEM,
243292beb3SGlauber Costa CPUTIME_SOFTIRQ,
253292beb3SGlauber Costa CPUTIME_IRQ,
263292beb3SGlauber Costa CPUTIME_IDLE,
273292beb3SGlauber Costa CPUTIME_IOWAIT,
283292beb3SGlauber Costa CPUTIME_STEAL,
293292beb3SGlauber Costa CPUTIME_GUEST,
303292beb3SGlauber Costa CPUTIME_GUEST_NICE,
311fcf54deSJosh Don #ifdef CONFIG_SCHED_CORE
321fcf54deSJosh Don CPUTIME_FORCEIDLE,
331fcf54deSJosh Don #endif
343292beb3SGlauber Costa NR_STATS,
353292beb3SGlauber Costa };
363292beb3SGlauber Costa
373292beb3SGlauber Costa struct kernel_cpustat {
383292beb3SGlauber Costa u64 cpustat[NR_STATS];
391da177e4SLinus Torvalds };
401da177e4SLinus Torvalds
411da177e4SLinus Torvalds struct kernel_stat {
42f2c66cd8SKAMEZAWA Hiroyuki unsigned long irqs_sum;
43aa0ce5bbSKeika Kobayashi unsigned int softirqs[NR_SOFTIRQS];
441da177e4SLinus Torvalds };
451da177e4SLinus Torvalds
461da177e4SLinus Torvalds DECLARE_PER_CPU(struct kernel_stat, kstat);
473292beb3SGlauber Costa DECLARE_PER_CPU(struct kernel_cpustat, kernel_cpustat);
481da177e4SLinus Torvalds
491da177e4SLinus Torvalds /* Must have preemption disabled for this to be meaningful. */
504a32fea9SChristoph Lameter #define kstat_this_cpu this_cpu_ptr(&kstat)
514a32fea9SChristoph Lameter #define kcpustat_this_cpu this_cpu_ptr(&kernel_cpustat)
523292beb3SGlauber Costa #define kstat_cpu(cpu) per_cpu(kstat, cpu)
533292beb3SGlauber Costa #define kcpustat_cpu(cpu) per_cpu(kernel_cpustat, cpu)
541da177e4SLinus Torvalds
55*7c182722SZhen Lei extern unsigned long long nr_context_switches_cpu(int cpu);
561da177e4SLinus Torvalds extern unsigned long long nr_context_switches(void);
571da177e4SLinus Torvalds
580b8f1efaSYinghai Lu extern unsigned int kstat_irqs_cpu(unsigned int irq, int cpu);
59792d0018SThomas Gleixner extern void kstat_incr_irq_this_cpu(unsigned int irq);
60d52a61c0SYinghai Lu
kstat_incr_softirqs_this_cpu(unsigned int irq)61aa0ce5bbSKeika Kobayashi static inline void kstat_incr_softirqs_this_cpu(unsigned int irq)
62aa0ce5bbSKeika Kobayashi {
636c9ae009SEric Dumazet __this_cpu_inc(kstat.softirqs[irq]);
64aa0ce5bbSKeika Kobayashi }
65aa0ce5bbSKeika Kobayashi
kstat_softirqs_cpu(unsigned int irq,int cpu)66aa0ce5bbSKeika Kobayashi static inline unsigned int kstat_softirqs_cpu(unsigned int irq, int cpu)
67aa0ce5bbSKeika Kobayashi {
68aa0ce5bbSKeika Kobayashi return kstat_cpu(cpu).softirqs[irq];
69aa0ce5bbSKeika Kobayashi }
70aa0ce5bbSKeika Kobayashi
kstat_cpu_softirqs_sum(int cpu)713ca0a6eaSZhen Lei static inline unsigned int kstat_cpu_softirqs_sum(int cpu)
723ca0a6eaSZhen Lei {
733ca0a6eaSZhen Lei int i;
743ca0a6eaSZhen Lei unsigned int sum = 0;
753ca0a6eaSZhen Lei
763ca0a6eaSZhen Lei for (i = 0; i < NR_SOFTIRQS; i++)
773ca0a6eaSZhen Lei sum += kstat_softirqs_cpu(i, cpu);
783ca0a6eaSZhen Lei
793ca0a6eaSZhen Lei return sum;
803ca0a6eaSZhen Lei }
813ca0a6eaSZhen Lei
821da177e4SLinus Torvalds /*
831da177e4SLinus Torvalds * Number of interrupts per specific IRQ source, since bootup
841da177e4SLinus Torvalds */
85c291ee62SThomas Gleixner extern unsigned int kstat_irqs_usr(unsigned int irq);
861da177e4SLinus Torvalds
87f2c66cd8SKAMEZAWA Hiroyuki /*
88f2c66cd8SKAMEZAWA Hiroyuki * Number of interrupts per cpu, since bootup
89f2c66cd8SKAMEZAWA Hiroyuki */
kstat_cpu_irqs_sum(unsigned int cpu)9047904aedSZhen Lei static inline unsigned long kstat_cpu_irqs_sum(unsigned int cpu)
91f2c66cd8SKAMEZAWA Hiroyuki {
92f2c66cd8SKAMEZAWA Hiroyuki return kstat_cpu(cpu).irqs_sum;
93f2c66cd8SKAMEZAWA Hiroyuki }
94aa9c4c0fSIngo Molnar
9564eea63cSFrederic Weisbecker #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
9664eea63cSFrederic Weisbecker extern u64 kcpustat_field(struct kernel_cpustat *kcpustat,
9764eea63cSFrederic Weisbecker enum cpu_usage_stat usage, int cpu);
9874722bb2SFrederic Weisbecker extern void kcpustat_cpu_fetch(struct kernel_cpustat *dst, int cpu);
9964eea63cSFrederic Weisbecker #else
kcpustat_field(struct kernel_cpustat * kcpustat,enum cpu_usage_stat usage,int cpu)10064eea63cSFrederic Weisbecker static inline u64 kcpustat_field(struct kernel_cpustat *kcpustat,
10164eea63cSFrederic Weisbecker enum cpu_usage_stat usage, int cpu)
10264eea63cSFrederic Weisbecker {
10364eea63cSFrederic Weisbecker return kcpustat->cpustat[usage];
10464eea63cSFrederic Weisbecker }
10574722bb2SFrederic Weisbecker
kcpustat_cpu_fetch(struct kernel_cpustat * dst,int cpu)10674722bb2SFrederic Weisbecker static inline void kcpustat_cpu_fetch(struct kernel_cpustat *dst, int cpu)
10774722bb2SFrederic Weisbecker {
10874722bb2SFrederic Weisbecker *dst = kcpustat_cpu(cpu);
10974722bb2SFrederic Weisbecker }
11074722bb2SFrederic Weisbecker
11164eea63cSFrederic Weisbecker #endif
11264eea63cSFrederic Weisbecker
11323244a5cSFrederic Weisbecker extern void account_user_time(struct task_struct *, u64);
114fb8b049cSFrederic Weisbecker extern void account_guest_time(struct task_struct *, u64);
115fb8b049cSFrederic Weisbecker extern void account_system_time(struct task_struct *, int, u64);
116fb8b049cSFrederic Weisbecker extern void account_system_index_time(struct task_struct *, u64,
117c31cc6a5SFrederic Weisbecker enum cpu_usage_stat);
118be9095edSFrederic Weisbecker extern void account_steal_time(u64);
11918b43a9bSFrederic Weisbecker extern void account_idle_time(u64);
120a130e8fbSJosh Don extern u64 get_idle_time(struct kernel_cpustat *kcs, int cpu);
12179741dd3SMartin Schwidefsky
122abf917cdSFrederic Weisbecker #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
account_process_tick(struct task_struct * tsk,int user)123bcebdf84SFrederic Weisbecker static inline void account_process_tick(struct task_struct *tsk, int user)
124bcebdf84SFrederic Weisbecker {
125c8d7dabfSFrederic Weisbecker vtime_flush(tsk);
126bcebdf84SFrederic Weisbecker }
127bcebdf84SFrederic Weisbecker #else
12879741dd3SMartin Schwidefsky extern void account_process_tick(struct task_struct *, int user);
129bcebdf84SFrederic Weisbecker #endif
130bcebdf84SFrederic Weisbecker
13179741dd3SMartin Schwidefsky extern void account_idle_ticks(unsigned long ticks);
1321da177e4SLinus Torvalds
1331fcf54deSJosh Don #ifdef CONFIG_SCHED_CORE
1341fcf54deSJosh Don extern void __account_forceidle_time(struct task_struct *tsk, u64 delta);
1351fcf54deSJosh Don #endif
1361fcf54deSJosh Don
1371da177e4SLinus Torvalds #endif /* _LINUX_KERNEL_STAT_H */
138