timer.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /* $Id$ */
  2. /*
  3. * Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
  4. * Copyright (c) 2013-2024 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. #pragma once
  20. #include "defines.h"
  21. #include "config.h"
  22. #include "common.h"
  23. #include "tcpreplay.h"
  24. #include <math.h>
  25. #include <sys/time.h>
  26. #include <time.h>
  27. /*
  28. * 1 sec = 1,0000 millisec (ms)
  29. * 1 sec = 1,000,000 microsec (us)
  30. * 1 sec = 1,000,000,000 nanosec (ns)
  31. * 1 millisec = 1,000 microsec
  32. * 1 microsec = 1,000 nanosec
  33. */
  34. void timesdiv_float(struct timespec *tvs, float div);
  35. /* convert float time to struct timeval *tvp */
  36. #ifndef float2timer
  37. #define float2timer(time, tvp) \
  38. do { \
  39. (tvp)->tv_sec = time; \
  40. (tvp)->tv_usec = (time - (tvp)->tv_sec) * 100000; \
  41. } while (0)
  42. #endif
  43. /* timesec to float */
  44. #ifndef timer2float
  45. #define timer2float(tvp, time) \
  46. do { \
  47. time = (tvp)->tv_sec; \
  48. time += (float)((tvp)->tv_usec / 10000) * 0.01; \
  49. } while (0)
  50. #endif
  51. #ifndef TIMEVAL_TO_TIMESPEC
  52. #define TIMEVAL_TO_TIMESPEC(tv, ts) \
  53. { \
  54. (ts)->tv_sec = (tv)->tv_sec; \
  55. (ts)->tv_nsec = (tv)->tv_usec * 1000; \
  56. }
  57. #endif
  58. #ifndef TIMESPEC_TO_TIMEVAL
  59. #define TIMESPEC_TO_TIMEVAL(tv, ts) \
  60. { \
  61. (tv)->tv_sec = (ts)->tv_sec; \
  62. (tv)->tv_usec = (ts)->tv_nsec / 1000; \
  63. }
  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. /* add tvp and uvp and store in vvp */
  100. #ifndef timeradd_timespec
  101. #define timeradd_timespec(tvp, uvp, vvp) \
  102. do { \
  103. (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
  104. (vvp)->tv_nsec = (tvp)->tv_nsec + (uvp)->tv_nsec; \
  105. if ((vvp)->tv_nsec >= 1000000000) { \
  106. (vvp)->tv_sec++; \
  107. (vvp)->tv_nsec -= 1000000000; \
  108. } \
  109. } while (0)
  110. #endif
  111. /* add tvp and uvp and store in vvp */
  112. #ifndef timeradd_timeval_timespec
  113. #define timeradd_timeval_timespec(tvp, uvp, vvp) \
  114. do { \
  115. (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
  116. (vvp)->tv_nsec = (tvp)->tv_nsec + (uvp)->tv_usec * 1000; \
  117. if ((vvp)->tv_nsec >= 1000000000) { \
  118. int seconds = (vvp)->tv_nsec % 1000000000; \
  119. (vvp)->tv_sec += seconds; \
  120. (vvp)->tv_nsec -= 1000000000 * seconds; \
  121. } \
  122. } while (0)
  123. #endif
  124. /* subtract uvp from tvp and store in vvp */
  125. #ifndef timersub
  126. #define timersub(tvp, uvp, vvp) \
  127. do { \
  128. (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
  129. (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
  130. if ((vvp)->tv_usec < 0) { \
  131. (vvp)->tv_sec--; \
  132. (vvp)->tv_usec += 1000000; \
  133. } \
  134. } while (0)
  135. #endif
  136. #ifndef timessub
  137. #define timessub(tsp, usp, vsp) \
  138. do { \
  139. (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \
  140. (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \
  141. if ((vsp)->tv_nsec < 0) { \
  142. (vsp)->tv_sec--; \
  143. (vsp)->tv_nsec += 1000000000; \
  144. } \
  145. } while (0)
  146. #endif
  147. /* compare tvp and uvp using cmp */
  148. #ifndef timercmp
  149. #define timercmp(tvp, uvp, cmp) \
  150. (((tvp)->tv_sec == (uvp)->tv_sec) ? ((tvp)->tv_usec cmp(uvp)->tv_usec) : ((tvp)->tv_sec cmp(uvp)->tv_sec))
  151. #endif
  152. #ifndef timescmp
  153. #define timescmp(tsp, usp, cmp) \
  154. (((tsp)->tv_sec == (usp)->tv_sec) ? ((tsp)->tv_nsec cmp(usp)->tv_nsec) : ((tsp)->tv_sec cmp(usp)->tv_sec))
  155. #endif
  156. typedef struct timeval timestamp_t;
  157. void init_timestamp(struct timespec *timestamp);
  158. int get_current_time(struct timespec *timestamp);