Merge pull request #8 from collectd/master
[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 = calloc (1, sizeof (*ce));
87   if (ce == NULL)
88   {
89     ERROR ("utils_cache: cache_alloc: calloc failed.");
90     return (NULL);
91   }
92   ce->values_num = values_num;
93
94   ce->values_gauge = calloc (values_num, sizeof (*ce->values_gauge));
95   ce->values_raw   = calloc (values_num, sizeof (*ce->values_raw));
96   if ((ce->values_gauge == NULL) || (ce->values_raw == NULL))
97   {
98     sfree (ce->values_gauge);
99     sfree (ce->values_raw);
100     sfree (ce);
101     ERROR ("utils_cache: cache_alloc: calloc failed.");
102     return (NULL);
103   }
104
105   ce->history = NULL;
106   ce->history_length = 0;
107   ce->meta = NULL;
108
109   return (ce);
110 } /* cache_entry_t *cache_alloc */
111
112 static void cache_free (cache_entry_t *ce)
113 {
114   if (ce == NULL)
115     return;
116
117   sfree (ce->values_gauge);
118   sfree (ce->values_raw);
119   sfree (ce->history);
120   if (ce->meta != NULL)
121   {
122     meta_data_destroy (ce->meta);
123     ce->meta = NULL;
124   }
125   sfree (ce);
126 } /* void cache_free */
127
128 static void uc_check_range (const data_set_t *ds, cache_entry_t *ce)
129 {
130   size_t i;
131
132   for (i = 0; i < ds->ds_num; i++)
133   {
134     if (isnan (ce->values_gauge[i]))
135       continue;
136     else if (ce->values_gauge[i] < ds->ds[i].min)
137       ce->values_gauge[i] = NAN;
138     else if (ce->values_gauge[i] > ds->ds[i].max)
139       ce->values_gauge[i] = NAN;
140   }
141 } /* void uc_check_range */
142
143 static int uc_insert (const data_set_t *ds, const value_list_t *vl,
144     const char *key)
145 {
146   char *key_copy;
147   cache_entry_t *ce;
148   size_t i;
149
150   /* `cache_lock' has been locked by `uc_update' */
151
152   key_copy = strdup (key);
153   if (key_copy == NULL)
154   {
155     ERROR ("uc_insert: strdup failed.");
156     return (-1);
157   }
158
159   ce = cache_alloc (ds->ds_num);
160   if (ce == NULL)
161   {
162     sfree (key_copy);
163     ERROR ("uc_insert: cache_alloc (%zu) failed.", ds->ds_num);
164     return (-1);
165   }
166
167   sstrncpy (ce->name, key, sizeof (ce->name));
168
169   for (i = 0; i < ds->ds_num; i++)
170   {
171     switch (ds->ds[i].type)
172     {
173       case DS_TYPE_COUNTER:
174         ce->values_gauge[i] = NAN;
175         ce->values_raw[i].counter = vl->values[i].counter;
176         break;
177
178       case DS_TYPE_GAUGE:
179         ce->values_gauge[i] = vl->values[i].gauge;
180         ce->values_raw[i].gauge = vl->values[i].gauge;
181         break;
182
183       case DS_TYPE_DERIVE:
184         ce->values_gauge[i] = NAN;
185         ce->values_raw[i].derive = vl->values[i].derive;
186         break;
187
188       case DS_TYPE_ABSOLUTE:
189         ce->values_gauge[i] = NAN;
190         if (vl->interval > 0)
191           ce->values_gauge[i] = ((double) vl->values[i].absolute)
192             / CDTIME_T_TO_DOUBLE (vl->interval);
193         ce->values_raw[i].absolute = vl->values[i].absolute;
194         break;
195
196       default:
197         /* This shouldn't happen. */
198         ERROR ("uc_insert: Don't know how to handle data source type %i.",
199             ds->ds[i].type);
200         sfree (key_copy);
201         cache_free (ce);
202         return (-1);
203     } /* switch (ds->ds[i].type) */
204   } /* for (i) */
205
206   /* Prune invalid gauge data */
207   uc_check_range (ds, ce);
208
209   ce->last_time = vl->time;
210   ce->last_update = cdtime ();
211   ce->interval = vl->interval;
212   ce->state = STATE_OKAY;
213
214   if (c_avl_insert (cache_tree, key_copy, ce) != 0)
215   {
216     sfree (key_copy);
217     ERROR ("uc_insert: c_avl_insert failed.");
218     return (-1);
219   }
220
221   DEBUG ("uc_insert: Added %s to the cache.", key);
222   return (0);
223 } /* int uc_insert */
224
225 int uc_init (void)
226 {
227   if (cache_tree == NULL)
228     cache_tree = c_avl_create ((int (*) (const void *, const void *))
229         cache_compare);
230
231   return (0);
232 } /* int uc_init */
233
234 int uc_check_timeout (void)
235 {
236   cdtime_t now;
237   cache_entry_t *ce;
238
239   char **keys = NULL;
240   cdtime_t *keys_time = NULL;
241   cdtime_t *keys_interval = NULL;
242   int keys_len = 0;
243
244   char *key;
245   c_avl_iterator_t *iter;
246
247   int status;
248   int i;
249
250   pthread_mutex_lock (&cache_lock);
251
252   now = cdtime ();
253
254   /* Build a list of entries to be flushed */
255   iter = c_avl_get_iterator (cache_tree);
256   while (c_avl_iterator_next (iter, (void *) &key, (void *) &ce) == 0)
257   {
258     char **tmp;
259     cdtime_t *tmp_time;
260
261     /* If the entry is fresh enough, continue. */
262     if ((now - ce->last_update) < (ce->interval * timeout_g))
263       continue;
264
265     /* If entry has not been updated, add to `keys' array */
266     tmp = realloc ((void *) keys,
267         (keys_len + 1) * sizeof (char *));
268     if (tmp == NULL)
269     {
270       ERROR ("uc_check_timeout: realloc failed.");
271       continue;
272     }
273     keys = tmp;
274
275     tmp_time = realloc (keys_time, (keys_len + 1) * sizeof (*keys_time));
276     if (tmp_time == NULL)
277     {
278       ERROR ("uc_check_timeout: realloc failed.");
279       continue;
280     }
281     keys_time = tmp_time;
282
283     tmp_time = realloc (keys_interval, (keys_len + 1) * sizeof (*keys_interval));
284     if (tmp_time == NULL)
285     {
286       ERROR ("uc_check_timeout: realloc failed.");
287       continue;
288     }
289     keys_interval = tmp_time;
290
291     keys[keys_len] = strdup (key);
292     if (keys[keys_len] == NULL)
293     {
294       ERROR ("uc_check_timeout: strdup failed.");
295       continue;
296     }
297     keys_time[keys_len] = ce->last_time;
298     keys_interval[keys_len] = ce->interval;
299
300     keys_len++;
301   } /* while (c_avl_iterator_next) */
302
303   c_avl_iterator_destroy (iter);
304   pthread_mutex_unlock (&cache_lock);
305
306   if (keys_len == 0)
307   {
308     /* realloc() may have been called for these. */
309     sfree (keys);
310     sfree (keys_time);
311     sfree (keys_interval);
312     return (0);
313   }
314
315   /* Call the "missing" callback for each value. Do this before removing the
316    * value from the cache, so that callbacks can still access the data stored,
317    * including plugin specific meta data, rates, history, â€¦. This must be done
318    * without holding the lock, otherwise we will run into a deadlock if a
319    * plugin calls the cache interface. */
320   for (i = 0; i < keys_len; i++)
321   {
322     value_list_t vl = VALUE_LIST_INIT;
323
324     vl.values = NULL;
325     vl.values_len = 0;
326     vl.meta = NULL;
327
328     status = parse_identifier_vl (keys[i], &vl);
329     if (status != 0)
330     {
331       ERROR ("uc_check_timeout: parse_identifier_vl (\"%s\") failed.", keys[i]);
332       continue;
333     }
334
335     vl.time = keys_time[i];
336     vl.interval = keys_interval[i];
337
338     plugin_dispatch_missing (&vl);
339   } /* for (i = 0; i < keys_len; i++) */
340
341   /* Now actually remove all the values from the cache. We don't re-evaluate
342    * the timestamp again, so in theory it is possible we remove a value after
343    * it is updated here. */
344   pthread_mutex_lock (&cache_lock);
345   for (i = 0; i < keys_len; i++)
346   {
347     key = NULL;
348     ce = NULL;
349
350     status = c_avl_remove (cache_tree, keys[i],
351         (void *) &key, (void *) &ce);
352     if (status != 0)
353     {
354       ERROR ("uc_check_timeout: c_avl_remove (\"%s\") failed.", keys[i]);
355       sfree (keys[i]);
356       continue;
357     }
358
359     sfree (keys[i]);
360     sfree (key);
361     cache_free (ce);
362   } /* for (i = 0; i < keys_len; i++) */
363   pthread_mutex_unlock (&cache_lock);
364
365   sfree (keys);
366   sfree (keys_time);
367   sfree (keys_interval);
368
369   return (0);
370 } /* int uc_check_timeout */
371
372 int uc_update (const data_set_t *ds, const value_list_t *vl)
373 {
374   char name[6 * DATA_MAX_NAME_LEN];
375   cache_entry_t *ce = NULL;
376   int status;
377   size_t i;
378
379   if (FORMAT_VL (name, sizeof (name), vl) != 0)
380   {
381     ERROR ("uc_update: FORMAT_VL failed.");
382     return (-1);
383   }
384
385   pthread_mutex_lock (&cache_lock);
386
387   status = c_avl_get (cache_tree, name, (void *) &ce);
388   if (status != 0) /* entry does not yet exist */
389   {
390     status = uc_insert (ds, vl, name);
391     pthread_mutex_unlock (&cache_lock);
392     return (status);
393   }
394
395   assert (ce != NULL);
396   assert (ce->values_num == ds->ds_num);
397
398   if (ce->last_time >= vl->time)
399   {
400     pthread_mutex_unlock (&cache_lock);
401     NOTICE ("uc_update: Value too old: name = %s; value time = %.3f; "
402         "last cache update = %.3f;",
403         name,
404         CDTIME_T_TO_DOUBLE (vl->time),
405         CDTIME_T_TO_DOUBLE (ce->last_time));
406     return (-1);
407   }
408
409   for (i = 0; i < ds->ds_num; i++)
410   {
411     switch (ds->ds[i].type)
412     {
413       case DS_TYPE_COUNTER:
414         {
415           counter_t diff = counter_diff (ce->values_raw[i].counter, vl->values[i].counter);
416           ce->values_gauge[i] = ((double) diff)
417             / (CDTIME_T_TO_DOUBLE (vl->time - ce->last_time));
418           ce->values_raw[i].counter = vl->values[i].counter;
419         }
420         break;
421
422       case DS_TYPE_GAUGE:
423         ce->values_raw[i].gauge = vl->values[i].gauge;
424         ce->values_gauge[i] = vl->values[i].gauge;
425         break;
426
427       case DS_TYPE_DERIVE:
428         {
429           derive_t diff = vl->values[i].derive - ce->values_raw[i].derive;
430
431           ce->values_gauge[i] = ((double) diff)
432             / (CDTIME_T_TO_DOUBLE (vl->time - ce->last_time));
433           ce->values_raw[i].derive = vl->values[i].derive;
434         }
435         break;
436
437       case DS_TYPE_ABSOLUTE:
438         ce->values_gauge[i] = ((double) vl->values[i].absolute)
439           / (CDTIME_T_TO_DOUBLE (vl->time - ce->last_time));
440         ce->values_raw[i].absolute = vl->values[i].absolute;
441         break;
442
443       default:
444         /* This shouldn't happen. */
445         pthread_mutex_unlock (&cache_lock);
446         ERROR ("uc_update: Don't know how to handle data source type %i.",
447             ds->ds[i].type);
448         return (-1);
449     } /* switch (ds->ds[i].type) */
450
451     DEBUG ("uc_update: %s: ds[%zu] = %lf", name, i, ce->values_gauge[i]);
452   } /* for (i) */
453
454   /* Update the history if it exists. */
455   if (ce->history != NULL)
456   {
457     assert (ce->history_index < ce->history_length);
458     for (i = 0; i < ce->values_num; i++)
459     {
460       size_t hist_idx = (ce->values_num * ce->history_index) + i;
461       ce->history[hist_idx] = ce->values_gauge[i];
462     }
463
464     assert (ce->history_length > 0);
465     ce->history_index = (ce->history_index + 1) % ce->history_length;
466   }
467
468   /* Prune invalid gauge data */
469   uc_check_range (ds, ce);
470
471   ce->last_time = vl->time;
472   ce->last_update = cdtime ();
473   ce->interval = vl->interval;
474
475   pthread_mutex_unlock (&cache_lock);
476
477   return (0);
478 } /* int uc_update */
479
480 int uc_get_rate_by_name (const char *name, gauge_t **ret_values, size_t *ret_values_num)
481 {
482   gauge_t *ret = NULL;
483   size_t ret_num = 0;
484   cache_entry_t *ce = NULL;
485   int status = 0;
486
487   pthread_mutex_lock (&cache_lock);
488
489   if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
490   {
491     assert (ce != NULL);
492
493     /* remove missing values from getval */
494     if (ce->state == STATE_MISSING)
495     {
496       status = -1;
497     }
498     else
499     {
500       ret_num = ce->values_num;
501       ret = malloc (ret_num * sizeof (*ret));
502       if (ret == NULL)
503       {
504         ERROR ("utils_cache: uc_get_rate_by_name: malloc failed.");
505         status = -1;
506       }
507       else
508       {
509         memcpy (ret, ce->values_gauge, ret_num * sizeof (gauge_t));
510       }
511     }
512   }
513   else
514   {
515     DEBUG ("utils_cache: uc_get_rate_by_name: No such value: %s", name);
516     status = -1;
517   }
518
519   pthread_mutex_unlock (&cache_lock);
520
521   if (status == 0)
522   {
523     *ret_values = ret;
524     *ret_values_num = ret_num;
525   }
526
527   return (status);
528 } /* gauge_t *uc_get_rate_by_name */
529
530 gauge_t *uc_get_rate (const data_set_t *ds, const value_list_t *vl)
531 {
532   char name[6 * DATA_MAX_NAME_LEN];
533   gauge_t *ret = NULL;
534   size_t ret_num = 0;
535   int status;
536
537   if (FORMAT_VL (name, sizeof (name), vl) != 0)
538   {
539     ERROR ("utils_cache: uc_get_rate: FORMAT_VL failed.");
540     return (NULL);
541   }
542
543   status = uc_get_rate_by_name (name, &ret, &ret_num);
544   if (status != 0)
545     return (NULL);
546
547   /* This is important - the caller has no other way of knowing how many
548    * values are returned. */
549   if (ret_num != (size_t) ds->ds_num)
550   {
551     ERROR ("utils_cache: uc_get_rate: ds[%s] has %zu values, "
552         "but uc_get_rate_by_name returned %zu.",
553         ds->type, ds->ds_num, ret_num);
554     sfree (ret);
555     return (NULL);
556   }
557
558   return (ret);
559 } /* gauge_t *uc_get_rate */
560
561 size_t uc_get_size (void) {
562   size_t size_arrays = 0;
563
564   pthread_mutex_lock (&cache_lock);
565   size_arrays = (size_t) c_avl_size (cache_tree);
566   pthread_mutex_unlock (&cache_lock);
567
568   return (size_arrays);
569 }
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     sfree (times);
646
647     return (-1);
648   }
649
650   *ret_names = names;
651   if (ret_times != NULL)
652     *ret_times = times;
653   else
654     sfree (times);
655   *ret_number = number;
656
657   return (0);
658 } /* int uc_get_names */
659
660 int uc_get_state (const data_set_t *ds, const value_list_t *vl)
661 {
662   char name[6 * DATA_MAX_NAME_LEN];
663   cache_entry_t *ce = NULL;
664   int ret = STATE_ERROR;
665
666   if (FORMAT_VL (name, sizeof (name), vl) != 0)
667   {
668     ERROR ("uc_get_state: FORMAT_VL failed.");
669     return (STATE_ERROR);
670   }
671
672   pthread_mutex_lock (&cache_lock);
673
674   if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
675   {
676     assert (ce != NULL);
677     ret = ce->state;
678   }
679
680   pthread_mutex_unlock (&cache_lock);
681
682   return (ret);
683 } /* int uc_get_state */
684
685 int uc_set_state (const data_set_t *ds, const value_list_t *vl, int state)
686 {
687   char name[6 * DATA_MAX_NAME_LEN];
688   cache_entry_t *ce = NULL;
689   int ret = -1;
690
691   if (FORMAT_VL (name, sizeof (name), vl) != 0)
692   {
693     ERROR ("uc_get_state: FORMAT_VL failed.");
694     return (STATE_ERROR);
695   }
696
697   pthread_mutex_lock (&cache_lock);
698
699   if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
700   {
701     assert (ce != NULL);
702     ret = ce->state;
703     ce->state = state;
704   }
705
706   pthread_mutex_unlock (&cache_lock);
707
708   return (ret);
709 } /* int uc_set_state */
710
711 int uc_get_history_by_name (const char *name,
712     gauge_t *ret_history, size_t num_steps, size_t num_ds)
713 {
714   cache_entry_t *ce = NULL;
715   size_t i;
716   int status = 0;
717
718   pthread_mutex_lock (&cache_lock);
719
720   status = c_avl_get (cache_tree, name, (void *) &ce);
721   if (status != 0)
722   {
723     pthread_mutex_unlock (&cache_lock);
724     return (-ENOENT);
725   }
726
727   if (((size_t) ce->values_num) != num_ds)
728   {
729     pthread_mutex_unlock (&cache_lock);
730     return (-EINVAL);
731   }
732
733   /* Check if there are enough values available. If not, increase the buffer
734    * size. */
735   if (ce->history_length < num_steps)
736   {
737     gauge_t *tmp;
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 : */