diff --git a/src/collectd-tg.c b/src/collectd-tg.c
index 71814a2955357a9a07df0390be43152c9e659060..7db9fe713ee2dcde8674e2f69aa3827f28295fa2 100644 (file)
--- a/src/collectd-tg.c
+++ b/src/collectd-tg.c
#include <time.h>
#include <signal.h>
#include <errno.h>
+#include <math.h>
+#include <sys/time.h>
#include "utils_heap.h"
#include "libcollectdclient/collectd/client.h"
#include "libcollectdclient/collectd/network.h"
-#include "libcollectdclient/collectd/network_buffer.h"
#define DEF_NUM_HOSTS 1000
#define DEF_NUM_PLUGINS 20
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;
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)
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)
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)
int main (int argc, char **argv) /* {{{ */
{
- int i;
- time_t last_time;
+ double last_time;
int values_sent = 0;
read_options (argc, argv);
else
{
lcc_server_t *srv;
-
+
srv = lcc_server_create (net, conf_destination, conf_service);
if (srv == NULL)
{
fprintf (stdout, "Creating %i values ... ", conf_num_values);
fflush (stdout);
- for (i = 0; i < conf_num_values; i++)
+ for (int i = 0; i < conf_num_values; i++)
{
lcc_value_list_t *vl;
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;
lcc_network_destroy (net);
exit (EXIT_SUCCESS);
- return (0);
} /* }}} int main */
/* vim: set sw=2 sts=2 et fdm=marker : */