timer.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. #include "config.h"
  20. #include "defines.h"
  21. #include "common.h"
  22. #include "timer.h"
  23. #include <stdlib.h>
  24. /* Miscellaneous timeval routines */
  25. /**
  26. * Divide tvp by div, storing the result in tvp
  27. */
  28. void
  29. timerdiv_float(struct timeval *tvp, float div)
  30. {
  31. double interval;
  32. if (div == 0 || div == 1)
  33. return;
  34. interval = ((double)tvp->tv_sec * 1000000 + tvp->tv_usec) / (double)div;
  35. tvp->tv_sec = interval / (int)1000000;
  36. tvp->tv_usec = interval - (tvp->tv_sec * 1000000);
  37. }
  38. /* Divide tvs by div, storing the result in tvs */
  39. void timesdiv_float(struct timespec *tvs, float div)
  40. {
  41. double interval;
  42. if (div == 0 || div == 1)
  43. return;
  44. interval = ((double)tvs->tv_sec * 1000000000 + tvs->tv_nsec) / (double)div;
  45. tvs->tv_sec = interval / (int)1000000000;
  46. tvs->tv_nsec = interval - (tvs->tv_sec * 1000000000);
  47. }
  48. void
  49. timerdiv(struct timeval *tvp, COUNTER div)
  50. {
  51. uint64_t interval;
  52. if (div == 0 || div == 1)
  53. return;
  54. interval = (uint64_t)tvp->tv_sec * 1000000 + tvp->tv_usec;
  55. do_div(interval, div);
  56. tvp->tv_sec = interval / 1000000;
  57. tvp->tv_usec = interval - (tvp->tv_sec * 1000000);
  58. }
  59. /* Divide tvs by div, storing the result in tvs */
  60. void timesdiv(struct timespec *tvs, COUNTER div)
  61. {
  62. uint64_t interval;
  63. if (div == 0 || div == 1)
  64. return;
  65. interval = (uint64_t)tvs->tv_sec * 1000000000 + tvs->tv_nsec;
  66. do_div(interval, div);
  67. tvs->tv_sec = interval / 1000000000;
  68. tvs->tv_nsec = interval - (tvs->tv_sec * 1000000000);
  69. }
  70. void
  71. init_timestamp(timestamp_t *ctx)
  72. {
  73. timerclear(ctx);
  74. }