Treewide: remove vim modelines from C code files
[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 = cdtime();
226
227   struct {
228     char *key;
229     cdtime_t time;
230     cdtime_t interval;
231   } *expired = NULL;
232   size_t expired_num = 0;
233
234   pthread_mutex_lock(&cache_lock);
235
236   /* Build a list of entries to be flushed */
237   c_avl_iterator_t *iter = c_avl_get_iterator(cache_tree);
238   char *key = NULL;
239   cache_entry_t *ce = NULL;
240   while (c_avl_iterator_next(iter, (void *)&key, (void *)&ce) == 0) {
241     /* If the entry is fresh enough, continue. */
242     if ((now - ce->last_update) < (ce->interval * timeout_g))
243       continue;
244
245     void *tmp = realloc(expired, (expired_num + 1) * sizeof(*expired));
246     if (tmp == NULL) {
247       ERROR("uc_check_timeout: realloc failed.");
248       continue;
249     }
250     expired = tmp;
251
252     expired[expired_num].key = strdup(key);
253     expired[expired_num].time = ce->last_time;
254     expired[expired_num].interval = ce->interval;
255
256     if (expired[expired_num].key == NULL) {
257       ERROR("uc_check_timeout: strdup failed.");
258       continue;
259     }
260
261     expired_num++;
262   } /* while (c_avl_iterator_next) */
263
264   c_avl_iterator_destroy(iter);
265   pthread_mutex_unlock(&cache_lock);
266
267   if (expired_num == 0) {
268     sfree(expired);
269     return (0);
270   }
271
272   /* Call the "missing" callback for each value. Do this before removing the
273    * value from the cache, so that callbacks can still access the data stored,
274    * including plugin specific meta data, rates, history, …. This must be done
275    * without holding the lock, otherwise we will run into a deadlock if a
276    * plugin calls the cache interface. */
277   for (size_t i = 0; i < expired_num; i++) {
278     value_list_t vl = {
279         .time = expired[i].time, .interval = expired[i].interval,
280     };
281
282     if (parse_identifier_vl(expired[i].key, &vl) != 0) {
283       ERROR("uc_check_timeout: parse_identifier_vl (\"%s\") failed.",
284             expired[i].key);
285       continue;
286     }
287
288     plugin_dispatch_missing(&vl);
289   } /* for (i = 0; i < expired_num; i++) */
290
291   /* Now actually remove all the values from the cache. We don't re-evaluate
292    * the timestamp again, so in theory it is possible we remove a value after
293    * it is updated here. */
294   pthread_mutex_lock(&cache_lock);
295   for (size_t i = 0; i < expired_num; i++) {
296     char *key = NULL;
297     cache_entry_t *value = NULL;
298
299     if (c_avl_remove(cache_tree, expired[i].key, (void *)&key,
300                      (void *)&value) != 0) {
301       ERROR("uc_check_timeout: c_avl_remove (\"%s\") failed.", expired[i].key);
302       sfree(expired[i].key);
303       continue;
304     }
305     sfree(key);
306     cache_free(value);
307
308     sfree(expired[i].key);
309   } /* for (i = 0; i < expired_num; i++) */
310   pthread_mutex_unlock(&cache_lock);
311
312   sfree(expired);
313   return (0);
314 } /* int uc_check_timeout */
315
316 int uc_update(const data_set_t *ds, const value_list_t *vl) {
317   char name[6 * DATA_MAX_NAME_LEN];
318   cache_entry_t *ce = NULL;
319   int status;
320
321   if (FORMAT_VL(name, sizeof(name), vl) != 0) {
322     ERROR("uc_update: FORMAT_VL failed.");
323     return (-1);
324   }
325
326   pthread_mutex_lock(&cache_lock);
327
328   status = c_avl_get(cache_tree, name, (void *)&ce);
329   if (status != 0) /* entry does not yet exist */
330   {
331     status = uc_insert(ds, vl, name);
332     pthread_mutex_unlock(&cache_lock);
333     return (status);
334   }
335
336   assert(ce != NULL);
337   assert(ce->values_num == ds->ds_num);
338
339   if (ce->last_time >= vl->time) {
340     pthread_mutex_unlock(&cache_lock);
341     NOTICE("uc_update: Value too old: name = %s; value time = %.3f; "
342            "last cache update = %.3f;",
343            name, CDTIME_T_TO_DOUBLE(vl->time),
344            CDTIME_T_TO_DOUBLE(ce->last_time));
345     return (-1);
346   }
347
348   for (size_t i = 0; i < ds->ds_num; i++) {
349     switch (ds->ds[i].type) {
350     case DS_TYPE_COUNTER: {
351       counter_t diff =
352           counter_diff(ce->values_raw[i].counter, vl->values[i].counter);
353       ce->values_gauge[i] =
354           ((double)diff) / (CDTIME_T_TO_DOUBLE(vl->time - ce->last_time));
355       ce->values_raw[i].counter = vl->values[i].counter;
356     } break;
357
358     case DS_TYPE_GAUGE:
359       ce->values_raw[i].gauge = vl->values[i].gauge;
360       ce->values_gauge[i] = vl->values[i].gauge;
361       break;
362
363     case DS_TYPE_DERIVE: {
364       derive_t diff = vl->values[i].derive - ce->values_raw[i].derive;
365
366       ce->values_gauge[i] =
367           ((double)diff) / (CDTIME_T_TO_DOUBLE(vl->time - ce->last_time));
368       ce->values_raw[i].derive = vl->values[i].derive;
369     } break;
370
371     case DS_TYPE_ABSOLUTE:
372       ce->values_gauge[i] = ((double)vl->values[i].absolute) /
373                             (CDTIME_T_TO_DOUBLE(vl->time - ce->last_time));
374       ce->values_raw[i].absolute = vl->values[i].absolute;
375       break;
376
377     default:
378       /* This shouldn't happen. */
379       pthread_mutex_unlock(&cache_lock);
380       ERROR("uc_update: Don't know how to handle data source type %i.",
381             ds->ds[i].type);
382       return (-1);
383     } /* switch (ds->ds[i].type) */
384
385     DEBUG("uc_update: %s: ds[%zu] = %lf", name, i, ce->values_gauge[i]);
386   } /* for (i) */
387
388   /* Update the history if it exists. */
389   if (ce->history != NULL) {
390     assert(ce->history_index < ce->history_length);
391     for (size_t i = 0; i < ce->values_num; i++) {
392       size_t hist_idx = (ce->values_num * ce->history_index) + i;
393       ce->history[hist_idx] = ce->values_gauge[i];
394     }
395
396     assert(ce->history_length > 0);
397     ce->history_index = (ce->history_index + 1) % ce->history_length;
398   }
399
400   /* Prune invalid gauge data */
401   uc_check_range(ds, ce);
402
403   ce->last_time = vl->time;
404   ce->last_update = cdtime();
405   ce->interval = vl->interval;
406
407   pthread_mutex_unlock(&cache_lock);
408
409   return (0);
410 } /* int uc_update */
411
412 int uc_get_rate_by_name(const char *name, gauge_t **ret_values,
413                         size_t *ret_values_num) {
414   gauge_t *ret = NULL;
415   size_t ret_num = 0;
416   cache_entry_t *ce = NULL;
417   int status = 0;
418
419   pthread_mutex_lock(&cache_lock);
420
421   if (c_avl_get(cache_tree, name, (void *)&ce) == 0) {
422     assert(ce != NULL);
423
424     /* remove missing values from getval */
425     if (ce->state == STATE_MISSING) {
426       status = -1;
427     } else {
428       ret_num = ce->values_num;
429       ret = malloc(ret_num * sizeof(*ret));
430       if (ret == NULL) {
431         ERROR("utils_cache: uc_get_rate_by_name: malloc failed.");
432         status = -1;
433       } else {
434         memcpy(ret, ce->values_gauge, ret_num * sizeof(gauge_t));
435       }
436     }
437   } else {
438     DEBUG("utils_cache: uc_get_rate_by_name: No such value: %s", name);
439     status = -1;
440   }
441
442   pthread_mutex_unlock(&cache_lock);
443
444   if (status == 0) {
445     *ret_values = ret;
446     *ret_values_num = ret_num;
447   }
448
449   return (status);
450 } /* gauge_t *uc_get_rate_by_name */
451
452 gauge_t *uc_get_rate(const data_set_t *ds, const value_list_t *vl) {
453   char name[6 * DATA_MAX_NAME_LEN];
454   gauge_t *ret = NULL;
455   size_t ret_num = 0;
456   int status;
457
458   if (FORMAT_VL(name, sizeof(name), vl) != 0) {
459     ERROR("utils_cache: uc_get_rate: FORMAT_VL failed.");
460     return (NULL);
461   }
462
463   status = uc_get_rate_by_name(name, &ret, &ret_num);
464   if (status != 0)
465     return (NULL);
466
467   /* This is important - the caller has no other way of knowing how many
468    * values are returned. */
469   if (ret_num != (size_t)ds->ds_num) {
470     ERROR("utils_cache: uc_get_rate: ds[%s] has %zu values, "
471           "but uc_get_rate_by_name returned %zu.",
472           ds->type, ds->ds_num, ret_num);
473     sfree(ret);
474     return (NULL);
475   }
476
477   return (ret);
478 } /* gauge_t *uc_get_rate */
479
480 size_t uc_get_size(void) {
481   size_t size_arrays = 0;
482
483   pthread_mutex_lock(&cache_lock);
484   size_arrays = (size_t)c_avl_size(cache_tree);
485   pthread_mutex_unlock(&cache_lock);
486
487   return (size_arrays);
488 }
489
490 int uc_get_names(char ***ret_names, cdtime_t **ret_times, size_t *ret_number) {
491   c_avl_iterator_t *iter;
492   char *key;
493   cache_entry_t *value;
494
495   char **names = NULL;
496   cdtime_t *times = NULL;
497   size_t number = 0;
498   size_t size_arrays = 0;
499
500   int status = 0;
501
502   if ((ret_names == NULL) || (ret_number == NULL))
503     return (-1);
504
505   pthread_mutex_lock(&cache_lock);
506
507   size_arrays = (size_t)c_avl_size(cache_tree);
508   if (size_arrays < 1) {
509     /* Handle the "no values" case here, to avoid the error message when
510      * calloc() returns NULL. */
511     pthread_mutex_unlock(&cache_lock);
512     return (0);
513   }
514
515   names = calloc(size_arrays, sizeof(*names));
516   times = calloc(size_arrays, sizeof(*times));
517   if ((names == NULL) || (times == NULL)) {
518     ERROR("uc_get_names: calloc failed.");
519     sfree(names);
520     sfree(times);
521     pthread_mutex_unlock(&cache_lock);
522     return (ENOMEM);
523   }
524
525   iter = c_avl_get_iterator(cache_tree);
526   while (c_avl_iterator_next(iter, (void *)&key, (void *)&value) == 0) {
527     /* remove missing values when list values */
528     if (value->state == STATE_MISSING)
529       continue;
530
531     /* c_avl_size does not return a number smaller than the number of elements
532      * returned by c_avl_iterator_next. */
533     assert(number < size_arrays);
534
535     if (ret_times != NULL)
536       times[number] = value->last_time;
537
538     names[number] = strdup(key);
539     if (names[number] == NULL) {
540       status = -1;
541       break;
542     }
543
544     number++;
545   } /* while (c_avl_iterator_next) */
546
547   c_avl_iterator_destroy(iter);
548   pthread_mutex_unlock(&cache_lock);
549
550   if (status != 0) {
551     for (size_t i = 0; i < number; i++) {
552       sfree(names[i]);
553     }
554     sfree(names);
555     sfree(times);
556
557     return (-1);
558   }
559
560   *ret_names = names;
561   if (ret_times != NULL)
562     *ret_times = times;
563   else
564     sfree(times);
565   *ret_number = number;
566
567   return (0);
568 } /* int uc_get_names */
569
570 int uc_get_state(const data_set_t *ds, const value_list_t *vl) {
571   char name[6 * DATA_MAX_NAME_LEN];
572   cache_entry_t *ce = NULL;
573   int ret = STATE_ERROR;
574
575   if (FORMAT_VL(name, sizeof(name), vl) != 0) {
576     ERROR("uc_get_state: FORMAT_VL failed.");
577     return (STATE_ERROR);
578   }
579
580   pthread_mutex_lock(&cache_lock);
581
582   if (c_avl_get(cache_tree, name, (void *)&ce) == 0) {
583     assert(ce != NULL);
584     ret = ce->state;
585   }
586
587   pthread_mutex_unlock(&cache_lock);
588
589   return (ret);
590 } /* int uc_get_state */
591
592 int uc_set_state(const data_set_t *ds, const value_list_t *vl, int state) {
593   char name[6 * DATA_MAX_NAME_LEN];
594   cache_entry_t *ce = NULL;
595   int ret = -1;
596
597   if (FORMAT_VL(name, sizeof(name), vl) != 0) {
598     ERROR("uc_set_state: FORMAT_VL failed.");
599     return (STATE_ERROR);
600   }
601
602   pthread_mutex_lock(&cache_lock);
603
604   if (c_avl_get(cache_tree, name, (void *)&ce) == 0) {
605     assert(ce != NULL);
606     ret = ce->state;
607     ce->state = state;
608   }
609
610   pthread_mutex_unlock(&cache_lock);
611
612   return (ret);
613 } /* int uc_set_state */
614
615 int uc_get_history_by_name(const char *name, gauge_t *ret_history,
616                            size_t num_steps, size_t num_ds) {
617   cache_entry_t *ce = NULL;
618   int status = 0;
619
620   pthread_mutex_lock(&cache_lock);
621
622   status = c_avl_get(cache_tree, name, (void *)&ce);
623   if (status != 0) {
624     pthread_mutex_unlock(&cache_lock);
625     return (-ENOENT);
626   }
627
628   if (((size_t)ce->values_num) != num_ds) {
629     pthread_mutex_unlock(&cache_lock);
630     return (-EINVAL);
631   }
632
633   /* Check if there are enough values available. If not, increase the buffer
634    * size. */
635   if (ce->history_length < num_steps) {
636     gauge_t *tmp;
637
638     tmp =
639         realloc(ce->history, sizeof(*ce->history) * num_steps * ce->values_num);
640     if (tmp == NULL) {
641       pthread_mutex_unlock(&cache_lock);
642       return (-ENOMEM);
643     }
644
645     for (size_t i = ce->history_length * ce->values_num;
646          i < (num_steps * ce->values_num); i++)
647       tmp[i] = NAN;
648
649     ce->history = tmp;
650     ce->history_length = num_steps;
651   } /* if (ce->history_length < num_steps) */
652
653   /* Copy the values to the output buffer. */
654   for (size_t i = 0; i < num_steps; i++) {
655     size_t src_index;
656     size_t dst_index;
657
658     if (i < ce->history_index)
659       src_index = ce->history_index - (i + 1);
660     else
661       src_index = ce->history_length + ce->history_index - (i + 1);
662     src_index = src_index * num_ds;
663
664     dst_index = i * num_ds;
665
666     memcpy(ret_history + dst_index, ce->history + src_index,
667            sizeof(*ret_history) * num_ds);
668   }
669
670   pthread_mutex_unlock(&cache_lock);
671
672   return (0);
673 } /* int uc_get_history_by_name */
674
675 int uc_get_history(const data_set_t *ds, const value_list_t *vl,
676                    gauge_t *ret_history, size_t num_steps, size_t num_ds) {
677   char name[6 * DATA_MAX_NAME_LEN];
678
679   if (FORMAT_VL(name, sizeof(name), vl) != 0) {
680     ERROR("utils_cache: uc_get_history: FORMAT_VL failed.");
681     return (-1);
682   }
683
684   return (uc_get_history_by_name(name, ret_history, num_steps, num_ds));
685 } /* int uc_get_history */
686
687 int uc_get_hits(const data_set_t *ds, const value_list_t *vl) {
688   char name[6 * DATA_MAX_NAME_LEN];
689   cache_entry_t *ce = NULL;
690   int ret = STATE_ERROR;
691
692   if (FORMAT_VL(name, sizeof(name), vl) != 0) {
693     ERROR("uc_get_hits: FORMAT_VL failed.");
694     return (STATE_ERROR);
695   }
696
697   pthread_mutex_lock(&cache_lock);
698
699   if (c_avl_get(cache_tree, name, (void *)&ce) == 0) {
700     assert(ce != NULL);
701     ret = ce->hits;
702   }
703
704   pthread_mutex_unlock(&cache_lock);
705
706   return (ret);
707 } /* int uc_get_hits */
708
709 int uc_set_hits(const data_set_t *ds, const value_list_t *vl, int hits) {
710   char name[6 * DATA_MAX_NAME_LEN];
711   cache_entry_t *ce = NULL;
712   int ret = -1;
713
714   if (FORMAT_VL(name, sizeof(name), vl) != 0) {
715     ERROR("uc_set_hits: FORMAT_VL failed.");
716     return (STATE_ERROR);
717   }
718
719   pthread_mutex_lock(&cache_lock);
720
721   if (c_avl_get(cache_tree, name, (void *)&ce) == 0) {
722     assert(ce != NULL);
723     ret = ce->hits;
724     ce->hits = hits;
725   }
726
727   pthread_mutex_unlock(&cache_lock);
728
729   return (ret);
730 } /* int uc_set_hits */
731
732 int uc_inc_hits(const data_set_t *ds, const value_list_t *vl, int step) {
733   char name[6 * DATA_MAX_NAME_LEN];
734   cache_entry_t *ce = NULL;
735   int ret = -1;
736
737   if (FORMAT_VL(name, sizeof(name), vl) != 0) {
738     ERROR("uc_inc_hits: FORMAT_VL failed.");
739     return (STATE_ERROR);
740   }
741
742   pthread_mutex_lock(&cache_lock);
743
744   if (c_avl_get(cache_tree, name, (void *)&ce) == 0) {
745     assert(ce != NULL);
746     ret = ce->hits;
747     ce->hits = ret + step;
748   }
749
750   pthread_mutex_unlock(&cache_lock);
751
752   return (ret);
753 } /* int uc_inc_hits */
754
755 /*
756  * Iterator interface
757  */
758 uc_iter_t *uc_get_iterator(void) {
759   uc_iter_t *iter;
760
761   iter = (uc_iter_t *)calloc(1, sizeof(*iter));
762   if (iter == NULL)
763     return (NULL);
764
765   pthread_mutex_lock(&cache_lock);
766
767   iter->iter = c_avl_get_iterator(cache_tree);
768   if (iter->iter == NULL) {
769     free(iter);
770     return (NULL);
771   }
772
773   return (iter);
774 } /* uc_iter_t *uc_get_iterator */
775
776 int uc_iterator_next(uc_iter_t *iter, char **ret_name) {
777   int status;
778
779   if (iter == NULL)
780     return (-1);
781
782   while ((status = c_avl_iterator_next(iter->iter, (void *)&iter->name,
783                                        (void *)&iter->entry)) == 0) {
784     if (iter->entry->state == STATE_MISSING)
785       continue;
786
787     break;
788   }
789   if (status != 0) {
790     iter->name = NULL;
791     iter->entry = NULL;
792     return (-1);
793   }
794
795   if (ret_name != NULL)
796     *ret_name = iter->name;
797
798   return (0);
799 } /* int uc_iterator_next */
800
801 void uc_iterator_destroy(uc_iter_t *iter) {
802   if (iter == NULL)
803     return;
804
805   c_avl_iterator_destroy(iter->iter);
806   pthread_mutex_unlock(&cache_lock);
807
808   free(iter);
809 } /* void uc_iterator_destroy */
810
811 int uc_iterator_get_time(uc_iter_t *iter, cdtime_t *ret_time) {
812   if ((iter == NULL) || (iter->entry == NULL) || (ret_time == NULL))
813     return (-1);
814
815   *ret_time = iter->entry->last_time;
816   return (0);
817 } /* int uc_iterator_get_name */
818
819 int uc_iterator_get_values(uc_iter_t *iter, value_t **ret_values,
820                            size_t *ret_num) {
821   if ((iter == NULL) || (iter->entry == NULL) || (ret_values == NULL) ||
822       (ret_num == NULL))
823     return (-1);
824
825   *ret_values =
826       calloc(iter->entry->values_num, sizeof(*iter->entry->values_raw));
827   if (*ret_values == NULL)
828     return (-1);
829   for (size_t i = 0; i < iter->entry->values_num; ++i)
830     *ret_values[i] = iter->entry->values_raw[i];
831
832   *ret_num = iter->entry->values_num;
833
834   return (0);
835 } /* int uc_iterator_get_values */
836
837 int uc_iterator_get_interval(uc_iter_t *iter, cdtime_t *ret_interval) {
838   if ((iter == NULL) || (iter->entry == NULL) || (ret_interval == NULL))
839     return (-1);
840
841   *ret_interval = iter->entry->interval;
842   return (0);
843 } /* int uc_iterator_get_name */
844
845 /*
846  * Meta data interface
847  */
848 /* XXX: This function will acquire `cache_lock' but will not free it! */
849 static meta_data_t *uc_get_meta(const value_list_t *vl) /* {{{ */
850 {
851   char name[6 * DATA_MAX_NAME_LEN];
852   cache_entry_t *ce = NULL;
853   int status;
854
855   status = FORMAT_VL(name, sizeof(name), vl);
856   if (status != 0) {
857     ERROR("utils_cache: uc_get_meta: FORMAT_VL failed.");
858     return (NULL);
859   }
860
861   pthread_mutex_lock(&cache_lock);
862
863   status = c_avl_get(cache_tree, name, (void *)&ce);
864   if (status != 0) {
865     pthread_mutex_unlock(&cache_lock);
866     return (NULL);
867   }
868   assert(ce != NULL);
869
870   if (ce->meta == NULL)
871     ce->meta = meta_data_create();
872
873   if (ce->meta == NULL)
874     pthread_mutex_unlock(&cache_lock);
875
876   return (ce->meta);
877 } /* }}} meta_data_t *uc_get_meta */
878
879 /* Sorry about this preprocessor magic, but it really makes this file much
880  * shorter.. */
881 #define UC_WRAP(wrap_function)                                                 \
882   {                                                                            \
883     meta_data_t *meta;                                                         \
884     int status;                                                                \
885     meta = uc_get_meta(vl);                                                    \
886     if (meta == NULL)                                                          \
887       return (-1);                                                             \
888     status = wrap_function(meta, key);                                         \
889     pthread_mutex_unlock(&cache_lock);                                         \
890     return (status);                                                           \
891   }
892 int uc_meta_data_exists(const value_list_t *vl,
893                         const char *key) UC_WRAP(meta_data_exists)
894
895     int uc_meta_data_delete(const value_list_t *vl,
896                             const char *key) UC_WRAP(meta_data_delete)
897 #undef UC_WRAP
898
899 /* We need a new version of this macro because the following functions take
900  * two argumetns. */
901 #define UC_WRAP(wrap_function)                                                 \
902   {                                                                            \
903     meta_data_t *meta;                                                         \
904     int status;                                                                \
905     meta = uc_get_meta(vl);                                                    \
906     if (meta == NULL)                                                          \
907       return (-1);                                                             \
908     status = wrap_function(meta, key, value);                                  \
909     pthread_mutex_unlock(&cache_lock);                                         \
910     return (status);                                                           \
911   }
912         int uc_meta_data_add_string(const value_list_t *vl, const char *key,
913                                     const char *value)
914             UC_WRAP(meta_data_add_string) int uc_meta_data_add_signed_int(
915                 const value_list_t *vl, const char *key, int64_t value)
916                 UC_WRAP(meta_data_add_signed_int) int uc_meta_data_add_unsigned_int(
917                     const value_list_t *vl, const char *key, uint64_t value)
918                     UC_WRAP(meta_data_add_unsigned_int) int uc_meta_data_add_double(
919                         const value_list_t *vl, const char *key, double value)
920                         UC_WRAP(meta_data_add_double) int uc_meta_data_add_boolean(
921                             const value_list_t *vl, const char *key,
922                             _Bool value) UC_WRAP(meta_data_add_boolean)
923
924                             int uc_meta_data_get_string(const value_list_t *vl,
925                                                         const char *key,
926                                                         char **value)
927                                 UC_WRAP(meta_data_get_string) int uc_meta_data_get_signed_int(
928                                     const value_list_t *vl, const char *key,
929                                     int64_t *value)
930                                     UC_WRAP(meta_data_get_signed_int) int uc_meta_data_get_unsigned_int(
931                                         const value_list_t *vl, const char *key,
932                                         uint64_t *value)
933                                         UC_WRAP(meta_data_get_unsigned_int) int uc_meta_data_get_double(
934                                             const value_list_t *vl,
935                                             const char *key, double *value)
936                                             UC_WRAP(meta_data_get_double) int uc_meta_data_get_boolean(
937                                                 const value_list_t *vl,
938                                                 const char *key, _Bool *value)
939                                                 UC_WRAP(meta_data_get_boolean)
940 #undef UC_WRAP