blob: 33ffd41935baba9d2fb657fee2f4ef5e11df3160 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Ingo Molnar425e0962007-07-09 18:51:58 +02002
3#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyanb5aadf72008-10-06 13:23:43 +04004
Ingo Molnar425e0962007-07-09 18:51:58 +02005/*
6 * Expects runqueue lock to be held for atomicity of update
7 */
8static inline void
9rq_sched_info_arrive(struct rq *rq, unsigned long long delta)
10{
11 if (rq) {
12 rq->rq_sched_info.run_delay += delta;
Ingo Molnar2d723762007-10-15 17:00:12 +020013 rq->rq_sched_info.pcount++;
Ingo Molnar425e0962007-07-09 18:51:58 +020014 }
15}
16
17/*
18 * Expects runqueue lock to be held for atomicity of update
19 */
20static inline void
21rq_sched_info_depart(struct rq *rq, unsigned long long delta)
22{
23 if (rq)
Ken Chen9c2c4802008-12-16 23:41:22 -080024 rq->rq_cpu_time += delta;
Ingo Molnar425e0962007-07-09 18:51:58 +020025}
Ankita Garg46ac22b2008-07-01 14:30:06 +053026
27static inline void
Peter Zijlstra4e29fb72021-05-04 22:43:45 +020028rq_sched_info_dequeue(struct rq *rq, unsigned long long delta)
Ankita Garg46ac22b2008-07-01 14:30:06 +053029{
30 if (rq)
31 rq->rq_sched_info.run_delay += delta;
32}
Ingo Molnar97fb7a02018-03-03 14:01:12 +010033#define schedstat_enabled() static_branch_unlikely(&sched_schedstats)
Peter Zijlstrab85c8b72018-01-16 20:51:06 +010034#define __schedstat_inc(var) do { var++; } while (0)
Ingo Molnar97fb7a02018-03-03 14:01:12 +010035#define schedstat_inc(var) do { if (schedstat_enabled()) { var++; } } while (0)
Peter Zijlstra2ed41a52018-01-23 20:34:30 +010036#define __schedstat_add(var, amt) do { var += (amt); } while (0)
Ingo Molnar97fb7a02018-03-03 14:01:12 +010037#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)
Josh Poimboeuf9c572592016-06-03 17:58:40 -050042
Ingo Molnar97fb7a02018-03-03 14:01:12 +010043#else /* !CONFIG_SCHEDSTATS: */
44static inline void rq_sched_info_arrive (struct rq *rq, unsigned long long delta) { }
Peter Zijlstra4e29fb72021-05-04 22:43:45 +020045static inline void rq_sched_info_dequeue(struct rq *rq, unsigned long long delta) { }
Ingo Molnar97fb7a02018-03-03 14:01:12 +010046static 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
Josh Poimboeufae928822016-06-17 12:43:24 -050056#endif /* CONFIG_SCHEDSTATS */
Ingo Molnar425e0962007-07-09 18:51:58 +020057
Johannes Weinereb414682018-10-26 15:06:27 -070058#ifdef CONFIG_PSI
59/*
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.
64 */
65static inline void psi_enqueue(struct task_struct *p, bool wakeup)
66{
67 int clear = 0, set = TSK_RUNNING;
68
Johannes Weinere0c27442018-11-30 14:09:58 -080069 if (static_branch_likely(&psi_disabled))
Johannes Weinereb414682018-10-26 15:06:27 -070070 return;
71
72 if (!wakeup || p->sched_psi_wake_requeue) {
Yafang Shao1066d1b2020-03-16 21:28:05 -040073 if (p->in_memstall)
Johannes Weinereb414682018-10-26 15:06:27 -070074 set |= TSK_MEMSTALL;
75 if (p->sched_psi_wake_requeue)
76 p->sched_psi_wake_requeue = 0;
77 } else {
78 if (p->in_iowait)
79 clear |= TSK_IOWAIT;
80 }
81
82 psi_task_change(p, clear, set);
83}
84
85static inline void psi_dequeue(struct task_struct *p, bool sleep)
86{
Chengming Zhou4117ceb2021-03-03 11:46:59 +080087 int clear = TSK_RUNNING;
Johannes Weinereb414682018-10-26 15:06:27 -070088
Johannes Weinere0c27442018-11-30 14:09:58 -080089 if (static_branch_likely(&psi_disabled))
Johannes Weinereb414682018-10-26 15:06:27 -070090 return;
91
Chengming Zhou4117ceb2021-03-03 11:46:59 +080092 /*
93 * A voluntary sleep is a dequeue followed by a task switch. To
94 * avoid walking all ancestors twice, psi_task_switch() handles
95 * TSK_RUNNING and TSK_IOWAIT for us when it moves TSK_ONCPU.
96 * Do nothing here.
97 */
98 if (sleep)
99 return;
Johannes Weinerb05e75d2020-03-16 15:13:31 -0400100
Chengming Zhou4117ceb2021-03-03 11:46:59 +0800101 if (p->in_memstall)
102 clear |= TSK_MEMSTALL;
Johannes Weinereb414682018-10-26 15:06:27 -0700103
Chengming Zhou4117ceb2021-03-03 11:46:59 +0800104 psi_task_change(p, clear, 0);
Johannes Weinereb414682018-10-26 15:06:27 -0700105}
106
107static inline void psi_ttwu_dequeue(struct task_struct *p)
108{
Johannes Weinere0c27442018-11-30 14:09:58 -0800109 if (static_branch_likely(&psi_disabled))
Johannes Weinereb414682018-10-26 15:06:27 -0700110 return;
111 /*
112 * Is the task being migrated during a wakeup? Make sure to
113 * deregister its sleep-persistent psi states from the old
114 * queue, and let psi_enqueue() know it has to requeue.
115 */
Yafang Shao1066d1b2020-03-16 21:28:05 -0400116 if (unlikely(p->in_iowait || p->in_memstall)) {
Johannes Weinereb414682018-10-26 15:06:27 -0700117 struct rq_flags rf;
118 struct rq *rq;
119 int clear = 0;
120
121 if (p->in_iowait)
122 clear |= TSK_IOWAIT;
Yafang Shao1066d1b2020-03-16 21:28:05 -0400123 if (p->in_memstall)
Johannes Weinereb414682018-10-26 15:06:27 -0700124 clear |= TSK_MEMSTALL;
125
126 rq = __task_rq_lock(p, &rf);
127 psi_task_change(p, clear, 0);
128 p->sched_psi_wake_requeue = 1;
129 __task_rq_unlock(rq, &rf);
130 }
131}
132
Johannes Weinerb05e75d2020-03-16 15:13:31 -0400133static inline void psi_sched_switch(struct task_struct *prev,
134 struct task_struct *next,
135 bool sleep)
136{
137 if (static_branch_likely(&psi_disabled))
138 return;
139
Johannes Weiner36b238d2020-03-16 15:13:32 -0400140 psi_task_switch(prev, next, sleep);
Johannes Weinerb05e75d2020-03-16 15:13:31 -0400141}
142
Johannes Weinereb414682018-10-26 15:06:27 -0700143#else /* CONFIG_PSI */
144static inline void psi_enqueue(struct task_struct *p, bool wakeup) {}
145static inline void psi_dequeue(struct task_struct *p, bool sleep) {}
146static inline void psi_ttwu_dequeue(struct task_struct *p) {}
Johannes Weinerb05e75d2020-03-16 15:13:31 -0400147static inline void psi_sched_switch(struct task_struct *prev,
148 struct task_struct *next,
149 bool sleep) {}
Johannes Weinereb414682018-10-26 15:06:27 -0700150#endif /* CONFIG_PSI */
151
Naveen N. Raof6db8342015-06-25 23:53:37 +0530152#ifdef CONFIG_SCHED_INFO
Ingo Molnar425e0962007-07-09 18:51:58 +0200153/*
Rakib Mullickd4a6f3c2010-10-24 16:28:47 +0600154 * We are interested in knowing how long it was from the *first* time a
Ingo Molnar97fb7a02018-03-03 14:01:12 +0100155 * task was queued to the time that it finally hit a CPU, we call this routine
156 * from dequeue_task() to account for possible rq->clock skew across CPUs. The
157 * delta taken on each CPU would annul the skew.
Ingo Molnar425e0962007-07-09 18:51:58 +0200158 */
Peter Zijlstra4e29fb72021-05-04 22:43:45 +0200159static inline void sched_info_dequeue(struct rq *rq, struct task_struct *t)
Ingo Molnar425e0962007-07-09 18:51:58 +0200160{
Peter Zijlstrac5895d32021-05-04 22:43:42 +0200161 unsigned long long delta = 0;
Ankita Garg46ac22b2008-07-01 14:30:06 +0530162
Peter Zijlstrac5895d32021-05-04 22:43:42 +0200163 if (t->sched_info.last_queued) {
164 delta = rq_clock(rq) - t->sched_info.last_queued;
165 t->sched_info.last_queued = 0;
Yi Wang65d74e92019-07-05 12:35:07 +0800166 }
Ankita Garg46ac22b2008-07-01 14:30:06 +0530167 t->sched_info.run_delay += delta;
168
Peter Zijlstra4e29fb72021-05-04 22:43:45 +0200169 rq_sched_info_dequeue(rq, delta);
Ingo Molnar425e0962007-07-09 18:51:58 +0200170}
171
172/*
Ingo Molnar97fb7a02018-03-03 14:01:12 +0100173 * Called when a task finally hits the CPU. We can now calculate how
Ingo Molnar425e0962007-07-09 18:51:58 +0200174 * long it was waiting to run. We also note when it began so that we
175 * can keep stats on how long its timeslice is.
176 */
Michael S. Tsirkin43148952013-09-22 17:20:54 +0300177static void sched_info_arrive(struct rq *rq, struct task_struct *t)
Ingo Molnar425e0962007-07-09 18:51:58 +0200178{
Michael S. Tsirkin43148952013-09-22 17:20:54 +0300179 unsigned long long now = rq_clock(rq), delta = 0;
Ingo Molnar425e0962007-07-09 18:51:58 +0200180
Peter Zijlstrac5895d32021-05-04 22:43:42 +0200181 if (t->sched_info.last_queued) {
Ingo Molnar425e0962007-07-09 18:51:58 +0200182 delta = now - t->sched_info.last_queued;
Peter Zijlstrac5895d32021-05-04 22:43:42 +0200183 t->sched_info.last_queued = 0;
184 }
Ingo Molnar425e0962007-07-09 18:51:58 +0200185 t->sched_info.run_delay += delta;
186 t->sched_info.last_arrival = now;
Ingo Molnar2d723762007-10-15 17:00:12 +0200187 t->sched_info.pcount++;
Ingo Molnar425e0962007-07-09 18:51:58 +0200188
Michael S. Tsirkin43148952013-09-22 17:20:54 +0300189 rq_sched_info_arrive(rq, delta);
Ingo Molnar425e0962007-07-09 18:51:58 +0200190}
191
192/*
Ingo Molnar425e0962007-07-09 18:51:58 +0200193 * This function is only called from enqueue_task(), but also only updates
194 * the timestamp if it is already not set. It's assumed that
Peter Zijlstra4e29fb72021-05-04 22:43:45 +0200195 * sched_info_dequeue() will clear that stamp when appropriate.
Ingo Molnar425e0962007-07-09 18:51:58 +0200196 */
Peter Zijlstra4e29fb72021-05-04 22:43:45 +0200197static inline void sched_info_enqueue(struct rq *rq, struct task_struct *t)
Ingo Molnar425e0962007-07-09 18:51:58 +0200198{
Peter Zijlstrac5895d32021-05-04 22:43:42 +0200199 if (!t->sched_info.last_queued)
200 t->sched_info.last_queued = rq_clock(rq);
Ingo Molnar425e0962007-07-09 18:51:58 +0200201}
202
203/*
Michael S. Tsirkin13b62e42013-09-16 11:30:36 +0300204 * Called when a process ceases being the active-running process involuntarily
205 * due, typically, to expiring its time slice (this may also be called when
206 * switching to the idle task). Now we can calculate how long we ran.
Bharath Ravid4abc232008-06-16 15:11:01 +0530207 * Also, if the process is still in the TASK_RUNNING state, call
Peter Zijlstra4e29fb72021-05-04 22:43:45 +0200208 * sched_info_enqueue() to mark that it has now again started waiting on
Bharath Ravid4abc232008-06-16 15:11:01 +0530209 * the runqueue.
Ingo Molnar425e0962007-07-09 18:51:58 +0200210 */
Michael S. Tsirkin43148952013-09-22 17:20:54 +0300211static inline void sched_info_depart(struct rq *rq, struct task_struct *t)
Ingo Molnar425e0962007-07-09 18:51:58 +0200212{
Ingo Molnar97fb7a02018-03-03 14:01:12 +0100213 unsigned long long delta = rq_clock(rq) - t->sched_info.last_arrival;
Ingo Molnar425e0962007-07-09 18:51:58 +0200214
Michael S. Tsirkin43148952013-09-22 17:20:54 +0300215 rq_sched_info_depart(rq, delta);
Bharath Ravid4abc232008-06-16 15:11:01 +0530216
217 if (t->state == TASK_RUNNING)
Peter Zijlstra4e29fb72021-05-04 22:43:45 +0200218 sched_info_enqueue(rq, t);
Ingo Molnar425e0962007-07-09 18:51:58 +0200219}
220
221/*
222 * Called when tasks are switched involuntarily due, typically, to expiring
223 * their time slice. (This may also be called when switching to or from
224 * the idle task.) We are only called when prev != next.
225 */
226static inline void
Peter Zijlstrac5895d32021-05-04 22:43:42 +0200227sched_info_switch(struct rq *rq, struct task_struct *prev, struct task_struct *next)
Ingo Molnar425e0962007-07-09 18:51:58 +0200228{
Ingo Molnar425e0962007-07-09 18:51:58 +0200229 /*
Ingo Molnar97fb7a02018-03-03 14:01:12 +0100230 * prev now departs the CPU. It's not interesting to record
Ingo Molnar425e0962007-07-09 18:51:58 +0200231 * stats about how efficient we were at scheduling the idle
232 * process, however.
233 */
234 if (prev != rq->idle)
Michael S. Tsirkin43148952013-09-22 17:20:54 +0300235 sched_info_depart(rq, prev);
Ingo Molnar425e0962007-07-09 18:51:58 +0200236
237 if (next != rq->idle)
Michael S. Tsirkin43148952013-09-22 17:20:54 +0300238 sched_info_arrive(rq, next);
Ingo Molnar425e0962007-07-09 18:51:58 +0200239}
Ingo Molnar97fb7a02018-03-03 14:01:12 +0100240
Ingo Molnar97fb7a02018-03-03 14:01:12 +0100241#else /* !CONFIG_SCHED_INFO: */
Peter Zijlstra4e29fb72021-05-04 22:43:45 +0200242# define sched_info_enqueue(rq, t) do { } while (0)
Peter Zijlstra4e29fb72021-05-04 22:43:45 +0200243# define sched_info_dequeue(rq, t) do { } while (0)
Ingo Molnar97fb7a02018-03-03 14:01:12 +0100244# define sched_info_switch(rq, t, next) do { } while (0)
Naveen N. Raof6db8342015-06-25 23:53:37 +0530245#endif /* CONFIG_SCHED_INFO */