Skip to contentSkip to conversation

cpuacct: VIRT_CPU_ACCOUNTING don't prevent percpu cputime count

PATCH Superseded KOSAKI Motohiro 5 participants 9 messages Apr 30, 2009 – May 1, 2009

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 cpuacct: VIRT_CPU_ACCOUNTING don't prevent percpu cputime count

Changelog:
since v1
- use percpu_counter_sum() instead percpu_counter_read()

-------------------------------------
Subject: [PATCH v2] cpuacct: VIRT_CPU_ACCOUNTING don't prevent percpu cputime count

cpuacct_update_stats() is called at every tick updating. and it use percpu_counter for avoiding performance degression.

For archs which define VIRT_CPU_ACCOUNTING, every tick would result in >1000 units of cputime updates and since this is much much greater than percpu_batch_counter, we end up taking spinlock on every tick.

This patch change batch rule. now, any cpu can store "percpu_counter_bach * jiffies" cputime in per-cpu cache.
it mean this patch don't have behavior change if VIRT_CPU_ACCOUNTING=n.

Cc Bharata B Rao <[email protected]>
Cc Balaji Rao <[email protected]>
Cc Dhaval Giani <[email protected]>
Cc KAMEZAWA Hiroyuki <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Balbir Singh <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Martin Schwidefsky <[email protected]>
---
 kernel/sched.c |    8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
kernel/sched.c +6 -2
Index: b/kernel/sched.c
===================================================================
@@ -10221,6 +10221,7 @@ struct cpuacct {
 };
 
 struct cgroup_subsys cpuacct_subsys;
+static s32 cpuacct_batch;
 
 /* return cpu accounting group corresponding to this container */
 static inline struct cpuacct *cgroup_ca(struct cgroup *cgrp)
@@ -10250,6 +10251,9 @@ static struct cgroup_subsys_state *cpuac
 	if (!ca->cpuusage)
 		goto out_free_ca;
 
+	if (!cpuacct_batch)
+		cpuacct_batch = jiffies_to_cputime(percpu_counter_batch);
+
 	for (i = 0; i < CPUACCT_STAT_NSTATS; i++)
 		if (percpu_counter_init(&ca->cpustat[i], 0))
 			goto out_free_counters;
@@ -10376,7 +10380,7 @@ static int cpuacct_stats_show(struct cgr
 	int i;
 
 	for (i = 0; i < CPUACCT_STAT_NSTATS; i++) {
-		s64 val = percpu_counter_read(&ca->cpustat[i]);
+		s64 val = percpu_counter_sum(&ca->cpustat[i]);
 		val = cputime64_to_clock_t(val);
 		cb->fill(cb, cpuacct_stat_desc[i], val);
 	}
@@ -10446,7 +10450,7 @@ static void cpuacct_update_stats(struct 
 	ca = task_ca(tsk);
 
 	do {
-		percpu_counter_add(&ca->cpustat[idx], val);
+		__percpu_counter_add(&ca->cpustat[idx], val, cpuacct_batch);
 		ca = ca->parent;
 	} while (ca);
 	rcu_read_unlock();
Acked-by on v2 1/1
#1 lore raw
Quoted 67 lines · KOSAKI Motohiro

Changelog:
since v1
- use percpu_counter_sum() instead percpu_counter_read()

-------------------------------------
Subject: [PATCH v2] cpuacct: VIRT_CPU_ACCOUNTING don't prevent percpu cputime count

cpuacct_update_stats() is called at every tick updating. and it use percpu_counter for avoiding performance degression.

For archs which define VIRT_CPU_ACCOUNTING, every tick would result in >1000 units of cputime updates and since this is much much greater than percpu_batch_counter, we end up taking spinlock on every tick.

This patch change batch rule. now, any cpu can store "percpu_counter_bach * jiffies" cputime in per-cpu cache.
it mean this patch don't have behavior change if VIRT_CPU_ACCOUNTING=n.

Cc Bharata B Rao <[email protected]>
Cc Balaji Rao <[email protected]>
Cc Dhaval Giani <[email protected]>
Cc KAMEZAWA Hiroyuki <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Balbir Singh <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Martin Schwidefsky <[email protected]>
---
 kernel/sched.c |    8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

Index: b/kernel/sched.c
===================================================================

kernel/sched.c +5 -1
@@ -10221,6 +10221,7 @@ struct cpuacct {
 };
 
 struct cgroup_subsys cpuacct_subsys;
+static s32 cpuacct_batch;
 
 /* return cpu accounting group corresponding to this container */
 static inline struct cpuacct *cgroup_ca(struct cgroup *cgrp)
@@ -10250,6 +10251,9 @@ static struct cgroup_subsys_state *cpuac
 	if (!ca->cpuusage)
 		goto out_free_ca;
 
+	if (!cpuacct_batch)
+		cpuacct_batch = jiffies_to_cputime(percpu_counter_batch);
+
 	for (i = 0; i < CPUACCT_STAT_NSTATS; i++)
 		if (percpu_counter_init(&ca->cpustat[i], 0))
 			goto out_free_counters;
@@ -10376,7 +10380,7 @@ static int cpuacct_stats_show(struct cgr
 	int i;
 
 	for (i = 0; i < CPUACCT_STAT_NSTATS; i++) {
-		s64 val = percpu_counter_read(&ca->cpustat[i]);
+		s64 val = percpu_counter_sum(&ca->cpustat[i]);
 		val = cputime64_to_clock_t(val);
 		cb->fill(cb, cpuacct_stat_desc[i], val);
 	}

kernel/sched.c static void cpuacct_update_stats(struct · line 10456

Quoted 8 lines
kernel/sched.c +1 -1
@@ -10446,7 +10450,7 @@ static void cpuacct_update_stats(struct 
 	ca = task_ca(tsk);
 
 	do {
-		percpu_counter_add(&ca->cpustat[idx], val);
+		__percpu_counter_add(&ca->cpustat[idx], val, cpuacct_batch);
 		ca = ca->parent;
 	} while (ca);
 	rcu_read_unlock();

What do the test results look like with this? I'll see if I can find some time to test this patch. On a patch read level this seems much better to me, Peter?

Signature · 1 line
	Balbir
#2 lore raw
Quoted 82 lines · KOSAKI Motohiro
Quoted 68 lines

Changelog:
since v1
- use percpu_counter_sum() instead percpu_counter_read()

-------------------------------------
Subject: [PATCH v2] cpuacct: VIRT_CPU_ACCOUNTING don't prevent percpu cputime count

cpuacct_update_stats() is called at every tick updating. and it use percpu_counter for avoiding performance degression.

For archs which define VIRT_CPU_ACCOUNTING, every tick would result in >1000 units of cputime updates and since this is much much greater than percpu_batch_counter, we end up taking spinlock on every tick.

This patch change batch rule. now, any cpu can store "percpu_counter_bach * jiffies" cputime in per-cpu cache.
it mean this patch don't have behavior change if VIRT_CPU_ACCOUNTING=n.

Cc Bharata B Rao <[email protected]>
Cc Balaji Rao <[email protected]>
Cc Dhaval Giani <[email protected]>
Cc KAMEZAWA Hiroyuki <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Balbir Singh <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Martin Schwidefsky <[email protected]>
---
 kernel/sched.c |    8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

Index: b/kernel/sched.c
===================================================================

kernel/sched.c +5 -1
@@ -10221,6 +10221,7 @@ struct cpuacct {
 };
 
 struct cgroup_subsys cpuacct_subsys;
+static s32 cpuacct_batch;
 
 /* return cpu accounting group corresponding to this container */
 static inline struct cpuacct *cgroup_ca(struct cgroup *cgrp)
@@ -10250,6 +10251,9 @@ static struct cgroup_subsys_state *cpuac
 	if (!ca->cpuusage)
 		goto out_free_ca;
 
+	if (!cpuacct_batch)
+		cpuacct_batch = jiffies_to_cputime(percpu_counter_batch);
+
 	for (i = 0; i < CPUACCT_STAT_NSTATS; i++)
 		if (percpu_counter_init(&ca->cpustat[i], 0))
 			goto out_free_counters;
@@ -10376,7 +10380,7 @@ static int cpuacct_stats_show(struct cgr
 	int i;
 
 	for (i = 0; i < CPUACCT_STAT_NSTATS; i++) {
-		s64 val = percpu_counter_read(&ca->cpustat[i]);
+		s64 val = percpu_counter_sum(&ca->cpustat[i]);
 		val = cputime64_to_clock_t(val);
 		cb->fill(cb, cpuacct_stat_desc[i], val);
 	}

kernel/sched.c static void cpuacct_update_stats(struct · line 10456

Quoted 8 lines
kernel/sched.c +1 -1
@@ -10446,7 +10450,7 @@ static void cpuacct_update_stats(struct 
 	ca = task_ca(tsk);
 
 	do {
-		percpu_counter_add(&ca->cpustat[idx], val);
+		__percpu_counter_add(&ca->cpustat[idx], val, cpuacct_batch);
 		ca = ca->parent;
 	} while (ca);
 	rcu_read_unlock();

What do the test results look like with this? I'll see if I can find some time to test this patch. On a patch read level this seems much better to me, Peter?

Quoted 1 line

this patch decrease slow down risk on large server. but this patch doesn't have functional change. you can't make functional test. AFAIK, percpu_counter_sum() don't make any performance degression, but you have good stress test, please tell me it.

#3 lore raw
In reply to Balbir Singh
Quoted 60 lines · Balbir Singh
Quoted 60 lines

Subject: [PATCH v2] cpuacct: VIRT_CPU_ACCOUNTING don't prevent percpu cputime count

cpuacct_update_stats() is called at every tick updating. and it use percpu_counter for avoiding performance degression.

For archs which define VIRT_CPU_ACCOUNTING, every tick would result in >1000 units of cputime updates and since this is much much greater than percpu_batch_counter, we end up taking spinlock on every tick.

This patch change batch rule. now, any cpu can store "percpu_counter_bach * jiffies" cputime in per-cpu cache.
it mean this patch don't have behavior change if VIRT_CPU_ACCOUNTING=n.

Cc Bharata B Rao <[email protected]>
Cc Balaji Rao <[email protected]>
Cc Dhaval Giani <[email protected]>
Cc KAMEZAWA Hiroyuki <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Balbir Singh <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Martin Schwidefsky <[email protected]>
---
 kernel/sched.c |    8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

Index: b/kernel/sched.c
===================================================================

kernel/sched.c +5 -1
@@ -10221,6 +10221,7 @@ struct cpuacct {
 };
 
 struct cgroup_subsys cpuacct_subsys;
+static s32 cpuacct_batch;
 
 /* return cpu accounting group corresponding to this container */
 static inline struct cpuacct *cgroup_ca(struct cgroup *cgrp)
@@ -10250,6 +10251,9 @@ static struct cgroup_subsys_state *cpuac
 	if (!ca->cpuusage)
 		goto out_free_ca;
 
+	if (!cpuacct_batch)
+		cpuacct_batch = jiffies_to_cputime(percpu_counter_batch);
+
 	for (i = 0; i < CPUACCT_STAT_NSTATS; i++)
 		if (percpu_counter_init(&ca->cpustat[i], 0))
 			goto out_free_counters;
@@ -10376,7 +10380,7 @@ static int cpuacct_stats_show(struct cgr
 	int i;
 
 	for (i = 0; i < CPUACCT_STAT_NSTATS; i++) {
-		s64 val = percpu_counter_read(&ca->cpustat[i]);
+		s64 val = percpu_counter_sum(&ca->cpustat[i]);
 		val = cputime64_to_clock_t(val);
 		cb->fill(cb, cpuacct_stat_desc[i], val);
 	}
@@ -10446,7 +10450,7 @@ static void cpuacct_update_stats(struct 
 	ca = task_ca(tsk);
 
Quoted 12 lines

kernel/sched.c static void cpuacct_update_stats(struct · line 10456

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

What do the test results look like with this? I'll see if I can find some time to test this patch. On a patch read level this seems much better to me, Peter?

I don't really fancy percpu_counter_sum() usage. I'm thinking its ok to degrate accuracy on larger machines and simply use percpu_counter_read().

#4 lore raw
Quoted 11 lines
Quoted 10 lines

kernel/sched.c static int cpuacct_stats_show(struct cgr · line 10386

Quoted 6 lines
kernel/sched.c +1 -1
 	for (i = 0; i < CPUACCT_STAT_NSTATS; i++) {
-		s64 val = percpu_counter_read(&ca->cpustat[i]);
+		s64 val = percpu_counter_sum(&ca->cpustat[i]);
 		val = cputime64_to_clock_t(val);
 		cb->fill(cb, cpuacct_stat_desc[i], val);
 	}

What do the test results look like with this? I'll see if I can find some time to test this patch. On a patch read level this seems much better to me, Peter?

Quoted 3 lines

I don't really fancy percpu_counter_sum() usage. I'm thinking its ok to degrate accuracy on larger machines and simply use percpu_counter_read().

I have same opinion with peter. Balbir, What do you think?

#6 lore raw
In reply to Peter Zijlstra
Quoted 75 lines · Peter Zijlstra
Quoted 60 lines · Balbir Singh
Quoted 60 lines

Subject: [PATCH v2] cpuacct: VIRT_CPU_ACCOUNTING don't prevent percpu cputime count

cpuacct_update_stats() is called at every tick updating. and it use percpu_counter for avoiding performance degression.

For archs which define VIRT_CPU_ACCOUNTING, every tick would result in >1000 units of cputime updates and since this is much much greater than percpu_batch_counter, we end up taking spinlock on every tick.

This patch change batch rule. now, any cpu can store "percpu_counter_bach * jiffies" cputime in per-cpu cache.
it mean this patch don't have behavior change if VIRT_CPU_ACCOUNTING=n.

Cc Bharata B Rao <[email protected]>
Cc Balaji Rao <[email protected]>
Cc Dhaval Giani <[email protected]>
Cc KAMEZAWA Hiroyuki <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Balbir Singh <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Martin Schwidefsky <[email protected]>
---
 kernel/sched.c |    8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

Index: b/kernel/sched.c
===================================================================

kernel/sched.c +5 -1
@@ -10221,6 +10221,7 @@ struct cpuacct {
 };
 
 struct cgroup_subsys cpuacct_subsys;
+static s32 cpuacct_batch;
 
 /* return cpu accounting group corresponding to this container */
 static inline struct cpuacct *cgroup_ca(struct cgroup *cgrp)
@@ -10250,6 +10251,9 @@ static struct cgroup_subsys_state *cpuac
 	if (!ca->cpuusage)
 		goto out_free_ca;
 
+	if (!cpuacct_batch)
+		cpuacct_batch = jiffies_to_cputime(percpu_counter_batch);
+
 	for (i = 0; i < CPUACCT_STAT_NSTATS; i++)
 		if (percpu_counter_init(&ca->cpustat[i], 0))
 			goto out_free_counters;
@@ -10376,7 +10380,7 @@ static int cpuacct_stats_show(struct cgr
 	int i;
 
 	for (i = 0; i < CPUACCT_STAT_NSTATS; i++) {
-		s64 val = percpu_counter_read(&ca->cpustat[i]);
+		s64 val = percpu_counter_sum(&ca->cpustat[i]);
 		val = cputime64_to_clock_t(val);
 		cb->fill(cb, cpuacct_stat_desc[i], val);
 	}
@@ -10446,7 +10450,7 @@ static void cpuacct_update_stats(struct 
 	ca = task_ca(tsk);
 
Quoted 12 lines

kernel/sched.c static void cpuacct_update_stats(struct · line 10456

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

What do the test results look like with this? I'll see if I can find some time to test this patch. On a patch read level this seems much better to me, Peter?

Quoted 3 lines

I don't really fancy percpu_counter_sum() usage. I'm thinking its ok to degrate accuracy on larger machines and simply use percpu_counter_read().

yes - and the values will converge anyway, right? So it's just a small delay, not even any genuine loss of accuracy.

Ingo

#5 lore raw
In reply to KOSAKI Motohiro
Quoted 11 lines · KOSAKI Motohiro
Quoted 11 lines
Quoted 10 lines

kernel/sched.c static int cpuacct_stats_show(struct cgr · line 10386

Quoted 6 lines
kernel/sched.c +1 -1
 	for (i = 0; i < CPUACCT_STAT_NSTATS; i++) {
-		s64 val = percpu_counter_read(&ca->cpustat[i]);
+		s64 val = percpu_counter_sum(&ca->cpustat[i]);
 		val = cputime64_to_clock_t(val);
 		cb->fill(cb, cpuacct_stat_desc[i], val);
 	}

What do the test results look like with this? I'll see if I can find some time to test this patch. On a patch read level this seems much better to me, Peter?

Quoted 6 lines
Quoted 3 lines

I don't really fancy percpu_counter_sum() usage. I'm thinking its ok to degrate accuracy on larger machines and simply use percpu_counter_read().

I have same opinion with peter. Balbir, What do you think?

Sure, but the larger the delta gets, the less useful the metric gets :) I am OK with going back to percpu_counter_read() if that is the consensus.

Signature · 1 line
	Balbir
#7 lore raw
In reply to KOSAKI Motohiro
Quoted 12 lines · On Thu, 30 Apr 2009 15:11:15 +0900 (JST) KOSAKI Motohiro…

Changelog:
since v1
- use percpu_counter_sum() instead percpu_counter_read()

-------------------------------------
Subject: [PATCH v2] cpuacct: VIRT_CPU_ACCOUNTING don't prevent percpu cputime count

cpuacct_update_stats() is called at every tick updating. and it use percpu_counter for avoiding performance degression.

Quoted 7 lines

For archs which define VIRT_CPU_ACCOUNTING, every tick would result in >1000 units of cputime updates and since this is much much greater than percpu_batch_counter, we end up taking spinlock on every tick.

This patch change batch rule. now, any cpu can store "percpu_counter_bach * jiffies" cputime in per-cpu cache.
it mean this patch don't have behavior change if VIRT_CPU_ACCOUNTING=n.

Does this actually matter?

If we're calling cpuacct_update_stats() with large values of `cputime' then presumably we're also calling cpuacct_update_stats() at a low frequency, so the common lock-taking won't cause performance problems?

#8 lore raw
Quoted 8 lines
Quoted 7 lines

For archs which define VIRT_CPU_ACCOUNTING, every tick would result in >1000 units of cputime updates and since this is much much greater than percpu_batch_counter, we end up taking spinlock on every tick.

This patch change batch rule. now, any cpu can store "percpu_counter_bach * jiffies" cputime in per-cpu cache.
it mean this patch don't have behavior change if VIRT_CPU_ACCOUNTING=n.

Quoted 5 lines

Does this actually matter?

If we're calling cpuacct_update_stats() with large values of `cputime' then presumably we're also calling cpuacct_update_stats() at a low frequency, so the common lock-taking won't cause performance problems?

VIRT_CPU_ACCOUNTING change cputime_t meaning. but don't change calling update time frequency.

example,
ia64, HZ=1000, VIRT_CPU_ACCOUNTING=y (1 cputime == 1ns, ie 1 jiffies == 1000000 cputime)

every tick updating makes 1000000 cputime. (see jiffies_to_cputime)

-----------------------------------------------------------------------------------------
void account_process_tick(struct task_struct *p, int user_tick)

{
        cputime_t one_jiffy = jiffies_to_cputime(1);
        cputime_t one_jiffy_scaled = cputime_to_scaled(one_jiffy);
        struct rq *rq = this_rq();

        if (user_tick)
                account_user_time(p, one_jiffy, one_jiffy_scaled);
        else if (p != rq->idle)
                account_system_time(p, HARDIRQ_OFFSET, one_jiffy,
                                    one_jiffy_scaled);
        else
                account_idle_time(one_jiffy);
}

-----------------------------------------------------------------------------------------

but tick updating frequency don't changed.