Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 1 | #include <linux/export.h> |
| 2 | #include <linux/sched.h> |
| 3 | #include <linux/tsacct_kern.h> |
| 4 | #include <linux/kernel_stat.h> |
| 5 | #include <linux/static_key.h> |
Frederic Weisbecker | abf917c | 2012-07-25 07:56:04 +0200 | [diff] [blame] | 6 | #include <linux/context_tracking.h> |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 7 | #include "sched.h" |
Stefano Stabellini | 1fe7c4e | 2015-11-10 12:36:46 +0000 | [diff] [blame] | 8 | #ifdef CONFIG_PARAVIRT |
| 9 | #include <asm/paravirt.h> |
| 10 | #endif |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 11 | |
| 12 | |
| 13 | #ifdef CONFIG_IRQ_TIME_ACCOUNTING |
| 14 | |
| 15 | /* |
| 16 | * There are no locks covering percpu hardirq/softirq time. |
Frederic Weisbecker | bf9fae9 | 2012-09-08 15:23:11 +0200 | [diff] [blame] | 17 | * They are only modified in vtime_account, on corresponding CPU |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 18 | * with interrupts disabled. So, writes are safe. |
| 19 | * They are read and saved off onto struct rq in update_rq_clock(). |
| 20 | * This may result in other CPU reading this CPU's irq time and can |
Frederic Weisbecker | bf9fae9 | 2012-09-08 15:23:11 +0200 | [diff] [blame] | 21 | * race with irq/vtime_account on this CPU. We would either get old |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 22 | * or new value with a side effect of accounting a slice of irq time to wrong |
| 23 | * task when irq is in progress while we read rq->clock. That is a worthy |
| 24 | * compromise in place of having locks on each irq in account_system_time. |
| 25 | */ |
| 26 | DEFINE_PER_CPU(u64, cpu_hardirq_time); |
| 27 | DEFINE_PER_CPU(u64, cpu_softirq_time); |
| 28 | |
| 29 | static DEFINE_PER_CPU(u64, irq_start_time); |
| 30 | static int sched_clock_irqtime; |
| 31 | |
| 32 | void enable_sched_clock_irqtime(void) |
| 33 | { |
| 34 | sched_clock_irqtime = 1; |
| 35 | } |
| 36 | |
| 37 | void disable_sched_clock_irqtime(void) |
| 38 | { |
| 39 | sched_clock_irqtime = 0; |
| 40 | } |
| 41 | |
| 42 | #ifndef CONFIG_64BIT |
| 43 | DEFINE_PER_CPU(seqcount_t, irq_time_seq); |
| 44 | #endif /* CONFIG_64BIT */ |
| 45 | |
| 46 | /* |
| 47 | * Called before incrementing preempt_count on {soft,}irq_enter |
| 48 | * and before decrementing preempt_count on {soft,}irq_exit. |
| 49 | */ |
Frederic Weisbecker | 3e1df4f5 | 2012-10-06 05:23:22 +0200 | [diff] [blame] | 50 | void irqtime_account_irq(struct task_struct *curr) |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 51 | { |
| 52 | unsigned long flags; |
| 53 | s64 delta; |
| 54 | int cpu; |
| 55 | |
| 56 | if (!sched_clock_irqtime) |
| 57 | return; |
| 58 | |
| 59 | local_irq_save(flags); |
| 60 | |
| 61 | cpu = smp_processor_id(); |
| 62 | delta = sched_clock_cpu(cpu) - __this_cpu_read(irq_start_time); |
| 63 | __this_cpu_add(irq_start_time, delta); |
| 64 | |
| 65 | irq_time_write_begin(); |
| 66 | /* |
| 67 | * We do not account for softirq time from ksoftirqd here. |
| 68 | * We want to continue accounting softirq time to ksoftirqd thread |
| 69 | * in that case, so as not to confuse scheduler with a special task |
| 70 | * that do not consume any time, but still wants to run. |
| 71 | */ |
| 72 | if (hardirq_count()) |
| 73 | __this_cpu_add(cpu_hardirq_time, delta); |
| 74 | else if (in_serving_softirq() && curr != this_cpu_ksoftirqd()) |
| 75 | __this_cpu_add(cpu_softirq_time, delta); |
| 76 | |
| 77 | irq_time_write_end(); |
| 78 | local_irq_restore(flags); |
| 79 | } |
Frederic Weisbecker | 3e1df4f5 | 2012-10-06 05:23:22 +0200 | [diff] [blame] | 80 | EXPORT_SYMBOL_GPL(irqtime_account_irq); |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 81 | |
Rik van Riel | 5743021 | 2016-07-13 16:50:01 +0200 | [diff] [blame] | 82 | static cputime_t irqtime_account_hi_update(cputime_t maxtime) |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 83 | { |
| 84 | u64 *cpustat = kcpustat_this_cpu->cpustat; |
| 85 | unsigned long flags; |
Rik van Riel | 5743021 | 2016-07-13 16:50:01 +0200 | [diff] [blame] | 86 | cputime_t irq_cputime; |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 87 | |
| 88 | local_irq_save(flags); |
Rik van Riel | 5743021 | 2016-07-13 16:50:01 +0200 | [diff] [blame] | 89 | irq_cputime = nsecs_to_cputime64(this_cpu_read(cpu_hardirq_time)) - |
| 90 | cpustat[CPUTIME_IRQ]; |
| 91 | irq_cputime = min(irq_cputime, maxtime); |
| 92 | cpustat[CPUTIME_IRQ] += irq_cputime; |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 93 | local_irq_restore(flags); |
Rik van Riel | 5743021 | 2016-07-13 16:50:01 +0200 | [diff] [blame] | 94 | return irq_cputime; |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 95 | } |
| 96 | |
Rik van Riel | 5743021 | 2016-07-13 16:50:01 +0200 | [diff] [blame] | 97 | static cputime_t irqtime_account_si_update(cputime_t maxtime) |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 98 | { |
| 99 | u64 *cpustat = kcpustat_this_cpu->cpustat; |
| 100 | unsigned long flags; |
Rik van Riel | 5743021 | 2016-07-13 16:50:01 +0200 | [diff] [blame] | 101 | cputime_t softirq_cputime; |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 102 | |
| 103 | local_irq_save(flags); |
Rik van Riel | 5743021 | 2016-07-13 16:50:01 +0200 | [diff] [blame] | 104 | softirq_cputime = nsecs_to_cputime64(this_cpu_read(cpu_softirq_time)) - |
| 105 | cpustat[CPUTIME_SOFTIRQ]; |
| 106 | softirq_cputime = min(softirq_cputime, maxtime); |
| 107 | cpustat[CPUTIME_SOFTIRQ] += softirq_cputime; |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 108 | local_irq_restore(flags); |
Rik van Riel | 5743021 | 2016-07-13 16:50:01 +0200 | [diff] [blame] | 109 | return softirq_cputime; |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | #else /* CONFIG_IRQ_TIME_ACCOUNTING */ |
| 113 | |
| 114 | #define sched_clock_irqtime (0) |
| 115 | |
Rik van Riel | 5743021 | 2016-07-13 16:50:01 +0200 | [diff] [blame] | 116 | static cputime_t irqtime_account_hi_update(cputime_t dummy) |
| 117 | { |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | static cputime_t irqtime_account_si_update(cputime_t dummy) |
| 122 | { |
| 123 | return 0; |
| 124 | } |
| 125 | |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 126 | #endif /* !CONFIG_IRQ_TIME_ACCOUNTING */ |
| 127 | |
| 128 | static inline void task_group_account_field(struct task_struct *p, int index, |
| 129 | u64 tmp) |
| 130 | { |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 131 | /* |
| 132 | * Since all updates are sure to touch the root cgroup, we |
| 133 | * get ourselves ahead and touch it first. If the root cgroup |
| 134 | * is the only cgroup, then nothing else should be necessary. |
| 135 | * |
| 136 | */ |
Christoph Lameter | a4f61cc | 2013-08-07 15:38:24 +0000 | [diff] [blame] | 137 | __this_cpu_add(kernel_cpustat.cpustat[index], tmp); |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 138 | |
Li Zefan | 1966aaf | 2013-03-29 14:37:06 +0800 | [diff] [blame] | 139 | cpuacct_account_field(p, index, tmp); |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | /* |
| 143 | * Account user cpu time to a process. |
| 144 | * @p: the process that the cpu time gets accounted to |
| 145 | * @cputime: the cpu time spent in user space since the last update |
| 146 | * @cputime_scaled: cputime scaled by cpu frequency |
| 147 | */ |
| 148 | void account_user_time(struct task_struct *p, cputime_t cputime, |
| 149 | cputime_t cputime_scaled) |
| 150 | { |
| 151 | int index; |
| 152 | |
| 153 | /* Add user time to process. */ |
| 154 | p->utime += cputime; |
| 155 | p->utimescaled += cputime_scaled; |
| 156 | account_group_user_time(p, cputime); |
| 157 | |
Dongsheng Yang | d0ea026 | 2014-01-27 22:00:45 -0500 | [diff] [blame] | 158 | index = (task_nice(p) > 0) ? CPUTIME_NICE : CPUTIME_USER; |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 159 | |
| 160 | /* Add user time to cpustat. */ |
| 161 | task_group_account_field(p, index, (__force u64) cputime); |
| 162 | |
| 163 | /* Account for user time used */ |
Frederic Weisbecker | 6fac482 | 2012-11-13 14:20:55 +0100 | [diff] [blame] | 164 | acct_account_cputime(p); |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | /* |
| 168 | * Account guest cpu time to a process. |
| 169 | * @p: the process that the cpu time gets accounted to |
| 170 | * @cputime: the cpu time spent in virtual machine since the last update |
| 171 | * @cputime_scaled: cputime scaled by cpu frequency |
| 172 | */ |
| 173 | static void account_guest_time(struct task_struct *p, cputime_t cputime, |
| 174 | cputime_t cputime_scaled) |
| 175 | { |
| 176 | u64 *cpustat = kcpustat_this_cpu->cpustat; |
| 177 | |
| 178 | /* Add guest time to process. */ |
| 179 | p->utime += cputime; |
| 180 | p->utimescaled += cputime_scaled; |
| 181 | account_group_user_time(p, cputime); |
| 182 | p->gtime += cputime; |
| 183 | |
| 184 | /* Add guest time to cpustat. */ |
Dongsheng Yang | d0ea026 | 2014-01-27 22:00:45 -0500 | [diff] [blame] | 185 | if (task_nice(p) > 0) { |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 186 | cpustat[CPUTIME_NICE] += (__force u64) cputime; |
| 187 | cpustat[CPUTIME_GUEST_NICE] += (__force u64) cputime; |
| 188 | } else { |
| 189 | cpustat[CPUTIME_USER] += (__force u64) cputime; |
| 190 | cpustat[CPUTIME_GUEST] += (__force u64) cputime; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | /* |
| 195 | * Account system cpu time to a process and desired cpustat field |
| 196 | * @p: the process that the cpu time gets accounted to |
| 197 | * @cputime: the cpu time spent in kernel space since the last update |
| 198 | * @cputime_scaled: cputime scaled by cpu frequency |
| 199 | * @target_cputime64: pointer to cpustat field that has to be updated |
| 200 | */ |
| 201 | static inline |
| 202 | void __account_system_time(struct task_struct *p, cputime_t cputime, |
| 203 | cputime_t cputime_scaled, int index) |
| 204 | { |
| 205 | /* Add system time to process. */ |
| 206 | p->stime += cputime; |
| 207 | p->stimescaled += cputime_scaled; |
| 208 | account_group_system_time(p, cputime); |
| 209 | |
| 210 | /* Add system time to cpustat. */ |
| 211 | task_group_account_field(p, index, (__force u64) cputime); |
| 212 | |
| 213 | /* Account for system time used */ |
Frederic Weisbecker | 6fac482 | 2012-11-13 14:20:55 +0100 | [diff] [blame] | 214 | acct_account_cputime(p); |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | /* |
| 218 | * Account system cpu time to a process. |
| 219 | * @p: the process that the cpu time gets accounted to |
| 220 | * @hardirq_offset: the offset to subtract from hardirq_count() |
| 221 | * @cputime: the cpu time spent in kernel space since the last update |
| 222 | * @cputime_scaled: cputime scaled by cpu frequency |
| 223 | */ |
| 224 | void account_system_time(struct task_struct *p, int hardirq_offset, |
| 225 | cputime_t cputime, cputime_t cputime_scaled) |
| 226 | { |
| 227 | int index; |
| 228 | |
| 229 | if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0)) { |
| 230 | account_guest_time(p, cputime, cputime_scaled); |
| 231 | return; |
| 232 | } |
| 233 | |
| 234 | if (hardirq_count() - hardirq_offset) |
| 235 | index = CPUTIME_IRQ; |
| 236 | else if (in_serving_softirq()) |
| 237 | index = CPUTIME_SOFTIRQ; |
| 238 | else |
| 239 | index = CPUTIME_SYSTEM; |
| 240 | |
| 241 | __account_system_time(p, cputime, cputime_scaled, index); |
| 242 | } |
| 243 | |
| 244 | /* |
| 245 | * Account for involuntary wait time. |
| 246 | * @cputime: the cpu time spent in involuntary wait |
| 247 | */ |
| 248 | void account_steal_time(cputime_t cputime) |
| 249 | { |
| 250 | u64 *cpustat = kcpustat_this_cpu->cpustat; |
| 251 | |
| 252 | cpustat[CPUTIME_STEAL] += (__force u64) cputime; |
| 253 | } |
| 254 | |
| 255 | /* |
| 256 | * Account for idle time. |
| 257 | * @cputime: the cpu time spent in idle wait |
| 258 | */ |
| 259 | void account_idle_time(cputime_t cputime) |
| 260 | { |
| 261 | u64 *cpustat = kcpustat_this_cpu->cpustat; |
| 262 | struct rq *rq = this_rq(); |
| 263 | |
| 264 | if (atomic_read(&rq->nr_iowait) > 0) |
| 265 | cpustat[CPUTIME_IOWAIT] += (__force u64) cputime; |
| 266 | else |
| 267 | cpustat[CPUTIME_IDLE] += (__force u64) cputime; |
| 268 | } |
| 269 | |
Rik van Riel | 5743021 | 2016-07-13 16:50:01 +0200 | [diff] [blame] | 270 | static __always_inline cputime_t steal_account_process_time(cputime_t maxtime) |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 271 | { |
| 272 | #ifdef CONFIG_PARAVIRT |
| 273 | if (static_key_false(¶virt_steal_enabled)) { |
Rik van Riel | 5743021 | 2016-07-13 16:50:01 +0200 | [diff] [blame] | 274 | cputime_t steal_cputime; |
Frederic Weisbecker | dee08a7 | 2014-03-05 17:02:22 +0100 | [diff] [blame] | 275 | u64 steal; |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 276 | |
| 277 | steal = paravirt_steal_clock(smp_processor_id()); |
| 278 | steal -= this_rq()->prev_steal_time; |
| 279 | |
Rik van Riel | 5743021 | 2016-07-13 16:50:01 +0200 | [diff] [blame] | 280 | steal_cputime = min(nsecs_to_cputime(steal), maxtime); |
| 281 | account_steal_time(steal_cputime); |
| 282 | this_rq()->prev_steal_time += cputime_to_nsecs(steal_cputime); |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 283 | |
Rik van Riel | 5743021 | 2016-07-13 16:50:01 +0200 | [diff] [blame] | 284 | return steal_cputime; |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 285 | } |
| 286 | #endif |
Wanpeng Li | 807e5b8 | 2016-06-13 18:32:46 +0800 | [diff] [blame] | 287 | return 0; |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 288 | } |
| 289 | |
Frederic Weisbecker | a634f93 | 2012-11-21 15:55:59 +0100 | [diff] [blame] | 290 | /* |
Rik van Riel | 5743021 | 2016-07-13 16:50:01 +0200 | [diff] [blame] | 291 | * Account how much elapsed time was spent in steal, irq, or softirq time. |
| 292 | */ |
| 293 | static inline cputime_t account_other_time(cputime_t max) |
| 294 | { |
| 295 | cputime_t accounted; |
| 296 | |
| 297 | accounted = steal_account_process_time(max); |
| 298 | |
| 299 | if (accounted < max) |
| 300 | accounted += irqtime_account_hi_update(max - accounted); |
| 301 | |
| 302 | if (accounted < max) |
| 303 | accounted += irqtime_account_si_update(max - accounted); |
| 304 | |
| 305 | return accounted; |
| 306 | } |
| 307 | |
| 308 | /* |
Frederic Weisbecker | a634f93 | 2012-11-21 15:55:59 +0100 | [diff] [blame] | 309 | * Accumulate raw cputime values of dead tasks (sig->[us]time) and live |
| 310 | * tasks (sum on group iteration) belonging to @tsk's group. |
| 311 | */ |
| 312 | void thread_group_cputime(struct task_struct *tsk, struct task_cputime *times) |
| 313 | { |
| 314 | struct signal_struct *sig = tsk->signal; |
Frederic Weisbecker | 6fac482 | 2012-11-13 14:20:55 +0100 | [diff] [blame] | 315 | cputime_t utime, stime; |
Frederic Weisbecker | a634f93 | 2012-11-21 15:55:59 +0100 | [diff] [blame] | 316 | struct task_struct *t; |
Rik van Riel | e78c349 | 2014-08-16 13:40:10 -0400 | [diff] [blame] | 317 | unsigned int seq, nextseq; |
Rik van Riel | 9c368b5 | 2014-09-12 09:12:15 -0400 | [diff] [blame] | 318 | unsigned long flags; |
Frederic Weisbecker | a634f93 | 2012-11-21 15:55:59 +0100 | [diff] [blame] | 319 | |
| 320 | rcu_read_lock(); |
Rik van Riel | e78c349 | 2014-08-16 13:40:10 -0400 | [diff] [blame] | 321 | /* Attempt a lockless read on the first round. */ |
| 322 | nextseq = 0; |
| 323 | do { |
| 324 | seq = nextseq; |
Rik van Riel | 9c368b5 | 2014-09-12 09:12:15 -0400 | [diff] [blame] | 325 | flags = read_seqbegin_or_lock_irqsave(&sig->stats_lock, &seq); |
Rik van Riel | e78c349 | 2014-08-16 13:40:10 -0400 | [diff] [blame] | 326 | times->utime = sig->utime; |
| 327 | times->stime = sig->stime; |
| 328 | times->sum_exec_runtime = sig->sum_sched_runtime; |
| 329 | |
| 330 | for_each_thread(tsk, t) { |
| 331 | task_cputime(t, &utime, &stime); |
| 332 | times->utime += utime; |
| 333 | times->stime += stime; |
| 334 | times->sum_exec_runtime += task_sched_runtime(t); |
| 335 | } |
| 336 | /* If lockless access failed, take the lock. */ |
| 337 | nextseq = 1; |
| 338 | } while (need_seqretry(&sig->stats_lock, seq)); |
Rik van Riel | 9c368b5 | 2014-09-12 09:12:15 -0400 | [diff] [blame] | 339 | done_seqretry_irqrestore(&sig->stats_lock, seq, flags); |
Frederic Weisbecker | a634f93 | 2012-11-21 15:55:59 +0100 | [diff] [blame] | 340 | rcu_read_unlock(); |
| 341 | } |
| 342 | |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 343 | #ifdef CONFIG_IRQ_TIME_ACCOUNTING |
| 344 | /* |
| 345 | * Account a tick to a process and cpustat |
| 346 | * @p: the process that the cpu time gets accounted to |
| 347 | * @user_tick: is the tick from userspace |
| 348 | * @rq: the pointer to rq |
| 349 | * |
| 350 | * Tick demultiplexing follows the order |
| 351 | * - pending hardirq update |
| 352 | * - pending softirq update |
| 353 | * - user_time |
| 354 | * - idle_time |
| 355 | * - system time |
| 356 | * - check for guest_time |
| 357 | * - else account as system_time |
| 358 | * |
| 359 | * Check for hardirq is done both for system and user time as there is |
| 360 | * no timer going off while we are on hardirq and hence we may never get an |
| 361 | * opportunity to update it solely in system time. |
| 362 | * p->stime and friends are only updated on system time and not on irq |
| 363 | * softirq as those do not count in task exec_runtime any more. |
| 364 | */ |
| 365 | static void irqtime_account_process_tick(struct task_struct *p, int user_tick, |
Thomas Gleixner | 2d51386 | 2014-05-02 23:26:24 +0200 | [diff] [blame] | 366 | struct rq *rq, int ticks) |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 367 | { |
Rik van Riel | 5743021 | 2016-07-13 16:50:01 +0200 | [diff] [blame] | 368 | u64 cputime = (__force u64) cputime_one_jiffy * ticks; |
| 369 | cputime_t scaled, other; |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 370 | |
Rik van Riel | 5743021 | 2016-07-13 16:50:01 +0200 | [diff] [blame] | 371 | /* |
| 372 | * When returning from idle, many ticks can get accounted at |
| 373 | * once, including some ticks of steal, irq, and softirq time. |
| 374 | * Subtract those ticks from the amount of time accounted to |
| 375 | * idle, or potentially user or system time. Due to rounding, |
| 376 | * other time can exceed ticks occasionally. |
| 377 | */ |
| 378 | other = account_other_time(cputime); |
| 379 | if (other >= cputime) |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 380 | return; |
Rik van Riel | 5743021 | 2016-07-13 16:50:01 +0200 | [diff] [blame] | 381 | cputime -= other; |
| 382 | scaled = cputime_to_scaled(cputime); |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 383 | |
Rik van Riel | 5743021 | 2016-07-13 16:50:01 +0200 | [diff] [blame] | 384 | if (this_cpu_ksoftirqd() == p) { |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 385 | /* |
| 386 | * ksoftirqd time do not get accounted in cpu_softirq_time. |
| 387 | * So, we have to handle it separately here. |
| 388 | * Also, p->stime needs to be updated for ksoftirqd. |
| 389 | */ |
Thomas Gleixner | 2d51386 | 2014-05-02 23:26:24 +0200 | [diff] [blame] | 390 | __account_system_time(p, cputime, scaled, CPUTIME_SOFTIRQ); |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 391 | } else if (user_tick) { |
Thomas Gleixner | 2d51386 | 2014-05-02 23:26:24 +0200 | [diff] [blame] | 392 | account_user_time(p, cputime, scaled); |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 393 | } else if (p == rq->idle) { |
Thomas Gleixner | 2d51386 | 2014-05-02 23:26:24 +0200 | [diff] [blame] | 394 | account_idle_time(cputime); |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 395 | } else if (p->flags & PF_VCPU) { /* System time or guest time */ |
Thomas Gleixner | 2d51386 | 2014-05-02 23:26:24 +0200 | [diff] [blame] | 396 | account_guest_time(p, cputime, scaled); |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 397 | } else { |
Thomas Gleixner | 2d51386 | 2014-05-02 23:26:24 +0200 | [diff] [blame] | 398 | __account_system_time(p, cputime, scaled, CPUTIME_SYSTEM); |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 399 | } |
| 400 | } |
| 401 | |
| 402 | static void irqtime_account_idle_ticks(int ticks) |
| 403 | { |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 404 | struct rq *rq = this_rq(); |
| 405 | |
Thomas Gleixner | 2d51386 | 2014-05-02 23:26:24 +0200 | [diff] [blame] | 406 | irqtime_account_process_tick(current, 0, rq, ticks); |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 407 | } |
| 408 | #else /* CONFIG_IRQ_TIME_ACCOUNTING */ |
Frederic Weisbecker | 3f4724e | 2012-07-16 18:00:34 +0200 | [diff] [blame] | 409 | static inline void irqtime_account_idle_ticks(int ticks) {} |
| 410 | static inline void irqtime_account_process_tick(struct task_struct *p, int user_tick, |
Thomas Gleixner | 2d51386 | 2014-05-02 23:26:24 +0200 | [diff] [blame] | 411 | struct rq *rq, int nr_ticks) {} |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 412 | #endif /* CONFIG_IRQ_TIME_ACCOUNTING */ |
| 413 | |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 414 | /* |
| 415 | * Use precise platform statistics if available: |
| 416 | */ |
| 417 | #ifdef CONFIG_VIRT_CPU_ACCOUNTING |
Frederic Weisbecker | a7e1a9e | 2012-09-08 16:14:02 +0200 | [diff] [blame] | 418 | |
Frederic Weisbecker | e3942ba | 2012-11-14 00:24:25 +0100 | [diff] [blame] | 419 | #ifndef __ARCH_HAS_VTIME_TASK_SWITCH |
Frederic Weisbecker | b049340 | 2013-07-12 03:10:15 +0200 | [diff] [blame] | 420 | void vtime_common_task_switch(struct task_struct *prev) |
Frederic Weisbecker | e3942ba | 2012-11-14 00:24:25 +0100 | [diff] [blame] | 421 | { |
| 422 | if (is_idle_task(prev)) |
| 423 | vtime_account_idle(prev); |
| 424 | else |
| 425 | vtime_account_system(prev); |
| 426 | |
Frederic Weisbecker | abf917c | 2012-07-25 07:56:04 +0200 | [diff] [blame] | 427 | #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE |
Frederic Weisbecker | e3942ba | 2012-11-14 00:24:25 +0100 | [diff] [blame] | 428 | vtime_account_user(prev); |
Frederic Weisbecker | abf917c | 2012-07-25 07:56:04 +0200 | [diff] [blame] | 429 | #endif |
Frederic Weisbecker | e3942ba | 2012-11-14 00:24:25 +0100 | [diff] [blame] | 430 | arch_vtime_task_switch(prev); |
| 431 | } |
| 432 | #endif |
Frederic Weisbecker | 1111333 | 2012-10-24 18:05:51 +0200 | [diff] [blame] | 433 | |
Frederic Weisbecker | a7e1a9e | 2012-09-08 16:14:02 +0200 | [diff] [blame] | 434 | /* |
| 435 | * Archs that account the whole time spent in the idle task |
| 436 | * (outside irq) as idle time can rely on this and just implement |
Frederic Weisbecker | fd25b4c | 2012-11-13 18:21:22 +0100 | [diff] [blame] | 437 | * vtime_account_system() and vtime_account_idle(). Archs that |
Frederic Weisbecker | a7e1a9e | 2012-09-08 16:14:02 +0200 | [diff] [blame] | 438 | * have other meaning of the idle time (s390 only includes the |
| 439 | * time spent by the CPU when it's in low power mode) must override |
| 440 | * vtime_account(). |
| 441 | */ |
| 442 | #ifndef __ARCH_HAS_VTIME_ACCOUNT |
Frederic Weisbecker | b049340 | 2013-07-12 03:10:15 +0200 | [diff] [blame] | 443 | void vtime_common_account_irq_enter(struct task_struct *tsk) |
Frederic Weisbecker | a7e1a9e | 2012-09-08 16:14:02 +0200 | [diff] [blame] | 444 | { |
Frederic Weisbecker | abf917c | 2012-07-25 07:56:04 +0200 | [diff] [blame] | 445 | if (!in_interrupt()) { |
| 446 | /* |
| 447 | * If we interrupted user, context_tracking_in_user() |
| 448 | * is 1 because the context tracking don't hook |
| 449 | * on irq entry/exit. This way we know if |
| 450 | * we need to flush user time on kernel entry. |
| 451 | */ |
| 452 | if (context_tracking_in_user()) { |
| 453 | vtime_account_user(tsk); |
| 454 | return; |
| 455 | } |
| 456 | |
| 457 | if (is_idle_task(tsk)) { |
| 458 | vtime_account_idle(tsk); |
| 459 | return; |
| 460 | } |
| 461 | } |
| 462 | vtime_account_system(tsk); |
Frederic Weisbecker | a7e1a9e | 2012-09-08 16:14:02 +0200 | [diff] [blame] | 463 | } |
Frederic Weisbecker | b049340 | 2013-07-12 03:10:15 +0200 | [diff] [blame] | 464 | EXPORT_SYMBOL_GPL(vtime_common_account_irq_enter); |
Frederic Weisbecker | a7e1a9e | 2012-09-08 16:14:02 +0200 | [diff] [blame] | 465 | #endif /* __ARCH_HAS_VTIME_ACCOUNT */ |
Frederic Weisbecker | 9fbc42e | 2013-02-25 17:25:39 +0100 | [diff] [blame] | 466 | #endif /* CONFIG_VIRT_CPU_ACCOUNTING */ |
Frederic Weisbecker | a7e1a9e | 2012-09-08 16:14:02 +0200 | [diff] [blame] | 467 | |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 468 | |
Frederic Weisbecker | 9fbc42e | 2013-02-25 17:25:39 +0100 | [diff] [blame] | 469 | #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE |
| 470 | void task_cputime_adjusted(struct task_struct *p, cputime_t *ut, cputime_t *st) |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 471 | { |
Frederic Weisbecker | 9fbc42e | 2013-02-25 17:25:39 +0100 | [diff] [blame] | 472 | *ut = p->utime; |
| 473 | *st = p->stime; |
| 474 | } |
Andrey Smetanin | 9eec50b | 2015-09-16 12:29:50 +0300 | [diff] [blame] | 475 | EXPORT_SYMBOL_GPL(task_cputime_adjusted); |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 476 | |
Frederic Weisbecker | 9fbc42e | 2013-02-25 17:25:39 +0100 | [diff] [blame] | 477 | void thread_group_cputime_adjusted(struct task_struct *p, cputime_t *ut, cputime_t *st) |
| 478 | { |
| 479 | struct task_cputime cputime; |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 480 | |
Frederic Weisbecker | 9fbc42e | 2013-02-25 17:25:39 +0100 | [diff] [blame] | 481 | thread_group_cputime(p, &cputime); |
| 482 | |
| 483 | *ut = cputime.utime; |
| 484 | *st = cputime.stime; |
| 485 | } |
| 486 | #else /* !CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */ |
| 487 | /* |
| 488 | * Account a single tick of cpu time. |
| 489 | * @p: the process that the cpu time gets accounted to |
| 490 | * @user_tick: indicates if the tick is a user or a system tick |
| 491 | */ |
| 492 | void account_process_tick(struct task_struct *p, int user_tick) |
| 493 | { |
Rik van Riel | 5743021 | 2016-07-13 16:50:01 +0200 | [diff] [blame] | 494 | cputime_t cputime, scaled, steal; |
Frederic Weisbecker | 9fbc42e | 2013-02-25 17:25:39 +0100 | [diff] [blame] | 495 | struct rq *rq = this_rq(); |
| 496 | |
Frederic Weisbecker | 55dbdcf | 2015-11-19 16:47:32 +0100 | [diff] [blame] | 497 | if (vtime_accounting_cpu_enabled()) |
Frederic Weisbecker | 9fbc42e | 2013-02-25 17:25:39 +0100 | [diff] [blame] | 498 | return; |
| 499 | |
| 500 | if (sched_clock_irqtime) { |
Thomas Gleixner | 2d51386 | 2014-05-02 23:26:24 +0200 | [diff] [blame] | 501 | irqtime_account_process_tick(p, user_tick, rq, 1); |
Frederic Weisbecker | 9fbc42e | 2013-02-25 17:25:39 +0100 | [diff] [blame] | 502 | return; |
| 503 | } |
| 504 | |
Rik van Riel | 5743021 | 2016-07-13 16:50:01 +0200 | [diff] [blame] | 505 | cputime = cputime_one_jiffy; |
| 506 | steal = steal_account_process_time(cputime); |
| 507 | |
| 508 | if (steal >= cputime) |
Frederic Weisbecker | 9fbc42e | 2013-02-25 17:25:39 +0100 | [diff] [blame] | 509 | return; |
| 510 | |
Rik van Riel | 5743021 | 2016-07-13 16:50:01 +0200 | [diff] [blame] | 511 | cputime -= steal; |
| 512 | scaled = cputime_to_scaled(cputime); |
| 513 | |
Frederic Weisbecker | 9fbc42e | 2013-02-25 17:25:39 +0100 | [diff] [blame] | 514 | if (user_tick) |
Rik van Riel | 5743021 | 2016-07-13 16:50:01 +0200 | [diff] [blame] | 515 | account_user_time(p, cputime, scaled); |
Frederic Weisbecker | 9fbc42e | 2013-02-25 17:25:39 +0100 | [diff] [blame] | 516 | else if ((p != rq->idle) || (irq_count() != HARDIRQ_OFFSET)) |
Rik van Riel | 5743021 | 2016-07-13 16:50:01 +0200 | [diff] [blame] | 517 | account_system_time(p, HARDIRQ_OFFSET, cputime, scaled); |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 518 | else |
Rik van Riel | 5743021 | 2016-07-13 16:50:01 +0200 | [diff] [blame] | 519 | account_idle_time(cputime); |
Frederic Weisbecker | 9fbc42e | 2013-02-25 17:25:39 +0100 | [diff] [blame] | 520 | } |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 521 | |
Frederic Weisbecker | 9fbc42e | 2013-02-25 17:25:39 +0100 | [diff] [blame] | 522 | /* |
| 523 | * Account multiple ticks of steal time. |
| 524 | * @p: the process from which the cpu time has been stolen |
| 525 | * @ticks: number of stolen ticks |
| 526 | */ |
| 527 | void account_steal_ticks(unsigned long ticks) |
| 528 | { |
| 529 | account_steal_time(jiffies_to_cputime(ticks)); |
| 530 | } |
| 531 | |
| 532 | /* |
| 533 | * Account multiple ticks of idle time. |
| 534 | * @ticks: number of stolen ticks |
| 535 | */ |
| 536 | void account_idle_ticks(unsigned long ticks) |
| 537 | { |
| 538 | |
| 539 | if (sched_clock_irqtime) { |
| 540 | irqtime_account_idle_ticks(ticks); |
| 541 | return; |
| 542 | } |
| 543 | |
| 544 | account_idle_time(jiffies_to_cputime(ticks)); |
| 545 | } |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 546 | |
Frederic Weisbecker | d9a3c98 | 2013-02-20 18:54:55 +0100 | [diff] [blame] | 547 | /* |
Stanislaw Gruszka | 55eaa7c | 2013-04-30 17:14:42 +0200 | [diff] [blame] | 548 | * Perform (stime * rtime) / total, but avoid multiplication overflow by |
| 549 | * loosing precision when the numbers are big. |
Frederic Weisbecker | d9a3c98 | 2013-02-20 18:54:55 +0100 | [diff] [blame] | 550 | */ |
| 551 | static cputime_t scale_stime(u64 stime, u64 rtime, u64 total) |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 552 | { |
Stanislaw Gruszka | 55eaa7c | 2013-04-30 17:14:42 +0200 | [diff] [blame] | 553 | u64 scaled; |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 554 | |
Stanislaw Gruszka | 55eaa7c | 2013-04-30 17:14:42 +0200 | [diff] [blame] | 555 | for (;;) { |
| 556 | /* Make sure "rtime" is the bigger of stime/rtime */ |
Stanislaw Gruszka | 84f9f3a | 2013-05-02 15:34:33 +0200 | [diff] [blame] | 557 | if (stime > rtime) |
| 558 | swap(rtime, stime); |
Stanislaw Gruszka | 55eaa7c | 2013-04-30 17:14:42 +0200 | [diff] [blame] | 559 | |
| 560 | /* Make sure 'total' fits in 32 bits */ |
| 561 | if (total >> 32) |
| 562 | goto drop_precision; |
| 563 | |
| 564 | /* Does rtime (and thus stime) fit in 32 bits? */ |
| 565 | if (!(rtime >> 32)) |
| 566 | break; |
| 567 | |
| 568 | /* Can we just balance rtime/stime rather than dropping bits? */ |
| 569 | if (stime >> 31) |
| 570 | goto drop_precision; |
| 571 | |
| 572 | /* We can grow stime and shrink rtime and try to make them both fit */ |
| 573 | stime <<= 1; |
| 574 | rtime >>= 1; |
| 575 | continue; |
| 576 | |
| 577 | drop_precision: |
| 578 | /* We drop from rtime, it has more bits than stime */ |
| 579 | rtime >>= 1; |
| 580 | total >>= 1; |
Frederic Weisbecker | d9a3c98 | 2013-02-20 18:54:55 +0100 | [diff] [blame] | 581 | } |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 582 | |
Stanislaw Gruszka | 55eaa7c | 2013-04-30 17:14:42 +0200 | [diff] [blame] | 583 | /* |
| 584 | * Make sure gcc understands that this is a 32x32->64 multiply, |
| 585 | * followed by a 64/32->64 divide. |
| 586 | */ |
| 587 | scaled = div_u64((u64) (u32) stime * (u64) (u32) rtime, (u32)total); |
Frederic Weisbecker | d9a3c98 | 2013-02-20 18:54:55 +0100 | [diff] [blame] | 588 | return (__force cputime_t) scaled; |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 589 | } |
| 590 | |
Frederic Weisbecker | fa09205 | 2012-11-28 17:00:57 +0100 | [diff] [blame] | 591 | /* |
Peter Zijlstra | 9d7fb04 | 2015-06-30 11:30:54 +0200 | [diff] [blame] | 592 | * Adjust tick based cputime random precision against scheduler runtime |
| 593 | * accounting. |
Rik van Riel | 347abad | 2014-09-30 15:59:47 -0400 | [diff] [blame] | 594 | * |
Peter Zijlstra | 9d7fb04 | 2015-06-30 11:30:54 +0200 | [diff] [blame] | 595 | * Tick based cputime accounting depend on random scheduling timeslices of a |
| 596 | * task to be interrupted or not by the timer. Depending on these |
| 597 | * circumstances, the number of these interrupts may be over or |
| 598 | * under-optimistic, matching the real user and system cputime with a variable |
| 599 | * precision. |
| 600 | * |
| 601 | * Fix this by scaling these tick based values against the total runtime |
| 602 | * accounted by the CFS scheduler. |
| 603 | * |
| 604 | * This code provides the following guarantees: |
| 605 | * |
| 606 | * stime + utime == rtime |
| 607 | * stime_i+1 >= stime_i, utime_i+1 >= utime_i |
| 608 | * |
| 609 | * Assuming that rtime_i+1 >= rtime_i. |
Frederic Weisbecker | fa09205 | 2012-11-28 17:00:57 +0100 | [diff] [blame] | 610 | */ |
Frederic Weisbecker | d37f761d | 2012-11-22 00:58:35 +0100 | [diff] [blame] | 611 | static void cputime_adjust(struct task_cputime *curr, |
Peter Zijlstra | 9d7fb04 | 2015-06-30 11:30:54 +0200 | [diff] [blame] | 612 | struct prev_cputime *prev, |
Frederic Weisbecker | d37f761d | 2012-11-22 00:58:35 +0100 | [diff] [blame] | 613 | cputime_t *ut, cputime_t *st) |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 614 | { |
Stanislaw Gruszka | 5a8e01f | 2013-09-04 15:16:03 +0200 | [diff] [blame] | 615 | cputime_t rtime, stime, utime; |
Peter Zijlstra | 9d7fb04 | 2015-06-30 11:30:54 +0200 | [diff] [blame] | 616 | unsigned long flags; |
Frederic Weisbecker | fa09205 | 2012-11-28 17:00:57 +0100 | [diff] [blame] | 617 | |
Peter Zijlstra | 9d7fb04 | 2015-06-30 11:30:54 +0200 | [diff] [blame] | 618 | /* Serialize concurrent callers such that we can honour our guarantees */ |
| 619 | raw_spin_lock_irqsave(&prev->lock, flags); |
Frederic Weisbecker | d37f761d | 2012-11-22 00:58:35 +0100 | [diff] [blame] | 620 | rtime = nsecs_to_cputime(curr->sum_exec_runtime); |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 621 | |
Stanislaw Gruszka | 772c808 | 2013-04-30 11:35:05 +0200 | [diff] [blame] | 622 | /* |
Peter Zijlstra | 9d7fb04 | 2015-06-30 11:30:54 +0200 | [diff] [blame] | 623 | * This is possible under two circumstances: |
| 624 | * - rtime isn't monotonic after all (a bug); |
| 625 | * - we got reordered by the lock. |
| 626 | * |
| 627 | * In both cases this acts as a filter such that the rest of the code |
| 628 | * can assume it is monotonic regardless of anything else. |
Stanislaw Gruszka | 772c808 | 2013-04-30 11:35:05 +0200 | [diff] [blame] | 629 | */ |
| 630 | if (prev->stime + prev->utime >= rtime) |
| 631 | goto out; |
| 632 | |
Stanislaw Gruszka | 5a8e01f | 2013-09-04 15:16:03 +0200 | [diff] [blame] | 633 | stime = curr->stime; |
| 634 | utime = curr->utime; |
| 635 | |
| 636 | if (utime == 0) { |
| 637 | stime = rtime; |
Peter Zijlstra | 9d7fb04 | 2015-06-30 11:30:54 +0200 | [diff] [blame] | 638 | goto update; |
Frederic Weisbecker | d9a3c98 | 2013-02-20 18:54:55 +0100 | [diff] [blame] | 639 | } |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 640 | |
Peter Zijlstra | 9d7fb04 | 2015-06-30 11:30:54 +0200 | [diff] [blame] | 641 | if (stime == 0) { |
| 642 | utime = rtime; |
| 643 | goto update; |
| 644 | } |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 645 | |
Peter Zijlstra | 9d7fb04 | 2015-06-30 11:30:54 +0200 | [diff] [blame] | 646 | stime = scale_stime((__force u64)stime, (__force u64)rtime, |
| 647 | (__force u64)(stime + utime)); |
| 648 | |
| 649 | /* |
| 650 | * Make sure stime doesn't go backwards; this preserves monotonicity |
| 651 | * for utime because rtime is monotonic. |
| 652 | * |
| 653 | * utime_i+1 = rtime_i+1 - stime_i |
| 654 | * = rtime_i+1 - (rtime_i - utime_i) |
| 655 | * = (rtime_i+1 - rtime_i) + utime_i |
| 656 | * >= utime_i |
| 657 | */ |
| 658 | if (stime < prev->stime) |
| 659 | stime = prev->stime; |
| 660 | utime = rtime - stime; |
| 661 | |
| 662 | /* |
| 663 | * Make sure utime doesn't go backwards; this still preserves |
| 664 | * monotonicity for stime, analogous argument to above. |
| 665 | */ |
| 666 | if (utime < prev->utime) { |
| 667 | utime = prev->utime; |
| 668 | stime = rtime - utime; |
| 669 | } |
| 670 | |
| 671 | update: |
| 672 | prev->stime = stime; |
| 673 | prev->utime = utime; |
Stanislaw Gruszka | 772c808 | 2013-04-30 11:35:05 +0200 | [diff] [blame] | 674 | out: |
Frederic Weisbecker | d37f761d | 2012-11-22 00:58:35 +0100 | [diff] [blame] | 675 | *ut = prev->utime; |
| 676 | *st = prev->stime; |
Peter Zijlstra | 9d7fb04 | 2015-06-30 11:30:54 +0200 | [diff] [blame] | 677 | raw_spin_unlock_irqrestore(&prev->lock, flags); |
Frederic Weisbecker | d37f761d | 2012-11-22 00:58:35 +0100 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | void task_cputime_adjusted(struct task_struct *p, cputime_t *ut, cputime_t *st) |
| 681 | { |
| 682 | struct task_cputime cputime = { |
Frederic Weisbecker | d37f761d | 2012-11-22 00:58:35 +0100 | [diff] [blame] | 683 | .sum_exec_runtime = p->se.sum_exec_runtime, |
| 684 | }; |
| 685 | |
Frederic Weisbecker | 6fac482 | 2012-11-13 14:20:55 +0100 | [diff] [blame] | 686 | task_cputime(p, &cputime.utime, &cputime.stime); |
Frederic Weisbecker | d37f761d | 2012-11-22 00:58:35 +0100 | [diff] [blame] | 687 | cputime_adjust(&cputime, &p->prev_cputime, ut, st); |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 688 | } |
Andrey Smetanin | 9eec50b | 2015-09-16 12:29:50 +0300 | [diff] [blame] | 689 | EXPORT_SYMBOL_GPL(task_cputime_adjusted); |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 690 | |
Frederic Weisbecker | e80d0a1 | 2012-11-21 16:26:44 +0100 | [diff] [blame] | 691 | void thread_group_cputime_adjusted(struct task_struct *p, cputime_t *ut, cputime_t *st) |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 692 | { |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 693 | struct task_cputime cputime; |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 694 | |
| 695 | thread_group_cputime(p, &cputime); |
Frederic Weisbecker | d37f761d | 2012-11-22 00:58:35 +0100 | [diff] [blame] | 696 | cputime_adjust(&cputime, &p->signal->prev_cputime, ut, st); |
Frederic Weisbecker | 73fbec6 | 2012-06-16 15:57:37 +0200 | [diff] [blame] | 697 | } |
Frederic Weisbecker | 9fbc42e | 2013-02-25 17:25:39 +0100 | [diff] [blame] | 698 | #endif /* !CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */ |
Frederic Weisbecker | abf917c | 2012-07-25 07:56:04 +0200 | [diff] [blame] | 699 | |
| 700 | #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN |
Rik van Riel | ff9a9b4 | 2016-02-10 20:08:27 -0500 | [diff] [blame] | 701 | static cputime_t vtime_delta(struct task_struct *tsk) |
Frederic Weisbecker | abf917c | 2012-07-25 07:56:04 +0200 | [diff] [blame] | 702 | { |
Rik van Riel | ff9a9b4 | 2016-02-10 20:08:27 -0500 | [diff] [blame] | 703 | unsigned long now = READ_ONCE(jiffies); |
Frederic Weisbecker | abf917c | 2012-07-25 07:56:04 +0200 | [diff] [blame] | 704 | |
Rik van Riel | ff9a9b4 | 2016-02-10 20:08:27 -0500 | [diff] [blame] | 705 | if (time_before(now, (unsigned long)tsk->vtime_snap)) |
Frederic Weisbecker | 6a61671 | 2012-12-16 20:00:34 +0100 | [diff] [blame] | 706 | return 0; |
| 707 | |
Rik van Riel | ff9a9b4 | 2016-02-10 20:08:27 -0500 | [diff] [blame] | 708 | return jiffies_to_cputime(now - tsk->vtime_snap); |
Frederic Weisbecker | 6a61671 | 2012-12-16 20:00:34 +0100 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | static cputime_t get_vtime_delta(struct task_struct *tsk) |
| 712 | { |
Rik van Riel | ff9a9b4 | 2016-02-10 20:08:27 -0500 | [diff] [blame] | 713 | unsigned long now = READ_ONCE(jiffies); |
Rik van Riel | b58c358 | 2016-07-13 16:50:02 +0200 | [diff] [blame^] | 714 | cputime_t delta, other; |
Frederic Weisbecker | 6a61671 | 2012-12-16 20:00:34 +0100 | [diff] [blame] | 715 | |
Rik van Riel | 5743021 | 2016-07-13 16:50:01 +0200 | [diff] [blame] | 716 | delta = jiffies_to_cputime(now - tsk->vtime_snap); |
Rik van Riel | b58c358 | 2016-07-13 16:50:02 +0200 | [diff] [blame^] | 717 | other = account_other_time(delta); |
Frederic Weisbecker | 7098c1e | 2015-11-19 16:47:30 +0100 | [diff] [blame] | 718 | WARN_ON_ONCE(tsk->vtime_snap_whence == VTIME_INACTIVE); |
Rik van Riel | ff9a9b4 | 2016-02-10 20:08:27 -0500 | [diff] [blame] | 719 | tsk->vtime_snap = now; |
Frederic Weisbecker | abf917c | 2012-07-25 07:56:04 +0200 | [diff] [blame] | 720 | |
Rik van Riel | b58c358 | 2016-07-13 16:50:02 +0200 | [diff] [blame^] | 721 | return delta - other; |
Frederic Weisbecker | abf917c | 2012-07-25 07:56:04 +0200 | [diff] [blame] | 722 | } |
| 723 | |
Frederic Weisbecker | 6a61671 | 2012-12-16 20:00:34 +0100 | [diff] [blame] | 724 | static void __vtime_account_system(struct task_struct *tsk) |
| 725 | { |
| 726 | cputime_t delta_cpu = get_vtime_delta(tsk); |
| 727 | |
| 728 | account_system_time(tsk, irq_count(), delta_cpu, cputime_to_scaled(delta_cpu)); |
| 729 | } |
| 730 | |
Frederic Weisbecker | abf917c | 2012-07-25 07:56:04 +0200 | [diff] [blame] | 731 | void vtime_account_system(struct task_struct *tsk) |
| 732 | { |
Rik van Riel | ff9a9b4 | 2016-02-10 20:08:27 -0500 | [diff] [blame] | 733 | if (!vtime_delta(tsk)) |
| 734 | return; |
| 735 | |
Frederic Weisbecker | b7ce227 | 2015-11-19 16:47:34 +0100 | [diff] [blame] | 736 | write_seqcount_begin(&tsk->vtime_seqcount); |
Frederic Weisbecker | 6a61671 | 2012-12-16 20:00:34 +0100 | [diff] [blame] | 737 | __vtime_account_system(tsk); |
Frederic Weisbecker | b7ce227 | 2015-11-19 16:47:34 +0100 | [diff] [blame] | 738 | write_seqcount_end(&tsk->vtime_seqcount); |
Frederic Weisbecker | 6a61671 | 2012-12-16 20:00:34 +0100 | [diff] [blame] | 739 | } |
| 740 | |
Frederic Weisbecker | abf917c | 2012-07-25 07:56:04 +0200 | [diff] [blame] | 741 | void vtime_account_user(struct task_struct *tsk) |
| 742 | { |
Frederic Weisbecker | 3f4724e | 2012-07-16 18:00:34 +0200 | [diff] [blame] | 743 | cputime_t delta_cpu; |
| 744 | |
Frederic Weisbecker | b7ce227 | 2015-11-19 16:47:34 +0100 | [diff] [blame] | 745 | write_seqcount_begin(&tsk->vtime_seqcount); |
Frederic Weisbecker | 6a61671 | 2012-12-16 20:00:34 +0100 | [diff] [blame] | 746 | tsk->vtime_snap_whence = VTIME_SYS; |
Rik van Riel | ff9a9b4 | 2016-02-10 20:08:27 -0500 | [diff] [blame] | 747 | if (vtime_delta(tsk)) { |
| 748 | delta_cpu = get_vtime_delta(tsk); |
| 749 | account_user_time(tsk, delta_cpu, cputime_to_scaled(delta_cpu)); |
| 750 | } |
Frederic Weisbecker | b7ce227 | 2015-11-19 16:47:34 +0100 | [diff] [blame] | 751 | write_seqcount_end(&tsk->vtime_seqcount); |
Frederic Weisbecker | 6a61671 | 2012-12-16 20:00:34 +0100 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | void vtime_user_enter(struct task_struct *tsk) |
| 755 | { |
Frederic Weisbecker | b7ce227 | 2015-11-19 16:47:34 +0100 | [diff] [blame] | 756 | write_seqcount_begin(&tsk->vtime_seqcount); |
Rik van Riel | ff9a9b4 | 2016-02-10 20:08:27 -0500 | [diff] [blame] | 757 | if (vtime_delta(tsk)) |
| 758 | __vtime_account_system(tsk); |
Frederic Weisbecker | af2350b | 2013-07-15 16:35:55 +0200 | [diff] [blame] | 759 | tsk->vtime_snap_whence = VTIME_USER; |
Frederic Weisbecker | b7ce227 | 2015-11-19 16:47:34 +0100 | [diff] [blame] | 760 | write_seqcount_end(&tsk->vtime_seqcount); |
Frederic Weisbecker | 6a61671 | 2012-12-16 20:00:34 +0100 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | void vtime_guest_enter(struct task_struct *tsk) |
| 764 | { |
Frederic Weisbecker | 5b206d4 | 2013-07-12 19:05:14 +0200 | [diff] [blame] | 765 | /* |
| 766 | * The flags must be updated under the lock with |
| 767 | * the vtime_snap flush and update. |
| 768 | * That enforces a right ordering and update sequence |
| 769 | * synchronization against the reader (task_gtime()) |
| 770 | * that can thus safely catch up with a tickless delta. |
| 771 | */ |
Frederic Weisbecker | b7ce227 | 2015-11-19 16:47:34 +0100 | [diff] [blame] | 772 | write_seqcount_begin(&tsk->vtime_seqcount); |
Rik van Riel | ff9a9b4 | 2016-02-10 20:08:27 -0500 | [diff] [blame] | 773 | if (vtime_delta(tsk)) |
| 774 | __vtime_account_system(tsk); |
Frederic Weisbecker | 6a61671 | 2012-12-16 20:00:34 +0100 | [diff] [blame] | 775 | current->flags |= PF_VCPU; |
Frederic Weisbecker | b7ce227 | 2015-11-19 16:47:34 +0100 | [diff] [blame] | 776 | write_seqcount_end(&tsk->vtime_seqcount); |
Frederic Weisbecker | 6a61671 | 2012-12-16 20:00:34 +0100 | [diff] [blame] | 777 | } |
Frederic Weisbecker | 48d6a81 | 2013-07-10 02:44:35 +0200 | [diff] [blame] | 778 | EXPORT_SYMBOL_GPL(vtime_guest_enter); |
Frederic Weisbecker | 6a61671 | 2012-12-16 20:00:34 +0100 | [diff] [blame] | 779 | |
| 780 | void vtime_guest_exit(struct task_struct *tsk) |
| 781 | { |
Frederic Weisbecker | b7ce227 | 2015-11-19 16:47:34 +0100 | [diff] [blame] | 782 | write_seqcount_begin(&tsk->vtime_seqcount); |
Frederic Weisbecker | 6a61671 | 2012-12-16 20:00:34 +0100 | [diff] [blame] | 783 | __vtime_account_system(tsk); |
| 784 | current->flags &= ~PF_VCPU; |
Frederic Weisbecker | b7ce227 | 2015-11-19 16:47:34 +0100 | [diff] [blame] | 785 | write_seqcount_end(&tsk->vtime_seqcount); |
Frederic Weisbecker | abf917c | 2012-07-25 07:56:04 +0200 | [diff] [blame] | 786 | } |
Frederic Weisbecker | 48d6a81 | 2013-07-10 02:44:35 +0200 | [diff] [blame] | 787 | EXPORT_SYMBOL_GPL(vtime_guest_exit); |
Frederic Weisbecker | abf917c | 2012-07-25 07:56:04 +0200 | [diff] [blame] | 788 | |
| 789 | void vtime_account_idle(struct task_struct *tsk) |
| 790 | { |
Frederic Weisbecker | 6a61671 | 2012-12-16 20:00:34 +0100 | [diff] [blame] | 791 | cputime_t delta_cpu = get_vtime_delta(tsk); |
Frederic Weisbecker | abf917c | 2012-07-25 07:56:04 +0200 | [diff] [blame] | 792 | |
| 793 | account_idle_time(delta_cpu); |
| 794 | } |
Frederic Weisbecker | 3f4724e | 2012-07-16 18:00:34 +0200 | [diff] [blame] | 795 | |
Frederic Weisbecker | 6a61671 | 2012-12-16 20:00:34 +0100 | [diff] [blame] | 796 | void arch_vtime_task_switch(struct task_struct *prev) |
| 797 | { |
Frederic Weisbecker | b7ce227 | 2015-11-19 16:47:34 +0100 | [diff] [blame] | 798 | write_seqcount_begin(&prev->vtime_seqcount); |
Frederic Weisbecker | 7098c1e | 2015-11-19 16:47:30 +0100 | [diff] [blame] | 799 | prev->vtime_snap_whence = VTIME_INACTIVE; |
Frederic Weisbecker | b7ce227 | 2015-11-19 16:47:34 +0100 | [diff] [blame] | 800 | write_seqcount_end(&prev->vtime_seqcount); |
Frederic Weisbecker | 6a61671 | 2012-12-16 20:00:34 +0100 | [diff] [blame] | 801 | |
Frederic Weisbecker | b7ce227 | 2015-11-19 16:47:34 +0100 | [diff] [blame] | 802 | write_seqcount_begin(¤t->vtime_seqcount); |
Frederic Weisbecker | 6a61671 | 2012-12-16 20:00:34 +0100 | [diff] [blame] | 803 | current->vtime_snap_whence = VTIME_SYS; |
Rik van Riel | ff9a9b4 | 2016-02-10 20:08:27 -0500 | [diff] [blame] | 804 | current->vtime_snap = jiffies; |
Frederic Weisbecker | b7ce227 | 2015-11-19 16:47:34 +0100 | [diff] [blame] | 805 | write_seqcount_end(¤t->vtime_seqcount); |
Frederic Weisbecker | 6a61671 | 2012-12-16 20:00:34 +0100 | [diff] [blame] | 806 | } |
| 807 | |
Frederic Weisbecker | 45eacc6 | 2013-05-15 22:16:32 +0200 | [diff] [blame] | 808 | void vtime_init_idle(struct task_struct *t, int cpu) |
Frederic Weisbecker | 6a61671 | 2012-12-16 20:00:34 +0100 | [diff] [blame] | 809 | { |
| 810 | unsigned long flags; |
| 811 | |
Frederic Weisbecker | b7ce227 | 2015-11-19 16:47:34 +0100 | [diff] [blame] | 812 | local_irq_save(flags); |
| 813 | write_seqcount_begin(&t->vtime_seqcount); |
Frederic Weisbecker | 6a61671 | 2012-12-16 20:00:34 +0100 | [diff] [blame] | 814 | t->vtime_snap_whence = VTIME_SYS; |
Rik van Riel | ff9a9b4 | 2016-02-10 20:08:27 -0500 | [diff] [blame] | 815 | t->vtime_snap = jiffies; |
Frederic Weisbecker | b7ce227 | 2015-11-19 16:47:34 +0100 | [diff] [blame] | 816 | write_seqcount_end(&t->vtime_seqcount); |
| 817 | local_irq_restore(flags); |
Frederic Weisbecker | 6a61671 | 2012-12-16 20:00:34 +0100 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | cputime_t task_gtime(struct task_struct *t) |
| 821 | { |
Frederic Weisbecker | 6a61671 | 2012-12-16 20:00:34 +0100 | [diff] [blame] | 822 | unsigned int seq; |
| 823 | cputime_t gtime; |
| 824 | |
Frederic Weisbecker | e592539 | 2015-11-19 16:47:33 +0100 | [diff] [blame] | 825 | if (!vtime_accounting_enabled()) |
Hiroshi Shimamoto | 2541117 | 2015-11-19 16:47:28 +0100 | [diff] [blame] | 826 | return t->gtime; |
| 827 | |
Frederic Weisbecker | 6a61671 | 2012-12-16 20:00:34 +0100 | [diff] [blame] | 828 | do { |
Frederic Weisbecker | b7ce227 | 2015-11-19 16:47:34 +0100 | [diff] [blame] | 829 | seq = read_seqcount_begin(&t->vtime_seqcount); |
Frederic Weisbecker | 6a61671 | 2012-12-16 20:00:34 +0100 | [diff] [blame] | 830 | |
| 831 | gtime = t->gtime; |
Frederic Weisbecker | cab245d | 2015-11-19 16:47:31 +0100 | [diff] [blame] | 832 | if (t->vtime_snap_whence == VTIME_SYS && t->flags & PF_VCPU) |
Frederic Weisbecker | 6a61671 | 2012-12-16 20:00:34 +0100 | [diff] [blame] | 833 | gtime += vtime_delta(t); |
| 834 | |
Frederic Weisbecker | b7ce227 | 2015-11-19 16:47:34 +0100 | [diff] [blame] | 835 | } while (read_seqcount_retry(&t->vtime_seqcount, seq)); |
Frederic Weisbecker | 6a61671 | 2012-12-16 20:00:34 +0100 | [diff] [blame] | 836 | |
| 837 | return gtime; |
| 838 | } |
| 839 | |
| 840 | /* |
| 841 | * Fetch cputime raw values from fields of task_struct and |
| 842 | * add up the pending nohz execution time since the last |
| 843 | * cputime snapshot. |
| 844 | */ |
| 845 | static void |
| 846 | fetch_task_cputime(struct task_struct *t, |
| 847 | cputime_t *u_dst, cputime_t *s_dst, |
| 848 | cputime_t *u_src, cputime_t *s_src, |
| 849 | cputime_t *udelta, cputime_t *sdelta) |
| 850 | { |
Frederic Weisbecker | 6a61671 | 2012-12-16 20:00:34 +0100 | [diff] [blame] | 851 | unsigned int seq; |
| 852 | unsigned long long delta; |
| 853 | |
| 854 | do { |
| 855 | *udelta = 0; |
| 856 | *sdelta = 0; |
| 857 | |
Frederic Weisbecker | b7ce227 | 2015-11-19 16:47:34 +0100 | [diff] [blame] | 858 | seq = read_seqcount_begin(&t->vtime_seqcount); |
Frederic Weisbecker | 6a61671 | 2012-12-16 20:00:34 +0100 | [diff] [blame] | 859 | |
| 860 | if (u_dst) |
| 861 | *u_dst = *u_src; |
| 862 | if (s_dst) |
| 863 | *s_dst = *s_src; |
| 864 | |
| 865 | /* Task is sleeping, nothing to add */ |
Frederic Weisbecker | 7098c1e | 2015-11-19 16:47:30 +0100 | [diff] [blame] | 866 | if (t->vtime_snap_whence == VTIME_INACTIVE || |
Frederic Weisbecker | 6a61671 | 2012-12-16 20:00:34 +0100 | [diff] [blame] | 867 | is_idle_task(t)) |
| 868 | continue; |
| 869 | |
| 870 | delta = vtime_delta(t); |
| 871 | |
| 872 | /* |
| 873 | * Task runs either in user or kernel space, add pending nohz time to |
| 874 | * the right place. |
| 875 | */ |
| 876 | if (t->vtime_snap_whence == VTIME_USER || t->flags & PF_VCPU) { |
| 877 | *udelta = delta; |
| 878 | } else { |
| 879 | if (t->vtime_snap_whence == VTIME_SYS) |
| 880 | *sdelta = delta; |
| 881 | } |
Frederic Weisbecker | b7ce227 | 2015-11-19 16:47:34 +0100 | [diff] [blame] | 882 | } while (read_seqcount_retry(&t->vtime_seqcount, seq)); |
Frederic Weisbecker | 6a61671 | 2012-12-16 20:00:34 +0100 | [diff] [blame] | 883 | } |
| 884 | |
| 885 | |
| 886 | void task_cputime(struct task_struct *t, cputime_t *utime, cputime_t *stime) |
| 887 | { |
| 888 | cputime_t udelta, sdelta; |
| 889 | |
Frederic Weisbecker | e592539 | 2015-11-19 16:47:33 +0100 | [diff] [blame] | 890 | if (!vtime_accounting_enabled()) { |
Hiroshi Shimamoto | 7877a0b | 2015-11-19 16:47:29 +0100 | [diff] [blame] | 891 | if (utime) |
| 892 | *utime = t->utime; |
| 893 | if (stime) |
| 894 | *stime = t->stime; |
| 895 | return; |
| 896 | } |
| 897 | |
Frederic Weisbecker | 6a61671 | 2012-12-16 20:00:34 +0100 | [diff] [blame] | 898 | fetch_task_cputime(t, utime, stime, &t->utime, |
| 899 | &t->stime, &udelta, &sdelta); |
| 900 | if (utime) |
| 901 | *utime += udelta; |
| 902 | if (stime) |
| 903 | *stime += sdelta; |
| 904 | } |
| 905 | |
| 906 | void task_cputime_scaled(struct task_struct *t, |
| 907 | cputime_t *utimescaled, cputime_t *stimescaled) |
| 908 | { |
| 909 | cputime_t udelta, sdelta; |
| 910 | |
Frederic Weisbecker | e592539 | 2015-11-19 16:47:33 +0100 | [diff] [blame] | 911 | if (!vtime_accounting_enabled()) { |
Hiroshi Shimamoto | 7877a0b | 2015-11-19 16:47:29 +0100 | [diff] [blame] | 912 | if (utimescaled) |
| 913 | *utimescaled = t->utimescaled; |
| 914 | if (stimescaled) |
| 915 | *stimescaled = t->stimescaled; |
| 916 | return; |
| 917 | } |
| 918 | |
Frederic Weisbecker | 6a61671 | 2012-12-16 20:00:34 +0100 | [diff] [blame] | 919 | fetch_task_cputime(t, utimescaled, stimescaled, |
| 920 | &t->utimescaled, &t->stimescaled, &udelta, &sdelta); |
| 921 | if (utimescaled) |
| 922 | *utimescaled += cputime_to_scaled(udelta); |
| 923 | if (stimescaled) |
| 924 | *stimescaled += cputime_to_scaled(sdelta); |
| 925 | } |
Frederic Weisbecker | abf917c | 2012-07-25 07:56:04 +0200 | [diff] [blame] | 926 | #endif /* CONFIG_VIRT_CPU_ACCOUNTING_GEN */ |