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;
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 int uc_send_notification (const char *name)
124 cache_entry_t *ce = NULL;
130 char *plugin_instance;
136 name_copy = strdup (name);
137 if (name_copy == NULL)
139 ERROR ("uc_send_notification: strdup failed.");
143 status = parse_identifier (name_copy, &host,
144 &plugin, &plugin_instance,
145 &type, &type_instance);
148 ERROR ("uc_send_notification: Cannot parse name `%s'", name);
152 /* Copy the associative members */
153 notification_init (&n, NOTIF_FAILURE, /* host = */ NULL,
154 host, plugin, plugin_instance, type, type_instance);
157 name_copy = host = plugin = plugin_instance = type = type_instance = NULL;
159 pthread_mutex_lock (&cache_lock);
162 * Set the time _after_ getting the lock because we don't know how long
163 * acquiring the lock takes and we will use this time later to decide
164 * whether or not the state is OKAY.
166 n.time = time (NULL);
168 status = c_avl_get (cache_tree, name, (void *) &ce);
171 pthread_mutex_unlock (&cache_lock);
176 /* Check if the entry has been updated in the meantime */
177 if ((n.time - ce->last_update) < (2 * ce->interval))
179 ce->state = STATE_OKAY;
180 pthread_mutex_unlock (&cache_lock);
185 ssnprintf (n.message, sizeof (n.message),
186 "%s has not been updated for %i seconds.", name,
187 (int) (n.time - ce->last_update));
189 pthread_mutex_unlock (&cache_lock);
191 plugin_dispatch_notification (&n);
194 } /* int uc_send_notification */
196 static void uc_check_range (const data_set_t *ds, cache_entry_t *ce)
200 for (i = 0; i < ds->ds_num; i++)
202 if (isnan (ce->values_gauge[i]))
204 else if (ce->values_gauge[i] < ds->ds[i].min)
205 ce->values_gauge[i] = NAN;
206 else if (ce->values_gauge[i] > ds->ds[i].max)
207 ce->values_gauge[i] = NAN;
209 } /* void uc_check_range */
211 static int uc_insert (const data_set_t *ds, const value_list_t *vl,
218 /* `cache_lock' has been locked by `uc_update' */
220 key_copy = strdup (key);
221 if (key_copy == NULL)
223 ERROR ("uc_insert: strdup failed.");
227 ce = cache_alloc (ds->ds_num);
231 ERROR ("uc_insert: cache_alloc (%i) failed.", ds->ds_num);
235 sstrncpy (ce->name, key, sizeof (ce->name));
237 for (i = 0; i < ds->ds_num; i++)
239 switch (ds->ds[i].type)
241 case DS_TYPE_COUNTER:
242 ce->values_gauge[i] = NAN;
243 ce->values_raw[i].counter = vl->values[i].counter;
247 ce->values_gauge[i] = vl->values[i].gauge;
248 ce->values_raw[i].gauge = vl->values[i].gauge;
252 ce->values_gauge[i] = NAN;
253 ce->values_raw[i].derive = vl->values[i].derive;
256 case DS_TYPE_ABSOLUTE:
257 ce->values_gauge[i] = NAN;
258 if (vl->interval > 0)
259 ce->values_gauge[i] = ((double) vl->values[i].absolute)
260 / ((double) vl->interval);
261 ce->values_raw[i].absolute = vl->values[i].absolute;
265 /* This shouldn't happen. */
266 ERROR ("uc_insert: Don't know how to handle data source type %i.",
269 } /* switch (ds->ds[i].type) */
272 /* Prune invalid gauge data */
273 uc_check_range (ds, ce);
275 ce->last_time = vl->time;
276 ce->last_update = time (NULL);
277 ce->interval = vl->interval;
278 ce->state = STATE_OKAY;
280 if (c_avl_insert (cache_tree, key_copy, ce) != 0)
283 ERROR ("uc_insert: c_avl_insert failed.");
287 DEBUG ("uc_insert: Added %s to the cache.", key);
289 } /* int uc_insert */
293 if (cache_tree == NULL)
294 cache_tree = c_avl_create ((int (*) (const void *, const void *))
300 int uc_check_timeout (void)
309 c_avl_iterator_t *iter;
312 pthread_mutex_lock (&cache_lock);
316 /* Build a list of entries to be flushed */
317 iter = c_avl_get_iterator (cache_tree);
318 while (c_avl_iterator_next (iter, (void *) &key, (void *) &ce) == 0)
320 /* If entry has not been updated, add to `keys' array */
321 if ((now - ce->last_update) >= (2 * ce->interval))
325 tmp = (char **) realloc ((void *) keys,
326 (keys_len + 1) * sizeof (char *));
329 ERROR ("uc_check_timeout: realloc failed.");
330 c_avl_iterator_destroy (iter);
332 pthread_mutex_unlock (&cache_lock);
337 keys[keys_len] = strdup (key);
338 if (keys[keys_len] == NULL)
340 ERROR ("uc_check_timeout: strdup failed.");
345 } /* while (c_avl_iterator_next) */
349 for (i = 0; i < keys_len; i++)
353 status = ut_check_interesting (keys[i]);
357 ERROR ("uc_check_timeout: ut_check_interesting failed.");
361 else if (status == 0) /* ``service'' is uninteresting */
363 DEBUG ("uc_check_timeout: %s is missing but ``uninteresting''",
366 status = c_avl_remove (cache_tree, keys[i],
367 (void *) &key, (void *) &ce);
370 ERROR ("uc_check_timeout: c_avl_remove (%s) failed.", keys[i]);
379 /* If we get here, the value is ``interesting''. Query the record from the
380 * cache and update the state field. */
381 if (c_avl_get (cache_tree, keys[i], (void *) &ce) != 0)
383 ERROR ("uc_check_timeout: cannot get data for %s from cache", keys[i]);
384 /* Do not free `keys[i]' so a notification is sent further down. */
389 if (status == 2) /* persist */
391 DEBUG ("uc_check_timeout: %s is missing, sending notification.",
393 ce->state = STATE_MISSING;
394 /* Do not free `keys[i]' so a notification is sent further down. */
396 else if (status == 1) /* do not persist */
398 if (ce->state == STATE_MISSING)
400 DEBUG ("uc_check_timeout: %s is missing but "
401 "notification has already been sent.",
403 /* Set `keys[i]' to NULL to no notification is sent. */
406 else /* (ce->state != STATE_MISSING) */
408 DEBUG ("uc_check_timeout: %s is missing, sending one notification.",
410 ce->state = STATE_MISSING;
411 /* Do not free `keys[i]' so a notification is sent further down. */
416 WARNING ("uc_check_timeout: ut_check_interesting (%s) returned "
417 "invalid status %i.",
422 /* Make really sure the next iteration doesn't work with this pointer.
423 * There have been too many bugs in the past.. :/ -- octo */
425 } /* for (keys[i]) */
427 c_avl_iterator_destroy (iter);
429 pthread_mutex_unlock (&cache_lock);
431 for (i = 0; i < keys_len; i++)
436 uc_send_notification (keys[i]);
443 } /* int uc_check_timeout */
445 int uc_update (const data_set_t *ds, const value_list_t *vl)
447 char name[6 * DATA_MAX_NAME_LEN];
448 cache_entry_t *ce = NULL;
449 int send_okay_notification = 0;
450 time_t update_delay = 0;
455 if (FORMAT_VL (name, sizeof (name), vl) != 0)
457 ERROR ("uc_update: FORMAT_VL failed.");
461 pthread_mutex_lock (&cache_lock);
463 status = c_avl_get (cache_tree, name, (void *) &ce);
464 if (status != 0) /* entry does not yet exist */
466 status = uc_insert (ds, vl, name);
467 pthread_mutex_unlock (&cache_lock);
472 assert (ce->values_num == ds->ds_num);
474 if (ce->last_time >= vl->time)
476 pthread_mutex_unlock (&cache_lock);
477 NOTICE ("uc_update: Value too old: name = %s; value time = %u; "
478 "last cache update = %u;",
479 name, (unsigned int) vl->time, (unsigned int) ce->last_time);
483 /* Send a notification (after the lock has been released) if we switch the
484 * state from something else to `okay'. */
485 if (ce->state == STATE_MISSING)
487 send_okay_notification = 1;
488 ce->state = STATE_OKAY;
489 update_delay = time (NULL) - ce->last_update;
492 for (i = 0; i < ds->ds_num; i++)
494 switch (ds->ds[i].type)
496 case DS_TYPE_COUNTER:
500 /* check if the counter has wrapped around */
501 if (vl->values[i].counter < ce->values_raw[i].counter)
503 if (ce->values_raw[i].counter <= 4294967295U)
504 diff = (4294967295U - ce->values_raw[i].counter)
505 + vl->values[i].counter;
507 diff = (18446744073709551615ULL - ce->values_raw[i].counter)
508 + vl->values[i].counter;
510 else /* counter has NOT wrapped around */
512 diff = vl->values[i].counter - ce->values_raw[i].counter;
515 ce->values_gauge[i] = ((double) diff)
516 / ((double) (vl->time - ce->last_time));
517 ce->values_raw[i].counter = vl->values[i].counter;
522 ce->values_raw[i].gauge = vl->values[i].gauge;
523 ce->values_gauge[i] = vl->values[i].gauge;
530 diff = vl->values[i].derive - ce->values_raw[i].derive;
532 ce->values_gauge[i] = ((double) diff)
533 / ((double) (vl->time - ce->last_time));
534 ce->values_raw[i].derive = vl->values[i].derive;
538 case DS_TYPE_ABSOLUTE:
539 ce->values_gauge[i] = ((double) vl->values[i].absolute)
540 / ((double) (vl->time - ce->last_time));
541 ce->values_raw[i].absolute = vl->values[i].absolute;
545 /* This shouldn't happen. */
546 pthread_mutex_unlock (&cache_lock);
547 ERROR ("uc_update: Don't know how to handle data source type %i.",
550 } /* switch (ds->ds[i].type) */
552 DEBUG ("uc_update: %s: ds[%i] = %lf", name, i, ce->values_gauge[i]);
555 /* Update the history if it exists. */
556 if (ce->history != NULL)
558 assert (ce->history_index < ce->history_length);
559 for (i = 0; i < ce->values_num; i++)
561 size_t hist_idx = (ce->values_num * ce->history_index) + i;
562 ce->history[hist_idx] = ce->values_gauge[i];
565 assert (ce->history_length > 0);
566 ce->history_index = (ce->history_index + 1) % ce->history_length;
569 /* Prune invalid gauge data */
570 uc_check_range (ds, ce);
572 ce->last_time = vl->time;
573 ce->last_update = time (NULL);
574 ce->interval = vl->interval;
576 pthread_mutex_unlock (&cache_lock);
578 if (send_okay_notification == 0)
581 /* Do not send okay notifications for uninteresting values, i. e. values for
582 * which no threshold is configured. */
583 status = ut_check_interesting (name);
587 /* Initialize the notification */
588 memset (&n, '\0', sizeof (n));
589 NOTIFICATION_INIT_VL (&n, vl, ds);
591 n.severity = NOTIF_OKAY;
594 ssnprintf (n.message, sizeof (n.message),
595 "Received a value for %s. It was missing for %u seconds.",
596 name, (unsigned int) update_delay);
598 plugin_dispatch_notification (&n);
601 } /* int uc_update */
603 int uc_get_rate_by_name (const char *name, gauge_t **ret_values, size_t *ret_values_num)
607 cache_entry_t *ce = NULL;
610 pthread_mutex_lock (&cache_lock);
612 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
616 /* remove missing values from getval */
617 if (ce->state == STATE_MISSING)
623 ret_num = ce->values_num;
624 ret = (gauge_t *) malloc (ret_num * sizeof (gauge_t));
627 ERROR ("utils_cache: uc_get_rate_by_name: malloc failed.");
632 memcpy (ret, ce->values_gauge, ret_num * sizeof (gauge_t));
638 DEBUG ("utils_cache: uc_get_rate_by_name: No such value: %s", name);
642 pthread_mutex_unlock (&cache_lock);
647 *ret_values_num = ret_num;
651 } /* gauge_t *uc_get_rate_by_name */
653 gauge_t *uc_get_rate (const data_set_t *ds, const value_list_t *vl)
655 char name[6 * DATA_MAX_NAME_LEN];
660 if (FORMAT_VL (name, sizeof (name), vl) != 0)
662 ERROR ("utils_cache: uc_get_rate: FORMAT_VL failed.");
666 status = uc_get_rate_by_name (name, &ret, &ret_num);
670 /* This is important - the caller has no other way of knowing how many
671 * values are returned. */
672 if (ret_num != (size_t) ds->ds_num)
674 ERROR ("utils_cache: uc_get_rate: ds[%s] has %i values, "
675 "but uc_get_rate_by_name returned %zu.",
676 ds->type, ds->ds_num, ret_num);
682 } /* gauge_t *uc_get_rate */
684 int uc_get_names (char ***ret_names, time_t **ret_times, size_t *ret_number)
686 c_avl_iterator_t *iter;
688 cache_entry_t *value;
691 time_t *times = NULL;
696 if ((ret_names == NULL) || (ret_number == NULL))
699 pthread_mutex_lock (&cache_lock);
701 iter = c_avl_get_iterator (cache_tree);
702 while (c_avl_iterator_next (iter, (void *) &key, (void *) &value) == 0)
706 /* remove missing values when list values */
707 if (value->state == STATE_MISSING)
710 if (ret_times != NULL)
714 tmp_times = (time_t *) realloc (times, sizeof (time_t) * (number + 1));
715 if (tmp_times == NULL)
721 times[number] = value->last_time;
724 temp = (char **) realloc (names, sizeof (char *) * (number + 1));
731 names[number] = strdup (key);
732 if (names[number] == NULL)
738 } /* while (c_avl_iterator_next) */
740 c_avl_iterator_destroy (iter);
741 pthread_mutex_unlock (&cache_lock);
747 for (i = 0; i < number; i++)
757 if (ret_times != NULL)
759 *ret_number = number;
762 } /* int uc_get_names */
764 int uc_get_state (const data_set_t *ds, const value_list_t *vl)
766 char name[6 * DATA_MAX_NAME_LEN];
767 cache_entry_t *ce = NULL;
768 int ret = STATE_ERROR;
770 if (FORMAT_VL (name, sizeof (name), vl) != 0)
772 ERROR ("uc_get_state: FORMAT_VL failed.");
773 return (STATE_ERROR);
776 pthread_mutex_lock (&cache_lock);
778 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
784 pthread_mutex_unlock (&cache_lock);
787 } /* int uc_get_state */
789 int uc_set_state (const data_set_t *ds, const value_list_t *vl, int state)
791 char name[6 * DATA_MAX_NAME_LEN];
792 cache_entry_t *ce = NULL;
795 if (FORMAT_VL (name, sizeof (name), vl) != 0)
797 ERROR ("uc_get_state: FORMAT_VL failed.");
798 return (STATE_ERROR);
801 pthread_mutex_lock (&cache_lock);
803 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
810 pthread_mutex_unlock (&cache_lock);
813 } /* int uc_set_state */
815 int uc_get_history_by_name (const char *name,
816 gauge_t *ret_history, size_t num_steps, size_t num_ds)
818 cache_entry_t *ce = NULL;
822 pthread_mutex_lock (&cache_lock);
824 status = c_avl_get (cache_tree, name, (void *) &ce);
827 pthread_mutex_unlock (&cache_lock);
831 if (((size_t) ce->values_num) != num_ds)
833 pthread_mutex_unlock (&cache_lock);
837 /* Check if there are enough values available. If not, increase the buffer
839 if (ce->history_length < num_steps)
844 tmp = realloc (ce->history, sizeof (*ce->history)
845 * num_steps * ce->values_num);
848 pthread_mutex_unlock (&cache_lock);
852 for (i = ce->history_length * ce->values_num;
853 i < (num_steps * ce->values_num);
858 ce->history_length = num_steps;
859 } /* if (ce->history_length < num_steps) */
861 /* Copy the values to the output buffer. */
862 for (i = 0; i < num_steps; i++)
867 if (i < ce->history_index)
868 src_index = ce->history_index - (i + 1);
870 src_index = ce->history_length + ce->history_index - (i + 1);
871 src_index = src_index * num_ds;
873 dst_index = i * num_ds;
875 memcpy (ret_history + dst_index, ce->history + src_index,
876 sizeof (*ret_history) * num_ds);
879 pthread_mutex_unlock (&cache_lock);
882 } /* int uc_get_history_by_name */
884 int uc_get_history (const data_set_t *ds, const value_list_t *vl,
885 gauge_t *ret_history, size_t num_steps, size_t num_ds)
887 char name[6 * DATA_MAX_NAME_LEN];
889 if (FORMAT_VL (name, sizeof (name), vl) != 0)
891 ERROR ("utils_cache: uc_get_history: FORMAT_VL failed.");
895 return (uc_get_history_by_name (name, ret_history, num_steps, num_ds));
896 } /* int uc_get_history */
899 * Meta data interface
901 /* XXX: This function will acquire `cache_lock' but will not free it! */
902 static meta_data_t *uc_get_meta (const value_list_t *vl) /* {{{ */
904 char name[6 * DATA_MAX_NAME_LEN];
905 cache_entry_t *ce = NULL;
908 status = FORMAT_VL (name, sizeof (name), vl);
911 ERROR ("utils_cache: uc_get_meta: FORMAT_VL failed.");
915 pthread_mutex_lock (&cache_lock);
917 status = c_avl_get (cache_tree, name, (void *) &ce);
920 pthread_mutex_unlock (&cache_lock);
925 if (ce->meta == NULL)
926 ce->meta = meta_data_create ();
929 } /* }}} meta_data_t *uc_get_meta */
931 /* Sorry about this preprocessor magic, but it really makes this file much
933 #define UC_WRAP(wrap_function) { \
936 meta = uc_get_meta (vl); \
937 if (meta == NULL) return (-1); \
938 status = wrap_function (meta, key); \
939 pthread_mutex_unlock (&cache_lock); \
942 int uc_meta_data_exists (const value_list_t *vl, const char *key)
943 UC_WRAP (meta_data_exists)
945 int uc_meta_data_delete (const value_list_t *vl, const char *key)
946 UC_WRAP (meta_data_delete)
949 /* We need a new version of this macro because the following functions take
951 #define UC_WRAP(wrap_function) { \
954 meta = uc_get_meta (vl); \
955 if (meta == NULL) return (-1); \
956 status = wrap_function (meta, key, value); \
957 pthread_mutex_unlock (&cache_lock); \
960 int uc_meta_data_add_string (const value_list_t *vl,
963 UC_WRAP(meta_data_add_string)
964 int uc_meta_data_add_signed_int (const value_list_t *vl,
967 UC_WRAP(meta_data_add_signed_int)
968 int uc_meta_data_add_unsigned_int (const value_list_t *vl,
971 UC_WRAP(meta_data_add_unsigned_int)
972 int uc_meta_data_add_double (const value_list_t *vl,
975 UC_WRAP(meta_data_add_double)
976 int uc_meta_data_add_boolean (const value_list_t *vl,
979 UC_WRAP(meta_data_add_boolean)
981 int uc_meta_data_get_string (const value_list_t *vl,
984 UC_WRAP(meta_data_get_string)
985 int uc_meta_data_get_signed_int (const value_list_t *vl,
988 UC_WRAP(meta_data_get_signed_int)
989 int uc_meta_data_get_unsigned_int (const value_list_t *vl,
992 UC_WRAP(meta_data_get_unsigned_int)
993 int uc_meta_data_get_double (const value_list_t *vl,
996 UC_WRAP(meta_data_get_double)
997 int uc_meta_data_get_boolean (const value_list_t *vl,
1000 UC_WRAP(meta_data_get_boolean)
1003 /* vim: set sw=2 ts=8 sts=2 tw=78 : */