clocks.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #ifndef NETDATA_CLOCKS_H
  3. #define NETDATA_CLOCKS_H 1
  4. #include "../libnetdata.h"
  5. #ifndef HAVE_STRUCT_TIMESPEC
  6. struct timespec {
  7. time_t tv_sec; /* seconds */
  8. long tv_nsec; /* nanoseconds */
  9. };
  10. #endif
  11. #ifndef HAVE_CLOCKID_T
  12. typedef int clockid_t;
  13. #endif
  14. typedef uint64_t nsec_t;
  15. typedef uint64_t msec_t;
  16. typedef uint64_t usec_t;
  17. typedef int64_t susec_t;
  18. typedef struct heartbeat {
  19. usec_t realtime;
  20. usec_t randomness;
  21. size_t statistics_id;
  22. } heartbeat_t;
  23. /* Linux value is as good as any other */
  24. #ifndef CLOCK_REALTIME
  25. #define CLOCK_REALTIME 0
  26. #endif
  27. #ifndef CLOCK_MONOTONIC
  28. /* fallback to CLOCK_REALTIME if not available */
  29. #define CLOCK_MONOTONIC CLOCK_REALTIME
  30. #endif
  31. #ifndef CLOCK_BOOTTIME
  32. #ifdef CLOCK_UPTIME
  33. /* CLOCK_BOOTTIME falls back to CLOCK_UPTIME on FreeBSD */
  34. #define CLOCK_BOOTTIME CLOCK_UPTIME
  35. #else // CLOCK_UPTIME
  36. /* CLOCK_BOOTTIME falls back to CLOCK_REALTIME */
  37. #define CLOCK_BOOTTIME CLOCK_REALTIME
  38. #endif // CLOCK_UPTIME
  39. #else // CLOCK_BOOTTIME
  40. #ifdef HAVE_CLOCK_GETTIME
  41. #define CLOCK_BOOTTIME_IS_AVAILABLE 1 // required for /proc/uptime
  42. #endif // HAVE_CLOCK_GETTIME
  43. #endif // CLOCK_BOOTTIME
  44. #ifndef NSEC_PER_MSEC
  45. #define NSEC_PER_MSEC 1000000ULL
  46. #endif
  47. #ifndef NSEC_PER_SEC
  48. #define NSEC_PER_SEC 1000000000ULL
  49. #endif
  50. #ifndef NSEC_PER_USEC
  51. #define NSEC_PER_USEC 1000ULL
  52. #endif
  53. #ifndef USEC_PER_SEC
  54. #define USEC_PER_SEC 1000000ULL
  55. #endif
  56. #ifndef MSEC_PER_SEC
  57. #define MSEC_PER_SEC 1000ULL
  58. #endif
  59. #define USEC_PER_MS 1000ULL
  60. #ifndef HAVE_CLOCK_GETTIME
  61. /* Fallback function for POSIX.1-2001 clock_gettime() function.
  62. *
  63. * We use a realtime clock from gettimeofday(), this will
  64. * make systems without clock_gettime() support sensitive
  65. * to time jumps or hibernation/suspend side effects.
  66. */
  67. int clock_gettime(clockid_t clk_id, struct timespec *ts);
  68. #endif
  69. /*
  70. * Three clocks are available (cf. man 3 clock_gettime):
  71. *
  72. * REALTIME clock (i.e. wall-clock):
  73. * This clock is affected by discontinuous jumps in the system time
  74. * (e.g., if the system administrator manually changes the clock), and by the incremental adjustments performed by adjtime(3) and NTP.
  75. *
  76. * MONOTONIC clock
  77. * Clock that cannot be set and represents monotonic time since some unspecified starting point.
  78. * This clock is not affected by discontinuous jumps in the system time
  79. * (e.g., if the system administrator manually changes the clock), but is affected by the incremental adjustments performed by adjtime(3) and NTP.
  80. * If not available on the system, this clock falls back to REALTIME clock.
  81. *
  82. * BOOTTIME clock
  83. * Identical to CLOCK_MONOTONIC, except it also includes any time that the system is suspended.
  84. * This allows applications to get a suspend-aware monotonic clock without having to deal with the complications of CLOCK_REALTIME,
  85. * which may have discontinuities if the time is changed using settimeofday(2).
  86. * If not available on the system, this clock falls back to MONOTONIC clock.
  87. *
  88. * All now_*_timeval() functions fill the `struct timeval` with the time from the appropriate clock.
  89. * Those functions return 0 on success, -1 else with errno set appropriately.
  90. *
  91. * All now_*_sec() functions return the time in seconds from the appropriate clock, or 0 on error.
  92. * All now_*_usec() functions return the time in microseconds from the appropriate clock, or 0 on error.
  93. *
  94. */
  95. int now_realtime_timeval(struct timeval *tv);
  96. time_t now_realtime_sec(void);
  97. usec_t now_realtime_usec(void);
  98. int now_monotonic_timeval(struct timeval *tv);
  99. time_t now_monotonic_sec(void);
  100. usec_t now_monotonic_usec(void);
  101. int now_monotonic_high_precision_timeval(struct timeval *tv);
  102. time_t now_monotonic_high_precision_sec(void);
  103. usec_t now_monotonic_high_precision_usec(void);
  104. int now_boottime_timeval(struct timeval *tv);
  105. time_t now_boottime_sec(void);
  106. usec_t now_boottime_usec(void);
  107. usec_t timeval_usec(struct timeval *tv);
  108. msec_t timeval_msec(struct timeval *tv);
  109. usec_t dt_usec(struct timeval *now, struct timeval *old);
  110. susec_t dt_usec_signed(struct timeval *now, struct timeval *old);
  111. void heartbeat_init(heartbeat_t *hb);
  112. /* Sleeps until next multiple of tick using monotonic clock.
  113. * Returns elapsed time in microseconds since previous heartbeat
  114. */
  115. usec_t heartbeat_next(heartbeat_t *hb, usec_t tick);
  116. void heartbeat_statistics(usec_t *min_ptr, usec_t *max_ptr, usec_t *average_ptr, size_t *count_ptr);
  117. void sleep_usec_with_now(usec_t usec, usec_t started_ut);
  118. #define sleep_usec(usec) sleep_usec_with_now(usec, 0);
  119. void clocks_init(void);
  120. // lower level functions - avoid using directly
  121. time_t now_sec(clockid_t clk_id);
  122. usec_t now_usec(clockid_t clk_id);
  123. int now_timeval(clockid_t clk_id, struct timeval *tv);
  124. collected_number uptime_msec(char *filename);
  125. extern usec_t clock_monotonic_resolution;
  126. extern usec_t clock_realtime_resolution;
  127. void sleep_to_absolute_time(usec_t usec);
  128. #endif /* NETDATA_CLOCKS_H */