syscnt.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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/macros.h>
  29. #include <kern/spinlock.h>
  30. #include <kern/stream.h>
  31. // Size of the buffer storing a system counter name.
  32. #define SYSCNT_NAME_SIZE 32
  33. #include <kern/syscnt_types.h>
  34. // System counter.
  35. struct syscnt;
  36. /*
  37. * Initialize and register the given counter.
  38. *
  39. * The counter is set to 0.
  40. */
  41. void syscnt_register (struct syscnt *syscnt, const char *name);
  42. #ifdef ATOMIC_HAVE_64B_OPS
  43. static inline void
  44. syscnt_set (struct syscnt *syscnt, uint64_t value)
  45. {
  46. atomic_store_rlx (&syscnt->value, value);
  47. }
  48. static inline void
  49. syscnt_add (struct syscnt *syscnt, int64_t delta)
  50. {
  51. atomic_add_rlx (&syscnt->value, delta);
  52. }
  53. static inline uint64_t
  54. syscnt_read (const struct syscnt *syscnt)
  55. {
  56. return (atomic_load_rlx ((uint64_t *)&syscnt->value));
  57. }
  58. #else
  59. static inline void
  60. syscnt_set (struct syscnt *syscnt, uint64_t value)
  61. {
  62. cpu_flags_t flags;
  63. spinlock_lock_intr_save (&syscnt->lock, &flags);
  64. syscnt->value = value;
  65. spinlock_unlock_intr_restore (&syscnt->lock, flags);
  66. }
  67. static inline void
  68. syscnt_add (struct syscnt *syscnt, int64_t delta)
  69. {
  70. cpu_flags_t flags;
  71. spinlock_lock_intr_save (&syscnt->lock, &flags);
  72. syscnt->value += delta;
  73. spinlock_unlock_intr_restore (&syscnt->lock, flags);
  74. }
  75. static inline uint64_t
  76. syscnt_read (struct syscnt *syscnt)
  77. {
  78. cpu_flags_t flags;
  79. spinlock_lock_intr_save (&syscnt->lock, &flags);
  80. uint64_t value = syscnt->value;
  81. spinlock_unlock_intr_restore (&syscnt->lock, flags);
  82. return (value);
  83. }
  84. #endif
  85. static inline void
  86. syscnt_inc (struct syscnt *syscnt)
  87. {
  88. syscnt_add (syscnt, 1);
  89. }
  90. static inline void
  91. syscnt_dec (struct syscnt *syscnt)
  92. {
  93. syscnt_add (syscnt, -1);
  94. }
  95. /*
  96. * Display system counters.
  97. *
  98. * A prefix can be used to filter the output, where only counters with the
  99. * given prefix are displayed. If NULL, all counters are reported.
  100. */
  101. void syscnt_info (const char *prefix, struct stream *stream);
  102. /*
  103. * This init operation provides :
  104. * - registration of system counters
  105. * - module fully initialized
  106. */
  107. INIT_OP_DECLARE (syscnt_setup);
  108. #endif