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 #ifndef HAVE_CLOCK_GETTIME
16 int clock_gettime(clockid_t clk_id, struct timespec *ts);
19 /* Linux value is as good as any other */
20 #ifndef CLOCK_REALTIME
21 #define CLOCK_REALTIME 0
24 #ifndef CLOCK_MONOTONIC
25 /* fallback to CLOCK_REALTIME if not available */
26 #define CLOCK_MONOTONIC CLOCK_REALTIME
29 #ifndef CLOCK_BOOTTIME
30 /* fallback to CLOCK_MONOTONIC if not available */
31 #define CLOCK_BOOTTIME CLOCK_MONOTONIC
33 #ifdef HAVE_CLOCK_GETTIME
34 #define CLOCK_BOOTTIME_IS_AVAILABLE 1 // required for /proc/uptime
38 typedef unsigned long long usec_t;
40 #define NSEC_PER_SEC 1000000000ULL
41 #define NSEC_PER_MSEC 1000000ULL
42 #define NSEC_PER_USEC 1000ULL
43 #define USEC_PER_SEC 1000000ULL
45 #ifndef HAVE_CLOCK_GETTIME
46 /* Fallback function for POSIX.1-2001 clock_gettime() function.
48 * We use a realtime clock from gettimeofday(), this will
49 * make systems without clock_gettime() support sensitive
50 * to time jumps or hibernation/suspend side effects.
52 extern int clock_gettime(clockid_t clk_id, struct timespec *ts);
55 /* Fills struct timeval with time since EPOCH from real-time clock (i.e. wall-clock).
56 * - Hibernation/suspend time is included
57 * - adjtime()/NTP adjustments affect this clock
58 * Return 0 on succes, -1 else with errno set appropriately.
60 extern int now_realtime_timeval(struct timeval *tv);
62 /* Returns time since EPOCH from real-time clock (i.e. wall-clock).
63 * - Hibernation/suspend time is included
64 * - adjtime()/NTP adjustments affect this clock
66 extern time_t now_realtime_sec(void);
67 extern usec_t now_realtime_usec(void);
69 /* Returns time from monotonic clock if available, real-time clock else.
70 * If monotonic clock is available:
71 * - hibernation/suspend time is not included
72 * - adjtime()/NTP adjusments affect this clock
73 * If monotonic clock is not available, this fallbacks to now_realtime().
75 extern time_t now_monotonic_sec(void);
76 extern usec_t now_monotonic_usec(void);
78 /* Returns time from boottime clock if available,
79 * monotonic clock else if available, real-time clock else.
80 * If boottime clock is available:
81 * - hibernation/suspend time is included
82 * - adjtime()/NTP adjusments affect this clock
83 * If boottime clock is not available, this fallbacks to now_monotonic().
84 * If monotonic clock is not available, this fallbacks to now_realtime().
86 extern time_t now_boottime_sec(void);
87 extern usec_t now_boottime_usec(void);
89 extern usec_t timeval_usec(struct timeval *ts);
90 extern usec_t dt_usec(struct timeval *now, struct timeval *old);
92 #endif /* NETDATA_CLOCKS_H */