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 static void uc_check_range (const data_set_t *ds, cache_entry_t *ce)
126 for (i = 0; i < ds->ds_num; i++)
128 if (isnan (ce->values_gauge[i]))
130 else if (ce->values_gauge[i] < ds->ds[i].min)
131 ce->values_gauge[i] = NAN;
132 else if (ce->values_gauge[i] > ds->ds[i].max)
133 ce->values_gauge[i] = NAN;
135 } /* void uc_check_range */
137 static int uc_insert (const data_set_t *ds, const value_list_t *vl,
144 /* `cache_lock' has been locked by `uc_update' */
146 key_copy = strdup (key);
147 if (key_copy == NULL)
149 ERROR ("uc_insert: strdup failed.");
153 ce = cache_alloc (ds->ds_num);
157 ERROR ("uc_insert: cache_alloc (%i) failed.", ds->ds_num);
161 sstrncpy (ce->name, key, sizeof (ce->name));
163 for (i = 0; i < ds->ds_num; i++)
165 switch (ds->ds[i].type)
167 case DS_TYPE_COUNTER:
168 ce->values_gauge[i] = NAN;
169 ce->values_raw[i].counter = vl->values[i].counter;
173 ce->values_gauge[i] = vl->values[i].gauge;
174 ce->values_raw[i].gauge = vl->values[i].gauge;
178 ce->values_gauge[i] = NAN;
179 ce->values_raw[i].derive = vl->values[i].derive;
182 case DS_TYPE_ABSOLUTE:
183 ce->values_gauge[i] = NAN;
184 if (vl->interval > 0)
185 ce->values_gauge[i] = ((double) vl->values[i].absolute)
186 / CDTIME_T_TO_DOUBLE (vl->interval);
187 ce->values_raw[i].absolute = vl->values[i].absolute;
191 /* This shouldn't happen. */
192 ERROR ("uc_insert: Don't know how to handle data source type %i.",
195 } /* switch (ds->ds[i].type) */
198 /* Prune invalid gauge data */
199 uc_check_range (ds, ce);
201 ce->last_time = vl->time;
202 ce->last_update = cdtime ();
203 ce->interval = vl->interval;
204 ce->state = STATE_OKAY;
206 if (c_avl_insert (cache_tree, key_copy, ce) != 0)
209 ERROR ("uc_insert: c_avl_insert failed.");
213 DEBUG ("uc_insert: Added %s to the cache.", key);
215 } /* int uc_insert */
219 if (cache_tree == NULL)
220 cache_tree = c_avl_create ((int (*) (const void *, const void *))
226 int uc_check_timeout (void)
232 cdtime_t *keys_time = NULL;
233 cdtime_t *keys_interval = NULL;
237 c_avl_iterator_t *iter;
242 pthread_mutex_lock (&cache_lock);
246 /* Build a list of entries to be flushed */
247 iter = c_avl_get_iterator (cache_tree);
248 while (c_avl_iterator_next (iter, (void *) &key, (void *) &ce) == 0)
253 /* If the entry is fresh enough, continue. */
254 if ((now - ce->last_update) < (ce->interval * timeout_g))
257 /* If entry has not been updated, add to `keys' array */
258 tmp = (char **) realloc ((void *) keys,
259 (keys_len + 1) * sizeof (char *));
262 ERROR ("uc_check_timeout: realloc failed.");
267 tmp_time = realloc (keys_time, (keys_len + 1) * sizeof (*keys_time));
268 if (tmp_time == NULL)
270 ERROR ("uc_check_timeout: realloc failed.");
273 keys_time = tmp_time;
275 tmp_time = realloc (keys_interval, (keys_len + 1) * sizeof (*keys_interval));
276 if (tmp_time == NULL)
278 ERROR ("uc_check_timeout: realloc failed.");
281 keys_interval = tmp_time;
283 keys[keys_len] = strdup (key);
284 if (keys[keys_len] == NULL)
286 ERROR ("uc_check_timeout: strdup failed.");
289 keys_time[keys_len] = ce->last_time;
290 keys_interval[keys_len] = ce->interval;
293 } /* while (c_avl_iterator_next) */
295 c_avl_iterator_destroy (iter);
296 pthread_mutex_unlock (&cache_lock);
301 /* Call the "missing" callback for each value. Do this before removing the
302 * value from the cache, so that callbacks can still access the data stored,
303 * including plugin specific meta data, rates, history, …. This must be done
304 * without holding the lock, otherwise we will run into a deadlock if a
305 * plugin calls the cache interface. */
306 for (i = 0; i < keys_len; i++)
308 value_list_t vl = VALUE_LIST_INIT;
314 status = parse_identifier_vl (keys[i], &vl);
317 ERROR ("uc_check_timeout: parse_identifier_vl (\"%s\") failed.", keys[i]);
322 vl.time = keys_time[i];
323 vl.interval = keys_interval[i];
325 plugin_dispatch_missing (&vl);
326 } /* for (i = 0; i < keys_len; i++) */
328 /* Now actually remove all the values from the cache. We don't re-evaluate
329 * the timestamp again, so in theory it is possible we remove a value after
330 * it is updated here. */
331 pthread_mutex_lock (&cache_lock);
332 for (i = 0; i < keys_len; i++)
337 status = c_avl_remove (cache_tree, keys[i],
338 (void *) &key, (void *) &ce);
341 ERROR ("uc_check_timeout: c_avl_remove (\"%s\") failed.", keys[i]);
349 } /* for (i = 0; i < keys_len; i++) */
350 pthread_mutex_unlock (&cache_lock);
354 sfree (keys_interval);
357 } /* int uc_check_timeout */
359 int uc_update (const data_set_t *ds, const value_list_t *vl)
361 char name[6 * DATA_MAX_NAME_LEN];
362 cache_entry_t *ce = NULL;
366 if (FORMAT_VL (name, sizeof (name), vl) != 0)
368 ERROR ("uc_update: FORMAT_VL failed.");
372 pthread_mutex_lock (&cache_lock);
374 status = c_avl_get (cache_tree, name, (void *) &ce);
375 if (status != 0) /* entry does not yet exist */
377 status = uc_insert (ds, vl, name);
378 pthread_mutex_unlock (&cache_lock);
383 assert (ce->values_num == ds->ds_num);
385 if (ce->last_time >= vl->time)
387 pthread_mutex_unlock (&cache_lock);
388 NOTICE ("uc_update: Value too old: name = %s; value time = %.3f; "
389 "last cache update = %.3f;",
391 CDTIME_T_TO_DOUBLE (vl->time),
392 CDTIME_T_TO_DOUBLE (ce->last_time));
396 for (i = 0; i < ds->ds_num; i++)
398 switch (ds->ds[i].type)
400 case DS_TYPE_COUNTER:
404 /* check if the counter has wrapped around */
405 if (vl->values[i].counter < ce->values_raw[i].counter)
407 if (ce->values_raw[i].counter <= 4294967295U)
408 diff = (4294967295U - ce->values_raw[i].counter)
409 + vl->values[i].counter;
411 diff = (18446744073709551615ULL - ce->values_raw[i].counter)
412 + vl->values[i].counter;
414 else /* counter has NOT wrapped around */
416 diff = vl->values[i].counter - ce->values_raw[i].counter;
419 ce->values_gauge[i] = ((double) diff)
420 / (CDTIME_T_TO_DOUBLE (vl->time - ce->last_time));
421 ce->values_raw[i].counter = vl->values[i].counter;
426 ce->values_raw[i].gauge = vl->values[i].gauge;
427 ce->values_gauge[i] = vl->values[i].gauge;
434 diff = vl->values[i].derive - ce->values_raw[i].derive;
436 ce->values_gauge[i] = ((double) diff)
437 / (CDTIME_T_TO_DOUBLE (vl->time - ce->last_time));
438 ce->values_raw[i].derive = vl->values[i].derive;
442 case DS_TYPE_ABSOLUTE:
443 ce->values_gauge[i] = ((double) vl->values[i].absolute)
444 / (CDTIME_T_TO_DOUBLE (vl->time - ce->last_time));
445 ce->values_raw[i].absolute = vl->values[i].absolute;
449 /* This shouldn't happen. */
450 pthread_mutex_unlock (&cache_lock);
451 ERROR ("uc_update: Don't know how to handle data source type %i.",
454 } /* switch (ds->ds[i].type) */
456 DEBUG ("uc_update: %s: ds[%i] = %lf", name, i, ce->values_gauge[i]);
459 /* Update the history if it exists. */
460 if (ce->history != NULL)
462 assert (ce->history_index < ce->history_length);
463 for (i = 0; i < ce->values_num; i++)
465 size_t hist_idx = (ce->values_num * ce->history_index) + i;
466 ce->history[hist_idx] = ce->values_gauge[i];
469 assert (ce->history_length > 0);
470 ce->history_index = (ce->history_index + 1) % ce->history_length;
473 /* Prune invalid gauge data */
474 uc_check_range (ds, ce);
476 ce->last_time = vl->time;
477 ce->last_update = cdtime ();
478 ce->interval = vl->interval;
480 pthread_mutex_unlock (&cache_lock);
483 } /* int uc_update */
485 int uc_get_rate_by_name (const char *name, gauge_t **ret_values, size_t *ret_values_num)
489 cache_entry_t *ce = NULL;
492 pthread_mutex_lock (&cache_lock);
494 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
498 /* remove missing values from getval */
499 if (ce->state == STATE_MISSING)
505 ret_num = ce->values_num;
506 ret = (gauge_t *) malloc (ret_num * sizeof (gauge_t));
509 ERROR ("utils_cache: uc_get_rate_by_name: malloc failed.");
514 memcpy (ret, ce->values_gauge, ret_num * sizeof (gauge_t));
520 DEBUG ("utils_cache: uc_get_rate_by_name: No such value: %s", name);
524 pthread_mutex_unlock (&cache_lock);
529 *ret_values_num = ret_num;
533 } /* gauge_t *uc_get_rate_by_name */
535 gauge_t *uc_get_rate (const data_set_t *ds, const value_list_t *vl)
537 char name[6 * DATA_MAX_NAME_LEN];
542 if (FORMAT_VL (name, sizeof (name), vl) != 0)
544 ERROR ("utils_cache: uc_get_rate: FORMAT_VL failed.");
548 status = uc_get_rate_by_name (name, &ret, &ret_num);
552 /* This is important - the caller has no other way of knowing how many
553 * values are returned. */
554 if (ret_num != (size_t) ds->ds_num)
556 ERROR ("utils_cache: uc_get_rate: ds[%s] has %i values, "
557 "but uc_get_rate_by_name returned %zu.",
558 ds->type, ds->ds_num, ret_num);
564 } /* gauge_t *uc_get_rate */
566 int uc_get_names (char ***ret_names, cdtime_t **ret_times, size_t *ret_number)
568 c_avl_iterator_t *iter;
570 cache_entry_t *value;
573 cdtime_t *times = NULL;
575 size_t size_arrays = 0;
579 if ((ret_names == NULL) || (ret_number == NULL))
582 pthread_mutex_lock (&cache_lock);
584 size_arrays = (size_t) c_avl_size (cache_tree);
587 /* Handle the "no values" case here, to avoid the error message when
588 * calloc() returns NULL. */
589 pthread_mutex_unlock (&cache_lock);
593 names = calloc (size_arrays, sizeof (*names));
594 times = calloc (size_arrays, sizeof (*times));
595 if ((names == NULL) || (times == NULL))
597 ERROR ("uc_get_names: calloc failed.");
600 pthread_mutex_unlock (&cache_lock);
604 iter = c_avl_get_iterator (cache_tree);
605 while (c_avl_iterator_next (iter, (void *) &key, (void *) &value) == 0)
607 /* remove missing values when list values */
608 if (value->state == STATE_MISSING)
611 /* c_avl_size does not return a number smaller than the number of elements
612 * returned by c_avl_iterator_next. */
613 assert (number < size_arrays);
615 if (ret_times != NULL)
616 times[number] = value->last_time;
618 names[number] = strdup (key);
619 if (names[number] == NULL)
626 } /* while (c_avl_iterator_next) */
628 c_avl_iterator_destroy (iter);
629 pthread_mutex_unlock (&cache_lock);
635 for (i = 0; i < number; i++)
645 if (ret_times != NULL)
647 *ret_number = number;
650 } /* int uc_get_names */
652 int uc_get_state (const data_set_t *ds, const value_list_t *vl)
654 char name[6 * DATA_MAX_NAME_LEN];
655 cache_entry_t *ce = NULL;
656 int ret = STATE_ERROR;
658 if (FORMAT_VL (name, sizeof (name), vl) != 0)
660 ERROR ("uc_get_state: FORMAT_VL failed.");
661 return (STATE_ERROR);
664 pthread_mutex_lock (&cache_lock);
666 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
672 pthread_mutex_unlock (&cache_lock);
675 } /* int uc_get_state */
677 int uc_set_state (const data_set_t *ds, const value_list_t *vl, int state)
679 char name[6 * DATA_MAX_NAME_LEN];
680 cache_entry_t *ce = NULL;
683 if (FORMAT_VL (name, sizeof (name), vl) != 0)
685 ERROR ("uc_get_state: FORMAT_VL failed.");
686 return (STATE_ERROR);
689 pthread_mutex_lock (&cache_lock);
691 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
698 pthread_mutex_unlock (&cache_lock);
701 } /* int uc_set_state */
703 int uc_get_history_by_name (const char *name,
704 gauge_t *ret_history, size_t num_steps, size_t num_ds)
706 cache_entry_t *ce = NULL;
710 pthread_mutex_lock (&cache_lock);
712 status = c_avl_get (cache_tree, name, (void *) &ce);
715 pthread_mutex_unlock (&cache_lock);
719 if (((size_t) ce->values_num) != num_ds)
721 pthread_mutex_unlock (&cache_lock);
725 /* Check if there are enough values available. If not, increase the buffer
727 if (ce->history_length < num_steps)
732 tmp = realloc (ce->history, sizeof (*ce->history)
733 * num_steps * ce->values_num);
736 pthread_mutex_unlock (&cache_lock);
740 for (i = ce->history_length * ce->values_num;
741 i < (num_steps * ce->values_num);
746 ce->history_length = num_steps;
747 } /* if (ce->history_length < num_steps) */
749 /* Copy the values to the output buffer. */
750 for (i = 0; i < num_steps; i++)
755 if (i < ce->history_index)
756 src_index = ce->history_index - (i + 1);
758 src_index = ce->history_length + ce->history_index - (i + 1);
759 src_index = src_index * num_ds;
761 dst_index = i * num_ds;
763 memcpy (ret_history + dst_index, ce->history + src_index,
764 sizeof (*ret_history) * num_ds);
767 pthread_mutex_unlock (&cache_lock);
770 } /* int uc_get_history_by_name */
772 int uc_get_history (const data_set_t *ds, const value_list_t *vl,
773 gauge_t *ret_history, size_t num_steps, size_t num_ds)
775 char name[6 * DATA_MAX_NAME_LEN];
777 if (FORMAT_VL (name, sizeof (name), vl) != 0)
779 ERROR ("utils_cache: uc_get_history: FORMAT_VL failed.");
783 return (uc_get_history_by_name (name, ret_history, num_steps, num_ds));
784 } /* int uc_get_history */
786 int uc_get_hits (const data_set_t *ds, const value_list_t *vl)
788 char name[6 * DATA_MAX_NAME_LEN];
789 cache_entry_t *ce = NULL;
790 int ret = STATE_ERROR;
792 if (FORMAT_VL (name, sizeof (name), vl) != 0)
794 ERROR ("uc_get_state: FORMAT_VL failed.");
795 return (STATE_ERROR);
798 pthread_mutex_lock (&cache_lock);
800 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
806 pthread_mutex_unlock (&cache_lock);
809 } /* int uc_get_hits */
811 int uc_set_hits (const data_set_t *ds, const value_list_t *vl, int hits)
813 char name[6 * DATA_MAX_NAME_LEN];
814 cache_entry_t *ce = NULL;
817 if (FORMAT_VL (name, sizeof (name), vl) != 0)
819 ERROR ("uc_get_state: FORMAT_VL failed.");
820 return (STATE_ERROR);
823 pthread_mutex_lock (&cache_lock);
825 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
832 pthread_mutex_unlock (&cache_lock);
835 } /* int uc_set_hits */
837 int uc_inc_hits (const data_set_t *ds, const value_list_t *vl, int step)
839 char name[6 * DATA_MAX_NAME_LEN];
840 cache_entry_t *ce = NULL;
843 if (FORMAT_VL (name, sizeof (name), vl) != 0)
845 ERROR ("uc_get_state: FORMAT_VL failed.");
846 return (STATE_ERROR);
849 pthread_mutex_lock (&cache_lock);
851 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
855 ce->hits = ret + step;
858 pthread_mutex_unlock (&cache_lock);
861 } /* int uc_inc_hits */
864 * Meta data interface
866 /* XXX: This function will acquire `cache_lock' but will not free it! */
867 static meta_data_t *uc_get_meta (const value_list_t *vl) /* {{{ */
869 char name[6 * DATA_MAX_NAME_LEN];
870 cache_entry_t *ce = NULL;
873 status = FORMAT_VL (name, sizeof (name), vl);
876 ERROR ("utils_cache: uc_get_meta: FORMAT_VL failed.");
880 pthread_mutex_lock (&cache_lock);
882 status = c_avl_get (cache_tree, name, (void *) &ce);
885 pthread_mutex_unlock (&cache_lock);
890 if (ce->meta == NULL)
891 ce->meta = meta_data_create ();
893 if (ce->meta == NULL)
894 pthread_mutex_unlock (&cache_lock);
897 } /* }}} meta_data_t *uc_get_meta */
899 /* Sorry about this preprocessor magic, but it really makes this file much
901 #define UC_WRAP(wrap_function) { \
904 meta = uc_get_meta (vl); \
905 if (meta == NULL) return (-1); \
906 status = wrap_function (meta, key); \
907 pthread_mutex_unlock (&cache_lock); \
910 int uc_meta_data_exists (const value_list_t *vl, const char *key)
911 UC_WRAP (meta_data_exists)
913 int uc_meta_data_delete (const value_list_t *vl, const char *key)
914 UC_WRAP (meta_data_delete)
917 /* We need a new version of this macro because the following functions take
919 #define UC_WRAP(wrap_function) { \
922 meta = uc_get_meta (vl); \
923 if (meta == NULL) return (-1); \
924 status = wrap_function (meta, key, value); \
925 pthread_mutex_unlock (&cache_lock); \
928 int uc_meta_data_add_string (const value_list_t *vl,
931 UC_WRAP(meta_data_add_string)
932 int uc_meta_data_add_signed_int (const value_list_t *vl,
935 UC_WRAP(meta_data_add_signed_int)
936 int uc_meta_data_add_unsigned_int (const value_list_t *vl,
939 UC_WRAP(meta_data_add_unsigned_int)
940 int uc_meta_data_add_double (const value_list_t *vl,
943 UC_WRAP(meta_data_add_double)
944 int uc_meta_data_add_boolean (const value_list_t *vl,
947 UC_WRAP(meta_data_add_boolean)
949 int uc_meta_data_get_string (const value_list_t *vl,
952 UC_WRAP(meta_data_get_string)
953 int uc_meta_data_get_signed_int (const value_list_t *vl,
956 UC_WRAP(meta_data_get_signed_int)
957 int uc_meta_data_get_unsigned_int (const value_list_t *vl,
960 UC_WRAP(meta_data_get_unsigned_int)
961 int uc_meta_data_get_double (const value_list_t *vl,
964 UC_WRAP(meta_data_get_double)
965 int uc_meta_data_get_boolean (const value_list_t *vl,
968 UC_WRAP(meta_data_get_boolean)
971 /* vim: set sw=2 ts=8 sts=2 tw=78 : */