@cryptotaxi247 / netdata-1 / commits / aef557205

complete and factorize clocks API

Signed-off-by: Rémi Lefèvre <remi.lefevre@parrot.com>

Rémi Lefèvre committed Dec 13, 2016 at 18:43 UTC aef5572053fa8c55a0ded6ed08e4250bfc8b7986
2 files changed +59 -47
src/clocks.c
+32 -24
@@ -11,55 +11,63 @@ inline int clock_gettime(clockid_t clk_id, struct timespec *ts) {
11 }
12 #endif
13
14 -inline time_t now_realtime_sec(void) {
14 +static inline time_t now_sec(clockid_t clk_id) {
15 struct timespec ts;
16 - if(unlikely(clock_gettime(CLOCK_REALTIME, &ts) == -1))
16 + if(unlikely(clock_gettime(clk_id, &ts) == -1))
17 return 0;
18 return ts.tv_sec;
19 }
20
21 -inline int now_realtime_timeval(struct timeval *tv) {
21 +static inline usec_t now_usec(clockid_t clk_id) {
22 + struct timespec ts;
23 + if(unlikely(clock_gettime(clk_id, &ts) == -1))
24 + return 0;
25 + return (usec_t)ts.tv_sec * USEC_PER_SEC + ts.tv_nsec / NSEC_PER_USEC;
26 +}
27 +
28 +static inline int now_timeval(clockid_t clk_id, struct timeval *tv) {
29 struct timespec ts;
23 - if(unlikely(clock_gettime(CLOCK_REALTIME, &ts) == -1))
30 + if(unlikely(clock_gettime(clk_id, &ts) == -1))
31 return -1;
32 tv->tv_sec = ts.tv_sec;
33 tv->tv_usec = ts.tv_nsec / NSEC_PER_USEC;
34 return 0;
35 }
36
37 +inline time_t now_realtime_sec(void) {
38 + return now_sec(CLOCK_REALTIME);
39 +}
40 +
41 inline usec_t now_realtime_usec(void) {
31 - struct timespec ts;
32 - if(unlikely(clock_gettime(CLOCK_REALTIME, &ts) == -1))
33 - return 0;
34 - return (usec_t)ts.tv_sec * USEC_PER_SEC + ts.tv_nsec / NSEC_PER_USEC;
42 + return now_usec(CLOCK_REALTIME);
43 +}
44 +
45 +inline int now_realtime_timeval(struct timeval *tv) {
46 + return now_timeval(CLOCK_REALTIME, tv);
47 }
48
49 inline time_t now_monotonic_sec(void) {
38 - struct timespec ts;
39 - if(unlikely(clock_gettime(CLOCK_MONOTONIC, &ts) == -1))
40 - return 0;
41 - return ts.tv_sec;
50 + return now_sec(CLOCK_MONOTONIC);
51 }
52
53 inline usec_t now_monotonic_usec(void) {
45 - struct timespec ts;
46 - if(unlikely(clock_gettime(CLOCK_MONOTONIC, &ts) == -1))
47 - return 0;
48 - return (usec_t)ts.tv_sec * USEC_PER_SEC + ts.tv_nsec / NSEC_PER_USEC;
54 + return now_usec(CLOCK_MONOTONIC);
55 +}
56 +
57 +inline int now_monotonic_timeval(struct timeval *tv) {
58 + return now_timeval(CLOCK_MONOTONIC, tv);
59 }
60
61 inline time_t now_boottime_sec(void) {
52 - struct timespec ts;
53 - if(unlikely(clock_gettime(CLOCK_BOOTTIME, &ts) == -1))
54 - return 0;
55 - return ts.tv_sec;
62 + return now_sec(CLOCK_BOOTTIME);
63 }
64
65 inline usec_t now_boottime_usec(void) {
59 - struct timespec ts;
60 - if(unlikely(clock_gettime(CLOCK_BOOTTIME, &ts) == -1))
61 - return 0;
62 - return (usec_t)ts.tv_sec * USEC_PER_SEC + ts.tv_nsec / NSEC_PER_USEC;
66 + return now_usec(CLOCK_BOOTTIME);
67 +}
68 +
69 +inline int now_boottime_timeval(struct timeval *tv) {
70 + return now_timeval(CLOCK_BOOTTIME, tv);
71 }
72
73 inline usec_t timeval_usec(struct timeval *tv) {
src/clocks.h
+27 -23
@@ -54,40 +54,44 @@ int clock_gettime(clockid_t clk_id, struct timespec *ts);
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.
57 +/*
58 + * Three clocks are available (cf. man 3 clock_gettime):
59 + *
60 + * REALTIME clock (i.e. wall-clock):
61 + * This clock is affected by discontinuous jumps in the system time
62 + * (e.g., if the system administrator manually changes the clock), and by the incremental adjustments performed by adjtime(3) and NTP.
63 + *
64 + * MONOTONIC clock
65 + * Clock that cannot be set and represents monotonic time since some unspecified starting point.
66 + * This clock is not affected by discontinuous jumps in the system time
67 + * (e.g., if the system administrator manually changes the clock), but is affected by the incremental adjustments performed by adjtime(3) and NTP.
68 + * If not available on the system, this clock falls back to REALTIME clock.
69 + *
70 + * BOOTTIME clock
71 + * Identical to CLOCK_MONOTONIC, except it also includes any time that the system is suspended.
72 + * This allows applications to get a suspend-aware monotonic clock without having to deal with the complications of CLOCK_REALTIME,
73 + * which may have discontinuities if the time is changed using settimeofday(2).
74 + * If not available on the system, this clock falls back to MONOTONIC clock.
75 + *
76 + * All now_*_timeval() functions fill the `struct timeval` with the time from the appropriate clock.
77 + * Those functions return 0 on success, -1 else with errno set appropriately.
78 + *
79 + * All now_*_sec() functions return the time in seconds from the approriate clock, or 0 on error.
80 + * All now_*_usec() functions return the time in microseconds from the approriate clock, or 0 on error.
81 */
82 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 - */
83 extern time_t now_realtime_sec(void);
84 extern usec_t now_realtime_usec(void);
85
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 - */
86 +extern int now_monotonic_timeval(struct timeval *tv);
87 extern time_t now_monotonic_sec(void);
88 extern usec_t now_monotonic_usec(void);
89
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 - */
90 +extern int now_boottime_timeval(struct timeval *tv);
91 extern time_t now_boottime_sec(void);
92 extern usec_t now_boottime_usec(void);
93
94 +
95 extern usec_t timeval_usec(struct timeval *ts);
96 extern usec_t dt_usec(struct timeval *now, struct timeval *old);
97