percpu_counter.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. void percpu_counter_destroy(struct percpu_counter *fbc);
  30. void percpu_counter_set(struct percpu_counter *fbc, s64 amount);
  31. void __percpu_counter_add(struct percpu_counter *fbc, s64 amount, s32 batch);
  32. s64 __percpu_counter_sum(struct percpu_counter *fbc);
  33. static inline void percpu_counter_add(struct percpu_counter *fbc, s64 amount)
  34. {
  35. __percpu_counter_add(fbc, amount, FBC_BATCH);
  36. }
  37. static inline s64 percpu_counter_sum_positive(struct percpu_counter *fbc)
  38. {
  39. s64 ret = __percpu_counter_sum(fbc);
  40. return ret < 0 ? 0 : ret;
  41. }
  42. static inline s64 percpu_counter_sum(struct percpu_counter *fbc)
  43. {
  44. return __percpu_counter_sum(fbc);
  45. }
  46. static inline s64 percpu_counter_read(struct percpu_counter *fbc)
  47. {
  48. return fbc->count;
  49. }
  50. /*
  51. * It is possible for the percpu_counter_read() to return a small negative
  52. * number for some counter which should never be negative.
  53. *
  54. */
  55. static inline s64 percpu_counter_read_positive(struct percpu_counter *fbc)
  56. {
  57. s64 ret = fbc->count;
  58. barrier(); /* Prevent reloads of fbc->count */
  59. if (ret >= 0)
  60. return ret;
  61. return 1;
  62. }
  63. #else
  64. struct percpu_counter {
  65. s64 count;
  66. };
  67. static inline int percpu_counter_init(struct percpu_counter *fbc, s64 amount)
  68. {
  69. fbc->count = amount;
  70. return 0;
  71. }
  72. static inline void percpu_counter_destroy(struct percpu_counter *fbc)
  73. {
  74. }
  75. static inline void percpu_counter_set(struct percpu_counter *fbc, s64 amount)
  76. {
  77. fbc->count = amount;
  78. }
  79. #define __percpu_counter_add(fbc, amount, batch) \
  80. percpu_counter_add(fbc, amount)
  81. static inline void
  82. percpu_counter_add(struct percpu_counter *fbc, s64 amount)
  83. {
  84. preempt_disable();
  85. fbc->count += amount;
  86. preempt_enable();
  87. }
  88. static inline s64 percpu_counter_read(struct percpu_counter *fbc)
  89. {
  90. return fbc->count;
  91. }
  92. static inline s64 percpu_counter_read_positive(struct percpu_counter *fbc)
  93. {
  94. return fbc->count;
  95. }
  96. static inline s64 percpu_counter_sum_positive(struct percpu_counter *fbc)
  97. {
  98. return percpu_counter_read_positive(fbc);
  99. }
  100. static inline s64 percpu_counter_sum(struct percpu_counter *fbc)
  101. {
  102. return percpu_counter_read(fbc);
  103. }
  104. #endif /* CONFIG_SMP */
  105. static inline void percpu_counter_inc(struct percpu_counter *fbc)
  106. {
  107. percpu_counter_add(fbc, 1);
  108. }
  109. static inline void percpu_counter_dec(struct percpu_counter *fbc)
  110. {
  111. percpu_counter_add(fbc, -1);
  112. }
  113. static inline void percpu_counter_sub(struct percpu_counter *fbc, s64 amount)
  114. {
  115. percpu_counter_add(fbc, -amount);
  116. }
  117. #endif /* _LINUX_PERCPU_COUNTER_H */