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