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 return (-1);
195 } /* switch (ds->ds[i].type) */
196 } /* for (i) */
198 /* Prune invalid gauge data */
199 uc_check_range (ds, ce);
201 ce->last_time = vl->time;
202 ce->last_update = cdtime ();
203 ce->interval = vl->interval;
204 ce->state = STATE_OKAY;
206 if (c_avl_insert (cache_tree, key_copy, ce) != 0)
207 {
208 sfree (key_copy);
209 ERROR ("uc_insert: c_avl_insert failed.");
210 return (-1);
211 }
213 DEBUG ("uc_insert: Added %s to the cache.", key);
214 return (0);
215 } /* int uc_insert */
217 int uc_init (void)
218 {
219 if (cache_tree == NULL)
220 cache_tree = c_avl_create ((int (*) (const void *, const void *))
221 cache_compare);
223 return (0);
224 } /* int uc_init */
226 int uc_check_timeout (void)
227 {
228 cdtime_t now;
229 cache_entry_t *ce;
231 char **keys = NULL;
232 cdtime_t *keys_time = NULL;
233 cdtime_t *keys_interval = NULL;
234 int keys_len = 0;
236 char *key;
237 c_avl_iterator_t *iter;
239 int status;
240 int i;
242 pthread_mutex_lock (&cache_lock);
244 now = cdtime ();
246 /* Build a list of entries to be flushed */
247 iter = c_avl_get_iterator (cache_tree);
248 while (c_avl_iterator_next (iter, (void *) &key, (void *) &ce) == 0)
249 {
250 char **tmp;
251 cdtime_t *tmp_time;
253 /* If the entry is fresh enough, continue. */
254 if ((now - ce->last_update) < (ce->interval * timeout_g))
255 continue;
257 /* If entry has not been updated, add to `keys' array */
258 tmp = (char **) realloc ((void *) keys,
259 (keys_len + 1) * sizeof (char *));
260 if (tmp == NULL)
261 {
262 ERROR ("uc_check_timeout: realloc failed.");
263 continue;
264 }
265 keys = tmp;
267 tmp_time = realloc (keys_time, (keys_len + 1) * sizeof (*keys_time));
268 if (tmp_time == NULL)
269 {
270 ERROR ("uc_check_timeout: realloc failed.");
271 continue;
272 }
273 keys_time = tmp_time;
275 tmp_time = realloc (keys_interval, (keys_len + 1) * sizeof (*keys_interval));
276 if (tmp_time == NULL)
277 {
278 ERROR ("uc_check_timeout: realloc failed.");
279 continue;
280 }
281 keys_interval = tmp_time;
283 keys[keys_len] = strdup (key);
284 if (keys[keys_len] == NULL)
285 {
286 ERROR ("uc_check_timeout: strdup failed.");
287 continue;
288 }
289 keys_time[keys_len] = ce->last_time;
290 keys_interval[keys_len] = ce->interval;
292 keys_len++;
293 } /* while (c_avl_iterator_next) */
295 c_avl_iterator_destroy (iter);
296 pthread_mutex_unlock (&cache_lock);
298 if (keys_len == 0)
299 return (0);
301 /* Call the "missing" callback for each value. Do this before removing the
302 * value from the cache, so that callbacks can still access the data stored,
303 * including plugin specific meta data, rates, history, …. This must be done
304 * without holding the lock, otherwise we will run into a deadlock if a
305 * plugin calls the cache interface. */
306 for (i = 0; i < keys_len; i++)
307 {
308 value_list_t vl = VALUE_LIST_INIT;
310 vl.values = NULL;
311 vl.values_len = 0;
312 vl.meta = NULL;
314 status = parse_identifier_vl (keys[i], &vl);
315 if (status != 0)
316 {
317 ERROR ("uc_check_timeout: parse_identifier_vl (\"%s\") failed.", keys[i]);
318 cache_free (ce);
319 continue;
320 }
322 vl.time = keys_time[i];
323 vl.interval = keys_interval[i];
325 plugin_dispatch_missing (&vl);
326 } /* for (i = 0; i < keys_len; i++) */
328 /* Now actually remove all the values from the cache. We don't re-evaluate
329 * the timestamp again, so in theory it is possible we remove a value after
330 * it is updated here. */
331 pthread_mutex_lock (&cache_lock);
332 for (i = 0; i < keys_len; i++)
333 {
334 key = NULL;
335 ce = NULL;
337 status = c_avl_remove (cache_tree, keys[i],
338 (void *) &key, (void *) &ce);
339 if (status != 0)
340 {
341 ERROR ("uc_check_timeout: c_avl_remove (\"%s\") failed.", keys[i]);
342 sfree (keys[i]);
343 continue;
344 }
346 sfree (keys[i]);
347 sfree (key);
348 cache_free (ce);
349 } /* for (i = 0; i < keys_len; i++) */
350 pthread_mutex_unlock (&cache_lock);
352 sfree (keys);
353 sfree (keys_time);
354 sfree (keys_interval);
356 return (0);
357 } /* int uc_check_timeout */
359 int uc_update (const data_set_t *ds, const value_list_t *vl)
360 {
361 char name[6 * DATA_MAX_NAME_LEN];
362 cache_entry_t *ce = NULL;
363 int status;
364 int i;
366 if (FORMAT_VL (name, sizeof (name), vl) != 0)
367 {
368 ERROR ("uc_update: FORMAT_VL failed.");
369 return (-1);
370 }
372 pthread_mutex_lock (&cache_lock);
374 status = c_avl_get (cache_tree, name, (void *) &ce);
375 if (status != 0) /* entry does not yet exist */
376 {
377 status = uc_insert (ds, vl, name);
378 pthread_mutex_unlock (&cache_lock);
379 return (status);
380 }
382 assert (ce != NULL);
383 assert (ce->values_num == ds->ds_num);
385 if (ce->last_time >= vl->time)
386 {
387 pthread_mutex_unlock (&cache_lock);
388 NOTICE ("uc_update: Value too old: name = %s; value time = %.3f; "
389 "last cache update = %.3f;",
390 name,
391 CDTIME_T_TO_DOUBLE (vl->time),
392 CDTIME_T_TO_DOUBLE (ce->last_time));
393 return (-1);
394 }
396 for (i = 0; i < ds->ds_num; i++)
397 {
398 switch (ds->ds[i].type)
399 {
400 case DS_TYPE_COUNTER:
401 {
402 counter_t diff;
404 /* check if the counter has wrapped around */
405 if (vl->values[i].counter < ce->values_raw[i].counter)
406 {
407 if (ce->values_raw[i].counter <= 4294967295U)
408 diff = (4294967295U - ce->values_raw[i].counter)
409 + vl->values[i].counter;
410 else
411 diff = (18446744073709551615ULL - ce->values_raw[i].counter)
412 + vl->values[i].counter;
413 }
414 else /* counter has NOT wrapped around */
415 {
416 diff = vl->values[i].counter - ce->values_raw[i].counter;
417 }
419 ce->values_gauge[i] = ((double) diff)
420 / (CDTIME_T_TO_DOUBLE (vl->time - ce->last_time));
421 ce->values_raw[i].counter = vl->values[i].counter;
422 }
423 break;
425 case DS_TYPE_GAUGE:
426 ce->values_raw[i].gauge = vl->values[i].gauge;
427 ce->values_gauge[i] = vl->values[i].gauge;
428 break;
430 case DS_TYPE_DERIVE:
431 {
432 derive_t diff;
434 diff = vl->values[i].derive - ce->values_raw[i].derive;
436 ce->values_gauge[i] = ((double) diff)
437 / (CDTIME_T_TO_DOUBLE (vl->time - ce->last_time));
438 ce->values_raw[i].derive = vl->values[i].derive;
439 }
440 break;
442 case DS_TYPE_ABSOLUTE:
443 ce->values_gauge[i] = ((double) vl->values[i].absolute)
444 / (CDTIME_T_TO_DOUBLE (vl->time - ce->last_time));
445 ce->values_raw[i].absolute = vl->values[i].absolute;
446 break;
448 default:
449 /* This shouldn't happen. */
450 pthread_mutex_unlock (&cache_lock);
451 ERROR ("uc_update: Don't know how to handle data source type %i.",
452 ds->ds[i].type);
453 return (-1);
454 } /* switch (ds->ds[i].type) */
456 DEBUG ("uc_update: %s: ds[%i] = %lf", name, i, ce->values_gauge[i]);
457 } /* for (i) */
459 /* Update the history if it exists. */
460 if (ce->history != NULL)
461 {
462 assert (ce->history_index < ce->history_length);
463 for (i = 0; i < ce->values_num; i++)
464 {
465 size_t hist_idx = (ce->values_num * ce->history_index) + i;
466 ce->history[hist_idx] = ce->values_gauge[i];
467 }
469 assert (ce->history_length > 0);
470 ce->history_index = (ce->history_index + 1) % ce->history_length;
471 }
473 /* Prune invalid gauge data */
474 uc_check_range (ds, ce);
476 ce->last_time = vl->time;
477 ce->last_update = cdtime ();
478 ce->interval = vl->interval;
480 pthread_mutex_unlock (&cache_lock);
482 return (0);
483 } /* int uc_update */
485 int uc_get_rate_by_name (const char *name, gauge_t **ret_values, size_t *ret_values_num)
486 {
487 gauge_t *ret = NULL;
488 size_t ret_num = 0;
489 cache_entry_t *ce = NULL;
490 int status = 0;
492 pthread_mutex_lock (&cache_lock);
494 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
495 {
496 assert (ce != NULL);
498 /* remove missing values from getval */
499 if (ce->state == STATE_MISSING)
500 {
501 status = -1;
502 }
503 else
504 {
505 ret_num = ce->values_num;
506 ret = (gauge_t *) malloc (ret_num * sizeof (gauge_t));
507 if (ret == NULL)
508 {
509 ERROR ("utils_cache: uc_get_rate_by_name: malloc failed.");
510 status = -1;
511 }
512 else
513 {
514 memcpy (ret, ce->values_gauge, ret_num * sizeof (gauge_t));
515 }
516 }
517 }
518 else
519 {
520 DEBUG ("utils_cache: uc_get_rate_by_name: No such value: %s", name);
521 status = -1;
522 }
524 pthread_mutex_unlock (&cache_lock);
526 if (status == 0)
527 {
528 *ret_values = ret;
529 *ret_values_num = ret_num;
530 }
532 return (status);
533 } /* gauge_t *uc_get_rate_by_name */
535 gauge_t *uc_get_rate (const data_set_t *ds, const value_list_t *vl)
536 {
537 char name[6 * DATA_MAX_NAME_LEN];
538 gauge_t *ret = NULL;
539 size_t ret_num = 0;
540 int status;
542 if (FORMAT_VL (name, sizeof (name), vl) != 0)
543 {
544 ERROR ("utils_cache: uc_get_rate: FORMAT_VL failed.");
545 return (NULL);
546 }
548 status = uc_get_rate_by_name (name, &ret, &ret_num);
549 if (status != 0)
550 return (NULL);
552 /* This is important - the caller has no other way of knowing how many
553 * values are returned. */
554 if (ret_num != (size_t) ds->ds_num)
555 {
556 ERROR ("utils_cache: uc_get_rate: ds[%s] has %i values, "
557 "but uc_get_rate_by_name returned %zu.",
558 ds->type, ds->ds_num, ret_num);
559 sfree (ret);
560 return (NULL);
561 }
563 return (ret);
564 } /* gauge_t *uc_get_rate */
566 int uc_get_names (char ***ret_names, cdtime_t **ret_times, size_t *ret_number)
567 {
568 c_avl_iterator_t *iter;
569 char *key;
570 cache_entry_t *value;
572 char **names = NULL;
573 cdtime_t *times = NULL;
574 size_t number = 0;
576 int status = 0;
578 if ((ret_names == NULL) || (ret_number == NULL))
579 return (-1);
581 pthread_mutex_lock (&cache_lock);
583 iter = c_avl_get_iterator (cache_tree);
584 while (c_avl_iterator_next (iter, (void *) &key, (void *) &value) == 0)
585 {
586 char **temp;
588 /* remove missing values when list values */
589 if (value->state == STATE_MISSING)
590 continue;
592 if (ret_times != NULL)
593 {
594 cdtime_t *tmp_times;
596 tmp_times = (cdtime_t *) realloc (times, sizeof (cdtime_t) * (number + 1));
597 if (tmp_times == NULL)
598 {
599 status = -1;
600 break;
601 }
602 times = tmp_times;
603 times[number] = value->last_time;
604 }
606 temp = (char **) realloc (names, sizeof (char *) * (number + 1));
607 if (temp == NULL)
608 {
609 status = -1;
610 break;
611 }
612 names = temp;
613 names[number] = strdup (key);
614 if (names[number] == NULL)
615 {
616 status = -1;
617 break;
618 }
619 number++;
620 } /* while (c_avl_iterator_next) */
622 c_avl_iterator_destroy (iter);
623 pthread_mutex_unlock (&cache_lock);
625 if (status != 0)
626 {
627 size_t i;
629 for (i = 0; i < number; i++)
630 {
631 sfree (names[i]);
632 }
633 sfree (names);
635 return (-1);
636 }
638 *ret_names = names;
639 if (ret_times != NULL)
640 *ret_times = times;
641 *ret_number = number;
643 return (0);
644 } /* int uc_get_names */
646 int uc_get_state (const data_set_t *ds, const value_list_t *vl)
647 {
648 char name[6 * DATA_MAX_NAME_LEN];
649 cache_entry_t *ce = NULL;
650 int ret = STATE_ERROR;
652 if (FORMAT_VL (name, sizeof (name), vl) != 0)
653 {
654 ERROR ("uc_get_state: FORMAT_VL failed.");
655 return (STATE_ERROR);
656 }
658 pthread_mutex_lock (&cache_lock);
660 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
661 {
662 assert (ce != NULL);
663 ret = ce->state;
664 }
666 pthread_mutex_unlock (&cache_lock);
668 return (ret);
669 } /* int uc_get_state */
671 int uc_set_state (const data_set_t *ds, const value_list_t *vl, int state)
672 {
673 char name[6 * DATA_MAX_NAME_LEN];
674 cache_entry_t *ce = NULL;
675 int ret = -1;
677 if (FORMAT_VL (name, sizeof (name), vl) != 0)
678 {
679 ERROR ("uc_get_state: FORMAT_VL failed.");
680 return (STATE_ERROR);
681 }
683 pthread_mutex_lock (&cache_lock);
685 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
686 {
687 assert (ce != NULL);
688 ret = ce->state;
689 ce->state = state;
690 }
692 pthread_mutex_unlock (&cache_lock);
694 return (ret);
695 } /* int uc_set_state */
697 int uc_get_history_by_name (const char *name,
698 gauge_t *ret_history, size_t num_steps, size_t num_ds)
699 {
700 cache_entry_t *ce = NULL;
701 size_t i;
702 int status = 0;
704 pthread_mutex_lock (&cache_lock);
706 status = c_avl_get (cache_tree, name, (void *) &ce);
707 if (status != 0)
708 {
709 pthread_mutex_unlock (&cache_lock);
710 return (-ENOENT);
711 }
713 if (((size_t) ce->values_num) != num_ds)
714 {
715 pthread_mutex_unlock (&cache_lock);
716 return (-EINVAL);
717 }
719 /* Check if there are enough values available. If not, increase the buffer
720 * size. */
721 if (ce->history_length < num_steps)
722 {
723 gauge_t *tmp;
724 size_t i;
726 tmp = realloc (ce->history, sizeof (*ce->history)
727 * num_steps * ce->values_num);
728 if (tmp == NULL)
729 {
730 pthread_mutex_unlock (&cache_lock);
731 return (-ENOMEM);
732 }
734 for (i = ce->history_length * ce->values_num;
735 i < (num_steps * ce->values_num);
736 i++)
737 tmp[i] = NAN;
739 ce->history = tmp;
740 ce->history_length = num_steps;
741 } /* if (ce->history_length < num_steps) */
743 /* Copy the values to the output buffer. */
744 for (i = 0; i < num_steps; i++)
745 {
746 size_t src_index;
747 size_t dst_index;
749 if (i < ce->history_index)
750 src_index = ce->history_index - (i + 1);
751 else
752 src_index = ce->history_length + ce->history_index - (i + 1);
753 src_index = src_index * num_ds;
755 dst_index = i * num_ds;
757 memcpy (ret_history + dst_index, ce->history + src_index,
758 sizeof (*ret_history) * num_ds);
759 }
761 pthread_mutex_unlock (&cache_lock);
763 return (0);
764 } /* int uc_get_history_by_name */
766 int uc_get_history (const data_set_t *ds, const value_list_t *vl,
767 gauge_t *ret_history, size_t num_steps, size_t num_ds)
768 {
769 char name[6 * DATA_MAX_NAME_LEN];
771 if (FORMAT_VL (name, sizeof (name), vl) != 0)
772 {
773 ERROR ("utils_cache: uc_get_history: FORMAT_VL failed.");
774 return (-1);
775 }
777 return (uc_get_history_by_name (name, ret_history, num_steps, num_ds));
778 } /* int uc_get_history */
780 int uc_get_hits (const data_set_t *ds, const value_list_t *vl)
781 {
782 char name[6 * DATA_MAX_NAME_LEN];
783 cache_entry_t *ce = NULL;
784 int ret = STATE_ERROR;
786 if (FORMAT_VL (name, sizeof (name), vl) != 0)
787 {
788 ERROR ("uc_get_state: FORMAT_VL failed.");
789 return (STATE_ERROR);
790 }
792 pthread_mutex_lock (&cache_lock);
794 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
795 {
796 assert (ce != NULL);
797 ret = ce->hits;
798 }
800 pthread_mutex_unlock (&cache_lock);
802 return (ret);
803 } /* int uc_get_hits */
805 int uc_set_hits (const data_set_t *ds, const value_list_t *vl, int hits)
806 {
807 char name[6 * DATA_MAX_NAME_LEN];
808 cache_entry_t *ce = NULL;
809 int ret = -1;
811 if (FORMAT_VL (name, sizeof (name), vl) != 0)
812 {
813 ERROR ("uc_get_state: FORMAT_VL failed.");
814 return (STATE_ERROR);
815 }
817 pthread_mutex_lock (&cache_lock);
819 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
820 {
821 assert (ce != NULL);
822 ret = ce->hits;
823 ce->hits = hits;
824 }
826 pthread_mutex_unlock (&cache_lock);
828 return (ret);
829 } /* int uc_set_hits */
831 int uc_inc_hits (const data_set_t *ds, const value_list_t *vl, int step)
832 {
833 char name[6 * DATA_MAX_NAME_LEN];
834 cache_entry_t *ce = NULL;
835 int ret = -1;
837 if (FORMAT_VL (name, sizeof (name), vl) != 0)
838 {
839 ERROR ("uc_get_state: FORMAT_VL failed.");
840 return (STATE_ERROR);
841 }
843 pthread_mutex_lock (&cache_lock);
845 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
846 {
847 assert (ce != NULL);
848 ret = ce->hits;
849 ce->hits = ret + step;
850 }
852 pthread_mutex_unlock (&cache_lock);
854 return (ret);
855 } /* int uc_inc_hits */
857 /*
858 * Meta data interface
859 */
860 /* XXX: This function will acquire `cache_lock' but will not free it! */
861 static meta_data_t *uc_get_meta (const value_list_t *vl) /* {{{ */
862 {
863 char name[6 * DATA_MAX_NAME_LEN];
864 cache_entry_t *ce = NULL;
865 int status;
867 status = FORMAT_VL (name, sizeof (name), vl);
868 if (status != 0)
869 {
870 ERROR ("utils_cache: uc_get_meta: FORMAT_VL failed.");
871 return (NULL);
872 }
874 pthread_mutex_lock (&cache_lock);
876 status = c_avl_get (cache_tree, name, (void *) &ce);
877 if (status != 0)
878 {
879 pthread_mutex_unlock (&cache_lock);
880 return (NULL);
881 }
882 assert (ce != NULL);
884 if (ce->meta == NULL)
885 ce->meta = meta_data_create ();
887 if (ce->meta == NULL)
888 pthread_mutex_unlock (&cache_lock);
890 return (ce->meta);
891 } /* }}} meta_data_t *uc_get_meta */
893 /* Sorry about this preprocessor magic, but it really makes this file much
894 * shorter.. */
895 #define UC_WRAP(wrap_function) { \
896 meta_data_t *meta; \
897 int status; \
898 meta = uc_get_meta (vl); \
899 if (meta == NULL) return (-1); \
900 status = wrap_function (meta, key); \
901 pthread_mutex_unlock (&cache_lock); \
902 return (status); \
903 }
904 int uc_meta_data_exists (const value_list_t *vl, const char *key)
905 UC_WRAP (meta_data_exists)
907 int uc_meta_data_delete (const value_list_t *vl, const char *key)
908 UC_WRAP (meta_data_delete)
909 #undef UC_WRAP
911 /* We need a new version of this macro because the following functions take
912 * two argumetns. */
913 #define UC_WRAP(wrap_function) { \
914 meta_data_t *meta; \
915 int status; \
916 meta = uc_get_meta (vl); \
917 if (meta == NULL) return (-1); \
918 status = wrap_function (meta, key, value); \
919 pthread_mutex_unlock (&cache_lock); \
920 return (status); \
921 }
922 int uc_meta_data_add_string (const value_list_t *vl,
923 const char *key,
924 const char *value)
925 UC_WRAP(meta_data_add_string)
926 int uc_meta_data_add_signed_int (const value_list_t *vl,
927 const char *key,
928 int64_t value)
929 UC_WRAP(meta_data_add_signed_int)
930 int uc_meta_data_add_unsigned_int (const value_list_t *vl,
931 const char *key,
932 uint64_t value)
933 UC_WRAP(meta_data_add_unsigned_int)
934 int uc_meta_data_add_double (const value_list_t *vl,
935 const char *key,
936 double value)
937 UC_WRAP(meta_data_add_double)
938 int uc_meta_data_add_boolean (const value_list_t *vl,
939 const char *key,
940 _Bool value)
941 UC_WRAP(meta_data_add_boolean)
943 int uc_meta_data_get_string (const value_list_t *vl,
944 const char *key,
945 char **value)
946 UC_WRAP(meta_data_get_string)
947 int uc_meta_data_get_signed_int (const value_list_t *vl,
948 const char *key,
949 int64_t *value)
950 UC_WRAP(meta_data_get_signed_int)
951 int uc_meta_data_get_unsigned_int (const value_list_t *vl,
952 const char *key,
953 uint64_t *value)
954 UC_WRAP(meta_data_get_unsigned_int)
955 int uc_meta_data_get_double (const value_list_t *vl,
956 const char *key,
957 double *value)
958 UC_WRAP(meta_data_get_double)
959 int uc_meta_data_get_boolean (const value_list_t *vl,
960 const char *key,
961 _Bool *value)
962 UC_WRAP(meta_data_get_boolean)
963 #undef UC_WRAP
965 /* vim: set sw=2 ts=8 sts=2 tw=78 : */