timer.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* $Id$ */
  2. /*
  3. * Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
  4. * Copyright (c) 2013-2018 Fred Klassen <tcpreplay at appneta dot com> - AppNeta
  5. *
  6. * The Tcpreplay Suite of tools is free software: you can redistribute it
  7. * and/or modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation, either version 3 of the
  9. * License, or with the authors permission any later version.
  10. *
  11. * The Tcpreplay Suite is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with the Tcpreplay Suite. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef _TIMER_H_
  20. #define _TIMER_H_
  21. #include "config.h"
  22. #include "defines.h"
  23. #include "tcpreplay.h"
  24. #include "common.h"
  25. #include <time.h>
  26. #include <sys/time.h>
  27. #include <math.h>
  28. /*
  29. * 1 sec = 1,0000 millisec (ms)
  30. * 1 sec = 1,000,000 microsec (us)
  31. * 1 sec = 1,000,000,000 nanosec (ns)
  32. * 1 millisec = 1,000 microsec
  33. * 1 microsec = 1,000 nanosec
  34. */
  35. void timerdiv_float(struct timeval *tvp, float div);
  36. void timesdiv_float(struct timespec *tvs, float div);
  37. void timerdiv(struct timeval *tvp, COUNTER div);
  38. void timesdiv(struct timespec *tvs, COUNTER div);
  39. /* convert float time to struct timeval *tvp */
  40. #ifndef float2timer
  41. #define float2timer(time, tvp) \
  42. do { \
  43. (tvp)->tv_sec = time; \
  44. (tvp)->tv_usec = (time - (tvp)->tv_sec) * 100000; \
  45. } while (0)
  46. #endif
  47. /* timesec to float */
  48. #ifndef timer2float
  49. #define timer2float(tvp, time) \
  50. do { \
  51. time = (tvp)->tv_sec; \
  52. time += (float)((tvp)->tv_usec / 10000) * 0.01; \
  53. } while (0)
  54. #endif
  55. #ifndef TIMEVAL_TO_TIMESPEC
  56. #define TIMEVAL_TO_TIMESPEC(tv, ts) { \
  57. (ts)->tv_sec = (tv)->tv_sec; \
  58. (ts)->tv_nsec = (tv)->tv_usec * 1000; }
  59. #endif
  60. #ifndef TIMESPEC_TO_TIMEVAL
  61. #define TIMESPEC_TO_TIMEVAL(tv, ts) { \
  62. (tv)->tv_sec = (ts)->tv_sec; \
  63. (tv)->tv_usec = (ts)->tv_nsec / 1000; }
  64. #endif
  65. #ifndef ROUND_TIMESPEC_TO_MICROSEC
  66. #define ROUND_TIMESPEC_TO_MICROSEC(ts) \
  67. do { \
  68. (ts)->tv_nsec = ((((ts)->tv_nsec / 1000) + ((ts)->tv_nsec % 1000 >= 500 ? 1 : 0)) * 1000); \
  69. } while (0)
  70. #endif
  71. /* zero out a timer */
  72. #ifndef timerclear
  73. #define timerclear(tvp) (tvp)->tv_sec = (tvp)->tv_usec = 0
  74. #endif
  75. /* zero out a timespec */
  76. #ifndef timesclear
  77. #define timesclear(tvs) (tvs)->tv_sec = (tvs)->tv_nsec = 0
  78. #endif
  79. /* is timer non-zero? */
  80. #ifndef timerisset
  81. #define timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
  82. #endif
  83. /* is timespec non-zero? */
  84. #ifndef timesisset
  85. #define timesisset(tvs) ((tvs)->tv_sec || (tvs)->tv_nsec)
  86. #endif
  87. /* add tvp and uvp and store in vvp */
  88. #ifndef timeradd
  89. #define timeradd(tvp, uvp, vvp) \
  90. do { \
  91. (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
  92. (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
  93. if ((vvp)->tv_usec >= 1000000) { \
  94. (vvp)->tv_sec++; \
  95. (vvp)->tv_usec -= 1000000; \
  96. } \
  97. } while (0)
  98. #endif
  99. /* subtract uvp from tvp and store in vvp */
  100. #ifndef timersub
  101. #define timersub(tvp, uvp, vvp) \
  102. do { \
  103. (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
  104. (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
  105. if ((vvp)->tv_usec < 0) { \
  106. (vvp)->tv_sec--; \
  107. (vvp)->tv_usec += 1000000; \
  108. } \
  109. } while (0)
  110. #endif
  111. #ifndef timessub
  112. #define timessub(tsp, usp, vsp) \
  113. do { \
  114. (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \
  115. (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \
  116. if ((vsp)->tv_nsec < 0) { \
  117. (vsp)->tv_sec--; \
  118. (vsp)->tv_nsec += 1000000000; \
  119. } \
  120. } while (0)
  121. #endif
  122. /* compare tvp and uvp using cmp */
  123. #ifndef timercmp
  124. #define timercmp(tvp, uvp, cmp) \
  125. (((tvp)->tv_sec == (uvp)->tv_sec) ? \
  126. ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
  127. ((tvp)->tv_sec cmp (uvp)->tv_sec))
  128. #endif
  129. #ifndef timescmp
  130. #define timescmp(tsp, usp, cmp) \
  131. (((tsp)->tv_sec == (usp)->tv_sec) ? \
  132. ((tsp)->tv_nsec cmp (usp)->tv_nsec) : \
  133. ((tsp)->tv_sec cmp (usp)->tv_sec))
  134. #endif
  135. /* multiply tvp by x and store in uvp */
  136. #define timermul(tvp, uvp, x) \
  137. do { \
  138. (uvp)->tv_sec = (tvp)->tv_sec * x; \
  139. (uvp)->tv_usec = (tvp)->tv_usec * x; \
  140. while((uvp)->tv_usec > 1000000) { \
  141. (uvp)->tv_sec++; \
  142. (uvp)->tv_usec -= 1000000; \
  143. } \
  144. } while(0)
  145. typedef struct timeval timestamp_t;
  146. void init_timestamp(timestamp_t *ctx);
  147. #endif /* _TIMER_H_ */