xref: /linux-6.15/include/linux/kernel_stat.h (revision bcebdf84)
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>
121da177e4SLinus Torvalds #include <asm/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 {
39d7e51e66SYinghai Lu #ifndef CONFIG_GENERIC_HARDIRQS
401da177e4SLinus Torvalds        unsigned int irqs[NR_IRQS];
410b8f1efaSYinghai Lu #endif
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. */
503292beb3SGlauber Costa #define kstat_this_cpu (&__get_cpu_var(kstat))
513292beb3SGlauber Costa #define kcpustat_this_cpu (&__get_cpu_var(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 
551da177e4SLinus Torvalds extern unsigned long long nr_context_switches(void);
561da177e4SLinus Torvalds 
57d7e51e66SYinghai Lu #ifndef CONFIG_GENERIC_HARDIRQS
580b8f1efaSYinghai Lu 
59d6c88a50SThomas Gleixner struct irq_desc;
608c464a4bSYinghai Lu 
61d6c88a50SThomas Gleixner static inline void kstat_incr_irqs_this_cpu(unsigned int irq,
62d6c88a50SThomas Gleixner 					    struct irq_desc *desc)
63d6c88a50SThomas Gleixner {
646c9ae009SEric Dumazet 	__this_cpu_inc(kstat.irqs[irq]);
656c9ae009SEric Dumazet 	__this_cpu_inc(kstat.irqs_sum);
66d6c88a50SThomas Gleixner }
678c464a4bSYinghai Lu 
687f95ec9eSYinghai Lu static inline unsigned int kstat_irqs_cpu(unsigned int irq, int cpu)
697f95ec9eSYinghai Lu {
707f95ec9eSYinghai Lu        return kstat_cpu(cpu).irqs[irq];
717f95ec9eSYinghai Lu }
720b8f1efaSYinghai Lu #else
73d52a61c0SYinghai Lu #include <linux/irq.h>
740b8f1efaSYinghai Lu extern unsigned int kstat_irqs_cpu(unsigned int irq, int cpu);
756c9ae009SEric Dumazet 
766c9ae009SEric Dumazet #define kstat_incr_irqs_this_cpu(irqno, DESC)		\
776c9ae009SEric Dumazet do {							\
786c9ae009SEric Dumazet 	__this_cpu_inc(*(DESC)->kstat_irqs);		\
796c9ae009SEric Dumazet 	__this_cpu_inc(kstat.irqs_sum);			\
806c9ae009SEric Dumazet } while (0)
81d52a61c0SYinghai Lu 
820b8f1efaSYinghai Lu #endif
837f95ec9eSYinghai Lu 
84aa0ce5bbSKeika Kobayashi static inline void kstat_incr_softirqs_this_cpu(unsigned int irq)
85aa0ce5bbSKeika Kobayashi {
866c9ae009SEric Dumazet 	__this_cpu_inc(kstat.softirqs[irq]);
87aa0ce5bbSKeika Kobayashi }
88aa0ce5bbSKeika Kobayashi 
89aa0ce5bbSKeika Kobayashi static inline unsigned int kstat_softirqs_cpu(unsigned int irq, int cpu)
90aa0ce5bbSKeika Kobayashi {
91aa0ce5bbSKeika Kobayashi        return kstat_cpu(cpu).softirqs[irq];
92aa0ce5bbSKeika Kobayashi }
93aa0ce5bbSKeika Kobayashi 
941da177e4SLinus Torvalds /*
951da177e4SLinus Torvalds  * Number of interrupts per specific IRQ source, since bootup
961da177e4SLinus Torvalds  */
97478735e3SKAMEZAWA Hiroyuki #ifndef CONFIG_GENERIC_HARDIRQS
987f95ec9eSYinghai Lu static inline unsigned int kstat_irqs(unsigned int irq)
991da177e4SLinus Torvalds {
1007f95ec9eSYinghai Lu 	unsigned int sum = 0;
1017f95ec9eSYinghai Lu 	int cpu;
1021da177e4SLinus Torvalds 
1030a945022SKAMEZAWA Hiroyuki 	for_each_possible_cpu(cpu)
1047f95ec9eSYinghai Lu 		sum += kstat_irqs_cpu(irq, cpu);
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds 	return sum;
1071da177e4SLinus Torvalds }
108478735e3SKAMEZAWA Hiroyuki #else
109478735e3SKAMEZAWA Hiroyuki extern unsigned int kstat_irqs(unsigned int irq);
110478735e3SKAMEZAWA Hiroyuki #endif
1111da177e4SLinus Torvalds 
112f2c66cd8SKAMEZAWA Hiroyuki /*
113f2c66cd8SKAMEZAWA Hiroyuki  * Number of interrupts per cpu, since bootup
114f2c66cd8SKAMEZAWA Hiroyuki  */
115f2c66cd8SKAMEZAWA Hiroyuki static inline unsigned int kstat_cpu_irqs_sum(unsigned int cpu)
116f2c66cd8SKAMEZAWA Hiroyuki {
117f2c66cd8SKAMEZAWA Hiroyuki 	return kstat_cpu(cpu).irqs_sum;
118f2c66cd8SKAMEZAWA Hiroyuki }
119aa9c4c0fSIngo Molnar 
120aa9c4c0fSIngo Molnar /*
121aa9c4c0fSIngo Molnar  * Lock/unlock the current runqueue - to extract task statistics:
122aa9c4c0fSIngo Molnar  */
123bb34d92fSFrank Mayhar extern unsigned long long task_delta_exec(struct task_struct *);
124aa9c4c0fSIngo Molnar 
125457533a7SMartin Schwidefsky extern void account_user_time(struct task_struct *, cputime_t, cputime_t);
126457533a7SMartin Schwidefsky extern void account_system_time(struct task_struct *, int, cputime_t, cputime_t);
12779741dd3SMartin Schwidefsky extern void account_steal_time(cputime_t);
12879741dd3SMartin Schwidefsky extern void account_idle_time(cputime_t);
12979741dd3SMartin Schwidefsky 
130*bcebdf84SFrederic Weisbecker #ifdef CONFIG_VIRT_CPU_ACCOUNTING
131*bcebdf84SFrederic Weisbecker static inline void account_process_tick(struct task_struct *tsk, int user)
132*bcebdf84SFrederic Weisbecker {
133*bcebdf84SFrederic Weisbecker 	vtime_account_user(tsk);
134*bcebdf84SFrederic Weisbecker }
135*bcebdf84SFrederic Weisbecker #else
13679741dd3SMartin Schwidefsky extern void account_process_tick(struct task_struct *, int user);
137*bcebdf84SFrederic Weisbecker #endif
138*bcebdf84SFrederic Weisbecker 
13979741dd3SMartin Schwidefsky extern void account_steal_ticks(unsigned long ticks);
14079741dd3SMartin Schwidefsky extern void account_idle_ticks(unsigned long ticks);
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds #endif /* _LINUX_KERNEL_STAT_H */
143