blob: 41f85c4d09387a8bd03299ef00f6e79482f68b40 [file] [log] [blame]
Li Zefan2e76c242013-03-29 14:36:31 +08001#include <linux/cgroup.h>
2#include <linux/slab.h>
3#include <linux/percpu.h>
4#include <linux/spinlock.h>
5#include <linux/cpumask.h>
6#include <linux/seq_file.h>
7#include <linux/rcupdate.h>
8#include <linux/kernel_stat.h>
Ingo Molnarb329fd52013-04-10 15:10:50 +02009#include <linux/err.h>
Li Zefan2e76c242013-03-29 14:36:31 +080010
11#include "sched.h"
12
13/*
14 * CPU accounting code for task groups.
15 *
16 * Based on the work by Paul Menage (menage@google.com) and Balbir Singh
17 * (balbir@in.ibm.com).
18 */
19
Li Zefand1712792013-03-29 14:38:13 +080020/* Time spent by the tasks of the cpu accounting group executing in ... */
21enum cpuacct_stat_index {
22 CPUACCT_STAT_USER, /* ... user mode */
23 CPUACCT_STAT_SYSTEM, /* ... kernel mode */
24
25 CPUACCT_STAT_NSTATS,
26};
27
Dongsheng Yangd7400372016-03-22 16:37:08 +080028enum cpuacct_usage_index {
29 CPUACCT_USAGE_USER, /* ... user mode */
30 CPUACCT_USAGE_SYSTEM, /* ... kernel mode */
31
32 CPUACCT_USAGE_NRUSAGE,
33};
34
35struct cpuacct_usage {
36 u64 usages[CPUACCT_USAGE_NRUSAGE];
37};
38
Li Zefand1712792013-03-29 14:38:13 +080039/* track cpu usage of a group of tasks and its child groups */
40struct cpuacct {
41 struct cgroup_subsys_state css;
42 /* cpuusage holds pointer to a u64-type object on every cpu */
Dongsheng Yangd7400372016-03-22 16:37:08 +080043 struct cpuacct_usage __percpu *cpuusage;
Li Zefand1712792013-03-29 14:38:13 +080044 struct kernel_cpustat __percpu *cpustat;
45};
46
Tejun Heoa7c6d552013-08-08 20:11:23 -040047static inline struct cpuacct *css_ca(struct cgroup_subsys_state *css)
48{
49 return css ? container_of(css, struct cpuacct, css) : NULL;
50}
51
Li Zefand1712792013-03-29 14:38:13 +080052/* return cpu accounting group to which this task belongs */
53static inline struct cpuacct *task_ca(struct task_struct *tsk)
54{
Tejun Heo073219e2014-02-08 10:36:58 -050055 return css_ca(task_css(tsk, cpuacct_cgrp_id));
Li Zefand1712792013-03-29 14:38:13 +080056}
57
Li Zefand1712792013-03-29 14:38:13 +080058static inline struct cpuacct *parent_ca(struct cpuacct *ca)
59{
Tejun Heo5c9d5352014-05-16 13:22:48 -040060 return css_ca(ca->css.parent);
Li Zefand1712792013-03-29 14:38:13 +080061}
62
Dongsheng Yangd7400372016-03-22 16:37:08 +080063static DEFINE_PER_CPU(struct cpuacct_usage, root_cpuacct_cpuusage);
Li Zefan14c6d3c2013-03-29 14:44:04 +080064static struct cpuacct root_cpuacct = {
65 .cpustat = &kernel_cpustat,
66 .cpuusage = &root_cpuacct_cpuusage,
67};
Li Zefan2e76c242013-03-29 14:36:31 +080068
69/* create a new cpu accounting group */
Tejun Heoeb954192013-08-08 20:11:23 -040070static struct cgroup_subsys_state *
71cpuacct_css_alloc(struct cgroup_subsys_state *parent_css)
Li Zefan2e76c242013-03-29 14:36:31 +080072{
73 struct cpuacct *ca;
74
Tejun Heoeb954192013-08-08 20:11:23 -040075 if (!parent_css)
Li Zefan2e76c242013-03-29 14:36:31 +080076 return &root_cpuacct.css;
77
78 ca = kzalloc(sizeof(*ca), GFP_KERNEL);
79 if (!ca)
80 goto out;
81
Dongsheng Yangd7400372016-03-22 16:37:08 +080082 ca->cpuusage = alloc_percpu(struct cpuacct_usage);
Li Zefan2e76c242013-03-29 14:36:31 +080083 if (!ca->cpuusage)
84 goto out_free_ca;
85
86 ca->cpustat = alloc_percpu(struct kernel_cpustat);
87 if (!ca->cpustat)
88 goto out_free_cpuusage;
89
90 return &ca->css;
91
92out_free_cpuusage:
93 free_percpu(ca->cpuusage);
94out_free_ca:
95 kfree(ca);
96out:
97 return ERR_PTR(-ENOMEM);
98}
99
100/* destroy an existing cpu accounting group */
Tejun Heoeb954192013-08-08 20:11:23 -0400101static void cpuacct_css_free(struct cgroup_subsys_state *css)
Li Zefan2e76c242013-03-29 14:36:31 +0800102{
Tejun Heoeb954192013-08-08 20:11:23 -0400103 struct cpuacct *ca = css_ca(css);
Li Zefan2e76c242013-03-29 14:36:31 +0800104
105 free_percpu(ca->cpustat);
106 free_percpu(ca->cpuusage);
107 kfree(ca);
108}
109
Dongsheng Yangd7400372016-03-22 16:37:08 +0800110static u64 cpuacct_cpuusage_read(struct cpuacct *ca, int cpu,
111 enum cpuacct_usage_index index)
Li Zefan2e76c242013-03-29 14:36:31 +0800112{
Dongsheng Yangd7400372016-03-22 16:37:08 +0800113 struct cpuacct_usage *cpuusage = per_cpu_ptr(ca->cpuusage, cpu);
Li Zefan2e76c242013-03-29 14:36:31 +0800114 u64 data;
115
Dongsheng Yangd7400372016-03-22 16:37:08 +0800116 /*
117 * We allow index == CPUACCT_USAGE_NRUSAGE here to read
118 * the sum of suages.
119 */
120 BUG_ON(index > CPUACCT_USAGE_NRUSAGE);
121
Li Zefan2e76c242013-03-29 14:36:31 +0800122#ifndef CONFIG_64BIT
123 /*
124 * Take rq->lock to make 64-bit read safe on 32-bit platforms.
125 */
126 raw_spin_lock_irq(&cpu_rq(cpu)->lock);
Dongsheng Yangd7400372016-03-22 16:37:08 +0800127#endif
128
129 if (index == CPUACCT_USAGE_NRUSAGE) {
130 int i = 0;
131
132 data = 0;
133 for (i = 0; i < CPUACCT_USAGE_NRUSAGE; i++)
134 data += cpuusage->usages[i];
135 } else {
136 data = cpuusage->usages[index];
137 }
138
139#ifndef CONFIG_64BIT
Li Zefan2e76c242013-03-29 14:36:31 +0800140 raw_spin_unlock_irq(&cpu_rq(cpu)->lock);
Li Zefan2e76c242013-03-29 14:36:31 +0800141#endif
142
143 return data;
144}
145
146static void cpuacct_cpuusage_write(struct cpuacct *ca, int cpu, u64 val)
147{
Dongsheng Yangd7400372016-03-22 16:37:08 +0800148 struct cpuacct_usage *cpuusage = per_cpu_ptr(ca->cpuusage, cpu);
149 int i;
Li Zefan2e76c242013-03-29 14:36:31 +0800150
151#ifndef CONFIG_64BIT
152 /*
153 * Take rq->lock to make 64-bit write safe on 32-bit platforms.
154 */
155 raw_spin_lock_irq(&cpu_rq(cpu)->lock);
Dongsheng Yangd7400372016-03-22 16:37:08 +0800156#endif
157
158 for (i = 0; i < CPUACCT_USAGE_NRUSAGE; i++)
159 cpuusage->usages[i] = val;
160
161#ifndef CONFIG_64BIT
Li Zefan2e76c242013-03-29 14:36:31 +0800162 raw_spin_unlock_irq(&cpu_rq(cpu)->lock);
Li Zefan2e76c242013-03-29 14:36:31 +0800163#endif
164}
165
166/* return total cpu usage (in nanoseconds) of a group */
Dongsheng Yangd7400372016-03-22 16:37:08 +0800167static u64 __cpuusage_read(struct cgroup_subsys_state *css,
168 enum cpuacct_usage_index index)
Li Zefan2e76c242013-03-29 14:36:31 +0800169{
Tejun Heo182446d2013-08-08 20:11:24 -0400170 struct cpuacct *ca = css_ca(css);
Li Zefan2e76c242013-03-29 14:36:31 +0800171 u64 totalcpuusage = 0;
172 int i;
173
Zhao Lei5ca37262016-03-22 16:37:07 +0800174 for_each_possible_cpu(i)
Dongsheng Yangd7400372016-03-22 16:37:08 +0800175 totalcpuusage += cpuacct_cpuusage_read(ca, i, index);
Li Zefan2e76c242013-03-29 14:36:31 +0800176
177 return totalcpuusage;
178}
179
Dongsheng Yangd7400372016-03-22 16:37:08 +0800180static u64 cpuusage_user_read(struct cgroup_subsys_state *css,
181 struct cftype *cft)
182{
183 return __cpuusage_read(css, CPUACCT_USAGE_USER);
184}
185
186static u64 cpuusage_sys_read(struct cgroup_subsys_state *css,
187 struct cftype *cft)
188{
189 return __cpuusage_read(css, CPUACCT_USAGE_SYSTEM);
190}
191
192static u64 cpuusage_read(struct cgroup_subsys_state *css, struct cftype *cft)
193{
194 return __cpuusage_read(css, CPUACCT_USAGE_NRUSAGE);
195}
196
Tejun Heo182446d2013-08-08 20:11:24 -0400197static int cpuusage_write(struct cgroup_subsys_state *css, struct cftype *cft,
Dongsheng Yang1a736b72015-12-21 19:14:42 +0800198 u64 val)
Li Zefan2e76c242013-03-29 14:36:31 +0800199{
Tejun Heo182446d2013-08-08 20:11:24 -0400200 struct cpuacct *ca = css_ca(css);
Dongsheng Yangd7400372016-03-22 16:37:08 +0800201 int cpu;
Li Zefan2e76c242013-03-29 14:36:31 +0800202
Dongsheng Yang1a736b72015-12-21 19:14:42 +0800203 /*
204 * Only allow '0' here to do a reset.
205 */
Dongsheng Yangd7400372016-03-22 16:37:08 +0800206 if (val)
207 return -EINVAL;
Li Zefan2e76c242013-03-29 14:36:31 +0800208
Dongsheng Yangd7400372016-03-22 16:37:08 +0800209 for_each_possible_cpu(cpu)
210 cpuacct_cpuusage_write(ca, cpu, 0);
Li Zefan2e76c242013-03-29 14:36:31 +0800211
Dongsheng Yangd7400372016-03-22 16:37:08 +0800212 return 0;
Li Zefan2e76c242013-03-29 14:36:31 +0800213}
214
Dongsheng Yangd7400372016-03-22 16:37:08 +0800215static int __cpuacct_percpu_seq_show(struct seq_file *m,
216 enum cpuacct_usage_index index)
Li Zefan2e76c242013-03-29 14:36:31 +0800217{
Tejun Heo2da8ca82013-12-05 12:28:04 -0500218 struct cpuacct *ca = css_ca(seq_css(m));
Li Zefan2e76c242013-03-29 14:36:31 +0800219 u64 percpu;
220 int i;
221
Zhao Lei5ca37262016-03-22 16:37:07 +0800222 for_each_possible_cpu(i) {
Dongsheng Yangd7400372016-03-22 16:37:08 +0800223 percpu = cpuacct_cpuusage_read(ca, i, index);
Li Zefan2e76c242013-03-29 14:36:31 +0800224 seq_printf(m, "%llu ", (unsigned long long) percpu);
225 }
226 seq_printf(m, "\n");
227 return 0;
228}
229
Dongsheng Yangd7400372016-03-22 16:37:08 +0800230static int cpuacct_percpu_user_seq_show(struct seq_file *m, void *V)
231{
232 return __cpuacct_percpu_seq_show(m, CPUACCT_USAGE_USER);
233}
234
235static int cpuacct_percpu_sys_seq_show(struct seq_file *m, void *V)
236{
237 return __cpuacct_percpu_seq_show(m, CPUACCT_USAGE_SYSTEM);
238}
239
240static int cpuacct_percpu_seq_show(struct seq_file *m, void *V)
241{
242 return __cpuacct_percpu_seq_show(m, CPUACCT_USAGE_NRUSAGE);
243}
244
Li Zefan2e76c242013-03-29 14:36:31 +0800245static const char * const cpuacct_stat_desc[] = {
246 [CPUACCT_STAT_USER] = "user",
247 [CPUACCT_STAT_SYSTEM] = "system",
248};
249
Tejun Heo2da8ca82013-12-05 12:28:04 -0500250static int cpuacct_stats_show(struct seq_file *sf, void *v)
Li Zefan2e76c242013-03-29 14:36:31 +0800251{
Tejun Heo2da8ca82013-12-05 12:28:04 -0500252 struct cpuacct *ca = css_ca(seq_css(sf));
Li Zefan2e76c242013-03-29 14:36:31 +0800253 int cpu;
254 s64 val = 0;
255
Zhao Lei5ca37262016-03-22 16:37:07 +0800256 for_each_possible_cpu(cpu) {
Li Zefan2e76c242013-03-29 14:36:31 +0800257 struct kernel_cpustat *kcpustat = per_cpu_ptr(ca->cpustat, cpu);
258 val += kcpustat->cpustat[CPUTIME_USER];
259 val += kcpustat->cpustat[CPUTIME_NICE];
260 }
261 val = cputime64_to_clock_t(val);
Tejun Heo44ffc752013-12-05 12:28:01 -0500262 seq_printf(sf, "%s %lld\n", cpuacct_stat_desc[CPUACCT_STAT_USER], val);
Li Zefan2e76c242013-03-29 14:36:31 +0800263
264 val = 0;
Zhao Lei5ca37262016-03-22 16:37:07 +0800265 for_each_possible_cpu(cpu) {
Li Zefan2e76c242013-03-29 14:36:31 +0800266 struct kernel_cpustat *kcpustat = per_cpu_ptr(ca->cpustat, cpu);
267 val += kcpustat->cpustat[CPUTIME_SYSTEM];
268 val += kcpustat->cpustat[CPUTIME_IRQ];
269 val += kcpustat->cpustat[CPUTIME_SOFTIRQ];
270 }
271
272 val = cputime64_to_clock_t(val);
Tejun Heo44ffc752013-12-05 12:28:01 -0500273 seq_printf(sf, "%s %lld\n", cpuacct_stat_desc[CPUACCT_STAT_SYSTEM], val);
Li Zefan2e76c242013-03-29 14:36:31 +0800274
275 return 0;
276}
277
278static struct cftype files[] = {
279 {
280 .name = "usage",
281 .read_u64 = cpuusage_read,
282 .write_u64 = cpuusage_write,
283 },
284 {
Dongsheng Yangd7400372016-03-22 16:37:08 +0800285 .name = "usage_user",
286 .read_u64 = cpuusage_user_read,
287 },
288 {
289 .name = "usage_sys",
290 .read_u64 = cpuusage_sys_read,
291 },
292 {
Li Zefan2e76c242013-03-29 14:36:31 +0800293 .name = "usage_percpu",
Tejun Heo2da8ca82013-12-05 12:28:04 -0500294 .seq_show = cpuacct_percpu_seq_show,
Li Zefan2e76c242013-03-29 14:36:31 +0800295 },
296 {
Dongsheng Yangd7400372016-03-22 16:37:08 +0800297 .name = "usage_percpu_user",
298 .seq_show = cpuacct_percpu_user_seq_show,
299 },
300 {
301 .name = "usage_percpu_sys",
302 .seq_show = cpuacct_percpu_sys_seq_show,
303 },
304 {
Li Zefan2e76c242013-03-29 14:36:31 +0800305 .name = "stat",
Tejun Heo2da8ca82013-12-05 12:28:04 -0500306 .seq_show = cpuacct_stats_show,
Li Zefan2e76c242013-03-29 14:36:31 +0800307 },
308 { } /* terminate */
309};
310
311/*
312 * charge this task's execution time to its accounting group.
313 *
314 * called with rq->lock held.
315 */
316void cpuacct_charge(struct task_struct *tsk, u64 cputime)
317{
318 struct cpuacct *ca;
Anton Blanchardbd928832016-04-06 21:59:50 +1000319 int index = CPUACCT_USAGE_SYSTEM;
320 struct pt_regs *regs = task_pt_regs(tsk);
Dongsheng Yangd7400372016-03-22 16:37:08 +0800321
Anton Blanchardbd928832016-04-06 21:59:50 +1000322 if (regs && user_mode(regs))
Dongsheng Yangd7400372016-03-22 16:37:08 +0800323 index = CPUACCT_USAGE_USER;
Li Zefan2e76c242013-03-29 14:36:31 +0800324
325 rcu_read_lock();
Dongsheng Yangd7400372016-03-22 16:37:08 +0800326
Zhao Lei73e6aaf2016-03-17 12:19:43 +0800327 for (ca = task_ca(tsk); ca; ca = parent_ca(ca))
Dongsheng Yangd7400372016-03-22 16:37:08 +0800328 this_cpu_ptr(ca->cpuusage)->usages[index] += cputime;
329
Li Zefan2e76c242013-03-29 14:36:31 +0800330 rcu_read_unlock();
331}
332
Li Zefan1966aaf2013-03-29 14:37:06 +0800333/*
334 * Add user/system time to cpuacct.
335 *
336 * Note: it's the caller that updates the account of the root cgroup.
337 */
Zhao Lei73e6aaf2016-03-17 12:19:43 +0800338void cpuacct_account_field(struct task_struct *tsk, int index, u64 val)
Li Zefan1966aaf2013-03-29 14:37:06 +0800339{
Li Zefan1966aaf2013-03-29 14:37:06 +0800340 struct cpuacct *ca;
341
Li Zefan1966aaf2013-03-29 14:37:06 +0800342 rcu_read_lock();
Zhao Lei73e6aaf2016-03-17 12:19:43 +0800343 for (ca = task_ca(tsk); ca != &root_cpuacct; ca = parent_ca(ca))
344 this_cpu_ptr(ca->cpustat)->cpustat[index] += val;
Li Zefan1966aaf2013-03-29 14:37:06 +0800345 rcu_read_unlock();
346}
347
Tejun Heo073219e2014-02-08 10:36:58 -0500348struct cgroup_subsys cpuacct_cgrp_subsys = {
Li Zefan621e2de2013-03-29 14:44:15 +0800349 .css_alloc = cpuacct_css_alloc,
350 .css_free = cpuacct_css_free,
Tejun Heo55779642014-07-15 11:05:09 -0400351 .legacy_cftypes = files,
Tejun Heob38e42e2016-02-23 10:00:50 -0500352 .early_init = true,
Li Zefan2e76c242013-03-29 14:36:31 +0800353};