Ingo Molnar | 425e096 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1 | |
| 2 | #ifdef CONFIG_SCHEDSTATS |
Alexey Dobriyan | b5aadf7 | 2008-10-06 13:23:43 +0400 | [diff] [blame] | 3 | |
Ingo Molnar | 425e096 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 4 | /* |
| 5 | * Expects runqueue lock to be held for atomicity of update |
| 6 | */ |
| 7 | static inline void |
| 8 | rq_sched_info_arrive(struct rq *rq, unsigned long long delta) |
| 9 | { |
| 10 | if (rq) { |
| 11 | rq->rq_sched_info.run_delay += delta; |
Ingo Molnar | 2d72376 | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 12 | rq->rq_sched_info.pcount++; |
Ingo Molnar | 425e096 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 13 | } |
| 14 | } |
| 15 | |
| 16 | /* |
| 17 | * Expects runqueue lock to be held for atomicity of update |
| 18 | */ |
| 19 | static inline void |
| 20 | rq_sched_info_depart(struct rq *rq, unsigned long long delta) |
| 21 | { |
| 22 | if (rq) |
Ken Chen | 9c2c480 | 2008-12-16 23:41:22 -0800 | [diff] [blame] | 23 | rq->rq_cpu_time += delta; |
Ingo Molnar | 425e096 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 24 | } |
Ankita Garg | 46ac22b | 2008-07-01 14:30:06 +0530 | [diff] [blame] | 25 | |
| 26 | static inline void |
| 27 | rq_sched_info_dequeued(struct rq *rq, unsigned long long delta) |
| 28 | { |
| 29 | if (rq) |
| 30 | rq->rq_sched_info.run_delay += delta; |
| 31 | } |
Mel Gorman | cb25176 | 2016-02-05 09:08:36 +0000 | [diff] [blame] | 32 | # 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 Poimboeuf | 9c57259 | 2016-06-03 17:58:40 -0500 | [diff] [blame] | 36 | # define schedstat_val(rq, field) ((schedstat_enabled()) ? (rq)->field : 0) |
| 37 | |
Ingo Molnar | 425e096 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 38 | #else /* !CONFIG_SCHEDSTATS */ |
| 39 | static inline void |
| 40 | rq_sched_info_arrive(struct rq *rq, unsigned long long delta) |
| 41 | {} |
| 42 | static inline void |
Ankita Garg | 46ac22b | 2008-07-01 14:30:06 +0530 | [diff] [blame] | 43 | rq_sched_info_dequeued(struct rq *rq, unsigned long long delta) |
| 44 | {} |
| 45 | static inline void |
Ingo Molnar | 425e096 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 46 | rq_sched_info_depart(struct rq *rq, unsigned long long delta) |
| 47 | {} |
Mel Gorman | cb25176 | 2016-02-05 09:08:36 +0000 | [diff] [blame] | 48 | # define schedstat_enabled() 0 |
Ingo Molnar | 425e096 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 49 | # define schedstat_inc(rq, field) do { } while (0) |
| 50 | # define schedstat_add(rq, field, amt) do { } while (0) |
Ingo Molnar | c3c7011 | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 51 | # define schedstat_set(var, val) do { } while (0) |
Josh Poimboeuf | 9c57259 | 2016-06-03 17:58:40 -0500 | [diff] [blame] | 52 | # define schedstat_val(rq, field) 0 |
Ingo Molnar | 425e096 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 53 | #endif |
| 54 | |
Naveen N. Rao | f6db834 | 2015-06-25 23:53:37 +0530 | [diff] [blame] | 55 | #ifdef CONFIG_SCHED_INFO |
Ankita Garg | 46ac22b | 2008-07-01 14:30:06 +0530 | [diff] [blame] | 56 | static inline void sched_info_reset_dequeued(struct task_struct *t) |
| 57 | { |
| 58 | t->sched_info.last_queued = 0; |
| 59 | } |
| 60 | |
Ingo Molnar | 425e096 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 61 | /* |
Rakib Mullick | d4a6f3c | 2010-10-24 16:28:47 +0600 | [diff] [blame] | 62 | * We are interested in knowing how long it was from the *first* time a |
Ankita Garg | 46ac22b | 2008-07-01 14:30:06 +0530 | [diff] [blame] | 63 | * 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 Molnar | 425e096 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 66 | */ |
Michael S. Tsirkin | 4314895 | 2013-09-22 17:20:54 +0300 | [diff] [blame] | 67 | static inline void sched_info_dequeued(struct rq *rq, struct task_struct *t) |
Ingo Molnar | 425e096 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 68 | { |
Michael S. Tsirkin | 4314895 | 2013-09-22 17:20:54 +0300 | [diff] [blame] | 69 | unsigned long long now = rq_clock(rq), delta = 0; |
Ankita Garg | 46ac22b | 2008-07-01 14:30:06 +0530 | [diff] [blame] | 70 | |
| 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. Tsirkin | 4314895 | 2013-09-22 17:20:54 +0300 | [diff] [blame] | 77 | rq_sched_info_dequeued(rq, delta); |
Ingo Molnar | 425e096 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 78 | } |
| 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. Tsirkin | 4314895 | 2013-09-22 17:20:54 +0300 | [diff] [blame] | 85 | static void sched_info_arrive(struct rq *rq, struct task_struct *t) |
Ingo Molnar | 425e096 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 86 | { |
Michael S. Tsirkin | 4314895 | 2013-09-22 17:20:54 +0300 | [diff] [blame] | 87 | unsigned long long now = rq_clock(rq), delta = 0; |
Ingo Molnar | 425e096 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 88 | |
| 89 | if (t->sched_info.last_queued) |
| 90 | delta = now - t->sched_info.last_queued; |
Ankita Garg | 46ac22b | 2008-07-01 14:30:06 +0530 | [diff] [blame] | 91 | sched_info_reset_dequeued(t); |
Ingo Molnar | 425e096 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 92 | t->sched_info.run_delay += delta; |
| 93 | t->sched_info.last_arrival = now; |
Ingo Molnar | 2d72376 | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 94 | t->sched_info.pcount++; |
Ingo Molnar | 425e096 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 95 | |
Michael S. Tsirkin | 4314895 | 2013-09-22 17:20:54 +0300 | [diff] [blame] | 96 | rq_sched_info_arrive(rq, delta); |
Ingo Molnar | 425e096 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /* |
Ingo Molnar | 425e096 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 100 | * 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. Tsirkin | 4314895 | 2013-09-22 17:20:54 +0300 | [diff] [blame] | 104 | static inline void sched_info_queued(struct rq *rq, struct task_struct *t) |
Ingo Molnar | 425e096 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 105 | { |
| 106 | if (unlikely(sched_info_on())) |
| 107 | if (!t->sched_info.last_queued) |
Michael S. Tsirkin | 4314895 | 2013-09-22 17:20:54 +0300 | [diff] [blame] | 108 | t->sched_info.last_queued = rq_clock(rq); |
Ingo Molnar | 425e096 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | /* |
Michael S. Tsirkin | 13b62e4 | 2013-09-16 11:30:36 +0300 | [diff] [blame] | 112 | * 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 Ravi | d4abc23 | 2008-06-16 15:11:01 +0530 | [diff] [blame] | 115 | * 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 Molnar | 425e096 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 118 | */ |
Michael S. Tsirkin | 4314895 | 2013-09-22 17:20:54 +0300 | [diff] [blame] | 119 | static inline void sched_info_depart(struct rq *rq, struct task_struct *t) |
Ingo Molnar | 425e096 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 120 | { |
Michael S. Tsirkin | 4314895 | 2013-09-22 17:20:54 +0300 | [diff] [blame] | 121 | unsigned long long delta = rq_clock(rq) - |
Balbir Singh | 9a41785 | 2007-11-09 22:39:37 +0100 | [diff] [blame] | 122 | t->sched_info.last_arrival; |
Ingo Molnar | 425e096 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 123 | |
Michael S. Tsirkin | 4314895 | 2013-09-22 17:20:54 +0300 | [diff] [blame] | 124 | rq_sched_info_depart(rq, delta); |
Bharath Ravi | d4abc23 | 2008-06-16 15:11:01 +0530 | [diff] [blame] | 125 | |
| 126 | if (t->state == TASK_RUNNING) |
Michael S. Tsirkin | 4314895 | 2013-09-22 17:20:54 +0300 | [diff] [blame] | 127 | sched_info_queued(rq, t); |
Ingo Molnar | 425e096 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 128 | } |
| 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 | */ |
| 135 | static inline void |
Michael S. Tsirkin | 4314895 | 2013-09-22 17:20:54 +0300 | [diff] [blame] | 136 | __sched_info_switch(struct rq *rq, |
| 137 | struct task_struct *prev, struct task_struct *next) |
Ingo Molnar | 425e096 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 138 | { |
Ingo Molnar | 425e096 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 139 | /* |
| 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. Tsirkin | 4314895 | 2013-09-22 17:20:54 +0300 | [diff] [blame] | 145 | sched_info_depart(rq, prev); |
Ingo Molnar | 425e096 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 146 | |
| 147 | if (next != rq->idle) |
Michael S. Tsirkin | 4314895 | 2013-09-22 17:20:54 +0300 | [diff] [blame] | 148 | sched_info_arrive(rq, next); |
Ingo Molnar | 425e096 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 149 | } |
| 150 | static inline void |
Michael S. Tsirkin | 4314895 | 2013-09-22 17:20:54 +0300 | [diff] [blame] | 151 | sched_info_switch(struct rq *rq, |
| 152 | struct task_struct *prev, struct task_struct *next) |
Ingo Molnar | 425e096 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 153 | { |
| 154 | if (unlikely(sched_info_on())) |
Michael S. Tsirkin | 4314895 | 2013-09-22 17:20:54 +0300 | [diff] [blame] | 155 | __sched_info_switch(rq, prev, next); |
Ingo Molnar | 425e096 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 156 | } |
| 157 | #else |
Michael S. Tsirkin | 4314895 | 2013-09-22 17:20:54 +0300 | [diff] [blame] | 158 | #define sched_info_queued(rq, t) do { } while (0) |
Ankita Garg | 46ac22b | 2008-07-01 14:30:06 +0530 | [diff] [blame] | 159 | #define sched_info_reset_dequeued(t) do { } while (0) |
Michael S. Tsirkin | 4314895 | 2013-09-22 17:20:54 +0300 | [diff] [blame] | 160 | #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. Rao | f6db834 | 2015-06-25 23:53:37 +0530 | [diff] [blame] | 164 | #endif /* CONFIG_SCHED_INFO */ |
Ingo Molnar | 425e096 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 165 | |
Frank Mayhar | bb34d92 | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 166 | /* |
| 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 Mayhar | bb34d92 | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 172 | /** |
KOSAKI Motohiro | fa18f7b | 2013-05-26 17:35:41 -0400 | [diff] [blame] | 173 | * cputimer_running - return true if cputimer is running |
| 174 | * |
| 175 | * @tsk: Pointer to target task. |
| 176 | */ |
| 177 | static inline bool cputimer_running(struct task_struct *tsk) |
| 178 | |
| 179 | { |
| 180 | struct thread_group_cputimer *cputimer = &tsk->signal->cputimer; |
| 181 | |
Jason Low | 1018016 | 2015-04-28 13:00:22 -0700 | [diff] [blame] | 182 | /* Check if cputimer isn't running. This is accessed without locking. */ |
| 183 | if (!READ_ONCE(cputimer->running)) |
KOSAKI Motohiro | fa18f7b | 2013-05-26 17:35:41 -0400 | [diff] [blame] | 184 | 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 Mayhar | 7086efe | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 207 | * account_group_user_time - Maintain utime for a thread group. |
Frank Mayhar | bb34d92 | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 208 | * |
Frank Mayhar | 7086efe | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 209 | * @tsk: Pointer to task structure. |
| 210 | * @cputime: Time value by which to increment the utime field of the |
| 211 | * thread_group_cputime structure. |
Frank Mayhar | bb34d92 | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 212 | * |
| 213 | * If thread group time is being maintained, get the structure for the |
| 214 | * running CPU and update the utime field there. |
| 215 | */ |
Frank Mayhar | 7086efe | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 216 | static inline void account_group_user_time(struct task_struct *tsk, |
| 217 | cputime_t cputime) |
Frank Mayhar | bb34d92 | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 218 | { |
Oleg Nesterov | 48286d5 | 2010-06-11 01:09:52 +0200 | [diff] [blame] | 219 | struct thread_group_cputimer *cputimer = &tsk->signal->cputimer; |
Frank Mayhar | bb34d92 | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 220 | |
KOSAKI Motohiro | fa18f7b | 2013-05-26 17:35:41 -0400 | [diff] [blame] | 221 | if (!cputimer_running(tsk)) |
Peter Zijlstra | 4cd4c1b | 2009-02-05 12:24:16 +0100 | [diff] [blame] | 222 | return; |
| 223 | |
Jason Low | 7110744 | 2015-04-28 13:00:24 -0700 | [diff] [blame] | 224 | atomic64_add(cputime, &cputimer->cputime_atomic.utime); |
Frank Mayhar | bb34d92 | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | /** |
Frank Mayhar | 7086efe | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 228 | * account_group_system_time - Maintain stime for a thread group. |
Frank Mayhar | bb34d92 | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 229 | * |
Frank Mayhar | 7086efe | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 230 | * @tsk: Pointer to task structure. |
| 231 | * @cputime: Time value by which to increment the stime field of the |
| 232 | * thread_group_cputime structure. |
Frank Mayhar | bb34d92 | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 233 | * |
| 234 | * If thread group time is being maintained, get the structure for the |
| 235 | * running CPU and update the stime field there. |
| 236 | */ |
Frank Mayhar | 7086efe | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 237 | static inline void account_group_system_time(struct task_struct *tsk, |
| 238 | cputime_t cputime) |
Frank Mayhar | bb34d92 | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 239 | { |
Oleg Nesterov | 48286d5 | 2010-06-11 01:09:52 +0200 | [diff] [blame] | 240 | struct thread_group_cputimer *cputimer = &tsk->signal->cputimer; |
Frank Mayhar | bb34d92 | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 241 | |
KOSAKI Motohiro | fa18f7b | 2013-05-26 17:35:41 -0400 | [diff] [blame] | 242 | if (!cputimer_running(tsk)) |
Peter Zijlstra | 4cd4c1b | 2009-02-05 12:24:16 +0100 | [diff] [blame] | 243 | return; |
| 244 | |
Jason Low | 7110744 | 2015-04-28 13:00:24 -0700 | [diff] [blame] | 245 | atomic64_add(cputime, &cputimer->cputime_atomic.stime); |
Frank Mayhar | bb34d92 | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | /** |
Frank Mayhar | 7086efe | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 249 | * account_group_exec_runtime - Maintain exec runtime for a thread group. |
Frank Mayhar | bb34d92 | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 250 | * |
Frank Mayhar | 7086efe | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 251 | * @tsk: Pointer to task structure. |
Frank Mayhar | bb34d92 | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 252 | * @ns: Time value by which to increment the sum_exec_runtime field |
Frank Mayhar | 7086efe | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 253 | * of the thread_group_cputime structure. |
Frank Mayhar | bb34d92 | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 254 | * |
| 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 Mayhar | 7086efe | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 258 | static inline void account_group_exec_runtime(struct task_struct *tsk, |
| 259 | unsigned long long ns) |
Frank Mayhar | bb34d92 | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 260 | { |
Oleg Nesterov | 48286d5 | 2010-06-11 01:09:52 +0200 | [diff] [blame] | 261 | struct thread_group_cputimer *cputimer = &tsk->signal->cputimer; |
Frank Mayhar | bb34d92 | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 262 | |
KOSAKI Motohiro | fa18f7b | 2013-05-26 17:35:41 -0400 | [diff] [blame] | 263 | if (!cputimer_running(tsk)) |
Peter Zijlstra | 4cd4c1b | 2009-02-05 12:24:16 +0100 | [diff] [blame] | 264 | return; |
| 265 | |
Jason Low | 7110744 | 2015-04-28 13:00:24 -0700 | [diff] [blame] | 266 | atomic64_add(ns, &cputimer->cputime_atomic.sum_exec_runtime); |
Frank Mayhar | bb34d92 | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 267 | } |