syscnt.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright (c) 2014-2019 Richard Braun.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. *
  18. * System counters.
  19. *
  20. * This module provides 64-bits general-purpose counters that can be
  21. * accessed and modified atomically from any context.
  22. */
  23. #ifndef KERN_SYSCNT_H
  24. #define KERN_SYSCNT_H
  25. #include <stdint.h>
  26. #include <kern/atomic.h>
  27. #include <kern/init.h>
  28. #include <kern/log.h>
  29. #include <kern/macros.h>
  30. #include <kern/spinlock.h>
  31. /*
  32. * Size of the buffer storing a system counter name.
  33. */
  34. #define SYSCNT_NAME_SIZE 32
  35. #include <kern/syscnt_types.h>
  36. /*
  37. * System counter.
  38. */
  39. struct syscnt;
  40. /*
  41. * Initialize and register the given counter.
  42. *
  43. * The counter is set to 0.
  44. */
  45. void syscnt_register(struct syscnt *syscnt, const char *name);
  46. #ifdef ATOMIC_HAVE_64B_OPS
  47. static inline void
  48. syscnt_set(struct syscnt *syscnt, uint64_t value)
  49. {
  50. atomic_store(&syscnt->value, value, ATOMIC_RELAXED);
  51. }
  52. static inline void
  53. syscnt_add(struct syscnt *syscnt, int64_t delta)
  54. {
  55. atomic_add(&syscnt->value, delta, ATOMIC_RELAXED);
  56. }
  57. static inline uint64_t
  58. syscnt_read(const struct syscnt *syscnt)
  59. {
  60. return atomic_load((uint64_t *)&syscnt->value, ATOMIC_RELAXED);
  61. }
  62. #else /* ATOMIC_HAVE_64B_OPS */
  63. static inline void
  64. syscnt_set(struct syscnt *syscnt, uint64_t value)
  65. {
  66. unsigned long flags;
  67. spinlock_lock_intr_save(&syscnt->lock, &flags);
  68. syscnt->value = value;
  69. spinlock_unlock_intr_restore(&syscnt->lock, flags);
  70. }
  71. static inline void
  72. syscnt_add(struct syscnt *syscnt, int64_t delta)
  73. {
  74. unsigned long flags;
  75. spinlock_lock_intr_save(&syscnt->lock, &flags);
  76. syscnt->value += delta;
  77. spinlock_unlock_intr_restore(&syscnt->lock, flags);
  78. }
  79. static inline uint64_t
  80. syscnt_read(struct syscnt *syscnt)
  81. {
  82. unsigned long flags;
  83. uint64_t value;
  84. spinlock_lock_intr_save(&syscnt->lock, &flags);
  85. value = syscnt->value;
  86. spinlock_unlock_intr_restore(&syscnt->lock, flags);
  87. return value;
  88. }
  89. #endif /* ATOMIC_HAVE_64B_OPS */
  90. static inline void
  91. syscnt_inc(struct syscnt *syscnt)
  92. {
  93. syscnt_add(syscnt, 1);
  94. }
  95. static inline void
  96. syscnt_dec(struct syscnt *syscnt)
  97. {
  98. syscnt_add(syscnt, -1);
  99. }
  100. /*
  101. * Display system counters.
  102. *
  103. * A prefix can be used to filter the output, where only counters with the
  104. * given prefix are displayed. If NULL, all counters are reported.
  105. */
  106. void syscnt_info(const char *prefix, log_print_fn_t print_fn);
  107. /*
  108. * This init operation provides :
  109. * - registration of system counters
  110. * - module fully initialized
  111. */
  112. INIT_OP_DECLARE(syscnt_setup);
  113. #endif /* KERN_SYSCNT_H */