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