time.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. *
  3. * Embedded Linux library
  4. *
  5. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. */
  22. #ifdef HAVE_CONFIG_H
  23. #include <config.h>
  24. #endif
  25. #define _GNU_SOURCE
  26. #include <time.h>
  27. #include "time.h"
  28. #include "time-private.h"
  29. #include "random.h"
  30. #include "private.h"
  31. /**
  32. * l_time_now:
  33. *
  34. * Get the running clocktime in microseconds
  35. *
  36. * Returns: Current clock time in microseconds
  37. **/
  38. LIB_EXPORT uint64_t l_time_now(void)
  39. {
  40. struct timespec now;
  41. clock_gettime(CLOCK_BOOTTIME, &now);
  42. return (uint64_t) now.tv_sec * 1000000 + now.tv_nsec / 1000;
  43. }
  44. /**
  45. * l_time_after
  46. *
  47. * Returns: True if time a is after time b
  48. **/
  49. /**
  50. * l_time_before
  51. *
  52. * Returns: True if time a is before time b
  53. **/
  54. /**
  55. * l_time_offset
  56. *
  57. * @time: Start time to calculate offset
  58. * @offset: Amount of time to add to 'time'
  59. *
  60. * Adds an offset to a time value. This checks for overflow, and if detected
  61. * returns UINT64_MAX.
  62. *
  63. * Returns: A time value 'time' + 'offset'. Or UINT64_MAX if time + offset
  64. * exceeds UINT64_MAX.
  65. **/
  66. /* Compute ms + RAND*ms where RAND is in range -0.1 .. 0.1 */
  67. uint64_t _time_fuzz_msecs(uint64_t ms)
  68. {
  69. /* We do this by subtracting 0.1ms and adding 0.1ms * rand[0 .. 2] */
  70. return ms - ms / 10 +
  71. (l_getrandom_uint32() % (2 * L_MSEC_PER_SEC)) *
  72. ms / 10 / L_MSEC_PER_SEC;
  73. }
  74. uint64_t _time_pick_interval_secs(uint32_t min_secs, uint32_t max_secs)
  75. {
  76. uint64_t min_ms = min_secs * L_MSEC_PER_SEC;
  77. uint64_t max_ms = max_secs * L_MSEC_PER_SEC;
  78. return l_getrandom_uint32() % (max_ms + 1 - min_ms) + min_ms;
  79. }
  80. /* Compute a time in ms based on seconds + max_offset * [-1.0 .. 1.0] */
  81. uint64_t _time_fuzz_secs(uint32_t secs, uint32_t max_offset)
  82. {
  83. uint64_t ms = secs * L_MSEC_PER_SEC;
  84. uint64_t r = l_getrandom_uint32();
  85. max_offset *= L_MSEC_PER_SEC;
  86. if (r & 0x80000000)
  87. ms += (r & 0x7fffffff) % max_offset;
  88. else
  89. ms -= (r & 0x7fffffff) % max_offset;
  90. return ms;
  91. }