timer.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /* $Id: timer.h 2071 2008-11-09 08:36:02Z 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. /* timesec to float */
  77. #ifndef timer2float
  78. #define timer2float(tvp, time) \
  79. do { \
  80. time = tvp->tv_sec; \
  81. time += (float)(tvp->tv_usec / 10000) * 0.01; \
  82. } while (0)
  83. #endif
  84. #ifndef TIMEVAL_TO_TIMESPEC
  85. #define TIMEVAL_TO_TIMESPEC(tv, ts) { \
  86. (ts)->tv_sec = (tv)->tv_sec; \
  87. (ts)->tv_nsec = (tv)->tv_usec * 1000; }
  88. #endif
  89. #ifndef TIMESPEC_TO_TIMEVAL
  90. #define TIMESPEC_TO_TIMEVAL(tv, ts) { \
  91. (tv)->tv_sec = (ts)->tv_sec; \
  92. (tv)->tv_usec = (ts)->tv_nsec / 1000; }
  93. #endif
  94. #ifndef ROUND_TIMESPEC_TO_MICROSEC
  95. #define ROUND_TIMESPEC_TO_MICROSEC(ts) \
  96. do { \
  97. (ts)->tv_nsec = ((((ts)->tv_nsec / 1000) + ((ts)->tv_nsec % 1000 >= 500 ? 1 : 0)) * 1000); \
  98. } while (0)
  99. #endif
  100. /* zero out a timer */
  101. #ifndef timerclear
  102. #define timerclear(tvp) (tvp)->tv_sec = (tvp)->tv_usec = 0
  103. #endif
  104. /* zero out a timespec */
  105. #ifndef timesclear
  106. #define timesclear(tvs) (tvs)->tv_sec = (tvs)->tv_nsec = 0
  107. #endif
  108. /* is timer non-zero? */
  109. #ifndef timerisset
  110. #define timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
  111. #endif
  112. /* is timespec non-zero? */
  113. #ifndef timesisset
  114. #define timesisset(tvs) ((tvs)->tv_sec || (tvs)->tv_nsec)
  115. #endif
  116. /* add tvp and uvp and store in vvp */
  117. #ifndef timeradd
  118. #define timeradd(tvp, uvp, vvp) \
  119. do { \
  120. (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
  121. (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
  122. if ((vvp)->tv_usec >= 1000000) { \
  123. (vvp)->tv_sec++; \
  124. (vvp)->tv_usec -= 1000000; \
  125. } \
  126. } while (0)
  127. #endif
  128. /* subtract uvp from tvp and store in vvp */
  129. #ifndef timersub
  130. #define timersub(tvp, uvp, vvp) \
  131. do { \
  132. (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
  133. (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
  134. if ((vvp)->tv_usec < 0) { \
  135. (vvp)->tv_sec--; \
  136. (vvp)->tv_usec += 1000000; \
  137. } \
  138. } while (0)
  139. #endif
  140. #ifndef timessub
  141. #define timessub(tsp, usp, vsp) \
  142. do { \
  143. (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \
  144. (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \
  145. if ((vsp)->tv_nsec < 0) { \
  146. (vsp)->tv_sec--; \
  147. (vsp)->tv_nsec += 1000000000; \
  148. } \
  149. } while (0)
  150. #endif
  151. /* compare tvp and uvp using cmp */
  152. #ifndef timercmp
  153. #define timercmp(tvp, uvp, cmp) \
  154. (((tvp)->tv_sec == (uvp)->tv_sec) ? \
  155. ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
  156. ((tvp)->tv_sec cmp (uvp)->tv_sec))
  157. #endif
  158. #ifndef timescmp
  159. #define timescmp(tsp, usp, cmp) \
  160. (((tsp)->tv_sec == (usp)->tv_sec) ? \
  161. ((tsp)->tv_nsec cmp (usp)->tv_nsec) : \
  162. ((tsp)->tv_sec cmp (usp)->tv_sec))
  163. #endif
  164. /* multiply tvp by x and store in uvp */
  165. #define timermul(tvp, uvp, x) \
  166. do { \
  167. (uvp)->tv_sec = (tvp)->tv_sec * x; \
  168. (uvp)->tv_usec = (tvp)->tv_usec * x; \
  169. while((uvp)->tv_usec > 1000000) { \
  170. (uvp)->tv_sec++; \
  171. (uvp)->tv_usec -= 1000000; \
  172. } \
  173. } while(0)
  174. #ifdef HAVE_ABSOLUTE_TIME
  175. typedef AbsoluteTime delta_t;
  176. #else
  177. typedef struct timeval delta_t;
  178. #endif
  179. /*
  180. * starts a timer so we can figure out how much time has passed
  181. * when we call get_delta_timer()
  182. */
  183. static inline void
  184. start_delta_time(delta_t *ctx)
  185. {
  186. #ifdef HAVE_ABSOLUTE_TIME
  187. *ctx = UpTime();
  188. #else
  189. gettimeofday(ctx, NULL);
  190. #endif
  191. }
  192. void init_delta_time(delta_t *ctx);
  193. /*
  194. * returns the amount of time that has passed since the
  195. * last time you called start_delta_time()
  196. */
  197. static inline void
  198. get_delta_time(delta_t *ctx, struct timespec *ret)
  199. {
  200. /* OS X has absolute time */
  201. #ifdef HAVE_ABSOLUTE_TIME
  202. AbsoluteTime now, delta;
  203. Nanoseconds nano;
  204. now = UpTime();
  205. if (! NonZero(*ctx)) {
  206. timesclear(ret);
  207. } else {
  208. delta = SubAbsoluteFromAbsolute(now, *ctx);
  209. nano = AbsoluteToNanoseconds(delta);
  210. NANOSEC_TO_TIMESPEC(UnsignedWideToUInt64(nano) / 10, ret);
  211. }
  212. /* Everyone else just uses gettimeofday */
  213. #else
  214. struct timeval now, delta;
  215. gettimeofday(&now, NULL);
  216. if (!timerisset(ctx)) {
  217. timesclear(ret);
  218. } else {
  219. timersub(&now, ctx, &delta);
  220. TIMEVAL_TO_TIMESPEC(&delta, ret);
  221. }
  222. #endif
  223. }
  224. #endif /* _TIMER_H_ */