ext4: EXT4_FREEBLOCKS_WATERMARK is overly pessimistic
Subsystems (3)
PATCH 1/2 ext4: EXT4_FREEBLOCKS_WATERMARK is overly pessimistic
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.
While we are summing 2 per cpu counters we set the breakpoint at 4 times the maximum amount in the per cpu counter portion. Reduce that to 2.
Since we fold the per cpu count of CPUs going offline into the global count, we can use num_online_cpus() instead of nr_cpu_ids here too.
Both these changes match percpu_counter_compare() which is used to optimise a comparison against one per cpu counter.
---
fs/ext4/ext4.h +4 -3
@@ -2051,10 +2051,11 @@ do { \#ifdef CONFIG_SMP /* Each CPU can accumulate percpu_counter_batch blocks in their local -* counters. So we need to make sure we have free blocks more -* than percpu_counter_batch * nr_cpu_ids. Also add a window of 4 times. +* counters. Since we sum two percpu counters (s_freeblocks_counter and +* s_dirtyblocks_counter), as a worst case we need to check for 2x this. */ -#define EXT4_FREEBLOCKS_WATERMARK (4 * (percpu_counter_batch * nr_cpu_ids)) +#define EXT4_FREEBLOCKS_WATERMARK \ + (2 * (percpu_counter_batch * num_online_cpus()))#else #define EXT4_FREEBLOCKS_WATERMARK 0#endif
PATCH 2/2 percpu_counter: Put a reasonable upper bound on percpu_counter_batch
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.
---
lib/percpu_counter.c +5 -1
@@ -149,11 +149,15 @@ EXPORT_SYMBOL(percpu_counter_destroy);int percpu_counter_batch__read_mostly =32 ;EXPORT_SYMBOL (percpu_counter_batch); +/* +* We set the batch at 2 * num_online_cpus(), with a minimum of 32 and +* a maximum of 1024. +*/ static void compute_batch_value (void ) {int nr =num_online_cpus (); - percpu_counter_batch =max (32 , nr*2 ); + percpu_counter_batch =min (1024 ,max (32 , nr*2 )); } static int __cpuinit percpu_counter_hotcpu_callback(struct notifier_block *nb,
Re: [PATCH 2/2] percpu_counter: Put a reasonable upper bound on percpu_counter_batch
lib/percpu_counter.c EXPORT_SYMBOL(percpu_counter_destroy); · line 163
Quoted 13 lines · Anton Blanchard
lib/percpu_counter.c +5
-1+/* +* We set the batch at 2 * num_online_cpus(), with a minimum of 32 and +* a maximum of 1024. +*/ static void compute_batch_value (void ) {int nr =num_online_cpus (); - percpu_counter_batch =max (32 , nr*2 ); + percpu_counter_batch =min (1024 ,max (32 , nr*2 )); } static int __cpuinit percpu_counter_hotcpu_callback(struct notifier_block *nb,
Or use the following :
percpu_counter_batch = clamp(nr*2, 32, 1024);
--
To unsubscribe from this list: send the line "unsubscribe linux-ext4" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
PATCH 1/1 percpu_counter: Put a reasonable upper bound on percpu_counter_batch
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.
---
lib/percpu_counter.c +8 -1
@@ -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,
Re: [PATCH] percpu_counter: Put a reasonable upper bound on percpu_counter_batch
Quoted 13 lines · Anton Blanchard
When testing on a 1024 thread ppc64 box I noticed a large amount of CPU time in ext4 code.
ext4_has_free_blockshas 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_batchscales withnum_online_cpus()and the maximum amount in all per cpu counters ispercpu_counter_batch*num_online_cpus(), this breakpoint grows at O(n^2).
Quoted 7 lines
This issue will also hit with users of
percpu_counter_comparewhich does a similar thing for one percpu counter.I chose to cap
percpu_counter_batchat 1024 as a conservative first step, but we may want to reduce it further based on further benchmarking.Signed-off-by Anton Blanchard <[email protected]>
Applied to percpu/for-3.2.
Thanks.
Signature · 1 line
tejun
Theodore Tso
maintainerRe: [PATCH] percpu_counter: Put a reasonable upper bound on percpu_counter_batch
Quoted 17 lines · Tejun Heo
Quoted 16 lines · Anton Blanchard
When testing on a 1024 thread ppc64 box I noticed a large amount of CPU time in ext4 code.
ext4_has_free_blockshas 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_batchscales withnum_online_cpus()and the maximum amount in all per cpu counters ispercpu_counter_batch*num_online_cpus(), this breakpoint grows at O(n^2).This issue will also hit with users of
percpu_counter_comparewhich does a similar thing for one percpu counter.
Quoted 6 lines
Quoted 4 lines
I chose to cap
percpu_counter_batchat 1024 as a conservative first step, but we may want to reduce it further based on further benchmarking.Signed-off-by Anton Blanchard <[email protected]>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 [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Tejun Heo
maintainerRe: [PATCH] percpu_counter: Put a reasonable upper bound on percpu_counter_batch
Quoted 8 lines · Theodore Tso
Quoted 6 lines
Quoted 4 lines
I chose to cap
percpu_counter_batchat 1024 as a conservative first step, but we may want to reduce it further based on further benchmarking.Signed-off-by Anton Blanchard <[email protected]>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.
Signature · 5 lines
tejun -- To unsubscribe from this list: send the line "unsubscribe linux-ext4" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [PATCH] percpu_counter: Put a reasonable upper bound on percpu_counter_batch
In reply to Theodore TsoHi Ted,
Quoted 2 lines
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 [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Tejun Heo
maintainerRe: [PATCH 2/2] percpu_counter: Put a reasonable upper bound on percpu_counter_batch
In reply to Anton BlanchardQuoted 13 lines · Anton Blanchard
When testing on a 1024 thread ppc64 box I noticed a large amount of CPU time in ext4 code.
ext4_has_free_blockshas 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_batchscales withnum_online_cpus()and the maximum amount in all per cpu counters ispercpu_counter_batch*num_online_cpus(), this breakpoint grows at O(n^2).
Quoted 7 lines
This issue will also hit with users of
percpu_counter_comparewhich does a similar thing for one percpu counter.I chose to cap
percpu_counter_batchat 1024 as a conservative first step, but we may want to reduce it further based on further benchmarking.Signed-off-by Anton Blanchard <[email protected]>
Yeah, capping the upper bound seems reasonable but can you please add some comment explaining why the upper bound is necessary there?
Thank you.
Signature · 1 line
tejun
Theodore Tso
maintainerRe: [PATCH 2/2] percpu_counter: Put a reasonable upper bound on percpu_counter_batch
In reply to Anton BlanchardQuoted 4 lines · Anton Blanchard
When testing on a 1024 thread ppc64 box I noticed a large amount of CPU time in ext4 code.
Quoted 8 lines
ext4_has_free_blockshas 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_batchscales withnum_online_cpus()and the maximum amount in all per cpu counters ispercpu_counter_batch*num_online_cpus(), this breakpoint grows at O(n^2).
I understand why we would want to reduce this number. Unfortunately, the
question is what do we do if all 1024 threads try to do buffered writes into the file system at the same instant, when we have less than 4 megabytes of space left?
The problem is that we can then do more writes than we have space, and we will only find out about it at write back time, when the process may have exited already -- at which point data loss is almost inevitable. (We could keep the data in cache and frantically page the system administrator to delete some files to make room for dirty data, but that's probably not going to end well….)
What we can do if we must clamp this threshold is to also increase the threshold at which we shift away from delayed allocation. We'll then allocate each block at write time, which does mean more CPU and less efficient allocation of blocks, but if we're down to our last 4 megabytes, there's probably not much we can do that will be efficient as far as block layout anyway….
-- Ted
Re: [PATCH 2/2] percpu_counter: Put a reasonable upper bound on percpu_counter_batch
Hi Ted,
Quoted 12 lines
I understand why we would want to reduce this number. Unfortunately, the question is what do we do if all 1024 threads try to do buffered writes into the file system at the same instant, when we have less than 4 megabytes of space left?
The problem is that we can then do more writes than we have space, and we will only find out about it at write back time, when the process may have exited already -- at which point data loss is almost inevitable. (We could keep the data in cache and frantically page the system administrator to delete some files to make room for dirty data, but that's probably not going to end well….)
Quoted 6 lines
What we can do if we must clamp this threshold is to also increase the threshold at which we shift away from delayed allocation. We'll then allocate each block at write time, which does mean more CPU and less efficient allocation of blocks, but if we're down to our last 4 megabytes, there's probably not much we can do that will be efficient as far as block layout anyway….
Thanks for the explanation, I'll go back and take another look.
Anton
--
To unsubscribe from this list: send the line "unsubscribe linux-ext4" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Ted Ts'o
maintainerRe: [PATCH 2/2] percpu_counter: Put a reasonable upper bound on percpu_counter_batch
In reply to Theodore TsoQuoted 5 lines · Theodore Tso
I understand why we would want to reduce this number. Unfortunately, the
question is what do we do if all 1024 threads try to do buffered writes into the file system at the same instant, when we have less than 4 megabytes of space left?
Oops, sorry, that should be 4 GB of space left (i.e., what do we do if all 1024 cpu's all try write 1024 4k blocks all at the same time). Imagine an simulation application where all threads finish at more or less the same time and then try to write out their data files at the same time....
- Ted