Tree wide: Reformat with clang-format.
[collectd.git] / src / daemon / utils_cache.c
1 /**
2  * collectd - src/utils_cache.c
3  * Copyright (C) 2007-2010  Florian octo Forster
4  * Copyright (C) 2016       Sebastian tokkee Harl
5  *
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:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
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.
23  *
24  * Authors:
25  *   Florian octo Forster <octo at collectd.org>
26  *   Sebastian tokkee Harl <sh at tokkee.org>
27  **/
28
29 #include "collectd.h"
30
31 #include "common.h"
32 #include "meta_data.h"
33 #include "plugin.h"
34 #include "utils_avltree.h"
35 #include "utils_cache.h"
36
37 #include <assert.h>
38
39 typedef struct cache_entry_s {
40   char name[6 * DATA_MAX_NAME_LEN];
41   size_t values_num;
42   gauge_t *values_gauge;
43   value_t *values_raw;
44   /* Time contained in the package
45    * (for calculating rates) */
46   cdtime_t last_time;
47   /* Time according to the local clock
48    * (for purging old entries) */
49   cdtime_t last_update;
50   /* Interval in which the data is collected
51    * (for purging old entries) */
52   cdtime_t interval;
53   int state;
54   int hits;
55
56   /*
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    * +-----------------+-----------------+-----------------+----
64    */
65   gauge_t *history;
66   size_t history_index; /* points to the next position to write to. */
67   size_t history_length;
68
69   meta_data_t *meta;
70 } cache_entry_t;
71
72 struct uc_iter_s {
73   c_avl_iterator_t *iter;
74
75   char *name;
76   cache_entry_t *entry;
77 };
78
79 static c_avl_tree_t *cache_tree = NULL;
80 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
81
82 static int cache_compare(const cache_entry_t *a, const cache_entry_t *b) {
83 #if COLLECT_DEBUG
84   assert((a != NULL) && (b != NULL));
85 #endif
86   return (strcmp(a->name, b->name));
87 } /* int cache_compare */
88
89 static cache_entry_t *cache_alloc(size_t values_num) {
90   cache_entry_t *ce;
91
92   ce = calloc(1, sizeof(*ce));
93   if (ce == NULL) {
94     ERROR("utils_cache: cache_alloc: calloc failed.");
95     return (NULL);
96   }
97   ce->values_num = values_num;
98
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);
104     sfree(ce);
105     ERROR("utils_cache: cache_alloc: calloc failed.");
106     return (NULL);
107   }
108
109   ce->history = NULL;
110   ce->history_length = 0;
111   ce->meta = NULL;
112
113   return (ce);
114 } /* cache_entry_t *cache_alloc */
115
116 static void cache_free(cache_entry_t *ce) {
117   if (ce == NULL)
118     return;
119
120   sfree(ce->values_gauge);
121   sfree(ce->values_raw);
122   sfree(ce->history);
123   if (ce->meta != NULL) {
124     meta_data_destroy(ce->meta);
125     ce->meta = NULL;
126   }
127   sfree(ce);
128 } /* void cache_free */
129
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]))
133       continue;
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;
138   }
139 } /* void uc_check_range */
140
141 static int uc_insert(const data_set_t *ds, const value_list_t *vl,
142                      const char *key) {
143   char *key_copy;
144   cache_entry_t *ce;
145
146   /* `cache_lock' has been locked by `uc_update' */
147
148   key_copy = strdup(key);
149   if (key_copy == NULL) {
150     ERROR("uc_insert: strdup failed.");
151     return (-1);
152   }
153
154   ce = cache_alloc(ds->ds_num);
155   if (ce == NULL) {
156     sfree(key_copy);
157     ERROR("uc_insert: cache_alloc (%zu) failed.", ds->ds_num);
158     return (-1);
159   }
160
161   sstrncpy(ce->name, key, sizeof(ce->name));
162
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;
168       break;
169
170     case DS_TYPE_GAUGE:
171       ce->values_gauge[i] = vl->values[i].gauge;
172       ce->values_raw[i].gauge = vl->values[i].gauge;
173       break;
174
175     case DS_TYPE_DERIVE:
176       ce->values_gauge[i] = NAN;
177       ce->values_raw[i].derive = vl->values[i].derive;
178       break;
179
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;
186       break;
187
188     default:
189       /* This shouldn't happen. */
190       ERROR("uc_insert: Don't know how to handle data source type %i.",
191             ds->ds[i].type);
192       sfree(key_copy);
193       cache_free(ce);
194       return (-1);
195     } /* switch (ds->ds[i].type) */
196   }   /* for (i) */
197
198   /* Prune invalid gauge data */
199   uc_check_range(ds, ce);
200
201   ce->last_time = vl->time;
202   ce->last_update = cdtime();
203   ce->interval = vl->interval;
204   ce->state = STATE_OKAY;
205
206   if (c_avl_insert(cache_tree, key_copy, ce) != 0) {
207     sfree(key_copy);
208     ERROR("uc_insert: c_avl_insert failed.");
209     return (-1);
210   }
211
212   DEBUG("uc_insert: Added %s to the cache.", key);
213   return (0);
214 } /* int uc_insert */
215
216 int uc_init(void) {
217   if (cache_tree == NULL)
218     cache_tree =
219         c_avl_create((int (*)(const void *, const void *))cache_compare);
220
221   return (0);
222 } /* int uc_init */
223
224 int uc_check_timeout(void) {
225   cdtime_t now;
226   cache_entry_t *ce;
227
228   char **keys = NULL;
229   cdtime_t *keys_time = NULL;
230   cdtime_t *keys_interval = NULL;
231   int keys_len = 0;
232
233   char *key;
234   c_avl_iterator_t *iter;
235
236   int status;
237
238   pthread_mutex_lock(&cache_lock);
239
240   now = cdtime();
241
242   /* Build a list of entries to be flushed */
243   iter = c_avl_get_iterator(cache_tree);
244   while (c_avl_iterator_next(iter, (void *)&key, (void *)&ce) == 0) {
245     char **tmp;
246     cdtime_t *tmp_time;
247
248     /* If the entry is fresh enough, continue. */
249     if ((now - ce->last_update) < (ce->interval * timeout_g))
250       continue;
251
252     /* If entry has not been updated, add to `keys' array */
253     tmp = realloc((void *)keys, (keys_len + 1) * sizeof(char *));
254     if (tmp == NULL) {
255       ERROR("uc_check_timeout: realloc failed.");
256       continue;
257     }
258     keys = tmp;
259
260     tmp_time = realloc(keys_time, (keys_len + 1) * sizeof(*keys_time));
261     if (tmp_time == NULL) {
262       ERROR("uc_check_timeout: realloc failed.");
263       continue;
264     }
265     keys_time = tmp_time;
266
267     tmp_time = realloc(keys_interval, (keys_len + 1) * sizeof(*keys_interval));
268     if (tmp_time == NULL) {
269       ERROR("uc_check_timeout: realloc failed.");
270       continue;
271     }
272     keys_interval = tmp_time;
273
274     keys[keys_len] = strdup(key);
275     if (keys[keys_len] == NULL) {
276       ERROR("uc_check_timeout: strdup failed.");
277       continue;
278     }
279     keys_time[keys_len] = ce->last_time;
280     keys_interval[keys_len] = ce->interval;
281
282     keys_len++;
283   } /* while (c_avl_iterator_next) */
284
285   c_avl_iterator_destroy(iter);
286   pthread_mutex_unlock(&cache_lock);
287
288   if (keys_len == 0) {
289     /* realloc() may have been called for these. */
290     sfree(keys);
291     sfree(keys_time);
292     sfree(keys_interval);
293     return (0);
294   }
295
296   /* Call the "missing" callback for each value. Do this before removing the
297    * value from the cache, so that callbacks can still access the data stored,
298    * including plugin specific meta data, rates, history, â€¦. This must be done
299    * without holding the lock, otherwise we will run into a deadlock if a
300    * plugin calls the cache interface. */
301   for (int i = 0; i < keys_len; i++) {
302     value_list_t vl = VALUE_LIST_INIT;
303
304     vl.values = NULL;
305     vl.values_len = 0;
306     vl.meta = NULL;
307
308     status = parse_identifier_vl(keys[i], &vl);
309     if (status != 0) {
310       ERROR("uc_check_timeout: parse_identifier_vl (\"%s\") failed.", keys[i]);
311       continue;
312     }
313
314     vl.time = keys_time[i];
315     vl.interval = keys_interval[i];
316
317     plugin_dispatch_missing(&vl);
318   } /* for (i = 0; i < keys_len; i++) */
319
320   /* Now actually remove all the values from the cache. We don't re-evaluate
321    * the timestamp again, so in theory it is possible we remove a value after
322    * it is updated here. */
323   pthread_mutex_lock(&cache_lock);
324   for (int i = 0; i < keys_len; i++) {
325     key = NULL;
326     ce = NULL;
327
328     status = c_avl_remove(cache_tree, keys[i], (void *)&key, (void *)&ce);
329     if (status != 0) {
330       ERROR("uc_check_timeout: c_avl_remove (\"%s\") failed.", keys[i]);
331       sfree(keys[i]);
332       continue;
333     }
334
335     sfree(keys[i]);
336     sfree(key);
337     cache_free(ce);
338   } /* for (i = 0; i < keys_len; i++) */
339   pthread_mutex_unlock(&cache_lock);
340
341   sfree(keys);
342   sfree(keys_time);
343   sfree(keys_interval);
344
345   return (0);
346 } /* int uc_check_timeout */
347
348 int uc_update(const data_set_t *ds, const value_list_t *vl) {
349   char name[6 * DATA_MAX_NAME_LEN];
350   cache_entry_t *ce = NULL;
351   int status;
352
353   if (FORMAT_VL(name, sizeof(name), vl) != 0) {
354     ERROR("uc_update: FORMAT_VL failed.");
355     return (-1);
356   }
357
358   pthread_mutex_lock(&cache_lock);
359
360   status = c_avl_get(cache_tree, name, (void *)&ce);
361   if (status != 0) /* entry does not yet exist */
362   {
363     status = uc_insert(ds, vl, name);
364     pthread_mutex_unlock(&cache_lock);
365     return (status);
366   }
367
368   assert(ce != NULL);
369   assert(ce->values_num == ds->ds_num);
370
371   if (ce->last_time >= vl->time) {
372     pthread_mutex_unlock(&cache_lock);
373     NOTICE("uc_update: Value too old: name = %s; value time = %.3f; "
374            "last cache update = %.3f;",
375            name, CDTIME_T_TO_DOUBLE(vl->time),
376            CDTIME_T_TO_DOUBLE(ce->last_time));
377     return (-1);
378   }
379
380   for (size_t i = 0; i < ds->ds_num; i++) {
381     switch (ds->ds[i].type) {
382     case DS_TYPE_COUNTER: {
383       counter_t diff =
384           counter_diff(ce->values_raw[i].counter, vl->values[i].counter);
385       ce->values_gauge[i] =
386           ((double)diff) / (CDTIME_T_TO_DOUBLE(vl->time - ce->last_time));
387       ce->values_raw[i].counter = vl->values[i].counter;
388     } break;
389
390     case DS_TYPE_GAUGE:
391       ce->values_raw[i].gauge = vl->values[i].gauge;
392       ce->values_gauge[i] = vl->values[i].gauge;
393       break;
394
395     case DS_TYPE_DERIVE: {
396       derive_t diff = vl->values[i].derive - ce->values_raw[i].derive;
397
398       ce->values_gauge[i] =
399           ((double)diff) / (CDTIME_T_TO_DOUBLE(vl->time - ce->last_time));
400       ce->values_raw[i].derive = vl->values[i].derive;
401     } break;
402
403     case DS_TYPE_ABSOLUTE:
404       ce->values_gauge[i] = ((double)vl->values[i].absolute) /
405                             (CDTIME_T_TO_DOUBLE(vl->time - ce->last_time));
406       ce->values_raw[i].absolute = vl->values[i].absolute;
407       break;
408
409     default:
410       /* This shouldn't happen. */
411       pthread_mutex_unlock(&cache_lock);
412       ERROR("uc_update: Don't know how to handle data source type %i.",
413             ds->ds[i].type);
414       return (-1);
415     } /* switch (ds->ds[i].type) */
416
417     DEBUG("uc_update: %s: ds[%zu] = %lf", name, i, ce->values_gauge[i]);
418   } /* for (i) */
419
420   /* Update the history if it exists. */
421   if (ce->history != NULL) {
422     assert(ce->history_index < ce->history_length);
423     for (size_t i = 0; i < ce->values_num; i++) {
424       size_t hist_idx = (ce->values_num * ce->history_index) + i;
425       ce->history[hist_idx] = ce->values_gauge[i];
426     }
427
428     assert(ce->history_length > 0);
429     ce->history_index = (ce->history_index + 1) % ce->history_length;
430   }
431
432   /* Prune invalid gauge data */
433   uc_check_range(ds, ce);
434
435   ce->last_time = vl->time;
436   ce->last_update = cdtime();
437   ce->interval = vl->interval;
438
439   pthread_mutex_unlock(&cache_lock);
440
441   return (0);
442 } /* int uc_update */
443
444 int uc_get_rate_by_name(const char *name, gauge_t **ret_values,
445                         size_t *ret_values_num) {
446   gauge_t *ret = NULL;
447   size_t ret_num = 0;
448   cache_entry_t *ce = NULL;
449   int status = 0;
450
451   pthread_mutex_lock(&cache_lock);
452
453   if (c_avl_get(cache_tree, name, (void *)&ce) == 0) {
454     assert(ce != NULL);
455
456     /* remove missing values from getval */
457     if (ce->state == STATE_MISSING) {
458       status = -1;
459     } else {
460       ret_num = ce->values_num;
461       ret = malloc(ret_num * sizeof(*ret));
462       if (ret == NULL) {
463         ERROR("utils_cache: uc_get_rate_by_name: malloc failed.");
464         status = -1;
465       } else {
466         memcpy(ret, ce->values_gauge, ret_num * sizeof(gauge_t));
467       }
468     }
469   } else {
470     DEBUG("utils_cache: uc_get_rate_by_name: No such value: %s", name);
471     status = -1;
472   }
473
474   pthread_mutex_unlock(&cache_lock);
475
476   if (status == 0) {
477     *ret_values = ret;
478     *ret_values_num = ret_num;
479   }
480
481   return (status);
482 } /* gauge_t *uc_get_rate_by_name */
483
484 gauge_t *uc_get_rate(const data_set_t *ds, const value_list_t *vl) {
485   char name[6 * DATA_MAX_NAME_LEN];
486   gauge_t *ret = NULL;
487   size_t ret_num = 0;
488   int status;
489
490   if (FORMAT_VL(name, sizeof(name), vl) != 0) {
491     ERROR("utils_cache: uc_get_rate: FORMAT_VL failed.");
492     return (NULL);
493   }
494
495   status = uc_get_rate_by_name(name, &ret, &ret_num);
496   if (status != 0)
497     return (NULL);
498
499   /* This is important - the caller has no other way of knowing how many
500    * values are returned. */
501   if (ret_num != (size_t)ds->ds_num) {
502     ERROR("utils_cache: uc_get_rate: ds[%s] has %zu values, "
503           "but uc_get_rate_by_name returned %zu.",
504           ds->type, ds->ds_num, ret_num);
505     sfree(ret);
506     return (NULL);
507   }
508
509   return (ret);
510 } /* gauge_t *uc_get_rate */
511
512 size_t uc_get_size(void) {
513   size_t size_arrays = 0;
514
515   pthread_mutex_lock(&cache_lock);
516   size_arrays = (size_t)c_avl_size(cache_tree);
517   pthread_mutex_unlock(&cache_lock);
518
519   return (size_arrays);
520 }
521
522 int uc_get_names(char ***ret_names, cdtime_t **ret_times, size_t *ret_number) {
523   c_avl_iterator_t *iter;
524   char *key;
525   cache_entry_t *value;
526
527   char **names = NULL;
528   cdtime_t *times = NULL;
529   size_t number = 0;
530   size_t size_arrays = 0;
531
532   int status = 0;
533
534   if ((ret_names == NULL) || (ret_number == NULL))
535     return (-1);
536
537   pthread_mutex_lock(&cache_lock);
538
539   size_arrays = (size_t)c_avl_size(cache_tree);
540   if (size_arrays < 1) {
541     /* Handle the "no values" case here, to avoid the error message when
542      * calloc() returns NULL. */
543     pthread_mutex_unlock(&cache_lock);
544     return (0);
545   }
546
547   names = calloc(size_arrays, sizeof(*names));
548   times = calloc(size_arrays, sizeof(*times));
549   if ((names == NULL) || (times == NULL)) {
550     ERROR("uc_get_names: calloc failed.");
551     sfree(names);
552     sfree(times);
553     pthread_mutex_unlock(&cache_lock);
554     return (ENOMEM);
555   }
556
557   iter = c_avl_get_iterator(cache_tree);
558   while (c_avl_iterator_next(iter, (void *)&key, (void *)&value) == 0) {
559     /* remove missing values when list values */
560     if (value->state == STATE_MISSING)
561       continue;
562
563     /* c_avl_size does not return a number smaller than the number of elements
564      * returned by c_avl_iterator_next. */
565     assert(number < size_arrays);
566
567     if (ret_times != NULL)
568       times[number] = value->last_time;
569
570     names[number] = strdup(key);
571     if (names[number] == NULL) {
572       status = -1;
573       break;
574     }
575
576     number++;
577   } /* while (c_avl_iterator_next) */
578
579   c_avl_iterator_destroy(iter);
580   pthread_mutex_unlock(&cache_lock);
581
582   if (status != 0) {
583     for (size_t i = 0; i < number; i++) {
584       sfree(names[i]);
585     }
586     sfree(names);
587     sfree(times);
588
589     return (-1);
590   }
591
592   *ret_names = names;
593   if (ret_times != NULL)
594     *ret_times = times;
595   else
596     sfree(times);
597   *ret_number = number;
598
599   return (0);
600 } /* int uc_get_names */
601
602 int uc_get_state(const data_set_t *ds, const value_list_t *vl) {
603   char name[6 * DATA_MAX_NAME_LEN];
604   cache_entry_t *ce = NULL;
605   int ret = STATE_ERROR;
606
607   if (FORMAT_VL(name, sizeof(name), vl) != 0) {
608     ERROR("uc_get_state: FORMAT_VL failed.");
609     return (STATE_ERROR);
610   }
611
612   pthread_mutex_lock(&cache_lock);
613
614   if (c_avl_get(cache_tree, name, (void *)&ce) == 0) {
615     assert(ce != NULL);
616     ret = ce->state;
617   }
618
619   pthread_mutex_unlock(&cache_lock);
620
621   return (ret);
622 } /* int uc_get_state */
623
624 int uc_set_state(const data_set_t *ds, const value_list_t *vl, int state) {
625   char name[6 * DATA_MAX_NAME_LEN];
626   cache_entry_t *ce = NULL;
627   int ret = -1;
628
629   if (FORMAT_VL(name, sizeof(name), vl) != 0) {
630     ERROR("uc_set_state: FORMAT_VL failed.");
631     return (STATE_ERROR);
632   }
633
634   pthread_mutex_lock(&cache_lock);
635
636   if (c_avl_get(cache_tree, name, (void *)&ce) == 0) {
637     assert(ce != NULL);
638     ret = ce->state;
639     ce->state = state;
640   }
641
642   pthread_mutex_unlock(&cache_lock);
643
644   return (ret);
645 } /* int uc_set_state */
646
647 int uc_get_history_by_name(const char *name, gauge_t *ret_history,
648                            size_t num_steps, size_t num_ds) {
649   cache_entry_t *ce = NULL;
650   int status = 0;
651
652   pthread_mutex_lock(&cache_lock);
653
654   status = c_avl_get(cache_tree, name, (void *)&ce);
655   if (status != 0) {
656     pthread_mutex_unlock(&cache_lock);
657     return (-ENOENT);
658   }
659
660   if (((size_t)ce->values_num) != num_ds) {
661     pthread_mutex_unlock(&cache_lock);
662     return (-EINVAL);
663   }
664
665   /* Check if there are enough values available. If not, increase the buffer
666    * size. */
667   if (ce->history_length < num_steps) {
668     gauge_t *tmp;
669
670     tmp =
671         realloc(ce->history, sizeof(*ce->history) * num_steps * ce->values_num);
672     if (tmp == NULL) {
673       pthread_mutex_unlock(&cache_lock);
674       return (-ENOMEM);
675     }
676
677     for (size_t i = ce->history_length * ce->values_num;
678          i < (num_steps * ce->values_num); i++)
679       tmp[i] = NAN;
680
681     ce->history = tmp;
682     ce->history_length = num_steps;
683   } /* if (ce->history_length < num_steps) */
684
685   /* Copy the values to the output buffer. */
686   for (size_t i = 0; i < num_steps; i++) {
687     size_t src_index;
688     size_t dst_index;
689
690     if (i < ce->history_index)
691       src_index = ce->history_index - (i + 1);
692     else
693       src_index = ce->history_length + ce->history_index - (i + 1);
694     src_index = src_index * num_ds;
695
696     dst_index = i * num_ds;
697
698     memcpy(ret_history + dst_index, ce->history + src_index,
699            sizeof(*ret_history) * num_ds);
700   }
701
702   pthread_mutex_unlock(&cache_lock);
703
704   return (0);
705 } /* int uc_get_history_by_name */
706
707 int uc_get_history(const data_set_t *ds, const value_list_t *vl,
708                    gauge_t *ret_history, size_t num_steps, size_t num_ds) {
709   char name[6 * DATA_MAX_NAME_LEN];
710
711   if (FORMAT_VL(name, sizeof(name), vl) != 0) {
712     ERROR("utils_cache: uc_get_history: FORMAT_VL failed.");
713     return (-1);
714   }
715
716   return (uc_get_history_by_name(name, ret_history, num_steps, num_ds));
717 } /* int uc_get_history */
718
719 int uc_get_hits(const data_set_t *ds, const value_list_t *vl) {
720   char name[6 * DATA_MAX_NAME_LEN];
721   cache_entry_t *ce = NULL;
722   int ret = STATE_ERROR;
723
724   if (FORMAT_VL(name, sizeof(name), vl) != 0) {
725     ERROR("uc_get_hits: FORMAT_VL failed.");
726     return (STATE_ERROR);
727   }
728
729   pthread_mutex_lock(&cache_lock);
730
731   if (c_avl_get(cache_tree, name, (void *)&ce) == 0) {
732     assert(ce != NULL);
733     ret = ce->hits;
734   }
735
736   pthread_mutex_unlock(&cache_lock);
737
738   return (ret);
739 } /* int uc_get_hits */
740
741 int uc_set_hits(const data_set_t *ds, const value_list_t *vl, int hits) {
742   char name[6 * DATA_MAX_NAME_LEN];
743   cache_entry_t *ce = NULL;
744   int ret = -1;
745
746   if (FORMAT_VL(name, sizeof(name), vl) != 0) {
747     ERROR("uc_set_hits: FORMAT_VL failed.");
748     return (STATE_ERROR);
749   }
750
751   pthread_mutex_lock(&cache_lock);
752
753   if (c_avl_get(cache_tree, name, (void *)&ce) == 0) {
754     assert(ce != NULL);
755     ret = ce->hits;
756     ce->hits = hits;
757   }
758
759   pthread_mutex_unlock(&cache_lock);
760
761   return (ret);
762 } /* int uc_set_hits */
763
764 int uc_inc_hits(const data_set_t *ds, const value_list_t *vl, int step) {
765   char name[6 * DATA_MAX_NAME_LEN];
766   cache_entry_t *ce = NULL;
767   int ret = -1;
768
769   if (FORMAT_VL(name, sizeof(name), vl) != 0) {
770     ERROR("uc_inc_hits: FORMAT_VL failed.");
771     return (STATE_ERROR);
772   }
773
774   pthread_mutex_lock(&cache_lock);
775
776   if (c_avl_get(cache_tree, name, (void *)&ce) == 0) {
777     assert(ce != NULL);
778     ret = ce->hits;
779     ce->hits = ret + step;
780   }
781
782   pthread_mutex_unlock(&cache_lock);
783
784   return (ret);
785 } /* int uc_inc_hits */
786
787 /*
788  * Iterator interface
789  */
790 uc_iter_t *uc_get_iterator(void) {
791   uc_iter_t *iter;
792
793   iter = (uc_iter_t *)calloc(1, sizeof(*iter));
794   if (iter == NULL)
795     return (NULL);
796
797   pthread_mutex_lock(&cache_lock);
798
799   iter->iter = c_avl_get_iterator(cache_tree);
800   if (iter->iter == NULL) {
801     free(iter);
802     return (NULL);
803   }
804
805   return (iter);
806 } /* uc_iter_t *uc_get_iterator */
807
808 int uc_iterator_next(uc_iter_t *iter, char **ret_name) {
809   int status;
810
811   if (iter == NULL)
812     return (-1);
813
814   while ((status = c_avl_iterator_next(iter->iter, (void *)&iter->name,
815                                        (void *)&iter->entry)) == 0) {
816     if (iter->entry->state == STATE_MISSING)
817       continue;
818
819     break;
820   }
821   if (status != 0) {
822     iter->name = NULL;
823     iter->entry = NULL;
824     return (-1);
825   }
826
827   if (ret_name != NULL)
828     *ret_name = iter->name;
829
830   return (0);
831 } /* int uc_iterator_next */
832
833 void uc_iterator_destroy(uc_iter_t *iter) {
834   if (iter == NULL)
835     return;
836
837   c_avl_iterator_destroy(iter->iter);
838   pthread_mutex_unlock(&cache_lock);
839
840   free(iter);
841 } /* void uc_iterator_destroy */
842
843 int uc_iterator_get_time(uc_iter_t *iter, cdtime_t *ret_time) {
844   if ((iter == NULL) || (iter->entry == NULL) || (ret_time == NULL))
845     return (-1);
846
847   *ret_time = iter->entry->last_time;
848   return (0);
849 } /* int uc_iterator_get_name */
850
851 int uc_iterator_get_values(uc_iter_t *iter, value_t **ret_values,
852                            size_t *ret_num) {
853   if ((iter == NULL) || (iter->entry == NULL) || (ret_values == NULL) ||
854       (ret_num == NULL))
855     return (-1);
856
857   *ret_values =
858       calloc(iter->entry->values_num, sizeof(*iter->entry->values_raw));
859   if (*ret_values == NULL)
860     return (-1);
861   for (size_t i = 0; i < iter->entry->values_num; ++i)
862     *ret_values[i] = iter->entry->values_raw[i];
863
864   *ret_num = iter->entry->values_num;
865
866   return (0);
867 } /* int uc_iterator_get_values */
868
869 int uc_iterator_get_interval(uc_iter_t *iter, cdtime_t *ret_interval) {
870   if ((iter == NULL) || (iter->entry == NULL) || (ret_interval == NULL))
871     return (-1);
872
873   *ret_interval = iter->entry->interval;
874   return (0);
875 } /* int uc_iterator_get_name */
876
877 /*
878  * Meta data interface
879  */
880 /* XXX: This function will acquire `cache_lock' but will not free it! */
881 static meta_data_t *uc_get_meta(const value_list_t *vl) /* {{{ */
882 {
883   char name[6 * DATA_MAX_NAME_LEN];
884   cache_entry_t *ce = NULL;
885   int status;
886
887   status = FORMAT_VL(name, sizeof(name), vl);
888   if (status != 0) {
889     ERROR("utils_cache: uc_get_meta: FORMAT_VL failed.");
890     return (NULL);
891   }
892
893   pthread_mutex_lock(&cache_lock);
894
895   status = c_avl_get(cache_tree, name, (void *)&ce);
896   if (status != 0) {
897     pthread_mutex_unlock(&cache_lock);
898     return (NULL);
899   }
900   assert(ce != NULL);
901
902   if (ce->meta == NULL)
903     ce->meta = meta_data_create();
904
905   if (ce->meta == NULL)
906     pthread_mutex_unlock(&cache_lock);
907
908   return (ce->meta);
909 } /* }}} meta_data_t *uc_get_meta */
910
911 /* Sorry about this preprocessor magic, but it really makes this file much
912  * shorter.. */
913 #define UC_WRAP(wrap_function)                                                 \
914   {                                                                            \
915     meta_data_t *meta;                                                         \
916     int status;                                                                \
917     meta = uc_get_meta(vl);                                                    \
918     if (meta == NULL)                                                          \
919       return (-1);                                                             \
920     status = wrap_function(meta, key);                                         \
921     pthread_mutex_unlock(&cache_lock);                                         \
922     return (status);                                                           \
923   }
924 int uc_meta_data_exists(const value_list_t *vl,
925                         const char *key) UC_WRAP(meta_data_exists)
926
927     int uc_meta_data_delete(const value_list_t *vl,
928                             const char *key) UC_WRAP(meta_data_delete)
929 #undef UC_WRAP
930
931 /* We need a new version of this macro because the following functions take
932  * two argumetns. */
933 #define UC_WRAP(wrap_function)                                                 \
934   {                                                                            \
935     meta_data_t *meta;                                                         \
936     int status;                                                                \
937     meta = uc_get_meta(vl);                                                    \
938     if (meta == NULL)                                                          \
939       return (-1);                                                             \
940     status = wrap_function(meta, key, value);                                  \
941     pthread_mutex_unlock(&cache_lock);                                         \
942     return (status);                                                           \
943   }
944         int uc_meta_data_add_string(const value_list_t *vl, const char *key,
945                                     const char *value)
946             UC_WRAP(meta_data_add_string) int uc_meta_data_add_signed_int(
947                 const value_list_t *vl, const char *key, int64_t value)
948                 UC_WRAP(meta_data_add_signed_int) int uc_meta_data_add_unsigned_int(
949                     const value_list_t *vl, const char *key, uint64_t value)
950                     UC_WRAP(meta_data_add_unsigned_int) int uc_meta_data_add_double(
951                         const value_list_t *vl, const char *key, double value)
952                         UC_WRAP(meta_data_add_double) int uc_meta_data_add_boolean(
953                             const value_list_t *vl, const char *key,
954                             _Bool value) UC_WRAP(meta_data_add_boolean)
955
956                             int uc_meta_data_get_string(const value_list_t *vl,
957                                                         const char *key,
958                                                         char **value)
959                                 UC_WRAP(meta_data_get_string) int uc_meta_data_get_signed_int(
960                                     const value_list_t *vl, const char *key,
961                                     int64_t *value)
962                                     UC_WRAP(meta_data_get_signed_int) int uc_meta_data_get_unsigned_int(
963                                         const value_list_t *vl, const char *key,
964                                         uint64_t *value)
965                                         UC_WRAP(meta_data_get_unsigned_int) int uc_meta_data_get_double(
966                                             const value_list_t *vl,
967                                             const char *key, double *value)
968                                             UC_WRAP(meta_data_get_double) int uc_meta_data_get_boolean(
969                                                 const value_list_t *vl,
970                                                 const char *key, _Bool *value)
971                                                 UC_WRAP(meta_data_get_boolean)
972 #undef UC_WRAP
973
974     /* vim: set sw=2 ts=8 sts=2 tw=78 : */