X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=lib%2Fpercpu_counter.c;fp=lib%2Fpercpu_counter.c;h=850449080e1cfb7f619eca4ae5d5ed6aca01a1d0;hb=f05f9504c50ed069377d37f02f22e7a16b5921de;hp=0000000000000000000000000000000000000000;hpb=16c70f8c1b54b61c3b951b6fb220df250fe09b32;p=linux-2.6.git diff --git a/lib/percpu_counter.c b/lib/percpu_counter.c new file mode 100644 index 000000000..850449080 --- /dev/null +++ b/lib/percpu_counter.c @@ -0,0 +1,46 @@ +/* + * Fast batching percpu counters. + */ + +#include +#include + +void percpu_counter_mod(struct percpu_counter *fbc, s32 amount) +{ + long count; + s32 *pcount; + int cpu = get_cpu(); + + pcount = per_cpu_ptr(fbc->counters, cpu); + count = *pcount + amount; + if (count >= FBC_BATCH || count <= -FBC_BATCH) { + spin_lock(&fbc->lock); + fbc->count += count; + *pcount = 0; + spin_unlock(&fbc->lock); + } else { + *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() + */ +s64 percpu_counter_sum(struct percpu_counter *fbc) +{ + s64 ret; + int cpu; + + spin_lock(&fbc->lock); + ret = fbc->count; + for_each_possible_cpu(cpu) { + s32 *pcount = per_cpu_ptr(fbc->counters, cpu); + ret += *pcount; + } + spin_unlock(&fbc->lock); + return ret < 0 ? 0 : ret; +} +EXPORT_SYMBOL(percpu_counter_sum);