1 #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>
14 * CPU accounting code for task groups.
16 * Based on the work by Paul Menage (menage@google.com) and Balbir Singh
17 * (balbir@in.ibm.com).
20 /* Time spent by the tasks of the cpu accounting group executing in ... */
21 enum cpuacct_stat_index
{
22 CPUACCT_STAT_USER
, /* ... user mode */
23 CPUACCT_STAT_SYSTEM
, /* ... kernel mode */
28 enum cpuacct_usage_index
{
29 CPUACCT_USAGE_USER
, /* ... user mode */
30 CPUACCT_USAGE_SYSTEM
, /* ... kernel mode */
32 CPUACCT_USAGE_NRUSAGE
,
35 struct cpuacct_usage
{
36 u64 usages
[CPUACCT_USAGE_NRUSAGE
];
39 /* track cpu usage of a group of tasks and its child groups */
41 struct cgroup_subsys_state css
;
42 /* cpuusage holds pointer to a u64-type object on every cpu */
43 struct cpuacct_usage __percpu
*cpuusage
;
44 struct kernel_cpustat __percpu
*cpustat
;
47 static inline struct cpuacct
*css_ca(struct cgroup_subsys_state
*css
)
49 return css
? container_of(css
, struct cpuacct
, css
) : NULL
;
52 /* return cpu accounting group to which this task belongs */
53 static inline struct cpuacct
*task_ca(struct task_struct
*tsk
)
55 return css_ca(task_css(tsk
, cpuacct_cgrp_id
));
58 static inline struct cpuacct
*parent_ca(struct cpuacct
*ca
)
60 return css_ca(ca
->css
.parent
);
63 static DEFINE_PER_CPU(struct cpuacct_usage
, root_cpuacct_cpuusage
);
64 static struct cpuacct root_cpuacct
= {
65 .cpustat
= &kernel_cpustat
,
66 .cpuusage
= &root_cpuacct_cpuusage
,
69 /* create a new cpu accounting group */
70 static struct cgroup_subsys_state
*
71 cpuacct_css_alloc(struct cgroup_subsys_state
*parent_css
)
76 return &root_cpuacct
.css
;
78 ca
= kzalloc(sizeof(*ca
), GFP_KERNEL
);
82 ca
->cpuusage
= alloc_percpu(struct cpuacct_usage
);
86 ca
->cpustat
= alloc_percpu(struct kernel_cpustat
);
88 goto out_free_cpuusage
;
93 free_percpu(ca
->cpuusage
);
97 return ERR_PTR(-ENOMEM
);
100 /* destroy an existing cpu accounting group */
101 static void cpuacct_css_free(struct cgroup_subsys_state
*css
)
103 struct cpuacct
*ca
= css_ca(css
);
105 free_percpu(ca
->cpustat
);
106 free_percpu(ca
->cpuusage
);
110 static u64
cpuacct_cpuusage_read(struct cpuacct
*ca
, int cpu
,
111 enum cpuacct_usage_index index
)
113 struct cpuacct_usage
*cpuusage
= per_cpu_ptr(ca
->cpuusage
, cpu
);
117 * We allow index == CPUACCT_USAGE_NRUSAGE here to read
120 BUG_ON(index
> CPUACCT_USAGE_NRUSAGE
);
124 * Take rq->lock to make 64-bit read safe on 32-bit platforms.
126 raw_spin_lock_irq(&cpu_rq(cpu
)->lock
);
129 if (index
== CPUACCT_USAGE_NRUSAGE
) {
133 for (i
= 0; i
< CPUACCT_USAGE_NRUSAGE
; i
++)
134 data
+= cpuusage
->usages
[i
];
136 data
= cpuusage
->usages
[index
];
140 raw_spin_unlock_irq(&cpu_rq(cpu
)->lock
);
146 static void cpuacct_cpuusage_write(struct cpuacct
*ca
, int cpu
, u64 val
)
148 struct cpuacct_usage
*cpuusage
= per_cpu_ptr(ca
->cpuusage
, cpu
);
153 * Take rq->lock to make 64-bit write safe on 32-bit platforms.
155 raw_spin_lock_irq(&cpu_rq(cpu
)->lock
);
158 for (i
= 0; i
< CPUACCT_USAGE_NRUSAGE
; i
++)
159 cpuusage
->usages
[i
] = val
;
162 raw_spin_unlock_irq(&cpu_rq(cpu
)->lock
);
166 /* return total cpu usage (in nanoseconds) of a group */
167 static u64
__cpuusage_read(struct cgroup_subsys_state
*css
,
168 enum cpuacct_usage_index index
)
170 struct cpuacct
*ca
= css_ca(css
);
171 u64 totalcpuusage
= 0;
174 for_each_possible_cpu(i
)
175 totalcpuusage
+= cpuacct_cpuusage_read(ca
, i
, index
);
177 return totalcpuusage
;
180 static u64
cpuusage_user_read(struct cgroup_subsys_state
*css
,
183 return __cpuusage_read(css
, CPUACCT_USAGE_USER
);
186 static u64
cpuusage_sys_read(struct cgroup_subsys_state
*css
,
189 return __cpuusage_read(css
, CPUACCT_USAGE_SYSTEM
);
192 static u64
cpuusage_read(struct cgroup_subsys_state
*css
, struct cftype
*cft
)
194 return __cpuusage_read(css
, CPUACCT_USAGE_NRUSAGE
);
197 static int cpuusage_write(struct cgroup_subsys_state
*css
, struct cftype
*cft
,
200 struct cpuacct
*ca
= css_ca(css
);
204 * Only allow '0' here to do a reset.
209 for_each_possible_cpu(cpu
)
210 cpuacct_cpuusage_write(ca
, cpu
, 0);
215 static int __cpuacct_percpu_seq_show(struct seq_file
*m
,
216 enum cpuacct_usage_index index
)
218 struct cpuacct
*ca
= css_ca(seq_css(m
));
222 for_each_possible_cpu(i
) {
223 percpu
= cpuacct_cpuusage_read(ca
, i
, index
);
224 seq_printf(m
, "%llu ", (unsigned long long) percpu
);
230 static int cpuacct_percpu_user_seq_show(struct seq_file
*m
, void *V
)
232 return __cpuacct_percpu_seq_show(m
, CPUACCT_USAGE_USER
);
235 static int cpuacct_percpu_sys_seq_show(struct seq_file
*m
, void *V
)
237 return __cpuacct_percpu_seq_show(m
, CPUACCT_USAGE_SYSTEM
);
240 static int cpuacct_percpu_seq_show(struct seq_file
*m
, void *V
)
242 return __cpuacct_percpu_seq_show(m
, CPUACCT_USAGE_NRUSAGE
);
245 static const char * const cpuacct_stat_desc
[] = {
246 [CPUACCT_STAT_USER
] = "user",
247 [CPUACCT_STAT_SYSTEM
] = "system",
250 static int cpuacct_stats_show(struct seq_file
*sf
, void *v
)
252 struct cpuacct
*ca
= css_ca(seq_css(sf
));
256 for_each_possible_cpu(cpu
) {
257 struct kernel_cpustat
*kcpustat
= per_cpu_ptr(ca
->cpustat
, cpu
);
258 val
+= kcpustat
->cpustat
[CPUTIME_USER
];
259 val
+= kcpustat
->cpustat
[CPUTIME_NICE
];
261 val
= cputime64_to_clock_t(val
);
262 seq_printf(sf
, "%s %lld\n", cpuacct_stat_desc
[CPUACCT_STAT_USER
], val
);
265 for_each_possible_cpu(cpu
) {
266 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
];
272 val
= cputime64_to_clock_t(val
);
273 seq_printf(sf
, "%s %lld\n", cpuacct_stat_desc
[CPUACCT_STAT_SYSTEM
], val
);
278 static struct cftype files
[] = {
281 .read_u64
= cpuusage_read
,
282 .write_u64
= cpuusage_write
,
285 .name
= "usage_user",
286 .read_u64
= cpuusage_user_read
,
290 .read_u64
= cpuusage_sys_read
,
293 .name
= "usage_percpu",
294 .seq_show
= cpuacct_percpu_seq_show
,
297 .name
= "usage_percpu_user",
298 .seq_show
= cpuacct_percpu_user_seq_show
,
301 .name
= "usage_percpu_sys",
302 .seq_show
= cpuacct_percpu_sys_seq_show
,
306 .seq_show
= cpuacct_stats_show
,
312 * charge this task's execution time to its accounting group.
314 * called with rq->lock held.
316 void cpuacct_charge(struct task_struct
*tsk
, u64 cputime
)
319 int index
= CPUACCT_USAGE_SYSTEM
;
320 struct pt_regs
*regs
= task_pt_regs(tsk
);
322 if (regs
&& user_mode(regs
))
323 index
= CPUACCT_USAGE_USER
;
327 for (ca
= task_ca(tsk
); ca
; ca
= parent_ca(ca
))
328 this_cpu_ptr(ca
->cpuusage
)->usages
[index
] += cputime
;
334 * Add user/system time to cpuacct.
336 * Note: it's the caller that updates the account of the root cgroup.
338 void cpuacct_account_field(struct task_struct
*tsk
, int index
, u64 val
)
343 for (ca
= task_ca(tsk
); ca
!= &root_cpuacct
; ca
= parent_ca(ca
))
344 this_cpu_ptr(ca
->cpustat
)->cpustat
[index
] += val
;
348 struct cgroup_subsys cpuacct_cgrp_subsys
= {
349 .css_alloc
= cpuacct_css_alloc
,
350 .css_free
= cpuacct_css_free
,
351 .legacy_cftypes
= files
,