2 * collectd - src/utils_cache.c
3 * Copyright (C) 2007-2010 Florian octo Forster
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.
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.
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
19 * Florian octo Forster <octo at collectd.org>
25 #include "utils_avltree.h"
26 #include "utils_cache.h"
27 #include "meta_data.h"
32 typedef struct cache_entry_s
34 char name[6 * DATA_MAX_NAME_LEN];
36 gauge_t *values_gauge;
38 /* Time contained in the package
39 * (for calculating rates) */
41 /* Time according to the local clock
42 * (for purging old entries) */
44 /* Interval in which the data is collected
45 * (for purding old entries) */
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 * +-----------------+-----------------+-----------------+----
60 size_t history_index; /* points to the next position to write to. */
61 size_t history_length;
66 static c_avl_tree_t *cache_tree = NULL;
67 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
69 static int cache_compare (const cache_entry_t *a, const cache_entry_t *b)
71 assert ((a != NULL) && (b != NULL));
72 return (strcmp (a->name, b->name));
73 } /* int cache_compare */
75 static cache_entry_t *cache_alloc (int values_num)
79 ce = (cache_entry_t *) malloc (sizeof (cache_entry_t));
82 ERROR ("utils_cache: cache_alloc: malloc failed.");
85 memset (ce, '\0', sizeof (cache_entry_t));
86 ce->values_num = values_num;
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))
92 sfree (ce->values_gauge);
93 sfree (ce->values_raw);
95 ERROR ("utils_cache: cache_alloc: calloc failed.");
100 ce->history_length = 0;
104 } /* cache_entry_t *cache_alloc */
106 static void cache_free (cache_entry_t *ce)
111 sfree (ce->values_gauge);
112 sfree (ce->values_raw);
114 if (ce->meta != NULL)
116 meta_data_destroy (ce->meta);
120 } /* void cache_free */
122 static void uc_check_range (const data_set_t *ds, cache_entry_t *ce)
126 for (i = 0; i < ds->ds_num; i++)
128 if (isnan (ce->values_gauge[i]))
130 else if (ce->values_gauge[i] < ds->ds[i].min)
131 ce->values_gauge[i] = NAN;
132 else if (ce->values_gauge[i] > ds->ds[i].max)
133 ce->values_gauge[i] = NAN;
135 } /* void uc_check_range */
137 static int uc_insert (const data_set_t *ds, const value_list_t *vl,
144 /* `cache_lock' has been locked by `uc_update' */
146 key_copy = strdup (key);
147 if (key_copy == NULL)
149 ERROR ("uc_insert: strdup failed.");
153 ce = cache_alloc (ds->ds_num);
157 ERROR ("uc_insert: cache_alloc (%i) failed.", ds->ds_num);
161 sstrncpy (ce->name, key, sizeof (ce->name));
163 for (i = 0; i < ds->ds_num; i++)
165 switch (ds->ds[i].type)
167 case DS_TYPE_COUNTER:
168 ce->values_gauge[i] = NAN;
169 ce->values_raw[i].counter = vl->values[i].counter;
173 ce->values_gauge[i] = vl->values[i].gauge;
174 ce->values_raw[i].gauge = vl->values[i].gauge;
178 ce->values_gauge[i] = NAN;
179 ce->values_raw[i].derive = vl->values[i].derive;
182 case DS_TYPE_ABSOLUTE:
183 ce->values_gauge[i] = NAN;
184 if (vl->interval > 0)
185 ce->values_gauge[i] = ((double) vl->values[i].absolute)
186 / CDTIME_T_TO_DOUBLE (vl->interval);
187 ce->values_raw[i].absolute = vl->values[i].absolute;
191 /* This shouldn't happen. */
192 ERROR ("uc_insert: Don't know how to handle data source type %i.",
196 } /* switch (ds->ds[i].type) */
199 /* Prune invalid gauge data */
200 uc_check_range (ds, ce);
202 ce->last_time = vl->time;
203 ce->last_update = cdtime ();
204 ce->interval = vl->interval;
205 ce->state = STATE_OKAY;
207 if (c_avl_insert (cache_tree, key_copy, ce) != 0)
210 ERROR ("uc_insert: c_avl_insert failed.");
214 DEBUG ("uc_insert: Added %s to the cache.", key);
216 } /* int uc_insert */
220 if (cache_tree == NULL)
221 cache_tree = c_avl_create ((int (*) (const void *, const void *))
227 int uc_check_timeout (void)
233 cdtime_t *keys_time = NULL;
234 cdtime_t *keys_interval = NULL;
238 c_avl_iterator_t *iter;
243 pthread_mutex_lock (&cache_lock);
247 /* Build a list of entries to be flushed */
248 iter = c_avl_get_iterator (cache_tree);
249 while (c_avl_iterator_next (iter, (void *) &key, (void *) &ce) == 0)
254 /* If the entry is fresh enough, continue. */
255 if ((now - ce->last_update) < (ce->interval * timeout_g))
258 /* If entry has not been updated, add to `keys' array */
259 tmp = (char **) realloc ((void *) keys,
260 (keys_len + 1) * sizeof (char *));
263 ERROR ("uc_check_timeout: realloc failed.");
268 tmp_time = realloc (keys_time, (keys_len + 1) * sizeof (*keys_time));
269 if (tmp_time == NULL)
271 ERROR ("uc_check_timeout: realloc failed.");
274 keys_time = tmp_time;
276 tmp_time = realloc (keys_interval, (keys_len + 1) * sizeof (*keys_interval));
277 if (tmp_time == NULL)
279 ERROR ("uc_check_timeout: realloc failed.");
282 keys_interval = tmp_time;
284 keys[keys_len] = strdup (key);
285 if (keys[keys_len] == NULL)
287 ERROR ("uc_check_timeout: strdup failed.");
290 keys_time[keys_len] = ce->last_time;
291 keys_interval[keys_len] = ce->interval;
294 } /* while (c_avl_iterator_next) */
296 c_avl_iterator_destroy (iter);
297 pthread_mutex_unlock (&cache_lock);
301 /* realloc() may have been called for these. */
304 sfree (keys_interval);
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++)
315 value_list_t vl = VALUE_LIST_INIT;
321 status = parse_identifier_vl (keys[i], &vl);
324 ERROR ("uc_check_timeout: parse_identifier_vl (\"%s\") failed.", keys[i]);
328 vl.time = keys_time[i];
329 vl.interval = keys_interval[i];
331 plugin_dispatch_missing (&vl);
332 } /* for (i = 0; i < keys_len; i++) */
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++)
343 status = c_avl_remove (cache_tree, keys[i],
344 (void *) &key, (void *) &ce);
347 ERROR ("uc_check_timeout: c_avl_remove (\"%s\") failed.", keys[i]);
355 } /* for (i = 0; i < keys_len; i++) */
356 pthread_mutex_unlock (&cache_lock);
360 sfree (keys_interval);
363 } /* int uc_check_timeout */
365 int uc_update (const data_set_t *ds, const value_list_t *vl)
367 char name[6 * DATA_MAX_NAME_LEN];
368 cache_entry_t *ce = NULL;
372 if (FORMAT_VL (name, sizeof (name), vl) != 0)
374 ERROR ("uc_update: FORMAT_VL failed.");
378 pthread_mutex_lock (&cache_lock);
380 status = c_avl_get (cache_tree, name, (void *) &ce);
381 if (status != 0) /* entry does not yet exist */
383 status = uc_insert (ds, vl, name);
384 pthread_mutex_unlock (&cache_lock);
389 assert (ce->values_num == ds->ds_num);
391 if (ce->last_time >= vl->time)
393 pthread_mutex_unlock (&cache_lock);
394 NOTICE ("uc_update: Value too old: name = %s; value time = %.3f; "
395 "last cache update = %.3f;",
397 CDTIME_T_TO_DOUBLE (vl->time),
398 CDTIME_T_TO_DOUBLE (ce->last_time));
402 for (i = 0; i < ds->ds_num; i++)
404 switch (ds->ds[i].type)
406 case DS_TYPE_COUNTER:
410 /* check if the counter has wrapped around */
411 if (vl->values[i].counter < ce->values_raw[i].counter)
413 if (ce->values_raw[i].counter <= 4294967295U)
414 diff = (4294967295U - ce->values_raw[i].counter)
415 + vl->values[i].counter;
417 diff = (18446744073709551615ULL - ce->values_raw[i].counter)
418 + vl->values[i].counter;
420 else /* counter has NOT wrapped around */
422 diff = vl->values[i].counter - ce->values_raw[i].counter;
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;
432 ce->values_raw[i].gauge = vl->values[i].gauge;
433 ce->values_gauge[i] = vl->values[i].gauge;
440 diff = vl->values[i].derive - ce->values_raw[i].derive;
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;
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;
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.",
460 } /* switch (ds->ds[i].type) */
462 DEBUG ("uc_update: %s: ds[%i] = %lf", name, i, ce->values_gauge[i]);
465 /* Update the history if it exists. */
466 if (ce->history != NULL)
468 assert (ce->history_index < ce->history_length);
469 for (i = 0; i < ce->values_num; i++)
471 size_t hist_idx = (ce->values_num * ce->history_index) + i;
472 ce->history[hist_idx] = ce->values_gauge[i];
475 assert (ce->history_length > 0);
476 ce->history_index = (ce->history_index + 1) % ce->history_length;
479 /* Prune invalid gauge data */
480 uc_check_range (ds, ce);
482 ce->last_time = vl->time;
483 ce->last_update = cdtime ();
484 ce->interval = vl->interval;
486 pthread_mutex_unlock (&cache_lock);
489 } /* int uc_update */
491 int uc_get_rate_by_name (const char *name, gauge_t **ret_values, size_t *ret_values_num)
495 cache_entry_t *ce = NULL;
498 pthread_mutex_lock (&cache_lock);
500 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
504 /* remove missing values from getval */
505 if (ce->state == STATE_MISSING)
511 ret_num = ce->values_num;
512 ret = (gauge_t *) malloc (ret_num * sizeof (gauge_t));
515 ERROR ("utils_cache: uc_get_rate_by_name: malloc failed.");
520 memcpy (ret, ce->values_gauge, ret_num * sizeof (gauge_t));
526 DEBUG ("utils_cache: uc_get_rate_by_name: No such value: %s", name);
530 pthread_mutex_unlock (&cache_lock);
535 *ret_values_num = ret_num;
539 } /* gauge_t *uc_get_rate_by_name */
541 gauge_t *uc_get_rate (const data_set_t *ds, const value_list_t *vl)
543 char name[6 * DATA_MAX_NAME_LEN];
548 if (FORMAT_VL (name, sizeof (name), vl) != 0)
550 ERROR ("utils_cache: uc_get_rate: FORMAT_VL failed.");
554 status = uc_get_rate_by_name (name, &ret, &ret_num);
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)
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);
570 } /* gauge_t *uc_get_rate */
572 int uc_get_names (char ***ret_names, cdtime_t **ret_times, size_t *ret_number)
574 c_avl_iterator_t *iter;
576 cache_entry_t *value;
579 cdtime_t *times = NULL;
581 size_t size_arrays = 0;
585 if ((ret_names == NULL) || (ret_number == NULL))
588 pthread_mutex_lock (&cache_lock);
590 size_arrays = (size_t) c_avl_size (cache_tree);
593 /* Handle the "no values" case here, to avoid the error message when
594 * calloc() returns NULL. */
595 pthread_mutex_unlock (&cache_lock);
599 names = calloc (size_arrays, sizeof (*names));
600 times = calloc (size_arrays, sizeof (*times));
601 if ((names == NULL) || (times == NULL))
603 ERROR ("uc_get_names: calloc failed.");
606 pthread_mutex_unlock (&cache_lock);
610 iter = c_avl_get_iterator (cache_tree);
611 while (c_avl_iterator_next (iter, (void *) &key, (void *) &value) == 0)
613 /* remove missing values when list values */
614 if (value->state == STATE_MISSING)
617 /* c_avl_size does not return a number smaller than the number of elements
618 * returned by c_avl_iterator_next. */
619 assert (number < size_arrays);
621 if (ret_times != NULL)
622 times[number] = value->last_time;
624 names[number] = strdup (key);
625 if (names[number] == NULL)
632 } /* while (c_avl_iterator_next) */
634 c_avl_iterator_destroy (iter);
635 pthread_mutex_unlock (&cache_lock);
641 for (i = 0; i < number; i++)
652 if (ret_times != NULL)
656 *ret_number = number;
659 } /* int uc_get_names */
661 int uc_get_state (const data_set_t *ds, const value_list_t *vl)
663 char name[6 * DATA_MAX_NAME_LEN];
664 cache_entry_t *ce = NULL;
665 int ret = STATE_ERROR;
667 if (FORMAT_VL (name, sizeof (name), vl) != 0)
669 ERROR ("uc_get_state: FORMAT_VL failed.");
670 return (STATE_ERROR);
673 pthread_mutex_lock (&cache_lock);
675 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
681 pthread_mutex_unlock (&cache_lock);
684 } /* int uc_get_state */
686 int uc_set_state (const data_set_t *ds, const value_list_t *vl, int state)
688 char name[6 * DATA_MAX_NAME_LEN];
689 cache_entry_t *ce = NULL;
692 if (FORMAT_VL (name, sizeof (name), vl) != 0)
694 ERROR ("uc_get_state: FORMAT_VL failed.");
695 return (STATE_ERROR);
698 pthread_mutex_lock (&cache_lock);
700 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
707 pthread_mutex_unlock (&cache_lock);
710 } /* int uc_set_state */
712 int uc_get_history_by_name (const char *name,
713 gauge_t *ret_history, size_t num_steps, size_t num_ds)
715 cache_entry_t *ce = NULL;
719 pthread_mutex_lock (&cache_lock);
721 status = c_avl_get (cache_tree, name, (void *) &ce);
724 pthread_mutex_unlock (&cache_lock);
728 if (((size_t) ce->values_num) != num_ds)
730 pthread_mutex_unlock (&cache_lock);
734 /* Check if there are enough values available. If not, increase the buffer
736 if (ce->history_length < num_steps)
741 tmp = realloc (ce->history, sizeof (*ce->history)
742 * num_steps * ce->values_num);
745 pthread_mutex_unlock (&cache_lock);
749 for (i = ce->history_length * ce->values_num;
750 i < (num_steps * ce->values_num);
755 ce->history_length = num_steps;
756 } /* if (ce->history_length < num_steps) */
758 /* Copy the values to the output buffer. */
759 for (i = 0; i < num_steps; i++)
764 if (i < ce->history_index)
765 src_index = ce->history_index - (i + 1);
767 src_index = ce->history_length + ce->history_index - (i + 1);
768 src_index = src_index * num_ds;
770 dst_index = i * num_ds;
772 memcpy (ret_history + dst_index, ce->history + src_index,
773 sizeof (*ret_history) * num_ds);
776 pthread_mutex_unlock (&cache_lock);
779 } /* int uc_get_history_by_name */
781 int uc_get_history (const data_set_t *ds, const value_list_t *vl,
782 gauge_t *ret_history, size_t num_steps, size_t num_ds)
784 char name[6 * DATA_MAX_NAME_LEN];
786 if (FORMAT_VL (name, sizeof (name), vl) != 0)
788 ERROR ("utils_cache: uc_get_history: FORMAT_VL failed.");
792 return (uc_get_history_by_name (name, ret_history, num_steps, num_ds));
793 } /* int uc_get_history */
795 int uc_get_hits (const data_set_t *ds, const value_list_t *vl)
797 char name[6 * DATA_MAX_NAME_LEN];
798 cache_entry_t *ce = NULL;
799 int ret = STATE_ERROR;
801 if (FORMAT_VL (name, sizeof (name), vl) != 0)
803 ERROR ("uc_get_state: FORMAT_VL failed.");
804 return (STATE_ERROR);
807 pthread_mutex_lock (&cache_lock);
809 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
815 pthread_mutex_unlock (&cache_lock);
818 } /* int uc_get_hits */
820 int uc_set_hits (const data_set_t *ds, const value_list_t *vl, int hits)
822 char name[6 * DATA_MAX_NAME_LEN];
823 cache_entry_t *ce = NULL;
826 if (FORMAT_VL (name, sizeof (name), vl) != 0)
828 ERROR ("uc_get_state: FORMAT_VL failed.");
829 return (STATE_ERROR);
832 pthread_mutex_lock (&cache_lock);
834 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
841 pthread_mutex_unlock (&cache_lock);
844 } /* int uc_set_hits */
846 int uc_inc_hits (const data_set_t *ds, const value_list_t *vl, int step)
848 char name[6 * DATA_MAX_NAME_LEN];
849 cache_entry_t *ce = NULL;
852 if (FORMAT_VL (name, sizeof (name), vl) != 0)
854 ERROR ("uc_get_state: FORMAT_VL failed.");
855 return (STATE_ERROR);
858 pthread_mutex_lock (&cache_lock);
860 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
864 ce->hits = ret + step;
867 pthread_mutex_unlock (&cache_lock);
870 } /* int uc_inc_hits */
873 * Meta data interface
875 /* XXX: This function will acquire `cache_lock' but will not free it! */
876 static meta_data_t *uc_get_meta (const value_list_t *vl) /* {{{ */
878 char name[6 * DATA_MAX_NAME_LEN];
879 cache_entry_t *ce = NULL;
882 status = FORMAT_VL (name, sizeof (name), vl);
885 ERROR ("utils_cache: uc_get_meta: FORMAT_VL failed.");
889 pthread_mutex_lock (&cache_lock);
891 status = c_avl_get (cache_tree, name, (void *) &ce);
894 pthread_mutex_unlock (&cache_lock);
899 if (ce->meta == NULL)
900 ce->meta = meta_data_create ();
902 if (ce->meta == NULL)
903 pthread_mutex_unlock (&cache_lock);
906 } /* }}} meta_data_t *uc_get_meta */
908 /* Sorry about this preprocessor magic, but it really makes this file much
910 #define UC_WRAP(wrap_function) { \
913 meta = uc_get_meta (vl); \
914 if (meta == NULL) return (-1); \
915 status = wrap_function (meta, key); \
916 pthread_mutex_unlock (&cache_lock); \
919 int uc_meta_data_exists (const value_list_t *vl, const char *key)
920 UC_WRAP (meta_data_exists)
922 int uc_meta_data_delete (const value_list_t *vl, const char *key)
923 UC_WRAP (meta_data_delete)
926 /* We need a new version of this macro because the following functions take
928 #define UC_WRAP(wrap_function) { \
931 meta = uc_get_meta (vl); \
932 if (meta == NULL) return (-1); \
933 status = wrap_function (meta, key, value); \
934 pthread_mutex_unlock (&cache_lock); \
937 int uc_meta_data_add_string (const value_list_t *vl,
940 UC_WRAP(meta_data_add_string)
941 int uc_meta_data_add_signed_int (const value_list_t *vl,
944 UC_WRAP(meta_data_add_signed_int)
945 int uc_meta_data_add_unsigned_int (const value_list_t *vl,
948 UC_WRAP(meta_data_add_unsigned_int)
949 int uc_meta_data_add_double (const value_list_t *vl,
952 UC_WRAP(meta_data_add_double)
953 int uc_meta_data_add_boolean (const value_list_t *vl,
956 UC_WRAP(meta_data_add_boolean)
958 int uc_meta_data_get_string (const value_list_t *vl,
961 UC_WRAP(meta_data_get_string)
962 int uc_meta_data_get_signed_int (const value_list_t *vl,
965 UC_WRAP(meta_data_get_signed_int)
966 int uc_meta_data_get_unsigned_int (const value_list_t *vl,
969 UC_WRAP(meta_data_get_unsigned_int)
970 int uc_meta_data_get_double (const value_list_t *vl,
973 UC_WRAP(meta_data_get_double)
974 int uc_meta_data_get_boolean (const value_list_t *vl,
977 UC_WRAP(meta_data_get_boolean)
980 /* vim: set sw=2 ts=8 sts=2 tw=78 : */