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);
58 * Three clocks are available (cf. man 3 clock_gettime):
60 * REALTIME clock (i.e. wall-clock):
61 * This clock is affected by discontinuous jumps in the system time
62 * (e.g., if the system administrator manually changes the clock), and by the incremental adjustments performed by adjtime(3) and NTP.
65 * Clock that cannot be set and represents monotonic time since some unspecified starting point.
66 * This clock is not affected by discontinuous jumps in the system time
67 * (e.g., if the system administrator manually changes the clock), but is affected by the incremental adjustments performed by adjtime(3) and NTP.
68 * If not available on the system, this clock falls back to REALTIME clock.
71 * Identical to CLOCK_MONOTONIC, except it also includes any time that the system is suspended.
72 * This allows applications to get a suspend-aware monotonic clock without having to deal with the complications of CLOCK_REALTIME,
73 * which may have discontinuities if the time is changed using settimeofday(2).
74 * If not available on the system, this clock falls back to MONOTONIC clock.
76 * All now_*_timeval() functions fill the `struct timeval` with the time from the appropriate clock.
77 * Those functions return 0 on success, -1 else with errno set appropriately.
79 * All now_*_sec() functions return the time in seconds from the approriate clock, or 0 on error.
80 * All now_*_usec() functions return the time in microseconds from the approriate clock, or 0 on error.
82 extern int now_realtime_timeval(struct timeval *tv);
83 extern time_t now_realtime_sec(void);
84 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);
90 extern int now_boottime_timeval(struct timeval *tv);
91 extern time_t now_boottime_sec(void);
92 extern usec_t now_boottime_usec(void);
95 extern usec_t timeval_usec(struct timeval *ts);
96 extern usec_t dt_usec(struct timeval *now, struct timeval *old);
98 extern void heartbeat_init(heartbeat_t *hb);
100 /* Sleeps until next multiple of tick using monotonic clock.
101 * Returns elapsed time in microseconds since previous heartbeat
103 extern usec_t heartbeat_next(heartbeat_t *hb, usec_t tick);
105 /* Returns elapsed time in microseconds since last heartbeat */
106 extern usec_t heartbeat_dt_usec(heartbeat_t *hb);
108 #endif /* NETDATA_CLOCKS_H */