percpu_counter.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. void 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 void percpu_counter_init(struct percpu_counter *fbc, s64 amount)
  68. {
  69. fbc->count = amount;
  70. }
  71. static inline void percpu_counter_destroy(struct percpu_counter *fbc)
  72. {
  73. }
  74. static inline void percpu_counter_set(struct percpu_counter *fbc, s64 amount)
  75. {
  76. fbc->count = amount;
  77. }
  78. #define __percpu_counter_add(fbc, amount, batch) \
  79. percpu_counter_add(fbc, amount)
  80. static inline void
  81. percpu_counter_add(struct percpu_counter *fbc, s64 amount)
  82. {
  83. preempt_disable();
  84. fbc->count += amount;
  85. preempt_enable();
  86. }
  87. static inline s64 percpu_counter_read(struct percpu_counter *fbc)
  88. {
  89. return fbc->count;
  90. }
  91. static inline s64 percpu_counter_read_positive(struct percpu_counter *fbc)
  92. {
  93. return fbc->count;
  94. }
  95. static inline s64 percpu_counter_sum_positive(struct percpu_counter *fbc)
  96. {
  97. return percpu_counter_read_positive(fbc);
  98. }
  99. static inline s64 percpu_counter_sum(struct percpu_counter *fbc)
  100. {
  101. return percpu_counter_read(fbc);
  102. }
  103. #endif /* CONFIG_SMP */
  104. static inline void percpu_counter_inc(struct percpu_counter *fbc)
  105. {
  106. percpu_counter_add(fbc, 1);
  107. }
  108. static inline void percpu_counter_dec(struct percpu_counter *fbc)
  109. {
  110. percpu_counter_add(fbc, -1);
  111. }
  112. static inline void percpu_counter_sub(struct percpu_counter *fbc, s64 amount)
  113. {
  114. percpu_counter_add(fbc, -amount);
  115. }
  116. #endif /* _LINUX_PERCPU_COUNTER_H */