1 #ifndef _LINUX_PERCPU_COUNTER_H
2 #define _LINUX_PERCPU_COUNTER_H
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.
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>
18 struct percpu_counter {
21 #ifdef CONFIG_HOTPLUG_CPU
22 struct list_head list; /* All percpu_counters are on a list */
28 #define FBC_BATCH (NR_CPUS*2)
30 #define FBC_BATCH (NR_CPUS*4)
33 void percpu_counter_init(struct percpu_counter *fbc, s64 amount);
34 void percpu_counter_destroy(struct percpu_counter *fbc);
35 void __percpu_counter_add(struct percpu_counter *fbc, s32 amount, s32 batch);
36 s64 percpu_counter_sum(struct percpu_counter *fbc);
38 static inline void percpu_counter_add(struct percpu_counter *fbc, s32 amount)
40 __percpu_counter_add(fbc, amount, FBC_BATCH);
43 static inline s64 percpu_counter_read(struct percpu_counter *fbc)
49 * It is possible for the percpu_counter_read() to return a small negative
50 * number for some counter which should never be negative.
53 static inline s64 percpu_counter_read_positive(struct percpu_counter *fbc)
57 barrier(); /* Prevent reloads of fbc->count */
65 struct percpu_counter {
69 static inline void percpu_counter_init(struct percpu_counter *fbc, s64 amount)
74 static inline void percpu_counter_destroy(struct percpu_counter *fbc)
78 #define __percpu_counter_add(fbc, amount, batch) \
79 percpu_counter_add(fbc, amount)
82 percpu_counter_add(struct percpu_counter *fbc, s32 amount)
89 static inline s64 percpu_counter_read(struct percpu_counter *fbc)
94 static inline s64 percpu_counter_read_positive(struct percpu_counter *fbc)
99 static inline s64 percpu_counter_sum(struct percpu_counter *fbc)
101 return percpu_counter_read_positive(fbc);
104 #endif /* CONFIG_SMP */
106 static inline void percpu_counter_inc(struct percpu_counter *fbc)
108 percpu_counter_add(fbc, 1);
111 static inline void percpu_counter_dec(struct percpu_counter *fbc)
113 percpu_counter_add(fbc, -1);
116 static inline void percpu_counter_sub(struct percpu_counter *fbc, s64 amount)
118 percpu_counter_add(fbc, -amount);
121 #endif /* _LINUX_PERCPU_COUNTER_H */