lib: percpu_counter_sum_positive
[deliverable/linux.git] / lib / percpu_counter.c
1 /*
2 * Fast batching percpu counters.
3 */
4
5 #include <linux/percpu_counter.h>
6 #include <linux/notifier.h>
7 #include <linux/mutex.h>
8 #include <linux/init.h>
9 #include <linux/cpu.h>
10 #include <linux/module.h>
11
12 #ifdef CONFIG_HOTPLUG_CPU
13 static LIST_HEAD(percpu_counters);
14 static DEFINE_MUTEX(percpu_counters_lock);
15 #endif
16
17 void percpu_counter_set(struct percpu_counter *fbc, s64 amount)
18 {
19 int cpu;
20
21 spin_lock(&fbc->lock);
22 for_each_possible_cpu(cpu) {
23 s32 *pcount = per_cpu_ptr(fbc->counters, cpu);
24 *pcount = 0;
25 }
26 fbc->count = amount;
27 spin_unlock(&fbc->lock);
28 }
29 EXPORT_SYMBOL(percpu_counter_set);
30
31 void __percpu_counter_add(struct percpu_counter *fbc, s64 amount, s32 batch)
32 {
33 s64 count;
34 s32 *pcount;
35 int cpu = get_cpu();
36
37 pcount = per_cpu_ptr(fbc->counters, cpu);
38 count = *pcount + amount;
39 if (count >= batch || count <= -batch) {
40 spin_lock(&fbc->lock);
41 fbc->count += count;
42 *pcount = 0;
43 spin_unlock(&fbc->lock);
44 } else {
45 *pcount = count;
46 }
47 put_cpu();
48 }
49 EXPORT_SYMBOL(__percpu_counter_add);
50
51 /*
52 * Add up all the per-cpu counts, return the result. This is a more accurate
53 * but much slower version of percpu_counter_read_positive()
54 */
55 s64 percpu_counter_sum_positive(struct percpu_counter *fbc)
56 {
57 s64 ret;
58 int cpu;
59
60 spin_lock(&fbc->lock);
61 ret = fbc->count;
62 for_each_online_cpu(cpu) {
63 s32 *pcount = per_cpu_ptr(fbc->counters, cpu);
64 ret += *pcount;
65 }
66 spin_unlock(&fbc->lock);
67 return ret < 0 ? 0 : ret;
68 }
69 EXPORT_SYMBOL(percpu_counter_sum_positive);
70
71 void percpu_counter_init(struct percpu_counter *fbc, s64 amount)
72 {
73 spin_lock_init(&fbc->lock);
74 fbc->count = amount;
75 fbc->counters = alloc_percpu(s32);
76 #ifdef CONFIG_HOTPLUG_CPU
77 mutex_lock(&percpu_counters_lock);
78 list_add(&fbc->list, &percpu_counters);
79 mutex_unlock(&percpu_counters_lock);
80 #endif
81 }
82 EXPORT_SYMBOL(percpu_counter_init);
83
84 void percpu_counter_destroy(struct percpu_counter *fbc)
85 {
86 free_percpu(fbc->counters);
87 #ifdef CONFIG_HOTPLUG_CPU
88 mutex_lock(&percpu_counters_lock);
89 list_del(&fbc->list);
90 mutex_unlock(&percpu_counters_lock);
91 #endif
92 }
93 EXPORT_SYMBOL(percpu_counter_destroy);
94
95 #ifdef CONFIG_HOTPLUG_CPU
96 static int __cpuinit percpu_counter_hotcpu_callback(struct notifier_block *nb,
97 unsigned long action, void *hcpu)
98 {
99 unsigned int cpu;
100 struct percpu_counter *fbc;
101
102 if (action != CPU_DEAD)
103 return NOTIFY_OK;
104
105 cpu = (unsigned long)hcpu;
106 mutex_lock(&percpu_counters_lock);
107 list_for_each_entry(fbc, &percpu_counters, list) {
108 s32 *pcount;
109
110 spin_lock(&fbc->lock);
111 pcount = per_cpu_ptr(fbc->counters, cpu);
112 fbc->count += *pcount;
113 *pcount = 0;
114 spin_unlock(&fbc->lock);
115 }
116 mutex_unlock(&percpu_counters_lock);
117 return NOTIFY_OK;
118 }
119
120 static int __init percpu_counter_startup(void)
121 {
122 hotcpu_notifier(percpu_counter_hotcpu_callback, 0);
123 return 0;
124 }
125 module_init(percpu_counter_startup);
126 #endif
This page took 0.033564 seconds and 6 git commands to generate.