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