blob: 3c0d8f133eb39474f01f25ca696246884d0acb2b [file] [log] [blame]
Ingo Molnar425e0962007-07-09 18:51:58 +02001
2#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyanb5aadf72008-10-06 13:23:43 +04003
Ingo Molnar425e0962007-07-09 18:51:58 +02004/*
5 * Expects runqueue lock to be held for atomicity of update
6 */
7static inline void
8rq_sched_info_arrive(struct rq *rq, unsigned long long delta)
9{
10 if (rq) {
11 rq->rq_sched_info.run_delay += delta;
Ingo Molnar2d723762007-10-15 17:00:12 +020012 rq->rq_sched_info.pcount++;
Ingo Molnar425e0962007-07-09 18:51:58 +020013 }
14}
15
16/*
17 * Expects runqueue lock to be held for atomicity of update
18 */
19static inline void
20rq_sched_info_depart(struct rq *rq, unsigned long long delta)
21{
22 if (rq)
Ken Chen9c2c4802008-12-16 23:41:22 -080023 rq->rq_cpu_time += delta;
Ingo Molnar425e0962007-07-09 18:51:58 +020024}
Ankita Garg46ac22b2008-07-01 14:30:06 +053025
26static inline void
27rq_sched_info_dequeued(struct rq *rq, unsigned long long delta)
28{
29 if (rq)
30 rq->rq_sched_info.run_delay += delta;
31}
Josh Poimboeufae928822016-06-17 12:43:24 -050032#define schedstat_enabled() static_branch_unlikely(&sched_schedstats)
33#define schedstat_inc(var) do { if (schedstat_enabled()) { var++; } } while (0)
34#define schedstat_add(var, amt) do { if (schedstat_enabled()) { var += (amt); } } while (0)
35#define schedstat_set(var, val) do { if (schedstat_enabled()) { var = (val); } } while (0)
Josh Poimboeuf20e1d482016-06-17 12:43:25 -050036#define schedstat_val(var) (var)
37#define schedstat_val_or_zero(var) ((schedstat_enabled()) ? (var) : 0)
Josh Poimboeuf9c572592016-06-03 17:58:40 -050038
Ingo Molnar425e0962007-07-09 18:51:58 +020039#else /* !CONFIG_SCHEDSTATS */
40static inline void
41rq_sched_info_arrive(struct rq *rq, unsigned long long delta)
42{}
43static inline void
Ankita Garg46ac22b2008-07-01 14:30:06 +053044rq_sched_info_dequeued(struct rq *rq, unsigned long long delta)
45{}
46static inline void
Ingo Molnar425e0962007-07-09 18:51:58 +020047rq_sched_info_depart(struct rq *rq, unsigned long long delta)
48{}
Josh Poimboeufae928822016-06-17 12:43:24 -050049#define schedstat_enabled() 0
50#define schedstat_inc(var) do { } while (0)
51#define schedstat_add(var, amt) do { } while (0)
52#define schedstat_set(var, val) do { } while (0)
53#define schedstat_val(var) 0
Josh Poimboeuf20e1d482016-06-17 12:43:25 -050054#define schedstat_val_or_zero(var) 0
Josh Poimboeufae928822016-06-17 12:43:24 -050055#endif /* CONFIG_SCHEDSTATS */
Ingo Molnar425e0962007-07-09 18:51:58 +020056
Johannes Weiner3df0e592018-10-26 15:06:27 -070057#ifdef CONFIG_PSI
58/*
59 * PSI tracks state that persists across sleeps, such as iowaits and
60 * memory stalls. As a result, it has to distinguish between sleeps,
61 * where a task's runnable state changes, and requeues, where a task
62 * and its state are being moved between CPUs and runqueues.
63 */
64static inline void psi_enqueue(struct task_struct *p, bool wakeup)
65{
66 int clear = 0, set = TSK_RUNNING;
67
Johannes Weinerc9f51ce2018-11-30 14:09:58 -080068 if (static_branch_likely(&psi_disabled))
Johannes Weiner3df0e592018-10-26 15:06:27 -070069 return;
70
71 if (!wakeup || p->sched_psi_wake_requeue) {
72 if (p->flags & PF_MEMSTALL)
73 set |= TSK_MEMSTALL;
74 if (p->sched_psi_wake_requeue)
75 p->sched_psi_wake_requeue = 0;
76 } else {
77 if (p->in_iowait)
78 clear |= TSK_IOWAIT;
79 }
80
81 psi_task_change(p, clear, set);
82}
83
84static inline void psi_dequeue(struct task_struct *p, bool sleep)
85{
86 int clear = TSK_RUNNING, set = 0;
87
Johannes Weinerc9f51ce2018-11-30 14:09:58 -080088 if (static_branch_likely(&psi_disabled))
Johannes Weiner3df0e592018-10-26 15:06:27 -070089 return;
90
91 if (!sleep) {
92 if (p->flags & PF_MEMSTALL)
93 clear |= TSK_MEMSTALL;
94 } else {
95 if (p->in_iowait)
96 set |= TSK_IOWAIT;
97 }
98
99 psi_task_change(p, clear, set);
100}
101
102static inline void psi_ttwu_dequeue(struct task_struct *p)
103{
Johannes Weinerc9f51ce2018-11-30 14:09:58 -0800104 if (static_branch_likely(&psi_disabled))
Johannes Weiner3df0e592018-10-26 15:06:27 -0700105 return;
106 /*
107 * Is the task being migrated during a wakeup? Make sure to
108 * deregister its sleep-persistent psi states from the old
109 * queue, and let psi_enqueue() know it has to requeue.
110 */
111 if (unlikely(p->in_iowait || (p->flags & PF_MEMSTALL))) {
112 struct rq_flags rf;
113 struct rq *rq;
114 int clear = 0;
115
116 if (p->in_iowait)
117 clear |= TSK_IOWAIT;
118 if (p->flags & PF_MEMSTALL)
119 clear |= TSK_MEMSTALL;
120
121 rq = __task_rq_lock(p, &rf);
122 psi_task_change(p, clear, 0);
123 p->sched_psi_wake_requeue = 1;
124 __task_rq_unlock(rq, &rf);
125 }
126}
127
128static inline void psi_task_tick(struct rq *rq)
129{
Johannes Weinerc9f51ce2018-11-30 14:09:58 -0800130 if (static_branch_likely(&psi_disabled))
Johannes Weiner3df0e592018-10-26 15:06:27 -0700131 return;
132
133 if (unlikely(rq->curr->flags & PF_MEMSTALL))
134 psi_memstall_tick(rq->curr, cpu_of(rq));
135}
136#else /* CONFIG_PSI */
137static inline void psi_enqueue(struct task_struct *p, bool wakeup) {}
138static inline void psi_dequeue(struct task_struct *p, bool sleep) {}
139static inline void psi_ttwu_dequeue(struct task_struct *p) {}
140static inline void psi_task_tick(struct rq *rq) {}
141#endif /* CONFIG_PSI */
142
Naveen N. Raof6db8342015-06-25 23:53:37 +0530143#ifdef CONFIG_SCHED_INFO
Ankita Garg46ac22b2008-07-01 14:30:06 +0530144static inline void sched_info_reset_dequeued(struct task_struct *t)
145{
146 t->sched_info.last_queued = 0;
147}
148
Ingo Molnar425e0962007-07-09 18:51:58 +0200149/*
Rakib Mullickd4a6f3c2010-10-24 16:28:47 +0600150 * We are interested in knowing how long it was from the *first* time a
Ankita Garg46ac22b2008-07-01 14:30:06 +0530151 * task was queued to the time that it finally hit a cpu, we call this routine
152 * from dequeue_task() to account for possible rq->clock skew across cpus. The
153 * delta taken on each cpu would annul the skew.
Ingo Molnar425e0962007-07-09 18:51:58 +0200154 */
Michael S. Tsirkin43148952013-09-22 17:20:54 +0300155static inline void sched_info_dequeued(struct rq *rq, struct task_struct *t)
Ingo Molnar425e0962007-07-09 18:51:58 +0200156{
Michael S. Tsirkin43148952013-09-22 17:20:54 +0300157 unsigned long long now = rq_clock(rq), delta = 0;
Ankita Garg46ac22b2008-07-01 14:30:06 +0530158
159 if (unlikely(sched_info_on()))
160 if (t->sched_info.last_queued)
161 delta = now - t->sched_info.last_queued;
162 sched_info_reset_dequeued(t);
163 t->sched_info.run_delay += delta;
164
Michael S. Tsirkin43148952013-09-22 17:20:54 +0300165 rq_sched_info_dequeued(rq, delta);
Ingo Molnar425e0962007-07-09 18:51:58 +0200166}
167
168/*
169 * Called when a task finally hits the cpu. We can now calculate how
170 * long it was waiting to run. We also note when it began so that we
171 * can keep stats on how long its timeslice is.
172 */
Michael S. Tsirkin43148952013-09-22 17:20:54 +0300173static void sched_info_arrive(struct rq *rq, struct task_struct *t)
Ingo Molnar425e0962007-07-09 18:51:58 +0200174{
Michael S. Tsirkin43148952013-09-22 17:20:54 +0300175 unsigned long long now = rq_clock(rq), delta = 0;
Ingo Molnar425e0962007-07-09 18:51:58 +0200176
177 if (t->sched_info.last_queued)
178 delta = now - t->sched_info.last_queued;
Ankita Garg46ac22b2008-07-01 14:30:06 +0530179 sched_info_reset_dequeued(t);
Ingo Molnar425e0962007-07-09 18:51:58 +0200180 t->sched_info.run_delay += delta;
181 t->sched_info.last_arrival = now;
Ingo Molnar2d723762007-10-15 17:00:12 +0200182 t->sched_info.pcount++;
Ingo Molnar425e0962007-07-09 18:51:58 +0200183
Michael S. Tsirkin43148952013-09-22 17:20:54 +0300184 rq_sched_info_arrive(rq, delta);
Ingo Molnar425e0962007-07-09 18:51:58 +0200185}
186
187/*
Ingo Molnar425e0962007-07-09 18:51:58 +0200188 * This function is only called from enqueue_task(), but also only updates
189 * the timestamp if it is already not set. It's assumed that
190 * sched_info_dequeued() will clear that stamp when appropriate.
191 */
Michael S. Tsirkin43148952013-09-22 17:20:54 +0300192static inline void sched_info_queued(struct rq *rq, struct task_struct *t)
Ingo Molnar425e0962007-07-09 18:51:58 +0200193{
194 if (unlikely(sched_info_on()))
195 if (!t->sched_info.last_queued)
Michael S. Tsirkin43148952013-09-22 17:20:54 +0300196 t->sched_info.last_queued = rq_clock(rq);
Ingo Molnar425e0962007-07-09 18:51:58 +0200197}
198
199/*
Michael S. Tsirkin13b62e42013-09-16 11:30:36 +0300200 * Called when a process ceases being the active-running process involuntarily
201 * due, typically, to expiring its time slice (this may also be called when
202 * switching to the idle task). Now we can calculate how long we ran.
Bharath Ravid4abc232008-06-16 15:11:01 +0530203 * Also, if the process is still in the TASK_RUNNING state, call
204 * sched_info_queued() to mark that it has now again started waiting on
205 * the runqueue.
Ingo Molnar425e0962007-07-09 18:51:58 +0200206 */
Michael S. Tsirkin43148952013-09-22 17:20:54 +0300207static inline void sched_info_depart(struct rq *rq, struct task_struct *t)
Ingo Molnar425e0962007-07-09 18:51:58 +0200208{
Michael S. Tsirkin43148952013-09-22 17:20:54 +0300209 unsigned long long delta = rq_clock(rq) -
Balbir Singh9a417852007-11-09 22:39:37 +0100210 t->sched_info.last_arrival;
Ingo Molnar425e0962007-07-09 18:51:58 +0200211
Michael S. Tsirkin43148952013-09-22 17:20:54 +0300212 rq_sched_info_depart(rq, delta);
Bharath Ravid4abc232008-06-16 15:11:01 +0530213
214 if (t->state == TASK_RUNNING)
Michael S. Tsirkin43148952013-09-22 17:20:54 +0300215 sched_info_queued(rq, t);
Ingo Molnar425e0962007-07-09 18:51:58 +0200216}
217
218/*
219 * Called when tasks are switched involuntarily due, typically, to expiring
220 * their time slice. (This may also be called when switching to or from
221 * the idle task.) We are only called when prev != next.
222 */
223static inline void
Michael S. Tsirkin43148952013-09-22 17:20:54 +0300224__sched_info_switch(struct rq *rq,
225 struct task_struct *prev, struct task_struct *next)
Ingo Molnar425e0962007-07-09 18:51:58 +0200226{
Ingo Molnar425e0962007-07-09 18:51:58 +0200227 /*
228 * prev now departs the cpu. It's not interesting to record
229 * stats about how efficient we were at scheduling the idle
230 * process, however.
231 */
232 if (prev != rq->idle)
Michael S. Tsirkin43148952013-09-22 17:20:54 +0300233 sched_info_depart(rq, prev);
Ingo Molnar425e0962007-07-09 18:51:58 +0200234
235 if (next != rq->idle)
Michael S. Tsirkin43148952013-09-22 17:20:54 +0300236 sched_info_arrive(rq, next);
Ingo Molnar425e0962007-07-09 18:51:58 +0200237}
238static inline void
Michael S. Tsirkin43148952013-09-22 17:20:54 +0300239sched_info_switch(struct rq *rq,
240 struct task_struct *prev, struct task_struct *next)
Ingo Molnar425e0962007-07-09 18:51:58 +0200241{
242 if (unlikely(sched_info_on()))
Michael S. Tsirkin43148952013-09-22 17:20:54 +0300243 __sched_info_switch(rq, prev, next);
Ingo Molnar425e0962007-07-09 18:51:58 +0200244}
245#else
Michael S. Tsirkin43148952013-09-22 17:20:54 +0300246#define sched_info_queued(rq, t) do { } while (0)
Ankita Garg46ac22b2008-07-01 14:30:06 +0530247#define sched_info_reset_dequeued(t) do { } while (0)
Michael S. Tsirkin43148952013-09-22 17:20:54 +0300248#define sched_info_dequeued(rq, t) do { } while (0)
249#define sched_info_depart(rq, t) do { } while (0)
250#define sched_info_arrive(rq, next) do { } while (0)
251#define sched_info_switch(rq, t, next) do { } while (0)
Naveen N. Raof6db8342015-06-25 23:53:37 +0530252#endif /* CONFIG_SCHED_INFO */
Ingo Molnar425e0962007-07-09 18:51:58 +0200253
Frank Mayharbb34d922008-09-12 09:54:39 -0700254/*
255 * The following are functions that support scheduler-internal time accounting.
256 * These functions are generally called at the timer tick. None of this depends
257 * on CONFIG_SCHEDSTATS.
258 */
259
Frank Mayharbb34d922008-09-12 09:54:39 -0700260/**
KOSAKI Motohirofa18f7b2013-05-26 17:35:41 -0400261 * cputimer_running - return true if cputimer is running
262 *
263 * @tsk: Pointer to target task.
264 */
265static inline bool cputimer_running(struct task_struct *tsk)
266
267{
268 struct thread_group_cputimer *cputimer = &tsk->signal->cputimer;
269
Jason Low10180162015-04-28 13:00:22 -0700270 /* Check if cputimer isn't running. This is accessed without locking. */
271 if (!READ_ONCE(cputimer->running))
KOSAKI Motohirofa18f7b2013-05-26 17:35:41 -0400272 return false;
273
274 /*
275 * After we flush the task's sum_exec_runtime to sig->sum_sched_runtime
276 * in __exit_signal(), we won't account to the signal struct further
277 * cputime consumed by that task, even though the task can still be
278 * ticking after __exit_signal().
279 *
280 * In order to keep a consistent behaviour between thread group cputime
281 * and thread group cputimer accounting, lets also ignore the cputime
282 * elapsing after __exit_signal() in any thread group timer running.
283 *
284 * This makes sure that POSIX CPU clocks and timers are synchronized, so
285 * that a POSIX CPU timer won't expire while the corresponding POSIX CPU
286 * clock delta is behind the expiring timer value.
287 */
288 if (unlikely(!tsk->sighand))
289 return false;
290
291 return true;
292}
293
294/**
Frank Mayhar7086efe2008-09-12 09:54:39 -0700295 * account_group_user_time - Maintain utime for a thread group.
Frank Mayharbb34d922008-09-12 09:54:39 -0700296 *
Frank Mayhar7086efe2008-09-12 09:54:39 -0700297 * @tsk: Pointer to task structure.
298 * @cputime: Time value by which to increment the utime field of the
299 * thread_group_cputime structure.
Frank Mayharbb34d922008-09-12 09:54:39 -0700300 *
301 * If thread group time is being maintained, get the structure for the
302 * running CPU and update the utime field there.
303 */
Frank Mayhar7086efe2008-09-12 09:54:39 -0700304static inline void account_group_user_time(struct task_struct *tsk,
305 cputime_t cputime)
Frank Mayharbb34d922008-09-12 09:54:39 -0700306{
Oleg Nesterov48286d52010-06-11 01:09:52 +0200307 struct thread_group_cputimer *cputimer = &tsk->signal->cputimer;
Frank Mayharbb34d922008-09-12 09:54:39 -0700308
KOSAKI Motohirofa18f7b2013-05-26 17:35:41 -0400309 if (!cputimer_running(tsk))
Peter Zijlstra4cd4c1b2009-02-05 12:24:16 +0100310 return;
311
Jason Low71107442015-04-28 13:00:24 -0700312 atomic64_add(cputime, &cputimer->cputime_atomic.utime);
Frank Mayharbb34d922008-09-12 09:54:39 -0700313}
314
315/**
Frank Mayhar7086efe2008-09-12 09:54:39 -0700316 * account_group_system_time - Maintain stime for a thread group.
Frank Mayharbb34d922008-09-12 09:54:39 -0700317 *
Frank Mayhar7086efe2008-09-12 09:54:39 -0700318 * @tsk: Pointer to task structure.
319 * @cputime: Time value by which to increment the stime field of the
320 * thread_group_cputime structure.
Frank Mayharbb34d922008-09-12 09:54:39 -0700321 *
322 * If thread group time is being maintained, get the structure for the
323 * running CPU and update the stime field there.
324 */
Frank Mayhar7086efe2008-09-12 09:54:39 -0700325static inline void account_group_system_time(struct task_struct *tsk,
326 cputime_t cputime)
Frank Mayharbb34d922008-09-12 09:54:39 -0700327{
Oleg Nesterov48286d52010-06-11 01:09:52 +0200328 struct thread_group_cputimer *cputimer = &tsk->signal->cputimer;
Frank Mayharbb34d922008-09-12 09:54:39 -0700329
KOSAKI Motohirofa18f7b2013-05-26 17:35:41 -0400330 if (!cputimer_running(tsk))
Peter Zijlstra4cd4c1b2009-02-05 12:24:16 +0100331 return;
332
Jason Low71107442015-04-28 13:00:24 -0700333 atomic64_add(cputime, &cputimer->cputime_atomic.stime);
Frank Mayharbb34d922008-09-12 09:54:39 -0700334}
335
336/**
Frank Mayhar7086efe2008-09-12 09:54:39 -0700337 * account_group_exec_runtime - Maintain exec runtime for a thread group.
Frank Mayharbb34d922008-09-12 09:54:39 -0700338 *
Frank Mayhar7086efe2008-09-12 09:54:39 -0700339 * @tsk: Pointer to task structure.
Frank Mayharbb34d922008-09-12 09:54:39 -0700340 * @ns: Time value by which to increment the sum_exec_runtime field
Frank Mayhar7086efe2008-09-12 09:54:39 -0700341 * of the thread_group_cputime structure.
Frank Mayharbb34d922008-09-12 09:54:39 -0700342 *
343 * If thread group time is being maintained, get the structure for the
344 * running CPU and update the sum_exec_runtime field there.
345 */
Frank Mayhar7086efe2008-09-12 09:54:39 -0700346static inline void account_group_exec_runtime(struct task_struct *tsk,
347 unsigned long long ns)
Frank Mayharbb34d922008-09-12 09:54:39 -0700348{
Oleg Nesterov48286d52010-06-11 01:09:52 +0200349 struct thread_group_cputimer *cputimer = &tsk->signal->cputimer;
Frank Mayharbb34d922008-09-12 09:54:39 -0700350
KOSAKI Motohirofa18f7b2013-05-26 17:35:41 -0400351 if (!cputimer_running(tsk))
Peter Zijlstra4cd4c1b2009-02-05 12:24:16 +0100352 return;
353
Jason Low71107442015-04-28 13:00:24 -0700354 atomic64_add(ns, &cputimer->cputime_atomic.sum_exec_runtime);
Frank Mayharbb34d922008-09-12 09:54:39 -0700355}