timer.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /* $Id: timer.h 2061 2008-06-20 07:00:39Z aturner $ */
  2. /*
  3. * Copyright (c) 2001-2007 Aaron Turner.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the names of the copyright owners nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  20. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  23. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  25. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  27. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  28. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  29. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #ifndef _TIMER_H_
  32. #define _TIMER_H_
  33. #include "config.h"
  34. #include "defines.h"
  35. #include "tcpreplay.h"
  36. #include "common.h"
  37. #include <time.h>
  38. #include <sys/time.h>
  39. #include <math.h>
  40. #ifdef HAVE_ABSOLUTE_TIME
  41. #include <CoreServices/CoreServices.h>
  42. #endif
  43. /* AbsoluteTime methods */
  44. #ifndef NonZero
  45. #define NonZero(x) ((x).hi | (x).lo)
  46. #endif
  47. #ifndef SetZero
  48. #define SetZero(x) do { (x).hi = 0 ; (x).lo = 0; } while(0)
  49. #endif
  50. #ifndef CopyAbsolute
  51. #define CopyAbsolute(x, y) do { (x).lo = (y).lo ; (x).hi = (y).hi; } while (0)
  52. #endif
  53. #ifndef AbsoluteCmp
  54. #define AbsoluteCmp(left, right, cmp) \
  55. (((left)->hi == (right)->hi) ? \
  56. ((left)->lo cmp (right)->lo) : \
  57. ((left)->hi cmp (right)->hi))
  58. #endif
  59. /*
  60. * 1 sec = 1,0000 millisec (ms)
  61. * 1 sec = 1,000,000 microsec (us)
  62. * 1 sec = 1,000,000,000 nanosec (ns)
  63. * 1 millisec = 1,000 microsec
  64. * 1 microsec = 1,000 nanosec
  65. */
  66. void timerdiv(struct timeval *tvp, float div);
  67. void timesdiv(struct timespec *tvs, float div);
  68. /* convert float time to struct timeval *tvp */
  69. #ifndef float2timer
  70. #define float2timer(time, tvp) \
  71. do { \
  72. tvp->tv_sec = time; \
  73. tvp->tv_usec = (time - tvp->tv_sec) * 100000; \
  74. } while (0)
  75. #endif
  76. #ifndef TIMEVAL_TO_TIMESPEC
  77. #define TIMEVAL_TO_TIMESPEC(tv, ts) { \
  78. (ts)->tv_sec = (tv)->tv_sec; \
  79. (ts)->tv_nsec = (tv)->tv_usec * 1000; }
  80. #endif
  81. #ifndef TIMESPEC_TO_TIMEVAL
  82. #define TIMESPEC_TO_TIMEVAL(tv, ts) { \
  83. (tv)->tv_sec = (ts)->tv_sec; \
  84. (tv)->tv_usec = (ts)->tv_nsec / 1000; }
  85. #endif
  86. #ifndef ROUND_TIMESPEC_TO_MICROSEC
  87. #define ROUND_TIMESPEC_TO_MICROSEC(ts) \
  88. do { \
  89. (ts)->tv_nsec = ((((ts)->tv_nsec / 1000) + ((ts)->tv_nsec % 1000 >= 500 ? 1 : 0)) * 1000); \
  90. } while (0)
  91. #endif
  92. /* zero out a timer */
  93. #ifndef timerclear
  94. #define timerclear(tvp) (tvp)->tv_sec = (tvp)->tv_usec = 0
  95. #endif
  96. /* zero out a timespec */
  97. #ifndef timesclear
  98. #define timesclear(tvs) (tvs)->tv_sec = (tvs)->tv_nsec = 0
  99. #endif
  100. /* is timer non-zero? */
  101. #ifndef timerisset
  102. #define timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
  103. #endif
  104. /* is timespec non-zero? */
  105. #ifndef timesisset
  106. #define timesisset(tvs) ((tvs)->tv_sec || (tvs)->tv_nsec)
  107. #endif
  108. /* add tvp and uvp and store in vvp */
  109. #ifndef timeradd
  110. #define timeradd(tvp, uvp, vvp) \
  111. do { \
  112. (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
  113. (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
  114. if ((vvp)->tv_usec >= 1000000) { \
  115. (vvp)->tv_sec++; \
  116. (vvp)->tv_usec -= 1000000; \
  117. } \
  118. } while (0)
  119. #endif
  120. /* subtract uvp from tvp and store in vvp */
  121. #ifndef timersub
  122. #define timersub(tvp, uvp, vvp) \
  123. do { \
  124. (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
  125. (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
  126. if ((vvp)->tv_usec < 0) { \
  127. (vvp)->tv_sec--; \
  128. (vvp)->tv_usec += 1000000; \
  129. } \
  130. } while (0)
  131. #endif
  132. #ifndef timessub
  133. #define timessub(tsp, usp, vsp) \
  134. do { \
  135. (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \
  136. (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \
  137. if ((vsp)->tv_nsec < 0) { \
  138. (vsp)->tv_sec--; \
  139. (vsp)->tv_nsec += 1000000000; \
  140. } \
  141. } while (0)
  142. #endif
  143. /* compare tvp and uvp using cmp */
  144. #ifndef timercmp
  145. #define timercmp(tvp, uvp, cmp) \
  146. (((tvp)->tv_sec == (uvp)->tv_sec) ? \
  147. ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
  148. ((tvp)->tv_sec cmp (uvp)->tv_sec))
  149. #endif
  150. #ifndef timescmp
  151. #define timescmp(tsp, usp, cmp) \
  152. (((tsp)->tv_sec == (usp)->tv_sec) ? \
  153. ((tsp)->tv_nsec cmp (usp)->tv_nsec) : \
  154. ((tsp)->tv_sec cmp (usp)->tv_sec))
  155. #endif
  156. /* multiply tvp by x and store in uvp */
  157. #define timermul(tvp, uvp, x) \
  158. do { \
  159. (uvp)->tv_sec = (tvp)->tv_sec * x; \
  160. (uvp)->tv_usec = (tvp)->tv_usec * x; \
  161. while((uvp)->tv_usec > 1000000) { \
  162. (uvp)->tv_sec++; \
  163. (uvp)->tv_usec -= 1000000; \
  164. } \
  165. } while(0)
  166. #ifdef HAVE_ABSOLUTE_TIME
  167. typedef AbsoluteTime delta_t;
  168. #else
  169. typedef struct timeval delta_t;
  170. #endif
  171. /*
  172. * starts a timer so we can figure out how much time has passed
  173. * when we call get_delta_timer()
  174. */
  175. static inline void
  176. start_delta_time(delta_t *ctx)
  177. {
  178. #ifdef HAVE_ABSOLUTE_TIME
  179. *ctx = UpTime();
  180. #else
  181. gettimeofday(ctx, NULL);
  182. #endif
  183. }
  184. void init_delta_time(delta_t *ctx);
  185. /*
  186. * returns the amount of time that has passed since the
  187. * last time you called start_delta_time()
  188. */
  189. static inline void
  190. get_delta_time(delta_t *ctx, struct timespec *ret)
  191. {
  192. /* OS X has absolute time */
  193. #ifdef HAVE_ABSOLUTE_TIME
  194. AbsoluteTime now, delta;
  195. Nanoseconds nano;
  196. now = UpTime();
  197. if (! NonZero(*ctx)) {
  198. timesclear(ret);
  199. } else {
  200. delta = SubAbsoluteFromAbsolute(now, *ctx);
  201. nano = AbsoluteToNanoseconds(delta);
  202. NANOSEC_TO_TIMESPEC(UnsignedWideToUInt64(nano) / 10, ret);
  203. }
  204. /* Everyone else just uses gettimeofday */
  205. #else
  206. struct timeval now, delta;
  207. gettimeofday(&now, NULL);
  208. if (!timerisset(ctx)) {
  209. timesclear(ret);
  210. } else {
  211. timersub(&now, ctx, &delta);
  212. TIMEVAL_TO_TIMESPEC(&delta, ret);
  213. }
  214. #endif
  215. }
  216. #endif /* _TIMER_H_ */