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 void task_cputime(struct task_struct *t,
22 u64 *utime, u64 *stime);
23 extern u64 task_gtime(struct task_struct *t);
25 static inline void task_cputime(struct task_struct *t,
26 u64 *utime, u64 *stime)
32 static inline u64 task_gtime(struct task_struct *t)
38 #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
39 static inline void task_cputime_scaled(struct task_struct *t,
43 *utimescaled = t->utimescaled;
44 *stimescaled = t->stimescaled;
47 static inline void task_cputime_scaled(struct task_struct *t,
51 task_cputime(t, utimescaled, stimescaled);
55 extern void task_cputime_adjusted(struct task_struct *p, u64 *ut, u64 *st);
56 extern void thread_group_cputime_adjusted(struct task_struct *p, u64 *ut, u64 *st);
60 * Thread group CPU time accounting.
62 void thread_group_cputime(struct task_struct *tsk, struct task_cputime *times);
63 void thread_group_cputimer(struct task_struct *tsk, struct task_cputime *times);
67 * The following are functions that support scheduler-internal time accounting.
68 * These functions are generally called at the timer tick. None of this depends
69 * on CONFIG_SCHEDSTATS.
73 * get_running_cputimer - return &tsk->signal->cputimer if cputimer is running
75 * @tsk: Pointer to target task.
77 #ifdef CONFIG_POSIX_TIMERS
79 struct thread_group_cputimer *get_running_cputimer(struct task_struct *tsk)
81 struct thread_group_cputimer *cputimer = &tsk->signal->cputimer;
83 /* Check if cputimer isn't running. This is accessed without locking. */
84 if (!READ_ONCE(cputimer->running))
88 * After we flush the task's sum_exec_runtime to sig->sum_sched_runtime
89 * in __exit_signal(), we won't account to the signal struct further
90 * cputime consumed by that task, even though the task can still be
91 * ticking after __exit_signal().
93 * In order to keep a consistent behaviour between thread group cputime
94 * and thread group cputimer accounting, lets also ignore the cputime
95 * elapsing after __exit_signal() in any thread group timer running.
97 * This makes sure that POSIX CPU clocks and timers are synchronized, so
98 * that a POSIX CPU timer won't expire while the corresponding POSIX CPU
99 * clock delta is behind the expiring timer value.
101 if (unlikely(!tsk->sighand))
108 struct thread_group_cputimer *get_running_cputimer(struct task_struct *tsk)
115 * account_group_user_time - Maintain utime for a thread group.
117 * @tsk: Pointer to task structure.
118 * @cputime: Time value by which to increment the utime field of the
119 * thread_group_cputime structure.
121 * If thread group time is being maintained, get the structure for the
122 * running CPU and update the utime field there.
124 static inline void account_group_user_time(struct task_struct *tsk,
127 struct thread_group_cputimer *cputimer = get_running_cputimer(tsk);
132 atomic64_add(cputime, &cputimer->cputime_atomic.utime);
136 * account_group_system_time - Maintain stime for a thread group.
138 * @tsk: Pointer to task structure.
139 * @cputime: Time value by which to increment the stime field of the
140 * thread_group_cputime structure.
142 * If thread group time is being maintained, get the structure for the
143 * running CPU and update the stime field there.
145 static inline void account_group_system_time(struct task_struct *tsk,
148 struct thread_group_cputimer *cputimer = get_running_cputimer(tsk);
153 atomic64_add(cputime, &cputimer->cputime_atomic.stime);
157 * account_group_exec_runtime - Maintain exec runtime for a thread group.
159 * @tsk: Pointer to task structure.
160 * @ns: Time value by which to increment the sum_exec_runtime field
161 * of the thread_group_cputime structure.
163 * If thread group time is being maintained, get the structure for the
164 * running CPU and update the sum_exec_runtime field there.
166 static inline void account_group_exec_runtime(struct task_struct *tsk,
167 unsigned long long ns)
169 struct thread_group_cputimer *cputimer = get_running_cputimer(tsk);
174 atomic64_add(ns, &cputimer->cputime_atomic.sum_exec_runtime);
177 static inline void prev_cputime_init(struct prev_cputime *prev)
179 #ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
180 prev->utime = prev->stime = 0;
181 raw_spin_lock_init(&prev->lock);
185 extern unsigned long long
186 task_sched_runtime(struct task_struct *task);
188 #endif /* _LINUX_SCHED_CPUTIME_H */