X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fcollectd-tg.c;h=23257fde9b2eaefbc52a063ad6b1cfad39843dc9;hb=e9b1ff6f13a7b203ec52a541aa468f23502c9584;hp=08bb74877de4ad9b375585fbc2175adf501d3e95;hpb=0a63e4493d92f7d739a1849bc65d4e971b047004;p=collectd.git diff --git a/src/collectd-tg.c b/src/collectd-tg.c index 08bb7487..23257fde 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; @@ -143,13 +156,12 @@ static lcc_value_list_t *create_value_list (void) /* {{{ */ lcc_value_list_t *vl; int host_num; - vl = malloc (sizeof (*vl)); + vl = calloc (1, sizeof (*vl)); if (vl == NULL) { - fprintf (stderr, "malloc failed.\n"); + fprintf (stderr, "calloc failed.\n"); return (NULL); } - memset (vl, 0, sizeof (*vl)); vl->values = calloc (/* nmemb = */ 1, sizeof (*vl->values)); if (vl->values == NULL) @@ -173,7 +185,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) @@ -212,7 +224,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) @@ -327,7 +339,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); @@ -355,7 +367,7 @@ int main (int argc, char **argv) /* {{{ */ else { lcc_server_t *srv; - + srv = lcc_server_create (net, conf_destination, conf_service); if (srv == NULL) { @@ -400,14 +412,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;