benchmark.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * $Id: benchmark.h 825 2007-02-16 13:04:16Z bastian $
  3. *
  4. * Benchmarking module for Kamailio
  5. *
  6. * Copyright (C) 2007 Collax GmbH
  7. * (Bastian Friedrich <[email protected]>)
  8. * Copyright (C) 2007 Voice Sistem SRL
  9. *
  10. * This file is part of Kamailio, a free SIP server.
  11. *
  12. * Kamailio is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version
  16. *
  17. * Kamailio is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  25. *
  26. */
  27. /*! \file
  28. * \brief Benchmark :: Module Core
  29. *
  30. * \ingroup benchmark
  31. * - \ref benchmark.c
  32. * - Module: benchmark
  33. */
  34. #ifndef _BENCHMARK_MOD_H_
  35. #define _BENCHMARK_MOD_H_
  36. #include <sys/time.h>
  37. #include <time.h>
  38. #include "benchmark_api.h"
  39. #define BM_NAME_LEN 32
  40. #ifdef BM_CLOCK_REALTIME
  41. /* nano seconds */
  42. typedef struct timespec bm_timeval_t;
  43. #else
  44. /* micro seconds */
  45. typedef struct timeval bm_timeval_t;
  46. #endif
  47. typedef struct benchmark_timer
  48. {
  49. char name[BM_NAME_LEN];
  50. unsigned int id;
  51. int enabled;
  52. bm_timeval_t *start; /* Current timer run */
  53. unsigned long long calls; /* Number of runs of this timer */
  54. unsigned long long sum; /* Accumulated runtime of this timer */
  55. unsigned long long last_sum; /* Accumulated runtime since last logging */
  56. unsigned long long last_max; /* Minimum in current period (between
  57. granularity) */
  58. unsigned long long last_min; /* Maximum ... */
  59. unsigned long long global_max; /* Global minimum, since start */
  60. unsigned long long global_min; /* ... maximum ... */
  61. struct benchmark_timer *next;
  62. } benchmark_timer_t;
  63. static inline int bm_get_time(bm_timeval_t *t)
  64. {
  65. #ifdef BM_CLOCK_REALTIME
  66. if(clock_gettime(CLOCK_REALTIME, t)!=0)
  67. #else
  68. if(gettimeofday(t, NULL))
  69. #endif
  70. {
  71. LM_ERR("error getting current time\n");
  72. return -1;
  73. }
  74. return 0;
  75. }
  76. static inline unsigned long long bm_diff_time(bm_timeval_t *t1, bm_timeval_t *t2)
  77. {
  78. unsigned long long tdiff;
  79. tdiff = t2->tv_sec - t1->tv_sec;
  80. #ifdef BM_CLOCK_REALTIME
  81. tdiff = tdiff*1000000000 + t2->tv_nsec - t1->tv_nsec;
  82. #else
  83. tdiff = tdiff*1000000 + t2->tv_usec - t1->tv_usec;
  84. #endif
  85. return tdiff;
  86. }
  87. #endif /* _BENCHMARK_MOD_H_ */