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