xref: /linux/kernel/sched/stats.h (revision 056e065a6b6e01ab54bb9770c0d5a15350e571e2)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _KERNEL_STATS_H
3 #define _KERNEL_STATS_H
4 
5 #ifdef CONFIG_SCHEDSTATS
6 
7 extern struct static_key_false sched_schedstats;
8 
9 /*
10  * Expects runqueue lock to be held for atomicity of update
11  */
12 static inline void
13 rq_sched_info_arrive(struct rq *rq, unsigned long long delta)
14 {
15 	if (rq) {
16 		rq->rq_sched_info.run_delay += delta;
17 		rq->rq_sched_info.pcount++;
18 	}
19 }
20 
21 /*
22  * Expects runqueue lock to be held for atomicity of update
23  */
24 static inline void
25 rq_sched_info_depart(struct rq *rq, unsigned long long delta)
26 {
27 	if (rq)
28 		rq->rq_cpu_time += delta;
29 }
30 
31 static inline void
32 rq_sched_info_dequeue(struct rq *rq, unsigned long long delta)
33 {
34 	if (rq)
35 		rq->rq_sched_info.run_delay += delta;
36 }
37 #define   schedstat_enabled()		static_branch_unlikely(&sched_schedstats)
38 #define __schedstat_inc(var)		do { var++; } while (0)
39 #define   schedstat_inc(var)		do { if (schedstat_enabled()) { var++; } } while (0)
40 #define __schedstat_add(var, amt)	do { var += (amt); } while (0)
41 #define   schedstat_add(var, amt)	do { if (schedstat_enabled()) { var += (amt); } } while (0)
42 #define __schedstat_set(var, val)	do { var = (val); } while (0)
43 #define   schedstat_set(var, val)	do { if (schedstat_enabled()) { var = (val); } } while (0)
44 #define   schedstat_val(var)		(var)
45 #define   schedstat_val_or_zero(var)	((schedstat_enabled()) ? (var) : 0)
46 
47 void __update_stats_wait_start(struct rq *rq, struct task_struct *p,
48 			       struct sched_statistics *stats);
49 
50 void __update_stats_wait_end(struct rq *rq, struct task_struct *p,
51 			     struct sched_statistics *stats);
52 void __update_stats_enqueue_sleeper(struct rq *rq, struct task_struct *p,
53 				    struct sched_statistics *stats);
54 
55 static inline void
56 check_schedstat_required(void)
57 {
58 	if (schedstat_enabled())
59 		return;
60 
61 	/* Force schedstat enabled if a dependent tracepoint is active */
62 	if (trace_sched_stat_wait_enabled()    ||
63 	    trace_sched_stat_sleep_enabled()   ||
64 	    trace_sched_stat_iowait_enabled()  ||
65 	    trace_sched_stat_blocked_enabled() ||
66 	    trace_sched_stat_runtime_enabled())
67 		printk_deferred_once("Scheduler tracepoints stat_sleep, stat_iowait, stat_blocked and stat_runtime require the kernel parameter schedstats=enable or kernel.sched_schedstats=1\n");
68 }
69 
70 #else /* !CONFIG_SCHEDSTATS: */
71 
72 static inline void rq_sched_info_arrive  (struct rq *rq, unsigned long long delta) { }
73 static inline void rq_sched_info_dequeue(struct rq *rq, unsigned long long delta) { }
74 static inline void rq_sched_info_depart  (struct rq *rq, unsigned long long delta) { }
75 # define   schedstat_enabled()		0
76 # define __schedstat_inc(var)		do { } while (0)
77 # define   schedstat_inc(var)		do { } while (0)
78 # define __schedstat_add(var, amt)	do { } while (0)
79 # define   schedstat_add(var, amt)	do { } while (0)
80 # define __schedstat_set(var, val)	do { } while (0)
81 # define   schedstat_set(var, val)	do { } while (0)
82 # define   schedstat_val(var)		0
83 # define   schedstat_val_or_zero(var)	0
84 
85 # define __update_stats_wait_start(rq, p, stats)       do { } while (0)
86 # define __update_stats_wait_end(rq, p, stats)         do { } while (0)
87 # define __update_stats_enqueue_sleeper(rq, p, stats)  do { } while (0)
88 # define check_schedstat_required()                    do { } while (0)
89 
90 #endif /* CONFIG_SCHEDSTATS */
91 
92 static inline struct sched_statistics *
93 __schedstats_from_se(struct sched_entity *se)
94 {
95 #ifdef CONFIG_FAIR_GROUP_SCHED
96 	if (!entity_is_task(se))
97 		return &container_of(se, struct cfs_tg_state, se)->stats;
98 #endif
99 	return &task_of(se)->stats;
100 }
101 
102 #ifdef CONFIG_PSI
103 void psi_task_change(struct task_struct *task, int clear, int set);
104 void psi_task_switch(struct task_struct *prev, struct task_struct *next,
105 		     bool sleep);
106 #ifdef CONFIG_IRQ_TIME_ACCOUNTING
107 void psi_account_irqtime(struct rq *rq, struct task_struct *curr, struct task_struct *prev);
108 #else /* !CONFIG_IRQ_TIME_ACCOUNTING: */
109 static inline void psi_account_irqtime(struct rq *rq, struct task_struct *curr,
110 				       struct task_struct *prev) {}
111 #endif /* !CONFIG_IRQ_TIME_ACCOUNTING */
112 /*
113  * PSI tracks state that persists across sleeps, such as iowaits and
114  * memory stalls. As a result, it has to distinguish between sleeps,
115  * where a task's runnable state changes, and migrations, where a task
116  * and its runnable state are being moved between CPUs and runqueues.
117  *
118  * A notable case is a task whose dequeue is delayed. PSI considers
119  * those sleeping, but because they are still on the runqueue they can
120  * go through migration requeues. In this case, *sleeping* states need
121  * to be transferred.
122  */
123 static inline void psi_enqueue(struct task_struct *p, int flags)
124 {
125 	int clear = 0, set = 0;
126 
127 	if (static_branch_likely(&psi_disabled))
128 		return;
129 
130 	/* Same runqueue, nothing changed for psi */
131 	if (flags & ENQUEUE_RESTORE)
132 		return;
133 
134 	/* psi_sched_switch() will handle the flags */
135 	if (task_on_cpu(task_rq(p), p))
136 		return;
137 
138 	if (p->se.sched_delayed) {
139 		/* CPU migration of "sleeping" task */
140 		WARN_ON_ONCE(!(flags & ENQUEUE_MIGRATED));
141 		if (p->in_memstall)
142 			set |= TSK_MEMSTALL;
143 		if (p->in_iowait)
144 			set |= TSK_IOWAIT;
145 	} else if (flags & ENQUEUE_MIGRATED) {
146 		/* CPU migration of runnable task */
147 		set = TSK_RUNNING;
148 		if (p->in_memstall)
149 			set |= TSK_MEMSTALL | TSK_MEMSTALL_RUNNING;
150 	} else {
151 		/* Wakeup of new or sleeping task */
152 		if (p->in_iowait)
153 			clear |= TSK_IOWAIT;
154 		set = TSK_RUNNING;
155 		if (p->in_memstall)
156 			set |= TSK_MEMSTALL_RUNNING;
157 	}
158 
159 	psi_task_change(p, clear, set);
160 }
161 
162 static inline void psi_dequeue(struct task_struct *p, int flags)
163 {
164 	if (static_branch_likely(&psi_disabled))
165 		return;
166 
167 	/* Same runqueue, nothing changed for psi */
168 	if (flags & DEQUEUE_SAVE)
169 		return;
170 
171 	/*
172 	 * A voluntary sleep is a dequeue followed by a task switch. To
173 	 * avoid walking all ancestors twice, psi_task_switch() handles
174 	 * TSK_RUNNING and TSK_IOWAIT for us when it moves TSK_ONCPU.
175 	 * Do nothing here.
176 	 *
177 	 * In the SCHED_PROXY_EXECUTION case we may do sleeping
178 	 * dequeues that are not followed by a task switch, so check
179 	 * TSK_ONCPU is set to ensure the task switch is imminent.
180 	 * Otherwise clear the flags as usual.
181 	 */
182 	if ((flags & DEQUEUE_SLEEP) && (p->psi_flags & TSK_ONCPU))
183 		return;
184 
185 	/*
186 	 * When migrating a task to another CPU, clear all psi
187 	 * state. The enqueue callback above will work it out.
188 	 */
189 	psi_task_change(p, p->psi_flags, 0);
190 }
191 
192 static inline void psi_ttwu_dequeue(struct task_struct *p)
193 {
194 	if (static_branch_likely(&psi_disabled))
195 		return;
196 	/*
197 	 * Is the task being migrated during a wakeup? Make sure to
198 	 * deregister its sleep-persistent psi states from the old
199 	 * queue, and let psi_enqueue() know it has to requeue.
200 	 */
201 	if (unlikely(p->psi_flags)) {
202 		struct rq_flags rf;
203 		struct rq *rq;
204 
205 		rq = __task_rq_lock(p, &rf);
206 		psi_task_change(p, p->psi_flags, 0);
207 		__task_rq_unlock(rq, p, &rf);
208 	}
209 }
210 
211 static inline void psi_sched_switch(struct task_struct *prev,
212 				    struct task_struct *next,
213 				    bool sleep)
214 {
215 	if (static_branch_likely(&psi_disabled))
216 		return;
217 
218 	psi_task_switch(prev, next, sleep);
219 }
220 
221 #else /* !CONFIG_PSI: */
222 static inline void psi_enqueue(struct task_struct *p, bool migrate) {}
223 static inline void psi_dequeue(struct task_struct *p, bool migrate) {}
224 static inline void psi_ttwu_dequeue(struct task_struct *p) {}
225 static inline void psi_sched_switch(struct task_struct *prev,
226 				    struct task_struct *next,
227 				    bool sleep) {}
228 static inline void psi_account_irqtime(struct rq *rq, struct task_struct *curr,
229 				       struct task_struct *prev) {}
230 #endif /* !CONFIG_PSI */
231 
232 #ifdef CONFIG_SCHED_INFO
233 /*
234  * We are interested in knowing how long it was from the *first* time a
235  * task was queued to the time that it finally hit a CPU, we call this routine
236  * from dequeue_task() to account for possible rq->clock skew across CPUs. The
237  * delta taken on each CPU would annul the skew.
238  */
239 static inline void sched_info_dequeue(struct rq *rq, struct task_struct *t)
240 {
241 	unsigned long long delta = 0;
242 
243 	if (!t->sched_info.last_queued)
244 		return;
245 
246 	delta = rq_clock(rq) - t->sched_info.last_queued;
247 	t->sched_info.last_queued = 0;
248 	t->sched_info.run_delay += delta;
249 	if (delta > t->sched_info.max_run_delay) {
250 		t->sched_info.max_run_delay = delta;
251 		ktime_get_real_ts64(&t->sched_info.max_run_delay_ts);
252 	}
253 	if (delta && (!t->sched_info.min_run_delay || delta < t->sched_info.min_run_delay))
254 		t->sched_info.min_run_delay = delta;
255 	rq_sched_info_dequeue(rq, delta);
256 }
257 
258 /*
259  * Called when a task finally hits the CPU.  We can now calculate how
260  * long it was waiting to run.  We also note when it began so that we
261  * can keep stats on how long its time-slice is.
262  */
263 static void sched_info_arrive(struct rq *rq, struct task_struct *t)
264 {
265 	unsigned long long now, delta = 0;
266 
267 	if (!t->sched_info.last_queued)
268 		return;
269 
270 	now = rq_clock(rq);
271 	delta = now - t->sched_info.last_queued;
272 	t->sched_info.last_queued = 0;
273 	t->sched_info.run_delay += delta;
274 	t->sched_info.last_arrival = now;
275 	t->sched_info.pcount++;
276 	if (delta > t->sched_info.max_run_delay) {
277 		t->sched_info.max_run_delay = delta;
278 		ktime_get_real_ts64(&t->sched_info.max_run_delay_ts);
279 	}
280 	if (delta && (!t->sched_info.min_run_delay || delta < t->sched_info.min_run_delay))
281 		t->sched_info.min_run_delay = delta;
282 
283 	rq_sched_info_arrive(rq, delta);
284 }
285 
286 /*
287  * This function is only called from enqueue_task(), but also only updates
288  * the timestamp if it is already not set.  It's assumed that
289  * sched_info_dequeue() will clear that stamp when appropriate.
290  */
291 static inline void sched_info_enqueue(struct rq *rq, struct task_struct *t)
292 {
293 	if (!t->sched_info.last_queued)
294 		t->sched_info.last_queued = rq_clock(rq);
295 }
296 
297 /*
298  * Called when a process ceases being the active-running process involuntarily
299  * due, typically, to expiring its time slice (this may also be called when
300  * switching to the idle task).  Now we can calculate how long we ran.
301  * Also, if the process is still in the TASK_RUNNING state, call
302  * sched_info_enqueue() to mark that it has now again started waiting on
303  * the runqueue.
304  */
305 static inline void sched_info_depart(struct rq *rq, struct task_struct *t)
306 {
307 	unsigned long long delta = rq_clock(rq) - t->sched_info.last_arrival;
308 
309 	rq_sched_info_depart(rq, delta);
310 
311 	if (task_is_running(t))
312 		sched_info_enqueue(rq, t);
313 }
314 
315 /*
316  * Called when tasks are switched involuntarily due, typically, to expiring
317  * their time slice.  (This may also be called when switching to or from
318  * the idle task.)  We are only called when prev != next.
319  */
320 static inline void
321 sched_info_switch(struct rq *rq, struct task_struct *prev, struct task_struct *next)
322 {
323 	/*
324 	 * prev now departs the CPU.  It's not interesting to record
325 	 * stats about how efficient we were at scheduling the idle
326 	 * process, however.
327 	 */
328 	if (prev != rq->idle)
329 		sched_info_depart(rq, prev);
330 
331 	if (next != rq->idle)
332 		sched_info_arrive(rq, next);
333 }
334 
335 #else /* !CONFIG_SCHED_INFO: */
336 # define sched_info_enqueue(rq, t)	do { } while (0)
337 # define sched_info_dequeue(rq, t)	do { } while (0)
338 # define sched_info_switch(rq, t, next)	do { } while (0)
339 #endif /* !CONFIG_SCHED_INFO */
340 
341 #endif /* _KERNEL_STATS_H */
342