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