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