1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_SCHED_CPUTIME_H
3 #define _LINUX_SCHED_CPUTIME_H
5 #include <linux/sched/signal.h>
8 * cputime accounting APIs:
11 #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
12 #include <asm/cputime.h>
14 #ifndef cputime_to_nsecs
15 # define cputime_to_nsecs(__ct) \
16 (cputime_to_usecs(__ct) * NSEC_PER_USEC)
18 #endif /* CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
20 #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
21 extern bool task_cputime(struct task_struct *t,
22 u64 *utime, u64 *stime);
23 extern u64 task_gtime(struct task_struct *t);
25 static inline bool task_cputime(struct task_struct *t,
26 u64 *utime, u64 *stime)
33 static inline u64 task_gtime(struct task_struct *t)
39 #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
40 static inline void task_cputime_scaled(struct task_struct *t,
44 *utimescaled = t->utimescaled;
45 *stimescaled = t->stimescaled;
48 static inline void task_cputime_scaled(struct task_struct *t,
52 task_cputime(t, utimescaled, stimescaled);
56 extern void task_cputime_adjusted(struct task_struct *p, u64 *ut, u64 *st);
57 extern void thread_group_cputime_adjusted(struct task_struct *p, u64 *ut, u64 *st);
58 extern void cputime_adjust(struct task_cputime *curr, struct prev_cputime *prev,
62 * Thread group CPU time accounting.
64 void thread_group_cputime(struct task_struct *tsk, struct task_cputime *times);
65 void thread_group_sample_cputime(struct task_struct *tsk, u64 *samples);
68 * The following are functions that support scheduler-internal time accounting.
69 * These functions are generally called at the timer tick. None of this depends
70 * on CONFIG_SCHEDSTATS.
74 * get_running_cputimer - return &tsk->signal->cputimer if cputimers are active
76 * @tsk: Pointer to target task.
78 #ifdef CONFIG_POSIX_TIMERS
80 struct thread_group_cputimer *get_running_cputimer(struct task_struct *tsk)
82 struct thread_group_cputimer *cputimer = &tsk->signal->cputimer;
85 * Check whether posix CPU timers are active. If not the thread
86 * group accounting is not active either. Lockless check.
88 if (!READ_ONCE(tsk->signal->posix_cputimers.timers_active))
92 * After we flush the task's sum_exec_runtime to sig->sum_sched_runtime
93 * in __exit_signal(), we won't account to the signal struct further
94 * cputime consumed by that task, even though the task can still be
95 * ticking after __exit_signal().
97 * In order to keep a consistent behaviour between thread group cputime
98 * and thread group cputimer accounting, lets also ignore the cputime
99 * elapsing after __exit_signal() in any thread group timer running.
101 * This makes sure that POSIX CPU clocks and timers are synchronized, so
102 * that a POSIX CPU timer won't expire while the corresponding POSIX CPU
103 * clock delta is behind the expiring timer value.
105 if (unlikely(!tsk->sighand))
112 struct thread_group_cputimer *get_running_cputimer(struct task_struct *tsk)
119 * account_group_user_time - Maintain utime for a thread group.
121 * @tsk: Pointer to task structure.
122 * @cputime: Time value by which to increment the utime field of the
123 * thread_group_cputime structure.
125 * If thread group time is being maintained, get the structure for the
126 * running CPU and update the utime field there.
128 static inline void account_group_user_time(struct task_struct *tsk,
131 struct thread_group_cputimer *cputimer = get_running_cputimer(tsk);
136 atomic64_add(cputime, &cputimer->cputime_atomic.utime);
140 * account_group_system_time - Maintain stime for a thread group.
142 * @tsk: Pointer to task structure.
143 * @cputime: Time value by which to increment the stime field of the
144 * thread_group_cputime structure.
146 * If thread group time is being maintained, get the structure for the
147 * running CPU and update the stime field there.
149 static inline void account_group_system_time(struct task_struct *tsk,
152 struct thread_group_cputimer *cputimer = get_running_cputimer(tsk);
157 atomic64_add(cputime, &cputimer->cputime_atomic.stime);
161 * account_group_exec_runtime - Maintain exec runtime for a thread group.
163 * @tsk: Pointer to task structure.
164 * @ns: Time value by which to increment the sum_exec_runtime field
165 * of the thread_group_cputime structure.
167 * If thread group time is being maintained, get the structure for the
168 * running CPU and update the sum_exec_runtime field there.
170 static inline void account_group_exec_runtime(struct task_struct *tsk,
171 unsigned long long ns)
173 struct thread_group_cputimer *cputimer = get_running_cputimer(tsk);
178 atomic64_add(ns, &cputimer->cputime_atomic.sum_exec_runtime);
181 static inline void prev_cputime_init(struct prev_cputime *prev)
183 #ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
184 prev->utime = prev->stime = 0;
185 raw_spin_lock_init(&prev->lock);
189 extern unsigned long long
190 task_sched_runtime(struct task_struct *task);
192 #endif /* _LINUX_SCHED_CPUTIME_H */