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 __attribute__((unused))
123 static int uc_send_notification (const char *name)
125 cache_entry_t *ce = NULL;
131 char *plugin_instance;
137 name_copy = strdup (name);
138 if (name_copy == NULL)
140 ERROR ("uc_send_notification: strdup failed.");
144 status = parse_identifier (name_copy, &host,
145 &plugin, &plugin_instance,
146 &type, &type_instance);
149 ERROR ("uc_send_notification: Cannot parse name `%s'", name);
153 /* Copy the associative members */
154 notification_init (&n, NOTIF_FAILURE, /* host = */ NULL,
155 host, plugin, plugin_instance, type, type_instance);
158 name_copy = host = plugin = plugin_instance = type = type_instance = NULL;
160 pthread_mutex_lock (&cache_lock);
163 * Set the time _after_ getting the lock because we don't know how long
164 * acquiring the lock takes and we will use this time later to decide
165 * whether or not the state is OKAY.
169 status = c_avl_get (cache_tree, name, (void *) &ce);
172 pthread_mutex_unlock (&cache_lock);
177 /* Check if the entry has been updated in the meantime */
178 if ((n.time - ce->last_update) < (timeout_g * ce->interval))
180 ce->state = STATE_OKAY;
181 pthread_mutex_unlock (&cache_lock);
186 ssnprintf (n.message, sizeof (n.message),
187 "%s has not been updated for %.3f seconds.", name,
188 CDTIME_T_TO_DOUBLE (n.time - ce->last_update));
190 pthread_mutex_unlock (&cache_lock);
192 plugin_dispatch_notification (&n);
195 } /* int uc_send_notification */
197 static void uc_check_range (const data_set_t *ds, cache_entry_t *ce)
201 for (i = 0; i < ds->ds_num; i++)
203 if (isnan (ce->values_gauge[i]))
205 else if (ce->values_gauge[i] < ds->ds[i].min)
206 ce->values_gauge[i] = NAN;
207 else if (ce->values_gauge[i] > ds->ds[i].max)
208 ce->values_gauge[i] = NAN;
210 } /* void uc_check_range */
212 static int uc_insert (const data_set_t *ds, const value_list_t *vl,
219 /* `cache_lock' has been locked by `uc_update' */
221 key_copy = strdup (key);
222 if (key_copy == NULL)
224 ERROR ("uc_insert: strdup failed.");
228 ce = cache_alloc (ds->ds_num);
232 ERROR ("uc_insert: cache_alloc (%i) failed.", ds->ds_num);
236 sstrncpy (ce->name, key, sizeof (ce->name));
238 for (i = 0; i < ds->ds_num; i++)
240 switch (ds->ds[i].type)
242 case DS_TYPE_COUNTER:
243 ce->values_gauge[i] = NAN;
244 ce->values_raw[i].counter = vl->values[i].counter;
248 ce->values_gauge[i] = vl->values[i].gauge;
249 ce->values_raw[i].gauge = vl->values[i].gauge;
253 ce->values_gauge[i] = NAN;
254 ce->values_raw[i].derive = vl->values[i].derive;
257 case DS_TYPE_ABSOLUTE:
258 ce->values_gauge[i] = NAN;
259 if (vl->interval > 0)
260 ce->values_gauge[i] = ((double) vl->values[i].absolute)
261 / CDTIME_T_TO_DOUBLE (vl->interval);
262 ce->values_raw[i].absolute = vl->values[i].absolute;
266 /* This shouldn't happen. */
267 ERROR ("uc_insert: Don't know how to handle data source type %i.",
270 } /* switch (ds->ds[i].type) */
273 /* Prune invalid gauge data */
274 uc_check_range (ds, ce);
276 ce->last_time = vl->time;
277 ce->last_update = cdtime ();
278 ce->interval = vl->interval;
279 ce->state = STATE_OKAY;
281 if (c_avl_insert (cache_tree, key_copy, ce) != 0)
284 ERROR ("uc_insert: c_avl_insert failed.");
288 DEBUG ("uc_insert: Added %s to the cache.", key);
290 } /* int uc_insert */
294 if (cache_tree == NULL)
295 cache_tree = c_avl_create ((int (*) (const void *, const void *))
301 int uc_check_timeout (void)
307 cdtime_t *keys_time = NULL;
308 cdtime_t *keys_interval = NULL;
312 c_avl_iterator_t *iter;
317 pthread_mutex_lock (&cache_lock);
321 /* Build a list of entries to be flushed */
322 iter = c_avl_get_iterator (cache_tree);
323 while (c_avl_iterator_next (iter, (void *) &key, (void *) &ce) == 0)
328 /* If the entry is fresh enough, continue. */
329 if ((now - ce->last_update) < (ce->interval * timeout_g))
332 /* If entry has not been updated, add to `keys' array */
333 tmp = (char **) realloc ((void *) keys,
334 (keys_len + 1) * sizeof (char *));
337 ERROR ("uc_check_timeout: realloc failed.");
342 tmp_time = realloc (keys_time, (keys_len + 1) * sizeof (*keys_time));
343 if (tmp_time == NULL)
345 ERROR ("uc_check_timeout: realloc failed.");
348 keys_time = tmp_time;
350 tmp_time = realloc (keys_interval, (keys_len + 1) * sizeof (*keys_interval));
351 if (tmp_time == NULL)
353 ERROR ("uc_check_timeout: realloc failed.");
356 keys_interval = tmp_time;
358 keys[keys_len] = strdup (key);
359 if (keys[keys_len] == NULL)
361 ERROR ("uc_check_timeout: strdup failed.");
364 keys_time[keys_len] = ce->last_time;
365 keys_interval[keys_len] = ce->interval;
368 } /* while (c_avl_iterator_next) */
370 c_avl_iterator_destroy (iter);
371 pthread_mutex_unlock (&cache_lock);
376 /* Call the "missing" callback for each value. Do this before removing the
377 * value from the cache, so that callbacks can still access the data stored,
378 * including plugin specific meta data, rates, history, …. This must be done
379 * without holding the lock, otherwise we will run into a deadlock if a
380 * plugin calls the cache interface. */
381 for (i = 0; i < keys_len; i++)
383 value_list_t vl = VALUE_LIST_INIT;
389 status = parse_identifier_vl (keys[i], &vl);
392 ERROR ("uc_check_timeout: parse_identifier_vl (\"%s\") failed.", keys[i]);
397 vl.time = keys_time[i];
398 vl.interval = keys_interval[i];
400 plugin_dispatch_missing (&vl);
401 } /* for (i = 0; i < keys_len; i++) */
403 /* Now actually remove all the values from the cache. We don't re-evaluate
404 * the timestamp again, so in theory it is possible we remove a value after
405 * it is updated here. */
406 pthread_mutex_lock (&cache_lock);
407 for (i = 0; i < keys_len; i++)
412 status = c_avl_remove (cache_tree, keys[i],
413 (void *) &key, (void *) &ce);
416 ERROR ("uc_check_timeout: c_avl_remove (\"%s\") failed.", keys[i]);
424 } /* for (i = 0; i < keys_len; i++) */
425 pthread_mutex_unlock (&cache_lock);
429 sfree (keys_interval);
432 } /* int uc_check_timeout */
434 int uc_update (const data_set_t *ds, const value_list_t *vl)
436 char name[6 * DATA_MAX_NAME_LEN];
437 cache_entry_t *ce = NULL;
441 if (FORMAT_VL (name, sizeof (name), vl) != 0)
443 ERROR ("uc_update: FORMAT_VL failed.");
447 pthread_mutex_lock (&cache_lock);
449 status = c_avl_get (cache_tree, name, (void *) &ce);
450 if (status != 0) /* entry does not yet exist */
452 status = uc_insert (ds, vl, name);
453 pthread_mutex_unlock (&cache_lock);
458 assert (ce->values_num == ds->ds_num);
460 if (ce->last_time >= vl->time)
462 pthread_mutex_unlock (&cache_lock);
463 NOTICE ("uc_update: Value too old: name = %s; value time = %.3f; "
464 "last cache update = %.3f;",
466 CDTIME_T_TO_DOUBLE (vl->time),
467 CDTIME_T_TO_DOUBLE (ce->last_time));
471 for (i = 0; i < ds->ds_num; i++)
473 switch (ds->ds[i].type)
475 case DS_TYPE_COUNTER:
479 /* check if the counter has wrapped around */
480 if (vl->values[i].counter < ce->values_raw[i].counter)
482 if (ce->values_raw[i].counter <= 4294967295U)
483 diff = (4294967295U - ce->values_raw[i].counter)
484 + vl->values[i].counter;
486 diff = (18446744073709551615ULL - ce->values_raw[i].counter)
487 + vl->values[i].counter;
489 else /* counter has NOT wrapped around */
491 diff = vl->values[i].counter - ce->values_raw[i].counter;
494 ce->values_gauge[i] = ((double) diff)
495 / (CDTIME_T_TO_DOUBLE (vl->time - ce->last_time));
496 ce->values_raw[i].counter = vl->values[i].counter;
501 ce->values_raw[i].gauge = vl->values[i].gauge;
502 ce->values_gauge[i] = vl->values[i].gauge;
509 diff = vl->values[i].derive - ce->values_raw[i].derive;
511 ce->values_gauge[i] = ((double) diff)
512 / (CDTIME_T_TO_DOUBLE (vl->time - ce->last_time));
513 ce->values_raw[i].derive = vl->values[i].derive;
517 case DS_TYPE_ABSOLUTE:
518 ce->values_gauge[i] = ((double) vl->values[i].absolute)
519 / (CDTIME_T_TO_DOUBLE (vl->time - ce->last_time));
520 ce->values_raw[i].absolute = vl->values[i].absolute;
524 /* This shouldn't happen. */
525 pthread_mutex_unlock (&cache_lock);
526 ERROR ("uc_update: Don't know how to handle data source type %i.",
529 } /* switch (ds->ds[i].type) */
531 DEBUG ("uc_update: %s: ds[%i] = %lf", name, i, ce->values_gauge[i]);
534 /* Update the history if it exists. */
535 if (ce->history != NULL)
537 assert (ce->history_index < ce->history_length);
538 for (i = 0; i < ce->values_num; i++)
540 size_t hist_idx = (ce->values_num * ce->history_index) + i;
541 ce->history[hist_idx] = ce->values_gauge[i];
544 assert (ce->history_length > 0);
545 ce->history_index = (ce->history_index + 1) % ce->history_length;
548 /* Prune invalid gauge data */
549 uc_check_range (ds, ce);
551 ce->last_time = vl->time;
552 ce->last_update = cdtime ();
553 ce->interval = vl->interval;
555 pthread_mutex_unlock (&cache_lock);
558 } /* int uc_update */
560 int uc_get_rate_by_name (const char *name, gauge_t **ret_values, size_t *ret_values_num)
564 cache_entry_t *ce = NULL;
567 pthread_mutex_lock (&cache_lock);
569 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
573 /* remove missing values from getval */
574 if (ce->state == STATE_MISSING)
580 ret_num = ce->values_num;
581 ret = (gauge_t *) malloc (ret_num * sizeof (gauge_t));
584 ERROR ("utils_cache: uc_get_rate_by_name: malloc failed.");
589 memcpy (ret, ce->values_gauge, ret_num * sizeof (gauge_t));
595 DEBUG ("utils_cache: uc_get_rate_by_name: No such value: %s", name);
599 pthread_mutex_unlock (&cache_lock);
604 *ret_values_num = ret_num;
608 } /* gauge_t *uc_get_rate_by_name */
610 gauge_t *uc_get_rate (const data_set_t *ds, const value_list_t *vl)
612 char name[6 * DATA_MAX_NAME_LEN];
617 if (FORMAT_VL (name, sizeof (name), vl) != 0)
619 ERROR ("utils_cache: uc_get_rate: FORMAT_VL failed.");
623 status = uc_get_rate_by_name (name, &ret, &ret_num);
627 /* This is important - the caller has no other way of knowing how many
628 * values are returned. */
629 if (ret_num != (size_t) ds->ds_num)
631 ERROR ("utils_cache: uc_get_rate: ds[%s] has %i values, "
632 "but uc_get_rate_by_name returned %zu.",
633 ds->type, ds->ds_num, ret_num);
639 } /* gauge_t *uc_get_rate */
641 int uc_get_names (char ***ret_names, cdtime_t **ret_times, size_t *ret_number)
643 c_avl_iterator_t *iter;
645 cache_entry_t *value;
648 cdtime_t *times = NULL;
653 if ((ret_names == NULL) || (ret_number == NULL))
656 pthread_mutex_lock (&cache_lock);
658 iter = c_avl_get_iterator (cache_tree);
659 while (c_avl_iterator_next (iter, (void *) &key, (void *) &value) == 0)
663 /* remove missing values when list values */
664 if (value->state == STATE_MISSING)
667 if (ret_times != NULL)
671 tmp_times = (cdtime_t *) realloc (times, sizeof (cdtime_t) * (number + 1));
672 if (tmp_times == NULL)
678 times[number] = value->last_time;
681 temp = (char **) realloc (names, sizeof (char *) * (number + 1));
688 names[number] = strdup (key);
689 if (names[number] == NULL)
695 } /* while (c_avl_iterator_next) */
697 c_avl_iterator_destroy (iter);
698 pthread_mutex_unlock (&cache_lock);
704 for (i = 0; i < number; i++)
714 if (ret_times != NULL)
716 *ret_number = number;
719 } /* int uc_get_names */
721 int uc_get_state (const data_set_t *ds, const value_list_t *vl)
723 char name[6 * DATA_MAX_NAME_LEN];
724 cache_entry_t *ce = NULL;
725 int ret = STATE_ERROR;
727 if (FORMAT_VL (name, sizeof (name), vl) != 0)
729 ERROR ("uc_get_state: FORMAT_VL failed.");
730 return (STATE_ERROR);
733 pthread_mutex_lock (&cache_lock);
735 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
741 pthread_mutex_unlock (&cache_lock);
744 } /* int uc_get_state */
746 int uc_set_state (const data_set_t *ds, const value_list_t *vl, int state)
748 char name[6 * DATA_MAX_NAME_LEN];
749 cache_entry_t *ce = NULL;
752 if (FORMAT_VL (name, sizeof (name), vl) != 0)
754 ERROR ("uc_get_state: FORMAT_VL failed.");
755 return (STATE_ERROR);
758 pthread_mutex_lock (&cache_lock);
760 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
767 pthread_mutex_unlock (&cache_lock);
770 } /* int uc_set_state */
772 int uc_get_history_by_name (const char *name,
773 gauge_t *ret_history, size_t num_steps, size_t num_ds)
775 cache_entry_t *ce = NULL;
779 pthread_mutex_lock (&cache_lock);
781 status = c_avl_get (cache_tree, name, (void *) &ce);
784 pthread_mutex_unlock (&cache_lock);
788 if (((size_t) ce->values_num) != num_ds)
790 pthread_mutex_unlock (&cache_lock);
794 /* Check if there are enough values available. If not, increase the buffer
796 if (ce->history_length < num_steps)
801 tmp = realloc (ce->history, sizeof (*ce->history)
802 * num_steps * ce->values_num);
805 pthread_mutex_unlock (&cache_lock);
809 for (i = ce->history_length * ce->values_num;
810 i < (num_steps * ce->values_num);
815 ce->history_length = num_steps;
816 } /* if (ce->history_length < num_steps) */
818 /* Copy the values to the output buffer. */
819 for (i = 0; i < num_steps; i++)
824 if (i < ce->history_index)
825 src_index = ce->history_index - (i + 1);
827 src_index = ce->history_length + ce->history_index - (i + 1);
828 src_index = src_index * num_ds;
830 dst_index = i * num_ds;
832 memcpy (ret_history + dst_index, ce->history + src_index,
833 sizeof (*ret_history) * num_ds);
836 pthread_mutex_unlock (&cache_lock);
839 } /* int uc_get_history_by_name */
841 int uc_get_history (const data_set_t *ds, const value_list_t *vl,
842 gauge_t *ret_history, size_t num_steps, size_t num_ds)
844 char name[6 * DATA_MAX_NAME_LEN];
846 if (FORMAT_VL (name, sizeof (name), vl) != 0)
848 ERROR ("utils_cache: uc_get_history: FORMAT_VL failed.");
852 return (uc_get_history_by_name (name, ret_history, num_steps, num_ds));
853 } /* int uc_get_history */
855 int uc_get_hits (const data_set_t *ds, const value_list_t *vl)
857 char name[6 * DATA_MAX_NAME_LEN];
858 cache_entry_t *ce = NULL;
859 int ret = STATE_ERROR;
861 if (FORMAT_VL (name, sizeof (name), vl) != 0)
863 ERROR ("uc_get_state: FORMAT_VL failed.");
864 return (STATE_ERROR);
867 pthread_mutex_lock (&cache_lock);
869 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
875 pthread_mutex_unlock (&cache_lock);
878 } /* int uc_get_hits */
880 int uc_set_hits (const data_set_t *ds, const value_list_t *vl, int hits)
882 char name[6 * DATA_MAX_NAME_LEN];
883 cache_entry_t *ce = NULL;
886 if (FORMAT_VL (name, sizeof (name), vl) != 0)
888 ERROR ("uc_get_state: FORMAT_VL failed.");
889 return (STATE_ERROR);
892 pthread_mutex_lock (&cache_lock);
894 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
901 pthread_mutex_unlock (&cache_lock);
904 } /* int uc_set_hits */
906 int uc_inc_hits (const data_set_t *ds, const value_list_t *vl, int step)
908 char name[6 * DATA_MAX_NAME_LEN];
909 cache_entry_t *ce = NULL;
912 if (FORMAT_VL (name, sizeof (name), vl) != 0)
914 ERROR ("uc_get_state: FORMAT_VL failed.");
915 return (STATE_ERROR);
918 pthread_mutex_lock (&cache_lock);
920 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
924 ce->hits = ret + step;
927 pthread_mutex_unlock (&cache_lock);
930 } /* int uc_inc_hits */
933 * Meta data interface
935 /* XXX: This function will acquire `cache_lock' but will not free it! */
936 static meta_data_t *uc_get_meta (const value_list_t *vl) /* {{{ */
938 char name[6 * DATA_MAX_NAME_LEN];
939 cache_entry_t *ce = NULL;
942 status = FORMAT_VL (name, sizeof (name), vl);
945 ERROR ("utils_cache: uc_get_meta: FORMAT_VL failed.");
949 pthread_mutex_lock (&cache_lock);
951 status = c_avl_get (cache_tree, name, (void *) &ce);
954 pthread_mutex_unlock (&cache_lock);
959 if (ce->meta == NULL)
960 ce->meta = meta_data_create ();
962 if (ce->meta == NULL)
963 pthread_mutex_unlock (&cache_lock);
966 } /* }}} meta_data_t *uc_get_meta */
968 /* Sorry about this preprocessor magic, but it really makes this file much
970 #define UC_WRAP(wrap_function) { \
973 meta = uc_get_meta (vl); \
974 if (meta == NULL) return (-1); \
975 status = wrap_function (meta, key); \
976 pthread_mutex_unlock (&cache_lock); \
979 int uc_meta_data_exists (const value_list_t *vl, const char *key)
980 UC_WRAP (meta_data_exists)
982 int uc_meta_data_delete (const value_list_t *vl, const char *key)
983 UC_WRAP (meta_data_delete)
986 /* We need a new version of this macro because the following functions take
988 #define UC_WRAP(wrap_function) { \
991 meta = uc_get_meta (vl); \
992 if (meta == NULL) return (-1); \
993 status = wrap_function (meta, key, value); \
994 pthread_mutex_unlock (&cache_lock); \
997 int uc_meta_data_add_string (const value_list_t *vl,
1000 UC_WRAP(meta_data_add_string)
1001 int uc_meta_data_add_signed_int (const value_list_t *vl,
1004 UC_WRAP(meta_data_add_signed_int)
1005 int uc_meta_data_add_unsigned_int (const value_list_t *vl,
1008 UC_WRAP(meta_data_add_unsigned_int)
1009 int uc_meta_data_add_double (const value_list_t *vl,
1012 UC_WRAP(meta_data_add_double)
1013 int uc_meta_data_add_boolean (const value_list_t *vl,
1016 UC_WRAP(meta_data_add_boolean)
1018 int uc_meta_data_get_string (const value_list_t *vl,
1021 UC_WRAP(meta_data_get_string)
1022 int uc_meta_data_get_signed_int (const value_list_t *vl,
1025 UC_WRAP(meta_data_get_signed_int)
1026 int uc_meta_data_get_unsigned_int (const value_list_t *vl,
1029 UC_WRAP(meta_data_get_unsigned_int)
1030 int uc_meta_data_get_double (const value_list_t *vl,
1033 UC_WRAP(meta_data_get_double)
1034 int uc_meta_data_get_boolean (const value_list_t *vl,
1037 UC_WRAP(meta_data_get_boolean)
1040 /* vim: set sw=2 ts=8 sts=2 tw=78 : */