Skip to content

Commit ef12fef

Browse files
Bharata B RaoIngo Molnar
authored andcommitted
cpuacct: add per-cgroup utime/stime statistics
Add per-cgroup cpuacct controller statistics like the system and user time consumed by the group of tasks. Changelog: v7 - Changed the name of the statistic from utime to user and from stime to system so that in future we could easily add other statistics like irq, softirq, steal times etc easily. v6 - Fixed a bug in the error path of cpuacct_create() (pointed by Li Zefan). v5 - In cpuacct_stats_show(), use cputime64_to_clock_t() since we are operating on a 64bit variable here. v4 - Remove comments in cpuacct_update_stats() which explained why rcu_read_lock() was needed (as per Peter Zijlstra's review comments). - Don't say that percpu_counter_read() is broken in Documentation/cpuacct.txt as per KAMEZAWA Hiroyuki's review comments. v3 - Fix a small race in the cpuacct hierarchy walk. v2 - stime and utime now exported in clock_t units instead of msecs. - Addressed the code review comments from Balbir and Li Zefan. - Moved to -tip tree. v1 - Moved the stime/utime accounting to cpuacct controller. Earlier versions - http://lkml.org/lkml/2009/2/25/129 Signed-off-by: Bharata B Rao <bharata@linux.vnet.ibm.com> Signed-off-by: Balaji Rao <balajirrao@gmail.com> Cc: Dhaval Giani <dhaval@linux.vnet.ibm.com> Cc: Paul Menage <menage@google.com> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com> Reviewed-by: Li Zefan <lizf@cn.fujitsu.com> Acked-by: Peter Zijlstra <a.p.zijlstra@chello.nl> Acked-by: Balbir Singh <balbir@linux.vnet.ibm.com> Tested-by: Balbir Singh <balbir@linux.vnet.ibm.com> LKML-Reference: <20090331043222.GA4093@in.ibm.com> Signed-off-by: Ingo Molnar <mingo@elte.hu>
1 parent c5f8d99 commit ef12fef

2 files changed

Lines changed: 99 additions & 6 deletions

File tree

‎Documentation/cgroups/cpuacct.txt‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,21 @@ The above steps create a new group g1 and move the current shell
3030
process (bash) into it. CPU time consumed by this bash and its children
3131
can be obtained from g1/cpuacct.usage and the same is accumulated in
3232
/cgroups/cpuacct.usage also.
33+
34+
cpuacct.stat file lists a few statistics which further divide the
35+
CPU time obtained by the cgroup into user and system times. Currently
36+
the following statistics are supported:
37+
38+
user: Time spent by tasks of the cgroup in user mode.
39+
system: Time spent by tasks of the cgroup in kernel mode.
40+
41+
user and system are in USER_HZ unit.
42+
43+
cpuacct controller uses percpu_counter interface to collect user and
44+
system times. This has two side effects:
45+
46+
- It is theoretically possible to see wrong values for user and system times.
47+
This is because percpu_counter_read() on 32bit systems isn't safe
48+
against concurrent writes.
49+
- It is possible to see slightly outdated values for user and system times
50+
due to the batch processing nature of percpu_counter.

‎kernel/sched.c‎

Lines changed: 81 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1393,10 +1393,22 @@ iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
13931393
struct rq_iterator *iterator);
13941394
#endif
13951395

1396+
/* Time spent by the tasks of the cpu accounting group executing in ... */
1397+
enum cpuacct_stat_index {
1398+
CPUACCT_STAT_USER, /* ... user mode */
1399+
CPUACCT_STAT_SYSTEM, /* ... kernel mode */
1400+
1401+
CPUACCT_STAT_NSTATS,
1402+
};
1403+
13961404
#ifdef CONFIG_CGROUP_CPUACCT
13971405
static void cpuacct_charge(struct task_struct *tsk, u64 cputime);
1406+
static void cpuacct_update_stats(struct task_struct *tsk,
1407+
enum cpuacct_stat_index idx, cputime_t val);
13981408
#else
13991409
static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {}
1410+
static inline void cpuacct_update_stats(struct task_struct *tsk,
1411+
enum cpuacct_stat_index idx, cputime_t val) {}
14001412
#endif
14011413

14021414
static inline void inc_cpu_load(struct rq *rq, unsigned long load)
@@ -4236,6 +4248,8 @@ void account_user_time(struct task_struct *p, cputime_t cputime,
42364248
cpustat->nice = cputime64_add(cpustat->nice, tmp);
42374249
else
42384250
cpustat->user = cputime64_add(cpustat->user, tmp);
4251+
4252+
cpuacct_update_stats(p, CPUACCT_STAT_USER, cputime);
42394253
/* Account for user time used */
42404254
acct_update_integrals(p);
42414255
}
@@ -4297,6 +4311,8 @@ void account_system_time(struct task_struct *p, int hardirq_offset,
42974311
else
42984312
cpustat->system = cputime64_add(cpustat->system, tmp);
42994313

4314+
cpuacct_update_stats(p, CPUACCT_STAT_SYSTEM, cputime);
4315+
43004316
/* Account for system time used */
43014317
acct_update_integrals(p);
43024318
}
@@ -9539,6 +9555,7 @@ struct cpuacct {
95399555
struct cgroup_subsys_state css;
95409556
/* cpuusage holds pointer to a u64-type object on every cpu */
95419557
u64 *cpuusage;
9558+
struct percpu_counter cpustat[CPUACCT_STAT_NSTATS];
95429559
struct cpuacct *parent;
95439560
};
95449561

@@ -9563,28 +9580,43 @@ static struct cgroup_subsys_state *cpuacct_create(
95639580
struct cgroup_subsys *ss, struct cgroup *cgrp)
95649581
{
95659582
struct cpuacct *ca = kzalloc(sizeof(*ca), GFP_KERNEL);
9583+
int i;
95669584

95679585
if (!ca)
9568-
return ERR_PTR(-ENOMEM);
9586+
goto out;
95699587

95709588
ca->cpuusage = alloc_percpu(u64);
9571-
if (!ca->cpuusage) {
9572-
kfree(ca);
9573-
return ERR_PTR(-ENOMEM);
9574-
}
9589+
if (!ca->cpuusage)
9590+
goto out_free_ca;
9591+
9592+
for (i = 0; i < CPUACCT_STAT_NSTATS; i++)
9593+
if (percpu_counter_init(&ca->cpustat[i], 0))
9594+
goto out_free_counters;
95759595

95769596
if (cgrp->parent)
95779597
ca->parent = cgroup_ca(cgrp->parent);
95789598

95799599
return &ca->css;
9600+
9601+
out_free_counters:
9602+
while (--i >= 0)
9603+
percpu_counter_destroy(&ca->cpustat[i]);
9604+
free_percpu(ca->cpuusage);
9605+
out_free_ca:
9606+
kfree(ca);
9607+
out:
9608+
return ERR_PTR(-ENOMEM);
95809609
}
95819610

95829611
/* destroy an existing cpu accounting group */
95839612
static void
95849613
cpuacct_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
95859614
{
95869615
struct cpuacct *ca = cgroup_ca(cgrp);
9616+
int i;
95879617

9618+
for (i = 0; i < CPUACCT_STAT_NSTATS; i++)
9619+
percpu_counter_destroy(&ca->cpustat[i]);
95889620
free_percpu(ca->cpuusage);
95899621
kfree(ca);
95909622
}
@@ -9671,6 +9703,25 @@ static int cpuacct_percpu_seq_read(struct cgroup *cgroup, struct cftype *cft,
96719703
return 0;
96729704
}
96739705

9706+
static const char *cpuacct_stat_desc[] = {
9707+
[CPUACCT_STAT_USER] = "user",
9708+
[CPUACCT_STAT_SYSTEM] = "system",
9709+
};
9710+
9711+
static int cpuacct_stats_show(struct cgroup *cgrp, struct cftype *cft,
9712+
struct cgroup_map_cb *cb)
9713+
{
9714+
struct cpuacct *ca = cgroup_ca(cgrp);
9715+
int i;
9716+
9717+
for (i = 0; i < CPUACCT_STAT_NSTATS; i++) {
9718+
s64 val = percpu_counter_read(&ca->cpustat[i]);
9719+
val = cputime64_to_clock_t(val);
9720+
cb->fill(cb, cpuacct_stat_desc[i], val);
9721+
}
9722+
return 0;
9723+
}
9724+
96749725
static struct cftype files[] = {
96759726
{
96769727
.name = "usage",
@@ -9681,7 +9732,10 @@ static struct cftype files[] = {
96819732
.name = "usage_percpu",
96829733
.read_seq_string = cpuacct_percpu_seq_read,
96839734
},
9684-
9735+
{
9736+
.name = "stat",
9737+
.read_map = cpuacct_stats_show,
9738+
},
96859739
};
96869740

96879741
static int cpuacct_populate(struct cgroup_subsys *ss, struct cgroup *cgrp)
@@ -9716,6 +9770,27 @@ static void cpuacct_charge(struct task_struct *tsk, u64 cputime)
97169770
rcu_read_unlock();
97179771
}
97189772

9773+
/*
9774+
* Charge the system/user time to the task's accounting group.
9775+
*/
9776+
static void cpuacct_update_stats(struct task_struct *tsk,
9777+
enum cpuacct_stat_index idx, cputime_t val)
9778+
{
9779+
struct cpuacct *ca;
9780+
9781+
if (unlikely(!cpuacct_subsys.active))
9782+
return;
9783+
9784+
rcu_read_lock();
9785+
ca = task_ca(tsk);
9786+
9787+
do {
9788+
percpu_counter_add(&ca->cpustat[idx], val);
9789+
ca = ca->parent;
9790+
} while (ca);
9791+
rcu_read_unlock();
9792+
}
9793+
97199794
struct cgroup_subsys cpuacct_subsys = {
97209795
.name = "cpuacct",
97219796
.create = cpuacct_create,

0 commit comments

Comments
 (0)