counters.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2010 iptelorg GmbH
  5. *
  6. * Permission to use, copy, modify, and distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. /** counter/stats.
  19. * @file counters.h
  20. * @ingroup: core
  21. *
  22. * Example usage:
  23. * 1. register (must be before forking, e.g. from mod_init()):
  24. * counter_handle_t h;
  25. * counter_register(&h, "my_counters", "foo", 0, 0, 0, "test counter", 0);
  26. * 2. increment/add:
  27. * counter_inc(h);
  28. * counter_add(h, 100);
  29. * 3. get and existing counter handle, knowing its group and name
  30. * counter_lookup(&h, "my_counters", "foo");
  31. * 4. get a counter value (the handle can be obtained like above)
  32. * val = counter_get(h);
  33. */
  34. /*
  35. * History:
  36. * --------
  37. * 2010-08-06 initial version (andrei)
  38. */
  39. #ifndef __counters_h
  40. #define __counters_h
  41. #include "pt.h"
  42. /* counter flags */
  43. #define CNT_F_NO_RESET 1 /* don't reset */
  44. typedef long counter_val_t;
  45. /* use a struct. to force errors on direct access attempts */
  46. struct counter_handle_s {
  47. unsigned short id;
  48. };
  49. struct counter_val_s {
  50. counter_val_t v;
  51. };
  52. typedef struct counter_handle_s counter_handle_t;
  53. typedef struct counter_val_s counter_array_t;
  54. typedef counter_val_t (*counter_cbk_f)(counter_handle_t h, void* param);
  55. /* counter definition structure, used in zero term. arrays for more
  56. * convenient registration of several counters at once
  57. * (see counter_register_array(group, counter_array)).
  58. */
  59. struct counter_def_s {
  60. counter_handle_t* handle; /** if non 0, will be filled with the counter
  61. handle */
  62. const char* name; /**< counter name (inside the group) */
  63. int flags; /**< counter flags */
  64. counter_cbk_f get_cbk; /**< callback function for reading */
  65. void* get_cbk_param; /**< callback parameter */
  66. const char* descr; /**< description/documentation string */
  67. };
  68. typedef struct counter_def_s counter_def_t;
  69. extern counter_array_t* _cnts_vals;
  70. extern int _cnts_row_len; /* number of elements per row */
  71. int counters_initialized(void);
  72. int init_counters(void);
  73. void destroy_counters(void);
  74. int counters_prefork_init(int max_process_no);
  75. int counter_register_array(const char* group, counter_def_t* defs);
  76. int counter_register( counter_handle_t* handle, const char* group,
  77. const char* name, int flags,
  78. counter_cbk_f cbk, void* cbk_param,
  79. const char* doc,
  80. int reg_flags);
  81. int counter_lookup(counter_handle_t* handle,
  82. const char* group, const char* name);
  83. int counter_lookup_str(counter_handle_t* handle, str* group, str* name);
  84. void counter_reset(counter_handle_t handle);
  85. counter_val_t counter_get_val(counter_handle_t handle);
  86. counter_val_t counter_get_raw_val(counter_handle_t handle);
  87. char* counter_get_name(counter_handle_t handle);
  88. char* counter_get_group(counter_handle_t handle);
  89. char* counter_get_doc(counter_handle_t handle);
  90. /** gets the per process value of counter h for process p_no.
  91. * Note that if used before counter_prefork_init() process_no is 0
  92. * and _cnts_vals will point into a temporary one "row" array.
  93. */
  94. #define counter_pprocess_val(p_no, h) \
  95. _cnts_vals[(p_no) * _cnts_row_len + (h).id].v
  96. /** increments a counter.
  97. * @param handle - counter handle.
  98. */
  99. inline static void counter_inc(counter_handle_t handle)
  100. {
  101. counter_pprocess_val(process_no, handle)++;
  102. }
  103. /** adds a value to a counter.
  104. * @param handle - counter handle.
  105. * @param v - value.
  106. */
  107. inline static void counter_add(counter_handle_t handle, int v)
  108. {
  109. counter_pprocess_val(process_no, handle)+=v;
  110. }
  111. void counter_iterate_grp_names(void (*cbk)(void* p, str* grp_name), void* p);
  112. void counter_iterate_grp_var_names( const char* group,
  113. void (*cbk)(void* p, str* var_name),
  114. void* p);
  115. void counter_iterate_grp_vars(const char* group,
  116. void (*cbk)(void* p, str* g, str* n,
  117. counter_handle_t h),
  118. void *p);
  119. #endif /*__counters_h*/
  120. /* vi: set ts=4 sw=4 tw=79:ai:cindent: */