Ravikiran G Thirumalai | 3cbc564 | 2006-06-23 02:05:40 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Fast batching percpu counters. |
| 3 | */ |
| 4 | |
| 5 | #include <linux/percpu_counter.h> |
Andrew Morton | c67ad91 | 2007-07-15 23:39:51 -0700 | [diff] [blame] | 6 | #include <linux/notifier.h> |
| 7 | #include <linux/mutex.h> |
| 8 | #include <linux/init.h> |
| 9 | #include <linux/cpu.h> |
Ravikiran G Thirumalai | 3cbc564 | 2006-06-23 02:05:40 -0700 | [diff] [blame] | 10 | #include <linux/module.h> |
| 11 | |
Andrew Morton | c67ad91 | 2007-07-15 23:39:51 -0700 | [diff] [blame] | 12 | static LIST_HEAD(percpu_counters); |
| 13 | static DEFINE_MUTEX(percpu_counters_lock); |
Andrew Morton | c67ad91 | 2007-07-15 23:39:51 -0700 | [diff] [blame] | 14 | |
Peter Zijlstra | 3a587f4 | 2007-10-16 23:25:44 -0700 | [diff] [blame] | 15 | void percpu_counter_set(struct percpu_counter *fbc, s64 amount) |
| 16 | { |
| 17 | int cpu; |
| 18 | |
| 19 | spin_lock(&fbc->lock); |
| 20 | for_each_possible_cpu(cpu) { |
| 21 | s32 *pcount = per_cpu_ptr(fbc->counters, cpu); |
| 22 | *pcount = 0; |
| 23 | } |
| 24 | fbc->count = amount; |
| 25 | spin_unlock(&fbc->lock); |
| 26 | } |
| 27 | EXPORT_SYMBOL(percpu_counter_set); |
| 28 | |
Peter Zijlstra | 20e8976 | 2007-10-16 23:25:43 -0700 | [diff] [blame] | 29 | void __percpu_counter_add(struct percpu_counter *fbc, s64 amount, s32 batch) |
Ravikiran G Thirumalai | 3cbc564 | 2006-06-23 02:05:40 -0700 | [diff] [blame] | 30 | { |
Peter Zijlstra | 20e8976 | 2007-10-16 23:25:43 -0700 | [diff] [blame] | 31 | s64 count; |
Mingming Cao | 0216bfc | 2006-06-23 02:05:41 -0700 | [diff] [blame] | 32 | s32 *pcount; |
Ravikiran G Thirumalai | 3cbc564 | 2006-06-23 02:05:40 -0700 | [diff] [blame] | 33 | int cpu = get_cpu(); |
| 34 | |
| 35 | pcount = per_cpu_ptr(fbc->counters, cpu); |
| 36 | count = *pcount + amount; |
Peter Zijlstra | 252e0ba | 2007-10-16 23:25:43 -0700 | [diff] [blame] | 37 | if (count >= batch || count <= -batch) { |
Ravikiran G Thirumalai | 3cbc564 | 2006-06-23 02:05:40 -0700 | [diff] [blame] | 38 | spin_lock(&fbc->lock); |
| 39 | fbc->count += count; |
| 40 | *pcount = 0; |
| 41 | spin_unlock(&fbc->lock); |
| 42 | } else { |
| 43 | *pcount = count; |
| 44 | } |
| 45 | put_cpu(); |
| 46 | } |
Peter Zijlstra | 252e0ba | 2007-10-16 23:25:43 -0700 | [diff] [blame] | 47 | EXPORT_SYMBOL(__percpu_counter_add); |
Ravikiran G Thirumalai | 3cbc564 | 2006-06-23 02:05:40 -0700 | [diff] [blame] | 48 | |
| 49 | /* |
| 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() |
| 52 | */ |
Andrew Morton | 02d2116 | 2008-12-09 13:14:14 -0800 | [diff] [blame] | 53 | s64 __percpu_counter_sum(struct percpu_counter *fbc) |
Ravikiran G Thirumalai | 3cbc564 | 2006-06-23 02:05:40 -0700 | [diff] [blame] | 54 | { |
Mingming Cao | 0216bfc | 2006-06-23 02:05:41 -0700 | [diff] [blame] | 55 | s64 ret; |
Ravikiran G Thirumalai | 3cbc564 | 2006-06-23 02:05:40 -0700 | [diff] [blame] | 56 | int cpu; |
| 57 | |
| 58 | spin_lock(&fbc->lock); |
| 59 | ret = fbc->count; |
Andrew Morton | b4ef0296 | 2007-07-15 23:39:51 -0700 | [diff] [blame] | 60 | for_each_online_cpu(cpu) { |
Mingming Cao | 0216bfc | 2006-06-23 02:05:41 -0700 | [diff] [blame] | 61 | s32 *pcount = per_cpu_ptr(fbc->counters, cpu); |
Ravikiran G Thirumalai | 3cbc564 | 2006-06-23 02:05:40 -0700 | [diff] [blame] | 62 | ret += *pcount; |
| 63 | } |
| 64 | spin_unlock(&fbc->lock); |
Peter Zijlstra | bf1d89c | 2007-10-16 23:25:45 -0700 | [diff] [blame] | 65 | return ret; |
Ravikiran G Thirumalai | 3cbc564 | 2006-06-23 02:05:40 -0700 | [diff] [blame] | 66 | } |
Peter Zijlstra | bf1d89c | 2007-10-16 23:25:45 -0700 | [diff] [blame] | 67 | EXPORT_SYMBOL(__percpu_counter_sum); |
Andrew Morton | c67ad91 | 2007-07-15 23:39:51 -0700 | [diff] [blame] | 68 | |
Peter Zijlstra | ea31951 | 2008-12-26 15:08:55 +0100 | [diff] [blame] | 69 | int __percpu_counter_init(struct percpu_counter *fbc, s64 amount, |
| 70 | struct lock_class_key *key) |
Andrew Morton | c67ad91 | 2007-07-15 23:39:51 -0700 | [diff] [blame] | 71 | { |
| 72 | spin_lock_init(&fbc->lock); |
Peter Zijlstra | ea31951 | 2008-12-26 15:08:55 +0100 | [diff] [blame] | 73 | lockdep_set_class(&fbc->lock, key); |
Andrew Morton | c67ad91 | 2007-07-15 23:39:51 -0700 | [diff] [blame] | 74 | fbc->count = amount; |
| 75 | fbc->counters = alloc_percpu(s32); |
Peter Zijlstra | 833f407 | 2007-10-16 23:25:45 -0700 | [diff] [blame] | 76 | if (!fbc->counters) |
| 77 | return -ENOMEM; |
Andrew Morton | c67ad91 | 2007-07-15 23:39:51 -0700 | [diff] [blame] | 78 | #ifdef CONFIG_HOTPLUG_CPU |
| 79 | mutex_lock(&percpu_counters_lock); |
| 80 | list_add(&fbc->list, &percpu_counters); |
| 81 | mutex_unlock(&percpu_counters_lock); |
| 82 | #endif |
Peter Zijlstra | 833f407 | 2007-10-16 23:25:45 -0700 | [diff] [blame] | 83 | return 0; |
Andrew Morton | c67ad91 | 2007-07-15 23:39:51 -0700 | [diff] [blame] | 84 | } |
Peter Zijlstra | ea31951 | 2008-12-26 15:08:55 +0100 | [diff] [blame] | 85 | EXPORT_SYMBOL(__percpu_counter_init); |
Peter Zijlstra | dc62a30 | 2007-10-16 23:25:46 -0700 | [diff] [blame] | 86 | |
Andrew Morton | c67ad91 | 2007-07-15 23:39:51 -0700 | [diff] [blame] | 87 | void percpu_counter_destroy(struct percpu_counter *fbc) |
| 88 | { |
Peter Zijlstra | 833f407 | 2007-10-16 23:25:45 -0700 | [diff] [blame] | 89 | if (!fbc->counters) |
| 90 | return; |
| 91 | |
Andrew Morton | c67ad91 | 2007-07-15 23:39:51 -0700 | [diff] [blame] | 92 | #ifdef CONFIG_HOTPLUG_CPU |
| 93 | mutex_lock(&percpu_counters_lock); |
| 94 | list_del(&fbc->list); |
| 95 | mutex_unlock(&percpu_counters_lock); |
| 96 | #endif |
Eric Dumazet | fd3d664 | 2008-12-09 13:14:11 -0800 | [diff] [blame] | 97 | free_percpu(fbc->counters); |
| 98 | fbc->counters = NULL; |
Andrew Morton | c67ad91 | 2007-07-15 23:39:51 -0700 | [diff] [blame] | 99 | } |
| 100 | EXPORT_SYMBOL(percpu_counter_destroy); |
| 101 | |
Eric Dumazet | 179f7eb | 2009-01-06 14:41:04 -0800 | [diff] [blame] | 102 | int percpu_counter_batch __read_mostly = 32; |
| 103 | EXPORT_SYMBOL(percpu_counter_batch); |
| 104 | |
| 105 | static void compute_batch_value(void) |
| 106 | { |
| 107 | int nr = num_online_cpus(); |
| 108 | |
| 109 | percpu_counter_batch = max(32, nr*2); |
| 110 | } |
| 111 | |
Andrew Morton | c67ad91 | 2007-07-15 23:39:51 -0700 | [diff] [blame] | 112 | static int __cpuinit percpu_counter_hotcpu_callback(struct notifier_block *nb, |
| 113 | unsigned long action, void *hcpu) |
| 114 | { |
Eric Dumazet | 179f7eb | 2009-01-06 14:41:04 -0800 | [diff] [blame] | 115 | #ifdef CONFIG_HOTPLUG_CPU |
Andrew Morton | c67ad91 | 2007-07-15 23:39:51 -0700 | [diff] [blame] | 116 | unsigned int cpu; |
| 117 | struct percpu_counter *fbc; |
| 118 | |
Eric Dumazet | 179f7eb | 2009-01-06 14:41:04 -0800 | [diff] [blame] | 119 | compute_batch_value(); |
Andrew Morton | c67ad91 | 2007-07-15 23:39:51 -0700 | [diff] [blame] | 120 | if (action != CPU_DEAD) |
| 121 | return NOTIFY_OK; |
| 122 | |
| 123 | cpu = (unsigned long)hcpu; |
| 124 | mutex_lock(&percpu_counters_lock); |
| 125 | list_for_each_entry(fbc, &percpu_counters, list) { |
| 126 | s32 *pcount; |
Gautham R Shenoy | d2b20b1 | 2007-10-18 23:40:47 -0700 | [diff] [blame] | 127 | unsigned long flags; |
Andrew Morton | c67ad91 | 2007-07-15 23:39:51 -0700 | [diff] [blame] | 128 | |
Gautham R Shenoy | d2b20b1 | 2007-10-18 23:40:47 -0700 | [diff] [blame] | 129 | spin_lock_irqsave(&fbc->lock, flags); |
Andrew Morton | c67ad91 | 2007-07-15 23:39:51 -0700 | [diff] [blame] | 130 | pcount = per_cpu_ptr(fbc->counters, cpu); |
| 131 | fbc->count += *pcount; |
| 132 | *pcount = 0; |
Gautham R Shenoy | d2b20b1 | 2007-10-18 23:40:47 -0700 | [diff] [blame] | 133 | spin_unlock_irqrestore(&fbc->lock, flags); |
Andrew Morton | c67ad91 | 2007-07-15 23:39:51 -0700 | [diff] [blame] | 134 | } |
| 135 | mutex_unlock(&percpu_counters_lock); |
Eric Dumazet | 179f7eb | 2009-01-06 14:41:04 -0800 | [diff] [blame] | 136 | #endif |
Andrew Morton | c67ad91 | 2007-07-15 23:39:51 -0700 | [diff] [blame] | 137 | return NOTIFY_OK; |
| 138 | } |
| 139 | |
Tim Chen | 27f5e0f | 2010-08-09 17:19:04 -0700 | [diff] [blame] | 140 | /* |
| 141 | * Compare counter against given value. |
| 142 | * Return 1 if greater, 0 if equal and -1 if less |
| 143 | */ |
| 144 | int percpu_counter_compare(struct percpu_counter *fbc, s64 rhs) |
| 145 | { |
| 146 | s64 count; |
| 147 | |
| 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())) { |
| 151 | if (count > rhs) |
| 152 | return 1; |
| 153 | else |
| 154 | return -1; |
| 155 | } |
| 156 | /* Need to use precise count */ |
| 157 | count = percpu_counter_sum(fbc); |
| 158 | if (count > rhs) |
| 159 | return 1; |
| 160 | else if (count < rhs) |
| 161 | return -1; |
| 162 | else |
| 163 | return 0; |
| 164 | } |
| 165 | EXPORT_SYMBOL(percpu_counter_compare); |
| 166 | |
Andrew Morton | c67ad91 | 2007-07-15 23:39:51 -0700 | [diff] [blame] | 167 | static int __init percpu_counter_startup(void) |
| 168 | { |
Eric Dumazet | 179f7eb | 2009-01-06 14:41:04 -0800 | [diff] [blame] | 169 | compute_batch_value(); |
Andrew Morton | c67ad91 | 2007-07-15 23:39:51 -0700 | [diff] [blame] | 170 | hotcpu_notifier(percpu_counter_hotcpu_callback, 0); |
| 171 | return 0; |
| 172 | } |
| 173 | module_init(percpu_counter_startup); |