Blame view
lib/percpu_counter.c
5.11 KB
3cbc56402
|
1 2 3 4 5 |
/* * Fast batching percpu counters. */ #include <linux/percpu_counter.h> |
c67ad917c
|
6 7 8 9 |
#include <linux/notifier.h> #include <linux/mutex.h> #include <linux/init.h> #include <linux/cpu.h> |
3cbc56402
|
10 |
#include <linux/module.h> |
e2852ae82
|
11 |
#include <linux/debugobjects.h> |
3cbc56402
|
12 |
|
3a8495c73
|
13 |
#ifdef CONFIG_HOTPLUG_CPU |
c67ad917c
|
14 15 |
static LIST_HEAD(percpu_counters); static DEFINE_MUTEX(percpu_counters_lock); |
3a8495c73
|
16 |
#endif |
c67ad917c
|
17 |
|
e2852ae82
|
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
#ifdef CONFIG_DEBUG_OBJECTS_PERCPU_COUNTER static struct debug_obj_descr percpu_counter_debug_descr; static int percpu_counter_fixup_free(void *addr, enum debug_obj_state state) { struct percpu_counter *fbc = addr; switch (state) { case ODEBUG_STATE_ACTIVE: percpu_counter_destroy(fbc); debug_object_free(fbc, &percpu_counter_debug_descr); return 1; default: return 0; } } static struct debug_obj_descr percpu_counter_debug_descr = { .name = "percpu_counter", .fixup_free = percpu_counter_fixup_free, }; static inline void debug_percpu_counter_activate(struct percpu_counter *fbc) { debug_object_init(fbc, &percpu_counter_debug_descr); debug_object_activate(fbc, &percpu_counter_debug_descr); } static inline void debug_percpu_counter_deactivate(struct percpu_counter *fbc) { debug_object_deactivate(fbc, &percpu_counter_debug_descr); debug_object_free(fbc, &percpu_counter_debug_descr); } #else /* CONFIG_DEBUG_OBJECTS_PERCPU_COUNTER */ static inline void debug_percpu_counter_activate(struct percpu_counter *fbc) { } static inline void debug_percpu_counter_deactivate(struct percpu_counter *fbc) { } #endif /* CONFIG_DEBUG_OBJECTS_PERCPU_COUNTER */ |
3a587f47b
|
59 60 61 |
void percpu_counter_set(struct percpu_counter *fbc, s64 amount) { int cpu; |
f032a4508
|
62 |
raw_spin_lock(&fbc->lock); |
3a587f47b
|
63 64 65 66 67 |
for_each_possible_cpu(cpu) { s32 *pcount = per_cpu_ptr(fbc->counters, cpu); *pcount = 0; } fbc->count = amount; |
f032a4508
|
68 |
raw_spin_unlock(&fbc->lock); |
3a587f47b
|
69 70 |
} EXPORT_SYMBOL(percpu_counter_set); |
20e897670
|
71 |
void __percpu_counter_add(struct percpu_counter *fbc, s64 amount, s32 batch) |
3cbc56402
|
72 |
{ |
20e897670
|
73 |
s64 count; |
3cbc56402
|
74 |
|
ea00c30b5
|
75 |
preempt_disable(); |
819a72af8
|
76 |
count = __this_cpu_read(*fbc->counters) + amount; |
252e0ba6b
|
77 |
if (count >= batch || count <= -batch) { |
f032a4508
|
78 |
raw_spin_lock(&fbc->lock); |
3cbc56402
|
79 |
fbc->count += count; |
819a72af8
|
80 |
__this_cpu_write(*fbc->counters, 0); |
f032a4508
|
81 |
raw_spin_unlock(&fbc->lock); |
3cbc56402
|
82 |
} else { |
819a72af8
|
83 |
__this_cpu_write(*fbc->counters, count); |
3cbc56402
|
84 |
} |
ea00c30b5
|
85 |
preempt_enable(); |
3cbc56402
|
86 |
} |
252e0ba6b
|
87 |
EXPORT_SYMBOL(__percpu_counter_add); |
3cbc56402
|
88 89 90 91 92 |
/* * Add up all the per-cpu counts, return the result. This is a more accurate * but much slower version of percpu_counter_read_positive() */ |
02d211688
|
93 |
s64 __percpu_counter_sum(struct percpu_counter *fbc) |
3cbc56402
|
94 |
{ |
0216bfcff
|
95 |
s64 ret; |
3cbc56402
|
96 |
int cpu; |
f032a4508
|
97 |
raw_spin_lock(&fbc->lock); |
3cbc56402
|
98 |
ret = fbc->count; |
b4ef0296f
|
99 |
for_each_online_cpu(cpu) { |
0216bfcff
|
100 |
s32 *pcount = per_cpu_ptr(fbc->counters, cpu); |
3cbc56402
|
101 102 |
ret += *pcount; } |
f032a4508
|
103 |
raw_spin_unlock(&fbc->lock); |
bf1d89c81
|
104 |
return ret; |
3cbc56402
|
105 |
} |
bf1d89c81
|
106 |
EXPORT_SYMBOL(__percpu_counter_sum); |
c67ad917c
|
107 |
|
ea319518b
|
108 109 |
int __percpu_counter_init(struct percpu_counter *fbc, s64 amount, struct lock_class_key *key) |
c67ad917c
|
110 |
{ |
f032a4508
|
111 |
raw_spin_lock_init(&fbc->lock); |
ea319518b
|
112 |
lockdep_set_class(&fbc->lock, key); |
c67ad917c
|
113 114 |
fbc->count = amount; fbc->counters = alloc_percpu(s32); |
833f4077b
|
115 116 |
if (!fbc->counters) return -ENOMEM; |
e2852ae82
|
117 118 |
debug_percpu_counter_activate(fbc); |
c67ad917c
|
119 |
#ifdef CONFIG_HOTPLUG_CPU |
8474b591f
|
120 |
INIT_LIST_HEAD(&fbc->list); |
c67ad917c
|
121 122 123 124 |
mutex_lock(&percpu_counters_lock); list_add(&fbc->list, &percpu_counters); mutex_unlock(&percpu_counters_lock); #endif |
833f4077b
|
125 |
return 0; |
c67ad917c
|
126 |
} |
ea319518b
|
127 |
EXPORT_SYMBOL(__percpu_counter_init); |
c67ad917c
|
128 129 130 |
void percpu_counter_destroy(struct percpu_counter *fbc) { |
833f4077b
|
131 132 |
if (!fbc->counters) return; |
e2852ae82
|
133 |
debug_percpu_counter_deactivate(fbc); |
c67ad917c
|
134 135 136 137 138 |
#ifdef CONFIG_HOTPLUG_CPU mutex_lock(&percpu_counters_lock); list_del(&fbc->list); mutex_unlock(&percpu_counters_lock); #endif |
fd3d664fe
|
139 140 |
free_percpu(fbc->counters); fbc->counters = NULL; |
c67ad917c
|
141 142 |
} EXPORT_SYMBOL(percpu_counter_destroy); |
179f7ebff
|
143 144 145 146 147 148 149 150 151 |
int percpu_counter_batch __read_mostly = 32; EXPORT_SYMBOL(percpu_counter_batch); static void compute_batch_value(void) { int nr = num_online_cpus(); percpu_counter_batch = max(32, nr*2); } |
c67ad917c
|
152 153 154 |
static int __cpuinit percpu_counter_hotcpu_callback(struct notifier_block *nb, unsigned long action, void *hcpu) { |
179f7ebff
|
155 |
#ifdef CONFIG_HOTPLUG_CPU |
c67ad917c
|
156 157 |
unsigned int cpu; struct percpu_counter *fbc; |
179f7ebff
|
158 |
compute_batch_value(); |
c67ad917c
|
159 160 161 162 163 164 165 |
if (action != CPU_DEAD) return NOTIFY_OK; cpu = (unsigned long)hcpu; mutex_lock(&percpu_counters_lock); list_for_each_entry(fbc, &percpu_counters, list) { s32 *pcount; |
d2b20b115
|
166 |
unsigned long flags; |
c67ad917c
|
167 |
|
f032a4508
|
168 |
raw_spin_lock_irqsave(&fbc->lock, flags); |
c67ad917c
|
169 170 171 |
pcount = per_cpu_ptr(fbc->counters, cpu); fbc->count += *pcount; *pcount = 0; |
f032a4508
|
172 |
raw_spin_unlock_irqrestore(&fbc->lock, flags); |
c67ad917c
|
173 174 |
} mutex_unlock(&percpu_counters_lock); |
179f7ebff
|
175 |
#endif |
c67ad917c
|
176 177 |
return NOTIFY_OK; } |
27f5e0f69
|
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
/* * Compare counter against given value. * Return 1 if greater, 0 if equal and -1 if less */ int percpu_counter_compare(struct percpu_counter *fbc, s64 rhs) { s64 count; count = percpu_counter_read(fbc); /* Check to see if rough count will be sufficient for comparison */ if (abs(count - rhs) > (percpu_counter_batch*num_online_cpus())) { if (count > rhs) return 1; else return -1; } /* Need to use precise count */ count = percpu_counter_sum(fbc); if (count > rhs) return 1; else if (count < rhs) return -1; else return 0; } EXPORT_SYMBOL(percpu_counter_compare); |
c67ad917c
|
204 205 |
static int __init percpu_counter_startup(void) { |
179f7ebff
|
206 |
compute_batch_value(); |
c67ad917c
|
207 208 209 210 |
hotcpu_notifier(percpu_counter_hotcpu_callback, 0); return 0; } module_init(percpu_counter_startup); |