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
34 typedef unsigned long long usec_t;
36 #define NSEC_PER_SEC 1000000000ULL
37 #define NSEC_PER_MSEC 1000000ULL
38 #define NSEC_PER_USEC 1000ULL
39 #define USEC_PER_SEC 1000000ULL
41 #ifndef HAVE_CLOCK_GETTIME
42 /* Fallback function for POSIX.1-2001 clock_gettime() function.
44 * We use a realtime clock from gettimeofday(), this will
45 * make systems without clock_gettime() support sensitive
46 * to time jumps or hibernation/suspend side effects.
48 extern int clock_gettime(clockid_t clk_id, struct timespec *ts);
51 /* Fills struct timeval with time since EPOCH from real-time clock (i.e. wall-clock).
52 * - Hibernation/suspend time is included
53 * - adjtime()/NTP adjustments affect this clock
54 * Return 0 on succes, -1 else with errno set appropriately.
56 extern int now_realtime_timeval(struct timeval *tv);
58 /* Returns time since EPOCH from real-time clock (i.e. wall-clock).
59 * - Hibernation/suspend time is included
60 * - adjtime()/NTP adjustments affect this clock
62 extern time_t now_realtime_sec(void);
63 extern usec_t now_realtime_usec(void);
65 /* Returns time from monotonic clock if available, real-time clock else.
66 * If monotonic clock is available:
67 * - hibernation/suspend time is not included
68 * - adjtime()/NTP adjusments affect this clock
69 * If monotonic clock is not available, this fallbacks to now_realtime().
71 extern time_t now_monotonic_sec(void);
72 extern usec_t now_monotonic_usec(void);
74 /* Returns time from boottime clock if available,
75 * monotonic clock else if available, real-time clock else.
76 * If boottime clock is available:
77 * - hibernation/suspend time is included
78 * - adjtime()/NTP adjusments affect this clock
79 * If boottime clock is not available, this fallbacks to now_monotonic().
80 * If monotonic clock is not available, this fallbacks to now_realtime().
82 extern time_t now_boottime_sec(void);
83 extern usec_t now_boottime_usec(void);
85 extern usec_t timeval_usec(struct timeval *ts);
86 extern usec_t dt_usec(struct timeval *now, struct timeval *old);
88 #endif /* NETDATA_CLOCKS_H */