clocks.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 unsigned long long nsec_t;
  15. typedef unsigned long long msec_t;
  16. typedef unsigned long long usec_t;
  17. typedef long long susec_t;
  18. typedef struct heartbeat {
  19. usec_t monotonic;
  20. usec_t realtime;
  21. } heartbeat_t;
  22. /* Linux value is as good as any other */
  23. #ifndef CLOCK_REALTIME
  24. #define CLOCK_REALTIME 0
  25. #endif
  26. #ifndef CLOCK_MONOTONIC
  27. /* fallback to CLOCK_REALTIME if not available */
  28. #define CLOCK_MONOTONIC CLOCK_REALTIME
  29. #endif
  30. #ifndef CLOCK_BOOTTIME
  31. #ifdef CLOCK_UPTIME
  32. /* CLOCK_BOOTTIME falls back to CLOCK_UPTIME on FreeBSD */
  33. #define CLOCK_BOOTTIME CLOCK_UPTIME
  34. #else // CLOCK_UPTIME
  35. /* CLOCK_BOOTTIME falls back to CLOCK_MONOTONIC */
  36. #define CLOCK_BOOTTIME CLOCK_MONOTONIC
  37. #endif // CLOCK_UPTIME
  38. #else // CLOCK_BOOTTIME
  39. #ifdef HAVE_CLOCK_GETTIME
  40. #define CLOCK_BOOTTIME_IS_AVAILABLE 1 // required for /proc/uptime
  41. #endif // HAVE_CLOCK_GETTIME
  42. #endif // CLOCK_BOOTTIME
  43. #define NSEC_PER_MSEC 1000000ULL
  44. #define NSEC_PER_SEC 1000000000ULL
  45. #define NSEC_PER_USEC 1000ULL
  46. #define USEC_PER_SEC 1000000ULL
  47. #define MSEC_PER_SEC 1000ULL
  48. #define USEC_PER_MS 1000ULL
  49. #ifndef HAVE_CLOCK_GETTIME
  50. /* Fallback function for POSIX.1-2001 clock_gettime() function.
  51. *
  52. * We use a realtime clock from gettimeofday(), this will
  53. * make systems without clock_gettime() support sensitive
  54. * to time jumps or hibernation/suspend side effects.
  55. */
  56. extern int clock_gettime(clockid_t clk_id, struct timespec *ts);
  57. #endif
  58. /*
  59. * Three clocks are available (cf. man 3 clock_gettime):
  60. *
  61. * REALTIME clock (i.e. wall-clock):
  62. * This clock is affected by discontinuous jumps in the system time
  63. * (e.g., if the system administrator manually changes the clock), and by the incremental adjustments performed by adjtime(3) and NTP.
  64. *
  65. * MONOTONIC clock
  66. * Clock that cannot be set and represents monotonic time since some unspecified starting point.
  67. * This clock is not affected by discontinuous jumps in the system time
  68. * (e.g., if the system administrator manually changes the clock), but is affected by the incremental adjustments performed by adjtime(3) and NTP.
  69. * If not available on the system, this clock falls back to REALTIME clock.
  70. *
  71. * BOOTTIME clock
  72. * Identical to CLOCK_MONOTONIC, except it also includes any time that the system is suspended.
  73. * This allows applications to get a suspend-aware monotonic clock without having to deal with the complications of CLOCK_REALTIME,
  74. * which may have discontinuities if the time is changed using settimeofday(2).
  75. * If not available on the system, this clock falls back to MONOTONIC clock.
  76. *
  77. * All now_*_timeval() functions fill the `struct timeval` with the time from the appropriate clock.
  78. * Those functions return 0 on success, -1 else with errno set appropriately.
  79. *
  80. * All now_*_sec() functions return the time in seconds from the approriate clock, or 0 on error.
  81. * All now_*_usec() functions return the time in microseconds from the approriate clock, or 0 on error.
  82. */
  83. extern int now_realtime_timeval(struct timeval *tv);
  84. extern time_t now_realtime_sec(void);
  85. extern usec_t now_realtime_usec(void);
  86. extern int now_monotonic_timeval(struct timeval *tv);
  87. extern time_t now_monotonic_sec(void);
  88. extern usec_t now_monotonic_usec(void);
  89. extern int now_boottime_timeval(struct timeval *tv);
  90. extern time_t now_boottime_sec(void);
  91. extern usec_t now_boottime_usec(void);
  92. extern usec_t timeval_usec(struct timeval *tv);
  93. extern msec_t timeval_msec(struct timeval *tv);
  94. extern usec_t dt_usec(struct timeval *now, struct timeval *old);
  95. extern susec_t dt_usec_signed(struct timeval *now, struct timeval *old);
  96. extern void heartbeat_init(heartbeat_t *hb);
  97. /* Sleeps until next multiple of tick using monotonic clock.
  98. * Returns elapsed time in microseconds since previous heartbeat
  99. */
  100. extern usec_t heartbeat_next(heartbeat_t *hb, usec_t tick);
  101. /* Returns elapsed time in microseconds since last heartbeat */
  102. extern usec_t heartbeat_monotonic_dt_to_now_usec(heartbeat_t *hb);
  103. extern int sleep_usec(usec_t usec);
  104. /*
  105. * When running a binary with CLOCK_BOOTTIME defined on a system with a linux kernel older than Linux 2.6.39 the
  106. * clock_gettime(2) system call fails with EINVAL. In that case it must fall-back to CLOCK_MONOTONIC.
  107. */
  108. void test_clock_boottime(void);
  109. #endif /* NETDATA_CLOCKS_H */