2 * collectd - src/utils_cache.c
3 * Copyright (C) 2007,2008 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 verplant.org>
25 #include "utils_avltree.h"
26 #include "utils_cache.h"
27 #include "utils_threshold.h"
28 #include "meta_data.h"
33 typedef struct cache_entry_s
35 char name[6 * DATA_MAX_NAME_LEN];
37 gauge_t *values_gauge;
39 /* Time contained in the package
40 * (for calculating rates) */
42 /* Time according to the local clock
43 * (for purging old entries) */
45 /* Interval in which the data is collected
46 * (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;
103 } /* cache_entry_t *cache_alloc */
105 static void cache_free (cache_entry_t *ce)
110 sfree (ce->values_gauge);
111 sfree (ce->values_raw);
114 } /* void cache_free */
116 static int uc_send_notification (const char *name)
118 cache_entry_t *ce = NULL;
124 char *plugin_instance;
130 name_copy = strdup (name);
131 if (name_copy == NULL)
133 ERROR ("uc_send_notification: strdup failed.");
137 status = parse_identifier (name_copy, &host,
138 &plugin, &plugin_instance,
139 &type, &type_instance);
142 ERROR ("uc_send_notification: Cannot parse name `%s'", name);
146 /* Copy the associative members */
147 notification_init (&n, NOTIF_FAILURE, /* host = */ NULL,
148 host, plugin, plugin_instance, type, type_instance);
151 name_copy = host = plugin = plugin_instance = type = type_instance = NULL;
153 pthread_mutex_lock (&cache_lock);
156 * Set the time _after_ getting the lock because we don't know how long
157 * acquiring the lock takes and we will use this time later to decide
158 * whether or not the state is OKAY.
160 n.time = time (NULL);
162 status = c_avl_get (cache_tree, name, (void *) &ce);
165 pthread_mutex_unlock (&cache_lock);
170 /* Check if the entry has been updated in the meantime */
171 if ((n.time - ce->last_update) < (2 * ce->interval))
173 ce->state = STATE_OKAY;
174 pthread_mutex_unlock (&cache_lock);
179 ssnprintf (n.message, sizeof (n.message),
180 "%s has not been updated for %i seconds.", name,
181 (int) (n.time - ce->last_update));
183 pthread_mutex_unlock (&cache_lock);
185 plugin_dispatch_notification (&n);
188 } /* int uc_send_notification */
190 static void uc_check_range (const data_set_t *ds, cache_entry_t *ce)
194 for (i = 0; i < ds->ds_num; i++)
196 if (isnan (ce->values_gauge[i]))
198 else if (ce->values_gauge[i] < ds->ds[i].min)
199 ce->values_gauge[i] = NAN;
200 else if (ce->values_gauge[i] > ds->ds[i].max)
201 ce->values_gauge[i] = NAN;
203 } /* void uc_check_range */
205 static int uc_insert (const data_set_t *ds, const value_list_t *vl,
212 /* `cache_lock' has been locked by `uc_update' */
214 key_copy = strdup (key);
215 if (key_copy == NULL)
217 ERROR ("uc_insert: strdup failed.");
221 ce = cache_alloc (ds->ds_num);
225 ERROR ("uc_insert: cache_alloc (%i) failed.", ds->ds_num);
229 sstrncpy (ce->name, key, sizeof (ce->name));
231 for (i = 0; i < ds->ds_num; i++)
233 switch (ds->ds[i].type)
235 case DS_TYPE_COUNTER:
236 ce->values_gauge[i] = NAN;
237 ce->values_raw[i].counter = vl->values[i].counter;
241 ce->values_gauge[i] = vl->values[i].gauge;
242 ce->values_raw[i].gauge = vl->values[i].gauge;
246 ce->values_gauge[i] = NAN;
247 ce->values_raw[i].derive = vl->values[i].derive;
250 case DS_TYPE_ABSOLUTE:
251 ce->values_gauge[i] = NAN;
252 if (vl->interval > 0)
253 ce->values_gauge[i] = ((double) vl->values[i].absolute)
254 / ((double) vl->interval);
255 ce->values_raw[i].absolute = vl->values[i].absolute;
259 /* This shouldn't happen. */
260 ERROR ("uc_insert: Don't know how to handle data source type %i.",
263 } /* switch (ds->ds[i].type) */
266 /* Prune invalid gauge data */
267 uc_check_range (ds, ce);
269 ce->last_time = vl->time;
270 ce->last_update = time (NULL);
271 ce->interval = vl->interval;
272 ce->state = STATE_OKAY;
274 if (c_avl_insert (cache_tree, key_copy, ce) != 0)
277 ERROR ("uc_insert: c_avl_insert failed.");
281 DEBUG ("uc_insert: Added %s to the cache.", key);
283 } /* int uc_insert */
287 if (cache_tree == NULL)
288 cache_tree = c_avl_create ((int (*) (const void *, const void *))
294 int uc_check_timeout (void)
303 c_avl_iterator_t *iter;
306 pthread_mutex_lock (&cache_lock);
310 /* Build a list of entries to be flushed */
311 iter = c_avl_get_iterator (cache_tree);
312 while (c_avl_iterator_next (iter, (void *) &key, (void *) &ce) == 0)
314 /* If entry has not been updated, add to `keys' array */
315 if ((now - ce->last_update) >= (2 * ce->interval))
319 tmp = (char **) realloc ((void *) keys,
320 (keys_len + 1) * sizeof (char *));
323 ERROR ("uc_check_timeout: realloc failed.");
324 c_avl_iterator_destroy (iter);
326 pthread_mutex_unlock (&cache_lock);
331 keys[keys_len] = strdup (key);
332 if (keys[keys_len] == NULL)
334 ERROR ("uc_check_timeout: strdup failed.");
339 } /* while (c_avl_iterator_next) */
343 for (i = 0; i < keys_len; i++)
347 status = ut_check_interesting (keys[i]);
351 ERROR ("uc_check_timeout: ut_check_interesting failed.");
355 else if (status == 0) /* ``service'' is uninteresting */
357 DEBUG ("uc_check_timeout: %s is missing but ``uninteresting''",
359 status = c_avl_remove (cache_tree, keys[i],
360 (void *) &key, (void *) &ce);
363 ERROR ("uc_check_timeout: c_avl_remove (%s) failed.", keys[i]);
371 /* If we get here, the value is ``interesting''. Query the record from the
372 * cache and update the state field. */
373 if (c_avl_get (cache_tree, keys[i], (void *) &ce) != 0)
375 ERROR ("uc_check_timeout: cannot get data for %s from cache", keys[i]);
376 /* Do not free `keys[i]' so a notification is sent further down. */
381 if (status == 2) /* persist */
383 DEBUG ("uc_check_timeout: %s is missing, sending notification.",
385 ce->state = STATE_MISSING;
386 /* Do not free `keys[i]' so a notification is sent further down. */
388 else if (status == 1) /* do not persist */
390 if (ce->state == STATE_MISSING)
392 DEBUG ("uc_check_timeout: %s is missing but "
393 "notification has already been sent.",
395 /* Set `keys[i]' to NULL to no notification is sent. */
398 else /* (ce->state != STATE_MISSING) */
400 DEBUG ("uc_check_timeout: %s is missing, sending one notification.",
402 ce->state = STATE_MISSING;
403 /* Do not free `keys[i]' so a notification is sent further down. */
408 WARNING ("uc_check_timeout: ut_check_interesting (%s) returned "
409 "invalid status %i.",
414 /* Make really sure the next iteration doesn't work with this pointer.
415 * There have been too many bugs in the past.. :/ -- octo */
417 } /* for (keys[i]) */
419 c_avl_iterator_destroy (iter);
421 pthread_mutex_unlock (&cache_lock);
423 for (i = 0; i < keys_len; i++)
428 uc_send_notification (keys[i]);
435 } /* int uc_check_timeout */
437 int uc_update (const data_set_t *ds, const value_list_t *vl)
439 char name[6 * DATA_MAX_NAME_LEN];
440 cache_entry_t *ce = NULL;
441 int send_okay_notification = 0;
442 time_t update_delay = 0;
447 if (FORMAT_VL (name, sizeof (name), vl) != 0)
449 ERROR ("uc_update: FORMAT_VL failed.");
453 pthread_mutex_lock (&cache_lock);
455 status = c_avl_get (cache_tree, name, (void *) &ce);
456 if (status != 0) /* entry does not yet exist */
458 status = uc_insert (ds, vl, name);
459 pthread_mutex_unlock (&cache_lock);
464 assert (ce->values_num == ds->ds_num);
466 if (ce->last_time >= vl->time)
468 pthread_mutex_unlock (&cache_lock);
469 NOTICE ("uc_update: Value too old: name = %s; value time = %u; "
470 "last cache update = %u;",
471 name, (unsigned int) vl->time, (unsigned int) ce->last_time);
475 /* Send a notification (after the lock has been released) if we switch the
476 * state from something else to `okay'. */
477 if (ce->state == STATE_MISSING)
479 send_okay_notification = 1;
480 ce->state = STATE_OKAY;
481 update_delay = time (NULL) - ce->last_update;
484 for (i = 0; i < ds->ds_num; i++)
486 switch (ds->ds[i].type)
488 case DS_TYPE_COUNTER:
492 /* check if the counter has wrapped around */
493 if (vl->values[i].counter < ce->values_raw[i].counter)
495 if (ce->values_raw[i].counter <= 4294967295U)
496 diff = (4294967295U - ce->values_raw[i].counter)
497 + vl->values[i].counter;
499 diff = (18446744073709551615ULL - ce->values_raw[i].counter)
500 + vl->values[i].counter;
502 else /* counter has NOT wrapped around */
504 diff = vl->values[i].counter - ce->values_raw[i].counter;
507 ce->values_gauge[i] = ((double) diff)
508 / ((double) (vl->time - ce->last_time));
509 ce->values_raw[i].counter = vl->values[i].counter;
514 ce->values_raw[i].gauge = vl->values[i].gauge;
515 ce->values_gauge[i] = vl->values[i].gauge;
522 diff = vl->values[i].derive - ce->values_raw[i].derive;
524 ce->values_gauge[i] = ((double) diff)
525 / ((double) (vl->time - ce->last_time));
526 ce->values_raw[i].derive = vl->values[i].derive;
530 case DS_TYPE_ABSOLUTE:
531 ce->values_gauge[i] = ((double) vl->values[i].absolute)
532 / ((double) (vl->time - ce->last_time));
533 ce->values_raw[i].absolute = vl->values[i].absolute;
537 /* This shouldn't happen. */
538 pthread_mutex_unlock (&cache_lock);
539 ERROR ("uc_update: Don't know how to handle data source type %i.",
542 } /* switch (ds->ds[i].type) */
544 DEBUG ("uc_update: %s: ds[%i] = %lf", name, i, ce->values_gauge[i]);
547 /* Update the history if it exists. */
548 if (ce->history != NULL)
550 assert (ce->history_index < ce->history_length);
551 for (i = 0; i < ce->values_num; i++)
553 size_t hist_idx = (ce->values_num * ce->history_index) + i;
554 ce->history[hist_idx] = ce->values_gauge[i];
557 assert (ce->history_length > 0);
558 ce->history_index = (ce->history_index + 1) % ce->history_length;
561 /* Prune invalid gauge data */
562 uc_check_range (ds, ce);
564 ce->last_time = vl->time;
565 ce->last_update = time (NULL);
566 ce->interval = vl->interval;
568 pthread_mutex_unlock (&cache_lock);
570 if (send_okay_notification == 0)
573 /* Do not send okay notifications for uninteresting values, i. e. values for
574 * which no threshold is configured. */
575 status = ut_check_interesting (name);
579 /* Initialize the notification */
580 memset (&n, '\0', sizeof (n));
581 NOTIFICATION_INIT_VL (&n, vl, ds);
583 n.severity = NOTIF_OKAY;
586 ssnprintf (n.message, sizeof (n.message),
587 "Received a value for %s. It was missing for %u seconds.",
588 name, (unsigned int) update_delay);
590 plugin_dispatch_notification (&n);
593 } /* int uc_update */
595 int uc_get_rate_by_name (const char *name, gauge_t **ret_values, size_t *ret_values_num)
599 cache_entry_t *ce = NULL;
602 pthread_mutex_lock (&cache_lock);
604 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
608 ret_num = ce->values_num;
609 ret = (gauge_t *) malloc (ret_num * sizeof (gauge_t));
612 ERROR ("utils_cache: uc_get_rate_by_name: malloc failed.");
617 memcpy (ret, ce->values_gauge, ret_num * sizeof (gauge_t));
622 DEBUG ("utils_cache: uc_get_rate_by_name: No such value: %s", name);
626 pthread_mutex_unlock (&cache_lock);
631 *ret_values_num = ret_num;
635 } /* gauge_t *uc_get_rate_by_name */
637 gauge_t *uc_get_rate (const data_set_t *ds, const value_list_t *vl)
639 char name[6 * DATA_MAX_NAME_LEN];
644 if (FORMAT_VL (name, sizeof (name), vl) != 0)
646 ERROR ("utils_cache: uc_get_rate: FORMAT_VL failed.");
650 status = uc_get_rate_by_name (name, &ret, &ret_num);
654 /* This is important - the caller has no other way of knowing how many
655 * values are returned. */
656 if (ret_num != (size_t) ds->ds_num)
658 ERROR ("utils_cache: uc_get_rate: ds[%s] has %i values, "
659 "but uc_get_rate_by_name returned %zu.",
660 ds->type, ds->ds_num, ret_num);
666 } /* gauge_t *uc_get_rate */
668 int uc_get_names (char ***ret_names, time_t **ret_times, size_t *ret_number)
670 c_avl_iterator_t *iter;
672 cache_entry_t *value;
675 time_t *times = NULL;
680 if ((ret_names == NULL) || (ret_number == NULL))
683 pthread_mutex_lock (&cache_lock);
685 iter = c_avl_get_iterator (cache_tree);
686 while (c_avl_iterator_next (iter, (void *) &key, (void *) &value) == 0)
690 if (ret_times != NULL)
694 tmp_times = (time_t *) realloc (times, sizeof (time_t) * (number + 1));
695 if (tmp_times == NULL)
701 times[number] = value->last_time;
704 temp = (char **) realloc (names, sizeof (char *) * (number + 1));
711 names[number] = strdup (key);
712 if (names[number] == NULL)
718 } /* while (c_avl_iterator_next) */
720 c_avl_iterator_destroy (iter);
721 pthread_mutex_unlock (&cache_lock);
727 for (i = 0; i < number; i++)
737 if (ret_times != NULL)
739 *ret_number = number;
742 } /* int uc_get_names */
744 int uc_get_state (const data_set_t *ds, const value_list_t *vl)
746 char name[6 * DATA_MAX_NAME_LEN];
747 cache_entry_t *ce = NULL;
748 int ret = STATE_ERROR;
750 if (FORMAT_VL (name, sizeof (name), vl) != 0)
752 ERROR ("uc_get_state: FORMAT_VL failed.");
753 return (STATE_ERROR);
756 pthread_mutex_lock (&cache_lock);
758 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
764 pthread_mutex_unlock (&cache_lock);
767 } /* int uc_get_state */
769 int uc_set_state (const data_set_t *ds, const value_list_t *vl, int state)
771 char name[6 * DATA_MAX_NAME_LEN];
772 cache_entry_t *ce = NULL;
775 if (FORMAT_VL (name, sizeof (name), vl) != 0)
777 ERROR ("uc_get_state: FORMAT_VL failed.");
778 return (STATE_ERROR);
781 pthread_mutex_lock (&cache_lock);
783 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
790 pthread_mutex_unlock (&cache_lock);
793 } /* int uc_set_state */
795 int uc_get_history_by_name (const char *name,
796 gauge_t *ret_history, size_t num_steps, size_t num_ds)
798 cache_entry_t *ce = NULL;
802 pthread_mutex_lock (&cache_lock);
804 status = c_avl_get (cache_tree, name, (void *) &ce);
807 pthread_mutex_unlock (&cache_lock);
811 if (((size_t) ce->values_num) != num_ds)
813 pthread_mutex_unlock (&cache_lock);
817 /* Check if there are enough values available. If not, increase the buffer
819 if (ce->history_length < num_steps)
824 tmp = realloc (ce->history, sizeof (*ce->history)
825 * num_steps * ce->values_num);
828 pthread_mutex_unlock (&cache_lock);
832 for (i = ce->history_length * ce->values_num;
833 i < (num_steps * ce->values_num);
838 ce->history_length = num_steps;
839 } /* if (ce->history_length < num_steps) */
841 /* Copy the values to the output buffer. */
842 for (i = 0; i < num_steps; i++)
847 if (i < ce->history_index)
848 src_index = ce->history_index - (i + 1);
850 src_index = ce->history_length + ce->history_index - (i + 1);
851 src_index = src_index * num_ds;
853 dst_index = i * num_ds;
855 memcpy (ret_history + dst_index, ce->history + src_index,
856 sizeof (*ret_history) * num_ds);
859 pthread_mutex_unlock (&cache_lock);
862 } /* int uc_get_history_by_name */
864 int uc_get_history (const data_set_t *ds, const value_list_t *vl,
865 gauge_t *ret_history, size_t num_steps, size_t num_ds)
867 char name[6 * DATA_MAX_NAME_LEN];
869 if (FORMAT_VL (name, sizeof (name), vl) != 0)
871 ERROR ("utils_cache: uc_get_history: FORMAT_VL failed.");
875 return (uc_get_history_by_name (name, ret_history, num_steps, num_ds));
876 } /* int uc_get_history */
879 * Meta data interface
881 /* XXX: This function will acquire `cache_lock' but will not free it! */
882 static meta_data_t *uc_get_meta (value_list_t *vl) /* {{{ */
884 char name[6 * DATA_MAX_NAME_LEN];
885 cache_entry_t *ce = NULL;
888 status = FORMAT_VL (name, sizeof (name), vl);
891 ERROR ("utils_cache: uc_get_meta: FORMAT_VL failed.");
895 pthread_mutex_lock (&cache_lock);
897 status = c_avl_get (cache_tree, name, (void *) &ce);
900 pthread_mutex_unlock (&cache_lock);
901 NOTICE ("utils_cache: uc_get_meta: No such cache entry: %s", name);
906 if (ce->meta == NULL)
907 ce->meta = meta_data_create ();
910 } /* }}} meta_data_t *uc_get_meta */
912 /* Sorry about this preprocessor magic, but it really makes this file much
914 #define UC_WRAP(wrap_function) { \
917 meta = uc_get_meta (vl); \
918 if (meta == NULL) return (-1); \
919 status = wrap_function (meta, key); \
920 pthread_mutex_unlock (&cache_lock); \
923 int uc_meta_data_exists (value_list_t *vl, const char *key)
924 UC_WRAP (meta_data_exists)
926 int uc_meta_data_delete (value_list_t *vl, const char *key)
927 UC_WRAP (meta_data_delete)
930 /* We need a new version of this macro because the following functions take
932 #define UC_WRAP(wrap_function) { \
935 meta = uc_get_meta (vl); \
936 if (meta == NULL) return (-1); \
937 status = wrap_function (meta, key, value); \
938 pthread_mutex_unlock (&cache_lock); \
941 int uc_meta_data_add_string (value_list_t *vl,
944 UC_WRAP(meta_data_add_string)
945 int uc_meta_data_add_signed_int (value_list_t *vl,
948 UC_WRAP(meta_data_add_signed_int)
949 int uc_meta_data_add_unsigned_int (value_list_t *vl,
952 UC_WRAP(meta_data_add_unsigned_int)
953 int uc_meta_data_add_double (value_list_t *vl,
956 UC_WRAP(meta_data_add_double)
957 int uc_meta_data_add_boolean (value_list_t *vl,
960 UC_WRAP(meta_data_add_boolean)
962 int uc_meta_data_get_string (value_list_t *vl,
965 UC_WRAP(meta_data_get_string)
966 int uc_meta_data_get_signed_int (value_list_t *vl,
969 UC_WRAP(meta_data_get_signed_int)
970 int uc_meta_data_get_unsigned_int (value_list_t *vl,
973 UC_WRAP(meta_data_get_unsigned_int)
974 int uc_meta_data_get_double (value_list_t *vl,
977 UC_WRAP(meta_data_get_double)
978 int uc_meta_data_get_boolean (value_list_t *vl,
981 UC_WRAP(meta_data_get_boolean)
984 /* vim: set sw=2 ts=8 sts=2 tw=78 : */