kstats_wrapper.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. /** k compatible statistics implemented in terms of sr counters.
  19. * @file kstats_wrapper.h
  20. * @ingroup: libkcore
  21. */
  22. /*
  23. * History:
  24. * --------
  25. * 2010-08-08 initial version (andrei)
  26. */
  27. #include "kstats_wrapper.h"
  28. #ifdef STATISTICS
  29. /** internal wrapper for kamailio type stat callbacks.
  30. * sr counter callbacks are different from the kamailio type stat callbacks.
  31. * This function is meant as a sr counter callback that will call
  32. * k stat callback passed as parameter.
  33. * @param h - not used.
  34. * @param param - k stat callback function pointer (stat_function).
  35. * @return result of calling the passed k stat_function.
  36. */
  37. static counter_val_t cnt_cbk_wrapper(counter_handle_t h, void* param)
  38. {
  39. stat_function k_stat_f;
  40. k_stat_f = param;
  41. return k_stat_f();
  42. }
  43. int register_stat( char *module, char *name, stat_var **pvar, int flags)
  44. {
  45. int cnt_flags;
  46. counter_handle_t h;
  47. int ret;
  48. if (module == 0 || name == 0 || pvar == 0) {
  49. BUG("invalid parameters (%p, %p, %p)\n", module, name, pvar);
  50. return -1;
  51. }
  52. /* translate kamailio stat flags into sr counter flags */
  53. cnt_flags = (flags & STAT_NO_RESET) ? CNT_F_NO_RESET : 0;
  54. if (flags & STAT_IS_FUNC)
  55. ret = counter_register(&h, module, name, cnt_flags,
  56. cnt_cbk_wrapper,(stat_function)pvar,
  57. "kamailio statistic (no description)",
  58. 0);
  59. else
  60. ret = counter_register(&h, module, name, cnt_flags, 0, 0,
  61. "kamailio statistic (no description)", 0);
  62. if (ret < 0) {
  63. if (ret == -2)
  64. ERR("counter %s.%s already registered\n", module, name);
  65. goto error;
  66. }
  67. if (!(flags & STAT_IS_FUNC))
  68. *pvar = (void*)(unsigned long)h.id;
  69. return 0;
  70. error:
  71. if (!(flags & STAT_IS_FUNC))
  72. *pvar = 0;
  73. return -1;
  74. }
  75. int register_module_stats(char *module, stat_export_t *stats)
  76. {
  77. if (module == 0 || *module == 0) {
  78. BUG("null or empty module name\n");
  79. goto error;
  80. }
  81. if (stats == 0 || stats[0].name == 0)
  82. /* empty stats */
  83. return 0;
  84. for (; stats->name; stats++)
  85. if (register_stat(module, stats->name, stats->stat_pointer,
  86. stats->flags) < 0 ){
  87. ERR("failed to add statistic %s.%s\n", module, stats->name);
  88. goto error;
  89. }
  90. return 0;
  91. error:
  92. return -1;
  93. }
  94. #endif /* STATISTICS */
  95. /* vi: set ts=4 sw=4 tw=79:ai:cindent: */