Skip to contentSkip to conversation

ext4: EXT4_FREEBLOCKS_WATERMARK is overly pessimistic

PATCH In review Anton Blanchard 4 participants 12 messages Aug 25, 2011 – Sep 7, 2011

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

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
Index: linux-2.6-work/fs/ext4/ext4.h
===================================================================
@@ -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
#1 lore raw

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
Index: linux-2.6-work/lib/percpu_counter.c
===================================================================
@@ -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,
#2 lore raw

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

#3 lore raw

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
Index: linux-2.6-work/lib/percpu_counter.c
===================================================================
@@ -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,
maintainer Applied
#4 lore raw

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_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).

Quoted 7 lines

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.

Applied to percpu/for-3.2.

Thanks.

Signature · 1 line
tejun
maintainer
#5 lore raw

Re: [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_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.

Quoted 6 lines
Quoted 4 lines

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.

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

maintainer
#6 lore raw

Re: [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_batch at 1024 as a conservative first step, but we may want to reduce it further based on further benchmarking.

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
#7 lore raw

Re: [PATCH] percpu_counter: Put a reasonable upper bound on percpu_counter_batch

In reply to Theodore Tso

Hi 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

maintainer
#8 lore raw

Re: [PATCH 2/2] percpu_counter: Put a reasonable upper bound on percpu_counter_batch

In reply to Anton Blanchard
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_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).

Quoted 7 lines

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.

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
maintainer
#9 lore raw

Re: [PATCH 2/2] percpu_counter: Put a reasonable upper bound on percpu_counter_batch

In reply to Anton Blanchard
Quoted 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_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).

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

#10 lore raw

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

maintainer
#11 lore raw

Re: [PATCH 2/2] percpu_counter: Put a reasonable upper bound on percpu_counter_batch

In reply to Theodore Tso
Quoted 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