From f45b8f6be49a380c0b1dce4328f47b08da802bce Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Wed, 18 Aug 2010 16:21:43 +0200 Subject: [PATCH] collectd-tg: Use the heap to manage the value lists. --- src/Makefile.am | 3 ++- src/collectd-tg.c | 55 +++++++++++++++++++++++++++-------------------- 2 files changed, 34 insertions(+), 24 deletions(-) diff --git a/src/Makefile.am b/src/Makefile.am index 24d3c348..d5fdc1e1 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -117,7 +117,8 @@ endif collectdctl_LDADD += libcollectdclient/libcollectdclient.la collectdctl_DEPENDENCIES = libcollectdclient/libcollectdclient.la -collectd_tg_SOURCES = collectd-tg.c +collectd_tg_SOURCES = collectd-tg.c \ + utils_heap.c utils_heap.h collectd_tg_LDADD = if BUILD_WITH_LIBSOCKET collectd_tg_LDADD += -lsocket diff --git a/src/collectd-tg.c b/src/collectd-tg.c index c5e15194..5b31cc9d 100644 --- a/src/collectd-tg.c +++ b/src/collectd-tg.c @@ -28,6 +28,8 @@ #include #include +#include "utils_heap.h" + #include "libcollectdclient/collectd/client.h" #include "libcollectdclient/collectd/network.h" #include "libcollectdclient/collectd/network_buffer.h" @@ -38,8 +40,7 @@ static int conf_num_values = 100000; static lcc_network_t *net; -static lcc_value_list_t **values; -static size_t values_num; +static c_heap_t *values_heap = NULL; static int compare_time (const void *v0, const void *v1) /* {{{ */ { @@ -203,7 +204,14 @@ static int send_value (lcc_value_list_t *vl) /* {{{ */ int main (int argc, char **argv) /* {{{ */ { - size_t i; + int i; + + values_heap = c_heap_create (compare_time); + if (values_heap == NULL) + { + fprintf (stderr, "c_heap_create failed.\n"); + exit (EXIT_FAILURE); + } net = lcc_network_create (); if (net == NULL) @@ -223,40 +231,41 @@ int main (int argc, char **argv) /* {{{ */ } lcc_server_set_ttl (srv, 42); - } - - values_num = (size_t) conf_num_values; - values = calloc (values_num, sizeof (*values)); - if (values == NULL) - { - fprintf (stderr, "calloc failed.\n"); - exit (EXIT_FAILURE); +#if 0 + lcc_server_set_security_level (srv, ENCRYPT, + "admin", "password1"); +#endif } fprintf (stdout, "Creating %i values ... ", conf_num_values); fflush (stdout); - for (i = 0; i < values_num; i++) + for (i = 0; i < conf_num_values; i++) { - values[i] = create_value_list (); - if (values[i] == NULL) + lcc_value_list_t *vl; + + vl = create_value_list (); + if (vl == NULL) { fprintf (stderr, "create_value_list failed.\n"); exit (EXIT_FAILURE); } + + c_heap_insert (values_heap, vl); } fprintf (stdout, "done\n"); - fprintf (stdout, "Sorting values by time ... "); - fflush (stdout); - qsort (values, values_num, sizeof (*values), compare_time); - fprintf (stdout, "done\n"); + while (42) + { + lcc_value_list_t *vl = c_heap_get_root (values_heap); - for (i = 0; i < values_num; i++) - send_value (values[i]); + if (vl == NULL) + break; + + send_value (vl); + destroy_value_list (vl); + } - for (i = 0; i < values_num; i++) - destroy_value_list (values[i]); - free (values); + c_heap_destroy (values_heap); lcc_network_destroy (net); exit (EXIT_SUCCESS); -- 2.30.2