Skip to contentSkip to conversation

cpuacct: per-cgroup utime/stime statistics - v2

RFC PATCH In review Bharata B Rao 3 participants 8 messages Mar 12, 2009 – Mar 17, 2009

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 cpuacct: per-cgroup utime/stime statistics - v2

Here is the 2nd version of the cpuacct statistics patch. Copying linux-arch list also this time to check if archs which define CONFIG_VIRT_CPU_ACCOUNTING are ok with this change.

Changes for 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:
- http://lkml.org/lkml/2009/3/10/150

cpuacct: Add stime and utime statistics

Add per-cgroup cpuacct controller statistics like the system and user time consumed by the group of tasks.

---
 Documentation/cgroups/cpuacct.txt |   17 +++++++
 kernel/sched.c                    |   86 +++++++++++++++++++++++++++++++++++---
 2 files changed, 97 insertions(+), 6 deletions(-)
Documentation/cgroups/cpuacct.txt +17 -0
@@ -30,3 +30,20 @@ The above steps create a new group g1 an
 process (bash) into it. CPU time consumed by this bash and its children
 can be obtained from g1/cpuacct.usage and the same is accumulated in
 /cgroups/cpuacct.usage also.
+
+cpuacct.stat file lists a few statistics which further divide the
+CPU time obtained by the cgroup into user and system times. Currently
+the following statistics are supported:
+
+utime: Time spent by tasks of the cgroup in user mode.
+stime: Time spent by tasks of the cgroup in kernel mode.
+
+utime and stime are in USER_HZ unit.
+
+cpuacct controller uses percpu_counter interface to collect utime and
+stime. This causes two side effects:
+
+- It is theoritically possible to see wrong values for stime and utime.
+  This is because percpu_counter_read() on 32bit systems is broken.
+- It is possible to see slightly outdated values for stime and utime
+  due to the batch processing nature of percpu_counter.
kernel/sched.c +80 -6

154 lines not on this page · raw message

#1 lore raw
Quoted 61 lines · On Thu, 12 Mar 2009 16:39:24 +0530 Bharata B Rao <[email protected]>…

Here is the 2nd version of the cpuacct statistics patch. Copying linux-arch list also this time to check if archs which define CONFIG_VIRT_CPU_ACCOUNTING are ok with this change.

Changes for 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:
- http://lkml.org/lkml/2009/3/10/150

cpuacct: Add stime and utime statistics

Add per-cgroup cpuacct controller statistics like the system and user time consumed by the group of tasks.

---
 Documentation/cgroups/cpuacct.txt |   17 +++++++
 kernel/sched.c                    |   86 +++++++++++++++++++++++++++++++++++---
 2 files changed, 97 insertions(+), 6 deletions(-)
Documentation/cgroups/cpuacct.txt +17 -0
@@ -30,3 +30,20 @@ The above steps create a new group g1 an
 process (bash) into it. CPU time consumed by this bash and its children
 can be obtained from g1/cpuacct.usage and the same is accumulated in
 /cgroups/cpuacct.usage also.
+
+cpuacct.stat file lists a few statistics which further divide the
+CPU time obtained by the cgroup into user and system times. Currently
+the following statistics are supported:
+
+utime: Time spent by tasks of the cgroup in user mode.
+stime: Time spent by tasks of the cgroup in kernel mode.
+
+utime and stime are in USER_HZ unit.
+
+cpuacct controller uses percpu_counter interface to collect utime and
+stime. This causes two side effects:
+
+- It is theoritically possible to see wrong values for stime and utime.
+  This is because percpu_counter_read() on 32bit systems is broken.
+- It is possible to see slightly outdated values for stime and utime
+  due to the batch processing nature of percpu_counter.
kernel/sched.c +7 -0
@@ -1434,10 +1434,22 @@ iter_move_one_task(struct rq *this_rq, i
 		   struct rq_iterator *iterator);
 #endif
 
+/* Time spent by the tasks of the cpu accounting group executing in ... */
+enum cpuacct_stat_index {
+	CPUACCT_STAT_UTIME,	/* ... user mode */
+	CPUACCT_STAT_STIME,	/* ... kernel mode */
+
+	CPUACCT_STAT_NSTATS,
+};

kernel/sched.c iter_move_one_task(struct rq *this_rq, i · line 1453

Quoted 10 lines
kernel/sched.c +5 -0
+
 #ifdef CONFIG_CGROUP_CPUACCT
 static void cpuacct_charge(struct task_struct *tsk, u64 cputime);
+static void cpuacct_update_stats(struct task_struct *tsk,
+		enum cpuacct_stat_index idx, cputime_t val);
 #else
 static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {}
+static void cpuacct_update_stats(struct task_struct *tsk,
+		enum cpuacct_stat_index idx, cputime_t val) {}
 #endif

"static inline"
if no "inline", the complier will show warning as
"this function is defined but not used.."

Quoted 128 lines
kernel/sched.c +58 -6
 static inline void inc_cpu_load(struct rq *rq, unsigned long load)
@@ -4379,6 +4391,8 @@ void account_user_time(struct task_struc
 		cpustat->nice = cputime64_add(cpustat->nice, tmp);
 	else
 		cpustat->user = cputime64_add(cpustat->user, tmp);
+
+	cpuacct_update_stats(p, CPUACCT_STAT_UTIME, cputime);
 	/* Account for user time used */
 	acct_update_integrals(p);
 }
@@ -4440,6 +4454,8 @@ void account_system_time(struct task_str
 	else
 		cpustat->system = cputime64_add(cpustat->system, tmp);
 
+	cpuacct_update_stats(p, CPUACCT_STAT_STIME, cputime);
+
 	/* Account for system time used */
 	acct_update_integrals(p);
 }
@@ -9723,6 +9739,7 @@ struct cpuacct {
 	struct cgroup_subsys_state css;
 	/* cpuusage holds pointer to a u64-type object on every cpu */
 	u64 *cpuusage;
+	struct percpu_counter cpustat[CPUACCT_STAT_NSTATS];
 	struct cpuacct *parent;
 };
 
@@ -9747,20 +9764,33 @@ static struct cgroup_subsys_state *cpuac
 	struct cgroup_subsys *ss, struct cgroup *cgrp)
 {
 	struct cpuacct *ca = kzalloc(sizeof(*ca), GFP_KERNEL);
+	int i;
 
 	if (!ca)
-		return ERR_PTR(-ENOMEM);
+		goto out;
 
 	ca->cpuusage = alloc_percpu(u64);
-	if (!ca->cpuusage) {
-		kfree(ca);
-		return ERR_PTR(-ENOMEM);
-	}
+	if (!ca->cpuusage)
+		goto out_free_ca;
+
+	for (i = 0; i < CPUACCT_STAT_NSTATS; i++)
+		if (percpu_counter_init(&ca->cpustat[i], 0))
+			goto out_free_counters;
 
 	if (cgrp->parent)
 		ca->parent = cgroup_ca(cgrp->parent);
 
 	return &ca->css;
+
+out_free_counters:
+	i--;
+	while (i-- >= 0)
+		percpu_counter_destroy(&ca->cpustat[i]);
+	free_percpu(ca->cpuusage);
+out_free_ca:
+	kfree(ca);
+out:
+	return ERR_PTR(-ENOMEM);
 }
 
 /* destroy an existing cpu accounting group */
@@ -9768,7 +9798,10 @@ static void
 cpuacct_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
 {
 	struct cpuacct *ca = cgroup_ca(cgrp);
+	int i;
 
+	for (i = 0; i < CPUACCT_STAT_NSTATS; i++)
+		percpu_counter_destroy(&ca->cpustat[i]);
 	free_percpu(ca->cpuusage);
 	kfree(ca);
 }
@@ -9855,6 +9888,25 @@ static int cpuacct_percpu_seq_read(struc
 	return 0;
 }
 
+static const char *cpuacct_stat_desc[] = {
+	[CPUACCT_STAT_UTIME] = "utime",
+	[CPUACCT_STAT_STIME] = "stime",
+};
+
+static int cpuacct_stats_show(struct cgroup *cgrp, struct cftype *cft,
+		struct cgroup_map_cb *cb)
+{
+	struct cpuacct *ca = cgroup_ca(cgrp);
+	int i;
+
+	for (i = 0; i < CPUACCT_STAT_NSTATS; i++) {
+		s64 val = percpu_counter_read(&ca->cpustat[i]);
+		val = cputime_to_clock_t(val);
+		cb->fill(cb, cpuacct_stat_desc[i], val);
+	}
+	return 0;
+}
+
 static struct cftype files[] = {
 	{
 		.name = "usage",
@@ -9865,7 +9917,10 @@ static struct cftype files[] = {
 		.name = "usage_percpu",
 		.read_seq_string = cpuacct_percpu_seq_read,
 	},
-
+	{
+		.name = "stat",
+		.read_map = cpuacct_stats_show,
+	},
 };
 
 static int cpuacct_populate(struct cgroup_subsys *ss, struct cgroup *cgrp)
@@ -9895,6 +9950,25 @@ static void cpuacct_charge(struct task_s
 	}
 }
 
+/*
+ * Account the system/user time to the task's accounting group.
+ */
+static void cpuacct_update_stats(struct task_struct *tsk,
+		enum cpuacct_stat_index idx, cputime_t val)
+{
+	struct cpuacct *ca;
+
+	if (unlikely(!cpuacct_subsys.active))

kernel/sched.c static void cpuacct_charge(struct task_s · line 9971

Quoted 10 lines
kernel/sched.c +10 -0
+		return;
+
+	ca = task_ca(tsk);
+
+	do {
+		percpu_counter_add(&ca->cpustat[idx], val);
+		ca = ca->parent;
+	} while (ca);
+}
+

IIUC, to make sure accessing "ca" to be safe, we need some condition. (task_lock() or some other.....

Could you add "why this is safe ?" as comment ?

-Kame

Quoted 9 lines
kernel/sched.c +0 -1
 struct cgroup_subsys cpuacct_subsys = {
 	.name = "cpuacct",
 	.create = cpuacct_create,
--

To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

#2 lore raw
Quoted 15 lines · KAMEZAWA Hiroyuki

kernel/sched.c iter_move_one_task(struct rq *this_rq, i · line 1453

Quoted 9 lines · On Thu, 12 Mar 2009 16:39:24 +0530 Bharata B Rao <[email protected]>…

#ifdef CONFIG_CGROUP_CPUACCT
static void cpuacct_charge(struct task_struct *tsk, u64 cputime);

kernel/sched.c +4 -0
+static void cpuacct_update_stats(struct task_struct *tsk,
+		enum cpuacct_stat_index idx, cputime_t val);
 #else
 static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {}
+static void cpuacct_update_stats(struct task_struct *tsk,
+		enum cpuacct_stat_index idx, cputime_t val) {}
 #endif

"static inline"
if no "inline", the complier will show warning as
"this function is defined but not used.."

Ok.

Quoted 15 lines
Quoted 15 lines
kernel/sched.c +14 -0
+/*
+ * Account the system/user time to the task's accounting group.
+ */
+static void cpuacct_update_stats(struct task_struct *tsk,
+		enum cpuacct_stat_index idx, cputime_t val)
+{
+	struct cpuacct *ca;
+
+	if (unlikely(!cpuacct_subsys.active))
+		return;
+
+	ca = task_ca(tsk);
+
+	do {
Quoted 8 lines

kernel/sched.c static void cpuacct_charge(struct task_s · line 9971

Quoted 5 lines
kernel/sched.c +5 -0
+		percpu_counter_add(&ca->cpustat[idx], val);
+		ca = ca->parent;
+	} while (ca);
+}
+

IIUC, to make sure accessing "ca" to be safe, we need some condition. (task_lock() or some other.....

task_lock() protects tsk->cgroups->subsys[]. So can we hold task_lock() to protect this walk ? But we do this cpuacct hierarchy walk for the current task here. So can a current task's ca or ca's parents disappear from under us ?

Regards,
Bharata.

#3 lore raw
Quoted 20 lines · On Mon, 16 Mar 2009 10:07:54 +0530 Bharata B Rao <[email protected]>…
Quoted 11 lines
Quoted 11 lines
kernel/sched.c +11 -0
+static void cpuacct_update_stats(struct task_struct *tsk,
+		enum cpuacct_stat_index idx, cputime_t val)
+{
+	struct cpuacct *ca;
+
+	if (unlikely(!cpuacct_subsys.active))
+		return;
+
+	ca = task_ca(tsk);
+
+	do {
Quoted 8 lines

kernel/sched.c static void cpuacct_charge(struct task_s · line 9971

Quoted 5 lines
kernel/sched.c +5 -0
+		percpu_counter_add(&ca->cpustat[idx], val);
+		ca = ca->parent;
+	} while (ca);
+}
+

IIUC, to make sure accessing "ca" to be safe, we need some condition. (task_lock() or some other.....

Quoted 2 lines

task_lock() protects tsk->cgroups->subsys[]. So can we hold task_lock() to protect this walk ?

If necessary (see below)

Quoted 3 lines

But we do this cpuacct hierarchy walk for the
current task here. So can a current task's ca or ca's parents disappear from under us ?

Ok, followings are correct, I think.
1. All updates are called under preempt-disabled.
2. If "ca" is pointed by task, "disappear" will not happen.
3. if one of children is alive, the parent is alive.
4. "attach" can happen and tsk may be moved to other cgroup, because we don't take task_lock.
So, ca = task_ca() may not be correct one.

What we should explain here is How we consider "4", here.

Quoted 6 lines
Quoted 6 lines

kernel/sched.c static void cpuacct_charge(struct task_s · line 9969

Quoted 6 lines
kernel/sched.c +6 -0
+	ca = task_ca(tsk);
+ ----------------------------------(*1)
+	do {
+		percpu_counter_add(&ca->cpustat[idx], val);
+		ca = ca->parent;
+	} while (ca);
        ----------------------------------(*2)

About accounting, we don't have any problem even if (*2) is different from (*1). But, considering "ca" can be empty cgroup after (*1), in _theory_, we cannot say "ca" is valid pointer after (*1).

Hmm, if necessary, adding rcu_read_lock() before (*1) is reasonable ? (I say "if necessary" because I'm not sure.)

Thanks,
-Kame

#4 lore raw
In reply to Bharata B Rao
Quoted 22 lines
Quoted 13 lines
Quoted 13 lines
kernel/sched.c +13 -0
+/*
+ * Account the system/user time to the task's accounting group.
+ */
+static void cpuacct_update_stats(struct task_struct *tsk,
+		enum cpuacct_stat_index idx, cputime_t val)
+{
+	struct cpuacct *ca;
+
+	if (unlikely(!cpuacct_subsys.active))
+		return;
+
+	ca = task_ca(tsk);
+
Quoted 8 lines

kernel/sched.c static void cpuacct_charge(struct task_s · line 9971

Quoted 6 lines
kernel/sched.c +6 -0
+	do {
+		percpu_counter_add(&ca->cpustat[idx], val);
+		ca = ca->parent;
+	} while (ca);
+}
+

IIUC, to make sure accessing "ca" to be safe, we need some condition. (task_lock() or some other.....

Quoted 5 lines

task_lock() protects tsk->cgroups->subsys[]. So can we hold task_lock() to protect this walk ? But we do this cpuacct hierarchy walk for the current task here. So can a current task's ca or ca's parents disappear from under us ?

task_ca() should be protected by task_lock() or rcu_read_lock(), otherwise there is a very small race:

ca = task_ca(tsk)
move @tsk to another cgroup rmdir old_cgrp (thus ca is freed)
ca->cpustat <--- accessing freed memory

As KAMEZAWA-san said all updates are called under preempt-disabled, and classic and tree rcu's rcu_read_lock does preempt disable only, so above code is ok, except for rcupreempt.

#5 lore raw
Quoted 31 lines · Li Zefan
Quoted 22 lines
Quoted 13 lines
Quoted 13 lines
kernel/sched.c +13 -0
+/*
+ * Account the system/user time to the task's accounting group.
+ */
+static void cpuacct_update_stats(struct task_struct *tsk,
+		enum cpuacct_stat_index idx, cputime_t val)
+{
+	struct cpuacct *ca;
+
+	if (unlikely(!cpuacct_subsys.active))
+		return;
+
+	ca = task_ca(tsk);
+
Quoted 8 lines

kernel/sched.c static void cpuacct_charge(struct task_s · line 9971

Quoted 6 lines
kernel/sched.c +6 -0
+	do {
+		percpu_counter_add(&ca->cpustat[idx], val);
+		ca = ca->parent;
+	} while (ca);
+}
+

IIUC, to make sure accessing "ca" to be safe, we need some condition. (task_lock() or some other.....

Quoted 5 lines

task_lock() protects tsk->cgroups->subsys[]. So can we hold task_lock() to protect this walk ? But we do this cpuacct hierarchy walk for the current task here. So can a current task's ca or ca's parents disappear from under us ?

task_ca() should be protected by task_lock() or rcu_read_lock(), otherwise there is a very small race:

Quoted 8 lines

ca = task_ca(tsk)
move @tsk to another cgroup rmdir old_cgrp (thus ca is freed)
ca->cpustat <--- accessing freed memory

As KAMEZAWA-san said all updates are called under preempt-disabled, and classic and tree rcu's rcu_read_lock does preempt disable only, so above code is ok, except for rcupreempt.

So I will protect task_ca() and ca hierarchy walk with explicit rcu_read_lock() to be fully safe.

By the same logic, hierarchy walk in cpuacct_charge() is also not safe with rcupreempt. It is under preempt disabled section due to rq->lock. Does cpuacct_charge() also need a fix then ?

Regards,
Bharata.

#6 lore raw
Quoted 35 lines · Bharata B Rao
Quoted 34 lines · Li Zefan
Quoted 21 lines
Quoted 13 lines
Quoted 13 lines
kernel/sched.c +13 -0
+/*
+ * Account the system/user time to the task's accounting group.
+ */
+static void cpuacct_update_stats(struct task_struct *tsk,
+		enum cpuacct_stat_index idx, cputime_t val)
+{
+	struct cpuacct *ca;
+
+	if (unlikely(!cpuacct_subsys.active))
+		return;
+
+	ca = task_ca(tsk);
+
Quoted 8 lines

kernel/sched.c static void cpuacct_charge(struct task_s · line 9971

Quoted 6 lines
kernel/sched.c +6 -0
+	do {
+		percpu_counter_add(&ca->cpustat[idx], val);
+		ca = ca->parent;
+	} while (ca);
+}
+

IIUC, to make sure accessing "ca" to be safe, we need some condition. (task_lock() or some other.....

Quoted 5 lines

task_lock() protects tsk->cgroups->subsys[]. So can we hold task_lock() to protect this walk ? But we do this cpuacct hierarchy walk for the current task here. So can a current task's ca or ca's parents disappear from under us ?

task_ca() should be protected by task_lock() or rcu_read_lock(), otherwise there is a very small race:

ca = task_ca(tsk)
move @tsk to another cgroup rmdir old_cgrp (thus ca is freed)
ca->cpustat <--- accessing freed memory

Quoted 7 lines
Quoted 3 lines

As KAMEZAWA-san said all updates are called under preempt-disabled, and classic and tree rcu's rcu_read_lock does preempt disable only, so above code is ok, except for rcupreempt.

So I will protect task_ca() and ca hierarchy walk with explicit rcu_read_lock() to be fully safe.

either:

rcu_read_lock();
ca = task_ca(tsk);

do {
	percpu_counter_add(&ca->cpustat[idx], val);
	ca = ca->parent;
} while (ca);

rcu_read_unlock();

or:

rcu_read_lock();
ca = task_ca(tsk);
css_get(&ca->css);
rcu_read_unlock();

do {
	percpu_counter_add(&ca->cpustat[idx], val);
	ca = ca->parent;
} while (ca);

css_put(&ca->css);

which is more efficient?

Quoted 4 lines

By the same logic, hierarchy walk in cpuacct_charge() is also not safe with rcupreempt. It is under preempt disabled section due to rq->lock. Does cpuacct_charge() also need a fix then ?

I guess so..

#7 lore raw
Quoted 60 lines · Li Zefan
Quoted 35 lines · Bharata B Rao
Quoted 34 lines · Li Zefan
Quoted 21 lines
Quoted 13 lines
Quoted 13 lines
kernel/sched.c +13 -0
+/*
+ * Account the system/user time to the task's accounting group.
+ */
+static void cpuacct_update_stats(struct task_struct *tsk,
+		enum cpuacct_stat_index idx, cputime_t val)
+{
+	struct cpuacct *ca;
+
+	if (unlikely(!cpuacct_subsys.active))
+		return;
+
+	ca = task_ca(tsk);
+
Quoted 8 lines

kernel/sched.c static void cpuacct_charge(struct task_s · line 9971

Quoted 6 lines
kernel/sched.c +6 -0
+	do {
+		percpu_counter_add(&ca->cpustat[idx], val);
+		ca = ca->parent;
+	} while (ca);
+}
+

IIUC, to make sure accessing "ca" to be safe, we need some condition. (task_lock() or some other.....

Quoted 5 lines

task_lock() protects tsk->cgroups->subsys[]. So can we hold task_lock() to protect this walk ? But we do this cpuacct hierarchy walk for the current task here. So can a current task's ca or ca's parents disappear from under us ?

task_ca() should be protected by task_lock() or rcu_read_lock(), otherwise there is a very small race:

ca = task_ca(tsk)
move @tsk to another cgroup rmdir old_cgrp (thus ca is freed)
ca->cpustat <--- accessing freed memory

Quoted 7 lines
Quoted 3 lines

As KAMEZAWA-san said all updates are called under preempt-disabled, and classic and tree rcu's rcu_read_lock does preempt disable only, so above code is ok, except for rcupreempt.

So I will protect task_ca() and ca hierarchy walk with explicit rcu_read_lock() to be fully safe.

either:

rcu_read_lock();
ca = task_ca(tsk);

do {
	percpu_counter_add(&ca->cpustat[idx], val);
	ca = ca->parent;
} while (ca);

rcu_read_unlock();

or:

rcu_read_lock();
ca = task_ca(tsk);
css_get(&ca->css);
rcu_read_unlock();

Quoted 7 lines
do {
	percpu_counter_add(&ca->cpustat[idx], val);
	ca = ca->parent;
} while (ca);

css_put(&ca->css);

which is more efficient?

Went with the first one in my next version of cpuacct stats patch as I felt it is better than the 2nd version which needs 2 atomic operations.

Quoted 7 lines
Quoted 5 lines

By the same logic, hierarchy walk in cpuacct_charge() is also not safe with rcupreempt. It is under preempt disabled section due to rq->lock. Does cpuacct_charge() also need a fix then ?

I guess so..

Sent a separate fix for this.

Thanks for your review and suggestions.

Regards,
Bharata.