• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * CPU accounting code for task groups.
4  *
5  * Based on the work by Paul Menage (menage@google.com) and Balbir Singh
6  * (balbir@in.ibm.com).
7  */
8 #include <asm/irq_regs.h>
9 #include "sched.h"
10 
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 */
15 
16 	CPUACCT_STAT_NSTATS,
17 };
18 
19 static const char * const cpuacct_stat_desc[] = {
20 	[CPUACCT_STAT_USER] = "user",
21 	[CPUACCT_STAT_SYSTEM] = "system",
22 };
23 
24 /* track CPU usage of a group of tasks and its child groups */
25 struct cpuacct {
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;
30 };
31 
css_ca(struct cgroup_subsys_state * css)32 static inline struct cpuacct *css_ca(struct cgroup_subsys_state *css)
33 {
34 	return css ? container_of(css, struct cpuacct, css) : NULL;
35 }
36 
37 /* Return CPU accounting group to which this task belongs */
task_ca(struct task_struct * tsk)38 static inline struct cpuacct *task_ca(struct task_struct *tsk)
39 {
40 	return css_ca(task_css(tsk, cpuacct_cgrp_id));
41 }
42 
parent_ca(struct cpuacct * ca)43 static inline struct cpuacct *parent_ca(struct cpuacct *ca)
44 {
45 	return css_ca(ca->css.parent);
46 }
47 
48 static DEFINE_PER_CPU(u64, root_cpuacct_cpuusage);
49 static struct cpuacct root_cpuacct = {
50 	.cpustat	= &kernel_cpustat,
51 	.cpuusage	= &root_cpuacct_cpuusage,
52 };
53 
54 /* Create a new CPU accounting group */
55 static struct cgroup_subsys_state *
cpuacct_css_alloc(struct cgroup_subsys_state * parent_css)56 cpuacct_css_alloc(struct cgroup_subsys_state *parent_css)
57 {
58 	struct cpuacct *ca;
59 
60 	if (!parent_css)
61 		return &root_cpuacct.css;
62 
63 	ca = kzalloc(sizeof(*ca), GFP_KERNEL);
64 	if (!ca)
65 		goto out;
66 
67 	ca->cpuusage = alloc_percpu(u64);
68 	if (!ca->cpuusage)
69 		goto out_free_ca;
70 
71 	ca->cpustat = alloc_percpu(struct kernel_cpustat);
72 	if (!ca->cpustat)
73 		goto out_free_cpuusage;
74 
75 	return &ca->css;
76 
77 out_free_cpuusage:
78 	free_percpu(ca->cpuusage);
79 out_free_ca:
80 	kfree(ca);
81 out:
82 	return ERR_PTR(-ENOMEM);
83 }
84 
85 /* Destroy an existing CPU accounting group */
cpuacct_css_free(struct cgroup_subsys_state * css)86 static void cpuacct_css_free(struct cgroup_subsys_state *css)
87 {
88 	struct cpuacct *ca = css_ca(css);
89 
90 	free_percpu(ca->cpustat);
91 	free_percpu(ca->cpuusage);
92 	kfree(ca);
93 }
94 
cpuacct_cpuusage_read(struct cpuacct * ca,int cpu,enum cpuacct_stat_index index)95 static u64 cpuacct_cpuusage_read(struct cpuacct *ca, int cpu,
96 				 enum cpuacct_stat_index index)
97 {
98 	u64 *cpuusage = per_cpu_ptr(ca->cpuusage, cpu);
99 	u64 *cpustat = per_cpu_ptr(ca->cpustat, cpu)->cpustat;
100 	u64 data;
101 
102 	/*
103 	 * We allow index == CPUACCT_STAT_NSTATS here to read
104 	 * the sum of usages.
105 	 */
106 	BUG_ON(index > CPUACCT_STAT_NSTATS);
107 
108 #ifndef CONFIG_64BIT
109 	/*
110 	 * Take rq->lock to make 64-bit read safe on 32-bit platforms.
111 	 */
112 	raw_spin_rq_lock_irq(cpu_rq(cpu));
113 #endif
114 
115 	switch (index) {
116 	case CPUACCT_STAT_USER:
117 		data = cpustat[CPUTIME_USER] + cpustat[CPUTIME_NICE];
118 		break;
119 	case CPUACCT_STAT_SYSTEM:
120 		data = cpustat[CPUTIME_SYSTEM] + cpustat[CPUTIME_IRQ] +
121 			cpustat[CPUTIME_SOFTIRQ];
122 		break;
123 	case CPUACCT_STAT_NSTATS:
124 		data = *cpuusage;
125 		break;
126 	}
127 
128 #ifndef CONFIG_64BIT
129 	raw_spin_rq_unlock_irq(cpu_rq(cpu));
130 #endif
131 
132 	return data;
133 }
134 
cpuacct_cpuusage_write(struct cpuacct * ca,int cpu)135 static void cpuacct_cpuusage_write(struct cpuacct *ca, int cpu)
136 {
137 	u64 *cpuusage = per_cpu_ptr(ca->cpuusage, cpu);
138 	u64 *cpustat = per_cpu_ptr(ca->cpustat, cpu)->cpustat;
139 
140 	/* Don't allow to reset global kernel_cpustat */
141 	if (ca == &root_cpuacct)
142 		return;
143 
144 #ifndef CONFIG_64BIT
145 	/*
146 	 * Take rq->lock to make 64-bit write safe on 32-bit platforms.
147 	 */
148 	raw_spin_rq_lock_irq(cpu_rq(cpu));
149 #endif
150 	*cpuusage = 0;
151 	cpustat[CPUTIME_USER] = cpustat[CPUTIME_NICE] = 0;
152 	cpustat[CPUTIME_SYSTEM] = cpustat[CPUTIME_IRQ] = 0;
153 	cpustat[CPUTIME_SOFTIRQ] = 0;
154 
155 #ifndef CONFIG_64BIT
156 	raw_spin_rq_unlock_irq(cpu_rq(cpu));
157 #endif
158 }
159 
160 /* Return total CPU usage (in nanoseconds) of a group */
__cpuusage_read(struct cgroup_subsys_state * css,enum cpuacct_stat_index index)161 static u64 __cpuusage_read(struct cgroup_subsys_state *css,
162 			   enum cpuacct_stat_index index)
163 {
164 	struct cpuacct *ca = css_ca(css);
165 	u64 totalcpuusage = 0;
166 	int i;
167 
168 	for_each_possible_cpu(i)
169 		totalcpuusage += cpuacct_cpuusage_read(ca, i, index);
170 
171 	return totalcpuusage;
172 }
173 
cpuusage_user_read(struct cgroup_subsys_state * css,struct cftype * cft)174 static u64 cpuusage_user_read(struct cgroup_subsys_state *css,
175 			      struct cftype *cft)
176 {
177 	return __cpuusage_read(css, CPUACCT_STAT_USER);
178 }
179 
cpuusage_sys_read(struct cgroup_subsys_state * css,struct cftype * cft)180 static u64 cpuusage_sys_read(struct cgroup_subsys_state *css,
181 			     struct cftype *cft)
182 {
183 	return __cpuusage_read(css, CPUACCT_STAT_SYSTEM);
184 }
185 
cpuusage_read(struct cgroup_subsys_state * css,struct cftype * cft)186 static u64 cpuusage_read(struct cgroup_subsys_state *css, struct cftype *cft)
187 {
188 	return __cpuusage_read(css, CPUACCT_STAT_NSTATS);
189 }
190 
cpuusage_write(struct cgroup_subsys_state * css,struct cftype * cft,u64 val)191 static int cpuusage_write(struct cgroup_subsys_state *css, struct cftype *cft,
192 			  u64 val)
193 {
194 	struct cpuacct *ca = css_ca(css);
195 	int cpu;
196 
197 	/*
198 	 * Only allow '0' here to do a reset.
199 	 */
200 	if (val)
201 		return -EINVAL;
202 
203 	for_each_possible_cpu(cpu)
204 		cpuacct_cpuusage_write(ca, cpu);
205 
206 	return 0;
207 }
208 
__cpuacct_percpu_seq_show(struct seq_file * m,enum cpuacct_stat_index index)209 static int __cpuacct_percpu_seq_show(struct seq_file *m,
210 				     enum cpuacct_stat_index index)
211 {
212 	struct cpuacct *ca = css_ca(seq_css(m));
213 	u64 percpu;
214 	int i;
215 
216 	for_each_possible_cpu(i) {
217 		percpu = cpuacct_cpuusage_read(ca, i, index);
218 		seq_printf(m, "%llu ", (unsigned long long) percpu);
219 	}
220 	seq_printf(m, "\n");
221 	return 0;
222 }
223 
cpuacct_percpu_user_seq_show(struct seq_file * m,void * V)224 static int cpuacct_percpu_user_seq_show(struct seq_file *m, void *V)
225 {
226 	return __cpuacct_percpu_seq_show(m, CPUACCT_STAT_USER);
227 }
228 
cpuacct_percpu_sys_seq_show(struct seq_file * m,void * V)229 static int cpuacct_percpu_sys_seq_show(struct seq_file *m, void *V)
230 {
231 	return __cpuacct_percpu_seq_show(m, CPUACCT_STAT_SYSTEM);
232 }
233 
cpuacct_percpu_seq_show(struct seq_file * m,void * V)234 static int cpuacct_percpu_seq_show(struct seq_file *m, void *V)
235 {
236 	return __cpuacct_percpu_seq_show(m, CPUACCT_STAT_NSTATS);
237 }
238 
cpuacct_all_seq_show(struct seq_file * m,void * V)239 static int cpuacct_all_seq_show(struct seq_file *m, void *V)
240 {
241 	struct cpuacct *ca = css_ca(seq_css(m));
242 	int index;
243 	int cpu;
244 
245 	seq_puts(m, "cpu");
246 	for (index = 0; index < CPUACCT_STAT_NSTATS; index++)
247 		seq_printf(m, " %s", cpuacct_stat_desc[index]);
248 	seq_puts(m, "\n");
249 
250 	for_each_possible_cpu(cpu) {
251 		seq_printf(m, "%d", cpu);
252 		for (index = 0; index < CPUACCT_STAT_NSTATS; index++)
253 			seq_printf(m, " %llu",
254 				   cpuacct_cpuusage_read(ca, cpu, index));
255 		seq_puts(m, "\n");
256 	}
257 	return 0;
258 }
259 
cpuacct_stats_show(struct seq_file * sf,void * v)260 static int cpuacct_stats_show(struct seq_file *sf, void *v)
261 {
262 	struct cpuacct *ca = css_ca(seq_css(sf));
263 	s64 val[CPUACCT_STAT_NSTATS];
264 	int cpu;
265 	int stat;
266 
267 	memset(val, 0, sizeof(val));
268 	for_each_possible_cpu(cpu) {
269 		u64 *cpustat = per_cpu_ptr(ca->cpustat, cpu)->cpustat;
270 
271 		val[CPUACCT_STAT_USER]   += cpustat[CPUTIME_USER];
272 		val[CPUACCT_STAT_USER]   += cpustat[CPUTIME_NICE];
273 		val[CPUACCT_STAT_SYSTEM] += cpustat[CPUTIME_SYSTEM];
274 		val[CPUACCT_STAT_SYSTEM] += cpustat[CPUTIME_IRQ];
275 		val[CPUACCT_STAT_SYSTEM] += cpustat[CPUTIME_SOFTIRQ];
276 	}
277 
278 	for (stat = 0; stat < CPUACCT_STAT_NSTATS; stat++) {
279 		seq_printf(sf, "%s %lld\n",
280 			   cpuacct_stat_desc[stat],
281 			   (long long)nsec_to_clock_t(val[stat]));
282 	}
283 
284 	return 0;
285 }
286 
287 static struct cftype files[] = {
288 	{
289 		.name = "usage",
290 		.read_u64 = cpuusage_read,
291 		.write_u64 = cpuusage_write,
292 	},
293 	{
294 		.name = "usage_user",
295 		.read_u64 = cpuusage_user_read,
296 	},
297 	{
298 		.name = "usage_sys",
299 		.read_u64 = cpuusage_sys_read,
300 	},
301 	{
302 		.name = "usage_percpu",
303 		.seq_show = cpuacct_percpu_seq_show,
304 	},
305 	{
306 		.name = "usage_percpu_user",
307 		.seq_show = cpuacct_percpu_user_seq_show,
308 	},
309 	{
310 		.name = "usage_percpu_sys",
311 		.seq_show = cpuacct_percpu_sys_seq_show,
312 	},
313 	{
314 		.name = "usage_all",
315 		.seq_show = cpuacct_all_seq_show,
316 	},
317 	{
318 		.name = "stat",
319 		.seq_show = cpuacct_stats_show,
320 	},
321 	{ }	/* terminate */
322 };
323 
324 /*
325  * charge this task's execution time to its accounting group.
326  *
327  * called with rq->lock held.
328  */
cpuacct_charge(struct task_struct * tsk,u64 cputime)329 void cpuacct_charge(struct task_struct *tsk, u64 cputime)
330 {
331 	unsigned int cpu = task_cpu(tsk);
332 	struct cpuacct *ca;
333 
334 	lockdep_assert_rq_held(cpu_rq(cpu));
335 
336 	for (ca = task_ca(tsk); ca; ca = parent_ca(ca))
337 		*per_cpu_ptr(ca->cpuusage, cpu) += cputime;
338 }
339 
340 /*
341  * Add user/system time to cpuacct.
342  *
343  * Note: it's the caller that updates the account of the root cgroup.
344  */
cpuacct_account_field(struct task_struct * tsk,int index,u64 val)345 void cpuacct_account_field(struct task_struct *tsk, int index, u64 val)
346 {
347 	struct cpuacct *ca;
348 
349 	rcu_read_lock();
350 	for (ca = task_ca(tsk); ca != &root_cpuacct; ca = parent_ca(ca))
351 		__this_cpu_add(ca->cpustat->cpustat[index], val);
352 	rcu_read_unlock();
353 }
354 
355 struct cgroup_subsys cpuacct_cgrp_subsys = {
356 	.css_alloc	= cpuacct_css_alloc,
357 	.css_free	= cpuacct_css_free,
358 	.legacy_cftypes	= files,
359 	.early_init	= true,
360 };
361