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>
8aa0ce5bbSKeika Kobayashi #include <linux/interrupt.h>
93292beb3SGlauber Costa #include <linux/sched.h>
10dcbf832eSFrederic Weisbecker #include <linux/vtime.h>
116859a840SAlan Mayer #include <asm/irq.h>
121da177e4SLinus Torvalds
131da177e4SLinus Torvalds /*
141da177e4SLinus Torvalds * 'kernel_stat.h' contains the definitions needed for doing
151da177e4SLinus Torvalds * some kernel statistics (CPU usage, context switches ...),
161da177e4SLinus Torvalds * used by rstatd/perfmeter
171da177e4SLinus Torvalds */
181da177e4SLinus Torvalds
193292beb3SGlauber Costa enum cpu_usage_stat {
203292beb3SGlauber Costa CPUTIME_USER,
213292beb3SGlauber Costa CPUTIME_NICE,
223292beb3SGlauber Costa CPUTIME_SYSTEM,
233292beb3SGlauber Costa CPUTIME_SOFTIRQ,
243292beb3SGlauber Costa CPUTIME_IRQ,
253292beb3SGlauber Costa CPUTIME_IDLE,
263292beb3SGlauber Costa CPUTIME_IOWAIT,
273292beb3SGlauber Costa CPUTIME_STEAL,
283292beb3SGlauber Costa CPUTIME_GUEST,
293292beb3SGlauber Costa CPUTIME_GUEST_NICE,
301fcf54deSJosh Don #ifdef CONFIG_SCHED_CORE
311fcf54deSJosh Don CPUTIME_FORCEIDLE,
321fcf54deSJosh Don #endif
333292beb3SGlauber Costa NR_STATS,
343292beb3SGlauber Costa };
353292beb3SGlauber Costa
363292beb3SGlauber Costa struct kernel_cpustat {
373292beb3SGlauber Costa u64 cpustat[NR_STATS];
381da177e4SLinus Torvalds };
391da177e4SLinus Torvalds
401da177e4SLinus Torvalds struct kernel_stat {
41f2c66cd8SKAMEZAWA Hiroyuki unsigned long irqs_sum;
42aa0ce5bbSKeika Kobayashi unsigned int softirqs[NR_SOFTIRQS];
431da177e4SLinus Torvalds };
441da177e4SLinus Torvalds
451da177e4SLinus Torvalds DECLARE_PER_CPU(struct kernel_stat, kstat);
463292beb3SGlauber Costa DECLARE_PER_CPU(struct kernel_cpustat, kernel_cpustat);
471da177e4SLinus Torvalds
481da177e4SLinus Torvalds /* Must have preemption disabled for this to be meaningful. */
494a32fea9SChristoph Lameter #define kstat_this_cpu this_cpu_ptr(&kstat)
504a32fea9SChristoph Lameter #define kcpustat_this_cpu this_cpu_ptr(&kernel_cpustat)
513292beb3SGlauber Costa #define kstat_cpu(cpu) per_cpu(kstat, cpu)
523292beb3SGlauber Costa #define kcpustat_cpu(cpu) per_cpu(kernel_cpustat, cpu)
531da177e4SLinus Torvalds
547c182722SZhen Lei extern unsigned long long nr_context_switches_cpu(int cpu);
551da177e4SLinus Torvalds extern unsigned long long nr_context_switches(void);
561da177e4SLinus Torvalds
570b8f1efaSYinghai Lu extern unsigned int kstat_irqs_cpu(unsigned int irq, int cpu);
58792d0018SThomas Gleixner extern void kstat_incr_irq_this_cpu(unsigned int irq);
59d52a61c0SYinghai Lu
kstat_incr_softirqs_this_cpu(unsigned int irq)60aa0ce5bbSKeika Kobayashi static inline void kstat_incr_softirqs_this_cpu(unsigned int irq)
61aa0ce5bbSKeika Kobayashi {
626c9ae009SEric Dumazet __this_cpu_inc(kstat.softirqs[irq]);
63aa0ce5bbSKeika Kobayashi }
64aa0ce5bbSKeika Kobayashi
kstat_softirqs_cpu(unsigned int irq,int cpu)65aa0ce5bbSKeika Kobayashi static inline unsigned int kstat_softirqs_cpu(unsigned int irq, int cpu)
66aa0ce5bbSKeika Kobayashi {
67aa0ce5bbSKeika Kobayashi return kstat_cpu(cpu).softirqs[irq];
68aa0ce5bbSKeika Kobayashi }
69aa0ce5bbSKeika Kobayashi
kstat_cpu_softirqs_sum(int cpu)703ca0a6eaSZhen Lei static inline unsigned int kstat_cpu_softirqs_sum(int cpu)
713ca0a6eaSZhen Lei {
723ca0a6eaSZhen Lei int i;
733ca0a6eaSZhen Lei unsigned int sum = 0;
743ca0a6eaSZhen Lei
753ca0a6eaSZhen Lei for (i = 0; i < NR_SOFTIRQS; i++)
763ca0a6eaSZhen Lei sum += kstat_softirqs_cpu(i, cpu);
773ca0a6eaSZhen Lei
783ca0a6eaSZhen Lei return sum;
793ca0a6eaSZhen Lei }
803ca0a6eaSZhen Lei
81*99cf63c5SBitao Hu #ifdef CONFIG_GENERIC_IRQ_STAT_SNAPSHOT
82*99cf63c5SBitao Hu extern void kstat_snapshot_irqs(void);
83*99cf63c5SBitao Hu extern unsigned int kstat_get_irq_since_snapshot(unsigned int irq);
84*99cf63c5SBitao Hu #else
kstat_snapshot_irqs(void)85*99cf63c5SBitao Hu static inline void kstat_snapshot_irqs(void) { }
kstat_get_irq_since_snapshot(unsigned int irq)86*99cf63c5SBitao Hu static inline unsigned int kstat_get_irq_since_snapshot(unsigned int irq) { return 0; }
87*99cf63c5SBitao Hu #endif
88*99cf63c5SBitao Hu
891da177e4SLinus Torvalds /*
901da177e4SLinus Torvalds * Number of interrupts per specific IRQ source, since bootup
911da177e4SLinus Torvalds */
92c291ee62SThomas Gleixner extern unsigned int kstat_irqs_usr(unsigned int irq);
931da177e4SLinus Torvalds
94f2c66cd8SKAMEZAWA Hiroyuki /*
95f2c66cd8SKAMEZAWA Hiroyuki * Number of interrupts per cpu, since bootup
96f2c66cd8SKAMEZAWA Hiroyuki */
kstat_cpu_irqs_sum(unsigned int cpu)9747904aedSZhen Lei static inline unsigned long kstat_cpu_irqs_sum(unsigned int cpu)
98f2c66cd8SKAMEZAWA Hiroyuki {
99f2c66cd8SKAMEZAWA Hiroyuki return kstat_cpu(cpu).irqs_sum;
100f2c66cd8SKAMEZAWA Hiroyuki }
101aa9c4c0fSIngo Molnar
10264eea63cSFrederic Weisbecker #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
10364eea63cSFrederic Weisbecker extern u64 kcpustat_field(struct kernel_cpustat *kcpustat,
10464eea63cSFrederic Weisbecker enum cpu_usage_stat usage, int cpu);
10574722bb2SFrederic Weisbecker extern void kcpustat_cpu_fetch(struct kernel_cpustat *dst, int cpu);
10664eea63cSFrederic Weisbecker #else
kcpustat_field(struct kernel_cpustat * kcpustat,enum cpu_usage_stat usage,int cpu)10764eea63cSFrederic Weisbecker static inline u64 kcpustat_field(struct kernel_cpustat *kcpustat,
10864eea63cSFrederic Weisbecker enum cpu_usage_stat usage, int cpu)
10964eea63cSFrederic Weisbecker {
11064eea63cSFrederic Weisbecker return kcpustat->cpustat[usage];
11164eea63cSFrederic Weisbecker }
11274722bb2SFrederic Weisbecker
kcpustat_cpu_fetch(struct kernel_cpustat * dst,int cpu)11374722bb2SFrederic Weisbecker static inline void kcpustat_cpu_fetch(struct kernel_cpustat *dst, int cpu)
11474722bb2SFrederic Weisbecker {
11574722bb2SFrederic Weisbecker *dst = kcpustat_cpu(cpu);
11674722bb2SFrederic Weisbecker }
11774722bb2SFrederic Weisbecker
11864eea63cSFrederic Weisbecker #endif
11964eea63cSFrederic Weisbecker
12023244a5cSFrederic Weisbecker extern void account_user_time(struct task_struct *, u64);
121fb8b049cSFrederic Weisbecker extern void account_guest_time(struct task_struct *, u64);
122fb8b049cSFrederic Weisbecker extern void account_system_time(struct task_struct *, int, u64);
123fb8b049cSFrederic Weisbecker extern void account_system_index_time(struct task_struct *, u64,
124c31cc6a5SFrederic Weisbecker enum cpu_usage_stat);
125be9095edSFrederic Weisbecker extern void account_steal_time(u64);
12618b43a9bSFrederic Weisbecker extern void account_idle_time(u64);
127a130e8fbSJosh Don extern u64 get_idle_time(struct kernel_cpustat *kcs, int cpu);
12879741dd3SMartin Schwidefsky
129abf917cdSFrederic Weisbecker #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
account_process_tick(struct task_struct * tsk,int user)130bcebdf84SFrederic Weisbecker static inline void account_process_tick(struct task_struct *tsk, int user)
131bcebdf84SFrederic Weisbecker {
132c8d7dabfSFrederic Weisbecker vtime_flush(tsk);
133bcebdf84SFrederic Weisbecker }
134bcebdf84SFrederic Weisbecker #else
13579741dd3SMartin Schwidefsky extern void account_process_tick(struct task_struct *, int user);
136bcebdf84SFrederic Weisbecker #endif
137bcebdf84SFrederic Weisbecker
13879741dd3SMartin Schwidefsky extern void account_idle_ticks(unsigned long ticks);
1391da177e4SLinus Torvalds
1401fcf54deSJosh Don #ifdef CONFIG_SCHED_CORE
1411fcf54deSJosh Don extern void __account_forceidle_time(struct task_struct *tsk, u64 delta);
1421fcf54deSJosh Don #endif
1431fcf54deSJosh Don
1441da177e4SLinus Torvalds #endif /* _LINUX_KERNEL_STAT_H */
145