blob: 96bdde36599ff7876c51eea0e05fc5166a8faee4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef _LINUX_PERCPU_COUNTER_H
2#define _LINUX_PERCPU_COUNTER_H
3/*
4 * A simple "approximate counter" for use in ext2 and ext3 superblocks.
5 *
6 * WARNING: these things are HUGE. 4 kbytes per counter on 32-way P4.
7 */
8
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/spinlock.h>
10#include <linux/smp.h>
Andrew Mortonc67ad912007-07-15 23:39:51 -070011#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/threads.h>
13#include <linux/percpu.h>
Mingming Cao0216bfc2006-06-23 02:05:41 -070014#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16#ifdef CONFIG_SMP
17
18struct percpu_counter {
19 spinlock_t lock;
Mingming Cao0216bfc2006-06-23 02:05:41 -070020 s64 count;
Andrew Mortonc67ad912007-07-15 23:39:51 -070021#ifdef CONFIG_HOTPLUG_CPU
22 struct list_head list; /* All percpu_counters are on a list */
23#endif
Mingming Cao0216bfc2006-06-23 02:05:41 -070024 s32 *counters;
Linus Torvalds1da177e2005-04-16 15:20:36 -070025};
26
27#if NR_CPUS >= 16
28#define FBC_BATCH (NR_CPUS*2)
29#else
30#define FBC_BATCH (NR_CPUS*4)
31#endif
32
Peter Zijlstraea319512008-12-26 15:08:55 +010033int __percpu_counter_init(struct percpu_counter *fbc, s64 amount,
34 struct lock_class_key *key);
35
36#define percpu_counter_init(fbc, value) \
37 ({ \
38 static struct lock_class_key __key; \
39 \
40 __percpu_counter_init(fbc, value, &__key); \
41 })
42
Andrew Mortonc67ad912007-07-15 23:39:51 -070043void percpu_counter_destroy(struct percpu_counter *fbc);
Peter Zijlstra3a587f42007-10-16 23:25:44 -070044void percpu_counter_set(struct percpu_counter *fbc, s64 amount);
Peter Zijlstra20e89762007-10-16 23:25:43 -070045void __percpu_counter_add(struct percpu_counter *fbc, s64 amount, s32 batch);
Mingming Cao1f7c14c2008-10-09 12:50:59 -040046s64 __percpu_counter_sum(struct percpu_counter *fbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Peter Zijlstra20e89762007-10-16 23:25:43 -070048static inline void percpu_counter_add(struct percpu_counter *fbc, s64 amount)
Peter Zijlstra252e0ba2007-10-16 23:25:43 -070049{
50 __percpu_counter_add(fbc, amount, FBC_BATCH);
51}
52
Peter Zijlstrabf1d89c2007-10-16 23:25:45 -070053static inline s64 percpu_counter_sum_positive(struct percpu_counter *fbc)
54{
Mingming Cao1f7c14c2008-10-09 12:50:59 -040055 s64 ret = __percpu_counter_sum(fbc);
Peter Zijlstrabf1d89c2007-10-16 23:25:45 -070056 return ret < 0 ? 0 : ret;
57}
58
59static inline s64 percpu_counter_sum(struct percpu_counter *fbc)
60{
Mingming Cao1f7c14c2008-10-09 12:50:59 -040061 return __percpu_counter_sum(fbc);
Peter Zijlstrabf1d89c2007-10-16 23:25:45 -070062}
63
Mingming Cao0216bfc2006-06-23 02:05:41 -070064static inline s64 percpu_counter_read(struct percpu_counter *fbc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
66 return fbc->count;
67}
68
69/*
70 * It is possible for the percpu_counter_read() to return a small negative
71 * number for some counter which should never be negative.
Mingming Cao0216bfc2006-06-23 02:05:41 -070072 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 */
Mingming Cao0216bfc2006-06-23 02:05:41 -070074static inline s64 percpu_counter_read_positive(struct percpu_counter *fbc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
Mingming Cao0216bfc2006-06-23 02:05:41 -070076 s64 ret = fbc->count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78 barrier(); /* Prevent reloads of fbc->count */
Mingming Cao0216bfc2006-06-23 02:05:41 -070079 if (ret >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 return ret;
81 return 1;
82}
83
84#else
85
86struct percpu_counter {
Mingming Cao0216bfc2006-06-23 02:05:41 -070087 s64 count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088};
89
Peter Zijlstra833f4072007-10-16 23:25:45 -070090static inline int percpu_counter_init(struct percpu_counter *fbc, s64 amount)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Mingming Cao0216bfc2006-06-23 02:05:41 -070092 fbc->count = amount;
Peter Zijlstra833f4072007-10-16 23:25:45 -070093 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094}
95
96static inline void percpu_counter_destroy(struct percpu_counter *fbc)
97{
98}
99
Peter Zijlstra3a587f42007-10-16 23:25:44 -0700100static inline void percpu_counter_set(struct percpu_counter *fbc, s64 amount)
101{
102 fbc->count = amount;
103}
104
Peter Zijlstra252e0ba2007-10-16 23:25:43 -0700105#define __percpu_counter_add(fbc, amount, batch) \
106 percpu_counter_add(fbc, amount)
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108static inline void
Peter Zijlstra20e89762007-10-16 23:25:43 -0700109percpu_counter_add(struct percpu_counter *fbc, s64 amount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
111 preempt_disable();
112 fbc->count += amount;
113 preempt_enable();
114}
115
Mingming Cao0216bfc2006-06-23 02:05:41 -0700116static inline s64 percpu_counter_read(struct percpu_counter *fbc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
118 return fbc->count;
119}
120
Mingming Cao0216bfc2006-06-23 02:05:41 -0700121static inline s64 percpu_counter_read_positive(struct percpu_counter *fbc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
123 return fbc->count;
124}
125
Peter Zijlstra52d9f3b2007-10-16 23:25:44 -0700126static inline s64 percpu_counter_sum_positive(struct percpu_counter *fbc)
Andrew Mortone2bab3d2006-03-07 21:55:31 -0800127{
128 return percpu_counter_read_positive(fbc);
129}
130
Peter Zijlstrabf1d89c2007-10-16 23:25:45 -0700131static inline s64 percpu_counter_sum(struct percpu_counter *fbc)
132{
133 return percpu_counter_read(fbc);
134}
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136#endif /* CONFIG_SMP */
137
138static inline void percpu_counter_inc(struct percpu_counter *fbc)
139{
Peter Zijlstraaa0dff22007-10-16 23:25:42 -0700140 percpu_counter_add(fbc, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
143static inline void percpu_counter_dec(struct percpu_counter *fbc)
144{
Peter Zijlstraaa0dff22007-10-16 23:25:42 -0700145 percpu_counter_add(fbc, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
147
Peter Zijlstra3cb4f9f2007-10-16 23:25:42 -0700148static inline void percpu_counter_sub(struct percpu_counter *fbc, s64 amount)
149{
150 percpu_counter_add(fbc, -amount);
151}
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153#endif /* _LINUX_PERCPU_COUNTER_H */