From d91c8449c887c87cd776d06eb07b78cca73707b1 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Sun, 31 Oct 2010 12:51:43 +0100 Subject: [PATCH] src/plugin.[ch]: Change the flush callbacks to take a "cdtime_t". --- src/network.c | 6 +++--- src/plugin.c | 2 +- src/plugin.h | 4 ++-- src/rrdtool.c | 33 +++++++++++++++++++-------------- src/write_http.c | 28 +++++++++++++++------------- 5 files changed, 40 insertions(+), 33 deletions(-) diff --git a/src/network.c b/src/network.c index 93d403f9..94856b40 100644 --- a/src/network.c +++ b/src/network.c @@ -3351,9 +3351,9 @@ static int network_init (void) * just send the buffer if `flush' is called - if the requested value was in * there, good. If not, well, then there is nothing to flush.. -octo */ -static int network_flush (int timeout, - const char __attribute__((unused)) *identifier, - user_data_t __attribute__((unused)) *user_data) +static int network_flush (__attribute__((unused)) cdtime_t timeout, + __attribute__((unused)) const char *identifier, + __attribute__((unused)) user_data_t *user_data) { pthread_mutex_lock (&send_buffer_lock); diff --git a/src/plugin.c b/src/plugin.c index 061b78bb..fb77a134 100644 --- a/src/plugin.c +++ b/src/plugin.c @@ -1251,7 +1251,7 @@ int plugin_write (const char *plugin, /* {{{ */ return (status); } /* }}} int plugin_write */ -int plugin_flush (const char *plugin, int timeout, const char *identifier) +int plugin_flush (const char *plugin, cdtime_t timeout, const char *identifier) { llentry_t *le; diff --git a/src/plugin.h b/src/plugin.h index 8fe7fc4a..eb9f81f8 100644 --- a/src/plugin.h +++ b/src/plugin.h @@ -168,7 +168,7 @@ typedef int (*plugin_init_cb) (void); typedef int (*plugin_read_cb) (user_data_t *); typedef int (*plugin_write_cb) (const data_set_t *, const value_list_t *, user_data_t *); -typedef int (*plugin_flush_cb) (int timeout, const char *identifier, +typedef int (*plugin_flush_cb) (cdtime_t timeout, const char *identifier, user_data_t *); typedef void (*plugin_log_cb) (int severity, const char *message, user_data_t *); @@ -249,7 +249,7 @@ void plugin_shutdown_all (void); int plugin_write (const char *plugin, const data_set_t *ds, const value_list_t *vl); -int plugin_flush (const char *plugin, int timeout, const char *identifier); +int plugin_flush (const char *plugin, cdtime_t timeout, const char *identifier); /* * The `plugin_register_*' functions are used to make `config', `init', diff --git a/src/rrdtool.c b/src/rrdtool.c index 9ef0fa1f..1e9e52a9 100644 --- a/src/rrdtool.c +++ b/src/rrdtool.c @@ -512,10 +512,11 @@ static int rrd_queue_dequeue (const char *filename, return (0); } /* int rrd_queue_dequeue */ -static void rrd_cache_flush (int timeout) +/* XXX: You must hold "cache_lock" when calling this function! */ +static void rrd_cache_flush (cdtime_t timeout) { rrd_cache_t *rc; - time_t now; + cdtime_t now; char **keys = NULL; int keys_num = 0; @@ -524,9 +525,11 @@ static void rrd_cache_flush (int timeout) c_avl_iterator_t *iter; int i; - DEBUG ("rrdtool plugin: Flushing cache, timeout = %i", timeout); + DEBUG ("rrdtool plugin: Flushing cache, timeout = %.3f", + CDTIME_T_TO_DOUBLE (timeout)); - now = time (NULL); + now = cdtime (); + timeout = TIME_T_TO_CDTIME_T (timeout); /* Build a list of entries to be flushed */ iter = c_avl_get_iterator (cache); @@ -534,7 +537,9 @@ static void rrd_cache_flush (int timeout) { if (rc->flags != FLAG_NONE) continue; - else if ((now - rc->first_value) < timeout) + /* timeout == 0 => flush everything */ + else if ((timeout != 0) + && ((now - rc->first_value) < timeout)) continue; else if (rc->values_num > 0) { @@ -587,10 +592,11 @@ static void rrd_cache_flush (int timeout) cache_flush_last = now; } /* void rrd_cache_flush */ -static int rrd_cache_flush_identifier (int timeout, const char *identifier) +static int rrd_cache_flush_identifier (cdtime_t timeout, + const char *identifier) { rrd_cache_t *rc; - time_t now; + cdtime_t now; int status; char key[2048]; @@ -600,7 +606,7 @@ static int rrd_cache_flush_identifier (int timeout, const char *identifier) return (0); } - now = time (NULL); + now = cdtime (); if (datadir == NULL) snprintf (key, sizeof (key), "%s.rrd", @@ -921,8 +927,8 @@ static int rrd_write (const data_set_t *ds, const value_list_t *vl, return (status); } /* int rrd_write */ -static int rrd_flush (int timeout, const char *identifier, - user_data_t __attribute__((unused)) *user_data) +static int rrd_flush (cdtime_t timeout, const char *identifier, + __attribute__((unused)) user_data_t *user_data) { pthread_mutex_lock (&cache_lock); @@ -1108,7 +1114,7 @@ static int rrd_config (const char *key, const char *value) static int rrd_shutdown (void) { pthread_mutex_lock (&cache_lock); - rrd_cache_flush (-1); + rrd_cache_flush (0); pthread_mutex_unlock (&cache_lock); pthread_mutex_lock (&queue_lock); @@ -1176,10 +1182,9 @@ static int rrd_init (void) return (-1); } - cache_flush_last = time (NULL); - if (cache_timeout < 2) + cache_flush_last = cdtime (); + if (cache_timeout == 0) { - cache_timeout = 0; cache_flush_timeout = 0; } else if (cache_flush_timeout < cache_timeout) diff --git a/src/write_http.c b/src/write_http.c index bac8e986..9cf9fe11 100644 --- a/src/write_http.c +++ b/src/write_http.c @@ -61,7 +61,7 @@ struct wh_callback_s char send_buffer[4096]; size_t send_buffer_free; size_t send_buffer_fill; - time_t send_buffer_init_time; + cdtime_t send_buffer_init_time; pthread_mutex_t send_lock; }; @@ -72,7 +72,7 @@ static void wh_reset_buffer (wh_callback_t *cb) /* {{{ */ memset (cb->send_buffer, 0, sizeof (cb->send_buffer)); cb->send_buffer_free = sizeof (cb->send_buffer); cb->send_buffer_fill = 0; - cb->send_buffer_init_time = time (NULL); + cb->send_buffer_init_time = cdtime (); if (cb->format == WH_FORMAT_JSON) { @@ -157,19 +157,21 @@ static int wh_callback_init (wh_callback_t *cb) /* {{{ */ return (0); } /* }}} int wh_callback_init */ -static int wh_flush_nolock (int timeout, wh_callback_t *cb) /* {{{ */ +static int wh_flush_nolock (cdtime_t timeout, wh_callback_t *cb) /* {{{ */ { int status; - DEBUG ("write_http plugin: wh_flush_nolock: timeout = %i; " + DEBUG ("write_http plugin: wh_flush_nolock: timeout = %.3f; " "send_buffer_fill = %zu;", - timeout, cb->send_buffer_fill); + CDTIME_T_TO_DOUBLE (timeout), + cb->send_buffer_fill); + /* timeout == 0 => flush unconditionally */ if (timeout > 0) { - time_t now; + cdtime_t now; - now = time (NULL); + now = cdtime (); if ((cb->send_buffer_init_time + timeout) > now) return (0); } @@ -178,7 +180,7 @@ static int wh_flush_nolock (int timeout, wh_callback_t *cb) /* {{{ */ { if (cb->send_buffer_fill <= 0) { - cb->send_buffer_init_time = time (NULL); + cb->send_buffer_init_time = cdtime (); return (0); } @@ -189,7 +191,7 @@ static int wh_flush_nolock (int timeout, wh_callback_t *cb) /* {{{ */ { if (cb->send_buffer_fill <= 2) { - cb->send_buffer_init_time = time (NULL); + cb->send_buffer_init_time = cdtime (); return (0); } @@ -218,7 +220,7 @@ static int wh_flush_nolock (int timeout, wh_callback_t *cb) /* {{{ */ return (status); } /* }}} wh_flush_nolock */ -static int wh_flush (int timeout, /* {{{ */ +static int wh_flush (cdtime_t timeout, /* {{{ */ const char *identifier __attribute__((unused)), user_data_t *user_data) { @@ -258,7 +260,7 @@ static void wh_callback_free (void *data) /* {{{ */ cb = data; - wh_flush_nolock (/* timeout = */ -1, cb); + wh_flush_nolock (/* timeout = */ 0, cb); curl_easy_cleanup (cb->curl); sfree (cb->location); @@ -327,7 +329,7 @@ static int wh_write_command (const data_set_t *ds, const value_list_t *vl, /* {{ if (command_len >= cb->send_buffer_free) { - status = wh_flush_nolock (/* timeout = */ -1, cb); + status = wh_flush_nolock (/* timeout = */ 0, cb); if (status != 0) { pthread_mutex_unlock (&cb->send_lock); @@ -379,7 +381,7 @@ static int wh_write_json (const data_set_t *ds, const value_list_t *vl, /* {{{ * ds, vl, cb->store_rates); if (status == (-ENOMEM)) { - status = wh_flush_nolock (/* timeout = */ -1, cb); + status = wh_flush_nolock (/* timeout = */ 0, cb); if (status != 0) { wh_reset_buffer (cb); -- 2.11.0