diff mbox

percpu_counter: Put a reasonable upper bound on percpu_counter_batch

Message ID 20110829214609.495ee299@kryten
State New, archived
Headers show

Commit Message

Anton Blanchard Aug. 29, 2011, 11:46 a.m. UTC
When testing on a 1024 thread ppc64 box I noticed a large amount of
CPU time in ext4 code.

ext4_has_free_blocks has a fast path to avoid summing every free and
dirty block per cpu counter, but only if the global count shows more
free blocks than the maximum amount that could be stored in all the
per cpu counters.

Since percpu_counter_batch scales with num_online_cpus() and the maximum
amount in all per cpu counters is percpu_counter_batch * num_online_cpus(),
this breakpoint grows at O(n^2).

This issue will also hit with users of percpu_counter_compare which
does a similar thing for one percpu counter.

I chose to cap percpu_counter_batch at 1024 as a conservative first
step, but we may want to reduce it further based on further benchmarking.

Signed-off-by: Anton Blanchard <anton@samba.org>
---

--
To unsubscribe from this list: send the line "unsubscribe linux-ext4" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Comments

Tejun Heo Sept. 6, 2011, 3:48 a.m. UTC | #1
On Mon, Aug 29, 2011 at 09:46:09PM +1000, Anton Blanchard wrote:
> 
> When testing on a 1024 thread ppc64 box I noticed a large amount of
> CPU time in ext4 code.
> 
> ext4_has_free_blocks has a fast path to avoid summing every free and
> dirty block per cpu counter, but only if the global count shows more
> free blocks than the maximum amount that could be stored in all the
> per cpu counters.
> 
> Since percpu_counter_batch scales with num_online_cpus() and the maximum
> amount in all per cpu counters is percpu_counter_batch * num_online_cpus(),
> this breakpoint grows at O(n^2).
> 
> This issue will also hit with users of percpu_counter_compare which
> does a similar thing for one percpu counter.
> 
> I chose to cap percpu_counter_batch at 1024 as a conservative first
> step, but we may want to reduce it further based on further benchmarking.
> 
> Signed-off-by: Anton Blanchard <anton@samba.org>

Applied to percpu/for-3.2.

Thanks.
Theodore Ts'o Sept. 6, 2011, 1:30 p.m. UTC | #2
On Sep 5, 2011, at 11:48 PM, Tejun Heo wrote:

> On Mon, Aug 29, 2011 at 09:46:09PM +1000, Anton Blanchard wrote:
>> 
>> When testing on a 1024 thread ppc64 box I noticed a large amount of
>> CPU time in ext4 code.
>> 
>> ext4_has_free_blocks has a fast path to avoid summing every free and
>> dirty block per cpu counter, but only if the global count shows more
>> free blocks than the maximum amount that could be stored in all the
>> per cpu counters.
>> 
>> Since percpu_counter_batch scales with num_online_cpus() and the maximum
>> amount in all per cpu counters is percpu_counter_batch * num_online_cpus(),
>> this breakpoint grows at O(n^2).
>> 
>> This issue will also hit with users of percpu_counter_compare which
>> does a similar thing for one percpu counter.
>> 
>> I chose to cap percpu_counter_batch at 1024 as a conservative first
>> step, but we may want to reduce it further based on further benchmarking.
>> 
>> Signed-off-by: Anton Blanchard <anton@samba.org>
> 
> Applied to percpu/for-3.2.

Um, this was an ext4 patch and I pointed out it could cause problems.  (Specifically, data loss…)

- Ted

--
To unsubscribe from this list: send the line "unsubscribe linux-ext4" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Tejun Heo Sept. 6, 2011, 4:44 p.m. UTC | #3
On Tue, Sep 06, 2011 at 09:30:50AM -0400, Theodore Tso wrote:
> >> I chose to cap percpu_counter_batch at 1024 as a conservative first
> >> step, but we may want to reduce it further based on further benchmarking.
> >> 
> >> Signed-off-by: Anton Blanchard <anton@samba.org>
> > 
> > Applied to percpu/for-3.2.
> 
> Um, this was an ext4 patch and I pointed out it could cause problems.  (Specifically, data loss…)

Ah okay, I thought you were talking about the first patch only.
Reverting for now.

Thanks.
Anton Blanchard Sept. 7, 2011, 11:08 a.m. UTC | #4
Hi Ted,

> Um, this was an ext4 patch and I pointed out it could cause
> problems.  (Specifically, data loss…)

I'm a bit confused. While the comment mentions ext4, the patch is just
putting an upper bound on the size of percpu_counter_batch and it is
useful for percpu_counter_compare() too:

 static void compute_batch_value(void)
 {
        int nr = num_online_cpus();

-       percpu_counter_batch = max(32, nr*2);
+       /*
+        * The cutoff point for the percpu_counter_compare() fast path grows
+        * at num_online_cpus^2 and on a big enough machine it will be
+        * unlikely to hit.
+        * We clamp the batch value to 1024 so the cutoff point only grows
+        * linearly past 512 CPUs.
+        */
+       percpu_counter_batch = clamp(nr*2, 32, 1024);
 }

The batch value should be opaque to the rest of the kernel. If ext4
requires a specific batch value we can use the functions that take
an explicit one (eg __percpu_counter_add).

Anton
--
To unsubscribe from this list: send the line "unsubscribe linux-ext4" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

Index: linux-2.6-work/lib/percpu_counter.c
===================================================================
--- linux-2.6-work.orig/lib/percpu_counter.c	2011-08-29 19:50:44.482008591 +1000
+++ linux-2.6-work/lib/percpu_counter.c	2011-08-29 21:21:10.026779139 +1000
@@ -153,7 +153,14 @@  static void compute_batch_value(void)
 {
 	int nr = num_online_cpus();
 
-	percpu_counter_batch = max(32, nr*2);
+	/*
+	 * The cutoff point for the percpu_counter_compare() fast path grows
+	 * at num_online_cpus^2 and on a big enough machine it will be
+	 * unlikely to hit.
+	 * We clamp the batch value to 1024 so the cutoff point only grows
+	 * linearly past 512 CPUs.
+	 */
+	percpu_counter_batch = clamp(nr*2, 32, 1024);
 }
 
 static int __cpuinit percpu_counter_hotcpu_callback(struct notifier_block *nb,