2 * collectd - src/utils_cache.c
3 * Copyright (C) 2007-2010 Florian octo Forster
4 * Copyright (C) 2016 Sebastian tokkee Harl
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
25 * Florian octo Forster <octo at collectd.org>
26 * Sebastian tokkee Harl <sh at tokkee.org>
32 #include "meta_data.h"
34 #include "utils_avltree.h"
35 #include "utils_cache.h"
39 typedef struct cache_entry_s {
40 char name[6 * DATA_MAX_NAME_LEN];
42 gauge_t *values_gauge;
44 /* Time contained in the package
45 * (for calculating rates) */
47 /* Time according to the local clock
48 * (for purging old entries) */
50 /* Interval in which the data is collected
51 * (for purging old entries) */
57 * +-----+-----+-----+-----+-----+-----+-----+-----+-----+----
58 * ! 0 ! 1 ! 2 ! 3 ! 4 ! 5 ! 6 ! 7 ! 8 ! ...
59 * +-----+-----+-----+-----+-----+-----+-----+-----+-----+----
60 * ! ds0 ! ds1 ! ds2 ! ds0 ! ds1 ! ds2 ! ds0 ! ds1 ! ds2 ! ...
61 * +-----+-----+-----+-----+-----+-----+-----+-----+-----+----
62 * ! t = 0 ! t = 1 ! t = 2 ! ...
63 * +-----------------+-----------------+-----------------+----
66 size_t history_index; /* points to the next position to write to. */
67 size_t history_length;
73 c_avl_iterator_t *iter;
79 static c_avl_tree_t *cache_tree = NULL;
80 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
82 static int cache_compare(const cache_entry_t *a, const cache_entry_t *b) {
84 assert((a != NULL) && (b != NULL));
86 return strcmp(a->name, b->name);
87 } /* int cache_compare */
89 static cache_entry_t *cache_alloc(size_t values_num) {
92 ce = calloc(1, sizeof(*ce));
94 ERROR("utils_cache: cache_alloc: calloc failed.");
97 ce->values_num = values_num;
99 ce->values_gauge = calloc(values_num, sizeof(*ce->values_gauge));
100 ce->values_raw = calloc(values_num, sizeof(*ce->values_raw));
101 if ((ce->values_gauge == NULL) || (ce->values_raw == NULL)) {
102 sfree(ce->values_gauge);
103 sfree(ce->values_raw);
105 ERROR("utils_cache: cache_alloc: calloc failed.");
110 ce->history_length = 0;
114 } /* cache_entry_t *cache_alloc */
116 static void cache_free(cache_entry_t *ce) {
120 sfree(ce->values_gauge);
121 sfree(ce->values_raw);
123 if (ce->meta != NULL) {
124 meta_data_destroy(ce->meta);
128 } /* void cache_free */
130 static void uc_check_range(const data_set_t *ds, cache_entry_t *ce) {
131 for (size_t i = 0; i < ds->ds_num; i++) {
132 if (isnan(ce->values_gauge[i]))
134 else if (ce->values_gauge[i] < ds->ds[i].min)
135 ce->values_gauge[i] = NAN;
136 else if (ce->values_gauge[i] > ds->ds[i].max)
137 ce->values_gauge[i] = NAN;
139 } /* void uc_check_range */
141 static int uc_insert(const data_set_t *ds, const value_list_t *vl,
146 /* `cache_lock' has been locked by `uc_update' */
148 key_copy = strdup(key);
149 if (key_copy == NULL) {
150 ERROR("uc_insert: strdup failed.");
154 ce = cache_alloc(ds->ds_num);
157 ERROR("uc_insert: cache_alloc (%zu) failed.", ds->ds_num);
161 sstrncpy(ce->name, key, sizeof(ce->name));
163 for (size_t i = 0; i < ds->ds_num; i++) {
164 switch (ds->ds[i].type) {
165 case DS_TYPE_COUNTER:
166 ce->values_gauge[i] = NAN;
167 ce->values_raw[i].counter = vl->values[i].counter;
171 ce->values_gauge[i] = vl->values[i].gauge;
172 ce->values_raw[i].gauge = vl->values[i].gauge;
176 ce->values_gauge[i] = NAN;
177 ce->values_raw[i].derive = vl->values[i].derive;
180 case DS_TYPE_ABSOLUTE:
181 ce->values_gauge[i] = NAN;
182 if (vl->interval > 0)
183 ce->values_gauge[i] =
184 ((double)vl->values[i].absolute) / CDTIME_T_TO_DOUBLE(vl->interval);
185 ce->values_raw[i].absolute = vl->values[i].absolute;
189 /* This shouldn't happen. */
190 ERROR("uc_insert: Don't know how to handle data source type %i.",
195 } /* switch (ds->ds[i].type) */
198 /* Prune invalid gauge data */
199 uc_check_range(ds, ce);
201 ce->last_time = vl->time;
202 ce->last_update = cdtime();
203 ce->interval = vl->interval;
204 ce->state = STATE_OKAY;
206 if (c_avl_insert(cache_tree, key_copy, ce) != 0) {
208 ERROR("uc_insert: c_avl_insert failed.");
212 DEBUG("uc_insert: Added %s to the cache.", key);
214 } /* int uc_insert */
217 if (cache_tree == NULL)
219 c_avl_create((int (*)(const void *, const void *))cache_compare);
224 int uc_check_timeout(void) {
230 size_t expired_num = 0;
232 pthread_mutex_lock(&cache_lock);
233 cdtime_t now = cdtime();
235 /* Build a list of entries to be flushed */
236 c_avl_iterator_t *iter = c_avl_get_iterator(cache_tree);
238 cache_entry_t *ce = NULL;
239 while (c_avl_iterator_next(iter, (void *)&key, (void *)&ce) == 0) {
240 /* If the entry is fresh enough, continue. */
241 if ((now - ce->last_update) < (ce->interval * timeout_g))
244 void *tmp = realloc(expired, (expired_num + 1) * sizeof(*expired));
246 ERROR("uc_check_timeout: realloc failed.");
251 expired[expired_num].key = strdup(key);
252 expired[expired_num].time = ce->last_time;
253 expired[expired_num].interval = ce->interval;
255 if (expired[expired_num].key == NULL) {
256 ERROR("uc_check_timeout: strdup failed.");
261 } /* while (c_avl_iterator_next) */
263 c_avl_iterator_destroy(iter);
264 pthread_mutex_unlock(&cache_lock);
266 if (expired_num == 0) {
271 /* Call the "missing" callback for each value. Do this before removing the
272 * value from the cache, so that callbacks can still access the data stored,
273 * including plugin specific meta data, rates, history, …. This must be done
274 * without holding the lock, otherwise we will run into a deadlock if a
275 * plugin calls the cache interface. */
276 for (size_t i = 0; i < expired_num; i++) {
278 .time = expired[i].time, .interval = expired[i].interval,
281 if (parse_identifier_vl(expired[i].key, &vl) != 0) {
282 ERROR("uc_check_timeout: parse_identifier_vl (\"%s\") failed.",
287 plugin_dispatch_missing(&vl);
288 } /* for (i = 0; i < expired_num; i++) */
290 /* Now actually remove all the values from the cache. We don't re-evaluate
291 * the timestamp again, so in theory it is possible we remove a value after
292 * it is updated here. */
293 pthread_mutex_lock(&cache_lock);
294 for (size_t i = 0; i < expired_num; i++) {
296 cache_entry_t *value = NULL;
298 if (c_avl_remove(cache_tree, expired[i].key, (void *)&key,
299 (void *)&value) != 0) {
300 ERROR("uc_check_timeout: c_avl_remove (\"%s\") failed.", expired[i].key);
301 sfree(expired[i].key);
307 sfree(expired[i].key);
308 } /* for (i = 0; i < expired_num; i++) */
309 pthread_mutex_unlock(&cache_lock);
313 } /* int uc_check_timeout */
315 int uc_update(const data_set_t *ds, const value_list_t *vl) {
316 char name[6 * DATA_MAX_NAME_LEN];
317 cache_entry_t *ce = NULL;
320 if (FORMAT_VL(name, sizeof(name), vl) != 0) {
321 ERROR("uc_update: FORMAT_VL failed.");
325 pthread_mutex_lock(&cache_lock);
327 status = c_avl_get(cache_tree, name, (void *)&ce);
328 if (status != 0) /* entry does not yet exist */
330 status = uc_insert(ds, vl, name);
331 pthread_mutex_unlock(&cache_lock);
336 assert(ce->values_num == ds->ds_num);
338 if (ce->last_time >= vl->time) {
339 pthread_mutex_unlock(&cache_lock);
340 NOTICE("uc_update: Value too old: name = %s; value time = %.3f; "
341 "last cache update = %.3f;",
342 name, CDTIME_T_TO_DOUBLE(vl->time),
343 CDTIME_T_TO_DOUBLE(ce->last_time));
347 for (size_t i = 0; i < ds->ds_num; i++) {
348 switch (ds->ds[i].type) {
349 case DS_TYPE_COUNTER: {
351 counter_diff(ce->values_raw[i].counter, vl->values[i].counter);
352 ce->values_gauge[i] =
353 ((double)diff) / (CDTIME_T_TO_DOUBLE(vl->time - ce->last_time));
354 ce->values_raw[i].counter = vl->values[i].counter;
358 ce->values_raw[i].gauge = vl->values[i].gauge;
359 ce->values_gauge[i] = vl->values[i].gauge;
362 case DS_TYPE_DERIVE: {
363 derive_t diff = vl->values[i].derive - ce->values_raw[i].derive;
365 ce->values_gauge[i] =
366 ((double)diff) / (CDTIME_T_TO_DOUBLE(vl->time - ce->last_time));
367 ce->values_raw[i].derive = vl->values[i].derive;
370 case DS_TYPE_ABSOLUTE:
371 ce->values_gauge[i] = ((double)vl->values[i].absolute) /
372 (CDTIME_T_TO_DOUBLE(vl->time - ce->last_time));
373 ce->values_raw[i].absolute = vl->values[i].absolute;
377 /* This shouldn't happen. */
378 pthread_mutex_unlock(&cache_lock);
379 ERROR("uc_update: Don't know how to handle data source type %i.",
382 } /* switch (ds->ds[i].type) */
384 DEBUG("uc_update: %s: ds[%zu] = %lf", name, i, ce->values_gauge[i]);
387 /* Update the history if it exists. */
388 if (ce->history != NULL) {
389 assert(ce->history_index < ce->history_length);
390 for (size_t i = 0; i < ce->values_num; i++) {
391 size_t hist_idx = (ce->values_num * ce->history_index) + i;
392 ce->history[hist_idx] = ce->values_gauge[i];
395 assert(ce->history_length > 0);
396 ce->history_index = (ce->history_index + 1) % ce->history_length;
399 /* Prune invalid gauge data */
400 uc_check_range(ds, ce);
402 ce->last_time = vl->time;
403 ce->last_update = cdtime();
404 ce->interval = vl->interval;
406 pthread_mutex_unlock(&cache_lock);
409 } /* int uc_update */
411 int uc_get_rate_by_name(const char *name, gauge_t **ret_values,
412 size_t *ret_values_num) {
415 cache_entry_t *ce = NULL;
418 pthread_mutex_lock(&cache_lock);
420 if (c_avl_get(cache_tree, name, (void *)&ce) == 0) {
423 /* remove missing values from getval */
424 if (ce->state == STATE_MISSING) {
425 DEBUG("utils_cache: uc_get_rate_by_name: requested metric \"%s\" is in "
426 "state \"missing\".",
430 ret_num = ce->values_num;
431 ret = malloc(ret_num * sizeof(*ret));
433 ERROR("utils_cache: uc_get_rate_by_name: malloc failed.");
436 memcpy(ret, ce->values_gauge, ret_num * sizeof(gauge_t));
440 DEBUG("utils_cache: uc_get_rate_by_name: No such value: %s", name);
444 pthread_mutex_unlock(&cache_lock);
448 *ret_values_num = ret_num;
452 } /* gauge_t *uc_get_rate_by_name */
454 gauge_t *uc_get_rate(const data_set_t *ds, const value_list_t *vl) {
455 char name[6 * DATA_MAX_NAME_LEN];
460 if (FORMAT_VL(name, sizeof(name), vl) != 0) {
461 ERROR("utils_cache: uc_get_rate: FORMAT_VL failed.");
465 status = uc_get_rate_by_name(name, &ret, &ret_num);
469 /* This is important - the caller has no other way of knowing how many
470 * values are returned. */
471 if (ret_num != ds->ds_num) {
472 ERROR("utils_cache: uc_get_rate: ds[%s] has %zu values, "
473 "but uc_get_rate_by_name returned %zu.",
474 ds->type, ds->ds_num, ret_num);
480 } /* gauge_t *uc_get_rate */
482 int uc_get_value_by_name(const char *name, value_t **ret_values,
483 size_t *ret_values_num) {
486 cache_entry_t *ce = NULL;
489 pthread_mutex_lock(&cache_lock);
491 if (c_avl_get(cache_tree, name, (void *) &ce) == 0) {
494 /* remove missing values from getval */
495 if (ce->state == STATE_MISSING) {
498 ret_num = ce->values_num;
499 ret = malloc(ret_num * sizeof(*ret));
501 ERROR("utils_cache: uc_get_value_by_name: malloc failed.");
504 memcpy(ret, ce->values_raw, ret_num * sizeof(value_t));
509 DEBUG("utils_cache: uc_get_value_by_name: No such value: %s", name);
513 pthread_mutex_unlock(&cache_lock);
517 *ret_values_num = ret_num;
521 } /* int uc_get_value_by_name */
523 value_t *uc_get_value(const data_set_t *ds, const value_list_t *vl) {
524 char name[6 * DATA_MAX_NAME_LEN];
529 if (FORMAT_VL(name, sizeof(name), vl) != 0) {
530 ERROR("utils_cache: uc_get_value: FORMAT_VL failed.");
534 status = uc_get_value_by_name(name, &ret, &ret_num);
538 /* This is important - the caller has no other way of knowing how many
539 * values are returned. */
540 if (ret_num != (size_t) ds->ds_num) {
541 ERROR("utils_cache: uc_get_value: ds[%s] has %zu values, "
542 "but uc_get_value_by_name returned %zu.", ds->type, ds->ds_num,
549 } /* value_t *uc_get_value */
551 size_t uc_get_size(void) {
552 size_t size_arrays = 0;
554 pthread_mutex_lock(&cache_lock);
555 size_arrays = (size_t)c_avl_size(cache_tree);
556 pthread_mutex_unlock(&cache_lock);
561 int uc_get_names(char ***ret_names, cdtime_t **ret_times, size_t *ret_number) {
562 c_avl_iterator_t *iter;
564 cache_entry_t *value;
567 cdtime_t *times = NULL;
569 size_t size_arrays = 0;
573 if ((ret_names == NULL) || (ret_number == NULL))
576 pthread_mutex_lock(&cache_lock);
578 size_arrays = (size_t)c_avl_size(cache_tree);
579 if (size_arrays < 1) {
580 /* Handle the "no values" case here, to avoid the error message when
581 * calloc() returns NULL. */
582 pthread_mutex_unlock(&cache_lock);
586 names = calloc(size_arrays, sizeof(*names));
587 times = calloc(size_arrays, sizeof(*times));
588 if ((names == NULL) || (times == NULL)) {
589 ERROR("uc_get_names: calloc failed.");
592 pthread_mutex_unlock(&cache_lock);
596 iter = c_avl_get_iterator(cache_tree);
597 while (c_avl_iterator_next(iter, (void *)&key, (void *)&value) == 0) {
598 /* remove missing values when list values */
599 if (value->state == STATE_MISSING)
602 /* c_avl_size does not return a number smaller than the number of elements
603 * returned by c_avl_iterator_next. */
604 assert(number < size_arrays);
606 if (ret_times != NULL)
607 times[number] = value->last_time;
609 names[number] = strdup(key);
610 if (names[number] == NULL) {
616 } /* while (c_avl_iterator_next) */
618 c_avl_iterator_destroy(iter);
619 pthread_mutex_unlock(&cache_lock);
622 for (size_t i = 0; i < number; i++) {
632 if (ret_times != NULL)
636 *ret_number = number;
639 } /* int uc_get_names */
641 int uc_get_state(const data_set_t *ds, const value_list_t *vl) {
642 char name[6 * DATA_MAX_NAME_LEN];
643 cache_entry_t *ce = NULL;
644 int ret = STATE_ERROR;
646 if (FORMAT_VL(name, sizeof(name), vl) != 0) {
647 ERROR("uc_get_state: FORMAT_VL failed.");
651 pthread_mutex_lock(&cache_lock);
653 if (c_avl_get(cache_tree, name, (void *)&ce) == 0) {
658 pthread_mutex_unlock(&cache_lock);
661 } /* int uc_get_state */
663 int uc_set_state(const data_set_t *ds, const value_list_t *vl, int state) {
664 char name[6 * DATA_MAX_NAME_LEN];
665 cache_entry_t *ce = NULL;
668 if (FORMAT_VL(name, sizeof(name), vl) != 0) {
669 ERROR("uc_set_state: FORMAT_VL failed.");
673 pthread_mutex_lock(&cache_lock);
675 if (c_avl_get(cache_tree, name, (void *)&ce) == 0) {
681 pthread_mutex_unlock(&cache_lock);
684 } /* int uc_set_state */
686 int uc_get_history_by_name(const char *name, gauge_t *ret_history,
687 size_t num_steps, size_t num_ds) {
688 cache_entry_t *ce = NULL;
691 pthread_mutex_lock(&cache_lock);
693 status = c_avl_get(cache_tree, name, (void *)&ce);
695 pthread_mutex_unlock(&cache_lock);
699 if (((size_t)ce->values_num) != num_ds) {
700 pthread_mutex_unlock(&cache_lock);
704 /* Check if there are enough values available. If not, increase the buffer
706 if (ce->history_length < num_steps) {
710 realloc(ce->history, sizeof(*ce->history) * num_steps * ce->values_num);
712 pthread_mutex_unlock(&cache_lock);
716 for (size_t i = ce->history_length * ce->values_num;
717 i < (num_steps * ce->values_num); i++)
721 ce->history_length = num_steps;
722 } /* if (ce->history_length < num_steps) */
724 /* Copy the values to the output buffer. */
725 for (size_t i = 0; i < num_steps; i++) {
729 if (i < ce->history_index)
730 src_index = ce->history_index - (i + 1);
732 src_index = ce->history_length + ce->history_index - (i + 1);
733 src_index = src_index * num_ds;
735 dst_index = i * num_ds;
737 memcpy(ret_history + dst_index, ce->history + src_index,
738 sizeof(*ret_history) * num_ds);
741 pthread_mutex_unlock(&cache_lock);
744 } /* int uc_get_history_by_name */
746 int uc_get_history(const data_set_t *ds, const value_list_t *vl,
747 gauge_t *ret_history, size_t num_steps, size_t num_ds) {
748 char name[6 * DATA_MAX_NAME_LEN];
750 if (FORMAT_VL(name, sizeof(name), vl) != 0) {
751 ERROR("utils_cache: uc_get_history: FORMAT_VL failed.");
755 return uc_get_history_by_name(name, ret_history, num_steps, num_ds);
756 } /* int uc_get_history */
758 int uc_get_hits(const data_set_t *ds, const value_list_t *vl) {
759 char name[6 * DATA_MAX_NAME_LEN];
760 cache_entry_t *ce = NULL;
761 int ret = STATE_ERROR;
763 if (FORMAT_VL(name, sizeof(name), vl) != 0) {
764 ERROR("uc_get_hits: FORMAT_VL failed.");
768 pthread_mutex_lock(&cache_lock);
770 if (c_avl_get(cache_tree, name, (void *)&ce) == 0) {
775 pthread_mutex_unlock(&cache_lock);
778 } /* int uc_get_hits */
780 int uc_set_hits(const data_set_t *ds, const value_list_t *vl, int hits) {
781 char name[6 * DATA_MAX_NAME_LEN];
782 cache_entry_t *ce = NULL;
785 if (FORMAT_VL(name, sizeof(name), vl) != 0) {
786 ERROR("uc_set_hits: FORMAT_VL failed.");
790 pthread_mutex_lock(&cache_lock);
792 if (c_avl_get(cache_tree, name, (void *)&ce) == 0) {
798 pthread_mutex_unlock(&cache_lock);
801 } /* int uc_set_hits */
803 int uc_inc_hits(const data_set_t *ds, const value_list_t *vl, int step) {
804 char name[6 * DATA_MAX_NAME_LEN];
805 cache_entry_t *ce = NULL;
808 if (FORMAT_VL(name, sizeof(name), vl) != 0) {
809 ERROR("uc_inc_hits: FORMAT_VL failed.");
813 pthread_mutex_lock(&cache_lock);
815 if (c_avl_get(cache_tree, name, (void *)&ce) == 0) {
818 ce->hits = ret + step;
821 pthread_mutex_unlock(&cache_lock);
824 } /* int uc_inc_hits */
829 uc_iter_t *uc_get_iterator(void) {
832 iter = (uc_iter_t *)calloc(1, sizeof(*iter));
836 pthread_mutex_lock(&cache_lock);
838 iter->iter = c_avl_get_iterator(cache_tree);
839 if (iter->iter == NULL) {
845 } /* uc_iter_t *uc_get_iterator */
847 int uc_iterator_next(uc_iter_t *iter, char **ret_name) {
853 while ((status = c_avl_iterator_next(iter->iter, (void *)&iter->name,
854 (void *)&iter->entry)) == 0) {
855 if (iter->entry->state == STATE_MISSING)
866 if (ret_name != NULL)
867 *ret_name = iter->name;
870 } /* int uc_iterator_next */
872 void uc_iterator_destroy(uc_iter_t *iter) {
876 c_avl_iterator_destroy(iter->iter);
877 pthread_mutex_unlock(&cache_lock);
880 } /* void uc_iterator_destroy */
882 int uc_iterator_get_time(uc_iter_t *iter, cdtime_t *ret_time) {
883 if ((iter == NULL) || (iter->entry == NULL) || (ret_time == NULL))
886 *ret_time = iter->entry->last_time;
888 } /* int uc_iterator_get_name */
890 int uc_iterator_get_values(uc_iter_t *iter, value_t **ret_values,
892 if ((iter == NULL) || (iter->entry == NULL) || (ret_values == NULL) ||
897 calloc(iter->entry->values_num, sizeof(*iter->entry->values_raw));
898 if (*ret_values == NULL)
900 for (size_t i = 0; i < iter->entry->values_num; ++i)
901 *ret_values[i] = iter->entry->values_raw[i];
903 *ret_num = iter->entry->values_num;
906 } /* int uc_iterator_get_values */
908 int uc_iterator_get_interval(uc_iter_t *iter, cdtime_t *ret_interval) {
909 if ((iter == NULL) || (iter->entry == NULL) || (ret_interval == NULL))
912 *ret_interval = iter->entry->interval;
914 } /* int uc_iterator_get_name */
916 int uc_iterator_get_meta(uc_iter_t *iter, meta_data_t **ret_meta) {
917 if ((iter == NULL) || (iter->entry == NULL) || (ret_meta == NULL))
920 *ret_meta = meta_data_clone(iter->entry->meta);
923 } /* int uc_iterator_get_meta */
926 * Meta data interface
928 /* XXX: This function will acquire `cache_lock' but will not free it! */
929 static meta_data_t *uc_get_meta(const value_list_t *vl) /* {{{ */
931 char name[6 * DATA_MAX_NAME_LEN];
932 cache_entry_t *ce = NULL;
935 status = FORMAT_VL(name, sizeof(name), vl);
937 ERROR("utils_cache: uc_get_meta: FORMAT_VL failed.");
941 pthread_mutex_lock(&cache_lock);
943 status = c_avl_get(cache_tree, name, (void *)&ce);
945 pthread_mutex_unlock(&cache_lock);
950 if (ce->meta == NULL)
951 ce->meta = meta_data_create();
953 if (ce->meta == NULL)
954 pthread_mutex_unlock(&cache_lock);
957 } /* }}} meta_data_t *uc_get_meta */
959 /* Sorry about this preprocessor magic, but it really makes this file much
961 #define UC_WRAP(wrap_function) \
965 meta = uc_get_meta(vl); \
968 status = wrap_function(meta, key); \
969 pthread_mutex_unlock(&cache_lock); \
972 int uc_meta_data_exists(const value_list_t *vl,
973 const char *key) UC_WRAP(meta_data_exists)
975 int uc_meta_data_delete(const value_list_t *vl,
976 const char *key) UC_WRAP(meta_data_delete)
979 /* We need a new version of this macro because the following functions take
981 #define UC_WRAP(wrap_function) \
985 meta = uc_get_meta(vl); \
988 status = wrap_function(meta, key, value); \
989 pthread_mutex_unlock(&cache_lock); \
992 int uc_meta_data_add_string(const value_list_t *vl, const char *key,
994 UC_WRAP(meta_data_add_string) int uc_meta_data_add_signed_int(
995 const value_list_t *vl, const char *key, int64_t value)
996 UC_WRAP(meta_data_add_signed_int) int uc_meta_data_add_unsigned_int(
997 const value_list_t *vl, const char *key, uint64_t value)
998 UC_WRAP(meta_data_add_unsigned_int) int uc_meta_data_add_double(
999 const value_list_t *vl, const char *key, double value)
1000 UC_WRAP(meta_data_add_double) int uc_meta_data_add_boolean(
1001 const value_list_t *vl, const char *key,
1002 _Bool value) UC_WRAP(meta_data_add_boolean)
1004 int uc_meta_data_get_string(const value_list_t *vl,
1007 UC_WRAP(meta_data_get_string) int uc_meta_data_get_signed_int(
1008 const value_list_t *vl, const char *key,
1010 UC_WRAP(meta_data_get_signed_int) int uc_meta_data_get_unsigned_int(
1011 const value_list_t *vl, const char *key,
1013 UC_WRAP(meta_data_get_unsigned_int) int uc_meta_data_get_double(
1014 const value_list_t *vl,
1015 const char *key, double *value)
1016 UC_WRAP(meta_data_get_double) int uc_meta_data_get_boolean(
1017 const value_list_t *vl,
1018 const char *key, _Bool *value)
1019 UC_WRAP(meta_data_get_boolean)