2 * collectd - src/utils_cache.c
3 * Copyright (C) 2007 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"
30 typedef struct cache_entry_s
32 char name[6 * DATA_MAX_NAME_LEN];
34 gauge_t *values_gauge;
35 counter_t *values_counter;
36 /* Time contained in the package
37 * (for calculating rates) */
39 /* Time according to the local clock
40 * (for purging old entries) */
42 /* Interval in which the data is collected
43 * (for purding old entries) */
47 static avl_tree_t *cache_tree = NULL;
48 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
50 static int cache_compare (const cache_entry_t *a, const cache_entry_t *b)
52 assert ((a != NULL) && (b != NULL));
53 return (strcmp (a->name, b->name));
54 } /* int cache_compare */
56 static int uc_send_notification (const char *name)
58 cache_entry_t *ce = NULL;
64 char *plugin_instance;
70 memset (&n, '\0', sizeof (n));
72 name_copy = strdup (name);
73 if (name_copy == NULL)
75 ERROR ("uc_send_notification: strdup failed.");
79 status = parse_identifier (name_copy, &host,
80 &plugin, &plugin_instance,
81 &type, &type_instance);
84 ERROR ("uc_send_notification: Cannot parse name `%s'", name);
88 n.severity = NOTIF_FAILURE;
89 strncpy (n.host, host, sizeof (n.host));
90 n.host[sizeof (n.host) - 1] = '\0';
93 name_copy = host = plugin = plugin_instance = type = type_instance = NULL;
95 pthread_mutex_lock (&cache_lock);
99 status = avl_get (cache_tree, name, (void *) &ce);
102 pthread_mutex_unlock (&cache_lock);
107 /* Check if the entry has been updated in the meantime */
108 if ((n.time - ce->last_update) <= (2 * ce->interval))
110 pthread_mutex_unlock (&cache_lock);
115 snprintf (n.message, sizeof (n.message),
116 "%s has not been updated for %i seconds.", name,
117 (int) (ce->last_update - n.time));
119 pthread_mutex_unlock (&cache_lock);
121 n.message[sizeof (n.message) - 1] = '\0';
122 plugin_dispatch_notification (&n);
125 } /* int uc_send_notification */
129 if (cache_tree == NULL)
130 cache_tree = avl_create ((int (*) (const void *, const void *))
136 int uc_check_timeout (void)
145 avl_iterator_t *iter;
148 pthread_mutex_lock (&cache_lock);
152 /* Build a list of entries to be flushed */
153 iter = avl_get_iterator (cache_tree);
154 while (avl_iterator_next (iter, (void *) &key, (void *) &ce) == 0)
156 /* If entry has not been updated, add to `keys' array */
157 if ((now - ce->last_update) > (2 * ce->interval))
161 tmp = (char **) realloc ((void *) keys,
162 (keys_len + 1) * sizeof (char *));
165 ERROR ("uc_purge: realloc failed.");
166 avl_iterator_destroy (iter);
171 keys[keys_len] = strdup (key);
172 if (keys[keys_len] == NULL)
174 ERROR ("uc_check_timeout: strdup failed.");
179 } /* while (avl_iterator_next) */
181 for (i = 0; i < keys_len; i++)
185 /* TODO: Check if value interesting:
186 * - Not interesting: Remove value from cache and shut up
187 * - Interesting: Don't remove value from cache but send a
190 status = avl_remove (cache_tree, keys[i], (void *) &key, (void *) &ce);
193 ERROR ("uc_check_timeout: avl_remove (%s) failed.", keys[i]);
201 avl_iterator_destroy (iter);
203 pthread_mutex_unlock (&cache_lock);
205 for (i = 0; i < keys_len; i++)
207 uc_send_notification (keys[i]);
214 } /* int uc_check_timeout */
216 int uc_update (const data_set_t *ds, const value_list_t *vl)
218 char name[6 * DATA_MAX_NAME_LEN];
219 cache_entry_t *ce = NULL;
221 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
223 ERROR ("uc_insert: FORMAT_VL failed.");
227 pthread_mutex_lock (&cache_lock);
229 if (avl_get (cache_tree, name, (void *) &ce) == 0)
234 assert (ce->values_num == ds->ds_num);
236 if (ce->last_time >= vl->time)
238 pthread_mutex_unlock (&cache_lock);
239 NOTICE ("uc_insert: Value too old: name = %s; value time = %u; "
240 "last cache update = %u;",
241 name, (unsigned int) vl->time, (unsigned int) ce->last_time);
245 if ((ce->last_time + ce->interval) < vl->time)
247 /* TODO: Implement a `real' okay notification. Watch out for locking
249 NOTICE ("uc_insert: Okay notification for %s", name);
252 for (i = 0; i < ds->ds_num; i++)
254 if (ds->ds[i].type == DS_TYPE_COUNTER)
258 /* check if the counter has wrapped around */
259 if (vl->values[i].counter < ce->values_counter[i])
261 if (ce->values_counter[i] <= 4294967295U)
262 diff = (4294967295U - ce->values_counter[i])
263 + vl->values[i].counter;
265 diff = (18446744073709551615ULL - ce->values_counter[i])
266 + vl->values[i].counter;
268 else /* counter has NOT wrapped around */
270 diff = vl->values[i].counter - ce->values_counter[i];
273 ce->values_gauge[i] = ((double) diff)
274 / ((double) (vl->time - ce->last_time));
275 ce->values_counter[i] = vl->values[i].counter;
277 else /* if (ds->ds[i].type == DS_TYPE_GAUGE) */
279 ce->values_gauge[i] = vl->values[i].gauge;
281 DEBUG ("uc_insert: %s: ds[%i] = %lf", name, i, ce->values_gauge[i]);
284 ce->last_time = vl->time;
285 ce->last_update = time (NULL);
286 ce->interval = vl->interval;
288 else /* key is not found */
291 size_t ce_size = sizeof (cache_entry_t)
292 + ds->ds_num * (sizeof (counter_t) + sizeof (gauge_t));
298 pthread_mutex_unlock (&cache_lock);
299 ERROR ("uc_insert: strdup failed.");
303 ce = (cache_entry_t *) malloc (ce_size);
306 pthread_mutex_unlock (&cache_lock);
307 ERROR ("uc_insert: malloc (%u) failed.", (unsigned int) ce_size);
311 memset (ce, '\0', ce_size);
313 strncpy (ce->name, name, sizeof (ce->name));
314 ce->name[sizeof (ce->name) - 1] = '\0';
316 ce->values_num = ds->ds_num;
317 ce->values_gauge = (gauge_t *) (ce + 1);
318 ce->values_counter = (counter_t *) (ce->values_gauge + ce->values_num);
320 for (i = 0; i < ds->ds_num; i++)
322 if (ds->ds[i].type == DS_TYPE_COUNTER)
324 ce->values_gauge[i] = NAN;
325 ce->values_counter[i] = vl->values[i].counter;
327 else /* if (ds->ds[i].type == DS_TYPE_GAUGE) */
329 ce->values_gauge[i] = vl->values[i].gauge;
333 ce->last_time = vl->time;
335 if (avl_insert (cache_tree, key, ce) != 0)
337 pthread_mutex_unlock (&cache_lock);
338 ERROR ("uc_insert: avl_insert failed.");
342 DEBUG ("uc_insert: Added %s to the cache.", name);
343 } /* if (key is not found) */
345 pthread_mutex_unlock (&cache_lock);
348 } /* int uc_insert */
350 gauge_t *uc_get_rate (const data_set_t *ds, const value_list_t *vl)
352 char name[6 * DATA_MAX_NAME_LEN];
354 cache_entry_t *ce = NULL;
356 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
358 ERROR ("uc_insert: FORMAT_VL failed.");
362 pthread_mutex_lock (&cache_lock);
364 if (avl_get (cache_tree, name, (void *) &ce) == 0)
367 assert (ce->values_num == ds->ds_num);
369 ret = (gauge_t *) malloc (ce->values_num * sizeof (gauge_t));
372 ERROR ("uc_get_rate: malloc failed.");
376 memcpy (ret, ce->values_gauge, ce->values_num * sizeof (gauge_t));
380 pthread_mutex_unlock (&cache_lock);
383 } /* gauge_t *uc_get_rate */
385 /* vim: set sw=2 ts=8 sts=2 tw=78 : */