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