]> arthur.barton.de Git - netdata.git/blob - src/clocks.h
Merge pull request #1365 from ktsaou/master
[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 #ifndef HAVE_CLOCK_GETTIME
16 int clock_gettime(clockid_t clk_id, struct timespec *ts);
17 #endif
18
19 /* Linux value is as good as any other */
20 #ifndef CLOCK_REALTIME
21 #define CLOCK_REALTIME  0
22 #endif
23
24 #ifndef CLOCK_MONOTONIC
25 /* fallback to CLOCK_REALTIME if not available */
26 #define CLOCK_MONOTONIC CLOCK_REALTIME
27 #endif
28
29 #ifndef CLOCK_BOOTTIME
30 /* fallback to CLOCK_MONOTONIC if not available */
31 #define CLOCK_BOOTTIME  CLOCK_MONOTONIC
32 #endif
33
34 typedef unsigned long long usec_t;
35
36 #define NSEC_PER_SEC    1000000000ULL
37 #define NSEC_PER_MSEC   1000000ULL
38 #define NSEC_PER_USEC   1000ULL
39 #define USEC_PER_SEC    1000000ULL
40
41 #ifndef HAVE_CLOCK_GETTIME
42 /* Fallback function for POSIX.1-2001 clock_gettime() function.
43  *
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.
47  */
48 extern int clock_gettime(clockid_t clk_id, struct timespec *ts);
49 #endif
50
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.
55  */
56 extern int now_realtime_timeval(struct timeval *tv);
57
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
61  */
62 extern time_t now_realtime_sec(void);
63 extern usec_t now_realtime_usec(void);
64
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().
70  */
71 extern time_t now_monotonic_sec(void);
72 extern usec_t now_monotonic_usec(void);
73
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().
81  */
82 extern time_t now_boottime_sec(void);
83 extern usec_t now_boottime_usec(void);
84
85 extern usec_t timeval_usec(struct timeval *ts);
86 extern usec_t dt_usec(struct timeval *now, struct timeval *old);
87
88 #endif /* NETDATA_CLOCKS_H */