percpu_counter.c 3.2 KB

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