blob: 8a7d510ffa9cb0fd11759d1224d125ff4e76a17f [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
Tejun Heo43cf38e2010-02-02 14:38:57 +090024 s32 __percpu *counters;
Linus Torvalds1da177e2005-04-16 15:20:36 -070025};
26
Eric Dumazet179f7eb2009-01-06 14:41:04 -080027extern int percpu_counter_batch;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Peter Zijlstraea319512008-12-26 15:08:55 +010029int __percpu_counter_init(struct percpu_counter *fbc, s64 amount,
30 struct lock_class_key *key);
31
32#define percpu_counter_init(fbc, value) \
33 ({ \
34 static struct lock_class_key __key; \
35 \
36 __percpu_counter_init(fbc, value, &__key); \
37 })
38
Andrew Mortonc67ad912007-07-15 23:39:51 -070039void percpu_counter_destroy(struct percpu_counter *fbc);
Peter Zijlstra3a587f42007-10-16 23:25:44 -070040void percpu_counter_set(struct percpu_counter *fbc, s64 amount);
Peter Zijlstra20e89762007-10-16 23:25:43 -070041void __percpu_counter_add(struct percpu_counter *fbc, s64 amount, s32 batch);
Andrew Morton02d21162008-12-09 13:14:14 -080042s64 __percpu_counter_sum(struct percpu_counter *fbc);
Tim Chen27f5e0f2010-08-09 17:19:04 -070043int percpu_counter_compare(struct percpu_counter *fbc, s64 rhs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Peter Zijlstra20e89762007-10-16 23:25:43 -070045static inline void percpu_counter_add(struct percpu_counter *fbc, s64 amount)
Peter Zijlstra252e0ba2007-10-16 23:25:43 -070046{
Eric Dumazet179f7eb2009-01-06 14:41:04 -080047 __percpu_counter_add(fbc, amount, percpu_counter_batch);
Peter Zijlstra252e0ba2007-10-16 23:25:43 -070048}
49
Peter Zijlstrabf1d89c2007-10-16 23:25:45 -070050static inline s64 percpu_counter_sum_positive(struct percpu_counter *fbc)
51{
Andrew Morton02d21162008-12-09 13:14:14 -080052 s64 ret = __percpu_counter_sum(fbc);
Peter Zijlstrabf1d89c2007-10-16 23:25:45 -070053 return ret < 0 ? 0 : ret;
54}
55
56static inline s64 percpu_counter_sum(struct percpu_counter *fbc)
57{
Andrew Morton02d21162008-12-09 13:14:14 -080058 return __percpu_counter_sum(fbc);
Peter Zijlstrabf1d89c2007-10-16 23:25:45 -070059}
60
Mingming Cao0216bfc2006-06-23 02:05:41 -070061static inline s64 percpu_counter_read(struct percpu_counter *fbc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
63 return fbc->count;
64}
65
66/*
67 * It is possible for the percpu_counter_read() to return a small negative
68 * number for some counter which should never be negative.
Mingming Cao0216bfc2006-06-23 02:05:41 -070069 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 */
Mingming Cao0216bfc2006-06-23 02:05:41 -070071static inline s64 percpu_counter_read_positive(struct percpu_counter *fbc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
Mingming Cao0216bfc2006-06-23 02:05:41 -070073 s64 ret = fbc->count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75 barrier(); /* Prevent reloads of fbc->count */
Mingming Cao0216bfc2006-06-23 02:05:41 -070076 if (ret >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 return ret;
78 return 1;
79}
80
81#else
82
83struct percpu_counter {
Mingming Cao0216bfc2006-06-23 02:05:41 -070084 s64 count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085};
86
Peter Zijlstra833f4072007-10-16 23:25:45 -070087static inline int percpu_counter_init(struct percpu_counter *fbc, s64 amount)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
Mingming Cao0216bfc2006-06-23 02:05:41 -070089 fbc->count = amount;
Peter Zijlstra833f4072007-10-16 23:25:45 -070090 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
92
93static inline void percpu_counter_destroy(struct percpu_counter *fbc)
94{
95}
96
Peter Zijlstra3a587f42007-10-16 23:25:44 -070097static inline void percpu_counter_set(struct percpu_counter *fbc, s64 amount)
98{
99 fbc->count = amount;
100}
101
Tim Chen27f5e0f2010-08-09 17:19:04 -0700102static inline int percpu_counter_compare(struct percpu_counter *fbc, s64 rhs)
103{
104 if (fbc->count > rhs)
105 return 1;
106 else if (fbc->count < rhs)
107 return -1;
108 else
109 return 0;
110}
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112static inline void
Peter Zijlstra20e89762007-10-16 23:25:43 -0700113percpu_counter_add(struct percpu_counter *fbc, s64 amount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
115 preempt_disable();
116 fbc->count += amount;
117 preempt_enable();
118}
119
Anton Blanchard0c9cf2e2010-02-02 14:46:10 -0800120static inline void
121__percpu_counter_add(struct percpu_counter *fbc, s64 amount, s32 batch)
122{
123 percpu_counter_add(fbc, amount);
124}
125
Mingming Cao0216bfc2006-06-23 02:05:41 -0700126static inline s64 percpu_counter_read(struct percpu_counter *fbc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
128 return fbc->count;
129}
130
Mingming Cao0216bfc2006-06-23 02:05:41 -0700131static inline s64 percpu_counter_read_positive(struct percpu_counter *fbc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
133 return fbc->count;
134}
135
Peter Zijlstra52d9f3b2007-10-16 23:25:44 -0700136static inline s64 percpu_counter_sum_positive(struct percpu_counter *fbc)
Andrew Mortone2bab3d2006-03-07 21:55:31 -0800137{
138 return percpu_counter_read_positive(fbc);
139}
140
Peter Zijlstrabf1d89c2007-10-16 23:25:45 -0700141static inline s64 percpu_counter_sum(struct percpu_counter *fbc)
142{
143 return percpu_counter_read(fbc);
144}
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146#endif /* CONFIG_SMP */
147
148static inline void percpu_counter_inc(struct percpu_counter *fbc)
149{
Peter Zijlstraaa0dff22007-10-16 23:25:42 -0700150 percpu_counter_add(fbc, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
153static inline void percpu_counter_dec(struct percpu_counter *fbc)
154{
Peter Zijlstraaa0dff22007-10-16 23:25:42 -0700155 percpu_counter_add(fbc, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
157
Peter Zijlstra3cb4f9f2007-10-16 23:25:42 -0700158static inline void percpu_counter_sub(struct percpu_counter *fbc, s64 amount)
159{
160 percpu_counter_add(fbc, -amount);
161}
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163#endif /* _LINUX_PERCPU_COUNTER_H */