46b6631ce735335c26906e5552484469693e7338
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 int 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 (int 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 int 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 int i;
148 char *key_copy;
149 cache_entry_t *ce;
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 (%i) 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 return (0);
308 /* Call the "missing" callback for each value. Do this before removing the
309 * value from the cache, so that callbacks can still access the data stored,
310 * including plugin specific meta data, rates, history, …. This must be done
311 * without holding the lock, otherwise we will run into a deadlock if a
312 * plugin calls the cache interface. */
313 for (i = 0; i < keys_len; i++)
314 {
315 value_list_t vl = VALUE_LIST_INIT;
317 vl.values = NULL;
318 vl.values_len = 0;
319 vl.meta = NULL;
321 status = parse_identifier_vl (keys[i], &vl);
322 if (status != 0)
323 {
324 ERROR ("uc_check_timeout: parse_identifier_vl (\"%s\") failed.", keys[i]);
325 continue;
326 }
328 vl.time = keys_time[i];
329 vl.interval = keys_interval[i];
331 plugin_dispatch_missing (&vl);
332 } /* for (i = 0; i < keys_len; i++) */
334 /* Now actually remove all the values from the cache. We don't re-evaluate
335 * the timestamp again, so in theory it is possible we remove a value after
336 * it is updated here. */
337 pthread_mutex_lock (&cache_lock);
338 for (i = 0; i < keys_len; i++)
339 {
340 key = NULL;
341 ce = NULL;
343 status = c_avl_remove (cache_tree, keys[i],
344 (void *) &key, (void *) &ce);
345 if (status != 0)
346 {
347 ERROR ("uc_check_timeout: c_avl_remove (\"%s\") failed.", keys[i]);
348 sfree (keys[i]);
349 continue;
350 }
352 sfree (keys[i]);
353 sfree (key);
354 cache_free (ce);
355 } /* for (i = 0; i < keys_len; i++) */
356 pthread_mutex_unlock (&cache_lock);
358 sfree (keys);
359 sfree (keys_time);
360 sfree (keys_interval);
362 return (0);
363 } /* int uc_check_timeout */
365 int uc_update (const data_set_t *ds, const value_list_t *vl)
366 {
367 char name[6 * DATA_MAX_NAME_LEN];
368 cache_entry_t *ce = NULL;
369 int status;
370 int i;
372 if (FORMAT_VL (name, sizeof (name), vl) != 0)
373 {
374 ERROR ("uc_update: FORMAT_VL failed.");
375 return (-1);
376 }
378 pthread_mutex_lock (&cache_lock);
380 status = c_avl_get (cache_tree, name, (void *) &ce);
381 if (status != 0) /* entry does not yet exist */
382 {
383 status = uc_insert (ds, vl, name);
384 pthread_mutex_unlock (&cache_lock);
385 return (status);
386 }
388 assert (ce != NULL);
389 assert (ce->values_num == ds->ds_num);
391 if (ce->last_time >= vl->time)
392 {
393 pthread_mutex_unlock (&cache_lock);
394 NOTICE ("uc_update: Value too old: name = %s; value time = %.3f; "
395 "last cache update = %.3f;",
396 name,
397 CDTIME_T_TO_DOUBLE (vl->time),
398 CDTIME_T_TO_DOUBLE (ce->last_time));
399 return (-1);
400 }
402 for (i = 0; i < ds->ds_num; i++)
403 {
404 switch (ds->ds[i].type)
405 {
406 case DS_TYPE_COUNTER:
407 {
408 counter_t diff;
410 /* check if the counter has wrapped around */
411 if (vl->values[i].counter < ce->values_raw[i].counter)
412 {
413 if (ce->values_raw[i].counter <= 4294967295U)
414 diff = (4294967295U - ce->values_raw[i].counter)
415 + vl->values[i].counter;
416 else
417 diff = (18446744073709551615ULL - ce->values_raw[i].counter)
418 + vl->values[i].counter;
419 }
420 else /* counter has NOT wrapped around */
421 {
422 diff = vl->values[i].counter - ce->values_raw[i].counter;
423 }
425 ce->values_gauge[i] = ((double) diff)
426 / (CDTIME_T_TO_DOUBLE (vl->time - ce->last_time));
427 ce->values_raw[i].counter = vl->values[i].counter;
428 }
429 break;
431 case DS_TYPE_GAUGE:
432 ce->values_raw[i].gauge = vl->values[i].gauge;
433 ce->values_gauge[i] = vl->values[i].gauge;
434 break;
436 case DS_TYPE_DERIVE:
437 {
438 derive_t diff;
440 diff = vl->values[i].derive - ce->values_raw[i].derive;
442 ce->values_gauge[i] = ((double) diff)
443 / (CDTIME_T_TO_DOUBLE (vl->time - ce->last_time));
444 ce->values_raw[i].derive = vl->values[i].derive;
445 }
446 break;
448 case DS_TYPE_ABSOLUTE:
449 ce->values_gauge[i] = ((double) vl->values[i].absolute)
450 / (CDTIME_T_TO_DOUBLE (vl->time - ce->last_time));
451 ce->values_raw[i].absolute = vl->values[i].absolute;
452 break;
454 default:
455 /* This shouldn't happen. */
456 pthread_mutex_unlock (&cache_lock);
457 ERROR ("uc_update: Don't know how to handle data source type %i.",
458 ds->ds[i].type);
459 return (-1);
460 } /* switch (ds->ds[i].type) */
462 DEBUG ("uc_update: %s: ds[%i] = %lf", name, i, ce->values_gauge[i]);
463 } /* for (i) */
465 /* Update the history if it exists. */
466 if (ce->history != NULL)
467 {
468 assert (ce->history_index < ce->history_length);
469 for (i = 0; i < ce->values_num; i++)
470 {
471 size_t hist_idx = (ce->values_num * ce->history_index) + i;
472 ce->history[hist_idx] = ce->values_gauge[i];
473 }
475 assert (ce->history_length > 0);
476 ce->history_index = (ce->history_index + 1) % ce->history_length;
477 }
479 /* Prune invalid gauge data */
480 uc_check_range (ds, ce);
482 ce->last_time = vl->time;
483 ce->last_update = cdtime ();
484 ce->interval = vl->interval;
486 pthread_mutex_unlock (&cache_lock);
488 return (0);
489 } /* int uc_update */
491 int uc_get_rate_by_name (const char *name, gauge_t **ret_values, size_t *ret_values_num)
492 {
493 gauge_t *ret = NULL;
494 size_t ret_num = 0;
495 cache_entry_t *ce = NULL;
496 int status = 0;
498 pthread_mutex_lock (&cache_lock);
500 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
501 {
502 assert (ce != NULL);
504 /* remove missing values from getval */
505 if (ce->state == STATE_MISSING)
506 {
507 status = -1;
508 }
509 else
510 {
511 ret_num = ce->values_num;
512 ret = (gauge_t *) malloc (ret_num * sizeof (gauge_t));
513 if (ret == NULL)
514 {
515 ERROR ("utils_cache: uc_get_rate_by_name: malloc failed.");
516 status = -1;
517 }
518 else
519 {
520 memcpy (ret, ce->values_gauge, ret_num * sizeof (gauge_t));
521 }
522 }
523 }
524 else
525 {
526 DEBUG ("utils_cache: uc_get_rate_by_name: No such value: %s", name);
527 status = -1;
528 }
530 pthread_mutex_unlock (&cache_lock);
532 if (status == 0)
533 {
534 *ret_values = ret;
535 *ret_values_num = ret_num;
536 }
538 return (status);
539 } /* gauge_t *uc_get_rate_by_name */
541 gauge_t *uc_get_rate (const data_set_t *ds, const value_list_t *vl)
542 {
543 char name[6 * DATA_MAX_NAME_LEN];
544 gauge_t *ret = NULL;
545 size_t ret_num = 0;
546 int status;
548 if (FORMAT_VL (name, sizeof (name), vl) != 0)
549 {
550 ERROR ("utils_cache: uc_get_rate: FORMAT_VL failed.");
551 return (NULL);
552 }
554 status = uc_get_rate_by_name (name, &ret, &ret_num);
555 if (status != 0)
556 return (NULL);
558 /* This is important - the caller has no other way of knowing how many
559 * values are returned. */
560 if (ret_num != (size_t) ds->ds_num)
561 {
562 ERROR ("utils_cache: uc_get_rate: ds[%s] has %i values, "
563 "but uc_get_rate_by_name returned %zu.",
564 ds->type, ds->ds_num, ret_num);
565 sfree (ret);
566 return (NULL);
567 }
569 return (ret);
570 } /* gauge_t *uc_get_rate */
572 size_t uc_get_size() {
573 size_t size_arrays = 0;
575 pthread_mutex_lock (&cache_lock);
576 size_arrays = (size_t) c_avl_size (cache_tree);
577 pthread_mutex_unlock (&cache_lock);
579 return (size_arrays);
580 }
582 int uc_get_names (char ***ret_names, cdtime_t **ret_times, size_t *ret_number)
583 {
584 c_avl_iterator_t *iter;
585 char *key;
586 cache_entry_t *value;
588 char **names = NULL;
589 cdtime_t *times = NULL;
590 size_t number = 0;
591 size_t size_arrays = 0;
593 int status = 0;
595 if ((ret_names == NULL) || (ret_number == NULL))
596 return (-1);
598 pthread_mutex_lock (&cache_lock);
600 size_arrays = (size_t) c_avl_size (cache_tree);
601 if (size_arrays < 1)
602 {
603 /* Handle the "no values" case here, to avoid the error message when
604 * calloc() returns NULL. */
605 pthread_mutex_unlock (&cache_lock);
606 return (0);
607 }
609 names = calloc (size_arrays, sizeof (*names));
610 times = calloc (size_arrays, sizeof (*times));
611 if ((names == NULL) || (times == NULL))
612 {
613 ERROR ("uc_get_names: calloc failed.");
614 sfree (names);
615 sfree (times);
616 pthread_mutex_unlock (&cache_lock);
617 return (ENOMEM);
618 }
620 iter = c_avl_get_iterator (cache_tree);
621 while (c_avl_iterator_next (iter, (void *) &key, (void *) &value) == 0)
622 {
623 /* remove missing values when list values */
624 if (value->state == STATE_MISSING)
625 continue;
627 /* c_avl_size does not return a number smaller than the number of elements
628 * returned by c_avl_iterator_next. */
629 assert (number < size_arrays);
631 if (ret_times != NULL)
632 times[number] = value->last_time;
634 names[number] = strdup (key);
635 if (names[number] == NULL)
636 {
637 status = -1;
638 break;
639 }
641 number++;
642 } /* while (c_avl_iterator_next) */
644 c_avl_iterator_destroy (iter);
645 pthread_mutex_unlock (&cache_lock);
647 if (status != 0)
648 {
649 size_t i;
651 for (i = 0; i < number; i++)
652 {
653 sfree (names[i]);
654 }
655 sfree (names);
657 return (-1);
658 }
660 *ret_names = names;
661 if (ret_times != NULL)
662 *ret_times = times;
663 *ret_number = number;
665 return (0);
666 } /* int uc_get_names */
668 int uc_get_state (const data_set_t *ds, const value_list_t *vl)
669 {
670 char name[6 * DATA_MAX_NAME_LEN];
671 cache_entry_t *ce = NULL;
672 int ret = STATE_ERROR;
674 if (FORMAT_VL (name, sizeof (name), vl) != 0)
675 {
676 ERROR ("uc_get_state: FORMAT_VL failed.");
677 return (STATE_ERROR);
678 }
680 pthread_mutex_lock (&cache_lock);
682 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
683 {
684 assert (ce != NULL);
685 ret = ce->state;
686 }
688 pthread_mutex_unlock (&cache_lock);
690 return (ret);
691 } /* int uc_get_state */
693 int uc_set_state (const data_set_t *ds, const value_list_t *vl, int state)
694 {
695 char name[6 * DATA_MAX_NAME_LEN];
696 cache_entry_t *ce = NULL;
697 int ret = -1;
699 if (FORMAT_VL (name, sizeof (name), vl) != 0)
700 {
701 ERROR ("uc_get_state: FORMAT_VL failed.");
702 return (STATE_ERROR);
703 }
705 pthread_mutex_lock (&cache_lock);
707 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
708 {
709 assert (ce != NULL);
710 ret = ce->state;
711 ce->state = state;
712 }
714 pthread_mutex_unlock (&cache_lock);
716 return (ret);
717 } /* int uc_set_state */
719 int uc_get_history_by_name (const char *name,
720 gauge_t *ret_history, size_t num_steps, size_t num_ds)
721 {
722 cache_entry_t *ce = NULL;
723 size_t i;
724 int status = 0;
726 pthread_mutex_lock (&cache_lock);
728 status = c_avl_get (cache_tree, name, (void *) &ce);
729 if (status != 0)
730 {
731 pthread_mutex_unlock (&cache_lock);
732 return (-ENOENT);
733 }
735 if (((size_t) ce->values_num) != num_ds)
736 {
737 pthread_mutex_unlock (&cache_lock);
738 return (-EINVAL);
739 }
741 /* Check if there are enough values available. If not, increase the buffer
742 * size. */
743 if (ce->history_length < num_steps)
744 {
745 gauge_t *tmp;
746 size_t i;
748 tmp = realloc (ce->history, sizeof (*ce->history)
749 * num_steps * ce->values_num);
750 if (tmp == NULL)
751 {
752 pthread_mutex_unlock (&cache_lock);
753 return (-ENOMEM);
754 }
756 for (i = ce->history_length * ce->values_num;
757 i < (num_steps * ce->values_num);
758 i++)
759 tmp[i] = NAN;
761 ce->history = tmp;
762 ce->history_length = num_steps;
763 } /* if (ce->history_length < num_steps) */
765 /* Copy the values to the output buffer. */
766 for (i = 0; i < num_steps; i++)
767 {
768 size_t src_index;
769 size_t dst_index;
771 if (i < ce->history_index)
772 src_index = ce->history_index - (i + 1);
773 else
774 src_index = ce->history_length + ce->history_index - (i + 1);
775 src_index = src_index * num_ds;
777 dst_index = i * num_ds;
779 memcpy (ret_history + dst_index, ce->history + src_index,
780 sizeof (*ret_history) * num_ds);
781 }
783 pthread_mutex_unlock (&cache_lock);
785 return (0);
786 } /* int uc_get_history_by_name */
788 int uc_get_history (const data_set_t *ds, const value_list_t *vl,
789 gauge_t *ret_history, size_t num_steps, size_t num_ds)
790 {
791 char name[6 * DATA_MAX_NAME_LEN];
793 if (FORMAT_VL (name, sizeof (name), vl) != 0)
794 {
795 ERROR ("utils_cache: uc_get_history: FORMAT_VL failed.");
796 return (-1);
797 }
799 return (uc_get_history_by_name (name, ret_history, num_steps, num_ds));
800 } /* int uc_get_history */
802 int uc_get_hits (const data_set_t *ds, const value_list_t *vl)
803 {
804 char name[6 * DATA_MAX_NAME_LEN];
805 cache_entry_t *ce = NULL;
806 int ret = STATE_ERROR;
808 if (FORMAT_VL (name, sizeof (name), vl) != 0)
809 {
810 ERROR ("uc_get_state: FORMAT_VL failed.");
811 return (STATE_ERROR);
812 }
814 pthread_mutex_lock (&cache_lock);
816 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
817 {
818 assert (ce != NULL);
819 ret = ce->hits;
820 }
822 pthread_mutex_unlock (&cache_lock);
824 return (ret);
825 } /* int uc_get_hits */
827 int uc_set_hits (const data_set_t *ds, const value_list_t *vl, int hits)
828 {
829 char name[6 * DATA_MAX_NAME_LEN];
830 cache_entry_t *ce = NULL;
831 int ret = -1;
833 if (FORMAT_VL (name, sizeof (name), vl) != 0)
834 {
835 ERROR ("uc_get_state: FORMAT_VL failed.");
836 return (STATE_ERROR);
837 }
839 pthread_mutex_lock (&cache_lock);
841 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
842 {
843 assert (ce != NULL);
844 ret = ce->hits;
845 ce->hits = hits;
846 }
848 pthread_mutex_unlock (&cache_lock);
850 return (ret);
851 } /* int uc_set_hits */
853 int uc_inc_hits (const data_set_t *ds, const value_list_t *vl, int step)
854 {
855 char name[6 * DATA_MAX_NAME_LEN];
856 cache_entry_t *ce = NULL;
857 int ret = -1;
859 if (FORMAT_VL (name, sizeof (name), vl) != 0)
860 {
861 ERROR ("uc_get_state: FORMAT_VL failed.");
862 return (STATE_ERROR);
863 }
865 pthread_mutex_lock (&cache_lock);
867 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
868 {
869 assert (ce != NULL);
870 ret = ce->hits;
871 ce->hits = ret + step;
872 }
874 pthread_mutex_unlock (&cache_lock);
876 return (ret);
877 } /* int uc_inc_hits */
879 /*
880 * Meta data interface
881 */
882 /* XXX: This function will acquire `cache_lock' but will not free it! */
883 static meta_data_t *uc_get_meta (const value_list_t *vl) /* {{{ */
884 {
885 char name[6 * DATA_MAX_NAME_LEN];
886 cache_entry_t *ce = NULL;
887 int status;
889 status = FORMAT_VL (name, sizeof (name), vl);
890 if (status != 0)
891 {
892 ERROR ("utils_cache: uc_get_meta: FORMAT_VL failed.");
893 return (NULL);
894 }
896 pthread_mutex_lock (&cache_lock);
898 status = c_avl_get (cache_tree, name, (void *) &ce);
899 if (status != 0)
900 {
901 pthread_mutex_unlock (&cache_lock);
902 return (NULL);
903 }
904 assert (ce != NULL);
906 if (ce->meta == NULL)
907 ce->meta = meta_data_create ();
909 if (ce->meta == NULL)
910 pthread_mutex_unlock (&cache_lock);
912 return (ce->meta);
913 } /* }}} meta_data_t *uc_get_meta */
915 /* Sorry about this preprocessor magic, but it really makes this file much
916 * shorter.. */
917 #define UC_WRAP(wrap_function) { \
918 meta_data_t *meta; \
919 int status; \
920 meta = uc_get_meta (vl); \
921 if (meta == NULL) return (-1); \
922 status = wrap_function (meta, key); \
923 pthread_mutex_unlock (&cache_lock); \
924 return (status); \
925 }
926 int uc_meta_data_exists (const value_list_t *vl, const char *key)
927 UC_WRAP (meta_data_exists)
929 int uc_meta_data_delete (const value_list_t *vl, const char *key)
930 UC_WRAP (meta_data_delete)
931 #undef UC_WRAP
933 /* We need a new version of this macro because the following functions take
934 * two argumetns. */
935 #define UC_WRAP(wrap_function) { \
936 meta_data_t *meta; \
937 int status; \
938 meta = uc_get_meta (vl); \
939 if (meta == NULL) return (-1); \
940 status = wrap_function (meta, key, value); \
941 pthread_mutex_unlock (&cache_lock); \
942 return (status); \
943 }
944 int uc_meta_data_add_string (const value_list_t *vl,
945 const char *key,
946 const char *value)
947 UC_WRAP(meta_data_add_string)
948 int uc_meta_data_add_signed_int (const value_list_t *vl,
949 const char *key,
950 int64_t value)
951 UC_WRAP(meta_data_add_signed_int)
952 int uc_meta_data_add_unsigned_int (const value_list_t *vl,
953 const char *key,
954 uint64_t value)
955 UC_WRAP(meta_data_add_unsigned_int)
956 int uc_meta_data_add_double (const value_list_t *vl,
957 const char *key,
958 double value)
959 UC_WRAP(meta_data_add_double)
960 int uc_meta_data_add_boolean (const value_list_t *vl,
961 const char *key,
962 _Bool value)
963 UC_WRAP(meta_data_add_boolean)
965 int uc_meta_data_get_string (const value_list_t *vl,
966 const char *key,
967 char **value)
968 UC_WRAP(meta_data_get_string)
969 int uc_meta_data_get_signed_int (const value_list_t *vl,
970 const char *key,
971 int64_t *value)
972 UC_WRAP(meta_data_get_signed_int)
973 int uc_meta_data_get_unsigned_int (const value_list_t *vl,
974 const char *key,
975 uint64_t *value)
976 UC_WRAP(meta_data_get_unsigned_int)
977 int uc_meta_data_get_double (const value_list_t *vl,
978 const char *key,
979 double *value)
980 UC_WRAP(meta_data_get_double)
981 int uc_meta_data_get_boolean (const value_list_t *vl,
982 const char *key,
983 _Bool *value)
984 UC_WRAP(meta_data_get_boolean)
985 #undef UC_WRAP
987 /* vim: set sw=2 ts=8 sts=2 tw=78 : */