Merge branch 'collectd-5.4' into collectd-5.5
[collectd.git] / src / daemon / utils_cache.c
1 /**
2  * collectd - src/utils_cache.c
3  * Copyright (C) 2007-2010  Florian octo Forster
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *   Florian octo Forster <octo at collectd.org>
25  **/
26
27 #include "collectd.h"
28 #include "common.h"
29 #include "plugin.h"
30 #include "utils_avltree.h"
31 #include "utils_cache.h"
32 #include "meta_data.h"
33
34 #include <assert.h>
35 #include <pthread.h>
36
37 typedef struct cache_entry_s
38 {
39         char name[6 * DATA_MAX_NAME_LEN];
40         int        values_num;
41         gauge_t   *values_gauge;
42         value_t   *values_raw;
43         /* Time contained in the package
44          * (for calculating rates) */
45         cdtime_t last_time;
46         /* Time according to the local clock
47          * (for purging old entries) */
48         cdtime_t last_update;
49         /* Interval in which the data is collected
50          * (for purding old entries) */
51         cdtime_t interval;
52         int state;
53         int hits;
54
55         /*
56          * +-----+-----+-----+-----+-----+-----+-----+-----+-----+----
57          * !  0  !  1  !  2  !  3  !  4  !  5  !  6  !  7  !  8  ! ...
58          * +-----+-----+-----+-----+-----+-----+-----+-----+-----+----
59          * ! ds0 ! ds1 ! ds2 ! ds0 ! ds1 ! ds2 ! ds0 ! ds1 ! ds2 ! ...
60          * +-----+-----+-----+-----+-----+-----+-----+-----+-----+----
61          * !      t = 0      !      t = 1      !      t = 2      ! ...
62          * +-----------------+-----------------+-----------------+----
63          */
64         gauge_t *history;
65         size_t   history_index; /* points to the next position to write to. */
66         size_t   history_length;
67
68         meta_data_t *meta;
69 } cache_entry_t;
70
71 static c_avl_tree_t   *cache_tree = NULL;
72 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
73
74 static int cache_compare (const cache_entry_t *a, const cache_entry_t *b)
75 {
76 #if COLLECT_DEBUG
77   assert ((a != NULL) && (b != NULL));
78 #endif
79   return (strcmp (a->name, b->name));
80 } /* int cache_compare */
81
82 static cache_entry_t *cache_alloc (int values_num)
83 {
84   cache_entry_t *ce;
85
86   ce = (cache_entry_t *) malloc (sizeof (cache_entry_t));
87   if (ce == NULL)
88   {
89     ERROR ("utils_cache: cache_alloc: malloc failed.");
90     return (NULL);
91   }
92   memset (ce, '\0', sizeof (cache_entry_t));
93   ce->values_num = values_num;
94
95   ce->values_gauge = calloc (values_num, sizeof (*ce->values_gauge));
96   ce->values_raw   = calloc (values_num, sizeof (*ce->values_raw));
97   if ((ce->values_gauge == NULL) || (ce->values_raw == NULL))
98   {
99     sfree (ce->values_gauge);
100     sfree (ce->values_raw);
101     sfree (ce);
102     ERROR ("utils_cache: cache_alloc: calloc failed.");
103     return (NULL);
104   }
105
106   ce->history = NULL;
107   ce->history_length = 0;
108   ce->meta = NULL;
109
110   return (ce);
111 } /* cache_entry_t *cache_alloc */
112
113 static void cache_free (cache_entry_t *ce)
114 {
115   if (ce == NULL)
116     return;
117
118   sfree (ce->values_gauge);
119   sfree (ce->values_raw);
120   sfree (ce->history);
121   if (ce->meta != NULL)
122   {
123     meta_data_destroy (ce->meta);
124     ce->meta = NULL;
125   }
126   sfree (ce);
127 } /* void cache_free */
128
129 static void uc_check_range (const data_set_t *ds, cache_entry_t *ce)
130 {
131   int i;
132
133   for (i = 0; i < ds->ds_num; i++)
134   {
135     if (isnan (ce->values_gauge[i]))
136       continue;
137     else if (ce->values_gauge[i] < ds->ds[i].min)
138       ce->values_gauge[i] = NAN;
139     else if (ce->values_gauge[i] > ds->ds[i].max)
140       ce->values_gauge[i] = NAN;
141   }
142 } /* void uc_check_range */
143
144 static int uc_insert (const data_set_t *ds, const value_list_t *vl,
145     const char *key)
146 {
147   int i;
148   char *key_copy;
149   cache_entry_t *ce;
150
151   /* `cache_lock' has been locked by `uc_update' */
152
153   key_copy = strdup (key);
154   if (key_copy == NULL)
155   {
156     ERROR ("uc_insert: strdup failed.");
157     return (-1);
158   }
159
160   ce = cache_alloc (ds->ds_num);
161   if (ce == NULL)
162   {
163     sfree (key_copy);
164     ERROR ("uc_insert: cache_alloc (%i) failed.", ds->ds_num);
165     return (-1);
166   }
167
168   sstrncpy (ce->name, key, sizeof (ce->name));
169
170   for (i = 0; i < ds->ds_num; i++)
171   {
172     switch (ds->ds[i].type)
173     {
174       case DS_TYPE_COUNTER:
175         ce->values_gauge[i] = NAN;
176         ce->values_raw[i].counter = vl->values[i].counter;
177         break;
178
179       case DS_TYPE_GAUGE:
180         ce->values_gauge[i] = vl->values[i].gauge;
181         ce->values_raw[i].gauge = vl->values[i].gauge;
182         break;
183
184       case DS_TYPE_DERIVE:
185         ce->values_gauge[i] = NAN;
186         ce->values_raw[i].derive = vl->values[i].derive;
187         break;
188
189       case DS_TYPE_ABSOLUTE:
190         ce->values_gauge[i] = NAN;
191         if (vl->interval > 0)
192           ce->values_gauge[i] = ((double) vl->values[i].absolute)
193             / CDTIME_T_TO_DOUBLE (vl->interval);
194         ce->values_raw[i].absolute = vl->values[i].absolute;
195         break;
196
197       default:
198         /* This shouldn't happen. */
199         ERROR ("uc_insert: Don't know how to handle data source type %i.",
200             ds->ds[i].type);
201         sfree (key_copy);
202         cache_free (ce);
203         return (-1);
204     } /* switch (ds->ds[i].type) */
205   } /* for (i) */
206
207   /* Prune invalid gauge data */
208   uc_check_range (ds, ce);
209
210   ce->last_time = vl->time;
211   ce->last_update = cdtime ();
212   ce->interval = vl->interval;
213   ce->state = STATE_OKAY;
214
215   if (c_avl_insert (cache_tree, key_copy, ce) != 0)
216   {
217     sfree (key_copy);
218     ERROR ("uc_insert: c_avl_insert failed.");
219     return (-1);
220   }
221
222   DEBUG ("uc_insert: Added %s to the cache.", key);
223   return (0);
224 } /* int uc_insert */
225
226 int uc_init (void)
227 {
228   if (cache_tree == NULL)
229     cache_tree = c_avl_create ((int (*) (const void *, const void *))
230         cache_compare);
231
232   return (0);
233 } /* int uc_init */
234
235 int uc_check_timeout (void)
236 {
237   cdtime_t now;
238   cache_entry_t *ce;
239
240   char **keys = NULL;
241   cdtime_t *keys_time = NULL;
242   cdtime_t *keys_interval = NULL;
243   int keys_len = 0;
244
245   char *key;
246   c_avl_iterator_t *iter;
247
248   int status;
249   int i;
250   
251   pthread_mutex_lock (&cache_lock);
252
253   now = cdtime ();
254
255   /* Build a list of entries to be flushed */
256   iter = c_avl_get_iterator (cache_tree);
257   while (c_avl_iterator_next (iter, (void *) &key, (void *) &ce) == 0)
258   {
259     char **tmp;
260     cdtime_t *tmp_time;
261
262     /* If the entry is fresh enough, continue. */
263     if ((now - ce->last_update) < (ce->interval * timeout_g))
264       continue;
265
266     /* If entry has not been updated, add to `keys' array */
267     tmp = (char **) realloc ((void *) keys,
268         (keys_len + 1) * sizeof (char *));
269     if (tmp == NULL)
270     {
271       ERROR ("uc_check_timeout: realloc failed.");
272       continue;
273     }
274     keys = tmp;
275
276     tmp_time = realloc (keys_time, (keys_len + 1) * sizeof (*keys_time));
277     if (tmp_time == NULL)
278     {
279       ERROR ("uc_check_timeout: realloc failed.");
280       continue;
281     }
282     keys_time = tmp_time;
283
284     tmp_time = realloc (keys_interval, (keys_len + 1) * sizeof (*keys_interval));
285     if (tmp_time == NULL)
286     {
287       ERROR ("uc_check_timeout: realloc failed.");
288       continue;
289     }
290     keys_interval = tmp_time;
291
292     keys[keys_len] = strdup (key);
293     if (keys[keys_len] == NULL)
294     {
295       ERROR ("uc_check_timeout: strdup failed.");
296       continue;
297     }
298     keys_time[keys_len] = ce->last_time;
299     keys_interval[keys_len] = ce->interval;
300
301     keys_len++;
302   } /* while (c_avl_iterator_next) */
303
304   c_avl_iterator_destroy (iter);
305   pthread_mutex_unlock (&cache_lock);
306
307   if (keys_len == 0)
308   {
309     /* realloc() may have been called for these. */
310     sfree (keys);
311     sfree (keys_time);
312     sfree (keys_interval);
313     return (0);
314   }
315
316   /* Call the "missing" callback for each value. Do this before removing the
317    * value from the cache, so that callbacks can still access the data stored,
318    * including plugin specific meta data, rates, history, â€¦. This must be done
319    * without holding the lock, otherwise we will run into a deadlock if a
320    * plugin calls the cache interface. */
321   for (i = 0; i < keys_len; i++)
322   {
323     value_list_t vl = VALUE_LIST_INIT;
324
325     vl.values = NULL;
326     vl.values_len = 0;
327     vl.meta = NULL;
328
329     status = parse_identifier_vl (keys[i], &vl);
330     if (status != 0)
331     {
332       ERROR ("uc_check_timeout: parse_identifier_vl (\"%s\") failed.", keys[i]);
333       continue;
334     }
335
336     vl.time = keys_time[i];
337     vl.interval = keys_interval[i];
338
339     plugin_dispatch_missing (&vl);
340   } /* for (i = 0; i < keys_len; i++) */
341
342   /* Now actually remove all the values from the cache. We don't re-evaluate
343    * the timestamp again, so in theory it is possible we remove a value after
344    * it is updated here. */
345   pthread_mutex_lock (&cache_lock);
346   for (i = 0; i < keys_len; i++)
347   {
348     key = NULL;
349     ce = NULL;
350
351     status = c_avl_remove (cache_tree, keys[i],
352         (void *) &key, (void *) &ce);
353     if (status != 0)
354     {
355       ERROR ("uc_check_timeout: c_avl_remove (\"%s\") failed.", keys[i]);
356       sfree (keys[i]);
357       continue;
358     }
359
360     sfree (keys[i]);
361     sfree (key);
362     cache_free (ce);
363   } /* for (i = 0; i < keys_len; i++) */
364   pthread_mutex_unlock (&cache_lock);
365
366   sfree (keys);
367   sfree (keys_time);
368   sfree (keys_interval);
369
370   return (0);
371 } /* int uc_check_timeout */
372
373 int uc_update (const data_set_t *ds, const value_list_t *vl)
374 {
375   char name[6 * DATA_MAX_NAME_LEN];
376   cache_entry_t *ce = NULL;
377   int status;
378   int i;
379
380   if (FORMAT_VL (name, sizeof (name), vl) != 0)
381   {
382     ERROR ("uc_update: FORMAT_VL failed.");
383     return (-1);
384   }
385
386   pthread_mutex_lock (&cache_lock);
387
388   status = c_avl_get (cache_tree, name, (void *) &ce);
389   if (status != 0) /* entry does not yet exist */
390   {
391     status = uc_insert (ds, vl, name);
392     pthread_mutex_unlock (&cache_lock);
393     return (status);
394   }
395
396   assert (ce != NULL);
397   assert (ce->values_num == ds->ds_num);
398
399   if (ce->last_time >= vl->time)
400   {
401     pthread_mutex_unlock (&cache_lock);
402     NOTICE ("uc_update: Value too old: name = %s; value time = %.3f; "
403         "last cache update = %.3f;",
404         name,
405         CDTIME_T_TO_DOUBLE (vl->time),
406         CDTIME_T_TO_DOUBLE (ce->last_time));
407     return (-1);
408   }
409
410   for (i = 0; i < ds->ds_num; i++)
411   {
412     switch (ds->ds[i].type)
413     {
414       case DS_TYPE_COUNTER:
415         {
416           counter_t diff;
417
418           /* check if the counter has wrapped around */
419           if (vl->values[i].counter < ce->values_raw[i].counter)
420           {
421             if (ce->values_raw[i].counter <= 4294967295U)
422               diff = (4294967295U - ce->values_raw[i].counter)
423                 + vl->values[i].counter;
424             else
425               diff = (18446744073709551615ULL - ce->values_raw[i].counter)
426                 + vl->values[i].counter;
427           }
428           else /* counter has NOT wrapped around */
429           {
430             diff = vl->values[i].counter - ce->values_raw[i].counter;
431           }
432
433           ce->values_gauge[i] = ((double) diff)
434             / (CDTIME_T_TO_DOUBLE (vl->time - ce->last_time));
435           ce->values_raw[i].counter = vl->values[i].counter;
436         }
437         break;
438
439       case DS_TYPE_GAUGE:
440         ce->values_raw[i].gauge = vl->values[i].gauge;
441         ce->values_gauge[i] = vl->values[i].gauge;
442         break;
443
444       case DS_TYPE_DERIVE:
445         {
446           derive_t diff;
447
448           diff = vl->values[i].derive - ce->values_raw[i].derive;
449
450           ce->values_gauge[i] = ((double) diff)
451             / (CDTIME_T_TO_DOUBLE (vl->time - ce->last_time));
452           ce->values_raw[i].derive = vl->values[i].derive;
453         }
454         break;
455
456       case DS_TYPE_ABSOLUTE:
457         ce->values_gauge[i] = ((double) vl->values[i].absolute)
458           / (CDTIME_T_TO_DOUBLE (vl->time - ce->last_time));
459         ce->values_raw[i].absolute = vl->values[i].absolute;
460         break;
461
462       default:
463         /* This shouldn't happen. */
464         pthread_mutex_unlock (&cache_lock);
465         ERROR ("uc_update: Don't know how to handle data source type %i.",
466             ds->ds[i].type);
467         return (-1);
468     } /* switch (ds->ds[i].type) */
469
470     DEBUG ("uc_update: %s: ds[%i] = %lf", name, i, ce->values_gauge[i]);
471   } /* for (i) */
472
473   /* Update the history if it exists. */
474   if (ce->history != NULL)
475   {
476     assert (ce->history_index < ce->history_length);
477     for (i = 0; i < ce->values_num; i++)
478     {
479       size_t hist_idx = (ce->values_num * ce->history_index) + i;
480       ce->history[hist_idx] = ce->values_gauge[i];
481     }
482
483     assert (ce->history_length > 0);
484     ce->history_index = (ce->history_index + 1) % ce->history_length;
485   }
486
487   /* Prune invalid gauge data */
488   uc_check_range (ds, ce);
489
490   ce->last_time = vl->time;
491   ce->last_update = cdtime ();
492   ce->interval = vl->interval;
493
494   pthread_mutex_unlock (&cache_lock);
495
496   return (0);
497 } /* int uc_update */
498
499 int uc_get_rate_by_name (const char *name, gauge_t **ret_values, size_t *ret_values_num)
500 {
501   gauge_t *ret = NULL;
502   size_t ret_num = 0;
503   cache_entry_t *ce = NULL;
504   int status = 0;
505
506   pthread_mutex_lock (&cache_lock);
507
508   if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
509   {
510     assert (ce != NULL);
511
512     /* remove missing values from getval */
513     if (ce->state == STATE_MISSING)
514     {
515       status = -1;
516     }
517     else
518     {
519       ret_num = ce->values_num;
520       ret = (gauge_t *) malloc (ret_num * sizeof (gauge_t));
521       if (ret == NULL)
522       {
523         ERROR ("utils_cache: uc_get_rate_by_name: malloc failed.");
524         status = -1;
525       }
526       else
527       {
528         memcpy (ret, ce->values_gauge, ret_num * sizeof (gauge_t));
529       }
530     }
531   }
532   else
533   {
534     DEBUG ("utils_cache: uc_get_rate_by_name: No such value: %s", name);
535     status = -1;
536   }
537
538   pthread_mutex_unlock (&cache_lock);
539
540   if (status == 0)
541   {
542     *ret_values = ret;
543     *ret_values_num = ret_num;
544   }
545
546   return (status);
547 } /* gauge_t *uc_get_rate_by_name */
548
549 gauge_t *uc_get_rate (const data_set_t *ds, const value_list_t *vl)
550 {
551   char name[6 * DATA_MAX_NAME_LEN];
552   gauge_t *ret = NULL;
553   size_t ret_num = 0;
554   int status;
555
556   if (FORMAT_VL (name, sizeof (name), vl) != 0)
557   {
558     ERROR ("utils_cache: uc_get_rate: FORMAT_VL failed.");
559     return (NULL);
560   }
561
562   status = uc_get_rate_by_name (name, &ret, &ret_num);
563   if (status != 0)
564     return (NULL);
565
566   /* This is important - the caller has no other way of knowing how many
567    * values are returned. */
568   if (ret_num != (size_t) ds->ds_num)
569   {
570     ERROR ("utils_cache: uc_get_rate: ds[%s] has %i values, "
571         "but uc_get_rate_by_name returned %zu.",
572         ds->type, ds->ds_num, ret_num);
573     sfree (ret);
574     return (NULL);
575   }
576
577   return (ret);
578 } /* gauge_t *uc_get_rate */
579
580 size_t uc_get_size (void) {
581   size_t size_arrays = 0;
582
583   pthread_mutex_lock (&cache_lock);
584   size_arrays = (size_t) c_avl_size (cache_tree);
585   pthread_mutex_unlock (&cache_lock);
586
587   return (size_arrays);
588 }
589
590 int uc_get_names (char ***ret_names, cdtime_t **ret_times, size_t *ret_number)
591 {
592   c_avl_iterator_t *iter;
593   char *key;
594   cache_entry_t *value;
595
596   char **names = NULL;
597   cdtime_t *times = NULL;
598   size_t number = 0;
599   size_t size_arrays = 0;
600
601   int status = 0;
602
603   if ((ret_names == NULL) || (ret_number == NULL))
604     return (-1);
605
606   pthread_mutex_lock (&cache_lock);
607
608   size_arrays = (size_t) c_avl_size (cache_tree);
609   if (size_arrays < 1)
610   {
611     /* Handle the "no values" case here, to avoid the error message when
612      * calloc() returns NULL. */
613     pthread_mutex_unlock (&cache_lock);
614     return (0);
615   }
616
617   names = calloc (size_arrays, sizeof (*names));
618   times = calloc (size_arrays, sizeof (*times));
619   if ((names == NULL) || (times == NULL))
620   {
621     ERROR ("uc_get_names: calloc failed.");
622     sfree (names);
623     sfree (times);
624     pthread_mutex_unlock (&cache_lock);
625     return (ENOMEM);
626   }
627
628   iter = c_avl_get_iterator (cache_tree);
629   while (c_avl_iterator_next (iter, (void *) &key, (void *) &value) == 0)
630   {
631     /* remove missing values when list values */
632     if (value->state == STATE_MISSING)
633       continue;
634
635     /* c_avl_size does not return a number smaller than the number of elements
636      * returned by c_avl_iterator_next. */
637     assert (number < size_arrays);
638
639     if (ret_times != NULL)
640       times[number] = value->last_time;
641
642     names[number] = strdup (key);
643     if (names[number] == NULL)
644     {
645       status = -1;
646       break;
647     }
648
649     number++;
650   } /* while (c_avl_iterator_next) */
651
652   c_avl_iterator_destroy (iter);
653   pthread_mutex_unlock (&cache_lock);
654
655   if (status != 0)
656   {
657     size_t i;
658
659     for (i = 0; i < number; i++)
660     {
661       sfree (names[i]);
662     }
663     sfree (names);
664     sfree (times);
665
666     return (-1);
667   }
668
669   *ret_names = names;
670   if (ret_times != NULL)
671     *ret_times = times;
672   else
673     sfree (times);
674   *ret_number = number;
675
676   return (0);
677 } /* int uc_get_names */
678
679 int uc_get_state (const data_set_t *ds, const value_list_t *vl)
680 {
681   char name[6 * DATA_MAX_NAME_LEN];
682   cache_entry_t *ce = NULL;
683   int ret = STATE_ERROR;
684
685   if (FORMAT_VL (name, sizeof (name), vl) != 0)
686   {
687     ERROR ("uc_get_state: FORMAT_VL failed.");
688     return (STATE_ERROR);
689   }
690
691   pthread_mutex_lock (&cache_lock);
692
693   if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
694   {
695     assert (ce != NULL);
696     ret = ce->state;
697   }
698
699   pthread_mutex_unlock (&cache_lock);
700
701   return (ret);
702 } /* int uc_get_state */
703
704 int uc_set_state (const data_set_t *ds, const value_list_t *vl, int state)
705 {
706   char name[6 * DATA_MAX_NAME_LEN];
707   cache_entry_t *ce = NULL;
708   int ret = -1;
709
710   if (FORMAT_VL (name, sizeof (name), vl) != 0)
711   {
712     ERROR ("uc_set_state: FORMAT_VL failed.");
713     return (STATE_ERROR);
714   }
715
716   pthread_mutex_lock (&cache_lock);
717
718   if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
719   {
720     assert (ce != NULL);
721     ret = ce->state;
722     ce->state = state;
723   }
724
725   pthread_mutex_unlock (&cache_lock);
726
727   return (ret);
728 } /* int uc_set_state */
729
730 int uc_get_history_by_name (const char *name,
731     gauge_t *ret_history, size_t num_steps, size_t num_ds)
732 {
733   cache_entry_t *ce = NULL;
734   size_t i;
735   int status = 0;
736
737   pthread_mutex_lock (&cache_lock);
738
739   status = c_avl_get (cache_tree, name, (void *) &ce);
740   if (status != 0)
741   {
742     pthread_mutex_unlock (&cache_lock);
743     return (-ENOENT);
744   }
745
746   if (((size_t) ce->values_num) != num_ds)
747   {
748     pthread_mutex_unlock (&cache_lock);
749     return (-EINVAL);
750   }
751
752   /* Check if there are enough values available. If not, increase the buffer
753    * size. */
754   if (ce->history_length < num_steps)
755   {
756     gauge_t *tmp;
757
758     tmp = realloc (ce->history, sizeof (*ce->history)
759         * num_steps * ce->values_num);
760     if (tmp == NULL)
761     {
762       pthread_mutex_unlock (&cache_lock);
763       return (-ENOMEM);
764     }
765
766     for (i = ce->history_length * ce->values_num;
767         i < (num_steps * ce->values_num);
768         i++)
769       tmp[i] = NAN;
770
771     ce->history = tmp;
772     ce->history_length = num_steps;
773   } /* if (ce->history_length < num_steps) */
774
775   /* Copy the values to the output buffer. */
776   for (i = 0; i < num_steps; i++)
777   {
778     size_t src_index;
779     size_t dst_index;
780
781     if (i < ce->history_index)
782       src_index = ce->history_index - (i + 1);
783     else
784       src_index = ce->history_length + ce->history_index - (i + 1);
785     src_index = src_index * num_ds;
786
787     dst_index = i * num_ds;
788
789     memcpy (ret_history + dst_index, ce->history + src_index,
790         sizeof (*ret_history) * num_ds);
791   }
792
793   pthread_mutex_unlock (&cache_lock);
794
795   return (0);
796 } /* int uc_get_history_by_name */
797
798 int uc_get_history (const data_set_t *ds, const value_list_t *vl,
799     gauge_t *ret_history, size_t num_steps, size_t num_ds)
800 {
801   char name[6 * DATA_MAX_NAME_LEN];
802
803   if (FORMAT_VL (name, sizeof (name), vl) != 0)
804   {
805     ERROR ("utils_cache: uc_get_history: FORMAT_VL failed.");
806     return (-1);
807   }
808
809   return (uc_get_history_by_name (name, ret_history, num_steps, num_ds));
810 } /* int uc_get_history */
811
812 int uc_get_hits (const data_set_t *ds, const value_list_t *vl)
813 {
814   char name[6 * DATA_MAX_NAME_LEN];
815   cache_entry_t *ce = NULL;
816   int ret = STATE_ERROR;
817
818   if (FORMAT_VL (name, sizeof (name), vl) != 0)
819   {
820     ERROR ("uc_get_hits: FORMAT_VL failed.");
821     return (STATE_ERROR);
822   }
823
824   pthread_mutex_lock (&cache_lock);
825
826   if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
827   {
828     assert (ce != NULL);
829     ret = ce->hits;
830   }
831
832   pthread_mutex_unlock (&cache_lock);
833
834   return (ret);
835 } /* int uc_get_hits */
836
837 int uc_set_hits (const data_set_t *ds, const value_list_t *vl, int hits)
838 {
839   char name[6 * DATA_MAX_NAME_LEN];
840   cache_entry_t *ce = NULL;
841   int ret = -1;
842
843   if (FORMAT_VL (name, sizeof (name), vl) != 0)
844   {
845     ERROR ("uc_set_hits: FORMAT_VL failed.");
846     return (STATE_ERROR);
847   }
848
849   pthread_mutex_lock (&cache_lock);
850
851   if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
852   {
853     assert (ce != NULL);
854     ret = ce->hits;
855     ce->hits = hits;
856   }
857
858   pthread_mutex_unlock (&cache_lock);
859
860   return (ret);
861 } /* int uc_set_hits */
862
863 int uc_inc_hits (const data_set_t *ds, const value_list_t *vl, int step)
864 {
865   char name[6 * DATA_MAX_NAME_LEN];
866   cache_entry_t *ce = NULL;
867   int ret = -1;
868
869   if (FORMAT_VL (name, sizeof (name), vl) != 0)
870   {
871     ERROR ("uc_inc_hits: FORMAT_VL failed.");
872     return (STATE_ERROR);
873   }
874
875   pthread_mutex_lock (&cache_lock);
876
877   if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
878   {
879     assert (ce != NULL);
880     ret = ce->hits;
881     ce->hits = ret + step;
882   }
883
884   pthread_mutex_unlock (&cache_lock);
885
886   return (ret);
887 } /* int uc_inc_hits */
888
889 /*
890  * Meta data interface
891  */
892 /* XXX: This function will acquire `cache_lock' but will not free it! */
893 static meta_data_t *uc_get_meta (const value_list_t *vl) /* {{{ */
894 {
895   char name[6 * DATA_MAX_NAME_LEN];
896   cache_entry_t *ce = NULL;
897   int status;
898
899   status = FORMAT_VL (name, sizeof (name), vl);
900   if (status != 0)
901   {
902     ERROR ("utils_cache: uc_get_meta: FORMAT_VL failed.");
903     return (NULL);
904   }
905
906   pthread_mutex_lock (&cache_lock);
907
908   status = c_avl_get (cache_tree, name, (void *) &ce);
909   if (status != 0)
910   {
911     pthread_mutex_unlock (&cache_lock);
912     return (NULL);
913   }
914   assert (ce != NULL);
915
916   if (ce->meta == NULL)
917     ce->meta = meta_data_create ();
918
919   if (ce->meta == NULL)
920     pthread_mutex_unlock (&cache_lock);
921
922   return (ce->meta);
923 } /* }}} meta_data_t *uc_get_meta */
924
925 /* Sorry about this preprocessor magic, but it really makes this file much
926  * shorter.. */
927 #define UC_WRAP(wrap_function) { \
928   meta_data_t *meta; \
929   int status; \
930   meta = uc_get_meta (vl); \
931   if (meta == NULL) return (-1); \
932   status = wrap_function (meta, key); \
933   pthread_mutex_unlock (&cache_lock); \
934   return (status); \
935 }
936 int uc_meta_data_exists (const value_list_t *vl, const char *key)
937   UC_WRAP (meta_data_exists)
938
939 int uc_meta_data_delete (const value_list_t *vl, const char *key)
940   UC_WRAP (meta_data_delete)
941 #undef UC_WRAP
942
943 /* We need a new version of this macro because the following functions take
944  * two argumetns. */
945 #define UC_WRAP(wrap_function) { \
946   meta_data_t *meta; \
947   int status; \
948   meta = uc_get_meta (vl); \
949   if (meta == NULL) return (-1); \
950   status = wrap_function (meta, key, value); \
951   pthread_mutex_unlock (&cache_lock); \
952   return (status); \
953 }
954 int uc_meta_data_add_string (const value_list_t *vl,
955     const char *key,
956     const char *value)
957   UC_WRAP(meta_data_add_string)
958 int uc_meta_data_add_signed_int (const value_list_t *vl,
959     const char *key,
960     int64_t value)
961   UC_WRAP(meta_data_add_signed_int)
962 int uc_meta_data_add_unsigned_int (const value_list_t *vl,
963     const char *key,
964     uint64_t value)
965   UC_WRAP(meta_data_add_unsigned_int)
966 int uc_meta_data_add_double (const value_list_t *vl,
967     const char *key,
968     double value)
969   UC_WRAP(meta_data_add_double)
970 int uc_meta_data_add_boolean (const value_list_t *vl,
971     const char *key,
972     _Bool value)
973   UC_WRAP(meta_data_add_boolean)
974
975 int uc_meta_data_get_string (const value_list_t *vl,
976     const char *key,
977     char **value)
978   UC_WRAP(meta_data_get_string)
979 int uc_meta_data_get_signed_int (const value_list_t *vl,
980     const char *key,
981     int64_t *value)
982   UC_WRAP(meta_data_get_signed_int)
983 int uc_meta_data_get_unsigned_int (const value_list_t *vl,
984     const char *key,
985     uint64_t *value)
986   UC_WRAP(meta_data_get_unsigned_int)
987 int uc_meta_data_get_double (const value_list_t *vl,
988     const char *key,
989     double *value)
990   UC_WRAP(meta_data_get_double)
991 int uc_meta_data_get_boolean (const value_list_t *vl,
992     const char *key,
993     _Bool *value)
994   UC_WRAP(meta_data_get_boolean)
995 #undef UC_WRAP
996
997 /* vim: set sw=2 ts=8 sts=2 tw=78 : */