1 #ifndef NETDATA_CLOCKS_H
2 #define NETDATA_CLOCKS_H 1
4 #ifndef HAVE_STRUCT_TIMESPEC
6 time_t tv_sec; /* seconds */
7 long tv_nsec; /* nanoseconds */
11 #ifndef HAVE_CLOCKID_T
12 typedef int clockid_t;
15 typedef unsigned long long usec_t;
17 typedef usec_t heartbeat_t;
19 #ifndef HAVE_CLOCK_GETTIME
20 int clock_gettime(clockid_t clk_id, struct timespec *ts);
23 /* Linux value is as good as any other */
24 #ifndef CLOCK_REALTIME
25 #define CLOCK_REALTIME 0
28 #ifndef CLOCK_MONOTONIC
29 /* fallback to CLOCK_REALTIME if not available */
30 #define CLOCK_MONOTONIC CLOCK_REALTIME
33 #ifndef CLOCK_BOOTTIME
34 /* fallback to CLOCK_MONOTONIC if not available */
35 #define CLOCK_BOOTTIME CLOCK_MONOTONIC
37 #ifdef HAVE_CLOCK_GETTIME
38 #define CLOCK_BOOTTIME_IS_AVAILABLE 1 // required for /proc/uptime
42 #define NSEC_PER_SEC 1000000000ULL
43 #define NSEC_PER_MSEC 1000000ULL
44 #define NSEC_PER_USEC 1000ULL
45 #define USEC_PER_SEC 1000000ULL
47 #ifndef HAVE_CLOCK_GETTIME
48 /* Fallback function for POSIX.1-2001 clock_gettime() function.
50 * We use a realtime clock from gettimeofday(), this will
51 * make systems without clock_gettime() support sensitive
52 * to time jumps or hibernation/suspend side effects.
54 extern int clock_gettime(clockid_t clk_id, struct timespec *ts);
57 /* Fills struct timeval with time since EPOCH from real-time clock (i.e. wall-clock).
58 * - Hibernation/suspend time is included
59 * - adjtime()/NTP adjustments affect this clock
60 * Return 0 on succes, -1 else with errno set appropriately.
62 extern int now_realtime_timeval(struct timeval *tv);
64 /* Returns time since EPOCH from real-time clock (i.e. wall-clock).
65 * - Hibernation/suspend time is included
66 * - adjtime()/NTP adjustments affect this clock
68 extern time_t now_realtime_sec(void);
69 extern usec_t now_realtime_usec(void);
71 /* Returns time from monotonic clock if available, real-time clock else.
72 * If monotonic clock is available:
73 * - hibernation/suspend time is not included
74 * - adjtime()/NTP adjusments affect this clock
75 * If monotonic clock is not available, this fallbacks to now_realtime().
77 extern time_t now_monotonic_sec(void);
78 extern usec_t now_monotonic_usec(void);
80 /* Returns time from boottime clock if available,
81 * monotonic clock else if available, real-time clock else.
82 * If boottime clock is available:
83 * - hibernation/suspend time is included
84 * - adjtime()/NTP adjusments affect this clock
85 * If boottime clock is not available, this fallbacks to now_monotonic().
86 * If monotonic clock is not available, this fallbacks to now_realtime().
88 extern time_t now_boottime_sec(void);
89 extern usec_t now_boottime_usec(void);
91 extern usec_t timeval_usec(struct timeval *ts);
92 extern usec_t dt_usec(struct timeval *now, struct timeval *old);
94 extern void heartbeat_init(heartbeat_t *hb);
96 /* Sleeps until next multiple of tick using monotonic clock.
97 * Returns elapsed time in microseconds since previous heartbeat
99 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_dt_usec(heartbeat_t *hb);
104 #endif /* NETDATA_CLOCKS_H */