1 /**
2 * collectd - src/utils_cache.c
3 * Copyright (C) 2007-2010 Florian octo Forster
4 *
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.
8 *
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.
13 *
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
17 *
18 * Author:
19 * Florian octo Forster <octo at collectd.org>
20 **/
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25 #include "utils_avltree.h"
26 #include "utils_cache.h"
27 #include "utils_threshold.h"
28 #include "meta_data.h"
30 #include <assert.h>
31 #include <pthread.h>
33 typedef struct cache_entry_s
34 {
35 char name[6 * DATA_MAX_NAME_LEN];
36 int values_num;
37 gauge_t *values_gauge;
38 value_t *values_raw;
39 /* Time contained in the package
40 * (for calculating rates) */
41 cdtime_t last_time;
42 /* Time according to the local clock
43 * (for purging old entries) */
44 cdtime_t last_update;
45 /* Interval in which the data is collected
46 * (for purding old entries) */
47 cdtime_t interval;
48 int state;
49 int hits;
51 /*
52 * +-----+-----+-----+-----+-----+-----+-----+-----+-----+----
53 * ! 0 ! 1 ! 2 ! 3 ! 4 ! 5 ! 6 ! 7 ! 8 ! ...
54 * +-----+-----+-----+-----+-----+-----+-----+-----+-----+----
55 * ! ds0 ! ds1 ! ds2 ! ds0 ! ds1 ! ds2 ! ds0 ! ds1 ! ds2 ! ...
56 * +-----+-----+-----+-----+-----+-----+-----+-----+-----+----
57 * ! t = 0 ! t = 1 ! t = 2 ! ...
58 * +-----------------+-----------------+-----------------+----
59 */
60 gauge_t *history;
61 size_t history_index; /* points to the next position to write to. */
62 size_t history_length;
64 meta_data_t *meta;
65 } cache_entry_t;
67 static c_avl_tree_t *cache_tree = NULL;
68 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
70 static int cache_compare (const cache_entry_t *a, const cache_entry_t *b)
71 {
72 assert ((a != NULL) && (b != NULL));
73 return (strcmp (a->name, b->name));
74 } /* int cache_compare */
76 static cache_entry_t *cache_alloc (int values_num)
77 {
78 cache_entry_t *ce;
80 ce = (cache_entry_t *) malloc (sizeof (cache_entry_t));
81 if (ce == NULL)
82 {
83 ERROR ("utils_cache: cache_alloc: malloc failed.");
84 return (NULL);
85 }
86 memset (ce, '\0', sizeof (cache_entry_t));
87 ce->values_num = values_num;
89 ce->values_gauge = calloc (values_num, sizeof (*ce->values_gauge));
90 ce->values_raw = calloc (values_num, sizeof (*ce->values_raw));
91 if ((ce->values_gauge == NULL) || (ce->values_raw == NULL))
92 {
93 sfree (ce->values_gauge);
94 sfree (ce->values_raw);
95 sfree (ce);
96 ERROR ("utils_cache: cache_alloc: calloc failed.");
97 return (NULL);
98 }
100 ce->history = NULL;
101 ce->history_length = 0;
102 ce->meta = NULL;
104 return (ce);
105 } /* cache_entry_t *cache_alloc */
107 static void cache_free (cache_entry_t *ce)
108 {
109 if (ce == NULL)
110 return;
112 sfree (ce->values_gauge);
113 sfree (ce->values_raw);
114 sfree (ce->history);
115 if (ce->meta != NULL)
116 {
117 meta_data_destroy (ce->meta);
118 ce->meta = NULL;
119 }
120 sfree (ce);
121 } /* void cache_free */
123 __attribute__((unused))
124 static int uc_send_notification (const char *name)
125 {
126 cache_entry_t *ce = NULL;
127 int status;
129 char *name_copy;
130 char *host;
131 char *plugin;
132 char *plugin_instance;
133 char *type;
134 char *type_instance;
136 notification_t n;
138 name_copy = strdup (name);
139 if (name_copy == NULL)
140 {
141 ERROR ("uc_send_notification: strdup failed.");
142 return (-1);
143 }
145 status = parse_identifier (name_copy, &host,
146 &plugin, &plugin_instance,
147 &type, &type_instance);
148 if (status != 0)
149 {
150 ERROR ("uc_send_notification: Cannot parse name `%s'", name);
151 return (-1);
152 }
154 /* Copy the associative members */
155 notification_init (&n, NOTIF_FAILURE, /* host = */ NULL,
156 host, plugin, plugin_instance, type, type_instance);
158 sfree (name_copy);
159 name_copy = host = plugin = plugin_instance = type = type_instance = NULL;
161 pthread_mutex_lock (&cache_lock);
163 /*
164 * Set the time _after_ getting the lock because we don't know how long
165 * acquiring the lock takes and we will use this time later to decide
166 * whether or not the state is OKAY.
167 */
168 n.time = cdtime ();
170 status = c_avl_get (cache_tree, name, (void *) &ce);
171 if (status != 0)
172 {
173 pthread_mutex_unlock (&cache_lock);
174 sfree (name_copy);
175 return (-1);
176 }
178 /* Check if the entry has been updated in the meantime */
179 if ((n.time - ce->last_update) < (timeout_g * ce->interval))
180 {
181 ce->state = STATE_OKAY;
182 pthread_mutex_unlock (&cache_lock);
183 sfree (name_copy);
184 return (-1);
185 }
187 ssnprintf (n.message, sizeof (n.message),
188 "%s has not been updated for %.3f seconds.", name,
189 CDTIME_T_TO_DOUBLE (n.time - ce->last_update));
191 pthread_mutex_unlock (&cache_lock);
193 plugin_dispatch_notification (&n);
195 return (0);
196 } /* int uc_send_notification */
198 static void uc_check_range (const data_set_t *ds, cache_entry_t *ce)
199 {
200 int i;
202 for (i = 0; i < ds->ds_num; i++)
203 {
204 if (isnan (ce->values_gauge[i]))
205 continue;
206 else if (ce->values_gauge[i] < ds->ds[i].min)
207 ce->values_gauge[i] = NAN;
208 else if (ce->values_gauge[i] > ds->ds[i].max)
209 ce->values_gauge[i] = NAN;
210 }
211 } /* void uc_check_range */
213 static int uc_insert (const data_set_t *ds, const value_list_t *vl,
214 const char *key)
215 {
216 int i;
217 char *key_copy;
218 cache_entry_t *ce;
220 /* `cache_lock' has been locked by `uc_update' */
222 key_copy = strdup (key);
223 if (key_copy == NULL)
224 {
225 ERROR ("uc_insert: strdup failed.");
226 return (-1);
227 }
229 ce = cache_alloc (ds->ds_num);
230 if (ce == NULL)
231 {
232 sfree (key_copy);
233 ERROR ("uc_insert: cache_alloc (%i) failed.", ds->ds_num);
234 return (-1);
235 }
237 sstrncpy (ce->name, key, sizeof (ce->name));
239 for (i = 0; i < ds->ds_num; i++)
240 {
241 switch (ds->ds[i].type)
242 {
243 case DS_TYPE_COUNTER:
244 ce->values_gauge[i] = NAN;
245 ce->values_raw[i].counter = vl->values[i].counter;
246 break;
248 case DS_TYPE_GAUGE:
249 ce->values_gauge[i] = vl->values[i].gauge;
250 ce->values_raw[i].gauge = vl->values[i].gauge;
251 break;
253 case DS_TYPE_DERIVE:
254 ce->values_gauge[i] = NAN;
255 ce->values_raw[i].derive = vl->values[i].derive;
256 break;
258 case DS_TYPE_ABSOLUTE:
259 ce->values_gauge[i] = NAN;
260 if (vl->interval > 0)
261 ce->values_gauge[i] = ((double) vl->values[i].absolute)
262 / CDTIME_T_TO_DOUBLE (vl->interval);
263 ce->values_raw[i].absolute = vl->values[i].absolute;
264 break;
266 default:
267 /* This shouldn't happen. */
268 ERROR ("uc_insert: Don't know how to handle data source type %i.",
269 ds->ds[i].type);
270 return (-1);
271 } /* switch (ds->ds[i].type) */
272 } /* for (i) */
274 /* Prune invalid gauge data */
275 uc_check_range (ds, ce);
277 ce->last_time = vl->time;
278 ce->last_update = cdtime ();
279 ce->interval = vl->interval;
280 ce->state = STATE_OKAY;
282 if (c_avl_insert (cache_tree, key_copy, ce) != 0)
283 {
284 sfree (key_copy);
285 ERROR ("uc_insert: c_avl_insert failed.");
286 return (-1);
287 }
289 DEBUG ("uc_insert: Added %s to the cache.", key);
290 return (0);
291 } /* int uc_insert */
293 int uc_init (void)
294 {
295 if (cache_tree == NULL)
296 cache_tree = c_avl_create ((int (*) (const void *, const void *))
297 cache_compare);
299 return (0);
300 } /* int uc_init */
302 int uc_check_timeout (void)
303 {
304 cdtime_t now;
305 cache_entry_t *ce;
307 char **keys = NULL;
308 cdtime_t *keys_time = NULL;
309 cdtime_t *keys_interval = NULL;
310 int keys_len = 0;
312 char *key;
313 c_avl_iterator_t *iter;
315 int status;
316 int i;
318 pthread_mutex_lock (&cache_lock);
320 now = cdtime ();
322 /* Build a list of entries to be flushed */
323 iter = c_avl_get_iterator (cache_tree);
324 while (c_avl_iterator_next (iter, (void *) &key, (void *) &ce) == 0)
325 {
326 char **tmp;
327 cdtime_t *tmp_time;
329 /* If the entry is fresh enough, continue. */
330 if ((now - ce->last_update) < (ce->interval * timeout_g))
331 continue;
333 /* If entry has not been updated, add to `keys' array */
334 tmp = (char **) realloc ((void *) keys,
335 (keys_len + 1) * sizeof (char *));
336 if (tmp == NULL)
337 {
338 ERROR ("uc_check_timeout: realloc failed.");
339 continue;
340 }
341 keys = tmp;
343 tmp_time = realloc (keys_time, (keys_len + 1) * sizeof (*keys_time));
344 if (tmp_time == NULL)
345 {
346 ERROR ("uc_check_timeout: realloc failed.");
347 continue;
348 }
349 keys_time = tmp_time;
351 tmp_time = realloc (keys_interval, (keys_len + 1) * sizeof (*keys_interval));
352 if (tmp_time == NULL)
353 {
354 ERROR ("uc_check_timeout: realloc failed.");
355 continue;
356 }
357 keys_interval = tmp_time;
359 keys[keys_len] = strdup (key);
360 if (keys[keys_len] == NULL)
361 {
362 ERROR ("uc_check_timeout: strdup failed.");
363 continue;
364 }
365 keys_time[keys_len] = ce->last_time;
366 keys_interval[keys_len] = ce->interval;
368 keys_len++;
369 } /* while (c_avl_iterator_next) */
371 c_avl_iterator_destroy (iter);
372 pthread_mutex_unlock (&cache_lock);
374 if (keys_len == 0)
375 return (0);
377 /* Call the "missing" callback for each value. Do this before removing the
378 * value from the cache, so that callbacks can still access the data stored,
379 * including plugin specific meta data, rates, history, …. This must be done
380 * without holding the lock, otherwise we will run into a deadlock if a
381 * plugin calls the cache interface. */
382 for (i = 0; i < keys_len; i++)
383 {
384 value_list_t vl = VALUE_LIST_INIT;
386 vl.values = NULL;
387 vl.values_len = 0;
388 vl.meta = NULL;
390 status = parse_identifier_vl (keys[i], &vl);
391 if (status != 0)
392 {
393 ERROR ("uc_check_timeout: parse_identifier_vl (\"%s\") failed.", keys[i]);
394 cache_free (ce);
395 continue;
396 }
398 vl.time = keys_time[i];
399 vl.interval = keys_interval[i];
401 plugin_dispatch_missing (&vl);
402 } /* for (i = 0; i < keys_len; i++) */
404 /* Now actually remove all the values from the cache. We don't re-evaluate
405 * the timestamp again, so in theory it is possible we remove a value after
406 * it is updated here. */
407 pthread_mutex_lock (&cache_lock);
408 for (i = 0; i < keys_len; i++)
409 {
410 key = NULL;
411 ce = NULL;
413 status = c_avl_remove (cache_tree, keys[i],
414 (void *) &key, (void *) &ce);
415 if (status != 0)
416 {
417 ERROR ("uc_check_timeout: c_avl_remove (\"%s\") failed.", keys[i]);
418 sfree (keys[i]);
419 continue;
420 }
422 sfree (keys[i]);
423 sfree (key);
424 cache_free (ce);
425 } /* for (i = 0; i < keys_len; i++) */
426 pthread_mutex_unlock (&cache_lock);
428 sfree (keys);
429 sfree (keys_time);
430 sfree (keys_interval);
432 return (0);
433 } /* int uc_check_timeout */
435 int uc_update (const data_set_t *ds, const value_list_t *vl)
436 {
437 char name[6 * DATA_MAX_NAME_LEN];
438 cache_entry_t *ce = NULL;
439 int send_okay_notification = 0;
440 cdtime_t update_delay = 0;
441 notification_t n;
442 int status;
443 int i;
445 if (FORMAT_VL (name, sizeof (name), vl) != 0)
446 {
447 ERROR ("uc_update: FORMAT_VL failed.");
448 return (-1);
449 }
451 pthread_mutex_lock (&cache_lock);
453 status = c_avl_get (cache_tree, name, (void *) &ce);
454 if (status != 0) /* entry does not yet exist */
455 {
456 status = uc_insert (ds, vl, name);
457 pthread_mutex_unlock (&cache_lock);
458 return (status);
459 }
461 assert (ce != NULL);
462 assert (ce->values_num == ds->ds_num);
464 if (ce->last_time >= vl->time)
465 {
466 pthread_mutex_unlock (&cache_lock);
467 NOTICE ("uc_update: Value too old: name = %s; value time = %.3f; "
468 "last cache update = %.3f;",
469 name,
470 CDTIME_T_TO_DOUBLE (vl->time),
471 CDTIME_T_TO_DOUBLE (ce->last_time));
472 return (-1);
473 }
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)
478 {
479 send_okay_notification = 1;
480 ce->state = STATE_OKAY;
481 update_delay = cdtime () - ce->last_update;
482 }
484 for (i = 0; i < ds->ds_num; i++)
485 {
486 switch (ds->ds[i].type)
487 {
488 case DS_TYPE_COUNTER:
489 {
490 counter_t diff;
492 /* check if the counter has wrapped around */
493 if (vl->values[i].counter < ce->values_raw[i].counter)
494 {
495 if (ce->values_raw[i].counter <= 4294967295U)
496 diff = (4294967295U - ce->values_raw[i].counter)
497 + vl->values[i].counter;
498 else
499 diff = (18446744073709551615ULL - ce->values_raw[i].counter)
500 + vl->values[i].counter;
501 }
502 else /* counter has NOT wrapped around */
503 {
504 diff = vl->values[i].counter - ce->values_raw[i].counter;
505 }
507 ce->values_gauge[i] = ((double) diff)
508 / (CDTIME_T_TO_DOUBLE (vl->time - ce->last_time));
509 ce->values_raw[i].counter = vl->values[i].counter;
510 }
511 break;
513 case DS_TYPE_GAUGE:
514 ce->values_raw[i].gauge = vl->values[i].gauge;
515 ce->values_gauge[i] = vl->values[i].gauge;
516 break;
518 case DS_TYPE_DERIVE:
519 {
520 derive_t diff;
522 diff = vl->values[i].derive - ce->values_raw[i].derive;
524 ce->values_gauge[i] = ((double) diff)
525 / (CDTIME_T_TO_DOUBLE (vl->time - ce->last_time));
526 ce->values_raw[i].derive = vl->values[i].derive;
527 }
528 break;
530 case DS_TYPE_ABSOLUTE:
531 ce->values_gauge[i] = ((double) vl->values[i].absolute)
532 / (CDTIME_T_TO_DOUBLE (vl->time - ce->last_time));
533 ce->values_raw[i].absolute = vl->values[i].absolute;
534 break;
536 default:
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.",
540 ds->ds[i].type);
541 return (-1);
542 } /* switch (ds->ds[i].type) */
544 DEBUG ("uc_update: %s: ds[%i] = %lf", name, i, ce->values_gauge[i]);
545 } /* for (i) */
547 /* Update the history if it exists. */
548 if (ce->history != NULL)
549 {
550 assert (ce->history_index < ce->history_length);
551 for (i = 0; i < ce->values_num; i++)
552 {
553 size_t hist_idx = (ce->values_num * ce->history_index) + i;
554 ce->history[hist_idx] = ce->values_gauge[i];
555 }
557 assert (ce->history_length > 0);
558 ce->history_index = (ce->history_index + 1) % ce->history_length;
559 }
561 /* Prune invalid gauge data */
562 uc_check_range (ds, ce);
564 ce->last_time = vl->time;
565 ce->last_update = cdtime ();
566 ce->interval = vl->interval;
568 pthread_mutex_unlock (&cache_lock);
570 if (send_okay_notification == 0)
571 return (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);
576 if (status <= 0)
577 return (0);
579 /* Initialize the notification */
580 memset (&n, '\0', sizeof (n));
581 NOTIFICATION_INIT_VL (&n, vl, ds);
583 n.severity = NOTIF_OKAY;
584 n.time = vl->time;
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);
592 return (0);
593 } /* int uc_update */
595 int uc_get_rate_by_name (const char *name, gauge_t **ret_values, size_t *ret_values_num)
596 {
597 gauge_t *ret = NULL;
598 size_t ret_num = 0;
599 cache_entry_t *ce = NULL;
600 int status = 0;
602 pthread_mutex_lock (&cache_lock);
604 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
605 {
606 assert (ce != NULL);
608 /* remove missing values from getval */
609 if (ce->state == STATE_MISSING)
610 {
611 status = -1;
612 }
613 else
614 {
615 ret_num = ce->values_num;
616 ret = (gauge_t *) malloc (ret_num * sizeof (gauge_t));
617 if (ret == NULL)
618 {
619 ERROR ("utils_cache: uc_get_rate_by_name: malloc failed.");
620 status = -1;
621 }
622 else
623 {
624 memcpy (ret, ce->values_gauge, ret_num * sizeof (gauge_t));
625 }
626 }
627 }
628 else
629 {
630 DEBUG ("utils_cache: uc_get_rate_by_name: No such value: %s", name);
631 status = -1;
632 }
634 pthread_mutex_unlock (&cache_lock);
636 if (status == 0)
637 {
638 *ret_values = ret;
639 *ret_values_num = ret_num;
640 }
642 return (status);
643 } /* gauge_t *uc_get_rate_by_name */
645 gauge_t *uc_get_rate (const data_set_t *ds, const value_list_t *vl)
646 {
647 char name[6 * DATA_MAX_NAME_LEN];
648 gauge_t *ret = NULL;
649 size_t ret_num = 0;
650 int status;
652 if (FORMAT_VL (name, sizeof (name), vl) != 0)
653 {
654 ERROR ("utils_cache: uc_get_rate: FORMAT_VL failed.");
655 return (NULL);
656 }
658 status = uc_get_rate_by_name (name, &ret, &ret_num);
659 if (status != 0)
660 return (NULL);
662 /* This is important - the caller has no other way of knowing how many
663 * values are returned. */
664 if (ret_num != (size_t) ds->ds_num)
665 {
666 ERROR ("utils_cache: uc_get_rate: ds[%s] has %i values, "
667 "but uc_get_rate_by_name returned %zu.",
668 ds->type, ds->ds_num, ret_num);
669 sfree (ret);
670 return (NULL);
671 }
673 return (ret);
674 } /* gauge_t *uc_get_rate */
676 int uc_get_names (char ***ret_names, cdtime_t **ret_times, size_t *ret_number)
677 {
678 c_avl_iterator_t *iter;
679 char *key;
680 cache_entry_t *value;
682 char **names = NULL;
683 cdtime_t *times = NULL;
684 size_t number = 0;
686 int status = 0;
688 if ((ret_names == NULL) || (ret_number == NULL))
689 return (-1);
691 pthread_mutex_lock (&cache_lock);
693 iter = c_avl_get_iterator (cache_tree);
694 while (c_avl_iterator_next (iter, (void *) &key, (void *) &value) == 0)
695 {
696 char **temp;
698 /* remove missing values when list values */
699 if (value->state == STATE_MISSING)
700 continue;
702 if (ret_times != NULL)
703 {
704 cdtime_t *tmp_times;
706 tmp_times = (cdtime_t *) realloc (times, sizeof (cdtime_t) * (number + 1));
707 if (tmp_times == NULL)
708 {
709 status = -1;
710 break;
711 }
712 times = tmp_times;
713 times[number] = value->last_time;
714 }
716 temp = (char **) realloc (names, sizeof (char *) * (number + 1));
717 if (temp == NULL)
718 {
719 status = -1;
720 break;
721 }
722 names = temp;
723 names[number] = strdup (key);
724 if (names[number] == NULL)
725 {
726 status = -1;
727 break;
728 }
729 number++;
730 } /* while (c_avl_iterator_next) */
732 c_avl_iterator_destroy (iter);
733 pthread_mutex_unlock (&cache_lock);
735 if (status != 0)
736 {
737 size_t i;
739 for (i = 0; i < number; i++)
740 {
741 sfree (names[i]);
742 }
743 sfree (names);
745 return (-1);
746 }
748 *ret_names = names;
749 if (ret_times != NULL)
750 *ret_times = times;
751 *ret_number = number;
753 return (0);
754 } /* int uc_get_names */
756 int uc_get_state (const data_set_t *ds, const value_list_t *vl)
757 {
758 char name[6 * DATA_MAX_NAME_LEN];
759 cache_entry_t *ce = NULL;
760 int ret = STATE_ERROR;
762 if (FORMAT_VL (name, sizeof (name), vl) != 0)
763 {
764 ERROR ("uc_get_state: FORMAT_VL failed.");
765 return (STATE_ERROR);
766 }
768 pthread_mutex_lock (&cache_lock);
770 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
771 {
772 assert (ce != NULL);
773 ret = ce->state;
774 }
776 pthread_mutex_unlock (&cache_lock);
778 return (ret);
779 } /* int uc_get_state */
781 int uc_set_state (const data_set_t *ds, const value_list_t *vl, int state)
782 {
783 char name[6 * DATA_MAX_NAME_LEN];
784 cache_entry_t *ce = NULL;
785 int ret = -1;
787 if (FORMAT_VL (name, sizeof (name), vl) != 0)
788 {
789 ERROR ("uc_get_state: FORMAT_VL failed.");
790 return (STATE_ERROR);
791 }
793 pthread_mutex_lock (&cache_lock);
795 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
796 {
797 assert (ce != NULL);
798 ret = ce->state;
799 ce->state = state;
800 }
802 pthread_mutex_unlock (&cache_lock);
804 return (ret);
805 } /* int uc_set_state */
807 int uc_get_history_by_name (const char *name,
808 gauge_t *ret_history, size_t num_steps, size_t num_ds)
809 {
810 cache_entry_t *ce = NULL;
811 size_t i;
812 int status = 0;
814 pthread_mutex_lock (&cache_lock);
816 status = c_avl_get (cache_tree, name, (void *) &ce);
817 if (status != 0)
818 {
819 pthread_mutex_unlock (&cache_lock);
820 return (-ENOENT);
821 }
823 if (((size_t) ce->values_num) != num_ds)
824 {
825 pthread_mutex_unlock (&cache_lock);
826 return (-EINVAL);
827 }
829 /* Check if there are enough values available. If not, increase the buffer
830 * size. */
831 if (ce->history_length < num_steps)
832 {
833 gauge_t *tmp;
834 size_t i;
836 tmp = realloc (ce->history, sizeof (*ce->history)
837 * num_steps * ce->values_num);
838 if (tmp == NULL)
839 {
840 pthread_mutex_unlock (&cache_lock);
841 return (-ENOMEM);
842 }
844 for (i = ce->history_length * ce->values_num;
845 i < (num_steps * ce->values_num);
846 i++)
847 tmp[i] = NAN;
849 ce->history = tmp;
850 ce->history_length = num_steps;
851 } /* if (ce->history_length < num_steps) */
853 /* Copy the values to the output buffer. */
854 for (i = 0; i < num_steps; i++)
855 {
856 size_t src_index;
857 size_t dst_index;
859 if (i < ce->history_index)
860 src_index = ce->history_index - (i + 1);
861 else
862 src_index = ce->history_length + ce->history_index - (i + 1);
863 src_index = src_index * num_ds;
865 dst_index = i * num_ds;
867 memcpy (ret_history + dst_index, ce->history + src_index,
868 sizeof (*ret_history) * num_ds);
869 }
871 pthread_mutex_unlock (&cache_lock);
873 return (0);
874 } /* int uc_get_history_by_name */
876 int uc_get_history (const data_set_t *ds, const value_list_t *vl,
877 gauge_t *ret_history, size_t num_steps, size_t num_ds)
878 {
879 char name[6 * DATA_MAX_NAME_LEN];
881 if (FORMAT_VL (name, sizeof (name), vl) != 0)
882 {
883 ERROR ("utils_cache: uc_get_history: FORMAT_VL failed.");
884 return (-1);
885 }
887 return (uc_get_history_by_name (name, ret_history, num_steps, num_ds));
888 } /* int uc_get_history */
890 int uc_get_hits (const data_set_t *ds, const value_list_t *vl)
891 {
892 char name[6 * DATA_MAX_NAME_LEN];
893 cache_entry_t *ce = NULL;
894 int ret = STATE_ERROR;
896 if (FORMAT_VL (name, sizeof (name), vl) != 0)
897 {
898 ERROR ("uc_get_state: FORMAT_VL failed.");
899 return (STATE_ERROR);
900 }
902 pthread_mutex_lock (&cache_lock);
904 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
905 {
906 assert (ce != NULL);
907 ret = ce->hits;
908 }
910 pthread_mutex_unlock (&cache_lock);
912 return (ret);
913 } /* int uc_get_hits */
915 int uc_set_hits (const data_set_t *ds, const value_list_t *vl, int hits)
916 {
917 char name[6 * DATA_MAX_NAME_LEN];
918 cache_entry_t *ce = NULL;
919 int ret = -1;
921 if (FORMAT_VL (name, sizeof (name), vl) != 0)
922 {
923 ERROR ("uc_get_state: FORMAT_VL failed.");
924 return (STATE_ERROR);
925 }
927 pthread_mutex_lock (&cache_lock);
929 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
930 {
931 assert (ce != NULL);
932 ret = ce->hits;
933 ce->hits = hits;
934 }
936 pthread_mutex_unlock (&cache_lock);
938 return (ret);
939 } /* int uc_set_hits */
941 int uc_inc_hits (const data_set_t *ds, const value_list_t *vl, int step)
942 {
943 char name[6 * DATA_MAX_NAME_LEN];
944 cache_entry_t *ce = NULL;
945 int ret = -1;
947 if (FORMAT_VL (name, sizeof (name), vl) != 0)
948 {
949 ERROR ("uc_get_state: FORMAT_VL failed.");
950 return (STATE_ERROR);
951 }
953 pthread_mutex_lock (&cache_lock);
955 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
956 {
957 assert (ce != NULL);
958 ret = ce->hits;
959 ce->hits = ret + step;
960 }
962 pthread_mutex_unlock (&cache_lock);
964 return (ret);
965 } /* int uc_inc_hits */
967 /*
968 * Meta data interface
969 */
970 /* XXX: This function will acquire `cache_lock' but will not free it! */
971 static meta_data_t *uc_get_meta (const value_list_t *vl) /* {{{ */
972 {
973 char name[6 * DATA_MAX_NAME_LEN];
974 cache_entry_t *ce = NULL;
975 int status;
977 status = FORMAT_VL (name, sizeof (name), vl);
978 if (status != 0)
979 {
980 ERROR ("utils_cache: uc_get_meta: FORMAT_VL failed.");
981 return (NULL);
982 }
984 pthread_mutex_lock (&cache_lock);
986 status = c_avl_get (cache_tree, name, (void *) &ce);
987 if (status != 0)
988 {
989 pthread_mutex_unlock (&cache_lock);
990 return (NULL);
991 }
992 assert (ce != NULL);
994 if (ce->meta == NULL)
995 ce->meta = meta_data_create ();
997 if (ce->meta == NULL)
998 pthread_mutex_unlock (&cache_lock);
1000 return (ce->meta);
1001 } /* }}} meta_data_t *uc_get_meta */
1003 /* Sorry about this preprocessor magic, but it really makes this file much
1004 * shorter.. */
1005 #define UC_WRAP(wrap_function) { \
1006 meta_data_t *meta; \
1007 int status; \
1008 meta = uc_get_meta (vl); \
1009 if (meta == NULL) return (-1); \
1010 status = wrap_function (meta, key); \
1011 pthread_mutex_unlock (&cache_lock); \
1012 return (status); \
1013 }
1014 int uc_meta_data_exists (const value_list_t *vl, const char *key)
1015 UC_WRAP (meta_data_exists)
1017 int uc_meta_data_delete (const value_list_t *vl, const char *key)
1018 UC_WRAP (meta_data_delete)
1019 #undef UC_WRAP
1021 /* We need a new version of this macro because the following functions take
1022 * two argumetns. */
1023 #define UC_WRAP(wrap_function) { \
1024 meta_data_t *meta; \
1025 int status; \
1026 meta = uc_get_meta (vl); \
1027 if (meta == NULL) return (-1); \
1028 status = wrap_function (meta, key, value); \
1029 pthread_mutex_unlock (&cache_lock); \
1030 return (status); \
1031 }
1032 int uc_meta_data_add_string (const value_list_t *vl,
1033 const char *key,
1034 const char *value)
1035 UC_WRAP(meta_data_add_string)
1036 int uc_meta_data_add_signed_int (const value_list_t *vl,
1037 const char *key,
1038 int64_t value)
1039 UC_WRAP(meta_data_add_signed_int)
1040 int uc_meta_data_add_unsigned_int (const value_list_t *vl,
1041 const char *key,
1042 uint64_t value)
1043 UC_WRAP(meta_data_add_unsigned_int)
1044 int uc_meta_data_add_double (const value_list_t *vl,
1045 const char *key,
1046 double value)
1047 UC_WRAP(meta_data_add_double)
1048 int uc_meta_data_add_boolean (const value_list_t *vl,
1049 const char *key,
1050 _Bool value)
1051 UC_WRAP(meta_data_add_boolean)
1053 int uc_meta_data_get_string (const value_list_t *vl,
1054 const char *key,
1055 char **value)
1056 UC_WRAP(meta_data_get_string)
1057 int uc_meta_data_get_signed_int (const value_list_t *vl,
1058 const char *key,
1059 int64_t *value)
1060 UC_WRAP(meta_data_get_signed_int)
1061 int uc_meta_data_get_unsigned_int (const value_list_t *vl,
1062 const char *key,
1063 uint64_t *value)
1064 UC_WRAP(meta_data_get_unsigned_int)
1065 int uc_meta_data_get_double (const value_list_t *vl,
1066 const char *key,
1067 double *value)
1068 UC_WRAP(meta_data_get_double)
1069 int uc_meta_data_get_boolean (const value_list_t *vl,
1070 const char *key,
1071 _Bool *value)
1072 UC_WRAP(meta_data_get_boolean)
1073 #undef UC_WRAP
1075 /* vim: set sw=2 ts=8 sts=2 tw=78 : */