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>
6 #include <linux/context_tracking.h>
9 #include <asm/paravirt.h>
13 #ifdef CONFIG_IRQ_TIME_ACCOUNTING
16 * There are no locks covering percpu hardirq/softirq time.
17 * They are only modified in vtime_account, on corresponding CPU
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
21 * race with irq/vtime_account on this CPU. We would either get old
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.
26 DEFINE_PER_CPU(struct irqtime
, cpu_irqtime
);
28 static int sched_clock_irqtime
;
30 void enable_sched_clock_irqtime(void)
32 sched_clock_irqtime
= 1;
35 void disable_sched_clock_irqtime(void)
37 sched_clock_irqtime
= 0;
41 * Called before incrementing preempt_count on {soft,}irq_enter
42 * and before decrementing preempt_count on {soft,}irq_exit.
44 void irqtime_account_irq(struct task_struct
*curr
)
46 struct irqtime
*irqtime
= this_cpu_ptr(&cpu_irqtime
);
50 if (!sched_clock_irqtime
)
53 cpu
= smp_processor_id();
54 delta
= sched_clock_cpu(cpu
) - irqtime
->irq_start_time
;
55 irqtime
->irq_start_time
+= delta
;
57 u64_stats_update_begin(&irqtime
->sync
);
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.
65 irqtime
->hardirq_time
+= delta
;
66 else if (in_serving_softirq() && curr
!= this_cpu_ksoftirqd())
67 irqtime
->softirq_time
+= delta
;
69 u64_stats_update_end(&irqtime
->sync
);
71 EXPORT_SYMBOL_GPL(irqtime_account_irq
);
73 static cputime_t
irqtime_account_hi_update(cputime_t maxtime
)
75 u64
*cpustat
= kcpustat_this_cpu
->cpustat
;
76 cputime_t irq_cputime
;
79 nsecs
= __this_cpu_read(cpu_irqtime
.hardirq_time
);
80 irq_cputime
= nsecs_to_cputime64(nsecs
) - cpustat
[CPUTIME_IRQ
];
81 irq_cputime
= min(irq_cputime
, maxtime
);
82 cpustat
[CPUTIME_IRQ
] += irq_cputime
;
87 static cputime_t
irqtime_account_si_update(cputime_t maxtime
)
89 u64
*cpustat
= kcpustat_this_cpu
->cpustat
;
90 cputime_t softirq_cputime
;
93 nsecs
= __this_cpu_read(cpu_irqtime
.softirq_time
);
94 softirq_cputime
= nsecs_to_cputime64(nsecs
) - cpustat
[CPUTIME_SOFTIRQ
];
95 softirq_cputime
= min(softirq_cputime
, maxtime
);
96 cpustat
[CPUTIME_SOFTIRQ
] += softirq_cputime
;
98 return softirq_cputime
;
101 #else /* CONFIG_IRQ_TIME_ACCOUNTING */
103 #define sched_clock_irqtime (0)
105 static cputime_t
irqtime_account_hi_update(cputime_t dummy
)
110 static cputime_t
irqtime_account_si_update(cputime_t dummy
)
115 #endif /* !CONFIG_IRQ_TIME_ACCOUNTING */
117 static inline void task_group_account_field(struct task_struct
*p
, int index
,
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.
126 __this_cpu_add(kernel_cpustat
.cpustat
[index
], tmp
);
128 cpuacct_account_field(p
, index
, tmp
);
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
137 void account_user_time(struct task_struct
*p
, cputime_t cputime
,
138 cputime_t cputime_scaled
)
142 /* Add user time to process. */
144 p
->utimescaled
+= cputime_scaled
;
145 account_group_user_time(p
, cputime
);
147 index
= (task_nice(p
) > 0) ? CPUTIME_NICE
: CPUTIME_USER
;
149 /* Add user time to cpustat. */
150 task_group_account_field(p
, index
, (__force u64
) cputime
);
152 /* Account for user time used */
153 acct_account_cputime(p
);
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
162 static void account_guest_time(struct task_struct
*p
, cputime_t cputime
,
163 cputime_t cputime_scaled
)
165 u64
*cpustat
= kcpustat_this_cpu
->cpustat
;
167 /* Add guest time to process. */
169 p
->utimescaled
+= cputime_scaled
;
170 account_group_user_time(p
, cputime
);
173 /* Add guest time to cpustat. */
174 if (task_nice(p
) > 0) {
175 cpustat
[CPUTIME_NICE
] += (__force u64
) cputime
;
176 cpustat
[CPUTIME_GUEST_NICE
] += (__force u64
) cputime
;
178 cpustat
[CPUTIME_USER
] += (__force u64
) cputime
;
179 cpustat
[CPUTIME_GUEST
] += (__force u64
) cputime
;
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
191 void __account_system_time(struct task_struct
*p
, cputime_t cputime
,
192 cputime_t cputime_scaled
, int index
)
194 /* Add system time to process. */
196 p
->stimescaled
+= cputime_scaled
;
197 account_group_system_time(p
, cputime
);
199 /* Add system time to cpustat. */
200 task_group_account_field(p
, index
, (__force u64
) cputime
);
202 /* Account for system time used */
203 acct_account_cputime(p
);
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
213 void account_system_time(struct task_struct
*p
, int hardirq_offset
,
214 cputime_t cputime
, cputime_t cputime_scaled
)
218 if ((p
->flags
& PF_VCPU
) && (irq_count() - hardirq_offset
== 0)) {
219 account_guest_time(p
, cputime
, cputime_scaled
);
223 if (hardirq_count() - hardirq_offset
)
225 else if (in_serving_softirq())
226 index
= CPUTIME_SOFTIRQ
;
228 index
= CPUTIME_SYSTEM
;
230 __account_system_time(p
, cputime
, cputime_scaled
, index
);
234 * Account for involuntary wait time.
235 * @cputime: the cpu time spent in involuntary wait
237 void account_steal_time(cputime_t cputime
)
239 u64
*cpustat
= kcpustat_this_cpu
->cpustat
;
241 cpustat
[CPUTIME_STEAL
] += (__force u64
) cputime
;
245 * Account for idle time.
246 * @cputime: the cpu time spent in idle wait
248 void account_idle_time(cputime_t cputime
)
250 u64
*cpustat
= kcpustat_this_cpu
->cpustat
;
251 struct rq
*rq
= this_rq();
253 if (atomic_read(&rq
->nr_iowait
) > 0)
254 cpustat
[CPUTIME_IOWAIT
] += (__force u64
) cputime
;
256 cpustat
[CPUTIME_IDLE
] += (__force u64
) cputime
;
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.
264 static __always_inline cputime_t
steal_account_process_time(cputime_t maxtime
)
266 #ifdef CONFIG_PARAVIRT
267 if (static_key_false(¶virt_steal_enabled
)) {
268 cputime_t steal_cputime
;
271 steal
= paravirt_steal_clock(smp_processor_id());
272 steal
-= this_rq()->prev_steal_time
;
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
);
278 return steal_cputime
;
285 * Account how much elapsed time was spent in steal, irq, or softirq time.
287 static inline cputime_t
account_other_time(cputime_t max
)
291 /* Shall be converted to a lockdep-enabled lightweight check */
292 WARN_ON_ONCE(!irqs_disabled());
294 accounted
= steal_account_process_time(max
);
297 accounted
+= irqtime_account_hi_update(max
- accounted
);
300 accounted
+= irqtime_account_si_update(max
- accounted
);
306 static inline u64
read_sum_exec_runtime(struct task_struct
*t
)
308 return t
->se
.sum_exec_runtime
;
311 static u64
read_sum_exec_runtime(struct task_struct
*t
)
317 rq
= task_rq_lock(t
, &rf
);
318 ns
= t
->se
.sum_exec_runtime
;
319 task_rq_unlock(rq
, t
, &rf
);
326 * Accumulate raw cputime values of dead tasks (sig->[us]time) and live
327 * tasks (sum on group iteration) belonging to @tsk's group.
329 void thread_group_cputime(struct task_struct
*tsk
, struct task_cputime
*times
)
331 struct signal_struct
*sig
= tsk
->signal
;
332 cputime_t utime
, stime
;
333 struct task_struct
*t
;
334 unsigned int seq
, nextseq
;
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.
345 if (same_thread_group(current
, tsk
))
346 (void) task_sched_runtime(current
);
349 /* Attempt a lockless read on the first round. */
353 flags
= read_seqbegin_or_lock_irqsave(&sig
->stats_lock
, &seq
);
354 times
->utime
= sig
->utime
;
355 times
->stime
= sig
->stime
;
356 times
->sum_exec_runtime
= sig
->sum_sched_runtime
;
358 for_each_thread(tsk
, t
) {
359 task_cputime(t
, &utime
, &stime
);
360 times
->utime
+= utime
;
361 times
->stime
+= stime
;
362 times
->sum_exec_runtime
+= read_sum_exec_runtime(t
);
364 /* If lockless access failed, take the lock. */
366 } while (need_seqretry(&sig
->stats_lock
, seq
));
367 done_seqretry_irqrestore(&sig
->stats_lock
, seq
, flags
);
371 #ifdef CONFIG_IRQ_TIME_ACCOUNTING
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
378 * Tick demultiplexing follows the order
379 * - pending hardirq update
380 * - pending softirq update
384 * - check for guest_time
385 * - else account as system_time
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.
393 static void irqtime_account_process_tick(struct task_struct
*p
, int user_tick
,
394 struct rq
*rq
, int ticks
)
396 u64 cputime
= (__force u64
) cputime_one_jiffy
* ticks
;
397 cputime_t scaled
, other
;
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.
406 other
= account_other_time(ULONG_MAX
);
407 if (other
>= cputime
)
410 scaled
= cputime_to_scaled(cputime
);
412 if (this_cpu_ksoftirqd() == p
) {
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.
418 __account_system_time(p
, cputime
, scaled
, CPUTIME_SOFTIRQ
);
419 } else if (user_tick
) {
420 account_user_time(p
, cputime
, scaled
);
421 } else if (p
== rq
->idle
) {
422 account_idle_time(cputime
);
423 } else if (p
->flags
& PF_VCPU
) { /* System time or guest time */
424 account_guest_time(p
, cputime
, scaled
);
426 __account_system_time(p
, cputime
, scaled
, CPUTIME_SYSTEM
);
430 static void irqtime_account_idle_ticks(int ticks
)
432 struct rq
*rq
= this_rq();
434 irqtime_account_process_tick(current
, 0, rq
, ticks
);
436 #else /* CONFIG_IRQ_TIME_ACCOUNTING */
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
,
439 struct rq
*rq
, int nr_ticks
) {}
440 #endif /* CONFIG_IRQ_TIME_ACCOUNTING */
443 * Use precise platform statistics if available:
445 #ifdef CONFIG_VIRT_CPU_ACCOUNTING
447 #ifndef __ARCH_HAS_VTIME_TASK_SWITCH
448 void vtime_common_task_switch(struct task_struct
*prev
)
450 if (is_idle_task(prev
))
451 vtime_account_idle(prev
);
453 vtime_account_system(prev
);
455 #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
456 vtime_account_user(prev
);
458 arch_vtime_task_switch(prev
);
462 #endif /* CONFIG_VIRT_CPU_ACCOUNTING */
465 #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
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
469 * vtime_account_system() and vtime_account_idle(). Archs that
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
474 #ifndef __ARCH_HAS_VTIME_ACCOUNT
475 void vtime_account_irq_enter(struct task_struct
*tsk
)
477 if (!in_interrupt() && is_idle_task(tsk
))
478 vtime_account_idle(tsk
);
480 vtime_account_system(tsk
);
482 EXPORT_SYMBOL_GPL(vtime_account_irq_enter
);
483 #endif /* __ARCH_HAS_VTIME_ACCOUNT */
485 void task_cputime_adjusted(struct task_struct
*p
, cputime_t
*ut
, cputime_t
*st
)
490 EXPORT_SYMBOL_GPL(task_cputime_adjusted
);
492 void thread_group_cputime_adjusted(struct task_struct
*p
, cputime_t
*ut
, cputime_t
*st
)
494 struct task_cputime cputime
;
496 thread_group_cputime(p
, &cputime
);
501 #else /* !CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
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
507 void account_process_tick(struct task_struct
*p
, int user_tick
)
509 cputime_t cputime
, scaled
, steal
;
510 struct rq
*rq
= this_rq();
512 if (vtime_accounting_cpu_enabled())
515 if (sched_clock_irqtime
) {
516 irqtime_account_process_tick(p
, user_tick
, rq
, 1);
520 cputime
= cputime_one_jiffy
;
521 steal
= steal_account_process_time(ULONG_MAX
);
523 if (steal
>= cputime
)
527 scaled
= cputime_to_scaled(cputime
);
530 account_user_time(p
, cputime
, scaled
);
531 else if ((p
!= rq
->idle
) || (irq_count() != HARDIRQ_OFFSET
))
532 account_system_time(p
, HARDIRQ_OFFSET
, cputime
, scaled
);
534 account_idle_time(cputime
);
538 * Account multiple ticks of idle time.
539 * @ticks: number of stolen ticks
541 void account_idle_ticks(unsigned long ticks
)
543 cputime_t cputime
, steal
;
545 if (sched_clock_irqtime
) {
546 irqtime_account_idle_ticks(ticks
);
550 cputime
= jiffies_to_cputime(ticks
);
551 steal
= steal_account_process_time(ULONG_MAX
);
553 if (steal
>= cputime
)
557 account_idle_time(cputime
);
561 * Perform (stime * rtime) / total, but avoid multiplication overflow by
562 * loosing precision when the numbers are big.
564 static cputime_t
scale_stime(u64 stime
, u64 rtime
, u64 total
)
569 /* Make sure "rtime" is the bigger of stime/rtime */
573 /* Make sure 'total' fits in 32 bits */
577 /* Does rtime (and thus stime) fit in 32 bits? */
581 /* Can we just balance rtime/stime rather than dropping bits? */
585 /* We can grow stime and shrink rtime and try to make them both fit */
591 /* We drop from rtime, it has more bits than stime */
597 * Make sure gcc understands that this is a 32x32->64 multiply,
598 * followed by a 64/32->64 divide.
600 scaled
= div_u64((u64
) (u32
) stime
* (u64
) (u32
) rtime
, (u32
)total
);
601 return (__force cputime_t
) scaled
;
605 * Adjust tick based cputime random precision against scheduler runtime
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
614 * Fix this by scaling these tick based values against the total runtime
615 * accounted by the CFS scheduler.
617 * This code provides the following guarantees:
619 * stime + utime == rtime
620 * stime_i+1 >= stime_i, utime_i+1 >= utime_i
622 * Assuming that rtime_i+1 >= rtime_i.
624 static void cputime_adjust(struct task_cputime
*curr
,
625 struct prev_cputime
*prev
,
626 cputime_t
*ut
, cputime_t
*st
)
628 cputime_t rtime
, stime
, utime
;
631 /* Serialize concurrent callers such that we can honour our guarantees */
632 raw_spin_lock_irqsave(&prev
->lock
, flags
);
633 rtime
= nsecs_to_cputime(curr
->sum_exec_runtime
);
636 * This is possible under two circumstances:
637 * - rtime isn't monotonic after all (a bug);
638 * - we got reordered by the lock.
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.
643 if (prev
->stime
+ prev
->utime
>= rtime
)
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.
664 stime
= scale_stime((__force u64
)stime
, (__force u64
)rtime
,
665 (__force u64
)(stime
+ utime
));
669 * Make sure stime doesn't go backwards; this preserves monotonicity
670 * for utime because rtime is monotonic.
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
677 if (stime
< prev
->stime
)
679 utime
= rtime
- stime
;
682 * Make sure utime doesn't go backwards; this still preserves
683 * monotonicity for stime, analogous argument to above.
685 if (utime
< prev
->utime
) {
687 stime
= rtime
- utime
;
695 raw_spin_unlock_irqrestore(&prev
->lock
, flags
);
698 void task_cputime_adjusted(struct task_struct
*p
, cputime_t
*ut
, cputime_t
*st
)
700 struct task_cputime cputime
= {
701 .sum_exec_runtime
= p
->se
.sum_exec_runtime
,
704 task_cputime(p
, &cputime
.utime
, &cputime
.stime
);
705 cputime_adjust(&cputime
, &p
->prev_cputime
, ut
, st
);
707 EXPORT_SYMBOL_GPL(task_cputime_adjusted
);
709 void thread_group_cputime_adjusted(struct task_struct
*p
, cputime_t
*ut
, cputime_t
*st
)
711 struct task_cputime cputime
;
713 thread_group_cputime(p
, &cputime
);
714 cputime_adjust(&cputime
, &p
->signal
->prev_cputime
, ut
, st
);
716 #endif /* !CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
718 #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
719 static cputime_t
vtime_delta(struct task_struct
*tsk
)
721 unsigned long now
= READ_ONCE(jiffies
);
723 if (time_before(now
, (unsigned long)tsk
->vtime_snap
))
726 return jiffies_to_cputime(now
- tsk
->vtime_snap
);
729 static cputime_t
get_vtime_delta(struct task_struct
*tsk
)
731 unsigned long now
= READ_ONCE(jiffies
);
732 cputime_t delta
, other
;
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.
741 delta
= jiffies_to_cputime(now
- tsk
->vtime_snap
);
742 other
= account_other_time(delta
);
743 WARN_ON_ONCE(tsk
->vtime_snap_whence
== VTIME_INACTIVE
);
744 tsk
->vtime_snap
= now
;
746 return delta
- other
;
749 static void __vtime_account_system(struct task_struct
*tsk
)
751 cputime_t delta_cpu
= get_vtime_delta(tsk
);
753 account_system_time(tsk
, irq_count(), delta_cpu
, cputime_to_scaled(delta_cpu
));
756 void vtime_account_system(struct task_struct
*tsk
)
758 if (!vtime_delta(tsk
))
761 write_seqcount_begin(&tsk
->vtime_seqcount
);
762 __vtime_account_system(tsk
);
763 write_seqcount_end(&tsk
->vtime_seqcount
);
766 void vtime_account_user(struct task_struct
*tsk
)
770 write_seqcount_begin(&tsk
->vtime_seqcount
);
771 tsk
->vtime_snap_whence
= VTIME_SYS
;
772 if (vtime_delta(tsk
)) {
773 delta_cpu
= get_vtime_delta(tsk
);
774 account_user_time(tsk
, delta_cpu
, cputime_to_scaled(delta_cpu
));
776 write_seqcount_end(&tsk
->vtime_seqcount
);
779 void vtime_user_enter(struct task_struct
*tsk
)
781 write_seqcount_begin(&tsk
->vtime_seqcount
);
782 if (vtime_delta(tsk
))
783 __vtime_account_system(tsk
);
784 tsk
->vtime_snap_whence
= VTIME_USER
;
785 write_seqcount_end(&tsk
->vtime_seqcount
);
788 void vtime_guest_enter(struct task_struct
*tsk
)
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.
797 write_seqcount_begin(&tsk
->vtime_seqcount
);
798 if (vtime_delta(tsk
))
799 __vtime_account_system(tsk
);
800 current
->flags
|= PF_VCPU
;
801 write_seqcount_end(&tsk
->vtime_seqcount
);
803 EXPORT_SYMBOL_GPL(vtime_guest_enter
);
805 void vtime_guest_exit(struct task_struct
*tsk
)
807 write_seqcount_begin(&tsk
->vtime_seqcount
);
808 __vtime_account_system(tsk
);
809 current
->flags
&= ~PF_VCPU
;
810 write_seqcount_end(&tsk
->vtime_seqcount
);
812 EXPORT_SYMBOL_GPL(vtime_guest_exit
);
814 void vtime_account_idle(struct task_struct
*tsk
)
816 cputime_t delta_cpu
= get_vtime_delta(tsk
);
818 account_idle_time(delta_cpu
);
821 void arch_vtime_task_switch(struct task_struct
*prev
)
823 write_seqcount_begin(&prev
->vtime_seqcount
);
824 prev
->vtime_snap_whence
= VTIME_INACTIVE
;
825 write_seqcount_end(&prev
->vtime_seqcount
);
827 write_seqcount_begin(¤t
->vtime_seqcount
);
828 current
->vtime_snap_whence
= VTIME_SYS
;
829 current
->vtime_snap
= jiffies
;
830 write_seqcount_end(¤t
->vtime_seqcount
);
833 void vtime_init_idle(struct task_struct
*t
, int cpu
)
837 local_irq_save(flags
);
838 write_seqcount_begin(&t
->vtime_seqcount
);
839 t
->vtime_snap_whence
= VTIME_SYS
;
840 t
->vtime_snap
= jiffies
;
841 write_seqcount_end(&t
->vtime_seqcount
);
842 local_irq_restore(flags
);
845 cputime_t
task_gtime(struct task_struct
*t
)
850 if (!vtime_accounting_enabled())
854 seq
= read_seqcount_begin(&t
->vtime_seqcount
);
857 if (t
->vtime_snap_whence
== VTIME_SYS
&& t
->flags
& PF_VCPU
)
858 gtime
+= vtime_delta(t
);
860 } while (read_seqcount_retry(&t
->vtime_seqcount
, seq
));
866 * Fetch cputime raw values from fields of task_struct and
867 * add up the pending nohz execution time since the last
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
)
877 unsigned long long delta
;
883 seq
= read_seqcount_begin(&t
->vtime_seqcount
);
890 /* Task is sleeping, nothing to add */
891 if (t
->vtime_snap_whence
== VTIME_INACTIVE
||
895 delta
= vtime_delta(t
);
898 * Task runs either in user or kernel space, add pending nohz time to
901 if (t
->vtime_snap_whence
== VTIME_USER
|| t
->flags
& PF_VCPU
) {
904 if (t
->vtime_snap_whence
== VTIME_SYS
)
907 } while (read_seqcount_retry(&t
->vtime_seqcount
, seq
));
911 void task_cputime(struct task_struct
*t
, cputime_t
*utime
, cputime_t
*stime
)
913 cputime_t udelta
, sdelta
;
915 if (!vtime_accounting_enabled()) {
923 fetch_task_cputime(t
, utime
, stime
, &t
->utime
,
924 &t
->stime
, &udelta
, &sdelta
);
931 void task_cputime_scaled(struct task_struct
*t
,
932 cputime_t
*utimescaled
, cputime_t
*stimescaled
)
934 cputime_t udelta
, sdelta
;
936 if (!vtime_accounting_enabled()) {
938 *utimescaled
= t
->utimescaled
;
940 *stimescaled
= t
->stimescaled
;
944 fetch_task_cputime(t
, utimescaled
, stimescaled
,
945 &t
->utimescaled
, &t
->stimescaled
, &udelta
, &sdelta
);
947 *utimescaled
+= cputime_to_scaled(udelta
);
949 *stimescaled
+= cputime_to_scaled(sdelta
);
951 #endif /* CONFIG_VIRT_CPU_ACCOUNTING_GEN */