src/utils_cache.c: Remove incorrect free.
[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     return (0);
300
301   /* Call the "missing" callback for each value. Do this before removing the
302    * value from the cache, so that callbacks can still access the data stored,
303    * including plugin specific meta data, rates, history, â€¦. This must be done
304    * without holding the lock, otherwise we will run into a deadlock if a
305    * plugin calls the cache interface. */
306   for (i = 0; i < keys_len; i++)
307   {
308     value_list_t vl = VALUE_LIST_INIT;
309
310     vl.values = NULL;
311     vl.values_len = 0;
312     vl.meta = NULL;
313
314     status = parse_identifier_vl (keys[i], &vl);
315     if (status != 0)
316     {
317       ERROR ("uc_check_timeout: parse_identifier_vl (\"%s\") failed.", keys[i]);
318       continue;
319     }
320
321     vl.time = keys_time[i];
322     vl.interval = keys_interval[i];
323
324     plugin_dispatch_missing (&vl);
325   } /* for (i = 0; i < keys_len; i++) */
326
327   /* Now actually remove all the values from the cache. We don't re-evaluate
328    * the timestamp again, so in theory it is possible we remove a value after
329    * it is updated here. */
330   pthread_mutex_lock (&cache_lock);
331   for (i = 0; i < keys_len; i++)
332   {
333     key = NULL;
334     ce = NULL;
335
336     status = c_avl_remove (cache_tree, keys[i],
337         (void *) &key, (void *) &ce);
338     if (status != 0)
339     {
340       ERROR ("uc_check_timeout: c_avl_remove (\"%s\") failed.", keys[i]);
341       sfree (keys[i]);
342       continue;
343     }
344
345     sfree (keys[i]);
346     sfree (key);
347     cache_free (ce);
348   } /* for (i = 0; i < keys_len; i++) */
349   pthread_mutex_unlock (&cache_lock);
350
351   sfree (keys);
352   sfree (keys_time);
353   sfree (keys_interval);
354
355   return (0);
356 } /* int uc_check_timeout */
357
358 int uc_update (const data_set_t *ds, const value_list_t *vl)
359 {
360   char name[6 * DATA_MAX_NAME_LEN];
361   cache_entry_t *ce = NULL;
362   int status;
363   int i;
364
365   if (FORMAT_VL (name, sizeof (name), vl) != 0)
366   {
367     ERROR ("uc_update: FORMAT_VL failed.");
368     return (-1);
369   }
370
371   pthread_mutex_lock (&cache_lock);
372
373   status = c_avl_get (cache_tree, name, (void *) &ce);
374   if (status != 0) /* entry does not yet exist */
375   {
376     status = uc_insert (ds, vl, name);
377     pthread_mutex_unlock (&cache_lock);
378     return (status);
379   }
380
381   assert (ce != NULL);
382   assert (ce->values_num == ds->ds_num);
383
384   if (ce->last_time >= vl->time)
385   {
386     pthread_mutex_unlock (&cache_lock);
387     NOTICE ("uc_update: Value too old: name = %s; value time = %.3f; "
388         "last cache update = %.3f;",
389         name,
390         CDTIME_T_TO_DOUBLE (vl->time),
391         CDTIME_T_TO_DOUBLE (ce->last_time));
392     return (-1);
393   }
394
395   for (i = 0; i < ds->ds_num; i++)
396   {
397     switch (ds->ds[i].type)
398     {
399       case DS_TYPE_COUNTER:
400         {
401           counter_t diff;
402
403           /* check if the counter has wrapped around */
404           if (vl->values[i].counter < ce->values_raw[i].counter)
405           {
406             if (ce->values_raw[i].counter <= 4294967295U)
407               diff = (4294967295U - ce->values_raw[i].counter)
408                 + vl->values[i].counter;
409             else
410               diff = (18446744073709551615ULL - ce->values_raw[i].counter)
411                 + vl->values[i].counter;
412           }
413           else /* counter has NOT wrapped around */
414           {
415             diff = vl->values[i].counter - ce->values_raw[i].counter;
416           }
417
418           ce->values_gauge[i] = ((double) diff)
419             / (CDTIME_T_TO_DOUBLE (vl->time - ce->last_time));
420           ce->values_raw[i].counter = vl->values[i].counter;
421         }
422         break;
423
424       case DS_TYPE_GAUGE:
425         ce->values_raw[i].gauge = vl->values[i].gauge;
426         ce->values_gauge[i] = vl->values[i].gauge;
427         break;
428
429       case DS_TYPE_DERIVE:
430         {
431           derive_t diff;
432
433           diff = vl->values[i].derive - ce->values_raw[i].derive;
434
435           ce->values_gauge[i] = ((double) diff)
436             / (CDTIME_T_TO_DOUBLE (vl->time - ce->last_time));
437           ce->values_raw[i].derive = vl->values[i].derive;
438         }
439         break;
440
441       case DS_TYPE_ABSOLUTE:
442         ce->values_gauge[i] = ((double) vl->values[i].absolute)
443           / (CDTIME_T_TO_DOUBLE (vl->time - ce->last_time));
444         ce->values_raw[i].absolute = vl->values[i].absolute;
445         break;
446
447       default:
448         /* This shouldn't happen. */
449         pthread_mutex_unlock (&cache_lock);
450         ERROR ("uc_update: Don't know how to handle data source type %i.",
451             ds->ds[i].type);
452         return (-1);
453     } /* switch (ds->ds[i].type) */
454
455     DEBUG ("uc_update: %s: ds[%i] = %lf", name, i, ce->values_gauge[i]);
456   } /* for (i) */
457
458   /* Update the history if it exists. */
459   if (ce->history != NULL)
460   {
461     assert (ce->history_index < ce->history_length);
462     for (i = 0; i < ce->values_num; i++)
463     {
464       size_t hist_idx = (ce->values_num * ce->history_index) + i;
465       ce->history[hist_idx] = ce->values_gauge[i];
466     }
467
468     assert (ce->history_length > 0);
469     ce->history_index = (ce->history_index + 1) % ce->history_length;
470   }
471
472   /* Prune invalid gauge data */
473   uc_check_range (ds, ce);
474
475   ce->last_time = vl->time;
476   ce->last_update = cdtime ();
477   ce->interval = vl->interval;
478
479   pthread_mutex_unlock (&cache_lock);
480
481   return (0);
482 } /* int uc_update */
483
484 int uc_get_rate_by_name (const char *name, gauge_t **ret_values, size_t *ret_values_num)
485 {
486   gauge_t *ret = NULL;
487   size_t ret_num = 0;
488   cache_entry_t *ce = NULL;
489   int status = 0;
490
491   pthread_mutex_lock (&cache_lock);
492
493   if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
494   {
495     assert (ce != NULL);
496
497     /* remove missing values from getval */
498     if (ce->state == STATE_MISSING)
499     {
500       status = -1;
501     }
502     else
503     {
504       ret_num = ce->values_num;
505       ret = (gauge_t *) malloc (ret_num * sizeof (gauge_t));
506       if (ret == NULL)
507       {
508         ERROR ("utils_cache: uc_get_rate_by_name: malloc failed.");
509         status = -1;
510       }
511       else
512       {
513         memcpy (ret, ce->values_gauge, ret_num * sizeof (gauge_t));
514       }
515     }
516   }
517   else
518   {
519     DEBUG ("utils_cache: uc_get_rate_by_name: No such value: %s", name);
520     status = -1;
521   }
522
523   pthread_mutex_unlock (&cache_lock);
524
525   if (status == 0)
526   {
527     *ret_values = ret;
528     *ret_values_num = ret_num;
529   }
530
531   return (status);
532 } /* gauge_t *uc_get_rate_by_name */
533
534 gauge_t *uc_get_rate (const data_set_t *ds, const value_list_t *vl)
535 {
536   char name[6 * DATA_MAX_NAME_LEN];
537   gauge_t *ret = NULL;
538   size_t ret_num = 0;
539   int status;
540
541   if (FORMAT_VL (name, sizeof (name), vl) != 0)
542   {
543     ERROR ("utils_cache: uc_get_rate: FORMAT_VL failed.");
544     return (NULL);
545   }
546
547   status = uc_get_rate_by_name (name, &ret, &ret_num);
548   if (status != 0)
549     return (NULL);
550
551   /* This is important - the caller has no other way of knowing how many
552    * values are returned. */
553   if (ret_num != (size_t) ds->ds_num)
554   {
555     ERROR ("utils_cache: uc_get_rate: ds[%s] has %i values, "
556         "but uc_get_rate_by_name returned %zu.",
557         ds->type, ds->ds_num, ret_num);
558     sfree (ret);
559     return (NULL);
560   }
561
562   return (ret);
563 } /* gauge_t *uc_get_rate */
564
565 int uc_get_names (char ***ret_names, cdtime_t **ret_times, size_t *ret_number)
566 {
567   c_avl_iterator_t *iter;
568   char *key;
569   cache_entry_t *value;
570
571   char **names = NULL;
572   cdtime_t *times = NULL;
573   size_t number = 0;
574   size_t size_arrays = 0;
575
576   int status = 0;
577
578   if ((ret_names == NULL) || (ret_number == NULL))
579     return (-1);
580
581   pthread_mutex_lock (&cache_lock);
582
583   size_arrays = (size_t) c_avl_size (cache_tree);
584   if (size_arrays < 1)
585   {
586     /* Handle the "no values" case here, to avoid the error message when
587      * calloc() returns NULL. */
588     pthread_mutex_unlock (&cache_lock);
589     return (0);
590   }
591
592   names = calloc (size_arrays, sizeof (*names));
593   times = calloc (size_arrays, sizeof (*times));
594   if ((names == NULL) || (times == NULL))
595   {
596     ERROR ("uc_get_names: calloc failed.");
597     sfree (names);
598     sfree (times);
599     pthread_mutex_unlock (&cache_lock);
600     return (ENOMEM);
601   }
602
603   iter = c_avl_get_iterator (cache_tree);
604   while (c_avl_iterator_next (iter, (void *) &key, (void *) &value) == 0)
605   {
606     /* remove missing values when list values */
607     if (value->state == STATE_MISSING)
608       continue;
609
610     /* c_avl_size does not return a number smaller than the number of elements
611      * returned by c_avl_iterator_next. */
612     assert (number < size_arrays);
613
614     if (ret_times != NULL)
615       times[number] = value->last_time;
616
617     names[number] = strdup (key);
618     if (names[number] == NULL)
619     {
620       status = -1;
621       break;
622     }
623
624     number++;
625   } /* while (c_avl_iterator_next) */
626
627   c_avl_iterator_destroy (iter);
628   pthread_mutex_unlock (&cache_lock);
629
630   if (status != 0)
631   {
632     size_t i;
633     
634     for (i = 0; i < number; i++)
635     {
636       sfree (names[i]);
637     }
638     sfree (names);
639
640     return (-1);
641   }
642
643   *ret_names = names;
644   if (ret_times != NULL)
645     *ret_times = times;
646   *ret_number = number;
647
648   return (0);
649 } /* int uc_get_names */
650
651 int uc_get_state (const data_set_t *ds, const value_list_t *vl)
652 {
653   char name[6 * DATA_MAX_NAME_LEN];
654   cache_entry_t *ce = NULL;
655   int ret = STATE_ERROR;
656
657   if (FORMAT_VL (name, sizeof (name), vl) != 0)
658   {
659     ERROR ("uc_get_state: FORMAT_VL failed.");
660     return (STATE_ERROR);
661   }
662
663   pthread_mutex_lock (&cache_lock);
664
665   if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
666   {
667     assert (ce != NULL);
668     ret = ce->state;
669   }
670
671   pthread_mutex_unlock (&cache_lock);
672
673   return (ret);
674 } /* int uc_get_state */
675
676 int uc_set_state (const data_set_t *ds, const value_list_t *vl, int state)
677 {
678   char name[6 * DATA_MAX_NAME_LEN];
679   cache_entry_t *ce = NULL;
680   int ret = -1;
681
682   if (FORMAT_VL (name, sizeof (name), vl) != 0)
683   {
684     ERROR ("uc_get_state: FORMAT_VL failed.");
685     return (STATE_ERROR);
686   }
687
688   pthread_mutex_lock (&cache_lock);
689
690   if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
691   {
692     assert (ce != NULL);
693     ret = ce->state;
694     ce->state = state;
695   }
696
697   pthread_mutex_unlock (&cache_lock);
698
699   return (ret);
700 } /* int uc_set_state */
701
702 int uc_get_history_by_name (const char *name,
703     gauge_t *ret_history, size_t num_steps, size_t num_ds)
704 {
705   cache_entry_t *ce = NULL;
706   size_t i;
707   int status = 0;
708
709   pthread_mutex_lock (&cache_lock);
710
711   status = c_avl_get (cache_tree, name, (void *) &ce);
712   if (status != 0)
713   {
714     pthread_mutex_unlock (&cache_lock);
715     return (-ENOENT);
716   }
717
718   if (((size_t) ce->values_num) != num_ds)
719   {
720     pthread_mutex_unlock (&cache_lock);
721     return (-EINVAL);
722   }
723
724   /* Check if there are enough values available. If not, increase the buffer
725    * size. */
726   if (ce->history_length < num_steps)
727   {
728     gauge_t *tmp;
729     size_t i;
730
731     tmp = realloc (ce->history, sizeof (*ce->history)
732         * num_steps * ce->values_num);
733     if (tmp == NULL)
734     {
735       pthread_mutex_unlock (&cache_lock);
736       return (-ENOMEM);
737     }
738
739     for (i = ce->history_length * ce->values_num;
740         i < (num_steps * ce->values_num);
741         i++)
742       tmp[i] = NAN;
743
744     ce->history = tmp;
745     ce->history_length = num_steps;
746   } /* if (ce->history_length < num_steps) */
747
748   /* Copy the values to the output buffer. */
749   for (i = 0; i < num_steps; i++)
750   {
751     size_t src_index;
752     size_t dst_index;
753
754     if (i < ce->history_index)
755       src_index = ce->history_index - (i + 1);
756     else
757       src_index = ce->history_length + ce->history_index - (i + 1);
758     src_index = src_index * num_ds;
759
760     dst_index = i * num_ds;
761
762     memcpy (ret_history + dst_index, ce->history + src_index,
763         sizeof (*ret_history) * num_ds);
764   }
765
766   pthread_mutex_unlock (&cache_lock);
767
768   return (0);
769 } /* int uc_get_history_by_name */
770
771 int uc_get_history (const data_set_t *ds, const value_list_t *vl,
772     gauge_t *ret_history, size_t num_steps, size_t num_ds)
773 {
774   char name[6 * DATA_MAX_NAME_LEN];
775
776   if (FORMAT_VL (name, sizeof (name), vl) != 0)
777   {
778     ERROR ("utils_cache: uc_get_history: FORMAT_VL failed.");
779     return (-1);
780   }
781
782   return (uc_get_history_by_name (name, ret_history, num_steps, num_ds));
783 } /* int uc_get_history */
784
785 int uc_get_hits (const data_set_t *ds, const value_list_t *vl)
786 {
787   char name[6 * DATA_MAX_NAME_LEN];
788   cache_entry_t *ce = NULL;
789   int ret = STATE_ERROR;
790
791   if (FORMAT_VL (name, sizeof (name), vl) != 0)
792   {
793     ERROR ("uc_get_state: FORMAT_VL failed.");
794     return (STATE_ERROR);
795   }
796
797   pthread_mutex_lock (&cache_lock);
798
799   if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
800   {
801     assert (ce != NULL);
802     ret = ce->hits;
803   }
804
805   pthread_mutex_unlock (&cache_lock);
806
807   return (ret);
808 } /* int uc_get_hits */
809
810 int uc_set_hits (const data_set_t *ds, const value_list_t *vl, int hits)
811 {
812   char name[6 * DATA_MAX_NAME_LEN];
813   cache_entry_t *ce = NULL;
814   int ret = -1;
815
816   if (FORMAT_VL (name, sizeof (name), vl) != 0)
817   {
818     ERROR ("uc_get_state: FORMAT_VL failed.");
819     return (STATE_ERROR);
820   }
821
822   pthread_mutex_lock (&cache_lock);
823
824   if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
825   {
826     assert (ce != NULL);
827     ret = ce->hits;
828     ce->hits = hits;
829   }
830
831   pthread_mutex_unlock (&cache_lock);
832
833   return (ret);
834 } /* int uc_set_hits */
835
836 int uc_inc_hits (const data_set_t *ds, const value_list_t *vl, int step)
837 {
838   char name[6 * DATA_MAX_NAME_LEN];
839   cache_entry_t *ce = NULL;
840   int ret = -1;
841
842   if (FORMAT_VL (name, sizeof (name), vl) != 0)
843   {
844     ERROR ("uc_get_state: FORMAT_VL failed.");
845     return (STATE_ERROR);
846   }
847
848   pthread_mutex_lock (&cache_lock);
849
850   if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
851   {
852     assert (ce != NULL);
853     ret = ce->hits;
854     ce->hits = ret + step;
855   }
856
857   pthread_mutex_unlock (&cache_lock);
858
859   return (ret);
860 } /* int uc_inc_hits */
861
862 /*
863  * Meta data interface
864  */
865 /* XXX: This function will acquire `cache_lock' but will not free it! */
866 static meta_data_t *uc_get_meta (const value_list_t *vl) /* {{{ */
867 {
868   char name[6 * DATA_MAX_NAME_LEN];
869   cache_entry_t *ce = NULL;
870   int status;
871
872   status = FORMAT_VL (name, sizeof (name), vl);
873   if (status != 0)
874   {
875     ERROR ("utils_cache: uc_get_meta: FORMAT_VL failed.");
876     return (NULL);
877   }
878
879   pthread_mutex_lock (&cache_lock);
880
881   status = c_avl_get (cache_tree, name, (void *) &ce);
882   if (status != 0)
883   {
884     pthread_mutex_unlock (&cache_lock);
885     return (NULL);
886   }
887   assert (ce != NULL);
888
889   if (ce->meta == NULL)
890     ce->meta = meta_data_create ();
891
892   if (ce->meta == NULL)
893     pthread_mutex_unlock (&cache_lock);
894
895   return (ce->meta);
896 } /* }}} meta_data_t *uc_get_meta */
897
898 /* Sorry about this preprocessor magic, but it really makes this file much
899  * shorter.. */
900 #define UC_WRAP(wrap_function) { \
901   meta_data_t *meta; \
902   int status; \
903   meta = uc_get_meta (vl); \
904   if (meta == NULL) return (-1); \
905   status = wrap_function (meta, key); \
906   pthread_mutex_unlock (&cache_lock); \
907   return (status); \
908 }
909 int uc_meta_data_exists (const value_list_t *vl, const char *key)
910   UC_WRAP (meta_data_exists)
911
912 int uc_meta_data_delete (const value_list_t *vl, const char *key)
913   UC_WRAP (meta_data_delete)
914 #undef UC_WRAP
915
916 /* We need a new version of this macro because the following functions take
917  * two argumetns. */
918 #define UC_WRAP(wrap_function) { \
919   meta_data_t *meta; \
920   int status; \
921   meta = uc_get_meta (vl); \
922   if (meta == NULL) return (-1); \
923   status = wrap_function (meta, key, value); \
924   pthread_mutex_unlock (&cache_lock); \
925   return (status); \
926 }
927 int uc_meta_data_add_string (const value_list_t *vl,
928     const char *key,
929     const char *value)
930   UC_WRAP(meta_data_add_string)
931 int uc_meta_data_add_signed_int (const value_list_t *vl,
932     const char *key,
933     int64_t value)
934   UC_WRAP(meta_data_add_signed_int)
935 int uc_meta_data_add_unsigned_int (const value_list_t *vl,
936     const char *key,
937     uint64_t value)
938   UC_WRAP(meta_data_add_unsigned_int)
939 int uc_meta_data_add_double (const value_list_t *vl,
940     const char *key,
941     double value)
942   UC_WRAP(meta_data_add_double)
943 int uc_meta_data_add_boolean (const value_list_t *vl,
944     const char *key,
945     _Bool value)
946   UC_WRAP(meta_data_add_boolean)
947
948 int uc_meta_data_get_string (const value_list_t *vl,
949     const char *key,
950     char **value)
951   UC_WRAP(meta_data_get_string)
952 int uc_meta_data_get_signed_int (const value_list_t *vl,
953     const char *key,
954     int64_t *value)
955   UC_WRAP(meta_data_get_signed_int)
956 int uc_meta_data_get_unsigned_int (const value_list_t *vl,
957     const char *key,
958     uint64_t *value)
959   UC_WRAP(meta_data_get_unsigned_int)
960 int uc_meta_data_get_double (const value_list_t *vl,
961     const char *key,
962     double *value)
963   UC_WRAP(meta_data_get_double)
964 int uc_meta_data_get_boolean (const value_list_t *vl,
965     const char *key,
966     _Bool *value)
967   UC_WRAP(meta_data_get_boolean)
968 #undef UC_WRAP
969
970 /* vim: set sw=2 ts=8 sts=2 tw=78 : */