From: Florian Forster Date: Wed, 27 Feb 2008 09:07:57 +0000 (+0100) Subject: src/utils_cache.[ch]: Implemented `uc_get_rate_by_name'. X-Git-Tag: collectd-4.4.0~87^2~4^2~3 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=985cc61fa747ad02749037c2d08497fce9703c32;p=collectd.git src/utils_cache.[ch]: Implemented `uc_get_rate_by_name'. This functions allows to get values from the cache without creating a `value_list_t' and `data_set_t' first. The existing function `uc_get_rate' has been changed to use this function, too. --- diff --git a/src/utils_cache.c b/src/utils_cache.c index 0d6961e7..e7ab03cd 100644 --- a/src/utils_cache.c +++ b/src/utils_cache.c @@ -404,38 +404,76 @@ int uc_update (const data_set_t *ds, const value_list_t *vl) return (0); } /* int uc_insert */ -gauge_t *uc_get_rate (const data_set_t *ds, const value_list_t *vl) +int uc_get_rate_by_name (const char *name, gauge_t **ret_values, size_t *ret_values_num) { - char name[6 * DATA_MAX_NAME_LEN]; gauge_t *ret = NULL; + size_t ret_num = 0; cache_entry_t *ce = NULL; - - if (FORMAT_VL (name, sizeof (name), vl, ds) != 0) - { - ERROR ("uc_insert: FORMAT_VL failed."); - return (NULL); - } + int status = 0; pthread_mutex_lock (&cache_lock); if (c_avl_get (cache_tree, name, (void *) &ce) == 0) { assert (ce != NULL); - assert (ce->values_num == ds->ds_num); - ret = (gauge_t *) malloc (ce->values_num * sizeof (gauge_t)); + ret_num = ce->values_num; + ret = (gauge_t *) malloc (ret_num * sizeof (gauge_t)); if (ret == NULL) { - ERROR ("uc_get_rate: malloc failed."); + ERROR ("utils_cache: uc_get_rate_by_name: malloc failed."); + status = -1; } else { - memcpy (ret, ce->values_gauge, ce->values_num * sizeof (gauge_t)); + memcpy (ret, ce->values_gauge, ret_num * sizeof (gauge_t)); } } + else + { + DEBUG ("utils_cache: uc_get_rate_by_name: No such value: %s", name); + status = -1; + } pthread_mutex_unlock (&cache_lock); + if (status == 0) + { + *ret_values = ret; + *ret_values_num = ret_num; + } + + return (status); +} /* gauge_t *uc_get_rate_by_name */ + +gauge_t *uc_get_rate (const data_set_t *ds, const value_list_t *vl) +{ + char name[6 * DATA_MAX_NAME_LEN]; + gauge_t *ret = NULL; + size_t ret_num = 0; + int status; + + if (FORMAT_VL (name, sizeof (name), vl, ds) != 0) + { + ERROR ("uc_insert: FORMAT_VL failed."); + return (NULL); + } + + status = uc_get_rate_by_name (name, &ret, &ret_num); + if (status != 0) + return (NULL); + + /* This is important - the caller has no other way of knowing how many + * values are returned. */ + if (ret_num != ds->ds_num) + { + ERROR ("utils_cache: uc_get_rate: ds[%s] has %i values, " + "but uc_get_rate_by_name returned %i.", + ds->type, ds->ds_num, ret_num); + sfree (ret); + return (NULL); + } + return (ret); } /* gauge_t *uc_get_rate */ diff --git a/src/utils_cache.h b/src/utils_cache.h index d6a56ab4..ed6830b6 100644 --- a/src/utils_cache.h +++ b/src/utils_cache.h @@ -31,6 +31,7 @@ int uc_init (void); int uc_check_timeout (void); int uc_update (const data_set_t *ds, const value_list_t *vl); +int uc_get_rate_by_name (const char *name, gauge_t **ret_values, size_t *ret_values_num); gauge_t *uc_get_rate (const data_set_t *ds, const value_list_t *vl); int uc_get_state (const data_set_t *ds, const value_list_t *vl);