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''",
365 status = c_avl_remove (cache_tree, keys[i],
366 (void *) &key, (void *) &ce);
369 ERROR ("uc_check_timeout: c_avl_remove (%s) failed.", keys[i]);
377 /* If we get here, the value is ``interesting''. Query the record from the
378 * cache and update the state field. */
379 if (c_avl_get (cache_tree, keys[i], (void *) &ce) != 0)
381 ERROR ("uc_check_timeout: cannot get data for %s from cache", keys[i]);
382 /* Do not free `keys[i]' so a notification is sent further down. */
387 if (status == 2) /* persist */
389 DEBUG ("uc_check_timeout: %s is missing, sending notification.",
391 ce->state = STATE_MISSING;
392 /* Do not free `keys[i]' so a notification is sent further down. */
394 else if (status == 1) /* do not persist */
396 if (ce->state == STATE_MISSING)
398 DEBUG ("uc_check_timeout: %s is missing but "
399 "notification has already been sent.",
401 /* Set `keys[i]' to NULL to no notification is sent. */
404 else /* (ce->state != STATE_MISSING) */
406 DEBUG ("uc_check_timeout: %s is missing, sending one notification.",
408 ce->state = STATE_MISSING;
409 /* Do not free `keys[i]' so a notification is sent further down. */
414 WARNING ("uc_check_timeout: ut_check_interesting (%s) returned "
415 "invalid status %i.",
420 /* Make really sure the next iteration doesn't work with this pointer.
421 * There have been too many bugs in the past.. :/ -- octo */
423 } /* for (keys[i]) */
425 c_avl_iterator_destroy (iter);
427 pthread_mutex_unlock (&cache_lock);
429 for (i = 0; i < keys_len; i++)
434 uc_send_notification (keys[i]);
441 } /* int uc_check_timeout */
443 int uc_update (const data_set_t *ds, const value_list_t *vl)
445 char name[6 * DATA_MAX_NAME_LEN];
446 cache_entry_t *ce = NULL;
447 int send_okay_notification = 0;
448 time_t update_delay = 0;
453 if (FORMAT_VL (name, sizeof (name), vl) != 0)
455 ERROR ("uc_update: FORMAT_VL failed.");
459 pthread_mutex_lock (&cache_lock);
461 status = c_avl_get (cache_tree, name, (void *) &ce);
462 if (status != 0) /* entry does not yet exist */
464 status = uc_insert (ds, vl, name);
465 pthread_mutex_unlock (&cache_lock);
470 assert (ce->values_num == ds->ds_num);
472 if (ce->last_time >= vl->time)
474 pthread_mutex_unlock (&cache_lock);
475 NOTICE ("uc_update: Value too old: name = %s; value time = %u; "
476 "last cache update = %u;",
477 name, (unsigned int) vl->time, (unsigned int) ce->last_time);
481 /* Send a notification (after the lock has been released) if we switch the
482 * state from something else to `okay'. */
483 if (ce->state == STATE_MISSING)
485 send_okay_notification = 1;
486 ce->state = STATE_OKAY;
487 update_delay = time (NULL) - ce->last_update;
490 for (i = 0; i < ds->ds_num; i++)
492 switch (ds->ds[i].type)
494 case DS_TYPE_COUNTER:
498 /* check if the counter has wrapped around */
499 if (vl->values[i].counter < ce->values_raw[i].counter)
501 if (ce->values_raw[i].counter <= 4294967295U)
502 diff = (4294967295U - ce->values_raw[i].counter)
503 + vl->values[i].counter;
505 diff = (18446744073709551615ULL - ce->values_raw[i].counter)
506 + vl->values[i].counter;
508 else /* counter has NOT wrapped around */
510 diff = vl->values[i].counter - ce->values_raw[i].counter;
513 ce->values_gauge[i] = ((double) diff)
514 / ((double) (vl->time - ce->last_time));
515 ce->values_raw[i].counter = vl->values[i].counter;
520 ce->values_raw[i].gauge = vl->values[i].gauge;
521 ce->values_gauge[i] = vl->values[i].gauge;
528 diff = vl->values[i].derive - ce->values_raw[i].derive;
530 ce->values_gauge[i] = ((double) diff)
531 / ((double) (vl->time - ce->last_time));
532 ce->values_raw[i].derive = vl->values[i].derive;
536 case DS_TYPE_ABSOLUTE:
537 ce->values_gauge[i] = ((double) vl->values[i].absolute)
538 / ((double) (vl->time - ce->last_time));
539 ce->values_raw[i].absolute = vl->values[i].absolute;
543 /* This shouldn't happen. */
544 pthread_mutex_unlock (&cache_lock);
545 ERROR ("uc_update: Don't know how to handle data source type %i.",
548 } /* switch (ds->ds[i].type) */
550 DEBUG ("uc_update: %s: ds[%i] = %lf", name, i, ce->values_gauge[i]);
553 /* Update the history if it exists. */
554 if (ce->history != NULL)
556 assert (ce->history_index < ce->history_length);
557 for (i = 0; i < ce->values_num; i++)
559 size_t hist_idx = (ce->values_num * ce->history_index) + i;
560 ce->history[hist_idx] = ce->values_gauge[i];
563 assert (ce->history_length > 0);
564 ce->history_index = (ce->history_index + 1) % ce->history_length;
567 /* Prune invalid gauge data */
568 uc_check_range (ds, ce);
570 ce->last_time = vl->time;
571 ce->last_update = time (NULL);
572 ce->interval = vl->interval;
574 pthread_mutex_unlock (&cache_lock);
576 if (send_okay_notification == 0)
579 /* Do not send okay notifications for uninteresting values, i. e. values for
580 * which no threshold is configured. */
581 status = ut_check_interesting (name);
585 /* Initialize the notification */
586 memset (&n, '\0', sizeof (n));
587 NOTIFICATION_INIT_VL (&n, vl, ds);
589 n.severity = NOTIF_OKAY;
592 ssnprintf (n.message, sizeof (n.message),
593 "Received a value for %s. It was missing for %u seconds.",
594 name, (unsigned int) update_delay);
596 plugin_dispatch_notification (&n);
599 } /* int uc_update */
601 int uc_get_rate_by_name (const char *name, gauge_t **ret_values, size_t *ret_values_num)
605 cache_entry_t *ce = NULL;
608 pthread_mutex_lock (&cache_lock);
610 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
614 ret_num = ce->values_num;
615 ret = (gauge_t *) malloc (ret_num * sizeof (gauge_t));
618 ERROR ("utils_cache: uc_get_rate_by_name: malloc failed.");
623 memcpy (ret, ce->values_gauge, ret_num * sizeof (gauge_t));
628 DEBUG ("utils_cache: uc_get_rate_by_name: No such value: %s", name);
632 pthread_mutex_unlock (&cache_lock);
637 *ret_values_num = ret_num;
641 } /* gauge_t *uc_get_rate_by_name */
643 gauge_t *uc_get_rate (const data_set_t *ds, const value_list_t *vl)
645 char name[6 * DATA_MAX_NAME_LEN];
650 if (FORMAT_VL (name, sizeof (name), vl) != 0)
652 ERROR ("utils_cache: uc_get_rate: FORMAT_VL failed.");
656 status = uc_get_rate_by_name (name, &ret, &ret_num);
660 /* This is important - the caller has no other way of knowing how many
661 * values are returned. */
662 if (ret_num != (size_t) ds->ds_num)
664 ERROR ("utils_cache: uc_get_rate: ds[%s] has %i values, "
665 "but uc_get_rate_by_name returned %zu.",
666 ds->type, ds->ds_num, ret_num);
672 } /* gauge_t *uc_get_rate */
674 int uc_get_names (char ***ret_names, time_t **ret_times, size_t *ret_number)
676 c_avl_iterator_t *iter;
678 cache_entry_t *value;
681 time_t *times = NULL;
686 if ((ret_names == NULL) || (ret_number == NULL))
689 pthread_mutex_lock (&cache_lock);
691 iter = c_avl_get_iterator (cache_tree);
692 while (c_avl_iterator_next (iter, (void *) &key, (void *) &value) == 0)
696 if (ret_times != NULL)
700 tmp_times = (time_t *) realloc (times, sizeof (time_t) * (number + 1));
701 if (tmp_times == NULL)
707 times[number] = value->last_time;
710 temp = (char **) realloc (names, sizeof (char *) * (number + 1));
717 names[number] = strdup (key);
718 if (names[number] == NULL)
724 } /* while (c_avl_iterator_next) */
726 c_avl_iterator_destroy (iter);
727 pthread_mutex_unlock (&cache_lock);
733 for (i = 0; i < number; i++)
743 if (ret_times != NULL)
745 *ret_number = number;
748 } /* int uc_get_names */
750 int uc_get_state (const data_set_t *ds, const value_list_t *vl)
752 char name[6 * DATA_MAX_NAME_LEN];
753 cache_entry_t *ce = NULL;
754 int ret = STATE_ERROR;
756 if (FORMAT_VL (name, sizeof (name), vl) != 0)
758 ERROR ("uc_get_state: FORMAT_VL failed.");
759 return (STATE_ERROR);
762 pthread_mutex_lock (&cache_lock);
764 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
770 pthread_mutex_unlock (&cache_lock);
773 } /* int uc_get_state */
775 int uc_set_state (const data_set_t *ds, const value_list_t *vl, int state)
777 char name[6 * DATA_MAX_NAME_LEN];
778 cache_entry_t *ce = NULL;
781 if (FORMAT_VL (name, sizeof (name), vl) != 0)
783 ERROR ("uc_get_state: FORMAT_VL failed.");
784 return (STATE_ERROR);
787 pthread_mutex_lock (&cache_lock);
789 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
796 pthread_mutex_unlock (&cache_lock);
799 } /* int uc_set_state */
801 int uc_get_history_by_name (const char *name,
802 gauge_t *ret_history, size_t num_steps, size_t num_ds)
804 cache_entry_t *ce = NULL;
808 pthread_mutex_lock (&cache_lock);
810 status = c_avl_get (cache_tree, name, (void *) &ce);
813 pthread_mutex_unlock (&cache_lock);
817 if (((size_t) ce->values_num) != num_ds)
819 pthread_mutex_unlock (&cache_lock);
823 /* Check if there are enough values available. If not, increase the buffer
825 if (ce->history_length < num_steps)
830 tmp = realloc (ce->history, sizeof (*ce->history)
831 * num_steps * ce->values_num);
834 pthread_mutex_unlock (&cache_lock);
838 for (i = ce->history_length * ce->values_num;
839 i < (num_steps * ce->values_num);
844 ce->history_length = num_steps;
845 } /* if (ce->history_length < num_steps) */
847 /* Copy the values to the output buffer. */
848 for (i = 0; i < num_steps; i++)
853 if (i < ce->history_index)
854 src_index = ce->history_index - (i + 1);
856 src_index = ce->history_length + ce->history_index - (i + 1);
857 src_index = src_index * num_ds;
859 dst_index = i * num_ds;
861 memcpy (ret_history + dst_index, ce->history + src_index,
862 sizeof (*ret_history) * num_ds);
865 pthread_mutex_unlock (&cache_lock);
868 } /* int uc_get_history_by_name */
870 int uc_get_history (const data_set_t *ds, const value_list_t *vl,
871 gauge_t *ret_history, size_t num_steps, size_t num_ds)
873 char name[6 * DATA_MAX_NAME_LEN];
875 if (FORMAT_VL (name, sizeof (name), vl) != 0)
877 ERROR ("utils_cache: uc_get_history: FORMAT_VL failed.");
881 return (uc_get_history_by_name (name, ret_history, num_steps, num_ds));
882 } /* int uc_get_history */
885 * Meta data interface
887 /* XXX: This function will acquire `cache_lock' but will not free it! */
888 static meta_data_t *uc_get_meta (const value_list_t *vl) /* {{{ */
890 char name[6 * DATA_MAX_NAME_LEN];
891 cache_entry_t *ce = NULL;
894 status = FORMAT_VL (name, sizeof (name), vl);
897 ERROR ("utils_cache: uc_get_meta: FORMAT_VL failed.");
901 pthread_mutex_lock (&cache_lock);
903 status = c_avl_get (cache_tree, name, (void *) &ce);
906 pthread_mutex_unlock (&cache_lock);
911 if (ce->meta == NULL)
912 ce->meta = meta_data_create ();
915 } /* }}} meta_data_t *uc_get_meta */
917 /* Sorry about this preprocessor magic, but it really makes this file much
919 #define UC_WRAP(wrap_function) { \
922 meta = uc_get_meta (vl); \
923 if (meta == NULL) return (-1); \
924 status = wrap_function (meta, key); \
925 pthread_mutex_unlock (&cache_lock); \
928 int uc_meta_data_exists (const value_list_t *vl, const char *key)
929 UC_WRAP (meta_data_exists)
931 int uc_meta_data_delete (const value_list_t *vl, const char *key)
932 UC_WRAP (meta_data_delete)
935 /* We need a new version of this macro because the following functions take
937 #define UC_WRAP(wrap_function) { \
940 meta = uc_get_meta (vl); \
941 if (meta == NULL) return (-1); \
942 status = wrap_function (meta, key, value); \
943 pthread_mutex_unlock (&cache_lock); \
946 int uc_meta_data_add_string (const value_list_t *vl,
949 UC_WRAP(meta_data_add_string)
950 int uc_meta_data_add_signed_int (const value_list_t *vl,
953 UC_WRAP(meta_data_add_signed_int)
954 int uc_meta_data_add_unsigned_int (const value_list_t *vl,
957 UC_WRAP(meta_data_add_unsigned_int)
958 int uc_meta_data_add_double (const value_list_t *vl,
961 UC_WRAP(meta_data_add_double)
962 int uc_meta_data_add_boolean (const value_list_t *vl,
965 UC_WRAP(meta_data_add_boolean)
967 int uc_meta_data_get_string (const value_list_t *vl,
970 UC_WRAP(meta_data_get_string)
971 int uc_meta_data_get_signed_int (const value_list_t *vl,
974 UC_WRAP(meta_data_get_signed_int)
975 int uc_meta_data_get_unsigned_int (const value_list_t *vl,
978 UC_WRAP(meta_data_get_unsigned_int)
979 int uc_meta_data_get_double (const value_list_t *vl,
982 UC_WRAP(meta_data_get_double)
983 int uc_meta_data_get_boolean (const value_list_t *vl,
986 UC_WRAP(meta_data_get_boolean)
989 /* vim: set sw=2 ts=8 sts=2 tw=78 : */