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