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