2 * collectd - src/utils_cache.c
3 * Copyright (C) 2007-2010 Florian octo Forster
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
24 * Florian octo Forster <octo at collectd.org>
30 #include "utils_avltree.h"
31 #include "utils_cache.h"
32 #include "meta_data.h"
37 typedef struct cache_entry_s
39 char name[6 * DATA_MAX_NAME_LEN];
41 gauge_t *values_gauge;
43 /* Time contained in the package
44 * (for calculating rates) */
46 /* Time according to the local clock
47 * (for purging old entries) */
49 /* Interval in which the data is collected
50 * (for purding old entries) */
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 * +-----------------+-----------------+-----------------+----
65 size_t history_index; /* points to the next position to write to. */
66 size_t history_length;
71 static c_avl_tree_t *cache_tree = NULL;
72 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
74 static int cache_compare (const cache_entry_t *a, const cache_entry_t *b)
77 assert ((a != NULL) && (b != NULL));
79 return (strcmp (a->name, b->name));
80 } /* int cache_compare */
82 static cache_entry_t *cache_alloc (int values_num)
86 ce = (cache_entry_t *) malloc (sizeof (cache_entry_t));
89 ERROR ("utils_cache: cache_alloc: malloc failed.");
92 memset (ce, '\0', sizeof (cache_entry_t));
93 ce->values_num = values_num;
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))
99 sfree (ce->values_gauge);
100 sfree (ce->values_raw);
102 ERROR ("utils_cache: cache_alloc: calloc failed.");
107 ce->history_length = 0;
111 } /* cache_entry_t *cache_alloc */
113 static void cache_free (cache_entry_t *ce)
118 sfree (ce->values_gauge);
119 sfree (ce->values_raw);
121 if (ce->meta != NULL)
123 meta_data_destroy (ce->meta);
127 } /* void cache_free */
129 static void uc_check_range (const data_set_t *ds, cache_entry_t *ce)
133 for (i = 0; i < ds->ds_num; i++)
135 if (isnan (ce->values_gauge[i]))
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;
142 } /* void uc_check_range */
144 static int uc_insert (const data_set_t *ds, const value_list_t *vl,
151 /* `cache_lock' has been locked by `uc_update' */
153 key_copy = strdup (key);
154 if (key_copy == NULL)
156 ERROR ("uc_insert: strdup failed.");
160 ce = cache_alloc (ds->ds_num);
164 ERROR ("uc_insert: cache_alloc (%i) failed.", ds->ds_num);
168 sstrncpy (ce->name, key, sizeof (ce->name));
170 for (i = 0; i < ds->ds_num; i++)
172 switch (ds->ds[i].type)
174 case DS_TYPE_COUNTER:
175 ce->values_gauge[i] = NAN;
176 ce->values_raw[i].counter = vl->values[i].counter;
180 ce->values_gauge[i] = vl->values[i].gauge;
181 ce->values_raw[i].gauge = vl->values[i].gauge;
185 ce->values_gauge[i] = NAN;
186 ce->values_raw[i].derive = vl->values[i].derive;
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;
198 /* This shouldn't happen. */
199 ERROR ("uc_insert: Don't know how to handle data source type %i.",
202 } /* switch (ds->ds[i].type) */
205 /* Prune invalid gauge data */
206 uc_check_range (ds, ce);
208 ce->last_time = vl->time;
209 ce->last_update = cdtime ();
210 ce->interval = vl->interval;
211 ce->state = STATE_OKAY;
213 if (c_avl_insert (cache_tree, key_copy, ce) != 0)
216 ERROR ("uc_insert: c_avl_insert failed.");
220 DEBUG ("uc_insert: Added %s to the cache.", key);
222 } /* int uc_insert */
226 if (cache_tree == NULL)
227 cache_tree = c_avl_create ((int (*) (const void *, const void *))
233 int uc_check_timeout (void)
239 cdtime_t *keys_time = NULL;
240 cdtime_t *keys_interval = NULL;
244 c_avl_iterator_t *iter;
249 pthread_mutex_lock (&cache_lock);
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)
260 /* If the entry is fresh enough, continue. */
261 if ((now - ce->last_update) < (ce->interval * timeout_g))
264 /* If entry has not been updated, add to `keys' array */
265 tmp = (char **) realloc ((void *) keys,
266 (keys_len + 1) * sizeof (char *));
269 ERROR ("uc_check_timeout: realloc failed.");
274 tmp_time = realloc (keys_time, (keys_len + 1) * sizeof (*keys_time));
275 if (tmp_time == NULL)
277 ERROR ("uc_check_timeout: realloc failed.");
280 keys_time = tmp_time;
282 tmp_time = realloc (keys_interval, (keys_len + 1) * sizeof (*keys_interval));
283 if (tmp_time == NULL)
285 ERROR ("uc_check_timeout: realloc failed.");
288 keys_interval = tmp_time;
290 keys[keys_len] = strdup (key);
291 if (keys[keys_len] == NULL)
293 ERROR ("uc_check_timeout: strdup failed.");
296 keys_time[keys_len] = ce->last_time;
297 keys_interval[keys_len] = ce->interval;
300 } /* while (c_avl_iterator_next) */
302 c_avl_iterator_destroy (iter);
303 pthread_mutex_unlock (&cache_lock);
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 size_t uc_get_size() {
573 size_t size_arrays = 0;
575 pthread_mutex_lock (&cache_lock);
576 size_arrays = (size_t) c_avl_size (cache_tree);
577 pthread_mutex_unlock (&cache_lock);
579 return (size_arrays);
582 int uc_get_names (char ***ret_names, cdtime_t **ret_times, size_t *ret_number)
584 c_avl_iterator_t *iter;
586 cache_entry_t *value;
589 cdtime_t *times = NULL;
591 size_t size_arrays = 0;
595 if ((ret_names == NULL) || (ret_number == NULL))
598 pthread_mutex_lock (&cache_lock);
600 size_arrays = (size_t) c_avl_size (cache_tree);
603 /* Handle the "no values" case here, to avoid the error message when
604 * calloc() returns NULL. */
605 pthread_mutex_unlock (&cache_lock);
609 names = calloc (size_arrays, sizeof (*names));
610 times = calloc (size_arrays, sizeof (*times));
611 if ((names == NULL) || (times == NULL))
613 ERROR ("uc_get_names: calloc failed.");
616 pthread_mutex_unlock (&cache_lock);
620 iter = c_avl_get_iterator (cache_tree);
621 while (c_avl_iterator_next (iter, (void *) &key, (void *) &value) == 0)
623 /* remove missing values when list values */
624 if (value->state == STATE_MISSING)
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);
631 if (ret_times != NULL)
632 times[number] = value->last_time;
634 names[number] = strdup (key);
635 if (names[number] == NULL)
642 } /* while (c_avl_iterator_next) */
644 c_avl_iterator_destroy (iter);
645 pthread_mutex_unlock (&cache_lock);
651 for (i = 0; i < number; i++)
661 if (ret_times != NULL)
663 *ret_number = number;
666 } /* int uc_get_names */
668 int uc_get_state (const data_set_t *ds, const value_list_t *vl)
670 char name[6 * DATA_MAX_NAME_LEN];
671 cache_entry_t *ce = NULL;
672 int ret = STATE_ERROR;
674 if (FORMAT_VL (name, sizeof (name), vl) != 0)
676 ERROR ("uc_get_state: FORMAT_VL failed.");
677 return (STATE_ERROR);
680 pthread_mutex_lock (&cache_lock);
682 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
688 pthread_mutex_unlock (&cache_lock);
691 } /* int uc_get_state */
693 int uc_set_state (const data_set_t *ds, const value_list_t *vl, int state)
695 char name[6 * DATA_MAX_NAME_LEN];
696 cache_entry_t *ce = NULL;
699 if (FORMAT_VL (name, sizeof (name), vl) != 0)
701 ERROR ("uc_get_state: FORMAT_VL failed.");
702 return (STATE_ERROR);
705 pthread_mutex_lock (&cache_lock);
707 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
714 pthread_mutex_unlock (&cache_lock);
717 } /* int uc_set_state */
719 int uc_get_history_by_name (const char *name,
720 gauge_t *ret_history, size_t num_steps, size_t num_ds)
722 cache_entry_t *ce = NULL;
726 pthread_mutex_lock (&cache_lock);
728 status = c_avl_get (cache_tree, name, (void *) &ce);
731 pthread_mutex_unlock (&cache_lock);
735 if (((size_t) ce->values_num) != num_ds)
737 pthread_mutex_unlock (&cache_lock);
741 /* Check if there are enough values available. If not, increase the buffer
743 if (ce->history_length < num_steps)
748 tmp = realloc (ce->history, sizeof (*ce->history)
749 * num_steps * ce->values_num);
752 pthread_mutex_unlock (&cache_lock);
756 for (i = ce->history_length * ce->values_num;
757 i < (num_steps * ce->values_num);
762 ce->history_length = num_steps;
763 } /* if (ce->history_length < num_steps) */
765 /* Copy the values to the output buffer. */
766 for (i = 0; i < num_steps; i++)
771 if (i < ce->history_index)
772 src_index = ce->history_index - (i + 1);
774 src_index = ce->history_length + ce->history_index - (i + 1);
775 src_index = src_index * num_ds;
777 dst_index = i * num_ds;
779 memcpy (ret_history + dst_index, ce->history + src_index,
780 sizeof (*ret_history) * num_ds);
783 pthread_mutex_unlock (&cache_lock);
786 } /* int uc_get_history_by_name */
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)
791 char name[6 * DATA_MAX_NAME_LEN];
793 if (FORMAT_VL (name, sizeof (name), vl) != 0)
795 ERROR ("utils_cache: uc_get_history: FORMAT_VL failed.");
799 return (uc_get_history_by_name (name, ret_history, num_steps, num_ds));
800 } /* int uc_get_history */
802 int uc_get_hits (const data_set_t *ds, const value_list_t *vl)
804 char name[6 * DATA_MAX_NAME_LEN];
805 cache_entry_t *ce = NULL;
806 int ret = STATE_ERROR;
808 if (FORMAT_VL (name, sizeof (name), vl) != 0)
810 ERROR ("uc_get_state: FORMAT_VL failed.");
811 return (STATE_ERROR);
814 pthread_mutex_lock (&cache_lock);
816 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
822 pthread_mutex_unlock (&cache_lock);
825 } /* int uc_get_hits */
827 int uc_set_hits (const data_set_t *ds, const value_list_t *vl, int hits)
829 char name[6 * DATA_MAX_NAME_LEN];
830 cache_entry_t *ce = NULL;
833 if (FORMAT_VL (name, sizeof (name), vl) != 0)
835 ERROR ("uc_get_state: FORMAT_VL failed.");
836 return (STATE_ERROR);
839 pthread_mutex_lock (&cache_lock);
841 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
848 pthread_mutex_unlock (&cache_lock);
851 } /* int uc_set_hits */
853 int uc_inc_hits (const data_set_t *ds, const value_list_t *vl, int step)
855 char name[6 * DATA_MAX_NAME_LEN];
856 cache_entry_t *ce = NULL;
859 if (FORMAT_VL (name, sizeof (name), vl) != 0)
861 ERROR ("uc_get_state: FORMAT_VL failed.");
862 return (STATE_ERROR);
865 pthread_mutex_lock (&cache_lock);
867 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
871 ce->hits = ret + step;
874 pthread_mutex_unlock (&cache_lock);
877 } /* int uc_inc_hits */
880 * Meta data interface
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) /* {{{ */
885 char name[6 * DATA_MAX_NAME_LEN];
886 cache_entry_t *ce = NULL;
889 status = FORMAT_VL (name, sizeof (name), vl);
892 ERROR ("utils_cache: uc_get_meta: FORMAT_VL failed.");
896 pthread_mutex_lock (&cache_lock);
898 status = c_avl_get (cache_tree, name, (void *) &ce);
901 pthread_mutex_unlock (&cache_lock);
906 if (ce->meta == NULL)
907 ce->meta = meta_data_create ();
909 if (ce->meta == NULL)
910 pthread_mutex_unlock (&cache_lock);
913 } /* }}} meta_data_t *uc_get_meta */
915 /* Sorry about this preprocessor magic, but it really makes this file much
917 #define UC_WRAP(wrap_function) { \
920 meta = uc_get_meta (vl); \
921 if (meta == NULL) return (-1); \
922 status = wrap_function (meta, key); \
923 pthread_mutex_unlock (&cache_lock); \
926 int uc_meta_data_exists (const value_list_t *vl, const char *key)
927 UC_WRAP (meta_data_exists)
929 int uc_meta_data_delete (const value_list_t *vl, const char *key)
930 UC_WRAP (meta_data_delete)
933 /* We need a new version of this macro because the following functions take
935 #define UC_WRAP(wrap_function) { \
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); \
944 int uc_meta_data_add_string (const value_list_t *vl,
947 UC_WRAP(meta_data_add_string)
948 int uc_meta_data_add_signed_int (const value_list_t *vl,
951 UC_WRAP(meta_data_add_signed_int)
952 int uc_meta_data_add_unsigned_int (const value_list_t *vl,
955 UC_WRAP(meta_data_add_unsigned_int)
956 int uc_meta_data_add_double (const value_list_t *vl,
959 UC_WRAP(meta_data_add_double)
960 int uc_meta_data_add_boolean (const value_list_t *vl,
963 UC_WRAP(meta_data_add_boolean)
965 int uc_meta_data_get_string (const value_list_t *vl,
968 UC_WRAP(meta_data_get_string)
969 int uc_meta_data_get_signed_int (const value_list_t *vl,
972 UC_WRAP(meta_data_get_signed_int)
973 int uc_meta_data_get_unsigned_int (const value_list_t *vl,
976 UC_WRAP(meta_data_get_unsigned_int)
977 int uc_meta_data_get_double (const value_list_t *vl,
980 UC_WRAP(meta_data_get_double)
981 int uc_meta_data_get_boolean (const value_list_t *vl,
984 UC_WRAP(meta_data_get_boolean)
987 /* vim: set sw=2 ts=8 sts=2 tw=78 : */