aboutsummaryrefslogtreecommitdiffstats
path: root/lib/percpu_counter.c
diff options
context:
space:
mode:
authorTim Chen <tim.c.chen@linux.intel.com>2010-08-09 20:19:04 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2010-08-09 23:44:58 -0400
commit27f5e0f694fd0600274a76854636c0749e3bb1f6 (patch)
tree0d3a0387b42eb0a830fa5ba896a205b2a3807b8b /lib/percpu_counter.c
parent4e60c86bd9e5a7110ed28874d0b6592186550ae8 (diff)
tmpfs: add accurate compare function to percpu_counter library
Add percpu_counter_compare that allows for a quick but accurate comparison of percpu_counter with a given value. A rough count is provided by the count field in percpu_counter structure, without accounting for the other values stored in individual cpu counters. The actual count is a sum of count and the cpu counters. However, count field is never different from the actual value by a factor of batch*num_online_cpu. We do not need to get actual count for comparison if count is different from the given value by this factor and allows for quick comparison without summing up all the per cpu counters. Signed-off-by: Tim Chen <tim.c.chen@linux.intel.com> Cc: Hugh Dickins <hughd@google.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'lib/percpu_counter.c')
-rw-r--r--lib/percpu_counter.c27
1 files changed, 27 insertions, 0 deletions
diff --git a/lib/percpu_counter.c b/lib/percpu_counter.c
index aeaa6d734447..ec9048e74f44 100644
--- a/lib/percpu_counter.c
+++ b/lib/percpu_counter.c
@@ -137,6 +137,33 @@ static int __cpuinit percpu_counter_hotcpu_callback(struct notifier_block *nb,
137 return NOTIFY_OK; 137 return NOTIFY_OK;
138} 138}
139 139
140/*
141 * Compare counter against given value.
142 * Return 1 if greater, 0 if equal and -1 if less
143 */
144int percpu_counter_compare(struct percpu_counter *fbc, s64 rhs)
145{
146 s64 count;
147
148 count = percpu_counter_read(fbc);
149 /* Check to see if rough count will be sufficient for comparison */
150 if (abs(count - rhs) > (percpu_counter_batch*num_online_cpus())) {
151 if (count > rhs)
152 return 1;
153 else
154 return -1;
155 }
156 /* Need to use precise count */
157 count = percpu_counter_sum(fbc);
158 if (count > rhs)
159 return 1;
160 else if (count < rhs)
161 return -1;
162 else
163 return 0;
164}
165EXPORT_SYMBOL(percpu_counter_compare);
166
140static int __init percpu_counter_startup(void) 167static int __init percpu_counter_startup(void)
141{ 168{
142 compute_batch_value(); 169 compute_batch_value();