X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fcollectd-tg.c;h=53fd3a17362287de70c2849dff0b207e5649d990;hb=5c2993e4121feae745551be182221b31bc23ff97;hp=45e788c22206c5c733d696bc6ab778ab732643ec;hpb=a359afebf9e55cdb46b445e3d46d728a4d6fe992;p=collectd.git diff --git a/src/collectd-tg.c b/src/collectd-tg.c index 45e788c2..53fd3a17 100644 --- a/src/collectd-tg.c +++ b/src/collectd-tg.c @@ -28,18 +28,6 @@ # include "config.h" #endif -#ifndef _ISOC99_SOURCE -# define _ISOC99_SOURCE -#endif - -#ifndef _POSIX_C_SOURCE -# define _POSIX_C_SOURCE 200809L -#endif - -#ifndef _XOPEN_SOURCE -# define _XOPEN_SOURCE 700 -#endif - #if !__GNUC__ # define __attribute__(x) /**/ #endif @@ -51,6 +39,8 @@ #include #include #include +#include +#include #include "utils_heap.h" @@ -111,6 +101,29 @@ static void signal_handler (int signal) /* {{{ */ loop = 0; } /* }}} void signal_handler */ +#if HAVE_CLOCK_GETTIME +static double dtime (void) /* {{{ */ +{ + struct timespec ts = { 0 }; + + if (clock_gettime (CLOCK_MONOTONIC, &ts) != 0) + perror ("clock_gettime"); + + return ((double) ts.tv_sec) + (((double) ts.tv_nsec) / 1e9); +} /* }}} double dtime */ +#else +/* Work around for Mac OS X which doesn't have clock_gettime(2). *sigh* */ +static double dtime (void) /* {{{ */ +{ + struct timeval tv = { 0 }; + + if (gettimeofday (&tv, /* timezone = */ NULL) != 0) + perror ("gettimeofday"); + + return ((double) tv.tv_sec) + (((double) tv.tv_usec) / 1e6); +} /* }}} double dtime */ +#endif + static int compare_time (const void *v0, const void *v1) /* {{{ */ { const lcc_value_list_t *vl0 = v0; @@ -173,7 +186,7 @@ static lcc_value_list_t *create_value_list (void) /* {{{ */ host_num = get_boundet_random (0, conf_num_hosts); vl->interval = conf_interval; - vl->time = 1.0 + time (NULL) + vl->time = 1.0 + dtime () + (host_num % (1 + (int) vl->interval)); if (get_boundet_random (0, 2) == 0) @@ -188,6 +201,7 @@ static lcc_value_list_t *create_value_list (void) /* {{{ */ strncpy (vl->identifier.type, (vl->values_types[0] == LCC_TYPE_GAUGE) ? "gauge" : "derive", sizeof (vl->identifier.type)); + vl->identifier.type[sizeof (vl->identifier.type) - 1] = 0; snprintf (vl->identifier.type_instance, sizeof (vl->identifier.type_instance), "ti%li", random ()); @@ -211,7 +225,7 @@ static int send_value (lcc_value_list_t *vl) /* {{{ */ if (vl->values_types[0] == LCC_TYPE_GAUGE) vl->values[0].gauge = 100.0 * ((gauge_t) random ()) / (((gauge_t) RAND_MAX) + 1.0); else - vl->values[0].derive += get_boundet_random (0, 100); + vl->values[0].derive += (derive_t) get_boundet_random (0, 100); status = lcc_network_values_send (net, vl); if (status != 0) @@ -326,7 +340,7 @@ static int read_options (int argc, char **argv) /* {{{ */ int main (int argc, char **argv) /* {{{ */ { int i; - time_t last_time; + double last_time; int values_sent = 0; read_options (argc, argv); @@ -399,14 +413,18 @@ int main (int argc, char **argv) /* {{{ */ printf ("%i values have been sent.\n", values_sent); /* Check if we need to sleep */ - time_t now = time (NULL); + double now = dtime (); while (now < vl->time) { /* 1 / 100 second */ struct timespec ts = { 0, 10000000 }; + + ts.tv_sec = (time_t) now; + ts.tv_nsec = (long) ((now - ((double) ts.tv_sec)) * 1e9); + nanosleep (&ts, /* remaining = */ NULL); - now = time (NULL); + now = dtime (); if (!loop) break;