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