xref: /linux-6.15/include/linux/kernel_stat.h (revision bfc3f028)
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>
12*bfc3f028SFrederic 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. */
473292beb3SGlauber Costa #define kstat_this_cpu (&__get_cpu_var(kstat))
483292beb3SGlauber Costa #define kcpustat_this_cpu (&__get_cpu_var(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 
54d52a61c0SYinghai Lu #include <linux/irq.h>
550b8f1efaSYinghai Lu extern unsigned int kstat_irqs_cpu(unsigned int irq, int cpu);
566c9ae009SEric Dumazet 
576c9ae009SEric Dumazet #define kstat_incr_irqs_this_cpu(irqno, DESC)		\
586c9ae009SEric Dumazet do {							\
596c9ae009SEric Dumazet 	__this_cpu_inc(*(DESC)->kstat_irqs);		\
606c9ae009SEric Dumazet 	__this_cpu_inc(kstat.irqs_sum);			\
616c9ae009SEric Dumazet } while (0)
62d52a61c0SYinghai Lu 
63aa0ce5bbSKeika Kobayashi static inline void kstat_incr_softirqs_this_cpu(unsigned int irq)
64aa0ce5bbSKeika Kobayashi {
656c9ae009SEric Dumazet 	__this_cpu_inc(kstat.softirqs[irq]);
66aa0ce5bbSKeika Kobayashi }
67aa0ce5bbSKeika Kobayashi 
68aa0ce5bbSKeika Kobayashi static inline unsigned int kstat_softirqs_cpu(unsigned int irq, int cpu)
69aa0ce5bbSKeika Kobayashi {
70aa0ce5bbSKeika Kobayashi        return kstat_cpu(cpu).softirqs[irq];
71aa0ce5bbSKeika Kobayashi }
72aa0ce5bbSKeika Kobayashi 
731da177e4SLinus Torvalds /*
741da177e4SLinus Torvalds  * Number of interrupts per specific IRQ source, since bootup
751da177e4SLinus Torvalds  */
76478735e3SKAMEZAWA Hiroyuki extern unsigned int kstat_irqs(unsigned int irq);
771da177e4SLinus Torvalds 
78f2c66cd8SKAMEZAWA Hiroyuki /*
79f2c66cd8SKAMEZAWA Hiroyuki  * Number of interrupts per cpu, since bootup
80f2c66cd8SKAMEZAWA Hiroyuki  */
81f2c66cd8SKAMEZAWA Hiroyuki static inline unsigned int kstat_cpu_irqs_sum(unsigned int cpu)
82f2c66cd8SKAMEZAWA Hiroyuki {
83f2c66cd8SKAMEZAWA Hiroyuki 	return kstat_cpu(cpu).irqs_sum;
84f2c66cd8SKAMEZAWA Hiroyuki }
85aa9c4c0fSIngo Molnar 
86aa9c4c0fSIngo Molnar /*
87aa9c4c0fSIngo Molnar  * Lock/unlock the current runqueue - to extract task statistics:
88aa9c4c0fSIngo Molnar  */
89bb34d92fSFrank Mayhar extern unsigned long long task_delta_exec(struct task_struct *);
90aa9c4c0fSIngo Molnar 
91457533a7SMartin Schwidefsky extern void account_user_time(struct task_struct *, cputime_t, cputime_t);
92457533a7SMartin Schwidefsky extern void account_system_time(struct task_struct *, int, cputime_t, cputime_t);
9379741dd3SMartin Schwidefsky extern void account_steal_time(cputime_t);
9479741dd3SMartin Schwidefsky extern void account_idle_time(cputime_t);
9579741dd3SMartin Schwidefsky 
96abf917cdSFrederic Weisbecker #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
97bcebdf84SFrederic Weisbecker static inline void account_process_tick(struct task_struct *tsk, int user)
98bcebdf84SFrederic Weisbecker {
99bcebdf84SFrederic Weisbecker 	vtime_account_user(tsk);
100bcebdf84SFrederic Weisbecker }
101bcebdf84SFrederic Weisbecker #else
10279741dd3SMartin Schwidefsky extern void account_process_tick(struct task_struct *, int user);
103bcebdf84SFrederic Weisbecker #endif
104bcebdf84SFrederic Weisbecker 
10579741dd3SMartin Schwidefsky extern void account_steal_ticks(unsigned long ticks);
10679741dd3SMartin Schwidefsky extern void account_idle_ticks(unsigned long ticks);
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds #endif /* _LINUX_KERNEL_STAT_H */
109