]> arthur.barton.de Git - netdata.git/blob - src/clocks.h
fix apps.plugin values on 32bit machines
[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 #else
33 #ifdef HAVE_CLOCK_GETTIME
34 #define CLOCK_BOOTTIME_IS_AVAILABLE 1 // required for /proc/uptime
35 #endif
36 #endif
37
38 typedef unsigned long long usec_t;
39
40 #define NSEC_PER_SEC    1000000000ULL
41 #define NSEC_PER_MSEC   1000000ULL
42 #define NSEC_PER_USEC   1000ULL
43 #define USEC_PER_SEC    1000000ULL
44
45 #ifndef HAVE_CLOCK_GETTIME
46 /* Fallback function for POSIX.1-2001 clock_gettime() function.
47  *
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.
51  */
52 extern int clock_gettime(clockid_t clk_id, struct timespec *ts);
53 #endif
54
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.
59  */
60 extern int now_realtime_timeval(struct timeval *tv);
61
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
65  */
66 extern time_t now_realtime_sec(void);
67 extern usec_t now_realtime_usec(void);
68
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().
74  */
75 extern time_t now_monotonic_sec(void);
76 extern usec_t now_monotonic_usec(void);
77
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().
85  */
86 extern time_t now_boottime_sec(void);
87 extern usec_t now_boottime_usec(void);
88
89 extern usec_t timeval_usec(struct timeval *ts);
90 extern usec_t dt_usec(struct timeval *now, struct timeval *old);
91
92 #endif /* NETDATA_CLOCKS_H */