percpu_counter.h 3.2 KB

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