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