timer.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. #include "timer.h"
  20. #include "config.h"
  21. #include <stdlib.h>
  22. /* Miscellaneous timeval/timespec routines */
  23. /* Divide tvs by div, storing the result in tvs */
  24. void
  25. timesdiv_float(struct timespec *tvs, float div)
  26. {
  27. double interval;
  28. if (div == 0.0 || div == 1.0)
  29. return;
  30. interval = ((double)tvs->tv_sec * 1000000000.0 + (double)tvs->tv_nsec) / (double)div;
  31. tvs->tv_sec = (time_t)interval / (time_t)1000000000;
  32. tvs->tv_nsec = (time_t)interval - (tvs->tv_sec * 1000000000);
  33. }
  34. void
  35. init_timestamp(struct timespec *timestamp)
  36. {
  37. timesclear(timestamp);
  38. }
  39. int
  40. get_current_time(struct timespec *ts)
  41. {
  42. #if defined CLOCK_MONOTONIC || defined _POSIX_C_SOURCE && _POSIX_C_SOURCE >= 199309L
  43. return clock_gettime(CLOCK_MONOTONIC, ts);
  44. #else
  45. struct timeval tv;
  46. int success = gettimeofday(&tv, NULL);
  47. TIMEVAL_TO_TIMESPEC(&tv, ts);
  48. return success;
  49. #endif
  50. }