From: Florian Forster Date: Wed, 7 Apr 2010 09:41:41 +0000 (+0200) Subject: rrdtool plugin: Correctly free the cache when shutting down. X-Git-Tag: collectd-4.8.4~2 X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=4d313b6f21b51a76a2d155d6833a327cfcf235e7;p=collectd.git rrdtool plugin: Correctly free the cache when shutting down. --- diff --git a/src/rrdtool.c b/src/rrdtool.c index ed1ced1f..01fca30a 100644 --- a/src/rrdtool.c +++ b/src/rrdtool.c @@ -432,11 +432,6 @@ static void *rrd_queue_thread (void __attribute__((unused)) *data) sfree (queue_entry); } /* while (42) */ - pthread_mutex_lock (&cache_lock); - c_avl_destroy (cache); - cache = NULL; - pthread_mutex_unlock (&cache_lock); - pthread_exit ((void *) 0); return ((void *) 0); } /* void *rrd_queue_thread */ @@ -786,6 +781,59 @@ static int rrd_cache_insert (const char *filename, return (0); } /* int rrd_cache_insert */ +static int rrd_cache_destroy (void) /* {{{ */ +{ + void *key = NULL; + void *value = NULL; + + int non_empty = 0; + + pthread_mutex_lock (&cache_lock); + + if (cache == NULL) + { + pthread_mutex_unlock (&cache_lock); + return (0); + } + + while (c_avl_pick (cache, &key, &value) == 0) + { + rrd_cache_t *rc; + int i; + + sfree (key); + key = NULL; + + rc = value; + value = NULL; + + if (rc->values_num > 0) + non_empty++; + + for (i = 0; i < rc->values_num; i++) + sfree (rc->values[i]); + sfree (rc->values); + sfree (rc); + } + + c_avl_destroy (cache); + cache = NULL; + + if (non_empty > 0) + { + INFO ("rrdtool plugin: %i cache %s had values when destroying the cache.", + non_empty, (non_empty == 1) ? "entry" : "entries"); + } + else + { + DEBUG ("rrdtool plugin: No values have been lost " + "when destroying the cache."); + } + + pthread_mutex_unlock (&cache_lock); + return (0); +} /* }}} int rrd_cache_destroy */ + static int rrd_compare_numeric (const void *a_ptr, const void *b_ptr) { int a = *((int *) a_ptr); @@ -807,6 +855,9 @@ static int rrd_write (const data_set_t *ds, const value_list_t *vl, char values[512]; int status; + if (do_shutdown) + return (0); + if (0 != strcmp (ds->type, vl->type)) { ERROR ("rrdtool plugin: DS type does not match value list type"); return -1; @@ -1063,7 +1114,7 @@ static int rrd_shutdown (void) DEBUG ("rrdtool plugin: queue_thread exited."); } - /* TODO: Maybe it'd be a good idea to free the cache here.. */ + rrd_cache_destroy (); return (0); } /* int rrd_shutdown */