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)
72 assert ((a != NULL) && (b != NULL));
74 return (strcmp (a->name, b->name));
75 } /* int cache_compare */
77 static cache_entry_t *cache_alloc (int values_num)
81 ce = (cache_entry_t *) malloc (sizeof (cache_entry_t));
84 ERROR ("utils_cache: cache_alloc: malloc failed.");
87 memset (ce, '\0', sizeof (cache_entry_t));
88 ce->values_num = values_num;
90 ce->values_gauge = calloc (values_num, sizeof (*ce->values_gauge));
91 ce->values_raw = calloc (values_num, sizeof (*ce->values_raw));
92 if ((ce->values_gauge == NULL) || (ce->values_raw == NULL))
94 sfree (ce->values_gauge);
95 sfree (ce->values_raw);
97 ERROR ("utils_cache: cache_alloc: calloc failed.");
102 ce->history_length = 0;
106 } /* cache_entry_t *cache_alloc */
108 static void cache_free (cache_entry_t *ce)
113 sfree (ce->values_gauge);
114 sfree (ce->values_raw);
116 if (ce->meta != NULL)
118 meta_data_destroy (ce->meta);
122 } /* void cache_free */
124 static void uc_check_range (const data_set_t *ds, cache_entry_t *ce)
128 for (i = 0; i < ds->ds_num; i++)
130 if (isnan (ce->values_gauge[i]))
132 else if (ce->values_gauge[i] < ds->ds[i].min)
133 ce->values_gauge[i] = NAN;
134 else if (ce->values_gauge[i] > ds->ds[i].max)
135 ce->values_gauge[i] = NAN;
137 } /* void uc_check_range */
139 static int uc_insert (const data_set_t *ds, const value_list_t *vl,
146 /* `cache_lock' has been locked by `uc_update' */
148 key_copy = strdup (key);
149 if (key_copy == NULL)
151 ERROR ("uc_insert: strdup failed.");
155 ce = cache_alloc (ds->ds_num);
159 ERROR ("uc_insert: cache_alloc (%i) failed.", ds->ds_num);
163 sstrncpy (ce->name, key, sizeof (ce->name));
165 for (i = 0; i < ds->ds_num; i++)
167 switch (ds->ds[i].type)
169 case DS_TYPE_COUNTER:
170 ce->values_gauge[i] = NAN;
171 ce->values_raw[i].counter = vl->values[i].counter;
175 ce->values_gauge[i] = vl->values[i].gauge;
176 ce->values_raw[i].gauge = vl->values[i].gauge;
180 ce->values_gauge[i] = NAN;
181 ce->values_raw[i].derive = vl->values[i].derive;
184 case DS_TYPE_ABSOLUTE:
185 ce->values_gauge[i] = NAN;
186 if (vl->interval > 0)
187 ce->values_gauge[i] = ((double) vl->values[i].absolute)
188 / CDTIME_T_TO_DOUBLE (vl->interval);
189 ce->values_raw[i].absolute = vl->values[i].absolute;
193 /* This shouldn't happen. */
194 ERROR ("uc_insert: Don't know how to handle data source type %i.",
197 } /* switch (ds->ds[i].type) */
200 /* Prune invalid gauge data */
201 uc_check_range (ds, ce);
203 ce->last_time = vl->time;
204 ce->last_update = cdtime ();
205 ce->interval = vl->interval;
206 ce->state = STATE_OKAY;
208 if (c_avl_insert (cache_tree, key_copy, ce) != 0)
211 ERROR ("uc_insert: c_avl_insert failed.");
215 DEBUG ("uc_insert: Added %s to the cache.", key);
217 } /* int uc_insert */
221 if (cache_tree == NULL)
222 cache_tree = c_avl_create ((int (*) (const void *, const void *))
228 int uc_check_timeout (void)
234 cdtime_t *keys_time = NULL;
235 cdtime_t *keys_interval = NULL;
239 c_avl_iterator_t *iter;
244 pthread_mutex_lock (&cache_lock);
248 /* Build a list of entries to be flushed */
249 iter = c_avl_get_iterator (cache_tree);
250 while (c_avl_iterator_next (iter, (void *) &key, (void *) &ce) == 0)
255 /* If the entry is fresh enough, continue. */
256 if ((now - ce->last_update) < (ce->interval * timeout_g))
259 /* If entry has not been updated, add to `keys' array */
260 tmp = (char **) realloc ((void *) keys,
261 (keys_len + 1) * sizeof (char *));
264 ERROR ("uc_check_timeout: realloc failed.");
269 tmp_time = realloc (keys_time, (keys_len + 1) * sizeof (*keys_time));
270 if (tmp_time == NULL)
272 ERROR ("uc_check_timeout: realloc failed.");
275 keys_time = tmp_time;
277 tmp_time = realloc (keys_interval, (keys_len + 1) * sizeof (*keys_interval));
278 if (tmp_time == NULL)
280 ERROR ("uc_check_timeout: realloc failed.");
283 keys_interval = tmp_time;
285 keys[keys_len] = strdup (key);
286 if (keys[keys_len] == NULL)
288 ERROR ("uc_check_timeout: strdup failed.");
291 keys_time[keys_len] = ce->last_time;
292 keys_interval[keys_len] = ce->interval;
295 } /* while (c_avl_iterator_next) */
297 c_avl_iterator_destroy (iter);
298 pthread_mutex_unlock (&cache_lock);
303 /* Call the "missing" callback for each value. Do this before removing the
304 * value from the cache, so that callbacks can still access the data stored,
305 * including plugin specific meta data, rates, history, …. This must be done
306 * without holding the lock, otherwise we will run into a deadlock if a
307 * plugin calls the cache interface. */
308 for (i = 0; i < keys_len; i++)
310 value_list_t vl = VALUE_LIST_INIT;
316 status = parse_identifier_vl (keys[i], &vl);
319 ERROR ("uc_check_timeout: parse_identifier_vl (\"%s\") failed.", keys[i]);
324 vl.time = keys_time[i];
325 vl.interval = keys_interval[i];
327 plugin_dispatch_missing (&vl);
328 } /* for (i = 0; i < keys_len; i++) */
330 /* Now actually remove all the values from the cache. We don't re-evaluate
331 * the timestamp again, so in theory it is possible we remove a value after
332 * it is updated here. */
333 pthread_mutex_lock (&cache_lock);
334 for (i = 0; i < keys_len; i++)
339 status = c_avl_remove (cache_tree, keys[i],
340 (void *) &key, (void *) &ce);
343 ERROR ("uc_check_timeout: c_avl_remove (\"%s\") failed.", keys[i]);
351 } /* for (i = 0; i < keys_len; i++) */
352 pthread_mutex_unlock (&cache_lock);
356 sfree (keys_interval);
359 } /* int uc_check_timeout */
361 int uc_update (const data_set_t *ds, const value_list_t *vl)
363 char name[6 * DATA_MAX_NAME_LEN];
364 cache_entry_t *ce = NULL;
368 if (FORMAT_VL (name, sizeof (name), vl) != 0)
370 ERROR ("uc_update: FORMAT_VL failed.");
374 pthread_mutex_lock (&cache_lock);
376 status = c_avl_get (cache_tree, name, (void *) &ce);
377 if (status != 0) /* entry does not yet exist */
379 status = uc_insert (ds, vl, name);
380 pthread_mutex_unlock (&cache_lock);
385 assert (ce->values_num == ds->ds_num);
387 if (ce->last_time >= vl->time)
389 pthread_mutex_unlock (&cache_lock);
390 NOTICE ("uc_update: Value too old: name = %s; value time = %.3f; "
391 "last cache update = %.3f;",
393 CDTIME_T_TO_DOUBLE (vl->time),
394 CDTIME_T_TO_DOUBLE (ce->last_time));
398 for (i = 0; i < ds->ds_num; i++)
400 switch (ds->ds[i].type)
402 case DS_TYPE_COUNTER:
406 /* check if the counter has wrapped around */
407 if (vl->values[i].counter < ce->values_raw[i].counter)
409 if (ce->values_raw[i].counter <= 4294967295U)
410 diff = (4294967295U - ce->values_raw[i].counter)
411 + vl->values[i].counter;
413 diff = (18446744073709551615ULL - ce->values_raw[i].counter)
414 + vl->values[i].counter;
416 else /* counter has NOT wrapped around */
418 diff = vl->values[i].counter - ce->values_raw[i].counter;
421 ce->values_gauge[i] = ((double) diff)
422 / (CDTIME_T_TO_DOUBLE (vl->time - ce->last_time));
423 ce->values_raw[i].counter = vl->values[i].counter;
428 ce->values_raw[i].gauge = vl->values[i].gauge;
429 ce->values_gauge[i] = vl->values[i].gauge;
436 diff = vl->values[i].derive - ce->values_raw[i].derive;
438 ce->values_gauge[i] = ((double) diff)
439 / (CDTIME_T_TO_DOUBLE (vl->time - ce->last_time));
440 ce->values_raw[i].derive = vl->values[i].derive;
444 case DS_TYPE_ABSOLUTE:
445 ce->values_gauge[i] = ((double) vl->values[i].absolute)
446 / (CDTIME_T_TO_DOUBLE (vl->time - ce->last_time));
447 ce->values_raw[i].absolute = vl->values[i].absolute;
451 /* This shouldn't happen. */
452 pthread_mutex_unlock (&cache_lock);
453 ERROR ("uc_update: Don't know how to handle data source type %i.",
456 } /* switch (ds->ds[i].type) */
458 DEBUG ("uc_update: %s: ds[%i] = %lf", name, i, ce->values_gauge[i]);
461 /* Update the history if it exists. */
462 if (ce->history != NULL)
464 assert (ce->history_index < ce->history_length);
465 for (i = 0; i < ce->values_num; i++)
467 size_t hist_idx = (ce->values_num * ce->history_index) + i;
468 ce->history[hist_idx] = ce->values_gauge[i];
471 assert (ce->history_length > 0);
472 ce->history_index = (ce->history_index + 1) % ce->history_length;
475 /* Prune invalid gauge data */
476 uc_check_range (ds, ce);
478 ce->last_time = vl->time;
479 ce->last_update = cdtime ();
480 ce->interval = vl->interval;
482 pthread_mutex_unlock (&cache_lock);
485 } /* int uc_update */
487 int uc_get_rate_by_name (const char *name, gauge_t **ret_values, size_t *ret_values_num)
491 cache_entry_t *ce = NULL;
494 pthread_mutex_lock (&cache_lock);
496 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
500 /* remove missing values from getval */
501 if (ce->state == STATE_MISSING)
507 ret_num = ce->values_num;
508 ret = (gauge_t *) malloc (ret_num * sizeof (gauge_t));
511 ERROR ("utils_cache: uc_get_rate_by_name: malloc failed.");
516 memcpy (ret, ce->values_gauge, ret_num * sizeof (gauge_t));
522 DEBUG ("utils_cache: uc_get_rate_by_name: No such value: %s", name);
526 pthread_mutex_unlock (&cache_lock);
531 *ret_values_num = ret_num;
535 } /* gauge_t *uc_get_rate_by_name */
537 gauge_t *uc_get_rate (const data_set_t *ds, const value_list_t *vl)
539 char name[6 * DATA_MAX_NAME_LEN];
544 if (FORMAT_VL (name, sizeof (name), vl) != 0)
546 ERROR ("utils_cache: uc_get_rate: FORMAT_VL failed.");
550 status = uc_get_rate_by_name (name, &ret, &ret_num);
554 /* This is important - the caller has no other way of knowing how many
555 * values are returned. */
556 if (ret_num != (size_t) ds->ds_num)
558 ERROR ("utils_cache: uc_get_rate: ds[%s] has %i values, "
559 "but uc_get_rate_by_name returned %zu.",
560 ds->type, ds->ds_num, ret_num);
566 } /* gauge_t *uc_get_rate */
568 int uc_get_names (char ***ret_names, cdtime_t **ret_times, size_t *ret_number)
570 c_avl_iterator_t *iter;
572 cache_entry_t *value;
575 cdtime_t *times = NULL;
577 size_t size_arrays = 0;
581 if ((ret_names == NULL) || (ret_number == NULL))
584 pthread_mutex_lock (&cache_lock);
586 size_arrays = (size_t) c_avl_size (cache_tree);
589 /* Handle the "no values" case here, to avoid the error message when
590 * calloc() returns NULL. */
591 pthread_mutex_unlock (&cache_lock);
595 names = calloc (size_arrays, sizeof (*names));
596 times = calloc (size_arrays, sizeof (*times));
597 if ((names == NULL) || (times == NULL))
599 ERROR ("uc_get_names: calloc failed.");
602 pthread_mutex_unlock (&cache_lock);
606 iter = c_avl_get_iterator (cache_tree);
607 while (c_avl_iterator_next (iter, (void *) &key, (void *) &value) == 0)
609 /* remove missing values when list values */
610 if (value->state == STATE_MISSING)
613 /* c_avl_size does not return a number smaller than the number of elements
614 * returned by c_avl_iterator_next. */
615 assert (number < size_arrays);
617 if (ret_times != NULL)
618 times[number] = value->last_time;
620 names[number] = strdup (key);
621 if (names[number] == NULL)
628 } /* while (c_avl_iterator_next) */
630 c_avl_iterator_destroy (iter);
631 pthread_mutex_unlock (&cache_lock);
637 for (i = 0; i < number; i++)
647 if (ret_times != NULL)
649 *ret_number = number;
652 } /* int uc_get_names */
654 int uc_get_state (const data_set_t *ds, const value_list_t *vl)
656 char name[6 * DATA_MAX_NAME_LEN];
657 cache_entry_t *ce = NULL;
658 int ret = STATE_ERROR;
660 if (FORMAT_VL (name, sizeof (name), vl) != 0)
662 ERROR ("uc_get_state: FORMAT_VL failed.");
663 return (STATE_ERROR);
666 pthread_mutex_lock (&cache_lock);
668 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
674 pthread_mutex_unlock (&cache_lock);
677 } /* int uc_get_state */
679 int uc_set_state (const data_set_t *ds, const value_list_t *vl, int state)
681 char name[6 * DATA_MAX_NAME_LEN];
682 cache_entry_t *ce = NULL;
685 if (FORMAT_VL (name, sizeof (name), vl) != 0)
687 ERROR ("uc_get_state: FORMAT_VL failed.");
688 return (STATE_ERROR);
691 pthread_mutex_lock (&cache_lock);
693 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
700 pthread_mutex_unlock (&cache_lock);
703 } /* int uc_set_state */
705 int uc_get_history_by_name (const char *name,
706 gauge_t *ret_history, size_t num_steps, size_t num_ds)
708 cache_entry_t *ce = NULL;
712 pthread_mutex_lock (&cache_lock);
714 status = c_avl_get (cache_tree, name, (void *) &ce);
717 pthread_mutex_unlock (&cache_lock);
721 if (((size_t) ce->values_num) != num_ds)
723 pthread_mutex_unlock (&cache_lock);
727 /* Check if there are enough values available. If not, increase the buffer
729 if (ce->history_length < num_steps)
734 tmp = realloc (ce->history, sizeof (*ce->history)
735 * num_steps * ce->values_num);
738 pthread_mutex_unlock (&cache_lock);
742 for (i = ce->history_length * ce->values_num;
743 i < (num_steps * ce->values_num);
748 ce->history_length = num_steps;
749 } /* if (ce->history_length < num_steps) */
751 /* Copy the values to the output buffer. */
752 for (i = 0; i < num_steps; i++)
757 if (i < ce->history_index)
758 src_index = ce->history_index - (i + 1);
760 src_index = ce->history_length + ce->history_index - (i + 1);
761 src_index = src_index * num_ds;
763 dst_index = i * num_ds;
765 memcpy (ret_history + dst_index, ce->history + src_index,
766 sizeof (*ret_history) * num_ds);
769 pthread_mutex_unlock (&cache_lock);
772 } /* int uc_get_history_by_name */
774 int uc_get_history (const data_set_t *ds, const value_list_t *vl,
775 gauge_t *ret_history, size_t num_steps, size_t num_ds)
777 char name[6 * DATA_MAX_NAME_LEN];
779 if (FORMAT_VL (name, sizeof (name), vl) != 0)
781 ERROR ("utils_cache: uc_get_history: FORMAT_VL failed.");
785 return (uc_get_history_by_name (name, ret_history, num_steps, num_ds));
786 } /* int uc_get_history */
788 int uc_get_hits (const data_set_t *ds, const value_list_t *vl)
790 char name[6 * DATA_MAX_NAME_LEN];
791 cache_entry_t *ce = NULL;
792 int ret = STATE_ERROR;
794 if (FORMAT_VL (name, sizeof (name), vl) != 0)
796 ERROR ("uc_get_state: FORMAT_VL failed.");
797 return (STATE_ERROR);
800 pthread_mutex_lock (&cache_lock);
802 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
808 pthread_mutex_unlock (&cache_lock);
811 } /* int uc_get_hits */
813 int uc_set_hits (const data_set_t *ds, const value_list_t *vl, int hits)
815 char name[6 * DATA_MAX_NAME_LEN];
816 cache_entry_t *ce = NULL;
819 if (FORMAT_VL (name, sizeof (name), vl) != 0)
821 ERROR ("uc_get_state: FORMAT_VL failed.");
822 return (STATE_ERROR);
825 pthread_mutex_lock (&cache_lock);
827 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
834 pthread_mutex_unlock (&cache_lock);
837 } /* int uc_set_hits */
839 int uc_inc_hits (const data_set_t *ds, const value_list_t *vl, int step)
841 char name[6 * DATA_MAX_NAME_LEN];
842 cache_entry_t *ce = NULL;
845 if (FORMAT_VL (name, sizeof (name), vl) != 0)
847 ERROR ("uc_get_state: FORMAT_VL failed.");
848 return (STATE_ERROR);
851 pthread_mutex_lock (&cache_lock);
853 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
857 ce->hits = ret + step;
860 pthread_mutex_unlock (&cache_lock);
863 } /* int uc_inc_hits */
866 * Meta data interface
868 /* XXX: This function will acquire `cache_lock' but will not free it! */
869 static meta_data_t *uc_get_meta (const value_list_t *vl) /* {{{ */
871 char name[6 * DATA_MAX_NAME_LEN];
872 cache_entry_t *ce = NULL;
875 status = FORMAT_VL (name, sizeof (name), vl);
878 ERROR ("utils_cache: uc_get_meta: FORMAT_VL failed.");
882 pthread_mutex_lock (&cache_lock);
884 status = c_avl_get (cache_tree, name, (void *) &ce);
887 pthread_mutex_unlock (&cache_lock);
892 if (ce->meta == NULL)
893 ce->meta = meta_data_create ();
895 if (ce->meta == NULL)
896 pthread_mutex_unlock (&cache_lock);
899 } /* }}} meta_data_t *uc_get_meta */
901 /* Sorry about this preprocessor magic, but it really makes this file much
903 #define UC_WRAP(wrap_function) { \
906 meta = uc_get_meta (vl); \
907 if (meta == NULL) return (-1); \
908 status = wrap_function (meta, key); \
909 pthread_mutex_unlock (&cache_lock); \
912 int uc_meta_data_exists (const value_list_t *vl, const char *key)
913 UC_WRAP (meta_data_exists)
915 int uc_meta_data_delete (const value_list_t *vl, const char *key)
916 UC_WRAP (meta_data_delete)
919 /* We need a new version of this macro because the following functions take
921 #define UC_WRAP(wrap_function) { \
924 meta = uc_get_meta (vl); \
925 if (meta == NULL) return (-1); \
926 status = wrap_function (meta, key, value); \
927 pthread_mutex_unlock (&cache_lock); \
930 int uc_meta_data_add_string (const value_list_t *vl,
933 UC_WRAP(meta_data_add_string)
934 int uc_meta_data_add_signed_int (const value_list_t *vl,
937 UC_WRAP(meta_data_add_signed_int)
938 int uc_meta_data_add_unsigned_int (const value_list_t *vl,
941 UC_WRAP(meta_data_add_unsigned_int)
942 int uc_meta_data_add_double (const value_list_t *vl,
945 UC_WRAP(meta_data_add_double)
946 int uc_meta_data_add_boolean (const value_list_t *vl,
949 UC_WRAP(meta_data_add_boolean)
951 int uc_meta_data_get_string (const value_list_t *vl,
954 UC_WRAP(meta_data_get_string)
955 int uc_meta_data_get_signed_int (const value_list_t *vl,
958 UC_WRAP(meta_data_get_signed_int)
959 int uc_meta_data_get_unsigned_int (const value_list_t *vl,
962 UC_WRAP(meta_data_get_unsigned_int)
963 int uc_meta_data_get_double (const value_list_t *vl,
966 UC_WRAP(meta_data_get_double)
967 int uc_meta_data_get_boolean (const value_list_t *vl,
970 UC_WRAP(meta_data_get_boolean)
973 /* vim: set sw=2 ts=8 sts=2 tw=78 : */