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