129cbb074afdde7333d1c05011e75fa4412d6925
1 /**
2 * collectd - src/utils_cache.c
3 * Copyright (C) 2007-2010 Florian octo Forster
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Florian octo Forster <octo at collectd.org>
25 **/
27 #include "collectd.h"
28 #include "common.h"
29 #include "plugin.h"
30 #include "utils_avltree.h"
31 #include "utils_cache.h"
32 #include "meta_data.h"
34 #include <assert.h>
35 #include <pthread.h>
37 typedef struct cache_entry_s
38 {
39 char name[6 * DATA_MAX_NAME_LEN];
40 size_t values_num;
41 gauge_t *values_gauge;
42 value_t *values_raw;
43 /* Time contained in the package
44 * (for calculating rates) */
45 cdtime_t last_time;
46 /* Time according to the local clock
47 * (for purging old entries) */
48 cdtime_t last_update;
49 /* Interval in which the data is collected
50 * (for purding old entries) */
51 cdtime_t interval;
52 int state;
53 int hits;
55 /*
56 * +-----+-----+-----+-----+-----+-----+-----+-----+-----+----
57 * ! 0 ! 1 ! 2 ! 3 ! 4 ! 5 ! 6 ! 7 ! 8 ! ...
58 * +-----+-----+-----+-----+-----+-----+-----+-----+-----+----
59 * ! ds0 ! ds1 ! ds2 ! ds0 ! ds1 ! ds2 ! ds0 ! ds1 ! ds2 ! ...
60 * +-----+-----+-----+-----+-----+-----+-----+-----+-----+----
61 * ! t = 0 ! t = 1 ! t = 2 ! ...
62 * +-----------------+-----------------+-----------------+----
63 */
64 gauge_t *history;
65 size_t history_index; /* points to the next position to write to. */
66 size_t history_length;
68 meta_data_t *meta;
69 } cache_entry_t;
71 static c_avl_tree_t *cache_tree = NULL;
72 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
74 static int cache_compare (const cache_entry_t *a, const cache_entry_t *b)
75 {
76 #if COLLECT_DEBUG
77 assert ((a != NULL) && (b != NULL));
78 #endif
79 return (strcmp (a->name, b->name));
80 } /* int cache_compare */
82 static cache_entry_t *cache_alloc (size_t values_num)
83 {
84 cache_entry_t *ce;
86 ce = (cache_entry_t *) malloc (sizeof (cache_entry_t));
87 if (ce == NULL)
88 {
89 ERROR ("utils_cache: cache_alloc: malloc failed.");
90 return (NULL);
91 }
92 memset (ce, '\0', sizeof (cache_entry_t));
93 ce->values_num = values_num;
95 ce->values_gauge = calloc (values_num, sizeof (*ce->values_gauge));
96 ce->values_raw = calloc (values_num, sizeof (*ce->values_raw));
97 if ((ce->values_gauge == NULL) || (ce->values_raw == NULL))
98 {
99 sfree (ce->values_gauge);
100 sfree (ce->values_raw);
101 sfree (ce);
102 ERROR ("utils_cache: cache_alloc: calloc failed.");
103 return (NULL);
104 }
106 ce->history = NULL;
107 ce->history_length = 0;
108 ce->meta = NULL;
110 return (ce);
111 } /* cache_entry_t *cache_alloc */
113 static void cache_free (cache_entry_t *ce)
114 {
115 if (ce == NULL)
116 return;
118 sfree (ce->values_gauge);
119 sfree (ce->values_raw);
120 sfree (ce->history);
121 if (ce->meta != NULL)
122 {
123 meta_data_destroy (ce->meta);
124 ce->meta = NULL;
125 }
126 sfree (ce);
127 } /* void cache_free */
129 static void uc_check_range (const data_set_t *ds, cache_entry_t *ce)
130 {
131 size_t i;
133 for (i = 0; i < ds->ds_num; i++)
134 {
135 if (isnan (ce->values_gauge[i]))
136 continue;
137 else if (ce->values_gauge[i] < ds->ds[i].min)
138 ce->values_gauge[i] = NAN;
139 else if (ce->values_gauge[i] > ds->ds[i].max)
140 ce->values_gauge[i] = NAN;
141 }
142 } /* void uc_check_range */
144 static int uc_insert (const data_set_t *ds, const value_list_t *vl,
145 const char *key)
146 {
147 char *key_copy;
148 cache_entry_t *ce;
149 size_t i;
151 /* `cache_lock' has been locked by `uc_update' */
153 key_copy = strdup (key);
154 if (key_copy == NULL)
155 {
156 ERROR ("uc_insert: strdup failed.");
157 return (-1);
158 }
160 ce = cache_alloc (ds->ds_num);
161 if (ce == NULL)
162 {
163 sfree (key_copy);
164 ERROR ("uc_insert: cache_alloc (%zu) failed.", ds->ds_num);
165 return (-1);
166 }
168 sstrncpy (ce->name, key, sizeof (ce->name));
170 for (i = 0; i < ds->ds_num; i++)
171 {
172 switch (ds->ds[i].type)
173 {
174 case DS_TYPE_COUNTER:
175 ce->values_gauge[i] = NAN;
176 ce->values_raw[i].counter = vl->values[i].counter;
177 break;
179 case DS_TYPE_GAUGE:
180 ce->values_gauge[i] = vl->values[i].gauge;
181 ce->values_raw[i].gauge = vl->values[i].gauge;
182 break;
184 case DS_TYPE_DERIVE:
185 ce->values_gauge[i] = NAN;
186 ce->values_raw[i].derive = vl->values[i].derive;
187 break;
189 case DS_TYPE_ABSOLUTE:
190 ce->values_gauge[i] = NAN;
191 if (vl->interval > 0)
192 ce->values_gauge[i] = ((double) vl->values[i].absolute)
193 / CDTIME_T_TO_DOUBLE (vl->interval);
194 ce->values_raw[i].absolute = vl->values[i].absolute;
195 break;
197 default:
198 /* This shouldn't happen. */
199 ERROR ("uc_insert: Don't know how to handle data source type %i.",
200 ds->ds[i].type);
201 return (-1);
202 } /* switch (ds->ds[i].type) */
203 } /* for (i) */
205 /* Prune invalid gauge data */
206 uc_check_range (ds, ce);
208 ce->last_time = vl->time;
209 ce->last_update = cdtime ();
210 ce->interval = vl->interval;
211 ce->state = STATE_OKAY;
213 if (c_avl_insert (cache_tree, key_copy, ce) != 0)
214 {
215 sfree (key_copy);
216 ERROR ("uc_insert: c_avl_insert failed.");
217 return (-1);
218 }
220 DEBUG ("uc_insert: Added %s to the cache.", key);
221 return (0);
222 } /* int uc_insert */
224 int uc_init (void)
225 {
226 if (cache_tree == NULL)
227 cache_tree = c_avl_create ((int (*) (const void *, const void *))
228 cache_compare);
230 return (0);
231 } /* int uc_init */
233 int uc_check_timeout (void)
234 {
235 cdtime_t now;
236 cache_entry_t *ce;
238 char **keys = NULL;
239 cdtime_t *keys_time = NULL;
240 cdtime_t *keys_interval = NULL;
241 int keys_len = 0;
243 char *key;
244 c_avl_iterator_t *iter;
246 int status;
247 int i;
249 pthread_mutex_lock (&cache_lock);
251 now = cdtime ();
253 /* Build a list of entries to be flushed */
254 iter = c_avl_get_iterator (cache_tree);
255 while (c_avl_iterator_next (iter, (void *) &key, (void *) &ce) == 0)
256 {
257 char **tmp;
258 cdtime_t *tmp_time;
260 /* If the entry is fresh enough, continue. */
261 if ((now - ce->last_update) < (ce->interval * timeout_g))
262 continue;
264 /* If entry has not been updated, add to `keys' array */
265 tmp = (char **) realloc ((void *) keys,
266 (keys_len + 1) * sizeof (char *));
267 if (tmp == NULL)
268 {
269 ERROR ("uc_check_timeout: realloc failed.");
270 continue;
271 }
272 keys = tmp;
274 tmp_time = realloc (keys_time, (keys_len + 1) * sizeof (*keys_time));
275 if (tmp_time == NULL)
276 {
277 ERROR ("uc_check_timeout: realloc failed.");
278 continue;
279 }
280 keys_time = tmp_time;
282 tmp_time = realloc (keys_interval, (keys_len + 1) * sizeof (*keys_interval));
283 if (tmp_time == NULL)
284 {
285 ERROR ("uc_check_timeout: realloc failed.");
286 continue;
287 }
288 keys_interval = tmp_time;
290 keys[keys_len] = strdup (key);
291 if (keys[keys_len] == NULL)
292 {
293 ERROR ("uc_check_timeout: strdup failed.");
294 continue;
295 }
296 keys_time[keys_len] = ce->last_time;
297 keys_interval[keys_len] = ce->interval;
299 keys_len++;
300 } /* while (c_avl_iterator_next) */
302 c_avl_iterator_destroy (iter);
303 pthread_mutex_unlock (&cache_lock);
305 if (keys_len == 0)
306 {
307 /* realloc() may have been called for these. */
308 sfree (keys);
309 sfree (keys_time);
310 sfree (keys_interval);
311 return (0);
312 }
314 /* Call the "missing" callback for each value. Do this before removing the
315 * value from the cache, so that callbacks can still access the data stored,
316 * including plugin specific meta data, rates, history, …. This must be done
317 * without holding the lock, otherwise we will run into a deadlock if a
318 * plugin calls the cache interface. */
319 for (i = 0; i < keys_len; i++)
320 {
321 value_list_t vl = VALUE_LIST_INIT;
323 vl.values = NULL;
324 vl.values_len = 0;
325 vl.meta = NULL;
327 status = parse_identifier_vl (keys[i], &vl);
328 if (status != 0)
329 {
330 ERROR ("uc_check_timeout: parse_identifier_vl (\"%s\") failed.", keys[i]);
331 continue;
332 }
334 vl.time = keys_time[i];
335 vl.interval = keys_interval[i];
337 plugin_dispatch_missing (&vl);
338 } /* for (i = 0; i < keys_len; i++) */
340 /* Now actually remove all the values from the cache. We don't re-evaluate
341 * the timestamp again, so in theory it is possible we remove a value after
342 * it is updated here. */
343 pthread_mutex_lock (&cache_lock);
344 for (i = 0; i < keys_len; i++)
345 {
346 key = NULL;
347 ce = NULL;
349 status = c_avl_remove (cache_tree, keys[i],
350 (void *) &key, (void *) &ce);
351 if (status != 0)
352 {
353 ERROR ("uc_check_timeout: c_avl_remove (\"%s\") failed.", keys[i]);
354 sfree (keys[i]);
355 continue;
356 }
358 sfree (keys[i]);
359 sfree (key);
360 cache_free (ce);
361 } /* for (i = 0; i < keys_len; i++) */
362 pthread_mutex_unlock (&cache_lock);
364 sfree (keys);
365 sfree (keys_time);
366 sfree (keys_interval);
368 return (0);
369 } /* int uc_check_timeout */
371 int uc_update (const data_set_t *ds, const value_list_t *vl)
372 {
373 char name[6 * DATA_MAX_NAME_LEN];
374 cache_entry_t *ce = NULL;
375 int status;
376 size_t i;
378 if (FORMAT_VL (name, sizeof (name), vl) != 0)
379 {
380 ERROR ("uc_update: FORMAT_VL failed.");
381 return (-1);
382 }
384 pthread_mutex_lock (&cache_lock);
386 status = c_avl_get (cache_tree, name, (void *) &ce);
387 if (status != 0) /* entry does not yet exist */
388 {
389 status = uc_insert (ds, vl, name);
390 pthread_mutex_unlock (&cache_lock);
391 return (status);
392 }
394 assert (ce != NULL);
395 assert (ce->values_num == ds->ds_num);
397 if (ce->last_time >= vl->time)
398 {
399 pthread_mutex_unlock (&cache_lock);
400 NOTICE ("uc_update: Value too old: name = %s; value time = %.3f; "
401 "last cache update = %.3f;",
402 name,
403 CDTIME_T_TO_DOUBLE (vl->time),
404 CDTIME_T_TO_DOUBLE (ce->last_time));
405 return (-1);
406 }
408 for (i = 0; i < ds->ds_num; i++)
409 {
410 switch (ds->ds[i].type)
411 {
412 case DS_TYPE_COUNTER:
413 {
414 counter_t diff;
416 /* check if the counter has wrapped around */
417 if (vl->values[i].counter < ce->values_raw[i].counter)
418 {
419 if (ce->values_raw[i].counter <= 4294967295U)
420 diff = (4294967295U - ce->values_raw[i].counter)
421 + vl->values[i].counter;
422 else
423 diff = (18446744073709551615ULL - ce->values_raw[i].counter)
424 + vl->values[i].counter;
425 }
426 else /* counter has NOT wrapped around */
427 {
428 diff = vl->values[i].counter - ce->values_raw[i].counter;
429 }
431 ce->values_gauge[i] = ((double) diff)
432 / (CDTIME_T_TO_DOUBLE (vl->time - ce->last_time));
433 ce->values_raw[i].counter = vl->values[i].counter;
434 }
435 break;
437 case DS_TYPE_GAUGE:
438 ce->values_raw[i].gauge = vl->values[i].gauge;
439 ce->values_gauge[i] = vl->values[i].gauge;
440 break;
442 case DS_TYPE_DERIVE:
443 {
444 derive_t diff;
446 diff = vl->values[i].derive - ce->values_raw[i].derive;
448 ce->values_gauge[i] = ((double) diff)
449 / (CDTIME_T_TO_DOUBLE (vl->time - ce->last_time));
450 ce->values_raw[i].derive = vl->values[i].derive;
451 }
452 break;
454 case DS_TYPE_ABSOLUTE:
455 ce->values_gauge[i] = ((double) vl->values[i].absolute)
456 / (CDTIME_T_TO_DOUBLE (vl->time - ce->last_time));
457 ce->values_raw[i].absolute = vl->values[i].absolute;
458 break;
460 default:
461 /* This shouldn't happen. */
462 pthread_mutex_unlock (&cache_lock);
463 ERROR ("uc_update: Don't know how to handle data source type %i.",
464 ds->ds[i].type);
465 return (-1);
466 } /* switch (ds->ds[i].type) */
468 DEBUG ("uc_update: %s: ds[%zu] = %lf", name, i, ce->values_gauge[i]);
469 } /* for (i) */
471 /* Update the history if it exists. */
472 if (ce->history != NULL)
473 {
474 assert (ce->history_index < ce->history_length);
475 for (i = 0; i < ce->values_num; i++)
476 {
477 size_t hist_idx = (ce->values_num * ce->history_index) + i;
478 ce->history[hist_idx] = ce->values_gauge[i];
479 }
481 assert (ce->history_length > 0);
482 ce->history_index = (ce->history_index + 1) % ce->history_length;
483 }
485 /* Prune invalid gauge data */
486 uc_check_range (ds, ce);
488 ce->last_time = vl->time;
489 ce->last_update = cdtime ();
490 ce->interval = vl->interval;
492 pthread_mutex_unlock (&cache_lock);
494 return (0);
495 } /* int uc_update */
497 int uc_get_rate_by_name (const char *name, gauge_t **ret_values, size_t *ret_values_num)
498 {
499 gauge_t *ret = NULL;
500 size_t ret_num = 0;
501 cache_entry_t *ce = NULL;
502 int status = 0;
504 pthread_mutex_lock (&cache_lock);
506 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
507 {
508 assert (ce != NULL);
510 /* remove missing values from getval */
511 if (ce->state == STATE_MISSING)
512 {
513 status = -1;
514 }
515 else
516 {
517 ret_num = ce->values_num;
518 ret = (gauge_t *) malloc (ret_num * sizeof (gauge_t));
519 if (ret == NULL)
520 {
521 ERROR ("utils_cache: uc_get_rate_by_name: malloc failed.");
522 status = -1;
523 }
524 else
525 {
526 memcpy (ret, ce->values_gauge, ret_num * sizeof (gauge_t));
527 }
528 }
529 }
530 else
531 {
532 DEBUG ("utils_cache: uc_get_rate_by_name: No such value: %s", name);
533 status = -1;
534 }
536 pthread_mutex_unlock (&cache_lock);
538 if (status == 0)
539 {
540 *ret_values = ret;
541 *ret_values_num = ret_num;
542 }
544 return (status);
545 } /* gauge_t *uc_get_rate_by_name */
547 gauge_t *uc_get_rate (const data_set_t *ds, const value_list_t *vl)
548 {
549 char name[6 * DATA_MAX_NAME_LEN];
550 gauge_t *ret = NULL;
551 size_t ret_num = 0;
552 int status;
554 if (FORMAT_VL (name, sizeof (name), vl) != 0)
555 {
556 ERROR ("utils_cache: uc_get_rate: FORMAT_VL failed.");
557 return (NULL);
558 }
560 status = uc_get_rate_by_name (name, &ret, &ret_num);
561 if (status != 0)
562 return (NULL);
564 /* This is important - the caller has no other way of knowing how many
565 * values are returned. */
566 if (ret_num != (size_t) ds->ds_num)
567 {
568 ERROR ("utils_cache: uc_get_rate: ds[%s] has %zu values, "
569 "but uc_get_rate_by_name returned %zu.",
570 ds->type, ds->ds_num, ret_num);
571 sfree (ret);
572 return (NULL);
573 }
575 return (ret);
576 } /* gauge_t *uc_get_rate */
578 size_t uc_get_size() {
579 size_t size_arrays = 0;
581 pthread_mutex_lock (&cache_lock);
582 size_arrays = (size_t) c_avl_size (cache_tree);
583 pthread_mutex_unlock (&cache_lock);
585 return (size_arrays);
586 }
588 int uc_get_names (char ***ret_names, cdtime_t **ret_times, size_t *ret_number)
589 {
590 c_avl_iterator_t *iter;
591 char *key;
592 cache_entry_t *value;
594 char **names = NULL;
595 cdtime_t *times = NULL;
596 size_t number = 0;
597 size_t size_arrays = 0;
599 int status = 0;
601 if ((ret_names == NULL) || (ret_number == NULL))
602 return (-1);
604 pthread_mutex_lock (&cache_lock);
606 size_arrays = (size_t) c_avl_size (cache_tree);
607 if (size_arrays < 1)
608 {
609 /* Handle the "no values" case here, to avoid the error message when
610 * calloc() returns NULL. */
611 pthread_mutex_unlock (&cache_lock);
612 return (0);
613 }
615 names = calloc (size_arrays, sizeof (*names));
616 times = calloc (size_arrays, sizeof (*times));
617 if ((names == NULL) || (times == NULL))
618 {
619 ERROR ("uc_get_names: calloc failed.");
620 sfree (names);
621 sfree (times);
622 pthread_mutex_unlock (&cache_lock);
623 return (ENOMEM);
624 }
626 iter = c_avl_get_iterator (cache_tree);
627 while (c_avl_iterator_next (iter, (void *) &key, (void *) &value) == 0)
628 {
629 /* remove missing values when list values */
630 if (value->state == STATE_MISSING)
631 continue;
633 /* c_avl_size does not return a number smaller than the number of elements
634 * returned by c_avl_iterator_next. */
635 assert (number < size_arrays);
637 if (ret_times != NULL)
638 times[number] = value->last_time;
640 names[number] = strdup (key);
641 if (names[number] == NULL)
642 {
643 status = -1;
644 break;
645 }
647 number++;
648 } /* while (c_avl_iterator_next) */
650 c_avl_iterator_destroy (iter);
651 pthread_mutex_unlock (&cache_lock);
653 if (status != 0)
654 {
655 size_t i;
657 for (i = 0; i < number; i++)
658 {
659 sfree (names[i]);
660 }
661 sfree (names);
662 sfree (times);
664 return (-1);
665 }
667 *ret_names = names;
668 if (ret_times != NULL)
669 *ret_times = times;
670 else
671 sfree (times);
672 *ret_number = number;
674 return (0);
675 } /* int uc_get_names */
677 int uc_get_state (const data_set_t *ds, const value_list_t *vl)
678 {
679 char name[6 * DATA_MAX_NAME_LEN];
680 cache_entry_t *ce = NULL;
681 int ret = STATE_ERROR;
683 if (FORMAT_VL (name, sizeof (name), vl) != 0)
684 {
685 ERROR ("uc_get_state: FORMAT_VL failed.");
686 return (STATE_ERROR);
687 }
689 pthread_mutex_lock (&cache_lock);
691 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
692 {
693 assert (ce != NULL);
694 ret = ce->state;
695 }
697 pthread_mutex_unlock (&cache_lock);
699 return (ret);
700 } /* int uc_get_state */
702 int uc_set_state (const data_set_t *ds, const value_list_t *vl, int state)
703 {
704 char name[6 * DATA_MAX_NAME_LEN];
705 cache_entry_t *ce = NULL;
706 int ret = -1;
708 if (FORMAT_VL (name, sizeof (name), vl) != 0)
709 {
710 ERROR ("uc_get_state: FORMAT_VL failed.");
711 return (STATE_ERROR);
712 }
714 pthread_mutex_lock (&cache_lock);
716 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
717 {
718 assert (ce != NULL);
719 ret = ce->state;
720 ce->state = state;
721 }
723 pthread_mutex_unlock (&cache_lock);
725 return (ret);
726 } /* int uc_set_state */
728 int uc_get_history_by_name (const char *name,
729 gauge_t *ret_history, size_t num_steps, size_t num_ds)
730 {
731 cache_entry_t *ce = NULL;
732 size_t i;
733 int status = 0;
735 pthread_mutex_lock (&cache_lock);
737 status = c_avl_get (cache_tree, name, (void *) &ce);
738 if (status != 0)
739 {
740 pthread_mutex_unlock (&cache_lock);
741 return (-ENOENT);
742 }
744 if (((size_t) ce->values_num) != num_ds)
745 {
746 pthread_mutex_unlock (&cache_lock);
747 return (-EINVAL);
748 }
750 /* Check if there are enough values available. If not, increase the buffer
751 * size. */
752 if (ce->history_length < num_steps)
753 {
754 gauge_t *tmp;
755 size_t i;
757 tmp = realloc (ce->history, sizeof (*ce->history)
758 * num_steps * ce->values_num);
759 if (tmp == NULL)
760 {
761 pthread_mutex_unlock (&cache_lock);
762 return (-ENOMEM);
763 }
765 for (i = ce->history_length * ce->values_num;
766 i < (num_steps * ce->values_num);
767 i++)
768 tmp[i] = NAN;
770 ce->history = tmp;
771 ce->history_length = num_steps;
772 } /* if (ce->history_length < num_steps) */
774 /* Copy the values to the output buffer. */
775 for (i = 0; i < num_steps; i++)
776 {
777 size_t src_index;
778 size_t dst_index;
780 if (i < ce->history_index)
781 src_index = ce->history_index - (i + 1);
782 else
783 src_index = ce->history_length + ce->history_index - (i + 1);
784 src_index = src_index * num_ds;
786 dst_index = i * num_ds;
788 memcpy (ret_history + dst_index, ce->history + src_index,
789 sizeof (*ret_history) * num_ds);
790 }
792 pthread_mutex_unlock (&cache_lock);
794 return (0);
795 } /* int uc_get_history_by_name */
797 int uc_get_history (const data_set_t *ds, const value_list_t *vl,
798 gauge_t *ret_history, size_t num_steps, size_t num_ds)
799 {
800 char name[6 * DATA_MAX_NAME_LEN];
802 if (FORMAT_VL (name, sizeof (name), vl) != 0)
803 {
804 ERROR ("utils_cache: uc_get_history: FORMAT_VL failed.");
805 return (-1);
806 }
808 return (uc_get_history_by_name (name, ret_history, num_steps, num_ds));
809 } /* int uc_get_history */
811 int uc_get_hits (const data_set_t *ds, const value_list_t *vl)
812 {
813 char name[6 * DATA_MAX_NAME_LEN];
814 cache_entry_t *ce = NULL;
815 int ret = STATE_ERROR;
817 if (FORMAT_VL (name, sizeof (name), vl) != 0)
818 {
819 ERROR ("uc_get_state: FORMAT_VL failed.");
820 return (STATE_ERROR);
821 }
823 pthread_mutex_lock (&cache_lock);
825 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
826 {
827 assert (ce != NULL);
828 ret = ce->hits;
829 }
831 pthread_mutex_unlock (&cache_lock);
833 return (ret);
834 } /* int uc_get_hits */
836 int uc_set_hits (const data_set_t *ds, const value_list_t *vl, int hits)
837 {
838 char name[6 * DATA_MAX_NAME_LEN];
839 cache_entry_t *ce = NULL;
840 int ret = -1;
842 if (FORMAT_VL (name, sizeof (name), vl) != 0)
843 {
844 ERROR ("uc_get_state: FORMAT_VL failed.");
845 return (STATE_ERROR);
846 }
848 pthread_mutex_lock (&cache_lock);
850 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
851 {
852 assert (ce != NULL);
853 ret = ce->hits;
854 ce->hits = hits;
855 }
857 pthread_mutex_unlock (&cache_lock);
859 return (ret);
860 } /* int uc_set_hits */
862 int uc_inc_hits (const data_set_t *ds, const value_list_t *vl, int step)
863 {
864 char name[6 * DATA_MAX_NAME_LEN];
865 cache_entry_t *ce = NULL;
866 int ret = -1;
868 if (FORMAT_VL (name, sizeof (name), vl) != 0)
869 {
870 ERROR ("uc_get_state: FORMAT_VL failed.");
871 return (STATE_ERROR);
872 }
874 pthread_mutex_lock (&cache_lock);
876 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
877 {
878 assert (ce != NULL);
879 ret = ce->hits;
880 ce->hits = ret + step;
881 }
883 pthread_mutex_unlock (&cache_lock);
885 return (ret);
886 } /* int uc_inc_hits */
888 /*
889 * Meta data interface
890 */
891 /* XXX: This function will acquire `cache_lock' but will not free it! */
892 static meta_data_t *uc_get_meta (const value_list_t *vl) /* {{{ */
893 {
894 char name[6 * DATA_MAX_NAME_LEN];
895 cache_entry_t *ce = NULL;
896 int status;
898 status = FORMAT_VL (name, sizeof (name), vl);
899 if (status != 0)
900 {
901 ERROR ("utils_cache: uc_get_meta: FORMAT_VL failed.");
902 return (NULL);
903 }
905 pthread_mutex_lock (&cache_lock);
907 status = c_avl_get (cache_tree, name, (void *) &ce);
908 if (status != 0)
909 {
910 pthread_mutex_unlock (&cache_lock);
911 return (NULL);
912 }
913 assert (ce != NULL);
915 if (ce->meta == NULL)
916 ce->meta = meta_data_create ();
918 if (ce->meta == NULL)
919 pthread_mutex_unlock (&cache_lock);
921 return (ce->meta);
922 } /* }}} meta_data_t *uc_get_meta */
924 /* Sorry about this preprocessor magic, but it really makes this file much
925 * shorter.. */
926 #define UC_WRAP(wrap_function) { \
927 meta_data_t *meta; \
928 int status; \
929 meta = uc_get_meta (vl); \
930 if (meta == NULL) return (-1); \
931 status = wrap_function (meta, key); \
932 pthread_mutex_unlock (&cache_lock); \
933 return (status); \
934 }
935 int uc_meta_data_exists (const value_list_t *vl, const char *key)
936 UC_WRAP (meta_data_exists)
938 int uc_meta_data_delete (const value_list_t *vl, const char *key)
939 UC_WRAP (meta_data_delete)
940 #undef UC_WRAP
942 /* We need a new version of this macro because the following functions take
943 * two argumetns. */
944 #define UC_WRAP(wrap_function) { \
945 meta_data_t *meta; \
946 int status; \
947 meta = uc_get_meta (vl); \
948 if (meta == NULL) return (-1); \
949 status = wrap_function (meta, key, value); \
950 pthread_mutex_unlock (&cache_lock); \
951 return (status); \
952 }
953 int uc_meta_data_add_string (const value_list_t *vl,
954 const char *key,
955 const char *value)
956 UC_WRAP(meta_data_add_string)
957 int uc_meta_data_add_signed_int (const value_list_t *vl,
958 const char *key,
959 int64_t value)
960 UC_WRAP(meta_data_add_signed_int)
961 int uc_meta_data_add_unsigned_int (const value_list_t *vl,
962 const char *key,
963 uint64_t value)
964 UC_WRAP(meta_data_add_unsigned_int)
965 int uc_meta_data_add_double (const value_list_t *vl,
966 const char *key,
967 double value)
968 UC_WRAP(meta_data_add_double)
969 int uc_meta_data_add_boolean (const value_list_t *vl,
970 const char *key,
971 _Bool value)
972 UC_WRAP(meta_data_add_boolean)
974 int uc_meta_data_get_string (const value_list_t *vl,
975 const char *key,
976 char **value)
977 UC_WRAP(meta_data_get_string)
978 int uc_meta_data_get_signed_int (const value_list_t *vl,
979 const char *key,
980 int64_t *value)
981 UC_WRAP(meta_data_get_signed_int)
982 int uc_meta_data_get_unsigned_int (const value_list_t *vl,
983 const char *key,
984 uint64_t *value)
985 UC_WRAP(meta_data_get_unsigned_int)
986 int uc_meta_data_get_double (const value_list_t *vl,
987 const char *key,
988 double *value)
989 UC_WRAP(meta_data_get_double)
990 int uc_meta_data_get_boolean (const value_list_t *vl,
991 const char *key,
992 _Bool *value)
993 UC_WRAP(meta_data_get_boolean)
994 #undef UC_WRAP
996 /* vim: set sw=2 ts=8 sts=2 tw=78 : */