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 assert ((a != NULL) && (b != NULL));
72 return (strcmp (a->name, b->name));
73 } /* int cache_compare */
75 static cache_entry_t *cache_alloc (int values_num)
76 {
77 cache_entry_t *ce;
79 ce = (cache_entry_t *) malloc (sizeof (cache_entry_t));
80 if (ce == NULL)
81 {
82 ERROR ("utils_cache: cache_alloc: malloc failed.");
83 return (NULL);
84 }
85 memset (ce, '\0', sizeof (cache_entry_t));
86 ce->values_num = values_num;
88 ce->values_gauge = calloc (values_num, sizeof (*ce->values_gauge));
89 ce->values_raw = calloc (values_num, sizeof (*ce->values_raw));
90 if ((ce->values_gauge == NULL) || (ce->values_raw == NULL))
91 {
92 sfree (ce->values_gauge);
93 sfree (ce->values_raw);
94 sfree (ce);
95 ERROR ("utils_cache: cache_alloc: calloc failed.");
96 return (NULL);
97 }
99 ce->history = NULL;
100 ce->history_length = 0;
101 ce->meta = NULL;
103 return (ce);
104 } /* cache_entry_t *cache_alloc */
106 static void cache_free (cache_entry_t *ce)
107 {
108 if (ce == NULL)
109 return;
111 sfree (ce->values_gauge);
112 sfree (ce->values_raw);
113 sfree (ce->history);
114 if (ce->meta != NULL)
115 {
116 meta_data_destroy (ce->meta);
117 ce->meta = NULL;
118 }
119 sfree (ce);
120 } /* void cache_free */
122 static void uc_check_range (const data_set_t *ds, cache_entry_t *ce)
123 {
124 int i;
126 for (i = 0; i < ds->ds_num; i++)
127 {
128 if (isnan (ce->values_gauge[i]))
129 continue;
130 else if (ce->values_gauge[i] < ds->ds[i].min)
131 ce->values_gauge[i] = NAN;
132 else if (ce->values_gauge[i] > ds->ds[i].max)
133 ce->values_gauge[i] = NAN;
134 }
135 } /* void uc_check_range */
137 static int uc_insert (const data_set_t *ds, const value_list_t *vl,
138 const char *key)
139 {
140 int i;
141 char *key_copy;
142 cache_entry_t *ce;
144 /* `cache_lock' has been locked by `uc_update' */
146 key_copy = strdup (key);
147 if (key_copy == NULL)
148 {
149 ERROR ("uc_insert: strdup failed.");
150 return (-1);
151 }
153 ce = cache_alloc (ds->ds_num);
154 if (ce == NULL)
155 {
156 sfree (key_copy);
157 ERROR ("uc_insert: cache_alloc (%i) failed.", ds->ds_num);
158 return (-1);
159 }
161 sstrncpy (ce->name, key, sizeof (ce->name));
163 for (i = 0; i < ds->ds_num; i++)
164 {
165 switch (ds->ds[i].type)
166 {
167 case DS_TYPE_COUNTER:
168 ce->values_gauge[i] = NAN;
169 ce->values_raw[i].counter = vl->values[i].counter;
170 break;
172 case DS_TYPE_GAUGE:
173 ce->values_gauge[i] = vl->values[i].gauge;
174 ce->values_raw[i].gauge = vl->values[i].gauge;
175 break;
177 case DS_TYPE_DERIVE:
178 ce->values_gauge[i] = NAN;
179 ce->values_raw[i].derive = vl->values[i].derive;
180 break;
182 case DS_TYPE_ABSOLUTE:
183 ce->values_gauge[i] = NAN;
184 if (vl->interval > 0)
185 ce->values_gauge[i] = ((double) vl->values[i].absolute)
186 / CDTIME_T_TO_DOUBLE (vl->interval);
187 ce->values_raw[i].absolute = vl->values[i].absolute;
188 break;
190 default:
191 /* This shouldn't happen. */
192 ERROR ("uc_insert: Don't know how to handle data source type %i.",
193 ds->ds[i].type);
194 sfree (key_copy);
195 cache_free (ce);
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 {
302 /* realloc() may have been called for these. */
303 sfree (keys);
304 sfree (keys_time);
305 sfree (keys_interval);
306 return (0);
307 }
309 /* Call the "missing" callback for each value. Do this before removing the
310 * value from the cache, so that callbacks can still access the data stored,
311 * including plugin specific meta data, rates, history, …. This must be done
312 * without holding the lock, otherwise we will run into a deadlock if a
313 * plugin calls the cache interface. */
314 for (i = 0; i < keys_len; i++)
315 {
316 value_list_t vl = VALUE_LIST_INIT;
318 vl.values = NULL;
319 vl.values_len = 0;
320 vl.meta = NULL;
322 status = parse_identifier_vl (keys[i], &vl);
323 if (status != 0)
324 {
325 ERROR ("uc_check_timeout: parse_identifier_vl (\"%s\") failed.", keys[i]);
326 continue;
327 }
329 vl.time = keys_time[i];
330 vl.interval = keys_interval[i];
332 plugin_dispatch_missing (&vl);
333 } /* for (i = 0; i < keys_len; i++) */
335 /* Now actually remove all the values from the cache. We don't re-evaluate
336 * the timestamp again, so in theory it is possible we remove a value after
337 * it is updated here. */
338 pthread_mutex_lock (&cache_lock);
339 for (i = 0; i < keys_len; i++)
340 {
341 key = NULL;
342 ce = NULL;
344 status = c_avl_remove (cache_tree, keys[i],
345 (void *) &key, (void *) &ce);
346 if (status != 0)
347 {
348 ERROR ("uc_check_timeout: c_avl_remove (\"%s\") failed.", keys[i]);
349 sfree (keys[i]);
350 continue;
351 }
353 sfree (keys[i]);
354 sfree (key);
355 cache_free (ce);
356 } /* for (i = 0; i < keys_len; i++) */
357 pthread_mutex_unlock (&cache_lock);
359 sfree (keys);
360 sfree (keys_time);
361 sfree (keys_interval);
363 return (0);
364 } /* int uc_check_timeout */
366 int uc_update (const data_set_t *ds, const value_list_t *vl)
367 {
368 char name[6 * DATA_MAX_NAME_LEN];
369 cache_entry_t *ce = NULL;
370 int status;
371 int i;
373 if (FORMAT_VL (name, sizeof (name), vl) != 0)
374 {
375 ERROR ("uc_update: FORMAT_VL failed.");
376 return (-1);
377 }
379 pthread_mutex_lock (&cache_lock);
381 status = c_avl_get (cache_tree, name, (void *) &ce);
382 if (status != 0) /* entry does not yet exist */
383 {
384 status = uc_insert (ds, vl, name);
385 pthread_mutex_unlock (&cache_lock);
386 return (status);
387 }
389 assert (ce != NULL);
390 assert (ce->values_num == ds->ds_num);
392 if (ce->last_time >= vl->time)
393 {
394 pthread_mutex_unlock (&cache_lock);
395 NOTICE ("uc_update: Value too old: name = %s; value time = %.3f; "
396 "last cache update = %.3f;",
397 name,
398 CDTIME_T_TO_DOUBLE (vl->time),
399 CDTIME_T_TO_DOUBLE (ce->last_time));
400 return (-1);
401 }
403 for (i = 0; i < ds->ds_num; i++)
404 {
405 switch (ds->ds[i].type)
406 {
407 case DS_TYPE_COUNTER:
408 {
409 counter_t diff;
411 /* check if the counter has wrapped around */
412 if (vl->values[i].counter < ce->values_raw[i].counter)
413 {
414 if (ce->values_raw[i].counter <= 4294967295U)
415 diff = (4294967295U - ce->values_raw[i].counter)
416 + vl->values[i].counter;
417 else
418 diff = (18446744073709551615ULL - ce->values_raw[i].counter)
419 + vl->values[i].counter;
420 }
421 else /* counter has NOT wrapped around */
422 {
423 diff = vl->values[i].counter - ce->values_raw[i].counter;
424 }
426 ce->values_gauge[i] = ((double) diff)
427 / (CDTIME_T_TO_DOUBLE (vl->time - ce->last_time));
428 ce->values_raw[i].counter = vl->values[i].counter;
429 }
430 break;
432 case DS_TYPE_GAUGE:
433 ce->values_raw[i].gauge = vl->values[i].gauge;
434 ce->values_gauge[i] = vl->values[i].gauge;
435 break;
437 case DS_TYPE_DERIVE:
438 {
439 derive_t diff;
441 diff = vl->values[i].derive - ce->values_raw[i].derive;
443 ce->values_gauge[i] = ((double) diff)
444 / (CDTIME_T_TO_DOUBLE (vl->time - ce->last_time));
445 ce->values_raw[i].derive = vl->values[i].derive;
446 }
447 break;
449 case DS_TYPE_ABSOLUTE:
450 ce->values_gauge[i] = ((double) vl->values[i].absolute)
451 / (CDTIME_T_TO_DOUBLE (vl->time - ce->last_time));
452 ce->values_raw[i].absolute = vl->values[i].absolute;
453 break;
455 default:
456 /* This shouldn't happen. */
457 pthread_mutex_unlock (&cache_lock);
458 ERROR ("uc_update: Don't know how to handle data source type %i.",
459 ds->ds[i].type);
460 return (-1);
461 } /* switch (ds->ds[i].type) */
463 DEBUG ("uc_update: %s: ds[%i] = %lf", name, i, ce->values_gauge[i]);
464 } /* for (i) */
466 /* Update the history if it exists. */
467 if (ce->history != NULL)
468 {
469 assert (ce->history_index < ce->history_length);
470 for (i = 0; i < ce->values_num; i++)
471 {
472 size_t hist_idx = (ce->values_num * ce->history_index) + i;
473 ce->history[hist_idx] = ce->values_gauge[i];
474 }
476 assert (ce->history_length > 0);
477 ce->history_index = (ce->history_index + 1) % ce->history_length;
478 }
480 /* Prune invalid gauge data */
481 uc_check_range (ds, ce);
483 ce->last_time = vl->time;
484 ce->last_update = cdtime ();
485 ce->interval = vl->interval;
487 pthread_mutex_unlock (&cache_lock);
489 return (0);
490 } /* int uc_update */
492 int uc_get_rate_by_name (const char *name, gauge_t **ret_values, size_t *ret_values_num)
493 {
494 gauge_t *ret = NULL;
495 size_t ret_num = 0;
496 cache_entry_t *ce = NULL;
497 int status = 0;
499 pthread_mutex_lock (&cache_lock);
501 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
502 {
503 assert (ce != NULL);
505 /* remove missing values from getval */
506 if (ce->state == STATE_MISSING)
507 {
508 status = -1;
509 }
510 else
511 {
512 ret_num = ce->values_num;
513 ret = (gauge_t *) malloc (ret_num * sizeof (gauge_t));
514 if (ret == NULL)
515 {
516 ERROR ("utils_cache: uc_get_rate_by_name: malloc failed.");
517 status = -1;
518 }
519 else
520 {
521 memcpy (ret, ce->values_gauge, ret_num * sizeof (gauge_t));
522 }
523 }
524 }
525 else
526 {
527 DEBUG ("utils_cache: uc_get_rate_by_name: No such value: %s", name);
528 status = -1;
529 }
531 pthread_mutex_unlock (&cache_lock);
533 if (status == 0)
534 {
535 *ret_values = ret;
536 *ret_values_num = ret_num;
537 }
539 return (status);
540 } /* gauge_t *uc_get_rate_by_name */
542 gauge_t *uc_get_rate (const data_set_t *ds, const value_list_t *vl)
543 {
544 char name[6 * DATA_MAX_NAME_LEN];
545 gauge_t *ret = NULL;
546 size_t ret_num = 0;
547 int status;
549 if (FORMAT_VL (name, sizeof (name), vl) != 0)
550 {
551 ERROR ("utils_cache: uc_get_rate: FORMAT_VL failed.");
552 return (NULL);
553 }
555 status = uc_get_rate_by_name (name, &ret, &ret_num);
556 if (status != 0)
557 return (NULL);
559 /* This is important - the caller has no other way of knowing how many
560 * values are returned. */
561 if (ret_num != (size_t) ds->ds_num)
562 {
563 ERROR ("utils_cache: uc_get_rate: ds[%s] has %i values, "
564 "but uc_get_rate_by_name returned %zu.",
565 ds->type, ds->ds_num, ret_num);
566 sfree (ret);
567 return (NULL);
568 }
570 return (ret);
571 } /* gauge_t *uc_get_rate */
573 int uc_get_names (char ***ret_names, cdtime_t **ret_times, size_t *ret_number)
574 {
575 c_avl_iterator_t *iter;
576 char *key;
577 cache_entry_t *value;
579 char **names = NULL;
580 cdtime_t *times = NULL;
581 size_t number = 0;
582 size_t size_arrays = 0;
584 int status = 0;
586 if ((ret_names == NULL) || (ret_number == NULL))
587 return (-1);
589 pthread_mutex_lock (&cache_lock);
591 size_arrays = (size_t) c_avl_size (cache_tree);
592 if (size_arrays < 1)
593 {
594 /* Handle the "no values" case here, to avoid the error message when
595 * calloc() returns NULL. */
596 pthread_mutex_unlock (&cache_lock);
597 return (0);
598 }
600 names = calloc (size_arrays, sizeof (*names));
601 times = calloc (size_arrays, sizeof (*times));
602 if ((names == NULL) || (times == NULL))
603 {
604 ERROR ("uc_get_names: calloc failed.");
605 sfree (names);
606 sfree (times);
607 pthread_mutex_unlock (&cache_lock);
608 return (ENOMEM);
609 }
611 iter = c_avl_get_iterator (cache_tree);
612 while (c_avl_iterator_next (iter, (void *) &key, (void *) &value) == 0)
613 {
614 /* remove missing values when list values */
615 if (value->state == STATE_MISSING)
616 continue;
618 /* c_avl_size does not return a number smaller than the number of elements
619 * returned by c_avl_iterator_next. */
620 assert (number < size_arrays);
622 if (ret_times != NULL)
623 times[number] = value->last_time;
625 names[number] = strdup (key);
626 if (names[number] == NULL)
627 {
628 status = -1;
629 break;
630 }
632 number++;
633 } /* while (c_avl_iterator_next) */
635 c_avl_iterator_destroy (iter);
636 pthread_mutex_unlock (&cache_lock);
638 if (status != 0)
639 {
640 size_t i;
642 for (i = 0; i < number; i++)
643 {
644 sfree (names[i]);
645 }
646 sfree (names);
647 sfree (times);
649 return (-1);
650 }
652 *ret_names = names;
653 if (ret_times != NULL)
654 *ret_times = times;
655 else
656 sfree (times);
657 *ret_number = number;
659 return (0);
660 } /* int uc_get_names */
662 int uc_get_state (const data_set_t *ds, const value_list_t *vl)
663 {
664 char name[6 * DATA_MAX_NAME_LEN];
665 cache_entry_t *ce = NULL;
666 int ret = STATE_ERROR;
668 if (FORMAT_VL (name, sizeof (name), vl) != 0)
669 {
670 ERROR ("uc_get_state: FORMAT_VL failed.");
671 return (STATE_ERROR);
672 }
674 pthread_mutex_lock (&cache_lock);
676 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
677 {
678 assert (ce != NULL);
679 ret = ce->state;
680 }
682 pthread_mutex_unlock (&cache_lock);
684 return (ret);
685 } /* int uc_get_state */
687 int uc_set_state (const data_set_t *ds, const value_list_t *vl, int state)
688 {
689 char name[6 * DATA_MAX_NAME_LEN];
690 cache_entry_t *ce = NULL;
691 int ret = -1;
693 if (FORMAT_VL (name, sizeof (name), vl) != 0)
694 {
695 ERROR ("uc_get_state: FORMAT_VL failed.");
696 return (STATE_ERROR);
697 }
699 pthread_mutex_lock (&cache_lock);
701 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
702 {
703 assert (ce != NULL);
704 ret = ce->state;
705 ce->state = state;
706 }
708 pthread_mutex_unlock (&cache_lock);
710 return (ret);
711 } /* int uc_set_state */
713 int uc_get_history_by_name (const char *name,
714 gauge_t *ret_history, size_t num_steps, size_t num_ds)
715 {
716 cache_entry_t *ce = NULL;
717 size_t i;
718 int status = 0;
720 pthread_mutex_lock (&cache_lock);
722 status = c_avl_get (cache_tree, name, (void *) &ce);
723 if (status != 0)
724 {
725 pthread_mutex_unlock (&cache_lock);
726 return (-ENOENT);
727 }
729 if (((size_t) ce->values_num) != num_ds)
730 {
731 pthread_mutex_unlock (&cache_lock);
732 return (-EINVAL);
733 }
735 /* Check if there are enough values available. If not, increase the buffer
736 * size. */
737 if (ce->history_length < num_steps)
738 {
739 gauge_t *tmp;
740 size_t i;
742 tmp = realloc (ce->history, sizeof (*ce->history)
743 * num_steps * ce->values_num);
744 if (tmp == NULL)
745 {
746 pthread_mutex_unlock (&cache_lock);
747 return (-ENOMEM);
748 }
750 for (i = ce->history_length * ce->values_num;
751 i < (num_steps * ce->values_num);
752 i++)
753 tmp[i] = NAN;
755 ce->history = tmp;
756 ce->history_length = num_steps;
757 } /* if (ce->history_length < num_steps) */
759 /* Copy the values to the output buffer. */
760 for (i = 0; i < num_steps; i++)
761 {
762 size_t src_index;
763 size_t dst_index;
765 if (i < ce->history_index)
766 src_index = ce->history_index - (i + 1);
767 else
768 src_index = ce->history_length + ce->history_index - (i + 1);
769 src_index = src_index * num_ds;
771 dst_index = i * num_ds;
773 memcpy (ret_history + dst_index, ce->history + src_index,
774 sizeof (*ret_history) * num_ds);
775 }
777 pthread_mutex_unlock (&cache_lock);
779 return (0);
780 } /* int uc_get_history_by_name */
782 int uc_get_history (const data_set_t *ds, const value_list_t *vl,
783 gauge_t *ret_history, size_t num_steps, size_t num_ds)
784 {
785 char name[6 * DATA_MAX_NAME_LEN];
787 if (FORMAT_VL (name, sizeof (name), vl) != 0)
788 {
789 ERROR ("utils_cache: uc_get_history: FORMAT_VL failed.");
790 return (-1);
791 }
793 return (uc_get_history_by_name (name, ret_history, num_steps, num_ds));
794 } /* int uc_get_history */
796 int uc_get_hits (const data_set_t *ds, const value_list_t *vl)
797 {
798 char name[6 * DATA_MAX_NAME_LEN];
799 cache_entry_t *ce = NULL;
800 int ret = STATE_ERROR;
802 if (FORMAT_VL (name, sizeof (name), vl) != 0)
803 {
804 ERROR ("uc_get_state: FORMAT_VL failed.");
805 return (STATE_ERROR);
806 }
808 pthread_mutex_lock (&cache_lock);
810 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
811 {
812 assert (ce != NULL);
813 ret = ce->hits;
814 }
816 pthread_mutex_unlock (&cache_lock);
818 return (ret);
819 } /* int uc_get_hits */
821 int uc_set_hits (const data_set_t *ds, const value_list_t *vl, int hits)
822 {
823 char name[6 * DATA_MAX_NAME_LEN];
824 cache_entry_t *ce = NULL;
825 int ret = -1;
827 if (FORMAT_VL (name, sizeof (name), vl) != 0)
828 {
829 ERROR ("uc_get_state: FORMAT_VL failed.");
830 return (STATE_ERROR);
831 }
833 pthread_mutex_lock (&cache_lock);
835 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
836 {
837 assert (ce != NULL);
838 ret = ce->hits;
839 ce->hits = hits;
840 }
842 pthread_mutex_unlock (&cache_lock);
844 return (ret);
845 } /* int uc_set_hits */
847 int uc_inc_hits (const data_set_t *ds, const value_list_t *vl, int step)
848 {
849 char name[6 * DATA_MAX_NAME_LEN];
850 cache_entry_t *ce = NULL;
851 int ret = -1;
853 if (FORMAT_VL (name, sizeof (name), vl) != 0)
854 {
855 ERROR ("uc_get_state: FORMAT_VL failed.");
856 return (STATE_ERROR);
857 }
859 pthread_mutex_lock (&cache_lock);
861 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
862 {
863 assert (ce != NULL);
864 ret = ce->hits;
865 ce->hits = ret + step;
866 }
868 pthread_mutex_unlock (&cache_lock);
870 return (ret);
871 } /* int uc_inc_hits */
873 /*
874 * Meta data interface
875 */
876 /* XXX: This function will acquire `cache_lock' but will not free it! */
877 static meta_data_t *uc_get_meta (const value_list_t *vl) /* {{{ */
878 {
879 char name[6 * DATA_MAX_NAME_LEN];
880 cache_entry_t *ce = NULL;
881 int status;
883 status = FORMAT_VL (name, sizeof (name), vl);
884 if (status != 0)
885 {
886 ERROR ("utils_cache: uc_get_meta: FORMAT_VL failed.");
887 return (NULL);
888 }
890 pthread_mutex_lock (&cache_lock);
892 status = c_avl_get (cache_tree, name, (void *) &ce);
893 if (status != 0)
894 {
895 pthread_mutex_unlock (&cache_lock);
896 return (NULL);
897 }
898 assert (ce != NULL);
900 if (ce->meta == NULL)
901 ce->meta = meta_data_create ();
903 if (ce->meta == NULL)
904 pthread_mutex_unlock (&cache_lock);
906 return (ce->meta);
907 } /* }}} meta_data_t *uc_get_meta */
909 /* Sorry about this preprocessor magic, but it really makes this file much
910 * shorter.. */
911 #define UC_WRAP(wrap_function) { \
912 meta_data_t *meta; \
913 int status; \
914 meta = uc_get_meta (vl); \
915 if (meta == NULL) return (-1); \
916 status = wrap_function (meta, key); \
917 pthread_mutex_unlock (&cache_lock); \
918 return (status); \
919 }
920 int uc_meta_data_exists (const value_list_t *vl, const char *key)
921 UC_WRAP (meta_data_exists)
923 int uc_meta_data_delete (const value_list_t *vl, const char *key)
924 UC_WRAP (meta_data_delete)
925 #undef UC_WRAP
927 /* We need a new version of this macro because the following functions take
928 * two argumetns. */
929 #define UC_WRAP(wrap_function) { \
930 meta_data_t *meta; \
931 int status; \
932 meta = uc_get_meta (vl); \
933 if (meta == NULL) return (-1); \
934 status = wrap_function (meta, key, value); \
935 pthread_mutex_unlock (&cache_lock); \
936 return (status); \
937 }
938 int uc_meta_data_add_string (const value_list_t *vl,
939 const char *key,
940 const char *value)
941 UC_WRAP(meta_data_add_string)
942 int uc_meta_data_add_signed_int (const value_list_t *vl,
943 const char *key,
944 int64_t value)
945 UC_WRAP(meta_data_add_signed_int)
946 int uc_meta_data_add_unsigned_int (const value_list_t *vl,
947 const char *key,
948 uint64_t value)
949 UC_WRAP(meta_data_add_unsigned_int)
950 int uc_meta_data_add_double (const value_list_t *vl,
951 const char *key,
952 double value)
953 UC_WRAP(meta_data_add_double)
954 int uc_meta_data_add_boolean (const value_list_t *vl,
955 const char *key,
956 _Bool value)
957 UC_WRAP(meta_data_add_boolean)
959 int uc_meta_data_get_string (const value_list_t *vl,
960 const char *key,
961 char **value)
962 UC_WRAP(meta_data_get_string)
963 int uc_meta_data_get_signed_int (const value_list_t *vl,
964 const char *key,
965 int64_t *value)
966 UC_WRAP(meta_data_get_signed_int)
967 int uc_meta_data_get_unsigned_int (const value_list_t *vl,
968 const char *key,
969 uint64_t *value)
970 UC_WRAP(meta_data_get_unsigned_int)
971 int uc_meta_data_get_double (const value_list_t *vl,
972 const char *key,
973 double *value)
974 UC_WRAP(meta_data_get_double)
975 int uc_meta_data_get_boolean (const value_list_t *vl,
976 const char *key,
977 _Bool *value)
978 UC_WRAP(meta_data_get_boolean)
979 #undef UC_WRAP
981 /* vim: set sw=2 ts=8 sts=2 tw=78 : */