pcounter.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #ifndef __LINUX_PCOUNTER_H
  2. #define __LINUX_PCOUNTER_H
  3. struct pcounter {
  4. #ifdef CONFIG_SMP
  5. void (*add)(struct pcounter *self, int inc);
  6. int (*getval)(const struct pcounter *self);
  7. int *per_cpu_values;
  8. #else
  9. int val;
  10. #endif
  11. };
  12. /*
  13. * Special macros to let pcounters use a fast version of {getvalue|add}
  14. * using a static percpu variable per pcounter instead of an allocated one,
  15. * saving one dereference.
  16. * This might be changed if/when dynamic percpu vars become fast.
  17. */
  18. #ifdef CONFIG_SMP
  19. #include <linux/cpumask.h>
  20. #include <linux/percpu.h>
  21. #define DEFINE_PCOUNTER(NAME) \
  22. static DEFINE_PER_CPU(int, NAME##_pcounter_values); \
  23. static void NAME##_pcounter_add(struct pcounter *self, int inc) \
  24. { \
  25. __get_cpu_var(NAME##_pcounter_values) += inc; \
  26. } \
  27. \
  28. static int NAME##_pcounter_getval(const struct pcounter *self) \
  29. { \
  30. int res = 0, cpu; \
  31. \
  32. for_each_possible_cpu(cpu) \
  33. res += per_cpu(NAME##_pcounter_values, cpu); \
  34. return res; \
  35. }
  36. #define PCOUNTER_MEMBER_INITIALIZER(NAME, MEMBER) \
  37. MEMBER = { \
  38. .add = NAME##_pcounter_add, \
  39. .getval = NAME##_pcounter_getval, \
  40. }
  41. extern void pcounter_def_add(struct pcounter *self, int inc);
  42. extern int pcounter_def_getval(const struct pcounter *self);
  43. static inline int pcounter_alloc(struct pcounter *self)
  44. {
  45. int rc = 0;
  46. if (self->add == NULL) {
  47. self->per_cpu_values = alloc_percpu(int);
  48. if (self->per_cpu_values != NULL) {
  49. self->add = pcounter_def_add;
  50. self->getval = pcounter_def_getval;
  51. } else
  52. rc = 1;
  53. }
  54. return rc;
  55. }
  56. static inline void pcounter_free(struct pcounter *self)
  57. {
  58. if (self->per_cpu_values != NULL) {
  59. free_percpu(self->per_cpu_values);
  60. self->per_cpu_values = NULL;
  61. self->getval = NULL;
  62. self->add = NULL;
  63. }
  64. }
  65. static inline void pcounter_add(struct pcounter *self, int inc)
  66. {
  67. self->add(self, inc);
  68. }
  69. static inline int pcounter_getval(const struct pcounter *self)
  70. {
  71. return self->getval(self);
  72. }
  73. #else /* CONFIG_SMP */
  74. static inline void pcounter_add(struct pcounter *self, int inc)
  75. {
  76. self->value += inc;
  77. }
  78. static inline int pcounter_getval(const struct pcounter *self)
  79. {
  80. return self->val;
  81. }
  82. #define DEFINE_PCOUNTER(NAME)
  83. #define PCOUNTER_MEMBER_INITIALIZER(NAME, MEMBER)
  84. #define pcounter_alloc(self) 0
  85. #define pcounter_free(self)
  86. #endif /* CONFIG_SMP */
  87. #endif /* __LINUX_PCOUNTER_H */