dc0ff0df27b33f21d5f3701c2d18648ae35a8ce6
[collectd.git] / src / utils_cache.c
1 /**
2  * collectd - src/utils_cache.c
3  * Copyright (C) 2007-2010  Florian octo Forster
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; only version 2 of the License is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  * Author:
19  *   Florian octo Forster <octo at collectd.org>
20  **/
21
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25 #include "utils_avltree.h"
26 #include "utils_cache.h"
27 #include "meta_data.h"
28
29 #include <assert.h>
30 #include <pthread.h>
31
32 typedef struct cache_entry_s
33 {
34         char name[6 * DATA_MAX_NAME_LEN];
35         int        values_num;
36         gauge_t   *values_gauge;
37         value_t   *values_raw;
38         /* Time contained in the package
39          * (for calculating rates) */
40         cdtime_t last_time;
41         /* Time according to the local clock
42          * (for purging old entries) */
43         cdtime_t last_update;
44         /* Interval in which the data is collected
45          * (for purding old entries) */
46         cdtime_t interval;
47         int state;
48         int hits;
49
50         /*
51          * +-----+-----+-----+-----+-----+-----+-----+-----+-----+----
52          * !  0  !  1  !  2  !  3  !  4  !  5  !  6  !  7  !  8  ! ...
53          * +-----+-----+-----+-----+-----+-----+-----+-----+-----+----
54          * ! ds0 ! ds1 ! ds2 ! ds0 ! ds1 ! ds2 ! ds0 ! ds1 ! ds2 ! ...
55          * +-----+-----+-----+-----+-----+-----+-----+-----+-----+----
56          * !      t = 0      !      t = 1      !      t = 2      ! ...
57          * +-----------------+-----------------+-----------------+----
58          */
59         gauge_t *history;
60         size_t   history_index; /* points to the next position to write to. */
61         size_t   history_length;
62
63         meta_data_t *meta;
64 } cache_entry_t;
65
66 static c_avl_tree_t   *cache_tree = NULL;
67 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
68
69 static int cache_compare (const cache_entry_t *a, const cache_entry_t *b)
70 {
71   assert ((a != NULL) && (b != NULL));
72   return (strcmp (a->name, b->name));
73 } /* int cache_compare */
74
75 static cache_entry_t *cache_alloc (int values_num)
76 {
77   cache_entry_t *ce;
78
79   ce = (cache_entry_t *) malloc (sizeof (cache_entry_t));
80   if (ce == NULL)
81   {
82     ERROR ("utils_cache: cache_alloc: malloc failed.");
83     return (NULL);
84   }
85   memset (ce, '\0', sizeof (cache_entry_t));
86   ce->values_num = values_num;
87
88   ce->values_gauge = calloc (values_num, sizeof (*ce->values_gauge));
89   ce->values_raw   = calloc (values_num, sizeof (*ce->values_raw));
90   if ((ce->values_gauge == NULL) || (ce->values_raw == NULL))
91   {
92     sfree (ce->values_gauge);
93     sfree (ce->values_raw);
94     sfree (ce);
95     ERROR ("utils_cache: cache_alloc: calloc failed.");
96     return (NULL);
97   }
98
99   ce->history = NULL;
100   ce->history_length = 0;
101   ce->meta = NULL;
102
103   return (ce);
104 } /* cache_entry_t *cache_alloc */
105
106 static void cache_free (cache_entry_t *ce)
107 {
108   if (ce == NULL)
109     return;
110
111   sfree (ce->values_gauge);
112   sfree (ce->values_raw);
113   sfree (ce->history);
114   if (ce->meta != NULL)
115   {
116     meta_data_destroy (ce->meta);
117     ce->meta = NULL;
118   }
119   sfree (ce);
120 } /* void cache_free */
121
122 static void uc_check_range (const data_set_t *ds, cache_entry_t *ce)
123 {
124   int i;
125
126   for (i = 0; i < ds->ds_num; i++)
127   {
128     if (isnan (ce->values_gauge[i]))
129       continue;
130     else if (ce->values_gauge[i] < ds->ds[i].min)
131       ce->values_gauge[i] = NAN;
132     else if (ce->values_gauge[i] > ds->ds[i].max)
133       ce->values_gauge[i] = NAN;
134   }
135 } /* void uc_check_range */
136
137 static int uc_insert (const data_set_t *ds, const value_list_t *vl,
138     const char *key)
139 {
140   int i;
141   char *key_copy;
142   cache_entry_t *ce;
143
144   /* `cache_lock' has been locked by `uc_update' */
145
146   key_copy = strdup (key);
147   if (key_copy == NULL)
148   {
149     ERROR ("uc_insert: strdup failed.");
150     return (-1);
151   }
152
153   ce = cache_alloc (ds->ds_num);
154   if (ce == NULL)
155   {
156     sfree (key_copy);
157     ERROR ("uc_insert: cache_alloc (%i) failed.", ds->ds_num);
158     return (-1);
159   }
160
161   sstrncpy (ce->name, key, sizeof (ce->name));
162
163   for (i = 0; i < ds->ds_num; i++)
164   {
165     switch (ds->ds[i].type)
166     {
167       case DS_TYPE_COUNTER:
168         ce->values_gauge[i] = NAN;
169         ce->values_raw[i].counter = vl->values[i].counter;
170         break;
171
172       case DS_TYPE_GAUGE:
173         ce->values_gauge[i] = vl->values[i].gauge;
174         ce->values_raw[i].gauge = vl->values[i].gauge;
175         break;
176
177       case DS_TYPE_DERIVE:
178         ce->values_gauge[i] = NAN;
179         ce->values_raw[i].derive = vl->values[i].derive;
180         break;
181
182       case DS_TYPE_ABSOLUTE:
183         ce->values_gauge[i] = NAN;
184         if (vl->interval > 0)
185           ce->values_gauge[i] = ((double) vl->values[i].absolute)
186             / CDTIME_T_TO_DOUBLE (vl->interval);
187         ce->values_raw[i].absolute = vl->values[i].absolute;
188         break;
189         
190       default:
191         /* This shouldn't happen. */
192         ERROR ("uc_insert: Don't know how to handle data source type %i.",
193             ds->ds[i].type);
194         return (-1);
195     } /* switch (ds->ds[i].type) */
196   } /* for (i) */
197
198   /* Prune invalid gauge data */
199   uc_check_range (ds, ce);
200
201   ce->last_time = vl->time;
202   ce->last_update = cdtime ();
203   ce->interval = vl->interval;
204   ce->state = STATE_OKAY;
205
206   if (c_avl_insert (cache_tree, key_copy, ce) != 0)
207   {
208     sfree (key_copy);
209     ERROR ("uc_insert: c_avl_insert failed.");
210     return (-1);
211   }
212
213   DEBUG ("uc_insert: Added %s to the cache.", key);
214   return (0);
215 } /* int uc_insert */
216
217 int uc_init (void)
218 {
219   if (cache_tree == NULL)
220     cache_tree = c_avl_create ((int (*) (const void *, const void *))
221         cache_compare);
222
223   return (0);
224 } /* int uc_init */
225
226 int uc_check_timeout (void)
227 {
228   cdtime_t now;
229   cache_entry_t *ce;
230
231   char **keys = NULL;
232   cdtime_t *keys_time = NULL;
233   cdtime_t *keys_interval = NULL;
234   int keys_len = 0;
235
236   char *key;
237   c_avl_iterator_t *iter;
238
239   int status;
240   int i;
241   
242   pthread_mutex_lock (&cache_lock);
243
244   now = cdtime ();
245
246   /* Build a list of entries to be flushed */
247   iter = c_avl_get_iterator (cache_tree);
248   while (c_avl_iterator_next (iter, (void *) &key, (void *) &ce) == 0)
249   {
250     char **tmp;
251     cdtime_t *tmp_time;
252
253     /* If the entry is fresh enough, continue. */
254     if ((now - ce->last_update) < (ce->interval * timeout_g))
255       continue;
256
257     /* If entry has not been updated, add to `keys' array */
258     tmp = (char **) realloc ((void *) keys,
259         (keys_len + 1) * sizeof (char *));
260     if (tmp == NULL)
261     {
262       ERROR ("uc_check_timeout: realloc failed.");
263       continue;
264     }
265     keys = tmp;
266
267     tmp_time = realloc (keys_time, (keys_len + 1) * sizeof (*keys_time));
268     if (tmp_time == NULL)
269     {
270       ERROR ("uc_check_timeout: realloc failed.");
271       continue;
272     }
273     keys_time = tmp_time;
274
275     tmp_time = realloc (keys_interval, (keys_len + 1) * sizeof (*keys_interval));
276     if (tmp_time == NULL)
277     {
278       ERROR ("uc_check_timeout: realloc failed.");
279       continue;
280     }
281     keys_interval = tmp_time;
282
283     keys[keys_len] = strdup (key);
284     if (keys[keys_len] == NULL)
285     {
286       ERROR ("uc_check_timeout: strdup failed.");
287       continue;
288     }
289     keys_time[keys_len] = ce->last_time;
290     keys_interval[keys_len] = ce->interval;
291
292     keys_len++;
293   } /* while (c_avl_iterator_next) */
294
295   c_avl_iterator_destroy (iter);
296   pthread_mutex_unlock (&cache_lock);
297
298   if (keys_len == 0)
299   {
300     /* realloc() may have been called for these. */
301     sfree (keys);
302     sfree (keys_time);
303     sfree (keys_interval);
304     return (0);
305   }
306
307   /* Call the "missing" callback for each value. Do this before removing the
308    * value from the cache, so that callbacks can still access the data stored,
309    * including plugin specific meta data, rates, history, â€¦. This must be done
310    * without holding the lock, otherwise we will run into a deadlock if a
311    * plugin calls the cache interface. */
312   for (i = 0; i < keys_len; i++)
313   {
314     value_list_t vl = VALUE_LIST_INIT;
315
316     vl.values = NULL;
317     vl.values_len = 0;
318     vl.meta = NULL;
319
320     status = parse_identifier_vl (keys[i], &vl);
321     if (status != 0)
322     {
323       ERROR ("uc_check_timeout: parse_identifier_vl (\"%s\") failed.", keys[i]);
324       continue;
325     }
326
327     vl.time = keys_time[i];
328     vl.interval = keys_interval[i];
329
330     plugin_dispatch_missing (&vl);
331   } /* for (i = 0; i < keys_len; i++) */
332
333   /* Now actually remove all the values from the cache. We don't re-evaluate
334    * the timestamp again, so in theory it is possible we remove a value after
335    * it is updated here. */
336   pthread_mutex_lock (&cache_lock);
337   for (i = 0; i < keys_len; i++)
338   {
339     key = NULL;
340     ce = NULL;
341
342     status = c_avl_remove (cache_tree, keys[i],
343         (void *) &key, (void *) &ce);
344     if (status != 0)
345     {
346       ERROR ("uc_check_timeout: c_avl_remove (\"%s\") failed.", keys[i]);
347       sfree (keys[i]);
348       continue;
349     }
350
351     sfree (keys[i]);
352     sfree (key);
353     cache_free (ce);
354   } /* for (i = 0; i < keys_len; i++) */
355   pthread_mutex_unlock (&cache_lock);
356
357   sfree (keys);
358   sfree (keys_time);
359   sfree (keys_interval);
360
361   return (0);
362 } /* int uc_check_timeout */
363
364 int uc_update (const data_set_t *ds, const value_list_t *vl)
365 {
366   char name[6 * DATA_MAX_NAME_LEN];
367   cache_entry_t *ce = NULL;
368   int status;
369   int i;
370
371   if (FORMAT_VL (name, sizeof (name), vl) != 0)
372   {
373     ERROR ("uc_update: FORMAT_VL failed.");
374     return (-1);
375   }
376
377   pthread_mutex_lock (&cache_lock);
378
379   status = c_avl_get (cache_tree, name, (void *) &ce);
380   if (status != 0) /* entry does not yet exist */
381   {
382     status = uc_insert (ds, vl, name);
383     pthread_mutex_unlock (&cache_lock);
384     return (status);
385   }
386
387   assert (ce != NULL);
388   assert (ce->values_num == ds->ds_num);
389
390   if (ce->last_time >= vl->time)
391   {
392     pthread_mutex_unlock (&cache_lock);
393     NOTICE ("uc_update: Value too old: name = %s; value time = %.3f; "
394         "last cache update = %.3f;",
395         name,
396         CDTIME_T_TO_DOUBLE (vl->time),
397         CDTIME_T_TO_DOUBLE (ce->last_time));
398     return (-1);
399   }
400
401   for (i = 0; i < ds->ds_num; i++)
402   {
403     switch (ds->ds[i].type)
404     {
405       case DS_TYPE_COUNTER:
406         {
407           counter_t diff;
408
409           /* check if the counter has wrapped around */
410           if (vl->values[i].counter < ce->values_raw[i].counter)
411           {
412             if (ce->values_raw[i].counter <= 4294967295U)
413               diff = (4294967295U - ce->values_raw[i].counter)
414                 + vl->values[i].counter;
415             else
416               diff = (18446744073709551615ULL - ce->values_raw[i].counter)
417                 + vl->values[i].counter;
418           }
419           else /* counter has NOT wrapped around */
420           {
421             diff = vl->values[i].counter - ce->values_raw[i].counter;
422           }
423
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;
438
439           diff = vl->values[i].derive - ce->values_raw[i].derive;
440
441           ce->values_gauge[i] = ((double) diff)
442             / (CDTIME_T_TO_DOUBLE (vl->time - ce->last_time));
443           ce->values_raw[i].derive = vl->values[i].derive;
444         }
445         break;
446
447       case DS_TYPE_ABSOLUTE:
448         ce->values_gauge[i] = ((double) vl->values[i].absolute)
449           / (CDTIME_T_TO_DOUBLE (vl->time - ce->last_time));
450         ce->values_raw[i].absolute = vl->values[i].absolute;
451         break;
452
453       default:
454         /* This shouldn't happen. */
455         pthread_mutex_unlock (&cache_lock);
456         ERROR ("uc_update: Don't know how to handle data source type %i.",
457             ds->ds[i].type);
458         return (-1);
459     } /* switch (ds->ds[i].type) */
460
461     DEBUG ("uc_update: %s: ds[%i] = %lf", name, i, ce->values_gauge[i]);
462   } /* for (i) */
463
464   /* Update the history if it exists. */
465   if (ce->history != NULL)
466   {
467     assert (ce->history_index < ce->history_length);
468     for (i = 0; i < ce->values_num; i++)
469     {
470       size_t hist_idx = (ce->values_num * ce->history_index) + i;
471       ce->history[hist_idx] = ce->values_gauge[i];
472     }
473
474     assert (ce->history_length > 0);
475     ce->history_index = (ce->history_index + 1) % ce->history_length;
476   }
477
478   /* Prune invalid gauge data */
479   uc_check_range (ds, ce);
480
481   ce->last_time = vl->time;
482   ce->last_update = cdtime ();
483   ce->interval = vl->interval;
484
485   pthread_mutex_unlock (&cache_lock);
486
487   return (0);
488 } /* int uc_update */
489
490 int uc_get_rate_by_name (const char *name, gauge_t **ret_values, size_t *ret_values_num)
491 {
492   gauge_t *ret = NULL;
493   size_t ret_num = 0;
494   cache_entry_t *ce = NULL;
495   int status = 0;
496
497   pthread_mutex_lock (&cache_lock);
498
499   if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
500   {
501     assert (ce != NULL);
502
503     /* remove missing values from getval */
504     if (ce->state == STATE_MISSING)
505     {
506       status = -1;
507     }
508     else
509     {
510       ret_num = ce->values_num;
511       ret = (gauge_t *) malloc (ret_num * sizeof (gauge_t));
512       if (ret == NULL)
513       {
514         ERROR ("utils_cache: uc_get_rate_by_name: malloc failed.");
515         status = -1;
516       }
517       else
518       {
519         memcpy (ret, ce->values_gauge, ret_num * sizeof (gauge_t));
520       }
521     }
522   }
523   else
524   {
525     DEBUG ("utils_cache: uc_get_rate_by_name: No such value: %s", name);
526     status = -1;
527   }
528
529   pthread_mutex_unlock (&cache_lock);
530
531   if (status == 0)
532   {
533     *ret_values = ret;
534     *ret_values_num = ret_num;
535   }
536
537   return (status);
538 } /* gauge_t *uc_get_rate_by_name */
539
540 gauge_t *uc_get_rate (const data_set_t *ds, const value_list_t *vl)
541 {
542   char name[6 * DATA_MAX_NAME_LEN];
543   gauge_t *ret = NULL;
544   size_t ret_num = 0;
545   int status;
546
547   if (FORMAT_VL (name, sizeof (name), vl) != 0)
548   {
549     ERROR ("utils_cache: uc_get_rate: FORMAT_VL failed.");
550     return (NULL);
551   }
552
553   status = uc_get_rate_by_name (name, &ret, &ret_num);
554   if (status != 0)
555     return (NULL);
556
557   /* This is important - the caller has no other way of knowing how many
558    * values are returned. */
559   if (ret_num != (size_t) ds->ds_num)
560   {
561     ERROR ("utils_cache: uc_get_rate: ds[%s] has %i values, "
562         "but uc_get_rate_by_name returned %zu.",
563         ds->type, ds->ds_num, ret_num);
564     sfree (ret);
565     return (NULL);
566   }
567
568   return (ret);
569 } /* gauge_t *uc_get_rate */
570
571 int uc_get_names (char ***ret_names, cdtime_t **ret_times, size_t *ret_number)
572 {
573   c_avl_iterator_t *iter;
574   char *key;
575   cache_entry_t *value;
576
577   char **names = NULL;
578   cdtime_t *times = NULL;
579   size_t number = 0;
580   size_t size_arrays = 0;
581
582   int status = 0;
583
584   if ((ret_names == NULL) || (ret_number == NULL))
585     return (-1);
586
587   pthread_mutex_lock (&cache_lock);
588
589   size_arrays = (size_t) c_avl_size (cache_tree);
590   if (size_arrays < 1)
591   {
592     /* Handle the "no values" case here, to avoid the error message when
593      * calloc() returns NULL. */
594     pthread_mutex_unlock (&cache_lock);
595     return (0);
596   }
597
598   names = calloc (size_arrays, sizeof (*names));
599   times = calloc (size_arrays, sizeof (*times));
600   if ((names == NULL) || (times == NULL))
601   {
602     ERROR ("uc_get_names: calloc failed.");
603     sfree (names);
604     sfree (times);
605     pthread_mutex_unlock (&cache_lock);
606     return (ENOMEM);
607   }
608
609   iter = c_avl_get_iterator (cache_tree);
610   while (c_avl_iterator_next (iter, (void *) &key, (void *) &value) == 0)
611   {
612     /* remove missing values when list values */
613     if (value->state == STATE_MISSING)
614       continue;
615
616     /* c_avl_size does not return a number smaller than the number of elements
617      * returned by c_avl_iterator_next. */
618     assert (number < size_arrays);
619
620     if (ret_times != NULL)
621       times[number] = value->last_time;
622
623     names[number] = strdup (key);
624     if (names[number] == NULL)
625     {
626       status = -1;
627       break;
628     }
629
630     number++;
631   } /* while (c_avl_iterator_next) */
632
633   c_avl_iterator_destroy (iter);
634   pthread_mutex_unlock (&cache_lock);
635
636   if (status != 0)
637   {
638     size_t i;
639     
640     for (i = 0; i < number; i++)
641     {
642       sfree (names[i]);
643     }
644     sfree (names);
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 : */