@cryptotaxi247 / netdata-1 / commits / 101b6b842

prevents usec/msec from using wrong .tv_nsec/.tv_nsec values; #1843

Costa Tsaousis (ktsaou) committed Mar 2, 2017 at 23:30 UTC 101b6b842bc2037d13b0e22d0511ce7d414dff74
3 files changed +32 -17
src/clocks.c
+8 -4
@@ -6,7 +6,7 @@ inline int clock_gettime(clockid_t clk_id, struct timespec *ts) {
6 if(unlikely(gettimeofday(&tv, NULL) == -1))
7 return -1;
8 ts->tv_sec = tv.tv_sec;
9 - ts->tv_nsec = tv.tv_usec * NSEC_PER_USEC;
9 + ts->tv_nsec = (tv.tv_usec % USEC_PER_SEC) * NSEC_PER_USEC;
10 return 0;
11 }
12 #endif
@@ -22,7 +22,7 @@ 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;
25 + return (usec_t)ts.tv_sec * USEC_PER_SEC + (ts.tv_nsec % NSEC_PER_SEC) / NSEC_PER_USEC;
26 }
27
28 static inline int now_timeval(clockid_t clk_id, struct timeval *tv) {
@@ -30,7 +30,7 @@ static inline int now_timeval(clockid_t clk_id, struct timeval *tv) {
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;
33 + tv->tv_usec = (suseconds_t)((ts.tv_nsec % NSEC_PER_SEC) / NSEC_PER_USEC);
34 return 0;
35 }
36
@@ -71,7 +71,11 @@ inline int now_boottime_timeval(struct timeval *tv) {
71 }
72
73 inline usec_t timeval_usec(struct timeval *tv) {
74 - return (usec_t)tv->tv_sec * USEC_PER_SEC + tv->tv_usec;
74 + return (usec_t)tv->tv_sec * USEC_PER_SEC + (tv->tv_usec % USEC_PER_SEC);
75 +}
76 +
77 +inline msec_t timeval_msec(struct timeval *tv) {
78 + return (msec_t)tv->tv_sec * MSEC_PER_SEC + ((tv->tv_usec % USEC_PER_SEC) / MSEC_PER_SEC);
79 }
80
81 inline susec_t dt_usec_signed(struct timeval *now, struct timeval *old) {
src/clocks.h
+21 -9
@@ -12,15 +12,13 @@ struct timespec {
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
20 -#ifndef HAVE_CLOCK_GETTIME
21 -int clock_gettime(clockid_t clk_id, struct timespec *ts);
22 -#endif
23 -
22 /* Linux value is as good as any other */
23 #ifndef CLOCK_REALTIME
24 #define CLOCK_REALTIME 0
@@ -32,18 +30,30 @@ int clock_gettime(clockid_t clk_id, struct timespec *ts);
30 #endif
31
32 #ifndef CLOCK_BOOTTIME
35 -/* fallback to CLOCK_MONOTONIC if not available */
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
37 -#else
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
40 -#endif
41 -#endif
46 +#endif // HAVE_CLOCK_GETTIME
47 +
48 +#endif // CLOCK_BOOTTIME
49
43 -#define NSEC_PER_SEC 1000000000ULL
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.
@@ -94,6 +104,8 @@ 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
src/rrd2json.c
+3 -4
@@ -218,12 +218,11 @@ void rrd_stats_api_v1_charts_allmetrics_prometheus(RRDHOST *host, BUFFER *wb) {
218 }
219
220 // calculated_number n = (calculated_number)rd->last_collected_value * (calculated_number)(abs(rd->multiplier)) / (calculated_number)(abs(rd->divisor));
221 - // buffer_sprintf(wb, "%s.%s " CALCULATED_NUMBER_FORMAT " %llu\n", st->id, rd->id, n,
222 - // (unsigned long long)((rd->last_collected_time.tv_sec * 1000) + (rd->last_collected_time.tv_usec / 1000)));
221 + // buffer_sprintf(wb, "%s.%s " CALCULATED_NUMBER_FORMAT " %llu\n", st->id, rd->id, n, timeval_msec(&rd->last_collected_time));
222
223 buffer_sprintf(wb, "%s_%s{instance=\"%s\"} " COLLECTED_NUMBER_FORMAT " %llu\n",
225 - chart, dimension, hostname, rd->last_collected_value,
226 - (unsigned long long)((rd->last_collected_time.tv_sec * 1000) + (rd->last_collected_time.tv_usec / 1000)));
224 + chart, dimension, hostname, rd->last_collected_value, timeval_msec(&rd->last_collected_time)
225 + );
226
227 }
228 }