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