]> arthur.barton.de Git - netdata.git/blob - src/clocks.h
dns_query_time plugin: replace "." with "_" in dimensions
[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 nsec_t;
16 typedef unsigned long long msec_t;
17 typedef unsigned long long usec_t;
18 typedef long long susec_t;
19
20 typedef usec_t heartbeat_t;
21
22 /* Linux value is as good as any other */
23 #ifndef CLOCK_REALTIME
24 #define CLOCK_REALTIME  0
25 #endif
26
27 #ifndef CLOCK_MONOTONIC
28 /* fallback to CLOCK_REALTIME if not available */
29 #define CLOCK_MONOTONIC CLOCK_REALTIME
30 #endif
31
32 #ifndef CLOCK_BOOTTIME
33
34 #ifdef CLOCK_UPTIME
35 /* CLOCK_BOOTTIME falls back to CLOCK_UPTIME on FreeBSD */
36 #define CLOCK_BOOTTIME CLOCK_UPTIME
37 #else // CLOCK_UPTIME
38 /* CLOCK_BOOTTIME falls back to CLOCK_MONOTONIC */
39 #define CLOCK_BOOTTIME  CLOCK_MONOTONIC
40 #endif // CLOCK_UPTIME
41
42 #else // CLOCK_BOOTTIME
43
44 #ifdef HAVE_CLOCK_GETTIME
45 #define CLOCK_BOOTTIME_IS_AVAILABLE 1 // required for /proc/uptime
46 #endif // HAVE_CLOCK_GETTIME
47
48 #endif // CLOCK_BOOTTIME
49
50 #define NSEC_PER_MSEC   1000000ULL
51
52 #define NSEC_PER_SEC    1000000000ULL
53 #define NSEC_PER_USEC   1000ULL
54
55 #define USEC_PER_SEC    1000000ULL
56 #define MSEC_PER_SEC    1000ULL
57
58 #ifndef HAVE_CLOCK_GETTIME
59 /* Fallback function for POSIX.1-2001 clock_gettime() function.
60  *
61  * We use a realtime clock from gettimeofday(), this will
62  * make systems without clock_gettime() support sensitive
63  * to time jumps or hibernation/suspend side effects.
64  */
65 extern int clock_gettime(clockid_t clk_id, struct timespec *ts);
66 #endif
67
68 /*
69  * Three clocks are available (cf. man 3 clock_gettime):
70  *
71  * REALTIME clock (i.e. wall-clock):
72  *  This clock is affected by discontinuous jumps in the system time
73  *  (e.g., if the system administrator manually changes the clock), and by the incremental adjustments performed by adjtime(3) and NTP.
74  *
75  * MONOTONIC clock
76  *  Clock that cannot be set and represents monotonic time since some unspecified starting point.
77  *  This clock is not affected by discontinuous jumps in the system time
78  *  (e.g., if the system administrator manually changes the clock), but is affected by the incremental adjustments performed by adjtime(3) and NTP.
79  *  If not available on the system, this clock falls back to REALTIME clock.
80  *
81  * BOOTTIME clock
82  *  Identical to  CLOCK_MONOTONIC, except it also includes any time that the system is suspended.
83  *  This allows applications to get a suspend-aware monotonic clock without having to deal with the complications of CLOCK_REALTIME,
84  *  which may have discontinuities if the time is changed using settimeofday(2).
85  *  If not available on the system, this clock falls back to MONOTONIC clock.
86  *
87  * All now_*_timeval() functions fill the `struct timeval` with the time from the appropriate clock.
88  * Those functions return 0 on success, -1 else with errno set appropriately.
89  *
90  * All now_*_sec() functions return the time in seconds from the approriate clock, or 0 on error.
91  * All now_*_usec() functions return the time in microseconds from the approriate clock, or 0 on error.
92  */
93 extern int now_realtime_timeval(struct timeval *tv);
94 extern time_t now_realtime_sec(void);
95 extern usec_t now_realtime_usec(void);
96
97 extern int now_monotonic_timeval(struct timeval *tv);
98 extern time_t now_monotonic_sec(void);
99 extern usec_t now_monotonic_usec(void);
100
101 extern int now_boottime_timeval(struct timeval *tv);
102 extern time_t now_boottime_sec(void);
103 extern usec_t now_boottime_usec(void);
104
105
106 extern usec_t timeval_usec(struct timeval *ts);
107 extern msec_t timeval_msec(struct timeval *tv);
108
109 extern usec_t dt_usec(struct timeval *now, struct timeval *old);
110 extern susec_t dt_usec_signed(struct timeval *now, struct timeval *old);
111
112 extern void heartbeat_init(heartbeat_t *hb);
113
114 /* Sleeps until next multiple of tick using monotonic clock.
115  * Returns elapsed time in microseconds since previous heartbeat
116  */
117 extern usec_t heartbeat_next(heartbeat_t *hb, usec_t tick);
118
119 /* Returns elapsed time in microseconds since last heartbeat */
120 extern usec_t heartbeat_dt_usec(heartbeat_t *hb);
121
122 #endif /* NETDATA_CLOCKS_H */