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.",
204 } /* switch (ds->ds[i].type) */
207 /* Prune invalid gauge data */
208 uc_check_range (ds, ce);
210 ce->last_time = vl->time;
211 ce->last_update = cdtime ();
212 ce->interval = vl->interval;
213 ce->state = STATE_OKAY;
215 if (c_avl_insert (cache_tree, key_copy, ce) != 0)
218 ERROR ("uc_insert: c_avl_insert failed.");
222 DEBUG ("uc_insert: Added %s to the cache.", key);
224 } /* int uc_insert */
228 if (cache_tree == NULL)
229 cache_tree = c_avl_create ((int (*) (const void *, const void *))
235 int uc_check_timeout (void)
241 cdtime_t *keys_time = NULL;
242 cdtime_t *keys_interval = NULL;
246 c_avl_iterator_t *iter;
251 pthread_mutex_lock (&cache_lock);
255 /* Build a list of entries to be flushed */
256 iter = c_avl_get_iterator (cache_tree);
257 while (c_avl_iterator_next (iter, (void *) &key, (void *) &ce) == 0)
262 /* If the entry is fresh enough, continue. */
263 if ((now - ce->last_update) < (ce->interval * timeout_g))
266 /* If entry has not been updated, add to `keys' array */
267 tmp = (char **) realloc ((void *) keys,
268 (keys_len + 1) * sizeof (char *));
271 ERROR ("uc_check_timeout: realloc failed.");
276 tmp_time = realloc (keys_time, (keys_len + 1) * sizeof (*keys_time));
277 if (tmp_time == NULL)
279 ERROR ("uc_check_timeout: realloc failed.");
282 keys_time = tmp_time;
284 tmp_time = realloc (keys_interval, (keys_len + 1) * sizeof (*keys_interval));
285 if (tmp_time == NULL)
287 ERROR ("uc_check_timeout: realloc failed.");
290 keys_interval = tmp_time;
292 keys[keys_len] = strdup (key);
293 if (keys[keys_len] == NULL)
295 ERROR ("uc_check_timeout: strdup failed.");
298 keys_time[keys_len] = ce->last_time;
299 keys_interval[keys_len] = ce->interval;
302 } /* while (c_avl_iterator_next) */
304 c_avl_iterator_destroy (iter);
305 pthread_mutex_unlock (&cache_lock);
309 /* realloc() may have been called for these. */
312 sfree (keys_interval);
316 /* Call the "missing" callback for each value. Do this before removing the
317 * value from the cache, so that callbacks can still access the data stored,
318 * including plugin specific meta data, rates, history, …. This must be done
319 * without holding the lock, otherwise we will run into a deadlock if a
320 * plugin calls the cache interface. */
321 for (i = 0; i < keys_len; i++)
323 value_list_t vl = VALUE_LIST_INIT;
329 status = parse_identifier_vl (keys[i], &vl);
332 ERROR ("uc_check_timeout: parse_identifier_vl (\"%s\") failed.", keys[i]);
336 vl.time = keys_time[i];
337 vl.interval = keys_interval[i];
339 plugin_dispatch_missing (&vl);
340 } /* for (i = 0; i < keys_len; i++) */
342 /* Now actually remove all the values from the cache. We don't re-evaluate
343 * the timestamp again, so in theory it is possible we remove a value after
344 * it is updated here. */
345 pthread_mutex_lock (&cache_lock);
346 for (i = 0; i < keys_len; i++)
351 status = c_avl_remove (cache_tree, keys[i],
352 (void *) &key, (void *) &ce);
355 ERROR ("uc_check_timeout: c_avl_remove (\"%s\") failed.", keys[i]);
363 } /* for (i = 0; i < keys_len; i++) */
364 pthread_mutex_unlock (&cache_lock);
368 sfree (keys_interval);
371 } /* int uc_check_timeout */
373 int uc_update (const data_set_t *ds, const value_list_t *vl)
375 char name[6 * DATA_MAX_NAME_LEN];
376 cache_entry_t *ce = NULL;
380 if (FORMAT_VL (name, sizeof (name), vl) != 0)
382 ERROR ("uc_update: FORMAT_VL failed.");
386 pthread_mutex_lock (&cache_lock);
388 status = c_avl_get (cache_tree, name, (void *) &ce);
389 if (status != 0) /* entry does not yet exist */
391 status = uc_insert (ds, vl, name);
392 pthread_mutex_unlock (&cache_lock);
397 assert (ce->values_num == ds->ds_num);
399 if (ce->last_time >= vl->time)
401 pthread_mutex_unlock (&cache_lock);
402 NOTICE ("uc_update: Value too old: name = %s; value time = %.3f; "
403 "last cache update = %.3f;",
405 CDTIME_T_TO_DOUBLE (vl->time),
406 CDTIME_T_TO_DOUBLE (ce->last_time));
410 for (i = 0; i < ds->ds_num; i++)
412 switch (ds->ds[i].type)
414 case DS_TYPE_COUNTER:
418 /* check if the counter has wrapped around */
419 if (vl->values[i].counter < ce->values_raw[i].counter)
421 if (ce->values_raw[i].counter <= 4294967295U)
422 diff = (4294967295U - ce->values_raw[i].counter)
423 + vl->values[i].counter;
425 diff = (18446744073709551615ULL - ce->values_raw[i].counter)
426 + vl->values[i].counter;
428 else /* counter has NOT wrapped around */
430 diff = vl->values[i].counter - ce->values_raw[i].counter;
433 ce->values_gauge[i] = ((double) diff)
434 / (CDTIME_T_TO_DOUBLE (vl->time - ce->last_time));
435 ce->values_raw[i].counter = vl->values[i].counter;
440 ce->values_raw[i].gauge = vl->values[i].gauge;
441 ce->values_gauge[i] = vl->values[i].gauge;
448 diff = vl->values[i].derive - ce->values_raw[i].derive;
450 ce->values_gauge[i] = ((double) diff)
451 / (CDTIME_T_TO_DOUBLE (vl->time - ce->last_time));
452 ce->values_raw[i].derive = vl->values[i].derive;
456 case DS_TYPE_ABSOLUTE:
457 ce->values_gauge[i] = ((double) vl->values[i].absolute)
458 / (CDTIME_T_TO_DOUBLE (vl->time - ce->last_time));
459 ce->values_raw[i].absolute = vl->values[i].absolute;
463 /* This shouldn't happen. */
464 pthread_mutex_unlock (&cache_lock);
465 ERROR ("uc_update: Don't know how to handle data source type %i.",
468 } /* switch (ds->ds[i].type) */
470 DEBUG ("uc_update: %s: ds[%i] = %lf", name, i, ce->values_gauge[i]);
473 /* Update the history if it exists. */
474 if (ce->history != NULL)
476 assert (ce->history_index < ce->history_length);
477 for (i = 0; i < ce->values_num; i++)
479 size_t hist_idx = (ce->values_num * ce->history_index) + i;
480 ce->history[hist_idx] = ce->values_gauge[i];
483 assert (ce->history_length > 0);
484 ce->history_index = (ce->history_index + 1) % ce->history_length;
487 /* Prune invalid gauge data */
488 uc_check_range (ds, ce);
490 ce->last_time = vl->time;
491 ce->last_update = cdtime ();
492 ce->interval = vl->interval;
494 pthread_mutex_unlock (&cache_lock);
497 } /* int uc_update */
499 int uc_get_rate_by_name (const char *name, gauge_t **ret_values, size_t *ret_values_num)
503 cache_entry_t *ce = NULL;
506 pthread_mutex_lock (&cache_lock);
508 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
512 /* remove missing values from getval */
513 if (ce->state == STATE_MISSING)
519 ret_num = ce->values_num;
520 ret = (gauge_t *) malloc (ret_num * sizeof (gauge_t));
523 ERROR ("utils_cache: uc_get_rate_by_name: malloc failed.");
528 memcpy (ret, ce->values_gauge, ret_num * sizeof (gauge_t));
534 DEBUG ("utils_cache: uc_get_rate_by_name: No such value: %s", name);
538 pthread_mutex_unlock (&cache_lock);
543 *ret_values_num = ret_num;
547 } /* gauge_t *uc_get_rate_by_name */
549 gauge_t *uc_get_rate (const data_set_t *ds, const value_list_t *vl)
551 char name[6 * DATA_MAX_NAME_LEN];
556 if (FORMAT_VL (name, sizeof (name), vl) != 0)
558 ERROR ("utils_cache: uc_get_rate: FORMAT_VL failed.");
562 status = uc_get_rate_by_name (name, &ret, &ret_num);
566 /* This is important - the caller has no other way of knowing how many
567 * values are returned. */
568 if (ret_num != (size_t) ds->ds_num)
570 ERROR ("utils_cache: uc_get_rate: ds[%s] has %i values, "
571 "but uc_get_rate_by_name returned %zu.",
572 ds->type, ds->ds_num, ret_num);
578 } /* gauge_t *uc_get_rate */
580 size_t uc_get_size() {
581 size_t size_arrays = 0;
583 pthread_mutex_lock (&cache_lock);
584 size_arrays = (size_t) c_avl_size (cache_tree);
585 pthread_mutex_unlock (&cache_lock);
587 return (size_arrays);
590 int uc_get_names (char ***ret_names, cdtime_t **ret_times, size_t *ret_number)
592 c_avl_iterator_t *iter;
594 cache_entry_t *value;
597 cdtime_t *times = NULL;
599 size_t size_arrays = 0;
603 if ((ret_names == NULL) || (ret_number == NULL))
606 pthread_mutex_lock (&cache_lock);
608 size_arrays = (size_t) c_avl_size (cache_tree);
611 /* Handle the "no values" case here, to avoid the error message when
612 * calloc() returns NULL. */
613 pthread_mutex_unlock (&cache_lock);
617 names = calloc (size_arrays, sizeof (*names));
618 times = calloc (size_arrays, sizeof (*times));
619 if ((names == NULL) || (times == NULL))
621 ERROR ("uc_get_names: calloc failed.");
624 pthread_mutex_unlock (&cache_lock);
628 iter = c_avl_get_iterator (cache_tree);
629 while (c_avl_iterator_next (iter, (void *) &key, (void *) &value) == 0)
631 /* remove missing values when list values */
632 if (value->state == STATE_MISSING)
635 /* c_avl_size does not return a number smaller than the number of elements
636 * returned by c_avl_iterator_next. */
637 assert (number < size_arrays);
639 if (ret_times != NULL)
640 times[number] = value->last_time;
642 names[number] = strdup (key);
643 if (names[number] == NULL)
650 } /* while (c_avl_iterator_next) */
652 c_avl_iterator_destroy (iter);
653 pthread_mutex_unlock (&cache_lock);
659 for (i = 0; i < number; i++)
670 if (ret_times != NULL)
674 *ret_number = number;
677 } /* int uc_get_names */
679 int uc_get_state (const data_set_t *ds, const value_list_t *vl)
681 char name[6 * DATA_MAX_NAME_LEN];
682 cache_entry_t *ce = NULL;
683 int ret = STATE_ERROR;
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)
699 pthread_mutex_unlock (&cache_lock);
702 } /* int uc_get_state */
704 int uc_set_state (const data_set_t *ds, const value_list_t *vl, int state)
706 char name[6 * DATA_MAX_NAME_LEN];
707 cache_entry_t *ce = NULL;
710 if (FORMAT_VL (name, sizeof (name), vl) != 0)
712 ERROR ("uc_get_state: FORMAT_VL failed.");
713 return (STATE_ERROR);
716 pthread_mutex_lock (&cache_lock);
718 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
725 pthread_mutex_unlock (&cache_lock);
728 } /* int uc_set_state */
730 int uc_get_history_by_name (const char *name,
731 gauge_t *ret_history, size_t num_steps, size_t num_ds)
733 cache_entry_t *ce = NULL;
737 pthread_mutex_lock (&cache_lock);
739 status = c_avl_get (cache_tree, name, (void *) &ce);
742 pthread_mutex_unlock (&cache_lock);
746 if (((size_t) ce->values_num) != num_ds)
748 pthread_mutex_unlock (&cache_lock);
752 /* Check if there are enough values available. If not, increase the buffer
754 if (ce->history_length < num_steps)
759 tmp = realloc (ce->history, sizeof (*ce->history)
760 * num_steps * ce->values_num);
763 pthread_mutex_unlock (&cache_lock);
767 for (i = ce->history_length * ce->values_num;
768 i < (num_steps * ce->values_num);
773 ce->history_length = num_steps;
774 } /* if (ce->history_length < num_steps) */
776 /* Copy the values to the output buffer. */
777 for (i = 0; i < num_steps; i++)
782 if (i < ce->history_index)
783 src_index = ce->history_index - (i + 1);
785 src_index = ce->history_length + ce->history_index - (i + 1);
786 src_index = src_index * num_ds;
788 dst_index = i * num_ds;
790 memcpy (ret_history + dst_index, ce->history + src_index,
791 sizeof (*ret_history) * num_ds);
794 pthread_mutex_unlock (&cache_lock);
797 } /* int uc_get_history_by_name */
799 int uc_get_history (const data_set_t *ds, const value_list_t *vl,
800 gauge_t *ret_history, size_t num_steps, size_t num_ds)
802 char name[6 * DATA_MAX_NAME_LEN];
804 if (FORMAT_VL (name, sizeof (name), vl) != 0)
806 ERROR ("utils_cache: uc_get_history: FORMAT_VL failed.");
810 return (uc_get_history_by_name (name, ret_history, num_steps, num_ds));
811 } /* int uc_get_history */
813 int uc_get_hits (const data_set_t *ds, const value_list_t *vl)
815 char name[6 * DATA_MAX_NAME_LEN];
816 cache_entry_t *ce = NULL;
817 int ret = STATE_ERROR;
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)
833 pthread_mutex_unlock (&cache_lock);
836 } /* int uc_get_hits */
838 int uc_set_hits (const data_set_t *ds, const value_list_t *vl, int hits)
840 char name[6 * DATA_MAX_NAME_LEN];
841 cache_entry_t *ce = NULL;
844 if (FORMAT_VL (name, sizeof (name), vl) != 0)
846 ERROR ("uc_get_state: FORMAT_VL failed.");
847 return (STATE_ERROR);
850 pthread_mutex_lock (&cache_lock);
852 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
859 pthread_mutex_unlock (&cache_lock);
862 } /* int uc_set_hits */
864 int uc_inc_hits (const data_set_t *ds, const value_list_t *vl, int step)
866 char name[6 * DATA_MAX_NAME_LEN];
867 cache_entry_t *ce = NULL;
870 if (FORMAT_VL (name, sizeof (name), vl) != 0)
872 ERROR ("uc_get_state: FORMAT_VL failed.");
873 return (STATE_ERROR);
876 pthread_mutex_lock (&cache_lock);
878 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
882 ce->hits = ret + step;
885 pthread_mutex_unlock (&cache_lock);
888 } /* int uc_inc_hits */
891 * Meta data interface
893 /* XXX: This function will acquire `cache_lock' but will not free it! */
894 static meta_data_t *uc_get_meta (const value_list_t *vl) /* {{{ */
896 char name[6 * DATA_MAX_NAME_LEN];
897 cache_entry_t *ce = NULL;
900 status = FORMAT_VL (name, sizeof (name), vl);
903 ERROR ("utils_cache: uc_get_meta: FORMAT_VL failed.");
907 pthread_mutex_lock (&cache_lock);
909 status = c_avl_get (cache_tree, name, (void *) &ce);
912 pthread_mutex_unlock (&cache_lock);
917 if (ce->meta == NULL)
918 ce->meta = meta_data_create ();
920 if (ce->meta == NULL)
921 pthread_mutex_unlock (&cache_lock);
924 } /* }}} meta_data_t *uc_get_meta */
926 /* Sorry about this preprocessor magic, but it really makes this file much
928 #define UC_WRAP(wrap_function) { \
931 meta = uc_get_meta (vl); \
932 if (meta == NULL) return (-1); \
933 status = wrap_function (meta, key); \
934 pthread_mutex_unlock (&cache_lock); \
937 int uc_meta_data_exists (const value_list_t *vl, const char *key)
938 UC_WRAP (meta_data_exists)
940 int uc_meta_data_delete (const value_list_t *vl, const char *key)
941 UC_WRAP (meta_data_delete)
944 /* We need a new version of this macro because the following functions take
946 #define UC_WRAP(wrap_function) { \
949 meta = uc_get_meta (vl); \
950 if (meta == NULL) return (-1); \
951 status = wrap_function (meta, key, value); \
952 pthread_mutex_unlock (&cache_lock); \
955 int uc_meta_data_add_string (const value_list_t *vl,
958 UC_WRAP(meta_data_add_string)
959 int uc_meta_data_add_signed_int (const value_list_t *vl,
962 UC_WRAP(meta_data_add_signed_int)
963 int uc_meta_data_add_unsigned_int (const value_list_t *vl,
966 UC_WRAP(meta_data_add_unsigned_int)
967 int uc_meta_data_add_double (const value_list_t *vl,
970 UC_WRAP(meta_data_add_double)
971 int uc_meta_data_add_boolean (const value_list_t *vl,
974 UC_WRAP(meta_data_add_boolean)
976 int uc_meta_data_get_string (const value_list_t *vl,
979 UC_WRAP(meta_data_get_string)
980 int uc_meta_data_get_signed_int (const value_list_t *vl,
983 UC_WRAP(meta_data_get_signed_int)
984 int uc_meta_data_get_unsigned_int (const value_list_t *vl,
987 UC_WRAP(meta_data_get_unsigned_int)
988 int uc_meta_data_get_double (const value_list_t *vl,
991 UC_WRAP(meta_data_get_double)
992 int uc_meta_data_get_boolean (const value_list_t *vl,
995 UC_WRAP(meta_data_get_boolean)
998 /* vim: set sw=2 ts=8 sts=2 tw=78 : */