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]);
329 vl.time = keys_time[i];
330 vl.interval = keys_interval[i];
332 plugin_dispatch_missing (&vl);
333 } /* for (i = 0; i < keys_len; i++) */
335 /* Now actually remove all the values from the cache. We don't re-evaluate
336 * the timestamp again, so in theory it is possible we remove a value after
337 * it is updated here. */
338 pthread_mutex_lock (&cache_lock);
339 for (i = 0; i < keys_len; i++)
344 status = c_avl_remove (cache_tree, keys[i],
345 (void *) &key, (void *) &ce);
348 ERROR ("uc_check_timeout: c_avl_remove (\"%s\") failed.", keys[i]);
356 } /* for (i = 0; i < keys_len; i++) */
357 pthread_mutex_unlock (&cache_lock);
361 sfree (keys_interval);
364 } /* int uc_check_timeout */
366 int uc_update (const data_set_t *ds, const value_list_t *vl)
368 char name[6 * DATA_MAX_NAME_LEN];
369 cache_entry_t *ce = NULL;
373 if (FORMAT_VL (name, sizeof (name), vl) != 0)
375 ERROR ("uc_update: FORMAT_VL failed.");
379 pthread_mutex_lock (&cache_lock);
381 status = c_avl_get (cache_tree, name, (void *) &ce);
382 if (status != 0) /* entry does not yet exist */
384 status = uc_insert (ds, vl, name);
385 pthread_mutex_unlock (&cache_lock);
390 assert (ce->values_num == ds->ds_num);
392 if (ce->last_time >= vl->time)
394 pthread_mutex_unlock (&cache_lock);
395 NOTICE ("uc_update: Value too old: name = %s; value time = %.3f; "
396 "last cache update = %.3f;",
398 CDTIME_T_TO_DOUBLE (vl->time),
399 CDTIME_T_TO_DOUBLE (ce->last_time));
403 for (i = 0; i < ds->ds_num; i++)
405 switch (ds->ds[i].type)
407 case DS_TYPE_COUNTER:
411 /* check if the counter has wrapped around */
412 if (vl->values[i].counter < ce->values_raw[i].counter)
414 if (ce->values_raw[i].counter <= 4294967295U)
415 diff = (4294967295U - ce->values_raw[i].counter)
416 + vl->values[i].counter;
418 diff = (18446744073709551615ULL - ce->values_raw[i].counter)
419 + vl->values[i].counter;
421 else /* counter has NOT wrapped around */
423 diff = vl->values[i].counter - ce->values_raw[i].counter;
426 ce->values_gauge[i] = ((double) diff)
427 / (CDTIME_T_TO_DOUBLE (vl->time - ce->last_time));
428 ce->values_raw[i].counter = vl->values[i].counter;
433 ce->values_raw[i].gauge = vl->values[i].gauge;
434 ce->values_gauge[i] = vl->values[i].gauge;
441 diff = vl->values[i].derive - ce->values_raw[i].derive;
443 ce->values_gauge[i] = ((double) diff)
444 / (CDTIME_T_TO_DOUBLE (vl->time - ce->last_time));
445 ce->values_raw[i].derive = vl->values[i].derive;
449 case DS_TYPE_ABSOLUTE:
450 ce->values_gauge[i] = ((double) vl->values[i].absolute)
451 / (CDTIME_T_TO_DOUBLE (vl->time - ce->last_time));
452 ce->values_raw[i].absolute = vl->values[i].absolute;
456 /* This shouldn't happen. */
457 pthread_mutex_unlock (&cache_lock);
458 ERROR ("uc_update: Don't know how to handle data source type %i.",
461 } /* switch (ds->ds[i].type) */
463 DEBUG ("uc_update: %s: ds[%i] = %lf", name, i, ce->values_gauge[i]);
466 /* Update the history if it exists. */
467 if (ce->history != NULL)
469 assert (ce->history_index < ce->history_length);
470 for (i = 0; i < ce->values_num; i++)
472 size_t hist_idx = (ce->values_num * ce->history_index) + i;
473 ce->history[hist_idx] = ce->values_gauge[i];
476 assert (ce->history_length > 0);
477 ce->history_index = (ce->history_index + 1) % ce->history_length;
480 /* Prune invalid gauge data */
481 uc_check_range (ds, ce);
483 ce->last_time = vl->time;
484 ce->last_update = cdtime ();
485 ce->interval = vl->interval;
487 pthread_mutex_unlock (&cache_lock);
490 } /* int uc_update */
492 int uc_get_rate_by_name (const char *name, gauge_t **ret_values, size_t *ret_values_num)
496 cache_entry_t *ce = NULL;
499 pthread_mutex_lock (&cache_lock);
501 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
505 /* remove missing values from getval */
506 if (ce->state == STATE_MISSING)
512 ret_num = ce->values_num;
513 ret = (gauge_t *) malloc (ret_num * sizeof (gauge_t));
516 ERROR ("utils_cache: uc_get_rate_by_name: malloc failed.");
521 memcpy (ret, ce->values_gauge, ret_num * sizeof (gauge_t));
527 DEBUG ("utils_cache: uc_get_rate_by_name: No such value: %s", name);
531 pthread_mutex_unlock (&cache_lock);
536 *ret_values_num = ret_num;
540 } /* gauge_t *uc_get_rate_by_name */
542 gauge_t *uc_get_rate (const data_set_t *ds, const value_list_t *vl)
544 char name[6 * DATA_MAX_NAME_LEN];
549 if (FORMAT_VL (name, sizeof (name), vl) != 0)
551 ERROR ("utils_cache: uc_get_rate: FORMAT_VL failed.");
555 status = uc_get_rate_by_name (name, &ret, &ret_num);
559 /* This is important - the caller has no other way of knowing how many
560 * values are returned. */
561 if (ret_num != (size_t) ds->ds_num)
563 ERROR ("utils_cache: uc_get_rate: ds[%s] has %i values, "
564 "but uc_get_rate_by_name returned %zu.",
565 ds->type, ds->ds_num, ret_num);
571 } /* gauge_t *uc_get_rate */
573 int uc_get_names (char ***ret_names, cdtime_t **ret_times, size_t *ret_number)
575 c_avl_iterator_t *iter;
577 cache_entry_t *value;
580 cdtime_t *times = NULL;
582 size_t size_arrays = 0;
586 if ((ret_names == NULL) || (ret_number == NULL))
589 pthread_mutex_lock (&cache_lock);
591 size_arrays = (size_t) c_avl_size (cache_tree);
594 /* Handle the "no values" case here, to avoid the error message when
595 * calloc() returns NULL. */
596 pthread_mutex_unlock (&cache_lock);
600 names = calloc (size_arrays, sizeof (*names));
601 times = calloc (size_arrays, sizeof (*times));
602 if ((names == NULL) || (times == NULL))
604 ERROR ("uc_get_names: calloc failed.");
607 pthread_mutex_unlock (&cache_lock);
611 iter = c_avl_get_iterator (cache_tree);
612 while (c_avl_iterator_next (iter, (void *) &key, (void *) &value) == 0)
614 /* remove missing values when list values */
615 if (value->state == STATE_MISSING)
618 /* c_avl_size does not return a number smaller than the number of elements
619 * returned by c_avl_iterator_next. */
620 assert (number < size_arrays);
622 if (ret_times != NULL)
623 times[number] = value->last_time;
625 names[number] = strdup (key);
626 if (names[number] == NULL)
633 } /* while (c_avl_iterator_next) */
635 c_avl_iterator_destroy (iter);
636 pthread_mutex_unlock (&cache_lock);
642 for (i = 0; i < number; i++)
652 if (ret_times != NULL)
654 *ret_number = number;
657 } /* int uc_get_names */
659 int uc_get_state (const data_set_t *ds, const value_list_t *vl)
661 char name[6 * DATA_MAX_NAME_LEN];
662 cache_entry_t *ce = NULL;
663 int ret = STATE_ERROR;
665 if (FORMAT_VL (name, sizeof (name), vl) != 0)
667 ERROR ("uc_get_state: FORMAT_VL failed.");
668 return (STATE_ERROR);
671 pthread_mutex_lock (&cache_lock);
673 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
679 pthread_mutex_unlock (&cache_lock);
682 } /* int uc_get_state */
684 int uc_set_state (const data_set_t *ds, const value_list_t *vl, int state)
686 char name[6 * DATA_MAX_NAME_LEN];
687 cache_entry_t *ce = NULL;
690 if (FORMAT_VL (name, sizeof (name), vl) != 0)
692 ERROR ("uc_get_state: FORMAT_VL failed.");
693 return (STATE_ERROR);
696 pthread_mutex_lock (&cache_lock);
698 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
705 pthread_mutex_unlock (&cache_lock);
708 } /* int uc_set_state */
710 int uc_get_history_by_name (const char *name,
711 gauge_t *ret_history, size_t num_steps, size_t num_ds)
713 cache_entry_t *ce = NULL;
717 pthread_mutex_lock (&cache_lock);
719 status = c_avl_get (cache_tree, name, (void *) &ce);
722 pthread_mutex_unlock (&cache_lock);
726 if (((size_t) ce->values_num) != num_ds)
728 pthread_mutex_unlock (&cache_lock);
732 /* Check if there are enough values available. If not, increase the buffer
734 if (ce->history_length < num_steps)
739 tmp = realloc (ce->history, sizeof (*ce->history)
740 * num_steps * ce->values_num);
743 pthread_mutex_unlock (&cache_lock);
747 for (i = ce->history_length * ce->values_num;
748 i < (num_steps * ce->values_num);
753 ce->history_length = num_steps;
754 } /* if (ce->history_length < num_steps) */
756 /* Copy the values to the output buffer. */
757 for (i = 0; i < num_steps; i++)
762 if (i < ce->history_index)
763 src_index = ce->history_index - (i + 1);
765 src_index = ce->history_length + ce->history_index - (i + 1);
766 src_index = src_index * num_ds;
768 dst_index = i * num_ds;
770 memcpy (ret_history + dst_index, ce->history + src_index,
771 sizeof (*ret_history) * num_ds);
774 pthread_mutex_unlock (&cache_lock);
777 } /* int uc_get_history_by_name */
779 int uc_get_history (const data_set_t *ds, const value_list_t *vl,
780 gauge_t *ret_history, size_t num_steps, size_t num_ds)
782 char name[6 * DATA_MAX_NAME_LEN];
784 if (FORMAT_VL (name, sizeof (name), vl) != 0)
786 ERROR ("utils_cache: uc_get_history: FORMAT_VL failed.");
790 return (uc_get_history_by_name (name, ret_history, num_steps, num_ds));
791 } /* int uc_get_history */
793 int uc_get_hits (const data_set_t *ds, const value_list_t *vl)
795 char name[6 * DATA_MAX_NAME_LEN];
796 cache_entry_t *ce = NULL;
797 int ret = STATE_ERROR;
799 if (FORMAT_VL (name, sizeof (name), vl) != 0)
801 ERROR ("uc_get_state: FORMAT_VL failed.");
802 return (STATE_ERROR);
805 pthread_mutex_lock (&cache_lock);
807 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
813 pthread_mutex_unlock (&cache_lock);
816 } /* int uc_get_hits */
818 int uc_set_hits (const data_set_t *ds, const value_list_t *vl, int hits)
820 char name[6 * DATA_MAX_NAME_LEN];
821 cache_entry_t *ce = NULL;
824 if (FORMAT_VL (name, sizeof (name), vl) != 0)
826 ERROR ("uc_get_state: FORMAT_VL failed.");
827 return (STATE_ERROR);
830 pthread_mutex_lock (&cache_lock);
832 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
839 pthread_mutex_unlock (&cache_lock);
842 } /* int uc_set_hits */
844 int uc_inc_hits (const data_set_t *ds, const value_list_t *vl, int step)
846 char name[6 * DATA_MAX_NAME_LEN];
847 cache_entry_t *ce = NULL;
850 if (FORMAT_VL (name, sizeof (name), vl) != 0)
852 ERROR ("uc_get_state: FORMAT_VL failed.");
853 return (STATE_ERROR);
856 pthread_mutex_lock (&cache_lock);
858 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
862 ce->hits = ret + step;
865 pthread_mutex_unlock (&cache_lock);
868 } /* int uc_inc_hits */
871 * Meta data interface
873 /* XXX: This function will acquire `cache_lock' but will not free it! */
874 static meta_data_t *uc_get_meta (const value_list_t *vl) /* {{{ */
876 char name[6 * DATA_MAX_NAME_LEN];
877 cache_entry_t *ce = NULL;
880 status = FORMAT_VL (name, sizeof (name), vl);
883 ERROR ("utils_cache: uc_get_meta: FORMAT_VL failed.");
887 pthread_mutex_lock (&cache_lock);
889 status = c_avl_get (cache_tree, name, (void *) &ce);
892 pthread_mutex_unlock (&cache_lock);
897 if (ce->meta == NULL)
898 ce->meta = meta_data_create ();
900 if (ce->meta == NULL)
901 pthread_mutex_unlock (&cache_lock);
904 } /* }}} meta_data_t *uc_get_meta */
906 /* Sorry about this preprocessor magic, but it really makes this file much
908 #define UC_WRAP(wrap_function) { \
911 meta = uc_get_meta (vl); \
912 if (meta == NULL) return (-1); \
913 status = wrap_function (meta, key); \
914 pthread_mutex_unlock (&cache_lock); \
917 int uc_meta_data_exists (const value_list_t *vl, const char *key)
918 UC_WRAP (meta_data_exists)
920 int uc_meta_data_delete (const value_list_t *vl, const char *key)
921 UC_WRAP (meta_data_delete)
924 /* We need a new version of this macro because the following functions take
926 #define UC_WRAP(wrap_function) { \
929 meta = uc_get_meta (vl); \
930 if (meta == NULL) return (-1); \
931 status = wrap_function (meta, key, value); \
932 pthread_mutex_unlock (&cache_lock); \
935 int uc_meta_data_add_string (const value_list_t *vl,
938 UC_WRAP(meta_data_add_string)
939 int uc_meta_data_add_signed_int (const value_list_t *vl,
942 UC_WRAP(meta_data_add_signed_int)
943 int uc_meta_data_add_unsigned_int (const value_list_t *vl,
946 UC_WRAP(meta_data_add_unsigned_int)
947 int uc_meta_data_add_double (const value_list_t *vl,
950 UC_WRAP(meta_data_add_double)
951 int uc_meta_data_add_boolean (const value_list_t *vl,
954 UC_WRAP(meta_data_add_boolean)
956 int uc_meta_data_get_string (const value_list_t *vl,
959 UC_WRAP(meta_data_get_string)
960 int uc_meta_data_get_signed_int (const value_list_t *vl,
963 UC_WRAP(meta_data_get_signed_int)
964 int uc_meta_data_get_unsigned_int (const value_list_t *vl,
967 UC_WRAP(meta_data_get_unsigned_int)
968 int uc_meta_data_get_double (const value_list_t *vl,
971 UC_WRAP(meta_data_get_double)
972 int uc_meta_data_get_boolean (const value_list_t *vl,
975 UC_WRAP(meta_data_get_boolean)
978 /* vim: set sw=2 ts=8 sts=2 tw=78 : */