percpu_counter.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #ifndef _LINUX_PERCPU_COUNTER_H
  2. #define _LINUX_PERCPU_COUNTER_H
  3. /*
  4. * A simple "approximate counter" for use in ext2 and ext3 superblocks.
  5. *
  6. * WARNING: these things are HUGE. 4 kbytes per counter on 32-way P4.
  7. */
  8. #include <linux/spinlock.h>
  9. #include <linux/smp.h>
  10. #include <linux/list.h>
  11. #include <linux/threads.h>
  12. #include <linux/percpu.h>
  13. #include <linux/types.h>
  14. #ifdef CONFIG_SMP
  15. struct percpu_counter {
  16. spinlock_t lock;
  17. s64 count;
  18. #ifdef CONFIG_HOTPLUG_CPU
  19. struct list_head list; /* All percpu_counters are on a list */
  20. #endif
  21. s32 *counters;
  22. };
  23. #if NR_CPUS >= 16
  24. #define FBC_BATCH (NR_CPUS*2)
  25. #else
  26. #define FBC_BATCH (NR_CPUS*4)
  27. #endif
  28. int percpu_counter_init(struct percpu_counter *fbc, s64 amount);
  29. int percpu_counter_init_irq(struct percpu_counter *fbc, s64 amount);
  30. void percpu_counter_destroy(struct percpu_counter *fbc);
  31. void percpu_counter_set(struct percpu_counter *fbc, s64 amount);
  32. void __percpu_counter_add(struct percpu_counter *fbc, s64 amount, s32 batch);
  33. s64 __percpu_counter_sum(struct percpu_counter *fbc, int set);
  34. static inline void percpu_counter_add(struct percpu_counter *fbc, s64 amount)
  35. {
  36. __percpu_counter_add(fbc, amount, FBC_BATCH);
  37. }
  38. static inline s64 percpu_counter_sum_positive(struct percpu_counter *fbc)
  39. {
  40. s64 ret = __percpu_counter_sum(fbc, 0);
  41. return ret < 0 ? 0 : ret;
  42. }
  43. static inline s64 percpu_counter_sum_and_set(struct percpu_counter *fbc)
  44. {
  45. return __percpu_counter_sum(fbc, 1);
  46. }
  47. static inline s64 percpu_counter_sum(struct percpu_counter *fbc)
  48. {
  49. return __percpu_counter_sum(fbc, 0);
  50. }
  51. static inline s64 percpu_counter_read(struct percpu_counter *fbc)
  52. {
  53. return fbc->count;
  54. }
  55. /*
  56. * It is possible for the percpu_counter_read() to return a small negative
  57. * number for some counter which should never be negative.
  58. *
  59. */
  60. static inline s64 percpu_counter_read_positive(struct percpu_counter *fbc)
  61. {
  62. s64 ret = fbc->count;
  63. barrier(); /* Prevent reloads of fbc->count */
  64. if (ret >= 0)
  65. return ret;
  66. return 1;
  67. }
  68. #else
  69. struct percpu_counter {
  70. s64 count;
  71. };
  72. static inline int percpu_counter_init(struct percpu_counter *fbc, s64 amount)
  73. {
  74. fbc->count = amount;
  75. return 0;
  76. }
  77. #define percpu_counter_init_irq percpu_counter_init
  78. static inline void percpu_counter_destroy(struct percpu_counter *fbc)
  79. {
  80. }
  81. static inline void percpu_counter_set(struct percpu_counter *fbc, s64 amount)
  82. {
  83. fbc->count = amount;
  84. }
  85. #define __percpu_counter_add(fbc, amount, batch) \
  86. percpu_counter_add(fbc, amount)
  87. static inline void
  88. percpu_counter_add(struct percpu_counter *fbc, s64 amount)
  89. {
  90. preempt_disable();
  91. fbc->count += amount;
  92. preempt_enable();
  93. }
  94. static inline s64 percpu_counter_read(struct percpu_counter *fbc)
  95. {
  96. return fbc->count;
  97. }
  98. static inline s64 percpu_counter_read_positive(struct percpu_counter *fbc)
  99. {
  100. return fbc->count;
  101. }
  102. static inline s64 percpu_counter_sum_positive(struct percpu_counter *fbc)
  103. {
  104. return percpu_counter_read_positive(fbc);
  105. }
  106. static inline s64 percpu_counter_sum(struct percpu_counter *fbc)
  107. {
  108. return percpu_counter_read(fbc);
  109. }
  110. #endif /* CONFIG_SMP */
  111. static inline void percpu_counter_inc(struct percpu_counter *fbc)
  112. {
  113. percpu_counter_add(fbc, 1);
  114. }
  115. static inline void percpu_counter_dec(struct percpu_counter *fbc)
  116. {
  117. percpu_counter_add(fbc, -1);
  118. }
  119. static inline void percpu_counter_sub(struct percpu_counter *fbc, s64 amount)
  120. {
  121. percpu_counter_add(fbc, -amount);
  122. }
  123. #endif /* _LINUX_PERCPU_COUNTER_H */