blob: 9305fd2f8cf97a473c040a7fcb917ffd29b4af18 [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>
9
10#include "sched.h"
11
12/*
13 * CPU accounting code for task groups.
14 *
15 * Based on the work by Paul Menage (menage@google.com) and Balbir Singh
16 * (balbir@in.ibm.com).
17 */
18
Li Zefand1712792013-03-29 14:38:13 +080019/* Time spent by the tasks of the cpu accounting group executing in ... */
20enum cpuacct_stat_index {
21 CPUACCT_STAT_USER, /* ... user mode */
22 CPUACCT_STAT_SYSTEM, /* ... kernel mode */
23
24 CPUACCT_STAT_NSTATS,
25};
26
27/* track cpu usage of a group of tasks and its child groups */
28struct cpuacct {
29 struct cgroup_subsys_state css;
30 /* cpuusage holds pointer to a u64-type object on every cpu */
31 u64 __percpu *cpuusage;
32 struct kernel_cpustat __percpu *cpustat;
33};
34
35/* return cpu accounting group corresponding to this container */
36static inline struct cpuacct *cgroup_ca(struct cgroup *cgrp)
37{
38 return container_of(cgroup_subsys_state(cgrp, cpuacct_subsys_id),
39 struct cpuacct, css);
40}
41
42/* return cpu accounting group to which this task belongs */
43static inline struct cpuacct *task_ca(struct task_struct *tsk)
44{
45 return container_of(task_subsys_state(tsk, cpuacct_subsys_id),
46 struct cpuacct, css);
47}
48
49static inline struct cpuacct *__parent_ca(struct cpuacct *ca)
50{
51 return cgroup_ca(ca->css.cgroup->parent);
52}
53
54static inline struct cpuacct *parent_ca(struct cpuacct *ca)
55{
56 if (!ca->css.cgroup->parent)
57 return NULL;
58 return cgroup_ca(ca->css.cgroup->parent);
59}
60
61static struct cpuacct root_cpuacct;
Li Zefan2e76c242013-03-29 14:36:31 +080062
63/* create a new cpu accounting group */
64static struct cgroup_subsys_state *cpuacct_css_alloc(struct cgroup *cgrp)
65{
66 struct cpuacct *ca;
67
68 if (!cgrp->parent)
69 return &root_cpuacct.css;
70
71 ca = kzalloc(sizeof(*ca), GFP_KERNEL);
72 if (!ca)
73 goto out;
74
75 ca->cpuusage = alloc_percpu(u64);
76 if (!ca->cpuusage)
77 goto out_free_ca;
78
79 ca->cpustat = alloc_percpu(struct kernel_cpustat);
80 if (!ca->cpustat)
81 goto out_free_cpuusage;
82
83 return &ca->css;
84
85out_free_cpuusage:
86 free_percpu(ca->cpuusage);
87out_free_ca:
88 kfree(ca);
89out:
90 return ERR_PTR(-ENOMEM);
91}
92
93/* destroy an existing cpu accounting group */
94static void cpuacct_css_free(struct cgroup *cgrp)
95{
96 struct cpuacct *ca = cgroup_ca(cgrp);
97
98 free_percpu(ca->cpustat);
99 free_percpu(ca->cpuusage);
100 kfree(ca);
101}
102
103static u64 cpuacct_cpuusage_read(struct cpuacct *ca, int cpu)
104{
105 u64 *cpuusage = per_cpu_ptr(ca->cpuusage, cpu);
106 u64 data;
107
108#ifndef CONFIG_64BIT
109 /*
110 * Take rq->lock to make 64-bit read safe on 32-bit platforms.
111 */
112 raw_spin_lock_irq(&cpu_rq(cpu)->lock);
113 data = *cpuusage;
114 raw_spin_unlock_irq(&cpu_rq(cpu)->lock);
115#else
116 data = *cpuusage;
117#endif
118
119 return data;
120}
121
122static void cpuacct_cpuusage_write(struct cpuacct *ca, int cpu, u64 val)
123{
124 u64 *cpuusage = per_cpu_ptr(ca->cpuusage, cpu);
125
126#ifndef CONFIG_64BIT
127 /*
128 * Take rq->lock to make 64-bit write safe on 32-bit platforms.
129 */
130 raw_spin_lock_irq(&cpu_rq(cpu)->lock);
131 *cpuusage = val;
132 raw_spin_unlock_irq(&cpu_rq(cpu)->lock);
133#else
134 *cpuusage = val;
135#endif
136}
137
138/* return total cpu usage (in nanoseconds) of a group */
139static u64 cpuusage_read(struct cgroup *cgrp, struct cftype *cft)
140{
141 struct cpuacct *ca = cgroup_ca(cgrp);
142 u64 totalcpuusage = 0;
143 int i;
144
145 for_each_present_cpu(i)
146 totalcpuusage += cpuacct_cpuusage_read(ca, i);
147
148 return totalcpuusage;
149}
150
151static int cpuusage_write(struct cgroup *cgrp, struct cftype *cftype,
152 u64 reset)
153{
154 struct cpuacct *ca = cgroup_ca(cgrp);
155 int err = 0;
156 int i;
157
158 if (reset) {
159 err = -EINVAL;
160 goto out;
161 }
162
163 for_each_present_cpu(i)
164 cpuacct_cpuusage_write(ca, i, 0);
165
166out:
167 return err;
168}
169
170static int cpuacct_percpu_seq_read(struct cgroup *cgroup, struct cftype *cft,
171 struct seq_file *m)
172{
173 struct cpuacct *ca = cgroup_ca(cgroup);
174 u64 percpu;
175 int i;
176
177 for_each_present_cpu(i) {
178 percpu = cpuacct_cpuusage_read(ca, i);
179 seq_printf(m, "%llu ", (unsigned long long) percpu);
180 }
181 seq_printf(m, "\n");
182 return 0;
183}
184
185static const char * const cpuacct_stat_desc[] = {
186 [CPUACCT_STAT_USER] = "user",
187 [CPUACCT_STAT_SYSTEM] = "system",
188};
189
190static int cpuacct_stats_show(struct cgroup *cgrp, struct cftype *cft,
191 struct cgroup_map_cb *cb)
192{
193 struct cpuacct *ca = cgroup_ca(cgrp);
194 int cpu;
195 s64 val = 0;
196
197 for_each_online_cpu(cpu) {
198 struct kernel_cpustat *kcpustat = per_cpu_ptr(ca->cpustat, cpu);
199 val += kcpustat->cpustat[CPUTIME_USER];
200 val += kcpustat->cpustat[CPUTIME_NICE];
201 }
202 val = cputime64_to_clock_t(val);
203 cb->fill(cb, cpuacct_stat_desc[CPUACCT_STAT_USER], val);
204
205 val = 0;
206 for_each_online_cpu(cpu) {
207 struct kernel_cpustat *kcpustat = per_cpu_ptr(ca->cpustat, cpu);
208 val += kcpustat->cpustat[CPUTIME_SYSTEM];
209 val += kcpustat->cpustat[CPUTIME_IRQ];
210 val += kcpustat->cpustat[CPUTIME_SOFTIRQ];
211 }
212
213 val = cputime64_to_clock_t(val);
214 cb->fill(cb, cpuacct_stat_desc[CPUACCT_STAT_SYSTEM], val);
215
216 return 0;
217}
218
219static struct cftype files[] = {
220 {
221 .name = "usage",
222 .read_u64 = cpuusage_read,
223 .write_u64 = cpuusage_write,
224 },
225 {
226 .name = "usage_percpu",
227 .read_seq_string = cpuacct_percpu_seq_read,
228 },
229 {
230 .name = "stat",
231 .read_map = cpuacct_stats_show,
232 },
233 { } /* terminate */
234};
235
236/*
237 * charge this task's execution time to its accounting group.
238 *
239 * called with rq->lock held.
240 */
241void cpuacct_charge(struct task_struct *tsk, u64 cputime)
242{
243 struct cpuacct *ca;
244 int cpu;
245
246 if (unlikely(!cpuacct_subsys.active))
247 return;
248
249 cpu = task_cpu(tsk);
250
251 rcu_read_lock();
252
253 ca = task_ca(tsk);
254
Li Zefan543bc0e2013-03-29 14:37:29 +0800255 while (true) {
Li Zefan2e76c242013-03-29 14:36:31 +0800256 u64 *cpuusage = per_cpu_ptr(ca->cpuusage, cpu);
257 *cpuusage += cputime;
Li Zefan543bc0e2013-03-29 14:37:29 +0800258
259 ca = parent_ca(ca);
260 if (!ca)
261 break;
Li Zefan2e76c242013-03-29 14:36:31 +0800262 }
263
264 rcu_read_unlock();
265}
266
Li Zefan1966aaf2013-03-29 14:37:06 +0800267/*
268 * Add user/system time to cpuacct.
269 *
270 * Note: it's the caller that updates the account of the root cgroup.
271 */
272void cpuacct_account_field(struct task_struct *p, int index, u64 val)
273{
274 struct kernel_cpustat *kcpustat;
275 struct cpuacct *ca;
276
277 if (unlikely(!cpuacct_subsys.active))
278 return;
279
280 rcu_read_lock();
281 ca = task_ca(p);
Li Zefan5f40d802013-03-29 14:37:43 +0800282 while (ca != &root_cpuacct) {
Li Zefan1966aaf2013-03-29 14:37:06 +0800283 kcpustat = this_cpu_ptr(ca->cpustat);
284 kcpustat->cpustat[index] += val;
Li Zefan5f40d802013-03-29 14:37:43 +0800285 ca = __parent_ca(ca);
Li Zefan1966aaf2013-03-29 14:37:06 +0800286 }
287 rcu_read_unlock();
288}
289
Li Zefandbe4b412013-03-29 14:36:55 +0800290void __init cpuacct_init(void)
291{
292 root_cpuacct.cpustat = &kernel_cpustat;
293 root_cpuacct.cpuusage = alloc_percpu(u64);
294 BUG_ON(!root_cpuacct.cpuusage); /* Too early, not expected to fail */
295}
296
Li Zefan2e76c242013-03-29 14:36:31 +0800297struct cgroup_subsys cpuacct_subsys = {
298 .name = "cpuacct",
299 .css_alloc = cpuacct_css_alloc,
300 .css_free = cpuacct_css_free,
301 .subsys_id = cpuacct_subsys_id,
302 .base_cftypes = files,
303};