blob: 78955cbea31c4a5378bc0b73f5f3bbcbdfa23d75 [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}
Mel Gormancb251762016-02-05 09:08:36 +000032# define schedstat_enabled() static_branch_unlikely(&sched_schedstats)
33# define schedstat_inc(rq, field) do { if (schedstat_enabled()) { (rq)->field++; } } while (0)
34# define schedstat_add(rq, field, amt) do { if (schedstat_enabled()) { (rq)->field += (amt); } } while (0)
35# define schedstat_set(var, val) do { if (schedstat_enabled()) { var = (val); } } while (0)
Josh Poimboeuf9c572592016-06-03 17:58:40 -050036# define schedstat_val(rq, field) ((schedstat_enabled()) ? (rq)->field : 0)
37
Ingo Molnar425e0962007-07-09 18:51:58 +020038#else /* !CONFIG_SCHEDSTATS */
39static inline void
40rq_sched_info_arrive(struct rq *rq, unsigned long long delta)
41{}
42static inline void
Ankita Garg46ac22b2008-07-01 14:30:06 +053043rq_sched_info_dequeued(struct rq *rq, unsigned long long delta)
44{}
45static inline void
Ingo Molnar425e0962007-07-09 18:51:58 +020046rq_sched_info_depart(struct rq *rq, unsigned long long delta)
47{}
Mel Gormancb251762016-02-05 09:08:36 +000048# define schedstat_enabled() 0
Ingo Molnar425e0962007-07-09 18:51:58 +020049# define schedstat_inc(rq, field) do { } while (0)
50# define schedstat_add(rq, field, amt) do { } while (0)
Ingo Molnarc3c70112007-08-02 17:41:40 +020051# define schedstat_set(var, val) do { } while (0)
Josh Poimboeuf9c572592016-06-03 17:58:40 -050052# define schedstat_val(rq, field) 0
Ingo Molnar425e0962007-07-09 18:51:58 +020053#endif
54
Naveen N. Raof6db8342015-06-25 23:53:37 +053055#ifdef CONFIG_SCHED_INFO
Ankita Garg46ac22b2008-07-01 14:30:06 +053056static inline void sched_info_reset_dequeued(struct task_struct *t)
57{
58 t->sched_info.last_queued = 0;
59}
60
Ingo Molnar425e0962007-07-09 18:51:58 +020061/*
Rakib Mullickd4a6f3c2010-10-24 16:28:47 +060062 * We are interested in knowing how long it was from the *first* time a
Ankita Garg46ac22b2008-07-01 14:30:06 +053063 * task was queued to the time that it finally hit a cpu, we call this routine
64 * from dequeue_task() to account for possible rq->clock skew across cpus. The
65 * delta taken on each cpu would annul the skew.
Ingo Molnar425e0962007-07-09 18:51:58 +020066 */
Michael S. Tsirkin43148952013-09-22 17:20:54 +030067static inline void sched_info_dequeued(struct rq *rq, struct task_struct *t)
Ingo Molnar425e0962007-07-09 18:51:58 +020068{
Michael S. Tsirkin43148952013-09-22 17:20:54 +030069 unsigned long long now = rq_clock(rq), delta = 0;
Ankita Garg46ac22b2008-07-01 14:30:06 +053070
71 if (unlikely(sched_info_on()))
72 if (t->sched_info.last_queued)
73 delta = now - t->sched_info.last_queued;
74 sched_info_reset_dequeued(t);
75 t->sched_info.run_delay += delta;
76
Michael S. Tsirkin43148952013-09-22 17:20:54 +030077 rq_sched_info_dequeued(rq, delta);
Ingo Molnar425e0962007-07-09 18:51:58 +020078}
79
80/*
81 * Called when a task finally hits the cpu. We can now calculate how
82 * long it was waiting to run. We also note when it began so that we
83 * can keep stats on how long its timeslice is.
84 */
Michael S. Tsirkin43148952013-09-22 17:20:54 +030085static void sched_info_arrive(struct rq *rq, struct task_struct *t)
Ingo Molnar425e0962007-07-09 18:51:58 +020086{
Michael S. Tsirkin43148952013-09-22 17:20:54 +030087 unsigned long long now = rq_clock(rq), delta = 0;
Ingo Molnar425e0962007-07-09 18:51:58 +020088
89 if (t->sched_info.last_queued)
90 delta = now - t->sched_info.last_queued;
Ankita Garg46ac22b2008-07-01 14:30:06 +053091 sched_info_reset_dequeued(t);
Ingo Molnar425e0962007-07-09 18:51:58 +020092 t->sched_info.run_delay += delta;
93 t->sched_info.last_arrival = now;
Ingo Molnar2d723762007-10-15 17:00:12 +020094 t->sched_info.pcount++;
Ingo Molnar425e0962007-07-09 18:51:58 +020095
Michael S. Tsirkin43148952013-09-22 17:20:54 +030096 rq_sched_info_arrive(rq, delta);
Ingo Molnar425e0962007-07-09 18:51:58 +020097}
98
99/*
Ingo Molnar425e0962007-07-09 18:51:58 +0200100 * This function is only called from enqueue_task(), but also only updates
101 * the timestamp if it is already not set. It's assumed that
102 * sched_info_dequeued() will clear that stamp when appropriate.
103 */
Michael S. Tsirkin43148952013-09-22 17:20:54 +0300104static inline void sched_info_queued(struct rq *rq, struct task_struct *t)
Ingo Molnar425e0962007-07-09 18:51:58 +0200105{
106 if (unlikely(sched_info_on()))
107 if (!t->sched_info.last_queued)
Michael S. Tsirkin43148952013-09-22 17:20:54 +0300108 t->sched_info.last_queued = rq_clock(rq);
Ingo Molnar425e0962007-07-09 18:51:58 +0200109}
110
111/*
Michael S. Tsirkin13b62e42013-09-16 11:30:36 +0300112 * Called when a process ceases being the active-running process involuntarily
113 * due, typically, to expiring its time slice (this may also be called when
114 * switching to the idle task). Now we can calculate how long we ran.
Bharath Ravid4abc232008-06-16 15:11:01 +0530115 * Also, if the process is still in the TASK_RUNNING state, call
116 * sched_info_queued() to mark that it has now again started waiting on
117 * the runqueue.
Ingo Molnar425e0962007-07-09 18:51:58 +0200118 */
Michael S. Tsirkin43148952013-09-22 17:20:54 +0300119static inline void sched_info_depart(struct rq *rq, struct task_struct *t)
Ingo Molnar425e0962007-07-09 18:51:58 +0200120{
Michael S. Tsirkin43148952013-09-22 17:20:54 +0300121 unsigned long long delta = rq_clock(rq) -
Balbir Singh9a417852007-11-09 22:39:37 +0100122 t->sched_info.last_arrival;
Ingo Molnar425e0962007-07-09 18:51:58 +0200123
Michael S. Tsirkin43148952013-09-22 17:20:54 +0300124 rq_sched_info_depart(rq, delta);
Bharath Ravid4abc232008-06-16 15:11:01 +0530125
126 if (t->state == TASK_RUNNING)
Michael S. Tsirkin43148952013-09-22 17:20:54 +0300127 sched_info_queued(rq, t);
Ingo Molnar425e0962007-07-09 18:51:58 +0200128}
129
130/*
131 * Called when tasks are switched involuntarily due, typically, to expiring
132 * their time slice. (This may also be called when switching to or from
133 * the idle task.) We are only called when prev != next.
134 */
135static inline void
Michael S. Tsirkin43148952013-09-22 17:20:54 +0300136__sched_info_switch(struct rq *rq,
137 struct task_struct *prev, struct task_struct *next)
Ingo Molnar425e0962007-07-09 18:51:58 +0200138{
Ingo Molnar425e0962007-07-09 18:51:58 +0200139 /*
140 * prev now departs the cpu. It's not interesting to record
141 * stats about how efficient we were at scheduling the idle
142 * process, however.
143 */
144 if (prev != rq->idle)
Michael S. Tsirkin43148952013-09-22 17:20:54 +0300145 sched_info_depart(rq, prev);
Ingo Molnar425e0962007-07-09 18:51:58 +0200146
147 if (next != rq->idle)
Michael S. Tsirkin43148952013-09-22 17:20:54 +0300148 sched_info_arrive(rq, next);
Ingo Molnar425e0962007-07-09 18:51:58 +0200149}
150static inline void
Michael S. Tsirkin43148952013-09-22 17:20:54 +0300151sched_info_switch(struct rq *rq,
152 struct task_struct *prev, struct task_struct *next)
Ingo Molnar425e0962007-07-09 18:51:58 +0200153{
154 if (unlikely(sched_info_on()))
Michael S. Tsirkin43148952013-09-22 17:20:54 +0300155 __sched_info_switch(rq, prev, next);
Ingo Molnar425e0962007-07-09 18:51:58 +0200156}
157#else
Michael S. Tsirkin43148952013-09-22 17:20:54 +0300158#define sched_info_queued(rq, t) do { } while (0)
Ankita Garg46ac22b2008-07-01 14:30:06 +0530159#define sched_info_reset_dequeued(t) do { } while (0)
Michael S. Tsirkin43148952013-09-22 17:20:54 +0300160#define sched_info_dequeued(rq, t) do { } while (0)
161#define sched_info_depart(rq, t) do { } while (0)
162#define sched_info_arrive(rq, next) do { } while (0)
163#define sched_info_switch(rq, t, next) do { } while (0)
Naveen N. Raof6db8342015-06-25 23:53:37 +0530164#endif /* CONFIG_SCHED_INFO */
Ingo Molnar425e0962007-07-09 18:51:58 +0200165
Frank Mayharbb34d922008-09-12 09:54:39 -0700166/*
167 * The following are functions that support scheduler-internal time accounting.
168 * These functions are generally called at the timer tick. None of this depends
169 * on CONFIG_SCHEDSTATS.
170 */
171
Frank Mayharbb34d922008-09-12 09:54:39 -0700172/**
KOSAKI Motohirofa18f7b2013-05-26 17:35:41 -0400173 * cputimer_running - return true if cputimer is running
174 *
175 * @tsk: Pointer to target task.
176 */
177static inline bool cputimer_running(struct task_struct *tsk)
178
179{
180 struct thread_group_cputimer *cputimer = &tsk->signal->cputimer;
181
Jason Low10180162015-04-28 13:00:22 -0700182 /* Check if cputimer isn't running. This is accessed without locking. */
183 if (!READ_ONCE(cputimer->running))
KOSAKI Motohirofa18f7b2013-05-26 17:35:41 -0400184 return false;
185
186 /*
187 * After we flush the task's sum_exec_runtime to sig->sum_sched_runtime
188 * in __exit_signal(), we won't account to the signal struct further
189 * cputime consumed by that task, even though the task can still be
190 * ticking after __exit_signal().
191 *
192 * In order to keep a consistent behaviour between thread group cputime
193 * and thread group cputimer accounting, lets also ignore the cputime
194 * elapsing after __exit_signal() in any thread group timer running.
195 *
196 * This makes sure that POSIX CPU clocks and timers are synchronized, so
197 * that a POSIX CPU timer won't expire while the corresponding POSIX CPU
198 * clock delta is behind the expiring timer value.
199 */
200 if (unlikely(!tsk->sighand))
201 return false;
202
203 return true;
204}
205
206/**
Frank Mayhar7086efe2008-09-12 09:54:39 -0700207 * account_group_user_time - Maintain utime for a thread group.
Frank Mayharbb34d922008-09-12 09:54:39 -0700208 *
Frank Mayhar7086efe2008-09-12 09:54:39 -0700209 * @tsk: Pointer to task structure.
210 * @cputime: Time value by which to increment the utime field of the
211 * thread_group_cputime structure.
Frank Mayharbb34d922008-09-12 09:54:39 -0700212 *
213 * If thread group time is being maintained, get the structure for the
214 * running CPU and update the utime field there.
215 */
Frank Mayhar7086efe2008-09-12 09:54:39 -0700216static inline void account_group_user_time(struct task_struct *tsk,
217 cputime_t cputime)
Frank Mayharbb34d922008-09-12 09:54:39 -0700218{
Oleg Nesterov48286d52010-06-11 01:09:52 +0200219 struct thread_group_cputimer *cputimer = &tsk->signal->cputimer;
Frank Mayharbb34d922008-09-12 09:54:39 -0700220
KOSAKI Motohirofa18f7b2013-05-26 17:35:41 -0400221 if (!cputimer_running(tsk))
Peter Zijlstra4cd4c1b2009-02-05 12:24:16 +0100222 return;
223
Jason Low71107442015-04-28 13:00:24 -0700224 atomic64_add(cputime, &cputimer->cputime_atomic.utime);
Frank Mayharbb34d922008-09-12 09:54:39 -0700225}
226
227/**
Frank Mayhar7086efe2008-09-12 09:54:39 -0700228 * account_group_system_time - Maintain stime for a thread group.
Frank Mayharbb34d922008-09-12 09:54:39 -0700229 *
Frank Mayhar7086efe2008-09-12 09:54:39 -0700230 * @tsk: Pointer to task structure.
231 * @cputime: Time value by which to increment the stime field of the
232 * thread_group_cputime structure.
Frank Mayharbb34d922008-09-12 09:54:39 -0700233 *
234 * If thread group time is being maintained, get the structure for the
235 * running CPU and update the stime field there.
236 */
Frank Mayhar7086efe2008-09-12 09:54:39 -0700237static inline void account_group_system_time(struct task_struct *tsk,
238 cputime_t cputime)
Frank Mayharbb34d922008-09-12 09:54:39 -0700239{
Oleg Nesterov48286d52010-06-11 01:09:52 +0200240 struct thread_group_cputimer *cputimer = &tsk->signal->cputimer;
Frank Mayharbb34d922008-09-12 09:54:39 -0700241
KOSAKI Motohirofa18f7b2013-05-26 17:35:41 -0400242 if (!cputimer_running(tsk))
Peter Zijlstra4cd4c1b2009-02-05 12:24:16 +0100243 return;
244
Jason Low71107442015-04-28 13:00:24 -0700245 atomic64_add(cputime, &cputimer->cputime_atomic.stime);
Frank Mayharbb34d922008-09-12 09:54:39 -0700246}
247
248/**
Frank Mayhar7086efe2008-09-12 09:54:39 -0700249 * account_group_exec_runtime - Maintain exec runtime for a thread group.
Frank Mayharbb34d922008-09-12 09:54:39 -0700250 *
Frank Mayhar7086efe2008-09-12 09:54:39 -0700251 * @tsk: Pointer to task structure.
Frank Mayharbb34d922008-09-12 09:54:39 -0700252 * @ns: Time value by which to increment the sum_exec_runtime field
Frank Mayhar7086efe2008-09-12 09:54:39 -0700253 * of the thread_group_cputime structure.
Frank Mayharbb34d922008-09-12 09:54:39 -0700254 *
255 * If thread group time is being maintained, get the structure for the
256 * running CPU and update the sum_exec_runtime field there.
257 */
Frank Mayhar7086efe2008-09-12 09:54:39 -0700258static inline void account_group_exec_runtime(struct task_struct *tsk,
259 unsigned long long ns)
Frank Mayharbb34d922008-09-12 09:54:39 -0700260{
Oleg Nesterov48286d52010-06-11 01:09:52 +0200261 struct thread_group_cputimer *cputimer = &tsk->signal->cputimer;
Frank Mayharbb34d922008-09-12 09:54:39 -0700262
KOSAKI Motohirofa18f7b2013-05-26 17:35:41 -0400263 if (!cputimer_running(tsk))
Peter Zijlstra4cd4c1b2009-02-05 12:24:16 +0100264 return;
265
Jason Low71107442015-04-28 13:00:24 -0700266 atomic64_add(ns, &cputimer->cputime_atomic.sum_exec_runtime);
Frank Mayharbb34d922008-09-12 09:54:39 -0700267}