timer.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* $Id: timer.h 1854 2007-05-02 04:41:29Z 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. /*
  41. * 1 sec = 1,000,000 microsec
  42. * 1 microsec = 1,000 nanosec
  43. * 1 sec = 1,000,000,000 nanosec
  44. */
  45. void timerdiv(struct timeval *tvp, float div);
  46. /* convert float time to struct timeval *tvp */
  47. #ifndef float2timer
  48. #define float2timer(time, tvp) \
  49. do { \
  50. tvp->tv_sec = time; \
  51. tvp->tv_usec = (time - tvp->tv_sec) * 100000; \
  52. } while (0);
  53. #endif
  54. #ifndef TIMEVAL_TO_TIMESPEC
  55. #define TIMEVAL_TO_TIMESPEC(tv, ts) { \
  56. (ts)->tv_sec = (tv)->tv_sec; \
  57. (ts)->tv_nsec = (tv)->tv_usec * 1000; }
  58. #endif
  59. /* zero out a timer */
  60. #ifndef timerclear
  61. #define timerclear(tvp) (tvp)->tv_sec = (tvp)->tv_usec = 0
  62. #endif
  63. /* is timer non-zero? */
  64. #ifndef timerisset
  65. #define timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
  66. #endif
  67. /* add tvp and uvp and store in vvp */
  68. #ifndef timeradd
  69. #define timeradd(tvp, uvp, vvp) \
  70. do { \
  71. (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
  72. (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
  73. if ((vvp)->tv_usec >= 1000000) { \
  74. (vvp)->tv_sec++; \
  75. (vvp)->tv_usec -= 1000000; \
  76. } \
  77. } while (0)
  78. #endif
  79. /* subtract uvp from tvp and store in vvp */
  80. #ifndef timersub
  81. #define timersub(tvp, uvp, vvp) \
  82. do { \
  83. (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
  84. (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
  85. if ((vvp)->tv_usec < 0) { \
  86. (vvp)->tv_sec--; \
  87. (vvp)->tv_usec += 1000000; \
  88. } \
  89. } while (0)
  90. #endif
  91. /* compare tvp and uvp using cmp */
  92. #ifndef timercmp
  93. #define timercmp(tvp, uvp, cmp) \
  94. (((tvp)->tv_sec == (uvp)->tv_sec) ? \
  95. ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
  96. ((tvp)->tv_sec cmp (uvp)->tv_sec))
  97. #endif
  98. /* multiply tvp by x and store in uvp */
  99. #define timermul(tvp, uvp, x) \
  100. do { \
  101. (uvp)->tv_sec = (tvp)->tv_sec * x; \
  102. (uvp)->tv_usec = (tvp)->tv_usec * x; \
  103. while((uvp)->tv_usec > 1000000) { \
  104. (uvp)->tv_sec++; \
  105. (uvp)->tv_usec -= 1000000; \
  106. } \
  107. } while(0)
  108. /* device tvp by x. store in tvp */
  109. #define timerdiv2(tvp, x) \
  110. do { \
  111. (tvp)->tv_sec = (tvp)->tv_sec / x; \
  112. (tvp)->tv_usec = (tvp)->tv_usec / x; \
  113. } while(0)
  114. #endif