blob: 20838883535704c89cf72bafe29a28d12dea45e3 [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 Zijlstra833f4072007-10-16 23:25:45 -070033int percpu_counter_init(struct percpu_counter *fbc, s64 amount);
Peter Zijlstradc62a302007-10-16 23:25:46 -070034int percpu_counter_init_irq(struct percpu_counter *fbc, s64 amount);
Andrew Mortonc67ad912007-07-15 23:39:51 -070035void percpu_counter_destroy(struct percpu_counter *fbc);
Peter Zijlstra3a587f42007-10-16 23:25:44 -070036void percpu_counter_set(struct percpu_counter *fbc, s64 amount);
Peter Zijlstra20e89762007-10-16 23:25:43 -070037void __percpu_counter_add(struct percpu_counter *fbc, s64 amount, s32 batch);
Mingming Caoe8ced392008-07-11 19:27:31 -040038s64 __percpu_counter_sum(struct percpu_counter *fbc, int set);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Peter Zijlstra20e89762007-10-16 23:25:43 -070040static inline void percpu_counter_add(struct percpu_counter *fbc, s64 amount)
Peter Zijlstra252e0ba2007-10-16 23:25:43 -070041{
42 __percpu_counter_add(fbc, amount, FBC_BATCH);
43}
44
Peter Zijlstrabf1d89c2007-10-16 23:25:45 -070045static inline s64 percpu_counter_sum_positive(struct percpu_counter *fbc)
46{
Mingming Caoe8ced392008-07-11 19:27:31 -040047 s64 ret = __percpu_counter_sum(fbc, 0);
Peter Zijlstrabf1d89c2007-10-16 23:25:45 -070048 return ret < 0 ? 0 : ret;
49}
50
Mingming Caoe8ced392008-07-11 19:27:31 -040051static inline s64 percpu_counter_sum_and_set(struct percpu_counter *fbc)
52{
53 return __percpu_counter_sum(fbc, 1);
54}
55
56
Peter Zijlstrabf1d89c2007-10-16 23:25:45 -070057static inline s64 percpu_counter_sum(struct percpu_counter *fbc)
58{
Mingming Caoe8ced392008-07-11 19:27:31 -040059 return __percpu_counter_sum(fbc, 0);
Peter Zijlstrabf1d89c2007-10-16 23:25:45 -070060}
61
Mingming Cao0216bfc2006-06-23 02:05:41 -070062static inline s64 percpu_counter_read(struct percpu_counter *fbc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
64 return fbc->count;
65}
66
67/*
68 * It is possible for the percpu_counter_read() to return a small negative
69 * number for some counter which should never be negative.
Mingming Cao0216bfc2006-06-23 02:05:41 -070070 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 */
Mingming Cao0216bfc2006-06-23 02:05:41 -070072static inline s64 percpu_counter_read_positive(struct percpu_counter *fbc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
Mingming Cao0216bfc2006-06-23 02:05:41 -070074 s64 ret = fbc->count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76 barrier(); /* Prevent reloads of fbc->count */
Mingming Cao0216bfc2006-06-23 02:05:41 -070077 if (ret >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 return ret;
79 return 1;
80}
81
82#else
83
84struct percpu_counter {
Mingming Cao0216bfc2006-06-23 02:05:41 -070085 s64 count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086};
87
Peter Zijlstra833f4072007-10-16 23:25:45 -070088static inline int percpu_counter_init(struct percpu_counter *fbc, s64 amount)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
Mingming Cao0216bfc2006-06-23 02:05:41 -070090 fbc->count = amount;
Peter Zijlstra833f4072007-10-16 23:25:45 -070091 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092}
93
Peter Zijlstradc62a302007-10-16 23:25:46 -070094#define percpu_counter_init_irq percpu_counter_init
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096static 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 */