1 // SPDX-License-Identifier: GPL-2.0
3 * CPU accounting code for task groups.
5 * Based on the work by Paul Menage (menage@google.com) and Balbir Singh
8 #include <asm/irq_regs.h>
11 /* Time spent by the tasks of the CPU accounting group executing in ... */
12 enum cpuacct_stat_index {
13 CPUACCT_STAT_USER, /* ... user mode */
14 CPUACCT_STAT_SYSTEM, /* ... kernel mode */
19 static const char * const cpuacct_stat_desc[] = {
20 [CPUACCT_STAT_USER] = "user",
21 [CPUACCT_STAT_SYSTEM] = "system",
24 /* track CPU usage of a group of tasks and its child groups */
26 struct cgroup_subsys_state css;
27 /* cpuusage holds pointer to a u64-type object on every CPU */
28 u64 __percpu *cpuusage;
29 struct kernel_cpustat __percpu *cpustat;
32 static inline struct cpuacct *css_ca(struct cgroup_subsys_state *css)
34 return css ? container_of(css, struct cpuacct, css) : NULL;
37 /* Return CPU accounting group to which this task belongs */
38 static inline struct cpuacct *task_ca(struct task_struct *tsk)
40 return css_ca(task_css(tsk, cpuacct_cgrp_id));
43 static inline struct cpuacct *parent_ca(struct cpuacct *ca)
45 return css_ca(ca->css.parent);
48 static DEFINE_PER_CPU(u64, root_cpuacct_cpuusage);
49 static struct cpuacct root_cpuacct = {
50 .cpustat = &kernel_cpustat,
51 .cpuusage = &root_cpuacct_cpuusage,
54 /* Create a new CPU accounting group */
55 static struct cgroup_subsys_state *
56 cpuacct_css_alloc(struct cgroup_subsys_state *parent_css)
61 return &root_cpuacct.css;
63 ca = kzalloc(sizeof(*ca), GFP_KERNEL);
67 ca->cpuusage = alloc_percpu(u64);
71 ca->cpustat = alloc_percpu(struct kernel_cpustat);
73 goto out_free_cpuusage;
78 free_percpu(ca->cpuusage);
82 return ERR_PTR(-ENOMEM);
85 /* Destroy an existing CPU accounting group */
86 static void cpuacct_css_free(struct cgroup_subsys_state *css)
88 struct cpuacct *ca = css_ca(css);
90 free_percpu(ca->cpustat);
91 free_percpu(ca->cpuusage);
95 static u64 cpuacct_cpuusage_read(struct cpuacct *ca, int cpu,
96 enum cpuacct_stat_index index)
98 u64 *cpuusage = per_cpu_ptr(ca->cpuusage, cpu);
99 u64 *cpustat = per_cpu_ptr(ca->cpustat, cpu)->cpustat;
103 * We allow index == CPUACCT_STAT_NSTATS here to read
106 if (WARN_ON_ONCE(index > CPUACCT_STAT_NSTATS))
111 * Take rq->lock to make 64-bit read safe on 32-bit platforms.
113 raw_spin_rq_lock_irq(cpu_rq(cpu));
117 case CPUACCT_STAT_USER:
118 data = cpustat[CPUTIME_USER] + cpustat[CPUTIME_NICE];
120 case CPUACCT_STAT_SYSTEM:
121 data = cpustat[CPUTIME_SYSTEM] + cpustat[CPUTIME_IRQ] +
122 cpustat[CPUTIME_SOFTIRQ];
124 case CPUACCT_STAT_NSTATS:
130 raw_spin_rq_unlock_irq(cpu_rq(cpu));
136 static void cpuacct_cpuusage_write(struct cpuacct *ca, int cpu)
138 u64 *cpuusage = per_cpu_ptr(ca->cpuusage, cpu);
139 u64 *cpustat = per_cpu_ptr(ca->cpustat, cpu)->cpustat;
141 /* Don't allow to reset global kernel_cpustat */
142 if (ca == &root_cpuacct)
147 * Take rq->lock to make 64-bit write safe on 32-bit platforms.
149 raw_spin_rq_lock_irq(cpu_rq(cpu));
152 cpustat[CPUTIME_USER] = cpustat[CPUTIME_NICE] = 0;
153 cpustat[CPUTIME_SYSTEM] = cpustat[CPUTIME_IRQ] = 0;
154 cpustat[CPUTIME_SOFTIRQ] = 0;
157 raw_spin_rq_unlock_irq(cpu_rq(cpu));
161 /* Return total CPU usage (in nanoseconds) of a group */
162 static u64 __cpuusage_read(struct cgroup_subsys_state *css,
163 enum cpuacct_stat_index index)
165 struct cpuacct *ca = css_ca(css);
166 u64 totalcpuusage = 0;
169 for_each_possible_cpu(i)
170 totalcpuusage += cpuacct_cpuusage_read(ca, i, index);
172 return totalcpuusage;
175 static u64 cpuusage_user_read(struct cgroup_subsys_state *css,
178 return __cpuusage_read(css, CPUACCT_STAT_USER);
181 static u64 cpuusage_sys_read(struct cgroup_subsys_state *css,
184 return __cpuusage_read(css, CPUACCT_STAT_SYSTEM);
187 static u64 cpuusage_read(struct cgroup_subsys_state *css, struct cftype *cft)
189 return __cpuusage_read(css, CPUACCT_STAT_NSTATS);
192 static int cpuusage_write(struct cgroup_subsys_state *css, struct cftype *cft,
195 struct cpuacct *ca = css_ca(css);
199 * Only allow '0' here to do a reset.
204 for_each_possible_cpu(cpu)
205 cpuacct_cpuusage_write(ca, cpu);
210 static int __cpuacct_percpu_seq_show(struct seq_file *m,
211 enum cpuacct_stat_index index)
213 struct cpuacct *ca = css_ca(seq_css(m));
217 for_each_possible_cpu(i) {
218 percpu = cpuacct_cpuusage_read(ca, i, index);
219 seq_printf(m, "%llu ", (unsigned long long) percpu);
225 static int cpuacct_percpu_user_seq_show(struct seq_file *m, void *V)
227 return __cpuacct_percpu_seq_show(m, CPUACCT_STAT_USER);
230 static int cpuacct_percpu_sys_seq_show(struct seq_file *m, void *V)
232 return __cpuacct_percpu_seq_show(m, CPUACCT_STAT_SYSTEM);
235 static int cpuacct_percpu_seq_show(struct seq_file *m, void *V)
237 return __cpuacct_percpu_seq_show(m, CPUACCT_STAT_NSTATS);
240 static int cpuacct_all_seq_show(struct seq_file *m, void *V)
242 struct cpuacct *ca = css_ca(seq_css(m));
247 for (index = 0; index < CPUACCT_STAT_NSTATS; index++)
248 seq_printf(m, " %s", cpuacct_stat_desc[index]);
251 for_each_possible_cpu(cpu) {
252 seq_printf(m, "%d", cpu);
253 for (index = 0; index < CPUACCT_STAT_NSTATS; index++)
254 seq_printf(m, " %llu",
255 cpuacct_cpuusage_read(ca, cpu, index));
261 static int cpuacct_stats_show(struct seq_file *sf, void *v)
263 struct cpuacct *ca = css_ca(seq_css(sf));
264 struct task_cputime cputime;
265 u64 val[CPUACCT_STAT_NSTATS];
269 memset(&cputime, 0, sizeof(cputime));
270 for_each_possible_cpu(cpu) {
271 u64 *cpustat = per_cpu_ptr(ca->cpustat, cpu)->cpustat;
273 cputime.utime += cpustat[CPUTIME_USER];
274 cputime.utime += cpustat[CPUTIME_NICE];
275 cputime.stime += cpustat[CPUTIME_SYSTEM];
276 cputime.stime += cpustat[CPUTIME_IRQ];
277 cputime.stime += cpustat[CPUTIME_SOFTIRQ];
279 cputime.sum_exec_runtime += *per_cpu_ptr(ca->cpuusage, cpu);
282 cputime_adjust(&cputime, &seq_css(sf)->cgroup->prev_cputime,
283 &val[CPUACCT_STAT_USER], &val[CPUACCT_STAT_SYSTEM]);
285 for (stat = 0; stat < CPUACCT_STAT_NSTATS; stat++) {
286 seq_printf(sf, "%s %llu\n", cpuacct_stat_desc[stat],
287 nsec_to_clock_t(val[stat]));
293 static struct cftype files[] = {
296 .read_u64 = cpuusage_read,
297 .write_u64 = cpuusage_write,
300 .name = "usage_user",
301 .read_u64 = cpuusage_user_read,
305 .read_u64 = cpuusage_sys_read,
308 .name = "usage_percpu",
309 .seq_show = cpuacct_percpu_seq_show,
312 .name = "usage_percpu_user",
313 .seq_show = cpuacct_percpu_user_seq_show,
316 .name = "usage_percpu_sys",
317 .seq_show = cpuacct_percpu_sys_seq_show,
321 .seq_show = cpuacct_all_seq_show,
325 .seq_show = cpuacct_stats_show,
331 * charge this task's execution time to its accounting group.
333 * called with rq->lock held.
335 void cpuacct_charge(struct task_struct *tsk, u64 cputime)
337 unsigned int cpu = task_cpu(tsk);
342 for (ca = task_ca(tsk); ca; ca = parent_ca(ca))
343 *per_cpu_ptr(ca->cpuusage, cpu) += cputime;
349 * Add user/system time to cpuacct.
351 * Note: it's the caller that updates the account of the root cgroup.
353 void cpuacct_account_field(struct task_struct *tsk, int index, u64 val)
358 for (ca = task_ca(tsk); ca != &root_cpuacct; ca = parent_ca(ca))
359 __this_cpu_add(ca->cpustat->cpustat[index], val);
363 struct cgroup_subsys cpuacct_cgrp_subsys = {
364 .css_alloc = cpuacct_css_alloc,
365 .css_free = cpuacct_css_free,
366 .legacy_cftypes = files,