2 * Fast batching percpu counters.
5 #include <linux/percpu_counter.h>
6 #include <linux/notifier.h>
7 #include <linux/mutex.h>
8 #include <linux/init.h>
10 #include <linux/module.h>
12 static LIST_HEAD(percpu_counters
);
13 static DEFINE_MUTEX(percpu_counters_lock
);
15 void percpu_counter_set(struct percpu_counter
*fbc
, s64 amount
)
19 spin_lock(&fbc
->lock
);
20 for_each_possible_cpu(cpu
) {
21 s32
*pcount
= per_cpu_ptr(fbc
->counters
, cpu
);
25 spin_unlock(&fbc
->lock
);
27 EXPORT_SYMBOL(percpu_counter_set
);
29 void __percpu_counter_add(struct percpu_counter
*fbc
, s64 amount
, s32 batch
)
35 pcount
= per_cpu_ptr(fbc
->counters
, cpu
);
36 count
= *pcount
+ amount
;
37 if (count
>= batch
|| count
<= -batch
) {
38 spin_lock(&fbc
->lock
);
41 spin_unlock(&fbc
->lock
);
47 EXPORT_SYMBOL(__percpu_counter_add
);
50 * Add up all the per-cpu counts, return the result. This is a more accurate
51 * but much slower version of percpu_counter_read_positive()
53 s64
__percpu_counter_sum(struct percpu_counter
*fbc
)
58 spin_lock(&fbc
->lock
);
60 for_each_online_cpu(cpu
) {
61 s32
*pcount
= per_cpu_ptr(fbc
->counters
, cpu
);
64 spin_unlock(&fbc
->lock
);
67 EXPORT_SYMBOL(__percpu_counter_sum
);
69 int __percpu_counter_init(struct percpu_counter
*fbc
, s64 amount
,
70 struct lock_class_key
*key
)
72 spin_lock_init(&fbc
->lock
);
73 lockdep_set_class(&fbc
->lock
, key
);
75 fbc
->counters
= alloc_percpu(s32
);
78 #ifdef CONFIG_HOTPLUG_CPU
79 mutex_lock(&percpu_counters_lock
);
80 list_add(&fbc
->list
, &percpu_counters
);
81 mutex_unlock(&percpu_counters_lock
);
85 EXPORT_SYMBOL(__percpu_counter_init
);
87 void percpu_counter_destroy(struct percpu_counter
*fbc
)
92 #ifdef CONFIG_HOTPLUG_CPU
93 mutex_lock(&percpu_counters_lock
);
95 mutex_unlock(&percpu_counters_lock
);
97 free_percpu(fbc
->counters
);
100 EXPORT_SYMBOL(percpu_counter_destroy
);
102 int percpu_counter_batch __read_mostly
= 32;
103 EXPORT_SYMBOL(percpu_counter_batch
);
105 static void compute_batch_value(void)
107 int nr
= num_online_cpus();
109 percpu_counter_batch
= max(32, nr
*2);
112 static int __cpuinit
percpu_counter_hotcpu_callback(struct notifier_block
*nb
,
113 unsigned long action
, void *hcpu
)
115 #ifdef CONFIG_HOTPLUG_CPU
117 struct percpu_counter
*fbc
;
119 compute_batch_value();
120 if (action
!= CPU_DEAD
)
123 cpu
= (unsigned long)hcpu
;
124 mutex_lock(&percpu_counters_lock
);
125 list_for_each_entry(fbc
, &percpu_counters
, list
) {
129 spin_lock_irqsave(&fbc
->lock
, flags
);
130 pcount
= per_cpu_ptr(fbc
->counters
, cpu
);
131 fbc
->count
+= *pcount
;
133 spin_unlock_irqrestore(&fbc
->lock
, flags
);
135 mutex_unlock(&percpu_counters_lock
);
141 * Compare counter against given value.
142 * Return 1 if greater, 0 if equal and -1 if less
144 int percpu_counter_compare(struct percpu_counter
*fbc
, s64 rhs
)
148 count
= percpu_counter_read(fbc
);
149 /* Check to see if rough count will be sufficient for comparison */
150 if (abs(count
- rhs
) > (percpu_counter_batch
*num_online_cpus())) {
156 /* Need to use precise count */
157 count
= percpu_counter_sum(fbc
);
160 else if (count
< rhs
)
165 EXPORT_SYMBOL(percpu_counter_compare
);
167 static int __init
percpu_counter_startup(void)
169 compute_batch_value();
170 hotcpu_notifier(percpu_counter_hotcpu_callback
, 0);
173 module_init(percpu_counter_startup
);