

From: Andrew Morton <akpm@osdl.org>

Implement percpu_counter_sum().  This is a more accurate but slower version of
percpu_counter_read_positive().

We need this for Alex's speedup-ext3_statfs patch.  Otherwise it would be too
inaccurate on large CPU counts.

(Note that this makes Ravi's optimisation of removing percpu_counter.lock
invalid.  We need that lock to prevent other CPUs from making large changes to
the accumulators).

Cc: Ravikiran G Thirumalai <kiran@scalex86.org>
Cc: Alex Tomas <alex@clusterfs.com>
Signed-off-by: Andrew Morton <akpm@osdl.org>
---



 include/linux/percpu_counter.h |    6 ++++++
 mm/swap.c                      |   25 +++++++++++++++++++++++--
 2 files changed, 29 insertions(+), 2 deletions(-)

diff -puN include/linux/percpu_counter.h~percpu-counter-sum include/linux/percpu_counter.h
--- linux-2.6.16-rc3-rcu/include/linux/percpu_counter.h~percpu-counter-sum	2006-03-05 11:46:42.000000000 +0530
+++ linux-2.6.16-rc3-rcu-dipankar/include/linux/percpu_counter.h	2006-03-05 11:46:42.000000000 +0530
@@ -39,6 +39,7 @@ static inline void percpu_counter_destro
 }
 
 void percpu_counter_mod(struct percpu_counter *fbc, long amount);
+long percpu_counter_sum(struct percpu_counter *fbc);
 
 static inline long percpu_counter_read(struct percpu_counter *fbc)
 {
@@ -92,6 +93,11 @@ static inline long percpu_counter_read_p
 	return fbc->count;
 }
 
+static inline long percpu_counter_sum(struct percpu_counter *fbc)
+{
+	return percpu_counter_read_positive(fbc);
+}
+
 #endif	/* CONFIG_SMP */
 
 static inline void percpu_counter_inc(struct percpu_counter *fbc)
diff -puN mm/swap.c~percpu-counter-sum mm/swap.c
--- linux-2.6.16-rc3-rcu/mm/swap.c~percpu-counter-sum	2006-03-05 11:46:42.000000000 +0530
+++ linux-2.6.16-rc3-rcu-dipankar/mm/swap.c	2006-03-05 11:46:42.000000000 +0530
@@ -489,13 +489,34 @@ void percpu_counter_mod(struct percpu_co
 	if (count >= FBC_BATCH || count <= -FBC_BATCH) {
 		spin_lock(&fbc->lock);
 		fbc->count += count;
+		*pcount = 0;
 		spin_unlock(&fbc->lock);
-		count = 0;
+	} else {
+		*pcount = count;
 	}
-	*pcount = count;
 	put_cpu();
 }
 EXPORT_SYMBOL(percpu_counter_mod);
+
+/*
+ * Add up all the per-cpu counts, return the result.  This is a more accurate
+ * but much slower version of percpu_counter_read_positive()
+ */
+long percpu_counter_sum(struct percpu_counter *fbc)
+{
+	long ret;
+	int cpu;
+
+	spin_lock(&fbc->lock);
+	ret = fbc->count;
+	for_each_cpu(cpu) {
+		long *pcount = per_cpu_ptr(fbc->counters, cpu);
+		ret += *pcount;
+	}
+	spin_unlock(&fbc->lock);
+	return ret < 0 ? 0 : ret;
+}
+EXPORT_SYMBOL(percpu_counter_sum);
 #endif
 
 /*

_
