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 "meta_data.h"
29 #include <assert.h>
30 #include <pthread.h>
32 typedef struct cache_entry_s
33 {
34 char name[6 * DATA_MAX_NAME_LEN];
35 int values_num;
36 gauge_t *values_gauge;
37 value_t *values_raw;
38 /* Time contained in the package
39 * (for calculating rates) */
40 cdtime_t last_time;
41 /* Time according to the local clock
42 * (for purging old entries) */
43 cdtime_t last_update;
44 /* Interval in which the data is collected
45 * (for purding old entries) */
46 cdtime_t interval;
47 int state;
48 int hits;
50 /*
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 * +-----------------+-----------------+-----------------+----
58 */
59 gauge_t *history;
60 size_t history_index; /* points to the next position to write to. */
61 size_t history_length;
63 meta_data_t *meta;
64 } cache_entry_t;
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)
70 {
71 #if COLLECT_DEBUG
72 assert ((a != NULL) && (b != NULL));
73 #endif
74 return (strcmp (a->name, b->name));
75 } /* int cache_compare */
77 static cache_entry_t *cache_alloc (int values_num)
78 {
79 cache_entry_t *ce;
81 ce = (cache_entry_t *) malloc (sizeof (cache_entry_t));
82 if (ce == NULL)
83 {
84 ERROR ("utils_cache: cache_alloc: malloc failed.");
85 return (NULL);
86 }
87 memset (ce, '\0', sizeof (cache_entry_t));
88 ce->values_num = values_num;
90 ce->values_gauge = calloc (values_num, sizeof (*ce->values_gauge));
91 ce->values_raw = calloc (values_num, sizeof (*ce->values_raw));
92 if ((ce->values_gauge == NULL) || (ce->values_raw == NULL))
93 {
94 sfree (ce->values_gauge);
95 sfree (ce->values_raw);
96 sfree (ce);
97 ERROR ("utils_cache: cache_alloc: calloc failed.");
98 return (NULL);
99 }
101 ce->history = NULL;
102 ce->history_length = 0;
103 ce->meta = NULL;
105 return (ce);
106 } /* cache_entry_t *cache_alloc */
108 static void cache_free (cache_entry_t *ce)
109 {
110 if (ce == NULL)
111 return;
113 sfree (ce->values_gauge);
114 sfree (ce->values_raw);
115 sfree (ce->history);
116 if (ce->meta != NULL)
117 {
118 meta_data_destroy (ce->meta);
119 ce->meta = NULL;
120 }
121 sfree (ce);
122 } /* void cache_free */
124 static void uc_check_range (const data_set_t *ds, cache_entry_t *ce)
125 {
126 int i;
128 for (i = 0; i < ds->ds_num; i++)
129 {
130 if (isnan (ce->values_gauge[i]))
131 continue;
132 else if (ce->values_gauge[i] < ds->ds[i].min)
133 ce->values_gauge[i] = NAN;
134 else if (ce->values_gauge[i] > ds->ds[i].max)
135 ce->values_gauge[i] = NAN;
136 }
137 } /* void uc_check_range */
139 static int uc_insert (const data_set_t *ds, const value_list_t *vl,
140 const char *key)
141 {
142 int i;
143 char *key_copy;
144 cache_entry_t *ce;
146 /* `cache_lock' has been locked by `uc_update' */
148 key_copy = strdup (key);
149 if (key_copy == NULL)
150 {
151 ERROR ("uc_insert: strdup failed.");
152 return (-1);
153 }
155 ce = cache_alloc (ds->ds_num);
156 if (ce == NULL)
157 {
158 sfree (key_copy);
159 ERROR ("uc_insert: cache_alloc (%i) failed.", ds->ds_num);
160 return (-1);
161 }
163 sstrncpy (ce->name, key, sizeof (ce->name));
165 for (i = 0; i < ds->ds_num; i++)
166 {
167 switch (ds->ds[i].type)
168 {
169 case DS_TYPE_COUNTER:
170 ce->values_gauge[i] = NAN;
171 ce->values_raw[i].counter = vl->values[i].counter;
172 break;
174 case DS_TYPE_GAUGE:
175 ce->values_gauge[i] = vl->values[i].gauge;
176 ce->values_raw[i].gauge = vl->values[i].gauge;
177 break;
179 case DS_TYPE_DERIVE:
180 ce->values_gauge[i] = NAN;
181 ce->values_raw[i].derive = vl->values[i].derive;
182 break;
184 case DS_TYPE_ABSOLUTE:
185 ce->values_gauge[i] = NAN;
186 if (vl->interval > 0)
187 ce->values_gauge[i] = ((double) vl->values[i].absolute)
188 / CDTIME_T_TO_DOUBLE (vl->interval);
189 ce->values_raw[i].absolute = vl->values[i].absolute;
190 break;
192 default:
193 /* This shouldn't happen. */
194 ERROR ("uc_insert: Don't know how to handle data source type %i.",
195 ds->ds[i].type);
196 return (-1);
197 } /* switch (ds->ds[i].type) */
198 } /* for (i) */
200 /* Prune invalid gauge data */
201 uc_check_range (ds, ce);
203 ce->last_time = vl->time;
204 ce->last_update = cdtime ();
205 ce->interval = vl->interval;
206 ce->state = STATE_OKAY;
208 if (c_avl_insert (cache_tree, key_copy, ce) != 0)
209 {
210 sfree (key_copy);
211 ERROR ("uc_insert: c_avl_insert failed.");
212 return (-1);
213 }
215 DEBUG ("uc_insert: Added %s to the cache.", key);
216 return (0);
217 } /* int uc_insert */
219 int uc_init (void)
220 {
221 if (cache_tree == NULL)
222 cache_tree = c_avl_create ((int (*) (const void *, const void *))
223 cache_compare);
225 return (0);
226 } /* int uc_init */
228 int uc_check_timeout (void)
229 {
230 cdtime_t now;
231 cache_entry_t *ce;
233 char **keys = NULL;
234 cdtime_t *keys_time = NULL;
235 cdtime_t *keys_interval = NULL;
236 int keys_len = 0;
238 char *key;
239 c_avl_iterator_t *iter;
241 int status;
242 int i;
244 pthread_mutex_lock (&cache_lock);
246 now = cdtime ();
248 /* Build a list of entries to be flushed */
249 iter = c_avl_get_iterator (cache_tree);
250 while (c_avl_iterator_next (iter, (void *) &key, (void *) &ce) == 0)
251 {
252 char **tmp;
253 cdtime_t *tmp_time;
255 /* If the entry is fresh enough, continue. */
256 if ((now - ce->last_update) < (ce->interval * timeout_g))
257 continue;
259 /* If entry has not been updated, add to `keys' array */
260 tmp = (char **) realloc ((void *) keys,
261 (keys_len + 1) * sizeof (char *));
262 if (tmp == NULL)
263 {
264 ERROR ("uc_check_timeout: realloc failed.");
265 continue;
266 }
267 keys = tmp;
269 tmp_time = realloc (keys_time, (keys_len + 1) * sizeof (*keys_time));
270 if (tmp_time == NULL)
271 {
272 ERROR ("uc_check_timeout: realloc failed.");
273 continue;
274 }
275 keys_time = tmp_time;
277 tmp_time = realloc (keys_interval, (keys_len + 1) * sizeof (*keys_interval));
278 if (tmp_time == NULL)
279 {
280 ERROR ("uc_check_timeout: realloc failed.");
281 continue;
282 }
283 keys_interval = tmp_time;
285 keys[keys_len] = strdup (key);
286 if (keys[keys_len] == NULL)
287 {
288 ERROR ("uc_check_timeout: strdup failed.");
289 continue;
290 }
291 keys_time[keys_len] = ce->last_time;
292 keys_interval[keys_len] = ce->interval;
294 keys_len++;
295 } /* while (c_avl_iterator_next) */
297 c_avl_iterator_destroy (iter);
298 pthread_mutex_unlock (&cache_lock);
300 if (keys_len == 0)
301 return (0);
303 /* Call the "missing" callback for each value. Do this before removing the
304 * value from the cache, so that callbacks can still access the data stored,
305 * including plugin specific meta data, rates, history, …. This must be done
306 * without holding the lock, otherwise we will run into a deadlock if a
307 * plugin calls the cache interface. */
308 for (i = 0; i < keys_len; i++)
309 {
310 value_list_t vl = VALUE_LIST_INIT;
312 vl.values = NULL;
313 vl.values_len = 0;
314 vl.meta = NULL;
316 status = parse_identifier_vl (keys[i], &vl);
317 if (status != 0)
318 {
319 ERROR ("uc_check_timeout: parse_identifier_vl (\"%s\") failed.", keys[i]);
320 cache_free (ce);
321 continue;
322 }
324 vl.time = keys_time[i];
325 vl.interval = keys_interval[i];
327 plugin_dispatch_missing (&vl);
328 } /* for (i = 0; i < keys_len; i++) */
330 /* Now actually remove all the values from the cache. We don't re-evaluate
331 * the timestamp again, so in theory it is possible we remove a value after
332 * it is updated here. */
333 pthread_mutex_lock (&cache_lock);
334 for (i = 0; i < keys_len; i++)
335 {
336 key = NULL;
337 ce = NULL;
339 status = c_avl_remove (cache_tree, keys[i],
340 (void *) &key, (void *) &ce);
341 if (status != 0)
342 {
343 ERROR ("uc_check_timeout: c_avl_remove (\"%s\") failed.", keys[i]);
344 sfree (keys[i]);
345 continue;
346 }
348 sfree (keys[i]);
349 sfree (key);
350 cache_free (ce);
351 } /* for (i = 0; i < keys_len; i++) */
352 pthread_mutex_unlock (&cache_lock);
354 sfree (keys);
355 sfree (keys_time);
356 sfree (keys_interval);
358 return (0);
359 } /* int uc_check_timeout */
361 int uc_update (const data_set_t *ds, const value_list_t *vl)
362 {
363 char name[6 * DATA_MAX_NAME_LEN];
364 cache_entry_t *ce = NULL;
365 int status;
366 int i;
368 if (FORMAT_VL (name, sizeof (name), vl) != 0)
369 {
370 ERROR ("uc_update: FORMAT_VL failed.");
371 return (-1);
372 }
374 pthread_mutex_lock (&cache_lock);
376 status = c_avl_get (cache_tree, name, (void *) &ce);
377 if (status != 0) /* entry does not yet exist */
378 {
379 status = uc_insert (ds, vl, name);
380 pthread_mutex_unlock (&cache_lock);
381 return (status);
382 }
384 assert (ce != NULL);
385 assert (ce->values_num == ds->ds_num);
387 if (ce->last_time >= vl->time)
388 {
389 pthread_mutex_unlock (&cache_lock);
390 NOTICE ("uc_update: Value too old: name = %s; value time = %.3f; "
391 "last cache update = %.3f;",
392 name,
393 CDTIME_T_TO_DOUBLE (vl->time),
394 CDTIME_T_TO_DOUBLE (ce->last_time));
395 return (-1);
396 }
398 for (i = 0; i < ds->ds_num; i++)
399 {
400 switch (ds->ds[i].type)
401 {
402 case DS_TYPE_COUNTER:
403 {
404 counter_t diff;
406 /* check if the counter has wrapped around */
407 if (vl->values[i].counter < ce->values_raw[i].counter)
408 {
409 if (ce->values_raw[i].counter <= 4294967295U)
410 diff = (4294967295U - ce->values_raw[i].counter)
411 + vl->values[i].counter;
412 else
413 diff = (18446744073709551615ULL - ce->values_raw[i].counter)
414 + vl->values[i].counter;
415 }
416 else /* counter has NOT wrapped around */
417 {
418 diff = vl->values[i].counter - ce->values_raw[i].counter;
419 }
421 ce->values_gauge[i] = ((double) diff)
422 / (CDTIME_T_TO_DOUBLE (vl->time - ce->last_time));
423 ce->values_raw[i].counter = vl->values[i].counter;
424 }
425 break;
427 case DS_TYPE_GAUGE:
428 ce->values_raw[i].gauge = vl->values[i].gauge;
429 ce->values_gauge[i] = vl->values[i].gauge;
430 break;
432 case DS_TYPE_DERIVE:
433 {
434 derive_t diff;
436 diff = vl->values[i].derive - ce->values_raw[i].derive;
438 ce->values_gauge[i] = ((double) diff)
439 / (CDTIME_T_TO_DOUBLE (vl->time - ce->last_time));
440 ce->values_raw[i].derive = vl->values[i].derive;
441 }
442 break;
444 case DS_TYPE_ABSOLUTE:
445 ce->values_gauge[i] = ((double) vl->values[i].absolute)
446 / (CDTIME_T_TO_DOUBLE (vl->time - ce->last_time));
447 ce->values_raw[i].absolute = vl->values[i].absolute;
448 break;
450 default:
451 /* This shouldn't happen. */
452 pthread_mutex_unlock (&cache_lock);
453 ERROR ("uc_update: Don't know how to handle data source type %i.",
454 ds->ds[i].type);
455 return (-1);
456 } /* switch (ds->ds[i].type) */
458 DEBUG ("uc_update: %s: ds[%i] = %lf", name, i, ce->values_gauge[i]);
459 } /* for (i) */
461 /* Update the history if it exists. */
462 if (ce->history != NULL)
463 {
464 assert (ce->history_index < ce->history_length);
465 for (i = 0; i < ce->values_num; i++)
466 {
467 size_t hist_idx = (ce->values_num * ce->history_index) + i;
468 ce->history[hist_idx] = ce->values_gauge[i];
469 }
471 assert (ce->history_length > 0);
472 ce->history_index = (ce->history_index + 1) % ce->history_length;
473 }
475 /* Prune invalid gauge data */
476 uc_check_range (ds, ce);
478 ce->last_time = vl->time;
479 ce->last_update = cdtime ();
480 ce->interval = vl->interval;
482 pthread_mutex_unlock (&cache_lock);
484 return (0);
485 } /* int uc_update */
487 int uc_get_rate_by_name (const char *name, gauge_t **ret_values, size_t *ret_values_num)
488 {
489 gauge_t *ret = NULL;
490 size_t ret_num = 0;
491 cache_entry_t *ce = NULL;
492 int status = 0;
494 pthread_mutex_lock (&cache_lock);
496 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
497 {
498 assert (ce != NULL);
500 /* remove missing values from getval */
501 if (ce->state == STATE_MISSING)
502 {
503 status = -1;
504 }
505 else
506 {
507 ret_num = ce->values_num;
508 ret = (gauge_t *) malloc (ret_num * sizeof (gauge_t));
509 if (ret == NULL)
510 {
511 ERROR ("utils_cache: uc_get_rate_by_name: malloc failed.");
512 status = -1;
513 }
514 else
515 {
516 memcpy (ret, ce->values_gauge, ret_num * sizeof (gauge_t));
517 }
518 }
519 }
520 else
521 {
522 DEBUG ("utils_cache: uc_get_rate_by_name: No such value: %s", name);
523 status = -1;
524 }
526 pthread_mutex_unlock (&cache_lock);
528 if (status == 0)
529 {
530 *ret_values = ret;
531 *ret_values_num = ret_num;
532 }
534 return (status);
535 } /* gauge_t *uc_get_rate_by_name */
537 gauge_t *uc_get_rate (const data_set_t *ds, const value_list_t *vl)
538 {
539 char name[6 * DATA_MAX_NAME_LEN];
540 gauge_t *ret = NULL;
541 size_t ret_num = 0;
542 int status;
544 if (FORMAT_VL (name, sizeof (name), vl) != 0)
545 {
546 ERROR ("utils_cache: uc_get_rate: FORMAT_VL failed.");
547 return (NULL);
548 }
550 status = uc_get_rate_by_name (name, &ret, &ret_num);
551 if (status != 0)
552 return (NULL);
554 /* This is important - the caller has no other way of knowing how many
555 * values are returned. */
556 if (ret_num != (size_t) ds->ds_num)
557 {
558 ERROR ("utils_cache: uc_get_rate: ds[%s] has %i values, "
559 "but uc_get_rate_by_name returned %zu.",
560 ds->type, ds->ds_num, ret_num);
561 sfree (ret);
562 return (NULL);
563 }
565 return (ret);
566 } /* gauge_t *uc_get_rate */
568 int uc_get_names (char ***ret_names, cdtime_t **ret_times, size_t *ret_number)
569 {
570 c_avl_iterator_t *iter;
571 char *key;
572 cache_entry_t *value;
574 char **names = NULL;
575 cdtime_t *times = NULL;
576 size_t number = 0;
577 size_t size_arrays = 0;
579 int status = 0;
581 if ((ret_names == NULL) || (ret_number == NULL))
582 return (-1);
584 pthread_mutex_lock (&cache_lock);
586 size_arrays = (size_t) c_avl_size (cache_tree);
587 if (size_arrays < 1)
588 {
589 /* Handle the "no values" case here, to avoid the error message when
590 * calloc() returns NULL. */
591 pthread_mutex_unlock (&cache_lock);
592 return (0);
593 }
595 names = calloc (size_arrays, sizeof (*names));
596 times = calloc (size_arrays, sizeof (*times));
597 if ((names == NULL) || (times == NULL))
598 {
599 ERROR ("uc_get_names: calloc failed.");
600 sfree (names);
601 sfree (times);
602 pthread_mutex_unlock (&cache_lock);
603 return (ENOMEM);
604 }
606 iter = c_avl_get_iterator (cache_tree);
607 while (c_avl_iterator_next (iter, (void *) &key, (void *) &value) == 0)
608 {
609 /* remove missing values when list values */
610 if (value->state == STATE_MISSING)
611 continue;
613 /* c_avl_size does not return a number smaller than the number of elements
614 * returned by c_avl_iterator_next. */
615 assert (number < size_arrays);
617 if (ret_times != NULL)
618 times[number] = value->last_time;
620 names[number] = strdup (key);
621 if (names[number] == NULL)
622 {
623 status = -1;
624 break;
625 }
627 number++;
628 } /* while (c_avl_iterator_next) */
630 c_avl_iterator_destroy (iter);
631 pthread_mutex_unlock (&cache_lock);
633 if (status != 0)
634 {
635 size_t i;
637 for (i = 0; i < number; i++)
638 {
639 sfree (names[i]);
640 }
641 sfree (names);
643 return (-1);
644 }
646 *ret_names = names;
647 if (ret_times != NULL)
648 *ret_times = times;
649 *ret_number = number;
651 return (0);
652 } /* int uc_get_names */
654 int uc_get_state (const data_set_t *ds, const value_list_t *vl)
655 {
656 char name[6 * DATA_MAX_NAME_LEN];
657 cache_entry_t *ce = NULL;
658 int ret = STATE_ERROR;
660 if (FORMAT_VL (name, sizeof (name), vl) != 0)
661 {
662 ERROR ("uc_get_state: FORMAT_VL failed.");
663 return (STATE_ERROR);
664 }
666 pthread_mutex_lock (&cache_lock);
668 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
669 {
670 assert (ce != NULL);
671 ret = ce->state;
672 }
674 pthread_mutex_unlock (&cache_lock);
676 return (ret);
677 } /* int uc_get_state */
679 int uc_set_state (const data_set_t *ds, const value_list_t *vl, int state)
680 {
681 char name[6 * DATA_MAX_NAME_LEN];
682 cache_entry_t *ce = NULL;
683 int ret = -1;
685 if (FORMAT_VL (name, sizeof (name), vl) != 0)
686 {
687 ERROR ("uc_get_state: FORMAT_VL failed.");
688 return (STATE_ERROR);
689 }
691 pthread_mutex_lock (&cache_lock);
693 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
694 {
695 assert (ce != NULL);
696 ret = ce->state;
697 ce->state = state;
698 }
700 pthread_mutex_unlock (&cache_lock);
702 return (ret);
703 } /* int uc_set_state */
705 int uc_get_history_by_name (const char *name,
706 gauge_t *ret_history, size_t num_steps, size_t num_ds)
707 {
708 cache_entry_t *ce = NULL;
709 size_t i;
710 int status = 0;
712 pthread_mutex_lock (&cache_lock);
714 status = c_avl_get (cache_tree, name, (void *) &ce);
715 if (status != 0)
716 {
717 pthread_mutex_unlock (&cache_lock);
718 return (-ENOENT);
719 }
721 if (((size_t) ce->values_num) != num_ds)
722 {
723 pthread_mutex_unlock (&cache_lock);
724 return (-EINVAL);
725 }
727 /* Check if there are enough values available. If not, increase the buffer
728 * size. */
729 if (ce->history_length < num_steps)
730 {
731 gauge_t *tmp;
732 size_t i;
734 tmp = realloc (ce->history, sizeof (*ce->history)
735 * num_steps * ce->values_num);
736 if (tmp == NULL)
737 {
738 pthread_mutex_unlock (&cache_lock);
739 return (-ENOMEM);
740 }
742 for (i = ce->history_length * ce->values_num;
743 i < (num_steps * ce->values_num);
744 i++)
745 tmp[i] = NAN;
747 ce->history = tmp;
748 ce->history_length = num_steps;
749 } /* if (ce->history_length < num_steps) */
751 /* Copy the values to the output buffer. */
752 for (i = 0; i < num_steps; i++)
753 {
754 size_t src_index;
755 size_t dst_index;
757 if (i < ce->history_index)
758 src_index = ce->history_index - (i + 1);
759 else
760 src_index = ce->history_length + ce->history_index - (i + 1);
761 src_index = src_index * num_ds;
763 dst_index = i * num_ds;
765 memcpy (ret_history + dst_index, ce->history + src_index,
766 sizeof (*ret_history) * num_ds);
767 }
769 pthread_mutex_unlock (&cache_lock);
771 return (0);
772 } /* int uc_get_history_by_name */
774 int uc_get_history (const data_set_t *ds, const value_list_t *vl,
775 gauge_t *ret_history, size_t num_steps, size_t num_ds)
776 {
777 char name[6 * DATA_MAX_NAME_LEN];
779 if (FORMAT_VL (name, sizeof (name), vl) != 0)
780 {
781 ERROR ("utils_cache: uc_get_history: FORMAT_VL failed.");
782 return (-1);
783 }
785 return (uc_get_history_by_name (name, ret_history, num_steps, num_ds));
786 } /* int uc_get_history */
788 int uc_get_hits (const data_set_t *ds, const value_list_t *vl)
789 {
790 char name[6 * DATA_MAX_NAME_LEN];
791 cache_entry_t *ce = NULL;
792 int ret = STATE_ERROR;
794 if (FORMAT_VL (name, sizeof (name), vl) != 0)
795 {
796 ERROR ("uc_get_state: FORMAT_VL failed.");
797 return (STATE_ERROR);
798 }
800 pthread_mutex_lock (&cache_lock);
802 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
803 {
804 assert (ce != NULL);
805 ret = ce->hits;
806 }
808 pthread_mutex_unlock (&cache_lock);
810 return (ret);
811 } /* int uc_get_hits */
813 int uc_set_hits (const data_set_t *ds, const value_list_t *vl, int hits)
814 {
815 char name[6 * DATA_MAX_NAME_LEN];
816 cache_entry_t *ce = NULL;
817 int ret = -1;
819 if (FORMAT_VL (name, sizeof (name), vl) != 0)
820 {
821 ERROR ("uc_get_state: FORMAT_VL failed.");
822 return (STATE_ERROR);
823 }
825 pthread_mutex_lock (&cache_lock);
827 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
828 {
829 assert (ce != NULL);
830 ret = ce->hits;
831 ce->hits = hits;
832 }
834 pthread_mutex_unlock (&cache_lock);
836 return (ret);
837 } /* int uc_set_hits */
839 int uc_inc_hits (const data_set_t *ds, const value_list_t *vl, int step)
840 {
841 char name[6 * DATA_MAX_NAME_LEN];
842 cache_entry_t *ce = NULL;
843 int ret = -1;
845 if (FORMAT_VL (name, sizeof (name), vl) != 0)
846 {
847 ERROR ("uc_get_state: FORMAT_VL failed.");
848 return (STATE_ERROR);
849 }
851 pthread_mutex_lock (&cache_lock);
853 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
854 {
855 assert (ce != NULL);
856 ret = ce->hits;
857 ce->hits = ret + step;
858 }
860 pthread_mutex_unlock (&cache_lock);
862 return (ret);
863 } /* int uc_inc_hits */
865 /*
866 * Meta data interface
867 */
868 /* XXX: This function will acquire `cache_lock' but will not free it! */
869 static meta_data_t *uc_get_meta (const value_list_t *vl) /* {{{ */
870 {
871 char name[6 * DATA_MAX_NAME_LEN];
872 cache_entry_t *ce = NULL;
873 int status;
875 status = FORMAT_VL (name, sizeof (name), vl);
876 if (status != 0)
877 {
878 ERROR ("utils_cache: uc_get_meta: FORMAT_VL failed.");
879 return (NULL);
880 }
882 pthread_mutex_lock (&cache_lock);
884 status = c_avl_get (cache_tree, name, (void *) &ce);
885 if (status != 0)
886 {
887 pthread_mutex_unlock (&cache_lock);
888 return (NULL);
889 }
890 assert (ce != NULL);
892 if (ce->meta == NULL)
893 ce->meta = meta_data_create ();
895 if (ce->meta == NULL)
896 pthread_mutex_unlock (&cache_lock);
898 return (ce->meta);
899 } /* }}} meta_data_t *uc_get_meta */
901 /* Sorry about this preprocessor magic, but it really makes this file much
902 * shorter.. */
903 #define UC_WRAP(wrap_function) { \
904 meta_data_t *meta; \
905 int status; \
906 meta = uc_get_meta (vl); \
907 if (meta == NULL) return (-1); \
908 status = wrap_function (meta, key); \
909 pthread_mutex_unlock (&cache_lock); \
910 return (status); \
911 }
912 int uc_meta_data_exists (const value_list_t *vl, const char *key)
913 UC_WRAP (meta_data_exists)
915 int uc_meta_data_delete (const value_list_t *vl, const char *key)
916 UC_WRAP (meta_data_delete)
917 #undef UC_WRAP
919 /* We need a new version of this macro because the following functions take
920 * two argumetns. */
921 #define UC_WRAP(wrap_function) { \
922 meta_data_t *meta; \
923 int status; \
924 meta = uc_get_meta (vl); \
925 if (meta == NULL) return (-1); \
926 status = wrap_function (meta, key, value); \
927 pthread_mutex_unlock (&cache_lock); \
928 return (status); \
929 }
930 int uc_meta_data_add_string (const value_list_t *vl,
931 const char *key,
932 const char *value)
933 UC_WRAP(meta_data_add_string)
934 int uc_meta_data_add_signed_int (const value_list_t *vl,
935 const char *key,
936 int64_t value)
937 UC_WRAP(meta_data_add_signed_int)
938 int uc_meta_data_add_unsigned_int (const value_list_t *vl,
939 const char *key,
940 uint64_t value)
941 UC_WRAP(meta_data_add_unsigned_int)
942 int uc_meta_data_add_double (const value_list_t *vl,
943 const char *key,
944 double value)
945 UC_WRAP(meta_data_add_double)
946 int uc_meta_data_add_boolean (const value_list_t *vl,
947 const char *key,
948 _Bool value)
949 UC_WRAP(meta_data_add_boolean)
951 int uc_meta_data_get_string (const value_list_t *vl,
952 const char *key,
953 char **value)
954 UC_WRAP(meta_data_get_string)
955 int uc_meta_data_get_signed_int (const value_list_t *vl,
956 const char *key,
957 int64_t *value)
958 UC_WRAP(meta_data_get_signed_int)
959 int uc_meta_data_get_unsigned_int (const value_list_t *vl,
960 const char *key,
961 uint64_t *value)
962 UC_WRAP(meta_data_get_unsigned_int)
963 int uc_meta_data_get_double (const value_list_t *vl,
964 const char *key,
965 double *value)
966 UC_WRAP(meta_data_get_double)
967 int uc_meta_data_get_boolean (const value_list_t *vl,
968 const char *key,
969 _Bool *value)
970 UC_WRAP(meta_data_get_boolean)
971 #undef UC_WRAP
973 /* vim: set sw=2 ts=8 sts=2 tw=78 : */