1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_KERNEL_STAT_H
3 #define _LINUX_KERNEL_STAT_H
4
5 #include <linux/smp.h>
6 #include <linux/threads.h>
7 #include <linux/percpu.h>
8 #include <linux/cpumask.h>
9 #include <linux/interrupt.h>
10 #include <linux/sched.h>
11 #include <linux/vtime.h>
12 #include <asm/irq.h>
13
14 /*
15 * 'kernel_stat.h' contains the definitions needed for doing
16 * some kernel statistics (CPU usage, context switches ...),
17 * used by rstatd/perfmeter
18 */
19
20 enum cpu_usage_stat {
21 CPUTIME_USER,
22 CPUTIME_NICE,
23 CPUTIME_SYSTEM,
24 CPUTIME_SOFTIRQ,
25 CPUTIME_IRQ,
26 CPUTIME_IDLE,
27 CPUTIME_IOWAIT,
28 CPUTIME_STEAL,
29 CPUTIME_GUEST,
30 CPUTIME_GUEST_NICE,
31 #ifdef CONFIG_SCHED_CORE
32 CPUTIME_FORCEIDLE,
33 #endif
34 NR_STATS,
35 };
36
37 struct kernel_cpustat {
38 u64 cpustat[NR_STATS];
39 };
40
41 struct kernel_stat {
42 unsigned long irqs_sum;
43 unsigned int softirqs[NR_SOFTIRQS];
44 };
45
46 DECLARE_PER_CPU(struct kernel_stat, kstat);
47 DECLARE_PER_CPU(struct kernel_cpustat, kernel_cpustat);
48
49 /* Must have preemption disabled for this to be meaningful. */
50 #define kstat_this_cpu this_cpu_ptr(&kstat)
51 #define kcpustat_this_cpu this_cpu_ptr(&kernel_cpustat)
52 #define kstat_cpu(cpu) per_cpu(kstat, cpu)
53 #define kcpustat_cpu(cpu) per_cpu(kernel_cpustat, cpu)
54
55 extern unsigned long long nr_context_switches_cpu(int cpu);
56 extern unsigned long long nr_context_switches(void);
57
58 extern unsigned int kstat_irqs_cpu(unsigned int irq, int cpu);
59 extern void kstat_incr_irq_this_cpu(unsigned int irq);
60
kstat_incr_softirqs_this_cpu(unsigned int irq)61 static inline void kstat_incr_softirqs_this_cpu(unsigned int irq)
62 {
63 __this_cpu_inc(kstat.softirqs[irq]);
64 }
65
kstat_softirqs_cpu(unsigned int irq,int cpu)66 static inline unsigned int kstat_softirqs_cpu(unsigned int irq, int cpu)
67 {
68 return kstat_cpu(cpu).softirqs[irq];
69 }
70
kstat_cpu_softirqs_sum(int cpu)71 static inline unsigned int kstat_cpu_softirqs_sum(int cpu)
72 {
73 int i;
74 unsigned int sum = 0;
75
76 for (i = 0; i < NR_SOFTIRQS; i++)
77 sum += kstat_softirqs_cpu(i, cpu);
78
79 return sum;
80 }
81
82 #ifdef CONFIG_GENERIC_IRQ_STAT_SNAPSHOT
83 extern void kstat_snapshot_irqs(void);
84 extern unsigned int kstat_get_irq_since_snapshot(unsigned int irq);
85 #else
kstat_snapshot_irqs(void)86 static inline void kstat_snapshot_irqs(void) { }
kstat_get_irq_since_snapshot(unsigned int irq)87 static inline unsigned int kstat_get_irq_since_snapshot(unsigned int irq) { return 0; }
88 #endif
89
90 /*
91 * Number of interrupts per specific IRQ source, since bootup
92 */
93 extern unsigned int kstat_irqs_usr(unsigned int irq);
94
95 /*
96 * Number of interrupts per cpu, since bootup
97 */
kstat_cpu_irqs_sum(unsigned int cpu)98 static inline unsigned long kstat_cpu_irqs_sum(unsigned int cpu)
99 {
100 return kstat_cpu(cpu).irqs_sum;
101 }
102
103 #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
104 extern u64 kcpustat_field(struct kernel_cpustat *kcpustat,
105 enum cpu_usage_stat usage, int cpu);
106 extern void kcpustat_cpu_fetch(struct kernel_cpustat *dst, int cpu);
107 #else
kcpustat_field(struct kernel_cpustat * kcpustat,enum cpu_usage_stat usage,int cpu)108 static inline u64 kcpustat_field(struct kernel_cpustat *kcpustat,
109 enum cpu_usage_stat usage, int cpu)
110 {
111 return kcpustat->cpustat[usage];
112 }
113
kcpustat_cpu_fetch(struct kernel_cpustat * dst,int cpu)114 static inline void kcpustat_cpu_fetch(struct kernel_cpustat *dst, int cpu)
115 {
116 *dst = kcpustat_cpu(cpu);
117 }
118
119 #endif
120
121 extern void account_user_time(struct task_struct *, u64);
122 extern void account_guest_time(struct task_struct *, u64);
123 extern void account_system_time(struct task_struct *, int, u64);
124 extern void account_system_index_time(struct task_struct *, u64,
125 enum cpu_usage_stat);
126 extern void account_steal_time(u64);
127 extern void account_idle_time(u64);
128 extern u64 get_idle_time(struct kernel_cpustat *kcs, int cpu);
129
130 #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
account_process_tick(struct task_struct * tsk,int user)131 static inline void account_process_tick(struct task_struct *tsk, int user)
132 {
133 vtime_flush(tsk);
134 }
135 #else
136 extern void account_process_tick(struct task_struct *, int user);
137 #endif
138
139 extern void account_idle_ticks(unsigned long ticks);
140
141 #ifdef CONFIG_SCHED_CORE
142 extern void __account_forceidle_time(struct task_struct *tsk, u64 delta);
143 #endif
144
145 #endif /* _LINUX_KERNEL_STAT_H */
146