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/interrupt.h>
9 #include <linux/sched.h>
10 #include <linux/vtime.h>
11 #include <asm/irq.h>
12
13 /*
14 * 'kernel_stat.h' contains the definitions needed for doing
15 * some kernel statistics (CPU usage, context switches ...),
16 * used by rstatd/perfmeter
17 */
18
19 enum cpu_usage_stat {
20 CPUTIME_USER,
21 CPUTIME_NICE,
22 CPUTIME_SYSTEM,
23 CPUTIME_SOFTIRQ,
24 CPUTIME_IRQ,
25 CPUTIME_IDLE,
26 CPUTIME_IOWAIT,
27 CPUTIME_STEAL,
28 CPUTIME_GUEST,
29 CPUTIME_GUEST_NICE,
30 #ifdef CONFIG_SCHED_CORE
31 CPUTIME_FORCEIDLE,
32 #endif
33 NR_STATS,
34 };
35
36 struct kernel_cpustat {
37 #ifdef CONFIG_NO_HZ_COMMON
38 bool idle_dyntick;
39 bool idle_elapse;
40 seqcount_t idle_sleeptime_seq;
41 u64 idle_entrytime;
42 u64 idle_stealtime[2];
43 u64 idle_dyntick_entry;
44 u64 idle_tick_period;
45 u64 idle_tick_overlap;
46 #endif
47 u64 cpustat[NR_STATS];
48 };
49
50 struct kernel_stat {
51 unsigned long irqs_sum;
52 unsigned int softirqs[NR_SOFTIRQS];
53 };
54
55 DECLARE_PER_CPU(struct kernel_stat, kstat);
56 DECLARE_PER_CPU(struct kernel_cpustat, kernel_cpustat);
57
58 /* Must have preemption disabled for this to be meaningful. */
59 #define kstat_this_cpu this_cpu_ptr(&kstat)
60 #define kcpustat_this_cpu this_cpu_ptr(&kernel_cpustat)
61 #define kstat_cpu(cpu) per_cpu(kstat, cpu)
62 #define kcpustat_cpu(cpu) per_cpu(kernel_cpustat, cpu)
63
64 extern unsigned long long nr_context_switches_cpu(int cpu);
65 extern unsigned long long nr_context_switches(void);
66
67 extern unsigned int kstat_irqs_cpu(unsigned int irq, int cpu);
68 extern void kstat_incr_irq_this_cpu(unsigned int irq);
69
kstat_incr_softirqs_this_cpu(unsigned int irq)70 static inline void kstat_incr_softirqs_this_cpu(unsigned int irq)
71 {
72 __this_cpu_inc(kstat.softirqs[irq]);
73 }
74
kstat_softirqs_cpu(unsigned int irq,int cpu)75 static inline unsigned int kstat_softirqs_cpu(unsigned int irq, int cpu)
76 {
77 return kstat_cpu(cpu).softirqs[irq];
78 }
79
kstat_cpu_softirqs_sum(int cpu)80 static inline unsigned int kstat_cpu_softirqs_sum(int cpu)
81 {
82 int i;
83 unsigned int sum = 0;
84
85 for (i = 0; i < NR_SOFTIRQS; i++)
86 sum += kstat_softirqs_cpu(i, cpu);
87
88 return sum;
89 }
90
91 #ifdef CONFIG_GENERIC_IRQ_STAT_SNAPSHOT
92 extern void kstat_snapshot_irqs(void);
93 extern unsigned int kstat_get_irq_since_snapshot(unsigned int irq);
94 #else
kstat_snapshot_irqs(void)95 static inline void kstat_snapshot_irqs(void) { }
kstat_get_irq_since_snapshot(unsigned int irq)96 static inline unsigned int kstat_get_irq_since_snapshot(unsigned int irq) { return 0; }
97 #endif
98
99 /*
100 * Number of interrupts per specific IRQ source, since bootup
101 */
102 extern unsigned int kstat_irqs_usr(unsigned int irq);
103
104 /*
105 * Number of interrupts per cpu, since bootup
106 */
kstat_cpu_irqs_sum(unsigned int cpu)107 static inline unsigned long kstat_cpu_irqs_sum(unsigned int cpu)
108 {
109 return kstat_cpu(cpu).irqs_sum;
110 }
111
112 #ifdef CONFIG_NO_HZ_COMMON
113
114 #ifdef CONFIG_HAVE_VIRT_CPU_ACCOUNTING_IDLE
115
kcpustat_dyntick_start(u64 now)116 static inline void kcpustat_dyntick_start(u64 now) { }
kcpustat_dyntick_stop(u64 now,u64 tick_start)117 static inline void kcpustat_dyntick_stop(u64 now, u64 tick_start) { }
kcpustat_irq_enter(u64 now)118 static inline void kcpustat_irq_enter(u64 now) { }
kcpustat_irq_exit(u64 now)119 static inline void kcpustat_irq_exit(u64 now) { }
kcpustat_idle_dyntick(void)120 static inline bool kcpustat_idle_dyntick(void) { return false; }
121
122 extern u64 arch_kcpustat_field_idle(int cpu);
123 extern u64 arch_kcpustat_field_iowait(int cpu);
124
kcpustat_field_idle(int cpu)125 static inline u64 kcpustat_field_idle(int cpu)
126 {
127 return arch_kcpustat_field_idle(cpu);
128 }
129
kcpustat_field_iowait(int cpu)130 static inline u64 kcpustat_field_iowait(int cpu)
131 {
132 return arch_kcpustat_field_iowait(cpu);
133 }
134
135 #else /* !CONFIG_HAVE_VIRT_CPU_ACCOUNTING_IDLE */
136
137 extern void kcpustat_dyntick_start(u64 now);
138 extern void kcpustat_dyntick_stop(u64 now, u64 tick_start);
139 extern void kcpustat_irq_enter(u64 now);
140 extern void kcpustat_irq_exit(u64 now);
141 extern u64 kcpustat_field_idle(int cpu);
142 extern u64 kcpustat_field_iowait(int cpu);
143
kcpustat_idle_dyntick(void)144 static inline bool kcpustat_idle_dyntick(void)
145 {
146 return __this_cpu_read(kernel_cpustat.idle_dyntick);
147 }
148
149 #endif /* !CONFIG_HAVE_VIRT_CPU_ACCOUNTING_IDLE */
150
151 #else
kcpustat_field_idle(int cpu)152 static inline u64 kcpustat_field_idle(int cpu)
153 {
154 return kcpustat_cpu(cpu).cpustat[CPUTIME_IDLE];
155 }
kcpustat_field_iowait(int cpu)156 static inline u64 kcpustat_field_iowait(int cpu)
157 {
158 return kcpustat_cpu(cpu).cpustat[CPUTIME_IOWAIT];
159 }
160
kcpustat_idle_dyntick(void)161 static inline bool kcpustat_idle_dyntick(void)
162 {
163 return false;
164 }
165 #endif /* CONFIG_NO_HZ_COMMON */
166
167 extern u64 get_cpu_idle_time_us(int cpu, u64 *last_update_time);
168 extern u64 get_cpu_iowait_time_us(int cpu, u64 *last_update_time);
169
170 /* Fetch cputime values when vtime is disabled on a CPU */
kcpustat_field_default(enum cpu_usage_stat usage,int cpu)171 static inline u64 kcpustat_field_default(enum cpu_usage_stat usage, int cpu)
172 {
173 if (usage == CPUTIME_IDLE)
174 return kcpustat_field_idle(cpu);
175 if (usage == CPUTIME_IOWAIT)
176 return kcpustat_field_iowait(cpu);
177 return kcpustat_cpu(cpu).cpustat[usage];
178 }
179
kcpustat_cpu_fetch_default(struct kernel_cpustat * dst,int cpu)180 static inline void kcpustat_cpu_fetch_default(struct kernel_cpustat *dst, int cpu)
181 {
182 *dst = kcpustat_cpu(cpu);
183 dst->cpustat[CPUTIME_IDLE] = kcpustat_field_idle(cpu);
184 dst->cpustat[CPUTIME_IOWAIT] = kcpustat_field_iowait(cpu);
185 }
186
187 #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
188 extern u64 kcpustat_field(enum cpu_usage_stat usage, int cpu);
189 extern void kcpustat_cpu_fetch(struct kernel_cpustat *dst, int cpu);
190 #else
kcpustat_field(enum cpu_usage_stat usage,int cpu)191 static inline u64 kcpustat_field(enum cpu_usage_stat usage, int cpu)
192 {
193 return kcpustat_field_default(usage, cpu);
194 }
195
kcpustat_cpu_fetch(struct kernel_cpustat * dst,int cpu)196 static inline void kcpustat_cpu_fetch(struct kernel_cpustat *dst, int cpu)
197 {
198 kcpustat_cpu_fetch_default(dst, cpu);
199 }
200 #endif /* !CONFIG_VIRT_CPU_ACCOUNTING_GEN */
201
kcpustat_field_total(enum cpu_usage_stat usage,const struct cpumask * cpus)202 static inline u64 kcpustat_field_total(enum cpu_usage_stat usage, const struct cpumask *cpus)
203 {
204 u64 total = 0;
205 int cpu;
206
207 for_each_cpu(cpu, cpus)
208 total += kcpustat_field(usage, cpu);
209
210 return total;
211 }
212
213 extern void account_user_time(struct task_struct *, u64);
214 extern void account_guest_time(struct task_struct *, u64);
215 extern void account_system_time(struct task_struct *, int, u64);
216 extern void account_system_index_time(struct task_struct *, u64,
217 enum cpu_usage_stat);
218 extern void account_steal_time(u64);
219 extern void account_idle_time(u64);
220
221 #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
account_process_tick(struct task_struct * tsk,int user)222 static inline void account_process_tick(struct task_struct *tsk, int user)
223 {
224 if (!kcpustat_idle_dyntick())
225 vtime_flush(tsk);
226 }
227 #else
228 extern void account_process_tick(struct task_struct *, int user);
229 #endif
230
231 #ifdef CONFIG_SCHED_CORE
232 extern void __account_forceidle_time(struct task_struct *tsk, u64 delta);
233 #endif
234
235 #endif /* _LINUX_KERNEL_STAT_H */
236