Blame view
lib/percpu_counter.c
5.3 KB
3cbc56402 [PATCH] percpu_co... |
1 2 3 4 5 |
/* * Fast batching percpu counters. */ #include <linux/percpu_counter.h> |
c67ad917c percpu_counters()... |
6 7 8 9 |
#include <linux/notifier.h> #include <linux/mutex.h> #include <linux/init.h> #include <linux/cpu.h> |
3cbc56402 [PATCH] percpu_co... |
10 |
#include <linux/module.h> |
e2852ae82 percpu_counter: a... |
11 |
#include <linux/debugobjects.h> |
3cbc56402 [PATCH] percpu_co... |
12 |
|
3a8495c73 lib/percpu_counte... |
13 |
#ifdef CONFIG_HOTPLUG_CPU |
c67ad917c percpu_counters()... |
14 |
static LIST_HEAD(percpu_counters); |
d87aae2f3 switch the protec... |
15 |
static DEFINE_SPINLOCK(percpu_counters_lock); |
3a8495c73 lib/percpu_counte... |
16 |
#endif |
c67ad917c percpu_counters()... |
17 |
|
e2852ae82 percpu_counter: a... |
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 lib: percpu_count... |
59 60 61 |
void percpu_counter_set(struct percpu_counter *fbc, s64 amount) { int cpu; |
098faf580 percpu_counter: m... |
62 |
unsigned long flags; |
3a587f47b lib: percpu_count... |
63 |
|
098faf580 percpu_counter: m... |
64 |
raw_spin_lock_irqsave(&fbc->lock, flags); |
3a587f47b lib: percpu_count... |
65 66 67 68 69 |
for_each_possible_cpu(cpu) { s32 *pcount = per_cpu_ptr(fbc->counters, cpu); *pcount = 0; } fbc->count = amount; |
098faf580 percpu_counter: m... |
70 |
raw_spin_unlock_irqrestore(&fbc->lock, flags); |
3a587f47b lib: percpu_count... |
71 72 |
} EXPORT_SYMBOL(percpu_counter_set); |
20e897670 lib: make percpu_... |
73 |
void __percpu_counter_add(struct percpu_counter *fbc, s64 amount, s32 batch) |
3cbc56402 [PATCH] percpu_co... |
74 |
{ |
20e897670 lib: make percpu_... |
75 |
s64 count; |
3cbc56402 [PATCH] percpu_co... |
76 |
|
ea00c30b5 percpu_counter: u... |
77 |
preempt_disable(); |
819a72af8 percpucounter: Op... |
78 |
count = __this_cpu_read(*fbc->counters) + amount; |
252e0ba6b lib: percpu_count... |
79 |
if (count >= batch || count <= -batch) { |
098faf580 percpu_counter: m... |
80 81 |
unsigned long flags; raw_spin_lock_irqsave(&fbc->lock, flags); |
3cbc56402 [PATCH] percpu_co... |
82 |
fbc->count += count; |
d1969a84d percpu_counter: u... |
83 |
__this_cpu_sub(*fbc->counters, count - amount); |
098faf580 percpu_counter: m... |
84 |
raw_spin_unlock_irqrestore(&fbc->lock, flags); |
3cbc56402 [PATCH] percpu_co... |
85 |
} else { |
74e72f894 lib/percpu_counte... |
86 |
this_cpu_add(*fbc->counters, amount); |
3cbc56402 [PATCH] percpu_co... |
87 |
} |
ea00c30b5 percpu_counter: u... |
88 |
preempt_enable(); |
3cbc56402 [PATCH] percpu_co... |
89 |
} |
252e0ba6b lib: percpu_count... |
90 |
EXPORT_SYMBOL(__percpu_counter_add); |
3cbc56402 [PATCH] percpu_co... |
91 92 93 94 95 |
/* * 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 revert "percpu_co... |
96 |
s64 __percpu_counter_sum(struct percpu_counter *fbc) |
3cbc56402 [PATCH] percpu_co... |
97 |
{ |
0216bfcff [PATCH] percpu co... |
98 |
s64 ret; |
3cbc56402 [PATCH] percpu_co... |
99 |
int cpu; |
098faf580 percpu_counter: m... |
100 |
unsigned long flags; |
3cbc56402 [PATCH] percpu_co... |
101 |
|
098faf580 percpu_counter: m... |
102 |
raw_spin_lock_irqsave(&fbc->lock, flags); |
3cbc56402 [PATCH] percpu_co... |
103 |
ret = fbc->count; |
b4ef0296f percpu_counters: ... |
104 |
for_each_online_cpu(cpu) { |
0216bfcff [PATCH] percpu co... |
105 |
s32 *pcount = per_cpu_ptr(fbc->counters, cpu); |
3cbc56402 [PATCH] percpu_co... |
106 107 |
ret += *pcount; } |
098faf580 percpu_counter: m... |
108 |
raw_spin_unlock_irqrestore(&fbc->lock, flags); |
bf1d89c81 lib: percpu_count... |
109 |
return ret; |
3cbc56402 [PATCH] percpu_co... |
110 |
} |
bf1d89c81 lib: percpu_count... |
111 |
EXPORT_SYMBOL(__percpu_counter_sum); |
c67ad917c percpu_counters()... |
112 |
|
ea319518b locking, percpu c... |
113 114 |
int __percpu_counter_init(struct percpu_counter *fbc, s64 amount, struct lock_class_key *key) |
c67ad917c percpu_counters()... |
115 |
{ |
f032a4508 locking, percpu_c... |
116 |
raw_spin_lock_init(&fbc->lock); |
ea319518b locking, percpu c... |
117 |
lockdep_set_class(&fbc->lock, key); |
c67ad917c percpu_counters()... |
118 119 |
fbc->count = amount; fbc->counters = alloc_percpu(s32); |
833f4077b lib: percpu_count... |
120 121 |
if (!fbc->counters) return -ENOMEM; |
e2852ae82 percpu_counter: a... |
122 123 |
debug_percpu_counter_activate(fbc); |
c67ad917c percpu_counters()... |
124 |
#ifdef CONFIG_HOTPLUG_CPU |
8474b591f percpu: fix list_... |
125 |
INIT_LIST_HEAD(&fbc->list); |
d87aae2f3 switch the protec... |
126 |
spin_lock(&percpu_counters_lock); |
c67ad917c percpu_counters()... |
127 |
list_add(&fbc->list, &percpu_counters); |
d87aae2f3 switch the protec... |
128 |
spin_unlock(&percpu_counters_lock); |
c67ad917c percpu_counters()... |
129 |
#endif |
833f4077b lib: percpu_count... |
130 |
return 0; |
c67ad917c percpu_counters()... |
131 |
} |
ea319518b locking, percpu c... |
132 |
EXPORT_SYMBOL(__percpu_counter_init); |
c67ad917c percpu_counters()... |
133 134 135 |
void percpu_counter_destroy(struct percpu_counter *fbc) { |
833f4077b lib: percpu_count... |
136 137 |
if (!fbc->counters) return; |
e2852ae82 percpu_counter: a... |
138 |
debug_percpu_counter_deactivate(fbc); |
c67ad917c percpu_counters()... |
139 |
#ifdef CONFIG_HOTPLUG_CPU |
d87aae2f3 switch the protec... |
140 |
spin_lock(&percpu_counters_lock); |
c67ad917c percpu_counters()... |
141 |
list_del(&fbc->list); |
d87aae2f3 switch the protec... |
142 |
spin_unlock(&percpu_counters_lock); |
c67ad917c percpu_counters()... |
143 |
#endif |
fd3d664fe percpu_counter: f... |
144 145 |
free_percpu(fbc->counters); fbc->counters = NULL; |
c67ad917c percpu_counters()... |
146 147 |
} EXPORT_SYMBOL(percpu_counter_destroy); |
179f7ebff percpu_counter: F... |
148 149 150 151 152 153 154 155 156 |
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); } |
0db0628d9 kernel: delete __... |
157 |
static int percpu_counter_hotcpu_callback(struct notifier_block *nb, |
c67ad917c percpu_counters()... |
158 159 |
unsigned long action, void *hcpu) { |
179f7ebff percpu_counter: F... |
160 |
#ifdef CONFIG_HOTPLUG_CPU |
c67ad917c percpu_counters()... |
161 162 |
unsigned int cpu; struct percpu_counter *fbc; |
179f7ebff percpu_counter: F... |
163 |
compute_batch_value(); |
e39435ce6 lib/percpu_counte... |
164 |
if (action != CPU_DEAD && action != CPU_DEAD_FROZEN) |
c67ad917c percpu_counters()... |
165 166 167 |
return NOTIFY_OK; cpu = (unsigned long)hcpu; |
d87aae2f3 switch the protec... |
168 |
spin_lock(&percpu_counters_lock); |
c67ad917c percpu_counters()... |
169 170 |
list_for_each_entry(fbc, &percpu_counters, list) { s32 *pcount; |
d2b20b115 Add irq protectio... |
171 |
unsigned long flags; |
c67ad917c percpu_counters()... |
172 |
|
f032a4508 locking, percpu_c... |
173 |
raw_spin_lock_irqsave(&fbc->lock, flags); |
c67ad917c percpu_counters()... |
174 175 176 |
pcount = per_cpu_ptr(fbc->counters, cpu); fbc->count += *pcount; *pcount = 0; |
f032a4508 locking, percpu_c... |
177 |
raw_spin_unlock_irqrestore(&fbc->lock, flags); |
c67ad917c percpu_counters()... |
178 |
} |
d87aae2f3 switch the protec... |
179 |
spin_unlock(&percpu_counters_lock); |
179f7ebff percpu_counter: F... |
180 |
#endif |
c67ad917c percpu_counters()... |
181 182 |
return NOTIFY_OK; } |
27f5e0f69 tmpfs: add accura... |
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
/* * 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 percpu_counters()... |
209 210 |
static int __init percpu_counter_startup(void) { |
179f7ebff percpu_counter: F... |
211 |
compute_batch_value(); |
c67ad917c percpu_counters()... |
212 213 214 215 |
hotcpu_notifier(percpu_counter_hotcpu_callback, 0); return 0; } module_init(percpu_counter_startup); |