fe22f211bceafac758ad5734672526cc86df640c
1 /**
2 * collectd - src/utils_cache.c
3 * Copyright (C) 2007-2010 Florian octo Forster
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Florian octo Forster <octo at collectd.org>
25 **/
27 #include "collectd.h"
28 #include "common.h"
29 #include "plugin.h"
30 #include "utils_avltree.h"
31 #include "utils_cache.h"
32 #include "meta_data.h"
34 #include <assert.h>
35 #include <pthread.h>
37 typedef struct cache_entry_s
38 {
39 char name[6 * DATA_MAX_NAME_LEN];
40 int values_num;
41 gauge_t *values_gauge;
42 value_t *values_raw;
43 /* Time contained in the package
44 * (for calculating rates) */
45 cdtime_t last_time;
46 /* Time according to the local clock
47 * (for purging old entries) */
48 cdtime_t last_update;
49 /* Interval in which the data is collected
50 * (for purding old entries) */
51 cdtime_t interval;
52 int state;
53 int hits;
55 /*
56 * +-----+-----+-----+-----+-----+-----+-----+-----+-----+----
57 * ! 0 ! 1 ! 2 ! 3 ! 4 ! 5 ! 6 ! 7 ! 8 ! ...
58 * +-----+-----+-----+-----+-----+-----+-----+-----+-----+----
59 * ! ds0 ! ds1 ! ds2 ! ds0 ! ds1 ! ds2 ! ds0 ! ds1 ! ds2 ! ...
60 * +-----+-----+-----+-----+-----+-----+-----+-----+-----+----
61 * ! t = 0 ! t = 1 ! t = 2 ! ...
62 * +-----------------+-----------------+-----------------+----
63 */
64 gauge_t *history;
65 size_t history_index; /* points to the next position to write to. */
66 size_t history_length;
68 meta_data_t *meta;
69 } cache_entry_t;
71 static c_avl_tree_t *cache_tree = NULL;
72 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
74 static int cache_compare (const cache_entry_t *a, const cache_entry_t *b)
75 {
76 #if COLLECT_DEBUG
77 assert ((a != NULL) && (b != NULL));
78 #endif
79 return (strcmp (a->name, b->name));
80 } /* int cache_compare */
82 static cache_entry_t *cache_alloc (int values_num)
83 {
84 cache_entry_t *ce;
86 ce = (cache_entry_t *) malloc (sizeof (cache_entry_t));
87 if (ce == NULL)
88 {
89 ERROR ("utils_cache: cache_alloc: malloc failed.");
90 return (NULL);
91 }
92 memset (ce, '\0', sizeof (cache_entry_t));
93 ce->values_num = values_num;
95 ce->values_gauge = calloc (values_num, sizeof (*ce->values_gauge));
96 ce->values_raw = calloc (values_num, sizeof (*ce->values_raw));
97 if ((ce->values_gauge == NULL) || (ce->values_raw == NULL))
98 {
99 sfree (ce->values_gauge);
100 sfree (ce->values_raw);
101 sfree (ce);
102 ERROR ("utils_cache: cache_alloc: calloc failed.");
103 return (NULL);
104 }
106 ce->history = NULL;
107 ce->history_length = 0;
108 ce->meta = NULL;
110 return (ce);
111 } /* cache_entry_t *cache_alloc */
113 static void cache_free (cache_entry_t *ce)
114 {
115 if (ce == NULL)
116 return;
118 sfree (ce->values_gauge);
119 sfree (ce->values_raw);
120 sfree (ce->history);
121 if (ce->meta != NULL)
122 {
123 meta_data_destroy (ce->meta);
124 ce->meta = NULL;
125 }
126 sfree (ce);
127 } /* void cache_free */
129 static void uc_check_range (const data_set_t *ds, cache_entry_t *ce)
130 {
131 int i;
133 for (i = 0; i < ds->ds_num; i++)
134 {
135 if (isnan (ce->values_gauge[i]))
136 continue;
137 else if (ce->values_gauge[i] < ds->ds[i].min)
138 ce->values_gauge[i] = NAN;
139 else if (ce->values_gauge[i] > ds->ds[i].max)
140 ce->values_gauge[i] = NAN;
141 }
142 } /* void uc_check_range */
144 static int uc_insert (const data_set_t *ds, const value_list_t *vl,
145 const char *key)
146 {
147 int i;
148 char *key_copy;
149 cache_entry_t *ce;
151 /* `cache_lock' has been locked by `uc_update' */
153 key_copy = strdup (key);
154 if (key_copy == NULL)
155 {
156 ERROR ("uc_insert: strdup failed.");
157 return (-1);
158 }
160 ce = cache_alloc (ds->ds_num);
161 if (ce == NULL)
162 {
163 sfree (key_copy);
164 ERROR ("uc_insert: cache_alloc (%i) failed.", ds->ds_num);
165 return (-1);
166 }
168 sstrncpy (ce->name, key, sizeof (ce->name));
170 for (i = 0; i < ds->ds_num; i++)
171 {
172 switch (ds->ds[i].type)
173 {
174 case DS_TYPE_COUNTER:
175 ce->values_gauge[i] = NAN;
176 ce->values_raw[i].counter = vl->values[i].counter;
177 break;
179 case DS_TYPE_GAUGE:
180 ce->values_gauge[i] = vl->values[i].gauge;
181 ce->values_raw[i].gauge = vl->values[i].gauge;
182 break;
184 case DS_TYPE_DERIVE:
185 ce->values_gauge[i] = NAN;
186 ce->values_raw[i].derive = vl->values[i].derive;
187 break;
189 case DS_TYPE_ABSOLUTE:
190 ce->values_gauge[i] = NAN;
191 if (vl->interval > 0)
192 ce->values_gauge[i] = ((double) vl->values[i].absolute)
193 / CDTIME_T_TO_DOUBLE (vl->interval);
194 ce->values_raw[i].absolute = vl->values[i].absolute;
195 break;
197 default:
198 /* This shouldn't happen. */
199 ERROR ("uc_insert: Don't know how to handle data source type %i.",
200 ds->ds[i].type);
201 return (-1);
202 } /* switch (ds->ds[i].type) */
203 } /* for (i) */
205 /* Prune invalid gauge data */
206 uc_check_range (ds, ce);
208 ce->last_time = vl->time;
209 ce->last_update = cdtime ();
210 ce->interval = vl->interval;
211 ce->state = STATE_OKAY;
213 if (c_avl_insert (cache_tree, key_copy, ce) != 0)
214 {
215 sfree (key_copy);
216 ERROR ("uc_insert: c_avl_insert failed.");
217 return (-1);
218 }
220 DEBUG ("uc_insert: Added %s to the cache.", key);
221 return (0);
222 } /* int uc_insert */
224 int uc_init (void)
225 {
226 if (cache_tree == NULL)
227 cache_tree = c_avl_create ((int (*) (const void *, const void *))
228 cache_compare);
230 return (0);
231 } /* int uc_init */
233 int uc_check_timeout (void)
234 {
235 cdtime_t now;
236 cache_entry_t *ce;
238 char **keys = NULL;
239 cdtime_t *keys_time = NULL;
240 cdtime_t *keys_interval = NULL;
241 int keys_len = 0;
243 char *key;
244 c_avl_iterator_t *iter;
246 int status;
247 int i;
249 pthread_mutex_lock (&cache_lock);
251 now = cdtime ();
253 /* Build a list of entries to be flushed */
254 iter = c_avl_get_iterator (cache_tree);
255 while (c_avl_iterator_next (iter, (void *) &key, (void *) &ce) == 0)
256 {
257 char **tmp;
258 cdtime_t *tmp_time;
260 /* If the entry is fresh enough, continue. */
261 if ((now - ce->last_update) < (ce->interval * timeout_g))
262 continue;
264 /* If entry has not been updated, add to `keys' array */
265 tmp = (char **) realloc ((void *) keys,
266 (keys_len + 1) * sizeof (char *));
267 if (tmp == NULL)
268 {
269 ERROR ("uc_check_timeout: realloc failed.");
270 continue;
271 }
272 keys = tmp;
274 tmp_time = realloc (keys_time, (keys_len + 1) * sizeof (*keys_time));
275 if (tmp_time == NULL)
276 {
277 ERROR ("uc_check_timeout: realloc failed.");
278 continue;
279 }
280 keys_time = tmp_time;
282 tmp_time = realloc (keys_interval, (keys_len + 1) * sizeof (*keys_interval));
283 if (tmp_time == NULL)
284 {
285 ERROR ("uc_check_timeout: realloc failed.");
286 continue;
287 }
288 keys_interval = tmp_time;
290 keys[keys_len] = strdup (key);
291 if (keys[keys_len] == NULL)
292 {
293 ERROR ("uc_check_timeout: strdup failed.");
294 continue;
295 }
296 keys_time[keys_len] = ce->last_time;
297 keys_interval[keys_len] = ce->interval;
299 keys_len++;
300 } /* while (c_avl_iterator_next) */
302 c_avl_iterator_destroy (iter);
303 pthread_mutex_unlock (&cache_lock);
305 if (keys_len == 0)
306 return (0);
308 /* Call the "missing" callback for each value. Do this before removing the
309 * value from the cache, so that callbacks can still access the data stored,
310 * including plugin specific meta data, rates, history, …. This must be done
311 * without holding the lock, otherwise we will run into a deadlock if a
312 * plugin calls the cache interface. */
313 for (i = 0; i < keys_len; i++)
314 {
315 value_list_t vl = VALUE_LIST_INIT;
317 vl.values = NULL;
318 vl.values_len = 0;
319 vl.meta = NULL;
321 status = parse_identifier_vl (keys[i], &vl);
322 if (status != 0)
323 {
324 ERROR ("uc_check_timeout: parse_identifier_vl (\"%s\") failed.", keys[i]);
325 cache_free (ce);
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 size_t uc_get_size() {
574 size_t size_arrays = 0;
576 pthread_mutex_lock (&cache_lock);
577 size_arrays = (size_t) c_avl_size (cache_tree);
578 pthread_mutex_unlock (&cache_lock);
580 return (size_arrays);
581 }
583 int uc_get_names (char ***ret_names, cdtime_t **ret_times, size_t *ret_number)
584 {
585 c_avl_iterator_t *iter;
586 char *key;
587 cache_entry_t *value;
589 char **names = NULL;
590 cdtime_t *times = NULL;
591 size_t number = 0;
592 size_t size_arrays = 0;
594 int status = 0;
596 if ((ret_names == NULL) || (ret_number == NULL))
597 return (-1);
599 pthread_mutex_lock (&cache_lock);
601 size_arrays = (size_t) c_avl_size (cache_tree);
602 if (size_arrays < 1)
603 {
604 /* Handle the "no values" case here, to avoid the error message when
605 * calloc() returns NULL. */
606 pthread_mutex_unlock (&cache_lock);
607 return (0);
608 }
610 names = calloc (size_arrays, sizeof (*names));
611 times = calloc (size_arrays, sizeof (*times));
612 if ((names == NULL) || (times == NULL))
613 {
614 ERROR ("uc_get_names: calloc failed.");
615 sfree (names);
616 sfree (times);
617 pthread_mutex_unlock (&cache_lock);
618 return (ENOMEM);
619 }
621 iter = c_avl_get_iterator (cache_tree);
622 while (c_avl_iterator_next (iter, (void *) &key, (void *) &value) == 0)
623 {
624 /* remove missing values when list values */
625 if (value->state == STATE_MISSING)
626 continue;
628 /* c_avl_size does not return a number smaller than the number of elements
629 * returned by c_avl_iterator_next. */
630 assert (number < size_arrays);
632 if (ret_times != NULL)
633 times[number] = value->last_time;
635 names[number] = strdup (key);
636 if (names[number] == NULL)
637 {
638 status = -1;
639 break;
640 }
642 number++;
643 } /* while (c_avl_iterator_next) */
645 c_avl_iterator_destroy (iter);
646 pthread_mutex_unlock (&cache_lock);
648 if (status != 0)
649 {
650 size_t i;
652 for (i = 0; i < number; i++)
653 {
654 sfree (names[i]);
655 }
656 sfree (names);
658 return (-1);
659 }
661 *ret_names = names;
662 if (ret_times != NULL)
663 *ret_times = times;
664 *ret_number = number;
666 return (0);
667 } /* int uc_get_names */
669 int uc_get_state (const data_set_t *ds, const value_list_t *vl)
670 {
671 char name[6 * DATA_MAX_NAME_LEN];
672 cache_entry_t *ce = NULL;
673 int ret = STATE_ERROR;
675 if (FORMAT_VL (name, sizeof (name), vl) != 0)
676 {
677 ERROR ("uc_get_state: FORMAT_VL failed.");
678 return (STATE_ERROR);
679 }
681 pthread_mutex_lock (&cache_lock);
683 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
684 {
685 assert (ce != NULL);
686 ret = ce->state;
687 }
689 pthread_mutex_unlock (&cache_lock);
691 return (ret);
692 } /* int uc_get_state */
694 int uc_set_state (const data_set_t *ds, const value_list_t *vl, int state)
695 {
696 char name[6 * DATA_MAX_NAME_LEN];
697 cache_entry_t *ce = NULL;
698 int ret = -1;
700 if (FORMAT_VL (name, sizeof (name), vl) != 0)
701 {
702 ERROR ("uc_get_state: FORMAT_VL failed.");
703 return (STATE_ERROR);
704 }
706 pthread_mutex_lock (&cache_lock);
708 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
709 {
710 assert (ce != NULL);
711 ret = ce->state;
712 ce->state = state;
713 }
715 pthread_mutex_unlock (&cache_lock);
717 return (ret);
718 } /* int uc_set_state */
720 int uc_get_history_by_name (const char *name,
721 gauge_t *ret_history, size_t num_steps, size_t num_ds)
722 {
723 cache_entry_t *ce = NULL;
724 size_t i;
725 int status = 0;
727 pthread_mutex_lock (&cache_lock);
729 status = c_avl_get (cache_tree, name, (void *) &ce);
730 if (status != 0)
731 {
732 pthread_mutex_unlock (&cache_lock);
733 return (-ENOENT);
734 }
736 if (((size_t) ce->values_num) != num_ds)
737 {
738 pthread_mutex_unlock (&cache_lock);
739 return (-EINVAL);
740 }
742 /* Check if there are enough values available. If not, increase the buffer
743 * size. */
744 if (ce->history_length < num_steps)
745 {
746 gauge_t *tmp;
747 size_t i;
749 tmp = realloc (ce->history, sizeof (*ce->history)
750 * num_steps * ce->values_num);
751 if (tmp == NULL)
752 {
753 pthread_mutex_unlock (&cache_lock);
754 return (-ENOMEM);
755 }
757 for (i = ce->history_length * ce->values_num;
758 i < (num_steps * ce->values_num);
759 i++)
760 tmp[i] = NAN;
762 ce->history = tmp;
763 ce->history_length = num_steps;
764 } /* if (ce->history_length < num_steps) */
766 /* Copy the values to the output buffer. */
767 for (i = 0; i < num_steps; i++)
768 {
769 size_t src_index;
770 size_t dst_index;
772 if (i < ce->history_index)
773 src_index = ce->history_index - (i + 1);
774 else
775 src_index = ce->history_length + ce->history_index - (i + 1);
776 src_index = src_index * num_ds;
778 dst_index = i * num_ds;
780 memcpy (ret_history + dst_index, ce->history + src_index,
781 sizeof (*ret_history) * num_ds);
782 }
784 pthread_mutex_unlock (&cache_lock);
786 return (0);
787 } /* int uc_get_history_by_name */
789 int uc_get_history (const data_set_t *ds, const value_list_t *vl,
790 gauge_t *ret_history, size_t num_steps, size_t num_ds)
791 {
792 char name[6 * DATA_MAX_NAME_LEN];
794 if (FORMAT_VL (name, sizeof (name), vl) != 0)
795 {
796 ERROR ("utils_cache: uc_get_history: FORMAT_VL failed.");
797 return (-1);
798 }
800 return (uc_get_history_by_name (name, ret_history, num_steps, num_ds));
801 } /* int uc_get_history */
803 int uc_get_hits (const data_set_t *ds, const value_list_t *vl)
804 {
805 char name[6 * DATA_MAX_NAME_LEN];
806 cache_entry_t *ce = NULL;
807 int ret = STATE_ERROR;
809 if (FORMAT_VL (name, sizeof (name), vl) != 0)
810 {
811 ERROR ("uc_get_state: FORMAT_VL failed.");
812 return (STATE_ERROR);
813 }
815 pthread_mutex_lock (&cache_lock);
817 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
818 {
819 assert (ce != NULL);
820 ret = ce->hits;
821 }
823 pthread_mutex_unlock (&cache_lock);
825 return (ret);
826 } /* int uc_get_hits */
828 int uc_set_hits (const data_set_t *ds, const value_list_t *vl, int hits)
829 {
830 char name[6 * DATA_MAX_NAME_LEN];
831 cache_entry_t *ce = NULL;
832 int ret = -1;
834 if (FORMAT_VL (name, sizeof (name), vl) != 0)
835 {
836 ERROR ("uc_get_state: FORMAT_VL failed.");
837 return (STATE_ERROR);
838 }
840 pthread_mutex_lock (&cache_lock);
842 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
843 {
844 assert (ce != NULL);
845 ret = ce->hits;
846 ce->hits = hits;
847 }
849 pthread_mutex_unlock (&cache_lock);
851 return (ret);
852 } /* int uc_set_hits */
854 int uc_inc_hits (const data_set_t *ds, const value_list_t *vl, int step)
855 {
856 char name[6 * DATA_MAX_NAME_LEN];
857 cache_entry_t *ce = NULL;
858 int ret = -1;
860 if (FORMAT_VL (name, sizeof (name), vl) != 0)
861 {
862 ERROR ("uc_get_state: FORMAT_VL failed.");
863 return (STATE_ERROR);
864 }
866 pthread_mutex_lock (&cache_lock);
868 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
869 {
870 assert (ce != NULL);
871 ret = ce->hits;
872 ce->hits = ret + step;
873 }
875 pthread_mutex_unlock (&cache_lock);
877 return (ret);
878 } /* int uc_inc_hits */
880 /*
881 * Meta data interface
882 */
883 /* XXX: This function will acquire `cache_lock' but will not free it! */
884 static meta_data_t *uc_get_meta (const value_list_t *vl) /* {{{ */
885 {
886 char name[6 * DATA_MAX_NAME_LEN];
887 cache_entry_t *ce = NULL;
888 int status;
890 status = FORMAT_VL (name, sizeof (name), vl);
891 if (status != 0)
892 {
893 ERROR ("utils_cache: uc_get_meta: FORMAT_VL failed.");
894 return (NULL);
895 }
897 pthread_mutex_lock (&cache_lock);
899 status = c_avl_get (cache_tree, name, (void *) &ce);
900 if (status != 0)
901 {
902 pthread_mutex_unlock (&cache_lock);
903 return (NULL);
904 }
905 assert (ce != NULL);
907 if (ce->meta == NULL)
908 ce->meta = meta_data_create ();
910 if (ce->meta == NULL)
911 pthread_mutex_unlock (&cache_lock);
913 return (ce->meta);
914 } /* }}} meta_data_t *uc_get_meta */
916 /* Sorry about this preprocessor magic, but it really makes this file much
917 * shorter.. */
918 #define UC_WRAP(wrap_function) { \
919 meta_data_t *meta; \
920 int status; \
921 meta = uc_get_meta (vl); \
922 if (meta == NULL) return (-1); \
923 status = wrap_function (meta, key); \
924 pthread_mutex_unlock (&cache_lock); \
925 return (status); \
926 }
927 int uc_meta_data_exists (const value_list_t *vl, const char *key)
928 UC_WRAP (meta_data_exists)
930 int uc_meta_data_delete (const value_list_t *vl, const char *key)
931 UC_WRAP (meta_data_delete)
932 #undef UC_WRAP
934 /* We need a new version of this macro because the following functions take
935 * two argumetns. */
936 #define UC_WRAP(wrap_function) { \
937 meta_data_t *meta; \
938 int status; \
939 meta = uc_get_meta (vl); \
940 if (meta == NULL) return (-1); \
941 status = wrap_function (meta, key, value); \
942 pthread_mutex_unlock (&cache_lock); \
943 return (status); \
944 }
945 int uc_meta_data_add_string (const value_list_t *vl,
946 const char *key,
947 const char *value)
948 UC_WRAP(meta_data_add_string)
949 int uc_meta_data_add_signed_int (const value_list_t *vl,
950 const char *key,
951 int64_t value)
952 UC_WRAP(meta_data_add_signed_int)
953 int uc_meta_data_add_unsigned_int (const value_list_t *vl,
954 const char *key,
955 uint64_t value)
956 UC_WRAP(meta_data_add_unsigned_int)
957 int uc_meta_data_add_double (const value_list_t *vl,
958 const char *key,
959 double value)
960 UC_WRAP(meta_data_add_double)
961 int uc_meta_data_add_boolean (const value_list_t *vl,
962 const char *key,
963 _Bool value)
964 UC_WRAP(meta_data_add_boolean)
966 int uc_meta_data_get_string (const value_list_t *vl,
967 const char *key,
968 char **value)
969 UC_WRAP(meta_data_get_string)
970 int uc_meta_data_get_signed_int (const value_list_t *vl,
971 const char *key,
972 int64_t *value)
973 UC_WRAP(meta_data_get_signed_int)
974 int uc_meta_data_get_unsigned_int (const value_list_t *vl,
975 const char *key,
976 uint64_t *value)
977 UC_WRAP(meta_data_get_unsigned_int)
978 int uc_meta_data_get_double (const value_list_t *vl,
979 const char *key,
980 double *value)
981 UC_WRAP(meta_data_get_double)
982 int uc_meta_data_get_boolean (const value_list_t *vl,
983 const char *key,
984 _Bool *value)
985 UC_WRAP(meta_data_get_boolean)
986 #undef UC_WRAP
988 /* vim: set sw=2 ts=8 sts=2 tw=78 : */