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