]> arthur.barton.de Git - netdata.git/blob - src/clocks.h
introduce heartbeat API using monotonic clock
[netdata.git] / src / clocks.h
1 #ifndef NETDATA_CLOCKS_H
2 #define NETDATA_CLOCKS_H 1
3
4 #ifndef HAVE_STRUCT_TIMESPEC
5 struct timespec {
6     time_t tv_sec;  /* seconds */
7     long   tv_nsec; /* nanoseconds */
8 };
9 #endif
10
11 #ifndef HAVE_CLOCKID_T
12 typedef int clockid_t;
13 #endif
14
15 typedef unsigned long long usec_t;
16
17 typedef usec_t heartbeat_t;
18
19 #ifndef HAVE_CLOCK_GETTIME
20 int clock_gettime(clockid_t clk_id, struct timespec *ts);
21 #endif
22
23 /* Linux value is as good as any other */
24 #ifndef CLOCK_REALTIME
25 #define CLOCK_REALTIME  0
26 #endif
27
28 #ifndef CLOCK_MONOTONIC
29 /* fallback to CLOCK_REALTIME if not available */
30 #define CLOCK_MONOTONIC CLOCK_REALTIME
31 #endif
32
33 #ifndef CLOCK_BOOTTIME
34 /* fallback to CLOCK_MONOTONIC if not available */
35 #define CLOCK_BOOTTIME  CLOCK_MONOTONIC
36 #else
37 #ifdef HAVE_CLOCK_GETTIME
38 #define CLOCK_BOOTTIME_IS_AVAILABLE 1 // required for /proc/uptime
39 #endif
40 #endif
41
42 #define NSEC_PER_SEC    1000000000ULL
43 #define NSEC_PER_MSEC   1000000ULL
44 #define NSEC_PER_USEC   1000ULL
45 #define USEC_PER_SEC    1000000ULL
46
47 #ifndef HAVE_CLOCK_GETTIME
48 /* Fallback function for POSIX.1-2001 clock_gettime() function.
49  *
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.
53  */
54 extern int clock_gettime(clockid_t clk_id, struct timespec *ts);
55 #endif
56
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.
61  */
62 extern int now_realtime_timeval(struct timeval *tv);
63
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
67  */
68 extern time_t now_realtime_sec(void);
69 extern usec_t now_realtime_usec(void);
70
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().
76  */
77 extern time_t now_monotonic_sec(void);
78 extern usec_t now_monotonic_usec(void);
79
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().
87  */
88 extern time_t now_boottime_sec(void);
89 extern usec_t now_boottime_usec(void);
90
91 extern usec_t timeval_usec(struct timeval *ts);
92 extern usec_t dt_usec(struct timeval *now, struct timeval *old);
93
94 extern void heartbeat_init(heartbeat_t *hb);
95
96 /* Sleeps until next multiple of tick using monotonic clock.
97  * Returns elapsed time in microseconds since previous heartbeat
98  */
99 extern usec_t heartbeat_next(heartbeat_t *hb, usec_t tick);
100
101 /* Returns elapsed time in microseconds since last heartbeat */
102 extern usec_t heartbeat_dt_usec(heartbeat_t *hb);
103
104 #endif /* NETDATA_CLOCKS_H */