xref: /linux-6.15/include/linux/kernel_stat.h (revision 1213699a)
11da177e4SLinus Torvalds #ifndef _LINUX_KERNEL_STAT_H
21da177e4SLinus Torvalds #define _LINUX_KERNEL_STAT_H
31da177e4SLinus Torvalds 
41da177e4SLinus Torvalds #include <linux/smp.h>
51da177e4SLinus Torvalds #include <linux/threads.h>
61da177e4SLinus Torvalds #include <linux/percpu.h>
728ef3584SIngo Molnar #include <linux/cpumask.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>
12bfc3f028SFrederic Weisbecker #include <linux/cputime.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,
313292beb3SGlauber Costa 	NR_STATS,
323292beb3SGlauber Costa };
333292beb3SGlauber Costa 
343292beb3SGlauber Costa struct kernel_cpustat {
353292beb3SGlauber Costa 	u64 cpustat[NR_STATS];
361da177e4SLinus Torvalds };
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds struct kernel_stat {
39f2c66cd8SKAMEZAWA Hiroyuki 	unsigned long irqs_sum;
40aa0ce5bbSKeika Kobayashi 	unsigned int softirqs[NR_SOFTIRQS];
411da177e4SLinus Torvalds };
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds DECLARE_PER_CPU(struct kernel_stat, kstat);
443292beb3SGlauber Costa DECLARE_PER_CPU(struct kernel_cpustat, kernel_cpustat);
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds /* Must have preemption disabled for this to be meaningful. */
474a32fea9SChristoph Lameter #define kstat_this_cpu this_cpu_ptr(&kstat)
484a32fea9SChristoph Lameter #define kcpustat_this_cpu this_cpu_ptr(&kernel_cpustat)
493292beb3SGlauber Costa #define kstat_cpu(cpu) per_cpu(kstat, cpu)
503292beb3SGlauber Costa #define kcpustat_cpu(cpu) per_cpu(kernel_cpustat, cpu)
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds extern unsigned long long nr_context_switches(void);
531da177e4SLinus Torvalds 
540b8f1efaSYinghai Lu extern unsigned int kstat_irqs_cpu(unsigned int irq, int cpu);
55792d0018SThomas Gleixner extern void kstat_incr_irq_this_cpu(unsigned int irq);
56d52a61c0SYinghai Lu 
57aa0ce5bbSKeika Kobayashi static inline void kstat_incr_softirqs_this_cpu(unsigned int irq)
58aa0ce5bbSKeika Kobayashi {
596c9ae009SEric Dumazet 	__this_cpu_inc(kstat.softirqs[irq]);
60aa0ce5bbSKeika Kobayashi }
61aa0ce5bbSKeika Kobayashi 
62aa0ce5bbSKeika Kobayashi static inline unsigned int kstat_softirqs_cpu(unsigned int irq, int cpu)
63aa0ce5bbSKeika Kobayashi {
64aa0ce5bbSKeika Kobayashi        return kstat_cpu(cpu).softirqs[irq];
65aa0ce5bbSKeika Kobayashi }
66aa0ce5bbSKeika Kobayashi 
671da177e4SLinus Torvalds /*
681da177e4SLinus Torvalds  * Number of interrupts per specific IRQ source, since bootup
691da177e4SLinus Torvalds  */
70478735e3SKAMEZAWA Hiroyuki extern unsigned int kstat_irqs(unsigned int irq);
71c291ee62SThomas Gleixner extern unsigned int kstat_irqs_usr(unsigned int irq);
721da177e4SLinus Torvalds 
73f2c66cd8SKAMEZAWA Hiroyuki /*
74f2c66cd8SKAMEZAWA Hiroyuki  * Number of interrupts per cpu, since bootup
75f2c66cd8SKAMEZAWA Hiroyuki  */
76f2c66cd8SKAMEZAWA Hiroyuki static inline unsigned int kstat_cpu_irqs_sum(unsigned int cpu)
77f2c66cd8SKAMEZAWA Hiroyuki {
78f2c66cd8SKAMEZAWA Hiroyuki 	return kstat_cpu(cpu).irqs_sum;
79f2c66cd8SKAMEZAWA Hiroyuki }
80aa9c4c0fSIngo Molnar 
8140565b5aSStanislaw Gruszka extern void account_user_time(struct task_struct *, cputime_t);
82*1213699aSFrederic Weisbecker extern void account_guest_time(struct task_struct *, cputime_t);
8340565b5aSStanislaw Gruszka extern void account_system_time(struct task_struct *, int, cputime_t);
84c31cc6a5SFrederic Weisbecker extern void account_system_index_time(struct task_struct *, cputime_t,
85c31cc6a5SFrederic Weisbecker 				      enum cpu_usage_stat);
8679741dd3SMartin Schwidefsky extern void account_steal_time(cputime_t);
8779741dd3SMartin Schwidefsky extern void account_idle_time(cputime_t);
8879741dd3SMartin Schwidefsky 
89abf917cdSFrederic Weisbecker #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
90bcebdf84SFrederic Weisbecker static inline void account_process_tick(struct task_struct *tsk, int user)
91bcebdf84SFrederic Weisbecker {
92bcebdf84SFrederic Weisbecker 	vtime_account_user(tsk);
93bcebdf84SFrederic Weisbecker }
94bcebdf84SFrederic Weisbecker #else
9579741dd3SMartin Schwidefsky extern void account_process_tick(struct task_struct *, int user);
96bcebdf84SFrederic Weisbecker #endif
97bcebdf84SFrederic Weisbecker 
9879741dd3SMartin Schwidefsky extern void account_idle_ticks(unsigned long ticks);
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds #endif /* _LINUX_KERNEL_STAT_H */
101