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"
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) */
50 * +-----+-----+-----+-----+-----+-----+-----+-----+-----+----
51 * ! 0 ! 1 ! 2 ! 3 ! 4 ! 5 ! 6 ! 7 ! 8 ! ...
52 * +-----+-----+-----+-----+-----+-----+-----+-----+-----+----
53 * ! ds0 ! ds1 ! ds2 ! ds0 ! ds1 ! ds2 ! ds0 ! ds1 ! ds2 ! ...
54 * +-----+-----+-----+-----+-----+-----+-----+-----+-----+----
55 * ! t = 0 ! t = 1 ! t = 2 ! ...
56 * +-----------------+-----------------+-----------------+----
59 size_t history_index; /* points to the next position to write to. */
60 size_t history_length;
63 static c_avl_tree_t *cache_tree = NULL;
64 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
66 static int cache_compare (const cache_entry_t *a, const cache_entry_t *b)
68 assert ((a != NULL) && (b != NULL));
69 return (strcmp (a->name, b->name));
70 } /* int cache_compare */
72 static cache_entry_t *cache_alloc (int values_num)
76 ce = (cache_entry_t *) malloc (sizeof (cache_entry_t));
79 ERROR ("utils_cache: cache_alloc: malloc failed.");
82 memset (ce, '\0', sizeof (cache_entry_t));
83 ce->values_num = values_num;
85 ce->values_gauge = calloc (values_num, sizeof (*ce->values_gauge));
86 ce->values_raw = calloc (values_num, sizeof (*ce->values_raw));
87 if ((ce->values_gauge == NULL) || (ce->values_raw == NULL))
89 sfree (ce->values_gauge);
90 sfree (ce->values_raw);
92 ERROR ("utils_cache: cache_alloc: calloc failed.");
97 ce->history_length = 0;
100 } /* cache_entry_t *cache_alloc */
102 static void cache_free (cache_entry_t *ce)
107 sfree (ce->values_gauge);
108 sfree (ce->values_raw);
111 } /* void cache_free */
113 static int uc_send_notification (const char *name)
115 cache_entry_t *ce = NULL;
121 char *plugin_instance;
127 name_copy = strdup (name);
128 if (name_copy == NULL)
130 ERROR ("uc_send_notification: strdup failed.");
134 status = parse_identifier (name_copy, &host,
135 &plugin, &plugin_instance,
136 &type, &type_instance);
139 ERROR ("uc_send_notification: Cannot parse name `%s'", name);
143 /* Copy the associative members */
144 notification_init (&n, NOTIF_FAILURE, /* host = */ NULL,
145 host, plugin, plugin_instance, type, type_instance);
148 name_copy = host = plugin = plugin_instance = type = type_instance = NULL;
150 pthread_mutex_lock (&cache_lock);
153 * Set the time _after_ getting the lock because we don't know how long
154 * acquiring the lock takes and we will use this time later to decide
155 * whether or not the state is OKAY.
157 n.time = time (NULL);
159 status = c_avl_get (cache_tree, name, (void *) &ce);
162 pthread_mutex_unlock (&cache_lock);
167 /* Check if the entry has been updated in the meantime */
168 if ((n.time - ce->last_update) < (2 * ce->interval))
170 ce->state = STATE_OKAY;
171 pthread_mutex_unlock (&cache_lock);
176 ssnprintf (n.message, sizeof (n.message),
177 "%s has not been updated for %i seconds.", name,
178 (int) (n.time - ce->last_update));
180 pthread_mutex_unlock (&cache_lock);
182 plugin_dispatch_notification (&n);
185 } /* int uc_send_notification */
187 static void uc_check_range (const data_set_t *ds, cache_entry_t *ce)
191 for (i = 0; i < ds->ds_num; i++)
193 if (isnan (ce->values_gauge[i]))
195 else if (ce->values_gauge[i] < ds->ds[i].min)
196 ce->values_gauge[i] = NAN;
197 else if (ce->values_gauge[i] > ds->ds[i].max)
198 ce->values_gauge[i] = NAN;
200 } /* void uc_check_range */
202 static int uc_insert (const data_set_t *ds, const value_list_t *vl,
209 /* `cache_lock' has been locked by `uc_update' */
211 key_copy = strdup (key);
212 if (key_copy == NULL)
214 ERROR ("uc_insert: strdup failed.");
218 ce = cache_alloc (ds->ds_num);
222 ERROR ("uc_insert: cache_alloc (%i) failed.", ds->ds_num);
226 sstrncpy (ce->name, key, sizeof (ce->name));
228 for (i = 0; i < ds->ds_num; i++)
230 switch (ds->ds[i].type)
232 case DS_TYPE_COUNTER:
233 ce->values_gauge[i] = NAN;
234 ce->values_raw[i].counter = vl->values[i].counter;
238 ce->values_gauge[i] = vl->values[i].gauge;
239 ce->values_raw[i].gauge = vl->values[i].gauge;
243 ce->values_gauge[i] = NAN;
244 ce->values_raw[i].derive = vl->values[i].derive;
247 case DS_TYPE_ABSOLUTE:
248 ce->values_gauge[i] = NAN;
249 if (vl->interval > 0)
250 ce->values_gauge[i] = ((double) vl->values[i].absolute)
251 / ((double) vl->interval);
252 ce->values_raw[i].absolute = vl->values[i].absolute;
256 /* This shouldn't happen. */
257 ERROR ("uc_insert: Don't know how to handle data source type %i.",
260 } /* switch (ds->ds[i].type) */
263 /* Prune invalid gauge data */
264 uc_check_range (ds, ce);
266 ce->last_time = vl->time;
267 ce->last_update = time (NULL);
268 ce->interval = vl->interval;
269 ce->state = STATE_OKAY;
271 if (c_avl_insert (cache_tree, key_copy, ce) != 0)
274 ERROR ("uc_insert: c_avl_insert failed.");
278 DEBUG ("uc_insert: Added %s to the cache.", key);
280 } /* int uc_insert */
284 if (cache_tree == NULL)
285 cache_tree = c_avl_create ((int (*) (const void *, const void *))
291 int uc_check_timeout (void)
300 c_avl_iterator_t *iter;
303 pthread_mutex_lock (&cache_lock);
307 /* Build a list of entries to be flushed */
308 iter = c_avl_get_iterator (cache_tree);
309 while (c_avl_iterator_next (iter, (void *) &key, (void *) &ce) == 0)
311 /* If entry has not been updated, add to `keys' array */
312 if ((now - ce->last_update) >= (2 * ce->interval))
316 tmp = (char **) realloc ((void *) keys,
317 (keys_len + 1) * sizeof (char *));
320 ERROR ("uc_check_timeout: realloc failed.");
321 c_avl_iterator_destroy (iter);
323 pthread_mutex_unlock (&cache_lock);
328 keys[keys_len] = strdup (key);
329 if (keys[keys_len] == NULL)
331 ERROR ("uc_check_timeout: strdup failed.");
336 } /* while (c_avl_iterator_next) */
340 for (i = 0; i < keys_len; i++)
344 status = ut_check_interesting (keys[i]);
348 ERROR ("uc_check_timeout: ut_check_interesting failed.");
352 else if (status == 0) /* ``service'' is uninteresting */
354 DEBUG ("uc_check_timeout: %s is missing but ``uninteresting''",
356 status = c_avl_remove (cache_tree, keys[i],
357 (void *) &key, (void *) &ce);
360 ERROR ("uc_check_timeout: c_avl_remove (%s) failed.", keys[i]);
368 /* If we get here, the value is ``interesting''. Query the record from the
369 * cache and update the state field. */
370 if (c_avl_get (cache_tree, keys[i], (void *) &ce) != 0)
372 ERROR ("uc_check_timeout: cannot get data for %s from cache", keys[i]);
373 /* Do not free `keys[i]' so a notification is sent further down. */
378 if (status == 2) /* persist */
380 DEBUG ("uc_check_timeout: %s is missing, sending notification.",
382 ce->state = STATE_MISSING;
383 /* Do not free `keys[i]' so a notification is sent further down. */
385 else if (status == 1) /* do not persist */
387 if (ce->state == STATE_MISSING)
389 DEBUG ("uc_check_timeout: %s is missing but "
390 "notification has already been sent.",
392 /* Set `keys[i]' to NULL to no notification is sent. */
395 else /* (ce->state != STATE_MISSING) */
397 DEBUG ("uc_check_timeout: %s is missing, sending one notification.",
399 ce->state = STATE_MISSING;
400 /* Do not free `keys[i]' so a notification is sent further down. */
405 WARNING ("uc_check_timeout: ut_check_interesting (%s) returned "
406 "invalid status %i.",
411 /* Make really sure the next iteration doesn't work with this pointer.
412 * There have been too many bugs in the past.. :/ -- octo */
414 } /* for (keys[i]) */
416 c_avl_iterator_destroy (iter);
418 pthread_mutex_unlock (&cache_lock);
420 for (i = 0; i < keys_len; i++)
425 uc_send_notification (keys[i]);
432 } /* int uc_check_timeout */
434 int uc_update (const data_set_t *ds, const value_list_t *vl)
436 char name[6 * DATA_MAX_NAME_LEN];
437 cache_entry_t *ce = NULL;
438 int send_okay_notification = 0;
439 time_t update_delay = 0;
444 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
446 ERROR ("uc_update: FORMAT_VL failed.");
450 pthread_mutex_lock (&cache_lock);
452 status = c_avl_get (cache_tree, name, (void *) &ce);
453 if (status != 0) /* entry does not yet exist */
455 status = uc_insert (ds, vl, name);
456 pthread_mutex_unlock (&cache_lock);
461 assert (ce->values_num == ds->ds_num);
463 if (ce->last_time >= vl->time)
465 pthread_mutex_unlock (&cache_lock);
466 NOTICE ("uc_update: Value too old: name = %s; value time = %u; "
467 "last cache update = %u;",
468 name, (unsigned int) vl->time, (unsigned int) ce->last_time);
472 /* Send a notification (after the lock has been released) if we switch the
473 * state from something else to `okay'. */
474 if (ce->state == STATE_MISSING)
476 send_okay_notification = 1;
477 ce->state = STATE_OKAY;
478 update_delay = time (NULL) - ce->last_update;
481 for (i = 0; i < ds->ds_num; i++)
483 switch (ds->ds[i].type)
485 case DS_TYPE_COUNTER:
489 /* check if the counter has wrapped around */
490 if (vl->values[i].counter < ce->values_raw[i].counter)
492 if (ce->values_raw[i].counter <= 4294967295U)
493 diff = (4294967295U - ce->values_raw[i].counter)
494 + vl->values[i].counter;
496 diff = (18446744073709551615ULL - ce->values_raw[i].counter)
497 + vl->values[i].counter;
499 else /* counter has NOT wrapped around */
501 diff = vl->values[i].counter - ce->values_raw[i].counter;
504 ce->values_gauge[i] = ((double) diff)
505 / ((double) (vl->time - ce->last_time));
506 ce->values_raw[i].counter = vl->values[i].counter;
511 ce->values_raw[i].gauge = vl->values[i].gauge;
512 ce->values_gauge[i] = vl->values[i].gauge;
519 diff = vl->values[i].derive - ce->values_raw[i].derive;
521 ce->values_gauge[i] = ((double) diff)
522 / ((double) (vl->time - ce->last_time));
523 ce->values_raw[i].derive = vl->values[i].derive;
527 case DS_TYPE_ABSOLUTE:
528 ce->values_gauge[i] = ((double) vl->values[i].absolute)
529 / ((double) (vl->time - ce->last_time));
530 ce->values_raw[i].absolute = vl->values[i].absolute;
534 /* This shouldn't happen. */
535 pthread_mutex_unlock (&cache_lock);
536 ERROR ("uc_update: Don't know how to handle data source type %i.",
539 } /* switch (ds->ds[i].type) */
541 DEBUG ("uc_update: %s: ds[%i] = %lf", name, i, ce->values_gauge[i]);
544 /* Update the history if it exists. */
545 if (ce->history != NULL)
547 assert (ce->history_index < ce->history_length);
548 for (i = 0; i < ce->values_num; i++)
550 size_t hist_idx = (ce->values_num * ce->history_index) + i;
551 ce->history[hist_idx] = ce->values_gauge[i];
554 assert (ce->history_length > 0);
555 ce->history_index = (ce->history_index + 1) % ce->history_length;
558 /* Prune invalid gauge data */
559 uc_check_range (ds, ce);
561 ce->last_time = vl->time;
562 ce->last_update = time (NULL);
563 ce->interval = vl->interval;
565 pthread_mutex_unlock (&cache_lock);
567 if (send_okay_notification == 0)
570 /* Do not send okay notifications for uninteresting values, i. e. values for
571 * which no threshold is configured. */
572 status = ut_check_interesting (name);
576 /* Initialize the notification */
577 memset (&n, '\0', sizeof (n));
578 NOTIFICATION_INIT_VL (&n, vl, ds);
580 n.severity = NOTIF_OKAY;
583 ssnprintf (n.message, sizeof (n.message),
584 "Received a value for %s. It was missing for %u seconds.",
585 name, (unsigned int) update_delay);
587 plugin_dispatch_notification (&n);
590 } /* int uc_update */
592 int uc_get_rate_by_name (const char *name, gauge_t **ret_values, size_t *ret_values_num)
596 cache_entry_t *ce = NULL;
599 pthread_mutex_lock (&cache_lock);
601 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
605 ret_num = ce->values_num;
606 ret = (gauge_t *) malloc (ret_num * sizeof (gauge_t));
609 ERROR ("utils_cache: uc_get_rate_by_name: malloc failed.");
614 memcpy (ret, ce->values_gauge, ret_num * sizeof (gauge_t));
619 DEBUG ("utils_cache: uc_get_rate_by_name: No such value: %s", name);
623 pthread_mutex_unlock (&cache_lock);
628 *ret_values_num = ret_num;
632 } /* gauge_t *uc_get_rate_by_name */
634 gauge_t *uc_get_rate (const data_set_t *ds, const value_list_t *vl)
636 char name[6 * DATA_MAX_NAME_LEN];
641 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
643 ERROR ("utils_cache: uc_get_rate: FORMAT_VL failed.");
647 status = uc_get_rate_by_name (name, &ret, &ret_num);
651 /* This is important - the caller has no other way of knowing how many
652 * values are returned. */
653 if (ret_num != (size_t) ds->ds_num)
655 ERROR ("utils_cache: uc_get_rate: ds[%s] has %i values, "
656 "but uc_get_rate_by_name returned %zu.",
657 ds->type, ds->ds_num, ret_num);
663 } /* gauge_t *uc_get_rate */
665 int uc_get_names (char ***ret_names, time_t **ret_times, size_t *ret_number)
667 c_avl_iterator_t *iter;
669 cache_entry_t *value;
672 time_t *times = NULL;
677 if ((ret_names == NULL) || (ret_number == NULL))
680 pthread_mutex_lock (&cache_lock);
682 iter = c_avl_get_iterator (cache_tree);
683 while (c_avl_iterator_next (iter, (void *) &key, (void *) &value) == 0)
687 if (ret_times != NULL)
691 tmp_times = (time_t *) realloc (times, sizeof (time_t) * (number + 1));
692 if (tmp_times == NULL)
698 times[number] = value->last_time;
701 temp = (char **) realloc (names, sizeof (char *) * (number + 1));
708 names[number] = strdup (key);
709 if (names[number] == NULL)
715 } /* while (c_avl_iterator_next) */
717 c_avl_iterator_destroy (iter);
718 pthread_mutex_unlock (&cache_lock);
724 for (i = 0; i < number; i++)
734 if (ret_times != NULL)
736 *ret_number = number;
739 } /* int uc_get_names */
741 int uc_get_state (const data_set_t *ds, const value_list_t *vl)
743 char name[6 * DATA_MAX_NAME_LEN];
744 cache_entry_t *ce = NULL;
745 int ret = STATE_ERROR;
747 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
749 ERROR ("uc_get_state: FORMAT_VL failed.");
750 return (STATE_ERROR);
753 pthread_mutex_lock (&cache_lock);
755 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
761 pthread_mutex_unlock (&cache_lock);
764 } /* int uc_get_state */
766 int uc_set_state (const data_set_t *ds, const value_list_t *vl, int state)
768 char name[6 * DATA_MAX_NAME_LEN];
769 cache_entry_t *ce = NULL;
772 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
774 ERROR ("uc_get_state: FORMAT_VL failed.");
775 return (STATE_ERROR);
778 pthread_mutex_lock (&cache_lock);
780 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
787 pthread_mutex_unlock (&cache_lock);
790 } /* int uc_set_state */
792 int uc_get_history_by_name (const char *name,
793 gauge_t *ret_history, size_t num_steps, size_t num_ds)
795 cache_entry_t *ce = NULL;
799 pthread_mutex_lock (&cache_lock);
801 status = c_avl_get (cache_tree, name, (void *) &ce);
804 pthread_mutex_unlock (&cache_lock);
808 if (((size_t) ce->values_num) != num_ds)
810 pthread_mutex_unlock (&cache_lock);
814 /* Check if there are enough values available. If not, increase the buffer
816 if (ce->history_length < num_steps)
821 tmp = realloc (ce->history, sizeof (*ce->history)
822 * num_steps * ce->values_num);
825 pthread_mutex_unlock (&cache_lock);
829 for (i = ce->history_length * ce->values_num;
830 i < (num_steps * ce->values_num);
835 ce->history_length = num_steps;
836 } /* if (ce->history_length < num_steps) */
838 /* Copy the values to the output buffer. */
839 for (i = 0; i < num_steps; i++)
844 if (i < ce->history_index)
845 src_index = ce->history_index - (i + 1);
847 src_index = ce->history_length + ce->history_index - (i + 1);
848 src_index = src_index * num_ds;
850 dst_index = i * num_ds;
852 memcpy (ret_history + dst_index, ce->history + src_index,
853 sizeof (*ret_history) * num_ds);
856 pthread_mutex_unlock (&cache_lock);
859 } /* int uc_get_history_by_name */
861 int uc_get_history (const data_set_t *ds, const value_list_t *vl,
862 gauge_t *ret_history, size_t num_steps, size_t num_ds)
864 char name[6 * DATA_MAX_NAME_LEN];
866 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
868 ERROR ("utils_cache: uc_get_history: FORMAT_VL failed.");
872 return (uc_get_history_by_name (name, ret_history, num_steps, num_ds));
873 } /* int uc_get_history */
875 /* vim: set sw=2 ts=8 sts=2 tw=78 : */