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