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 (size_t 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 (%zu) 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.",
203 } /* switch (ds->ds[i].type) */
206 /* Prune invalid gauge data */
207 uc_check_range (ds, ce);
209 ce->last_time = vl->time;
210 ce->last_update = cdtime ();
211 ce->interval = vl->interval;
212 ce->state = STATE_OKAY;
214 if (c_avl_insert (cache_tree, key_copy, ce) != 0)
217 ERROR ("uc_insert: c_avl_insert failed.");
221 DEBUG ("uc_insert: Added %s to the cache.", key);
223 } /* int uc_insert */
227 if (cache_tree == NULL)
228 cache_tree = c_avl_create ((int (*) (const void *, const void *))
234 int uc_check_timeout (void)
240 cdtime_t *keys_time = NULL;
241 cdtime_t *keys_interval = NULL;
245 c_avl_iterator_t *iter;
250 pthread_mutex_lock (&cache_lock);
254 /* Build a list of entries to be flushed */
255 iter = c_avl_get_iterator (cache_tree);
256 while (c_avl_iterator_next (iter, (void *) &key, (void *) &ce) == 0)
261 /* If the entry is fresh enough, continue. */
262 if ((now - ce->last_update) < (ce->interval * timeout_g))
265 /* If entry has not been updated, add to `keys' array */
266 tmp = (char **) realloc ((void *) keys,
267 (keys_len + 1) * sizeof (char *));
270 ERROR ("uc_check_timeout: realloc failed.");
275 tmp_time = realloc (keys_time, (keys_len + 1) * sizeof (*keys_time));
276 if (tmp_time == NULL)
278 ERROR ("uc_check_timeout: realloc failed.");
281 keys_time = tmp_time;
283 tmp_time = realloc (keys_interval, (keys_len + 1) * sizeof (*keys_interval));
284 if (tmp_time == NULL)
286 ERROR ("uc_check_timeout: realloc failed.");
289 keys_interval = tmp_time;
291 keys[keys_len] = strdup (key);
292 if (keys[keys_len] == NULL)
294 ERROR ("uc_check_timeout: strdup failed.");
297 keys_time[keys_len] = ce->last_time;
298 keys_interval[keys_len] = ce->interval;
301 } /* while (c_avl_iterator_next) */
303 c_avl_iterator_destroy (iter);
304 pthread_mutex_unlock (&cache_lock);
308 /* realloc() may have been called for these. */
311 sfree (keys_interval);
315 /* Call the "missing" callback for each value. Do this before removing the
316 * value from the cache, so that callbacks can still access the data stored,
317 * including plugin specific meta data, rates, history, …. This must be done
318 * without holding the lock, otherwise we will run into a deadlock if a
319 * plugin calls the cache interface. */
320 for (i = 0; i < keys_len; i++)
322 value_list_t vl = VALUE_LIST_INIT;
328 status = parse_identifier_vl (keys[i], &vl);
331 ERROR ("uc_check_timeout: parse_identifier_vl (\"%s\") failed.", keys[i]);
335 vl.time = keys_time[i];
336 vl.interval = keys_interval[i];
338 plugin_dispatch_missing (&vl);
339 } /* for (i = 0; i < keys_len; i++) */
341 /* Now actually remove all the values from the cache. We don't re-evaluate
342 * the timestamp again, so in theory it is possible we remove a value after
343 * it is updated here. */
344 pthread_mutex_lock (&cache_lock);
345 for (i = 0; i < keys_len; i++)
350 status = c_avl_remove (cache_tree, keys[i],
351 (void *) &key, (void *) &ce);
354 ERROR ("uc_check_timeout: c_avl_remove (\"%s\") failed.", keys[i]);
362 } /* for (i = 0; i < keys_len; i++) */
363 pthread_mutex_unlock (&cache_lock);
367 sfree (keys_interval);
370 } /* int uc_check_timeout */
372 int uc_update (const data_set_t *ds, const value_list_t *vl)
374 char name[6 * DATA_MAX_NAME_LEN];
375 cache_entry_t *ce = NULL;
379 if (FORMAT_VL (name, sizeof (name), vl) != 0)
381 ERROR ("uc_update: FORMAT_VL failed.");
385 pthread_mutex_lock (&cache_lock);
387 status = c_avl_get (cache_tree, name, (void *) &ce);
388 if (status != 0) /* entry does not yet exist */
390 status = uc_insert (ds, vl, name);
391 pthread_mutex_unlock (&cache_lock);
396 assert (ce->values_num == ds->ds_num);
398 if (ce->last_time >= vl->time)
400 pthread_mutex_unlock (&cache_lock);
401 NOTICE ("uc_update: Value too old: name = %s; value time = %.3f; "
402 "last cache update = %.3f;",
404 CDTIME_T_TO_DOUBLE (vl->time),
405 CDTIME_T_TO_DOUBLE (ce->last_time));
409 for (i = 0; i < ds->ds_num; i++)
411 switch (ds->ds[i].type)
413 case DS_TYPE_COUNTER:
415 counter_t diff = counter_diff (ce->values_raw[i].counter, vl->values[i].counter);
416 ce->values_gauge[i] = ((double) diff)
417 / (CDTIME_T_TO_DOUBLE (vl->time - ce->last_time));
418 ce->values_raw[i].counter = vl->values[i].counter;
423 ce->values_raw[i].gauge = vl->values[i].gauge;
424 ce->values_gauge[i] = vl->values[i].gauge;
429 derive_t diff = vl->values[i].derive - ce->values_raw[i].derive;
431 ce->values_gauge[i] = ((double) diff)
432 / (CDTIME_T_TO_DOUBLE (vl->time - ce->last_time));
433 ce->values_raw[i].derive = vl->values[i].derive;
437 case DS_TYPE_ABSOLUTE:
438 ce->values_gauge[i] = ((double) vl->values[i].absolute)
439 / (CDTIME_T_TO_DOUBLE (vl->time - ce->last_time));
440 ce->values_raw[i].absolute = vl->values[i].absolute;
444 /* This shouldn't happen. */
445 pthread_mutex_unlock (&cache_lock);
446 ERROR ("uc_update: Don't know how to handle data source type %i.",
449 } /* switch (ds->ds[i].type) */
451 DEBUG ("uc_update: %s: ds[%zu] = %lf", name, i, ce->values_gauge[i]);
454 /* Update the history if it exists. */
455 if (ce->history != NULL)
457 assert (ce->history_index < ce->history_length);
458 for (i = 0; i < ce->values_num; i++)
460 size_t hist_idx = (ce->values_num * ce->history_index) + i;
461 ce->history[hist_idx] = ce->values_gauge[i];
464 assert (ce->history_length > 0);
465 ce->history_index = (ce->history_index + 1) % ce->history_length;
468 /* Prune invalid gauge data */
469 uc_check_range (ds, ce);
471 ce->last_time = vl->time;
472 ce->last_update = cdtime ();
473 ce->interval = vl->interval;
475 pthread_mutex_unlock (&cache_lock);
478 } /* int uc_update */
480 int uc_get_rate_by_name (const char *name, gauge_t **ret_values, size_t *ret_values_num)
484 cache_entry_t *ce = NULL;
487 pthread_mutex_lock (&cache_lock);
489 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
493 /* remove missing values from getval */
494 if (ce->state == STATE_MISSING)
500 ret_num = ce->values_num;
501 ret = (gauge_t *) malloc (ret_num * sizeof (gauge_t));
504 ERROR ("utils_cache: uc_get_rate_by_name: malloc failed.");
509 memcpy (ret, ce->values_gauge, ret_num * sizeof (gauge_t));
515 DEBUG ("utils_cache: uc_get_rate_by_name: No such value: %s", name);
519 pthread_mutex_unlock (&cache_lock);
524 *ret_values_num = ret_num;
528 } /* gauge_t *uc_get_rate_by_name */
530 gauge_t *uc_get_rate (const data_set_t *ds, const value_list_t *vl)
532 char name[6 * DATA_MAX_NAME_LEN];
537 if (FORMAT_VL (name, sizeof (name), vl) != 0)
539 ERROR ("utils_cache: uc_get_rate: FORMAT_VL failed.");
543 status = uc_get_rate_by_name (name, &ret, &ret_num);
547 /* This is important - the caller has no other way of knowing how many
548 * values are returned. */
549 if (ret_num != (size_t) ds->ds_num)
551 ERROR ("utils_cache: uc_get_rate: ds[%s] has %zu values, "
552 "but uc_get_rate_by_name returned %zu.",
553 ds->type, ds->ds_num, ret_num);
559 } /* gauge_t *uc_get_rate */
561 size_t uc_get_size() {
562 size_t size_arrays = 0;
564 pthread_mutex_lock (&cache_lock);
565 size_arrays = (size_t) c_avl_size (cache_tree);
566 pthread_mutex_unlock (&cache_lock);
568 return (size_arrays);
571 int uc_get_names (char ***ret_names, cdtime_t **ret_times, size_t *ret_number)
573 c_avl_iterator_t *iter;
575 cache_entry_t *value;
578 cdtime_t *times = NULL;
580 size_t size_arrays = 0;
584 if ((ret_names == NULL) || (ret_number == NULL))
587 pthread_mutex_lock (&cache_lock);
589 size_arrays = (size_t) c_avl_size (cache_tree);
592 /* Handle the "no values" case here, to avoid the error message when
593 * calloc() returns NULL. */
594 pthread_mutex_unlock (&cache_lock);
598 names = calloc (size_arrays, sizeof (*names));
599 times = calloc (size_arrays, sizeof (*times));
600 if ((names == NULL) || (times == NULL))
602 ERROR ("uc_get_names: calloc failed.");
605 pthread_mutex_unlock (&cache_lock);
609 iter = c_avl_get_iterator (cache_tree);
610 while (c_avl_iterator_next (iter, (void *) &key, (void *) &value) == 0)
612 /* remove missing values when list values */
613 if (value->state == STATE_MISSING)
616 /* c_avl_size does not return a number smaller than the number of elements
617 * returned by c_avl_iterator_next. */
618 assert (number < size_arrays);
620 if (ret_times != NULL)
621 times[number] = value->last_time;
623 names[number] = strdup (key);
624 if (names[number] == NULL)
631 } /* while (c_avl_iterator_next) */
633 c_avl_iterator_destroy (iter);
634 pthread_mutex_unlock (&cache_lock);
640 for (i = 0; i < number; i++)
651 if (ret_times != NULL)
655 *ret_number = number;
658 } /* int uc_get_names */
660 int uc_get_state (const data_set_t *ds, const value_list_t *vl)
662 char name[6 * DATA_MAX_NAME_LEN];
663 cache_entry_t *ce = NULL;
664 int ret = STATE_ERROR;
666 if (FORMAT_VL (name, sizeof (name), vl) != 0)
668 ERROR ("uc_get_state: FORMAT_VL failed.");
669 return (STATE_ERROR);
672 pthread_mutex_lock (&cache_lock);
674 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
680 pthread_mutex_unlock (&cache_lock);
683 } /* int uc_get_state */
685 int uc_set_state (const data_set_t *ds, const value_list_t *vl, int state)
687 char name[6 * DATA_MAX_NAME_LEN];
688 cache_entry_t *ce = NULL;
691 if (FORMAT_VL (name, sizeof (name), vl) != 0)
693 ERROR ("uc_get_state: FORMAT_VL failed.");
694 return (STATE_ERROR);
697 pthread_mutex_lock (&cache_lock);
699 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
706 pthread_mutex_unlock (&cache_lock);
709 } /* int uc_set_state */
711 int uc_get_history_by_name (const char *name,
712 gauge_t *ret_history, size_t num_steps, size_t num_ds)
714 cache_entry_t *ce = NULL;
718 pthread_mutex_lock (&cache_lock);
720 status = c_avl_get (cache_tree, name, (void *) &ce);
723 pthread_mutex_unlock (&cache_lock);
727 if (((size_t) ce->values_num) != num_ds)
729 pthread_mutex_unlock (&cache_lock);
733 /* Check if there are enough values available. If not, increase the buffer
735 if (ce->history_length < num_steps)
740 tmp = realloc (ce->history, sizeof (*ce->history)
741 * num_steps * ce->values_num);
744 pthread_mutex_unlock (&cache_lock);
748 for (i = ce->history_length * ce->values_num;
749 i < (num_steps * ce->values_num);
754 ce->history_length = num_steps;
755 } /* if (ce->history_length < num_steps) */
757 /* Copy the values to the output buffer. */
758 for (i = 0; i < num_steps; i++)
763 if (i < ce->history_index)
764 src_index = ce->history_index - (i + 1);
766 src_index = ce->history_length + ce->history_index - (i + 1);
767 src_index = src_index * num_ds;
769 dst_index = i * num_ds;
771 memcpy (ret_history + dst_index, ce->history + src_index,
772 sizeof (*ret_history) * num_ds);
775 pthread_mutex_unlock (&cache_lock);
778 } /* int uc_get_history_by_name */
780 int uc_get_history (const data_set_t *ds, const value_list_t *vl,
781 gauge_t *ret_history, size_t num_steps, size_t num_ds)
783 char name[6 * DATA_MAX_NAME_LEN];
785 if (FORMAT_VL (name, sizeof (name), vl) != 0)
787 ERROR ("utils_cache: uc_get_history: FORMAT_VL failed.");
791 return (uc_get_history_by_name (name, ret_history, num_steps, num_ds));
792 } /* int uc_get_history */
794 int uc_get_hits (const data_set_t *ds, const value_list_t *vl)
796 char name[6 * DATA_MAX_NAME_LEN];
797 cache_entry_t *ce = NULL;
798 int ret = STATE_ERROR;
800 if (FORMAT_VL (name, sizeof (name), vl) != 0)
802 ERROR ("uc_get_state: FORMAT_VL failed.");
803 return (STATE_ERROR);
806 pthread_mutex_lock (&cache_lock);
808 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
814 pthread_mutex_unlock (&cache_lock);
817 } /* int uc_get_hits */
819 int uc_set_hits (const data_set_t *ds, const value_list_t *vl, int hits)
821 char name[6 * DATA_MAX_NAME_LEN];
822 cache_entry_t *ce = NULL;
825 if (FORMAT_VL (name, sizeof (name), vl) != 0)
827 ERROR ("uc_get_state: FORMAT_VL failed.");
828 return (STATE_ERROR);
831 pthread_mutex_lock (&cache_lock);
833 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
840 pthread_mutex_unlock (&cache_lock);
843 } /* int uc_set_hits */
845 int uc_inc_hits (const data_set_t *ds, const value_list_t *vl, int step)
847 char name[6 * DATA_MAX_NAME_LEN];
848 cache_entry_t *ce = NULL;
851 if (FORMAT_VL (name, sizeof (name), vl) != 0)
853 ERROR ("uc_get_state: FORMAT_VL failed.");
854 return (STATE_ERROR);
857 pthread_mutex_lock (&cache_lock);
859 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
863 ce->hits = ret + step;
866 pthread_mutex_unlock (&cache_lock);
869 } /* int uc_inc_hits */
872 * Meta data interface
874 /* XXX: This function will acquire `cache_lock' but will not free it! */
875 static meta_data_t *uc_get_meta (const value_list_t *vl) /* {{{ */
877 char name[6 * DATA_MAX_NAME_LEN];
878 cache_entry_t *ce = NULL;
881 status = FORMAT_VL (name, sizeof (name), vl);
884 ERROR ("utils_cache: uc_get_meta: FORMAT_VL failed.");
888 pthread_mutex_lock (&cache_lock);
890 status = c_avl_get (cache_tree, name, (void *) &ce);
893 pthread_mutex_unlock (&cache_lock);
898 if (ce->meta == NULL)
899 ce->meta = meta_data_create ();
901 if (ce->meta == NULL)
902 pthread_mutex_unlock (&cache_lock);
905 } /* }}} meta_data_t *uc_get_meta */
907 /* Sorry about this preprocessor magic, but it really makes this file much
909 #define UC_WRAP(wrap_function) { \
912 meta = uc_get_meta (vl); \
913 if (meta == NULL) return (-1); \
914 status = wrap_function (meta, key); \
915 pthread_mutex_unlock (&cache_lock); \
918 int uc_meta_data_exists (const value_list_t *vl, const char *key)
919 UC_WRAP (meta_data_exists)
921 int uc_meta_data_delete (const value_list_t *vl, const char *key)
922 UC_WRAP (meta_data_delete)
925 /* We need a new version of this macro because the following functions take
927 #define UC_WRAP(wrap_function) { \
930 meta = uc_get_meta (vl); \
931 if (meta == NULL) return (-1); \
932 status = wrap_function (meta, key, value); \
933 pthread_mutex_unlock (&cache_lock); \
936 int uc_meta_data_add_string (const value_list_t *vl,
939 UC_WRAP(meta_data_add_string)
940 int uc_meta_data_add_signed_int (const value_list_t *vl,
943 UC_WRAP(meta_data_add_signed_int)
944 int uc_meta_data_add_unsigned_int (const value_list_t *vl,
947 UC_WRAP(meta_data_add_unsigned_int)
948 int uc_meta_data_add_double (const value_list_t *vl,
951 UC_WRAP(meta_data_add_double)
952 int uc_meta_data_add_boolean (const value_list_t *vl,
955 UC_WRAP(meta_data_add_boolean)
957 int uc_meta_data_get_string (const value_list_t *vl,
960 UC_WRAP(meta_data_get_string)
961 int uc_meta_data_get_signed_int (const value_list_t *vl,
964 UC_WRAP(meta_data_get_signed_int)
965 int uc_meta_data_get_unsigned_int (const value_list_t *vl,
968 UC_WRAP(meta_data_get_unsigned_int)
969 int uc_meta_data_get_double (const value_list_t *vl,
972 UC_WRAP(meta_data_get_double)
973 int uc_meta_data_get_boolean (const value_list_t *vl,
976 UC_WRAP(meta_data_get_boolean)
979 /* vim: set sw=2 ts=8 sts=2 tw=78 : */