Merge branch 'collectd-4.7'
[collectd.git] / src / utils_cache.c
1 /**
2  * collectd - src/utils_cache.c
3  * Copyright (C) 2007,2008  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 verplant.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 "utils_threshold.h"
28 #include "meta_data.h"
29
30 #include <assert.h>
31 #include <pthread.h>
32
33 typedef struct cache_entry_s
34 {
35         char name[6 * DATA_MAX_NAME_LEN];
36         int        values_num;
37         gauge_t   *values_gauge;
38         value_t   *values_raw;
39         /* Time contained in the package
40          * (for calculating rates) */
41         time_t last_time;
42         /* Time according to the local clock
43          * (for purging old entries) */
44         time_t last_update;
45         /* Interval in which the data is collected
46          * (for purding old entries) */
47         int interval;
48         int state;
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 int uc_send_notification (const char *name)
123 {
124   cache_entry_t *ce = NULL;
125   int status;
126
127   char *name_copy;
128   char *host;
129   char *plugin;
130   char *plugin_instance;
131   char *type;
132   char *type_instance;
133
134   notification_t n;
135
136   name_copy = strdup (name);
137   if (name_copy == NULL)
138   {
139     ERROR ("uc_send_notification: strdup failed.");
140     return (-1);
141   }
142
143   status = parse_identifier (name_copy, &host,
144       &plugin, &plugin_instance,
145       &type, &type_instance);
146   if (status != 0)
147   {
148     ERROR ("uc_send_notification: Cannot parse name `%s'", name);
149     return (-1);
150   }
151
152   /* Copy the associative members */
153   notification_init (&n, NOTIF_FAILURE, /* host = */ NULL,
154       host, plugin, plugin_instance, type, type_instance);
155
156   sfree (name_copy);
157   name_copy = host = plugin = plugin_instance = type = type_instance = NULL;
158
159   pthread_mutex_lock (&cache_lock);
160
161   /*
162    * Set the time _after_ getting the lock because we don't know how long
163    * acquiring the lock takes and we will use this time later to decide
164    * whether or not the state is OKAY.
165    */
166   n.time = time (NULL);
167
168   status = c_avl_get (cache_tree, name, (void *) &ce);
169   if (status != 0)
170   {
171     pthread_mutex_unlock (&cache_lock);
172     sfree (name_copy);
173     return (-1);
174   }
175     
176   /* Check if the entry has been updated in the meantime */
177   if ((n.time - ce->last_update) < (2 * ce->interval))
178   {
179     ce->state = STATE_OKAY;
180     pthread_mutex_unlock (&cache_lock);
181     sfree (name_copy);
182     return (-1);
183   }
184
185   ssnprintf (n.message, sizeof (n.message),
186       "%s has not been updated for %i seconds.", name,
187       (int) (n.time - ce->last_update));
188
189   pthread_mutex_unlock (&cache_lock);
190
191   plugin_dispatch_notification (&n);
192
193   return (0);
194 } /* int uc_send_notification */
195
196 static void uc_check_range (const data_set_t *ds, cache_entry_t *ce)
197 {
198   int i;
199
200   for (i = 0; i < ds->ds_num; i++)
201   {
202     if (isnan (ce->values_gauge[i]))
203       continue;
204     else if (ce->values_gauge[i] < ds->ds[i].min)
205       ce->values_gauge[i] = NAN;
206     else if (ce->values_gauge[i] > ds->ds[i].max)
207       ce->values_gauge[i] = NAN;
208   }
209 } /* void uc_check_range */
210
211 static int uc_insert (const data_set_t *ds, const value_list_t *vl,
212     const char *key)
213 {
214   int i;
215   char *key_copy;
216   cache_entry_t *ce;
217
218   /* `cache_lock' has been locked by `uc_update' */
219
220   key_copy = strdup (key);
221   if (key_copy == NULL)
222   {
223     ERROR ("uc_insert: strdup failed.");
224     return (-1);
225   }
226
227   ce = cache_alloc (ds->ds_num);
228   if (ce == NULL)
229   {
230     sfree (key_copy);
231     ERROR ("uc_insert: cache_alloc (%i) failed.", ds->ds_num);
232     return (-1);
233   }
234
235   sstrncpy (ce->name, key, sizeof (ce->name));
236
237   for (i = 0; i < ds->ds_num; i++)
238   {
239     switch (ds->ds[i].type)
240     {
241       case DS_TYPE_COUNTER:
242         ce->values_gauge[i] = NAN;
243         ce->values_raw[i].counter = vl->values[i].counter;
244         break;
245
246       case DS_TYPE_GAUGE:
247         ce->values_gauge[i] = vl->values[i].gauge;
248         ce->values_raw[i].gauge = vl->values[i].gauge;
249         break;
250
251       case DS_TYPE_DERIVE:
252         ce->values_gauge[i] = NAN;
253         ce->values_raw[i].derive = vl->values[i].derive;
254         break;
255
256       case DS_TYPE_ABSOLUTE:
257         ce->values_gauge[i] = NAN;
258         if (vl->interval > 0)
259           ce->values_gauge[i] = ((double) vl->values[i].absolute)
260             / ((double) vl->interval);
261         ce->values_raw[i].absolute = vl->values[i].absolute;
262         break;
263         
264       default:
265         /* This shouldn't happen. */
266         ERROR ("uc_insert: Don't know how to handle data source type %i.",
267             ds->ds[i].type);
268         return (-1);
269     } /* switch (ds->ds[i].type) */
270   } /* for (i) */
271
272   /* Prune invalid gauge data */
273   uc_check_range (ds, ce);
274
275   ce->last_time = vl->time;
276   ce->last_update = time (NULL);
277   ce->interval = vl->interval;
278   ce->state = STATE_OKAY;
279
280   if (c_avl_insert (cache_tree, key_copy, ce) != 0)
281   {
282     sfree (key_copy);
283     ERROR ("uc_insert: c_avl_insert failed.");
284     return (-1);
285   }
286
287   DEBUG ("uc_insert: Added %s to the cache.", key);
288   return (0);
289 } /* int uc_insert */
290
291 int uc_init (void)
292 {
293   if (cache_tree == NULL)
294     cache_tree = c_avl_create ((int (*) (const void *, const void *))
295         cache_compare);
296
297   return (0);
298 } /* int uc_init */
299
300 int uc_check_timeout (void)
301 {
302   time_t now;
303   cache_entry_t *ce;
304
305   char **keys = NULL;
306   int keys_len = 0;
307
308   char *key;
309   c_avl_iterator_t *iter;
310   int i;
311   
312   pthread_mutex_lock (&cache_lock);
313
314   now = time (NULL);
315
316   /* Build a list of entries to be flushed */
317   iter = c_avl_get_iterator (cache_tree);
318   while (c_avl_iterator_next (iter, (void *) &key, (void *) &ce) == 0)
319   {
320     /* If entry has not been updated, add to `keys' array */
321     if ((now - ce->last_update) >= (2 * ce->interval))
322     {
323       char **tmp;
324
325       tmp = (char **) realloc ((void *) keys,
326           (keys_len + 1) * sizeof (char *));
327       if (tmp == NULL)
328       {
329         ERROR ("uc_check_timeout: realloc failed.");
330         c_avl_iterator_destroy (iter);
331         sfree (keys);
332         pthread_mutex_unlock (&cache_lock);
333         return (-1);
334       }
335
336       keys = tmp;
337       keys[keys_len] = strdup (key);
338       if (keys[keys_len] == NULL)
339       {
340         ERROR ("uc_check_timeout: strdup failed.");
341         continue;
342       }
343       keys_len++;
344     }
345   } /* while (c_avl_iterator_next) */
346
347   ce = NULL;
348
349   for (i = 0; i < keys_len; i++)
350   {
351     int status;
352
353     status = ut_check_interesting (keys[i]);
354
355     if (status < 0)
356     {
357       ERROR ("uc_check_timeout: ut_check_interesting failed.");
358       sfree (keys[i]);
359       continue;
360     }
361     else if (status == 0) /* ``service'' is uninteresting */
362     {
363       DEBUG ("uc_check_timeout: %s is missing but ``uninteresting''",
364           keys[i]);
365       ce = NULL;
366       status = c_avl_remove (cache_tree, keys[i],
367           (void *) &key, (void *) &ce);
368       if (status != 0)
369       {
370         ERROR ("uc_check_timeout: c_avl_remove (%s) failed.", keys[i]);
371       }
372       sfree (keys[i]);
373       sfree (key);
374       if (ce != NULL)
375         cache_free (ce);
376       continue;
377     }
378
379     /* If we get here, the value is ``interesting''. Query the record from the
380      * cache and update the state field. */
381     if (c_avl_get (cache_tree, keys[i], (void *) &ce) != 0)
382     {
383       ERROR ("uc_check_timeout: cannot get data for %s from cache", keys[i]);
384       /* Do not free `keys[i]' so a notification is sent further down. */
385       continue;
386     }
387     assert (ce != NULL);
388
389     if (status == 2) /* persist */
390     {
391       DEBUG ("uc_check_timeout: %s is missing, sending notification.",
392           keys[i]);
393       ce->state = STATE_MISSING;
394       /* Do not free `keys[i]' so a notification is sent further down. */
395     }
396     else if (status == 1) /* do not persist */
397     {
398       if (ce->state == STATE_MISSING)
399       {
400         DEBUG ("uc_check_timeout: %s is missing but "
401             "notification has already been sent.",
402             keys[i]);
403         /* Set `keys[i]' to NULL to no notification is sent. */
404         sfree (keys[i]);
405       }
406       else /* (ce->state != STATE_MISSING) */
407       {
408         DEBUG ("uc_check_timeout: %s is missing, sending one notification.",
409             keys[i]);
410         ce->state = STATE_MISSING;
411         /* Do not free `keys[i]' so a notification is sent further down. */
412       }
413     }
414     else
415     {
416       WARNING ("uc_check_timeout: ut_check_interesting (%s) returned "
417           "invalid status %i.",
418           keys[i], status);
419       sfree (keys[i]);
420     }
421
422     /* Make really sure the next iteration doesn't work with this pointer.
423      * There have been too many bugs in the past.. :/  -- octo */
424     ce = NULL;
425   } /* for (keys[i]) */
426
427   c_avl_iterator_destroy (iter);
428
429   pthread_mutex_unlock (&cache_lock);
430
431   for (i = 0; i < keys_len; i++)
432   {
433     if (keys[i] == NULL)
434       continue;
435
436     uc_send_notification (keys[i]);
437     sfree (keys[i]);
438   }
439
440   sfree (keys);
441
442   return (0);
443 } /* int uc_check_timeout */
444
445 int uc_update (const data_set_t *ds, const value_list_t *vl)
446 {
447   char name[6 * DATA_MAX_NAME_LEN];
448   cache_entry_t *ce = NULL;
449   int send_okay_notification = 0;
450   time_t update_delay = 0;
451   notification_t n;
452   int status;
453   int i;
454
455   if (FORMAT_VL (name, sizeof (name), vl) != 0)
456   {
457     ERROR ("uc_update: FORMAT_VL failed.");
458     return (-1);
459   }
460
461   pthread_mutex_lock (&cache_lock);
462
463   status = c_avl_get (cache_tree, name, (void *) &ce);
464   if (status != 0) /* entry does not yet exist */
465   {
466     status = uc_insert (ds, vl, name);
467     pthread_mutex_unlock (&cache_lock);
468     return (status);
469   }
470
471   assert (ce != NULL);
472   assert (ce->values_num == ds->ds_num);
473
474   if (ce->last_time >= vl->time)
475   {
476     pthread_mutex_unlock (&cache_lock);
477     NOTICE ("uc_update: Value too old: name = %s; value time = %u; "
478         "last cache update = %u;",
479         name, (unsigned int) vl->time, (unsigned int) ce->last_time);
480     return (-1);
481   }
482
483   /* Send a notification (after the lock has been released) if we switch the
484    * state from something else to `okay'. */
485   if (ce->state == STATE_MISSING)
486   {
487     send_okay_notification = 1;
488     ce->state = STATE_OKAY;
489     update_delay = time (NULL) - ce->last_update;
490   }
491
492   for (i = 0; i < ds->ds_num; i++)
493   {
494     switch (ds->ds[i].type)
495     {
496       case DS_TYPE_COUNTER:
497         {
498           counter_t diff;
499
500           /* check if the counter has wrapped around */
501           if (vl->values[i].counter < ce->values_raw[i].counter)
502           {
503             if (ce->values_raw[i].counter <= 4294967295U)
504               diff = (4294967295U - ce->values_raw[i].counter)
505                 + vl->values[i].counter;
506             else
507               diff = (18446744073709551615ULL - ce->values_raw[i].counter)
508                 + vl->values[i].counter;
509           }
510           else /* counter has NOT wrapped around */
511           {
512             diff = vl->values[i].counter - ce->values_raw[i].counter;
513           }
514
515           ce->values_gauge[i] = ((double) diff)
516             / ((double) (vl->time - ce->last_time));
517           ce->values_raw[i].counter = vl->values[i].counter;
518         }
519         break;
520
521       case DS_TYPE_GAUGE:
522         ce->values_raw[i].gauge = vl->values[i].gauge;
523         ce->values_gauge[i] = vl->values[i].gauge;
524         break;
525
526       case DS_TYPE_DERIVE:
527         {
528           derive_t diff;
529
530           diff = vl->values[i].derive - ce->values_raw[i].derive;
531
532           ce->values_gauge[i] = ((double) diff)
533             / ((double) (vl->time - ce->last_time));
534           ce->values_raw[i].derive = vl->values[i].derive;
535         }
536         break;
537
538       case DS_TYPE_ABSOLUTE:
539         ce->values_gauge[i] = ((double) vl->values[i].absolute)
540           / ((double) (vl->time - ce->last_time));
541         ce->values_raw[i].absolute = vl->values[i].absolute;
542         break;
543
544       default:
545         /* This shouldn't happen. */
546         pthread_mutex_unlock (&cache_lock);
547         ERROR ("uc_update: Don't know how to handle data source type %i.",
548             ds->ds[i].type);
549         return (-1);
550     } /* switch (ds->ds[i].type) */
551
552     DEBUG ("uc_update: %s: ds[%i] = %lf", name, i, ce->values_gauge[i]);
553   } /* for (i) */
554
555   /* Update the history if it exists. */
556   if (ce->history != NULL)
557   {
558     assert (ce->history_index < ce->history_length);
559     for (i = 0; i < ce->values_num; i++)
560     {
561       size_t hist_idx = (ce->values_num * ce->history_index) + i;
562       ce->history[hist_idx] = ce->values_gauge[i];
563     }
564
565     assert (ce->history_length > 0);
566     ce->history_index = (ce->history_index + 1) % ce->history_length;
567   }
568
569   /* Prune invalid gauge data */
570   uc_check_range (ds, ce);
571
572   ce->last_time = vl->time;
573   ce->last_update = time (NULL);
574   ce->interval = vl->interval;
575
576   pthread_mutex_unlock (&cache_lock);
577
578   if (send_okay_notification == 0)
579     return (0);
580
581   /* Do not send okay notifications for uninteresting values, i. e. values for
582    * which no threshold is configured. */
583   status = ut_check_interesting (name);
584   if (status <= 0)
585     return (0);
586
587   /* Initialize the notification */
588   memset (&n, '\0', sizeof (n));
589   NOTIFICATION_INIT_VL (&n, vl, ds);
590
591   n.severity = NOTIF_OKAY;
592   n.time = vl->time;
593
594   ssnprintf (n.message, sizeof (n.message),
595       "Received a value for %s. It was missing for %u seconds.",
596       name, (unsigned int) update_delay);
597
598   plugin_dispatch_notification (&n);
599
600   return (0);
601 } /* int uc_update */
602
603 int uc_get_rate_by_name (const char *name, gauge_t **ret_values, size_t *ret_values_num)
604 {
605   gauge_t *ret = NULL;
606   size_t ret_num = 0;
607   cache_entry_t *ce = NULL;
608   int status = 0;
609
610   pthread_mutex_lock (&cache_lock);
611
612   if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
613   {
614     assert (ce != NULL);
615
616     /* remove missing values from getval */
617     if (ce->state == STATE_MISSING)
618     {
619       status = -1;
620     }
621     else
622     {
623       ret_num = ce->values_num;
624       ret = (gauge_t *) malloc (ret_num * sizeof (gauge_t));
625       if (ret == NULL)
626       {
627         ERROR ("utils_cache: uc_get_rate_by_name: malloc failed.");
628         status = -1;
629       }
630       else
631       {
632         memcpy (ret, ce->values_gauge, ret_num * sizeof (gauge_t));
633       }
634     }
635   }
636   else
637   {
638     DEBUG ("utils_cache: uc_get_rate_by_name: No such value: %s", name);
639     status = -1;
640   }
641
642   pthread_mutex_unlock (&cache_lock);
643
644   if (status == 0)
645   {
646     *ret_values = ret;
647     *ret_values_num = ret_num;
648   }
649
650   return (status);
651 } /* gauge_t *uc_get_rate_by_name */
652
653 gauge_t *uc_get_rate (const data_set_t *ds, const value_list_t *vl)
654 {
655   char name[6 * DATA_MAX_NAME_LEN];
656   gauge_t *ret = NULL;
657   size_t ret_num = 0;
658   int status;
659
660   if (FORMAT_VL (name, sizeof (name), vl) != 0)
661   {
662     ERROR ("utils_cache: uc_get_rate: FORMAT_VL failed.");
663     return (NULL);
664   }
665
666   status = uc_get_rate_by_name (name, &ret, &ret_num);
667   if (status != 0)
668     return (NULL);
669
670   /* This is important - the caller has no other way of knowing how many
671    * values are returned. */
672   if (ret_num != (size_t) ds->ds_num)
673   {
674     ERROR ("utils_cache: uc_get_rate: ds[%s] has %i values, "
675         "but uc_get_rate_by_name returned %zu.",
676         ds->type, ds->ds_num, ret_num);
677     sfree (ret);
678     return (NULL);
679   }
680
681   return (ret);
682 } /* gauge_t *uc_get_rate */
683
684 int uc_get_names (char ***ret_names, time_t **ret_times, size_t *ret_number)
685 {
686   c_avl_iterator_t *iter;
687   char *key;
688   cache_entry_t *value;
689
690   char **names = NULL;
691   time_t *times = NULL;
692   size_t number = 0;
693
694   int status = 0;
695
696   if ((ret_names == NULL) || (ret_number == NULL))
697     return (-1);
698
699   pthread_mutex_lock (&cache_lock);
700
701   iter = c_avl_get_iterator (cache_tree);
702   while (c_avl_iterator_next (iter, (void *) &key, (void *) &value) == 0)
703   {
704     char **temp;
705
706     /* remove missing values when list values */
707     if (value->state == STATE_MISSING)
708       continue;
709
710     if (ret_times != NULL)
711     {
712       time_t *tmp_times;
713
714       tmp_times = (time_t *) realloc (times, sizeof (time_t) * (number + 1));
715       if (tmp_times == NULL)
716       {
717         status = -1;
718         break;
719       }
720       times = tmp_times;
721       times[number] = value->last_time;
722     }
723
724     temp = (char **) realloc (names, sizeof (char *) * (number + 1));
725     if (temp == NULL)
726     {
727       status = -1;
728       break;
729     }
730     names = temp;
731     names[number] = strdup (key);
732     if (names[number] == NULL)
733     {
734       status = -1;
735       break;
736     }
737     number++;
738   } /* while (c_avl_iterator_next) */
739
740   c_avl_iterator_destroy (iter);
741   pthread_mutex_unlock (&cache_lock);
742
743   if (status != 0)
744   {
745     size_t i;
746     
747     for (i = 0; i < number; i++)
748     {
749       sfree (names[i]);
750     }
751     sfree (names);
752
753     return (-1);
754   }
755
756   *ret_names = names;
757   if (ret_times != NULL)
758     *ret_times = times;
759   *ret_number = number;
760
761   return (0);
762 } /* int uc_get_names */
763
764 int uc_get_state (const data_set_t *ds, const value_list_t *vl)
765 {
766   char name[6 * DATA_MAX_NAME_LEN];
767   cache_entry_t *ce = NULL;
768   int ret = STATE_ERROR;
769
770   if (FORMAT_VL (name, sizeof (name), vl) != 0)
771   {
772     ERROR ("uc_get_state: FORMAT_VL failed.");
773     return (STATE_ERROR);
774   }
775
776   pthread_mutex_lock (&cache_lock);
777
778   if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
779   {
780     assert (ce != NULL);
781     ret = ce->state;
782   }
783
784   pthread_mutex_unlock (&cache_lock);
785
786   return (ret);
787 } /* int uc_get_state */
788
789 int uc_set_state (const data_set_t *ds, const value_list_t *vl, int state)
790 {
791   char name[6 * DATA_MAX_NAME_LEN];
792   cache_entry_t *ce = NULL;
793   int ret = -1;
794
795   if (FORMAT_VL (name, sizeof (name), vl) != 0)
796   {
797     ERROR ("uc_get_state: FORMAT_VL failed.");
798     return (STATE_ERROR);
799   }
800
801   pthread_mutex_lock (&cache_lock);
802
803   if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
804   {
805     assert (ce != NULL);
806     ret = ce->state;
807     ce->state = state;
808   }
809
810   pthread_mutex_unlock (&cache_lock);
811
812   return (ret);
813 } /* int uc_set_state */
814
815 int uc_get_history_by_name (const char *name,
816     gauge_t *ret_history, size_t num_steps, size_t num_ds)
817 {
818   cache_entry_t *ce = NULL;
819   size_t i;
820   int status = 0;
821
822   pthread_mutex_lock (&cache_lock);
823
824   status = c_avl_get (cache_tree, name, (void *) &ce);
825   if (status != 0)
826   {
827     pthread_mutex_unlock (&cache_lock);
828     return (-ENOENT);
829   }
830
831   if (((size_t) ce->values_num) != num_ds)
832   {
833     pthread_mutex_unlock (&cache_lock);
834     return (-EINVAL);
835   }
836
837   /* Check if there are enough values available. If not, increase the buffer
838    * size. */
839   if (ce->history_length < num_steps)
840   {
841     gauge_t *tmp;
842     size_t i;
843
844     tmp = realloc (ce->history, sizeof (*ce->history)
845         * num_steps * ce->values_num);
846     if (tmp == NULL)
847     {
848       pthread_mutex_unlock (&cache_lock);
849       return (-ENOMEM);
850     }
851
852     for (i = ce->history_length * ce->values_num;
853         i < (num_steps * ce->values_num);
854         i++)
855       tmp[i] = NAN;
856
857     ce->history = tmp;
858     ce->history_length = num_steps;
859   } /* if (ce->history_length < num_steps) */
860
861   /* Copy the values to the output buffer. */
862   for (i = 0; i < num_steps; i++)
863   {
864     size_t src_index;
865     size_t dst_index;
866
867     if (i < ce->history_index)
868       src_index = ce->history_index - (i + 1);
869     else
870       src_index = ce->history_length + ce->history_index - (i + 1);
871     src_index = src_index * num_ds;
872
873     dst_index = i * num_ds;
874
875     memcpy (ret_history + dst_index, ce->history + src_index,
876         sizeof (*ret_history) * num_ds);
877   }
878
879   pthread_mutex_unlock (&cache_lock);
880
881   return (0);
882 } /* int uc_get_history_by_name */
883
884 int uc_get_history (const data_set_t *ds, const value_list_t *vl,
885     gauge_t *ret_history, size_t num_steps, size_t num_ds)
886 {
887   char name[6 * DATA_MAX_NAME_LEN];
888
889   if (FORMAT_VL (name, sizeof (name), vl) != 0)
890   {
891     ERROR ("utils_cache: uc_get_history: FORMAT_VL failed.");
892     return (-1);
893   }
894
895   return (uc_get_history_by_name (name, ret_history, num_steps, num_ds));
896 } /* int uc_get_history */
897
898 /*
899  * Meta data interface
900  */
901 /* XXX: This function will acquire `cache_lock' but will not free it! */
902 static meta_data_t *uc_get_meta (const value_list_t *vl) /* {{{ */
903 {
904   char name[6 * DATA_MAX_NAME_LEN];
905   cache_entry_t *ce = NULL;
906   int status;
907
908   status = FORMAT_VL (name, sizeof (name), vl);
909   if (status != 0)
910   {
911     ERROR ("utils_cache: uc_get_meta: FORMAT_VL failed.");
912     return (NULL);
913   }
914
915   pthread_mutex_lock (&cache_lock);
916
917   status = c_avl_get (cache_tree, name, (void *) &ce);
918   if (status != 0)
919   {
920     pthread_mutex_unlock (&cache_lock);
921     return (NULL);
922   }
923   assert (ce != NULL);
924
925   if (ce->meta == NULL)
926     ce->meta = meta_data_create ();
927
928   return (ce->meta);
929 } /* }}} meta_data_t *uc_get_meta */
930
931 /* Sorry about this preprocessor magic, but it really makes this file much
932  * shorter.. */
933 #define UC_WRAP(wrap_function) { \
934   meta_data_t *meta; \
935   int status; \
936   meta = uc_get_meta (vl); \
937   if (meta == NULL) return (-1); \
938   status = wrap_function (meta, key); \
939   pthread_mutex_unlock (&cache_lock); \
940   return (status); \
941 }
942 int uc_meta_data_exists (const value_list_t *vl, const char *key)
943   UC_WRAP (meta_data_exists)
944
945 int uc_meta_data_delete (const value_list_t *vl, const char *key)
946   UC_WRAP (meta_data_delete)
947 #undef UC_WRAP
948
949 /* We need a new version of this macro because the following functions take
950  * two argumetns. */
951 #define UC_WRAP(wrap_function) { \
952   meta_data_t *meta; \
953   int status; \
954   meta = uc_get_meta (vl); \
955   if (meta == NULL) return (-1); \
956   status = wrap_function (meta, key, value); \
957   pthread_mutex_unlock (&cache_lock); \
958   return (status); \
959 }
960 int uc_meta_data_add_string (const value_list_t *vl,
961     const char *key,
962     const char *value)
963   UC_WRAP(meta_data_add_string)
964 int uc_meta_data_add_signed_int (const value_list_t *vl,
965     const char *key,
966     int64_t value)
967   UC_WRAP(meta_data_add_signed_int)
968 int uc_meta_data_add_unsigned_int (const value_list_t *vl,
969     const char *key,
970     uint64_t value)
971   UC_WRAP(meta_data_add_unsigned_int)
972 int uc_meta_data_add_double (const value_list_t *vl,
973     const char *key,
974     double value)
975   UC_WRAP(meta_data_add_double)
976 int uc_meta_data_add_boolean (const value_list_t *vl,
977     const char *key,
978     _Bool value)
979   UC_WRAP(meta_data_add_boolean)
980
981 int uc_meta_data_get_string (const value_list_t *vl,
982     const char *key,
983     char **value)
984   UC_WRAP(meta_data_get_string)
985 int uc_meta_data_get_signed_int (const value_list_t *vl,
986     const char *key,
987     int64_t *value)
988   UC_WRAP(meta_data_get_signed_int)
989 int uc_meta_data_get_unsigned_int (const value_list_t *vl,
990     const char *key,
991     uint64_t *value)
992   UC_WRAP(meta_data_get_unsigned_int)
993 int uc_meta_data_get_double (const value_list_t *vl,
994     const char *key,
995     double *value)
996   UC_WRAP(meta_data_get_double)
997 int uc_meta_data_get_boolean (const value_list_t *vl,
998     const char *key,
999     _Bool *value)
1000   UC_WRAP(meta_data_get_boolean)
1001 #undef UC_WRAP
1002
1003 /* vim: set sw=2 ts=8 sts=2 tw=78 : */