1 /* SPDX-License-Identifier: GPL-2.0 */
3 #ifdef CONFIG_SCHEDSTATS
6 * Expects runqueue lock to be held for atomicity of update
9 rq_sched_info_arrive(struct rq *rq, unsigned long long delta)
12 rq->rq_sched_info.run_delay += delta;
13 rq->rq_sched_info.pcount++;
18 * Expects runqueue lock to be held for atomicity of update
21 rq_sched_info_depart(struct rq *rq, unsigned long long delta)
24 rq->rq_cpu_time += delta;
28 rq_sched_info_dequeue(struct rq *rq, unsigned long long delta)
31 rq->rq_sched_info.run_delay += delta;
33 #define schedstat_enabled() static_branch_unlikely(&sched_schedstats)
34 #define __schedstat_inc(var) do { var++; } while (0)
35 #define schedstat_inc(var) do { if (schedstat_enabled()) { var++; } } while (0)
36 #define __schedstat_add(var, amt) do { var += (amt); } while (0)
37 #define schedstat_add(var, amt) do { if (schedstat_enabled()) { var += (amt); } } while (0)
38 #define __schedstat_set(var, val) do { var = (val); } while (0)
39 #define schedstat_set(var, val) do { if (schedstat_enabled()) { var = (val); } } while (0)
40 #define schedstat_val(var) (var)
41 #define schedstat_val_or_zero(var) ((schedstat_enabled()) ? (var) : 0)
43 #else /* !CONFIG_SCHEDSTATS: */
44 static inline void rq_sched_info_arrive (struct rq *rq, unsigned long long delta) { }
45 static inline void rq_sched_info_dequeue(struct rq *rq, unsigned long long delta) { }
46 static inline void rq_sched_info_depart (struct rq *rq, unsigned long long delta) { }
47 # define schedstat_enabled() 0
48 # define __schedstat_inc(var) do { } while (0)
49 # define schedstat_inc(var) do { } while (0)
50 # define __schedstat_add(var, amt) do { } while (0)
51 # define schedstat_add(var, amt) do { } while (0)
52 # define __schedstat_set(var, val) do { } while (0)
53 # define schedstat_set(var, val) do { } while (0)
54 # define schedstat_val(var) 0
55 # define schedstat_val_or_zero(var) 0
56 #endif /* CONFIG_SCHEDSTATS */
60 * PSI tracks state that persists across sleeps, such as iowaits and
61 * memory stalls. As a result, it has to distinguish between sleeps,
62 * where a task's runnable state changes, and requeues, where a task
63 * and its state are being moved between CPUs and runqueues.
65 static inline void psi_enqueue(struct task_struct *p, bool wakeup)
67 int clear = 0, set = TSK_RUNNING;
69 if (static_branch_likely(&psi_disabled))
73 set |= TSK_MEMSTALL_RUNNING;
75 if (!wakeup || p->sched_psi_wake_requeue) {
78 if (p->sched_psi_wake_requeue)
79 p->sched_psi_wake_requeue = 0;
85 psi_task_change(p, clear, set);
88 static inline void psi_dequeue(struct task_struct *p, bool sleep)
90 int clear = TSK_RUNNING;
92 if (static_branch_likely(&psi_disabled))
96 * A voluntary sleep is a dequeue followed by a task switch. To
97 * avoid walking all ancestors twice, psi_task_switch() handles
98 * TSK_RUNNING and TSK_IOWAIT for us when it moves TSK_ONCPU.
105 clear |= (TSK_MEMSTALL | TSK_MEMSTALL_RUNNING);
107 psi_task_change(p, clear, 0);
110 static inline void psi_ttwu_dequeue(struct task_struct *p)
112 if (static_branch_likely(&psi_disabled))
115 * Is the task being migrated during a wakeup? Make sure to
116 * deregister its sleep-persistent psi states from the old
117 * queue, and let psi_enqueue() know it has to requeue.
119 if (unlikely(p->in_iowait || p->in_memstall)) {
127 clear |= TSK_MEMSTALL;
129 rq = __task_rq_lock(p, &rf);
130 psi_task_change(p, clear, 0);
131 p->sched_psi_wake_requeue = 1;
132 __task_rq_unlock(rq, &rf);
136 static inline void psi_sched_switch(struct task_struct *prev,
137 struct task_struct *next,
140 if (static_branch_likely(&psi_disabled))
143 psi_task_switch(prev, next, sleep);
146 #else /* CONFIG_PSI */
147 static inline void psi_enqueue(struct task_struct *p, bool wakeup) {}
148 static inline void psi_dequeue(struct task_struct *p, bool sleep) {}
149 static inline void psi_ttwu_dequeue(struct task_struct *p) {}
150 static inline void psi_sched_switch(struct task_struct *prev,
151 struct task_struct *next,
153 #endif /* CONFIG_PSI */
155 #ifdef CONFIG_SCHED_INFO
157 * We are interested in knowing how long it was from the *first* time a
158 * task was queued to the time that it finally hit a CPU, we call this routine
159 * from dequeue_task() to account for possible rq->clock skew across CPUs. The
160 * delta taken on each CPU would annul the skew.
162 static inline void sched_info_dequeue(struct rq *rq, struct task_struct *t)
164 unsigned long long delta = 0;
166 if (!t->sched_info.last_queued)
169 delta = rq_clock(rq) - t->sched_info.last_queued;
170 t->sched_info.last_queued = 0;
171 t->sched_info.run_delay += delta;
173 rq_sched_info_dequeue(rq, delta);
177 * Called when a task finally hits the CPU. We can now calculate how
178 * long it was waiting to run. We also note when it began so that we
179 * can keep stats on how long its timeslice is.
181 static void sched_info_arrive(struct rq *rq, struct task_struct *t)
183 unsigned long long now, delta = 0;
185 if (!t->sched_info.last_queued)
189 delta = now - t->sched_info.last_queued;
190 t->sched_info.last_queued = 0;
191 t->sched_info.run_delay += delta;
192 t->sched_info.last_arrival = now;
193 t->sched_info.pcount++;
195 rq_sched_info_arrive(rq, delta);
199 * This function is only called from enqueue_task(), but also only updates
200 * the timestamp if it is already not set. It's assumed that
201 * sched_info_dequeue() will clear that stamp when appropriate.
203 static inline void sched_info_enqueue(struct rq *rq, struct task_struct *t)
205 if (!t->sched_info.last_queued)
206 t->sched_info.last_queued = rq_clock(rq);
210 * Called when a process ceases being the active-running process involuntarily
211 * due, typically, to expiring its time slice (this may also be called when
212 * switching to the idle task). Now we can calculate how long we ran.
213 * Also, if the process is still in the TASK_RUNNING state, call
214 * sched_info_enqueue() to mark that it has now again started waiting on
217 static inline void sched_info_depart(struct rq *rq, struct task_struct *t)
219 unsigned long long delta = rq_clock(rq) - t->sched_info.last_arrival;
221 rq_sched_info_depart(rq, delta);
223 if (task_is_running(t))
224 sched_info_enqueue(rq, t);
228 * Called when tasks are switched involuntarily due, typically, to expiring
229 * their time slice. (This may also be called when switching to or from
230 * the idle task.) We are only called when prev != next.
233 sched_info_switch(struct rq *rq, struct task_struct *prev, struct task_struct *next)
236 * prev now departs the CPU. It's not interesting to record
237 * stats about how efficient we were at scheduling the idle
240 if (prev != rq->idle)
241 sched_info_depart(rq, prev);
243 if (next != rq->idle)
244 sched_info_arrive(rq, next);
247 #else /* !CONFIG_SCHED_INFO: */
248 # define sched_info_enqueue(rq, t) do { } while (0)
249 # define sched_info_dequeue(rq, t) do { } while (0)
250 # define sched_info_switch(rq, t, next) do { } while (0)
251 #endif /* CONFIG_SCHED_INFO */