X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Futils_cache.c;h=c471ee28d29e1f87bf6a38a856debb932b7bf1c2;hb=a813e8af8379bd0f55a2b46fc7918fa31de9522e;hp=b9b896215be1cb631f5c1b0252bcf4c341af6846;hpb=4f2642f86673329db9f8cf30854bf39bbdc4c2b2;p=collectd.git diff --git a/src/utils_cache.c b/src/utils_cache.c index b9b89621..c471ee28 100644 --- a/src/utils_cache.c +++ b/src/utils_cache.c @@ -460,11 +460,12 @@ int uc_update (const data_set_t *ds, const value_list_t *vl) return (0); } /* int uc_update */ -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; + int status = 0; if (FORMAT_VL (name, sizeof (name), vl, ds) != 0) { @@ -477,21 +478,64 @@ gauge_t *uc_get_rate (const data_set_t *ds, const value_list_t *vl) 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 */