src/daemon/utils_time.h: Treat nanoseconds as 64bit integer.
authorFlorian Forster <octo@collectd.org>
Fri, 21 Aug 2015 09:56:57 +0000 (11:56 +0200)
committerFlorian Forster <octo@collectd.org>
Fri, 21 Aug 2015 09:56:57 +0000 (11:56 +0200)
The assumed type was "long", because that is what struct timespec is
using. However, struct timespec only stores the fraction of a second in
the approrpiate field and therefore only cares about values up to 10^9.
We, on the other hand, assume a UNIX epoch in ns precision, so we
require the entire 64bits.

This patch changes the [MUN]S_TO_CDTIME_T() macros to assume a uint64_t
input and moves the casting to the appropriate data type for struct
time{val,spec} to the CDTIME_T_TO_TIME{VAL,SPEC}() macros. Appropriate
casts are added to the cURL based plugins which need to pass a "long" to
cURL when specifying timeouts.

It also fixes the unit test, which assigned large (> 32 bit) literals to
a "long" field, which breaks on 32 bit architectures.

src/apache.c
src/ascent.c
src/bind.c
src/curl.c
src/curl_json.c
src/curl_xml.c
src/daemon/utils_time.h
src/daemon/utils_time_test.c
src/nginx.c

index e384d80..41b807a 100644 (file)
@@ -383,8 +383,7 @@ static int init_host (apache_t *st) /* {{{ */
        if (st->timeout >= 0)
                curl_easy_setopt (st->curl, CURLOPT_TIMEOUT_MS, (long) st->timeout);
        else
-               curl_easy_setopt (st->curl, CURLOPT_TIMEOUT_MS,
-                               CDTIME_T_TO_MS(plugin_get_interval()));
+               curl_easy_setopt (st->curl, CURLOPT_TIMEOUT_MS, (long) CDTIME_T_TO_MS(plugin_get_interval()));
 #endif
 
        return (0);
index 11175af..2ba3c77 100644 (file)
@@ -594,8 +594,7 @@ static int ascent_init (void) /* {{{ */
   if (timeout != NULL)
     curl_easy_setopt (curl, CURLOPT_TIMEOUT_MS, atol(timeout));
   else
-    curl_easy_setopt (curl, CURLOPT_TIMEOUT_MS,
-       CDTIME_T_TO_MS(plugin_get_interval()));
+    curl_easy_setopt (curl, CURLOPT_TIMEOUT_MS, (long) CDTIME_T_TO_MS(plugin_get_interval()));
 #endif
 
   return (0);
index 32b0f16..06b4ace 100644 (file)
@@ -1759,7 +1759,7 @@ static int bind_init (void) /* {{{ */
   curl_easy_setopt (curl, CURLOPT_MAXREDIRS, 50L);
 #ifdef HAVE_CURLOPT_TIMEOUT_MS
   curl_easy_setopt (curl, CURLOPT_TIMEOUT_MS, (timeout >= 0) ?
-      (long) timeout : CDTIME_T_TO_MS(plugin_get_interval()));
+      (long) timeout : (long) CDTIME_T_TO_MS(plugin_get_interval()));
 #endif
 
 
index ac4cc51..16ae3ab 100644 (file)
@@ -418,8 +418,7 @@ static int cc_page_init_curl (web_page_t *wp) /* {{{ */
   if (wp->timeout >= 0)
     curl_easy_setopt (wp->curl, CURLOPT_TIMEOUT_MS, (long) wp->timeout);
   else
-    curl_easy_setopt (wp->curl, CURLOPT_TIMEOUT_MS,
-       CDTIME_T_TO_MS(plugin_get_interval()));
+    curl_easy_setopt (wp->curl, CURLOPT_TIMEOUT_MS, (long) CDTIME_T_TO_MS(plugin_get_interval()));
 #endif
 
   return (0);
index 8063242..510d9b6 100644 (file)
@@ -654,11 +654,9 @@ static int cj_init_curl (cj_t *db) /* {{{ */
   if (db->timeout >= 0)
     curl_easy_setopt (db->curl, CURLOPT_TIMEOUT_MS, (long) db->timeout);
   else if (db->interval > 0)
-    curl_easy_setopt (db->curl, CURLOPT_TIMEOUT_MS,
-        CDTIME_T_TO_MS(db->timeout));
+    curl_easy_setopt (db->curl, CURLOPT_TIMEOUT_MS, (long) CDTIME_T_TO_MS(db->timeout));
   else
-    curl_easy_setopt (db->curl, CURLOPT_TIMEOUT_MS,
-        CDTIME_T_TO_MS(plugin_get_interval()));
+    curl_easy_setopt (db->curl, CURLOPT_TIMEOUT_MS, (long) CDTIME_T_TO_MS(plugin_get_interval()));
 #endif
 
   return (0);
index 8a466ba..5a1f2ba 100644 (file)
@@ -897,8 +897,7 @@ static int cx_init_curl (cx_t *db) /* {{{ */
   if (db->timeout >= 0)
     curl_easy_setopt (db->curl, CURLOPT_TIMEOUT_MS, (long) db->timeout);
   else
-    curl_easy_setopt (db->curl, CURLOPT_TIMEOUT_MS,
-       CDTIME_T_TO_MS(plugin_get_interval()));
+    curl_easy_setopt (db->curl, CURLOPT_TIMEOUT_MS, (long) CDTIME_T_TO_MS(plugin_get_interval()));
 #endif
 
   return (0);
index f2d474a..6476780 100644 (file)
     ((((((cdtime_t) (ns)) % 1000000000) << 30) + 500000000) / 1000000000))
 
 #define CDTIME_T_TO_TIME_T(t) ((((time_t) (t)) + (1 << 29)) >> 30)
-#define CDTIME_T_TO_MS(t)  (((((long) (t)) >> 30) * 1000L) + \
-  (((((long) (t)) & 0x3fffffff) * 1000L + (1 << 29)) >> 30))
-#define CDTIME_T_TO_US(t)  (((((suseconds_t) (t)) >> 30) * 1000000L) + \
-  (((((suseconds_t) (t)) & 0x3fffffff) * 1000000L + (1 << 29)) >> 30))
-#define CDTIME_T_TO_NS(t)  (((((long) (t)) >> 30) * 1000000000L) + \
-  (((((long) (t)) & 0x3fffffff) * 1000000000L + (1 << 29)) >> 30))
+#define CDTIME_T_TO_MS(t)  (((((uint64_t) (t)) >> 30) * 1000L) + \
+  (((((uint64_t) (t)) & 0x3fffffff) * 1000L + (1 << 29)) >> 30))
+#define CDTIME_T_TO_US(t)  (((((uint64_t) (t)) >> 30) * 1000000L) + \
+  (((((uint64_t) (t)) & 0x3fffffff) * 1000000L + (1 << 29)) >> 30))
+#define CDTIME_T_TO_NS(t)  (((((uint64_t) (t)) >> 30) * 1000000000L) + \
+  (((((uint64_t) (t)) & 0x3fffffff) * 1000000000L + (1 << 29)) >> 30))
 
 #define CDTIME_T_TO_DOUBLE(t) (((double) (t)) / 1073741824.0)
 #define DOUBLE_TO_CDTIME_T(d) ((cdtime_t) ((d) * 1073741824.0))
 
 #define CDTIME_T_TO_TIMEVAL(cdt,tvp) do {                                    \
         (tvp)->tv_sec = CDTIME_T_TO_TIME_T (cdt);                            \
-        (tvp)->tv_usec = CDTIME_T_TO_US ((cdt) & 0x3fffffff);                \
+        (tvp)->tv_usec = (suseconds_t) CDTIME_T_TO_US ((cdt) & 0x3fffffff);  \
 } while (0)
 #define TIMEVAL_TO_CDTIME_T(tv) US_TO_CDTIME_T(1000000 * (tv)->tv_sec + (tv)->tv_usec)
 
 #define CDTIME_T_TO_TIMESPEC(cdt,tsp) do {                                   \
   (tsp)->tv_sec = CDTIME_T_TO_TIME_T (cdt);                                  \
-  (tsp)->tv_nsec = CDTIME_T_TO_NS ((cdt) & 0x3fffffff);                      \
+  (tsp)->tv_nsec = (long) CDTIME_T_TO_NS ((cdt) & 0x3fffffff);               \
 } while (0)
 #define TIMESPEC_TO_CDTIME_T(ts) NS_TO_CDTIME_T(1000000000 * (ts)->tv_sec + (ts)->tv_nsec)
 
index 1f13556..0c52729 100644 (file)
@@ -93,7 +93,7 @@ DEF_TEST(conversion)
 DEF_TEST(ns_to_cdtime)
 {
   struct {
-    long ns;
+    uint64_t ns;
     cdtime_t want;
   } cases[] = {
     // 1439981652801860766 * 2^30 / 10^9 = 1546168526406004689.4
index 4e4ce3b..69ec06d 100644 (file)
@@ -188,8 +188,7 @@ static int init (void)
   }
   else
   {
-    curl_easy_setopt (curl, CURLOPT_TIMEOUT_MS,
-       CDTIME_T_TO_MS(plugin_get_interval()));
+    curl_easy_setopt (curl, CURLOPT_TIMEOUT_MS, (long) CDTIME_T_TO_MS(plugin_get_interval()));
   }
 #endif