Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/avi/kvm
[linux-2.6/linux-2.6-openrd.git] / include / linux / percpu_counter.h
blob3d9f70972cdfa7a0943653037f777b548dc85478
1 #ifndef _LINUX_PERCPU_COUNTER_H
2 #define _LINUX_PERCPU_COUNTER_H
3 /*
4 * A simple "approximate counter" for use in ext2 and ext3 superblocks.
6 * WARNING: these things are HUGE. 4 kbytes per counter on 32-way P4.
7 */
9 #include <linux/spinlock.h>
10 #include <linux/smp.h>
11 #include <linux/list.h>
12 #include <linux/threads.h>
13 #include <linux/percpu.h>
14 #include <linux/types.h>
16 #ifdef CONFIG_SMP
18 struct percpu_counter {
19 spinlock_t lock;
20 s64 count;
21 #ifdef CONFIG_HOTPLUG_CPU
22 struct list_head list; /* All percpu_counters are on a list */
23 #endif
24 s32 *counters;
27 #if NR_CPUS >= 16
28 #define FBC_BATCH (NR_CPUS*2)
29 #else
30 #define FBC_BATCH (NR_CPUS*4)
31 #endif
33 void percpu_counter_init(struct percpu_counter *fbc, s64 amount);
34 void percpu_counter_destroy(struct percpu_counter *fbc);
35 void percpu_counter_mod(struct percpu_counter *fbc, s32 amount);
36 s64 percpu_counter_sum(struct percpu_counter *fbc);
38 static inline s64 percpu_counter_read(struct percpu_counter *fbc)
40 return fbc->count;
44 * It is possible for the percpu_counter_read() to return a small negative
45 * number for some counter which should never be negative.
48 static inline s64 percpu_counter_read_positive(struct percpu_counter *fbc)
50 s64 ret = fbc->count;
52 barrier(); /* Prevent reloads of fbc->count */
53 if (ret >= 0)
54 return ret;
55 return 1;
58 #else
60 struct percpu_counter {
61 s64 count;
64 static inline void percpu_counter_init(struct percpu_counter *fbc, s64 amount)
66 fbc->count = amount;
69 static inline void percpu_counter_destroy(struct percpu_counter *fbc)
73 static inline void
74 percpu_counter_mod(struct percpu_counter *fbc, s32 amount)
76 preempt_disable();
77 fbc->count += amount;
78 preempt_enable();
81 static inline s64 percpu_counter_read(struct percpu_counter *fbc)
83 return fbc->count;
86 static inline s64 percpu_counter_read_positive(struct percpu_counter *fbc)
88 return fbc->count;
91 static inline s64 percpu_counter_sum(struct percpu_counter *fbc)
93 return percpu_counter_read_positive(fbc);
96 #endif /* CONFIG_SMP */
98 static inline void percpu_counter_inc(struct percpu_counter *fbc)
100 percpu_counter_mod(fbc, 1);
103 static inline void percpu_counter_dec(struct percpu_counter *fbc)
105 percpu_counter_mod(fbc, -1);
108 #endif /* _LINUX_PERCPU_COUNTER_H */