1 /**
2 * collectd - src/utils_cache.c
3 * Copyright (C) 2007-2010 Florian octo Forster
4 * Copyright (C) 2016 Sebastian tokkee Harl
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Florian octo Forster <octo at collectd.org>
26 * Sebastian tokkee Harl <sh at tokkee.org>
27 **/
29 #include "collectd.h"
31 #include "common.h"
32 #include "meta_data.h"
33 #include "plugin.h"
34 #include "utils_avltree.h"
35 #include "utils_cache.h"
37 #include <assert.h>
39 typedef struct cache_entry_s {
40 char name[6 * DATA_MAX_NAME_LEN];
41 size_t values_num;
42 gauge_t *values_gauge;
43 value_t *values_raw;
44 /* Time contained in the package
45 * (for calculating rates) */
46 cdtime_t last_time;
47 /* Time according to the local clock
48 * (for purging old entries) */
49 cdtime_t last_update;
50 /* Interval in which the data is collected
51 * (for purging old entries) */
52 cdtime_t interval;
53 int state;
54 int hits;
56 /*
57 * +-----+-----+-----+-----+-----+-----+-----+-----+-----+----
58 * ! 0 ! 1 ! 2 ! 3 ! 4 ! 5 ! 6 ! 7 ! 8 ! ...
59 * +-----+-----+-----+-----+-----+-----+-----+-----+-----+----
60 * ! ds0 ! ds1 ! ds2 ! ds0 ! ds1 ! ds2 ! ds0 ! ds1 ! ds2 ! ...
61 * +-----+-----+-----+-----+-----+-----+-----+-----+-----+----
62 * ! t = 0 ! t = 1 ! t = 2 ! ...
63 * +-----------------+-----------------+-----------------+----
64 */
65 gauge_t *history;
66 size_t history_index; /* points to the next position to write to. */
67 size_t history_length;
69 meta_data_t *meta;
70 } cache_entry_t;
72 struct uc_iter_s {
73 c_avl_iterator_t *iter;
75 char *name;
76 cache_entry_t *entry;
77 };
79 static c_avl_tree_t *cache_tree = NULL;
80 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
82 static int cache_compare(const cache_entry_t *a, const cache_entry_t *b) {
83 #if COLLECT_DEBUG
84 assert((a != NULL) && (b != NULL));
85 #endif
86 return (strcmp(a->name, b->name));
87 } /* int cache_compare */
89 static cache_entry_t *cache_alloc(size_t values_num) {
90 cache_entry_t *ce;
92 ce = calloc(1, sizeof(*ce));
93 if (ce == NULL) {
94 ERROR("utils_cache: cache_alloc: calloc failed.");
95 return (NULL);
96 }
97 ce->values_num = values_num;
99 ce->values_gauge = calloc(values_num, sizeof(*ce->values_gauge));
100 ce->values_raw = calloc(values_num, sizeof(*ce->values_raw));
101 if ((ce->values_gauge == NULL) || (ce->values_raw == NULL)) {
102 sfree(ce->values_gauge);
103 sfree(ce->values_raw);
104 sfree(ce);
105 ERROR("utils_cache: cache_alloc: calloc failed.");
106 return (NULL);
107 }
109 ce->history = NULL;
110 ce->history_length = 0;
111 ce->meta = NULL;
113 return (ce);
114 } /* cache_entry_t *cache_alloc */
116 static void cache_free(cache_entry_t *ce) {
117 if (ce == NULL)
118 return;
120 sfree(ce->values_gauge);
121 sfree(ce->values_raw);
122 sfree(ce->history);
123 if (ce->meta != NULL) {
124 meta_data_destroy(ce->meta);
125 ce->meta = NULL;
126 }
127 sfree(ce);
128 } /* void cache_free */
130 static void uc_check_range(const data_set_t *ds, cache_entry_t *ce) {
131 for (size_t i = 0; i < ds->ds_num; i++) {
132 if (isnan(ce->values_gauge[i]))
133 continue;
134 else if (ce->values_gauge[i] < ds->ds[i].min)
135 ce->values_gauge[i] = NAN;
136 else if (ce->values_gauge[i] > ds->ds[i].max)
137 ce->values_gauge[i] = NAN;
138 }
139 } /* void uc_check_range */
141 static int uc_insert(const data_set_t *ds, const value_list_t *vl,
142 const char *key) {
143 char *key_copy;
144 cache_entry_t *ce;
146 /* `cache_lock' has been locked by `uc_update' */
148 key_copy = strdup(key);
149 if (key_copy == NULL) {
150 ERROR("uc_insert: strdup failed.");
151 return (-1);
152 }
154 ce = cache_alloc(ds->ds_num);
155 if (ce == NULL) {
156 sfree(key_copy);
157 ERROR("uc_insert: cache_alloc (%zu) failed.", ds->ds_num);
158 return (-1);
159 }
161 sstrncpy(ce->name, key, sizeof(ce->name));
163 for (size_t i = 0; i < ds->ds_num; i++) {
164 switch (ds->ds[i].type) {
165 case DS_TYPE_COUNTER:
166 ce->values_gauge[i] = NAN;
167 ce->values_raw[i].counter = vl->values[i].counter;
168 break;
170 case DS_TYPE_GAUGE:
171 ce->values_gauge[i] = vl->values[i].gauge;
172 ce->values_raw[i].gauge = vl->values[i].gauge;
173 break;
175 case DS_TYPE_DERIVE:
176 ce->values_gauge[i] = NAN;
177 ce->values_raw[i].derive = vl->values[i].derive;
178 break;
180 case DS_TYPE_ABSOLUTE:
181 ce->values_gauge[i] = NAN;
182 if (vl->interval > 0)
183 ce->values_gauge[i] =
184 ((double)vl->values[i].absolute) / CDTIME_T_TO_DOUBLE(vl->interval);
185 ce->values_raw[i].absolute = vl->values[i].absolute;
186 break;
188 default:
189 /* This shouldn't happen. */
190 ERROR("uc_insert: Don't know how to handle data source type %i.",
191 ds->ds[i].type);
192 sfree(key_copy);
193 cache_free(ce);
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 sfree(key_copy);
208 ERROR("uc_insert: c_avl_insert failed.");
209 return (-1);
210 }
212 DEBUG("uc_insert: Added %s to the cache.", key);
213 return (0);
214 } /* int uc_insert */
216 int uc_init(void) {
217 if (cache_tree == NULL)
218 cache_tree =
219 c_avl_create((int (*)(const void *, const void *))cache_compare);
221 return (0);
222 } /* int uc_init */
224 int uc_check_timeout(void) {
225 cdtime_t now;
226 cache_entry_t *ce;
228 char **keys = NULL;
229 cdtime_t *keys_time = NULL;
230 cdtime_t *keys_interval = NULL;
231 int keys_len = 0;
233 char *key;
234 c_avl_iterator_t *iter;
236 int status;
238 pthread_mutex_lock(&cache_lock);
240 now = cdtime();
242 /* Build a list of entries to be flushed */
243 iter = c_avl_get_iterator(cache_tree);
244 while (c_avl_iterator_next(iter, (void *)&key, (void *)&ce) == 0) {
245 char **tmp;
246 cdtime_t *tmp_time;
248 /* If the entry is fresh enough, continue. */
249 if ((now - ce->last_update) < (ce->interval * timeout_g))
250 continue;
252 /* If entry has not been updated, add to `keys' array */
253 tmp = realloc((void *)keys, (keys_len + 1) * sizeof(char *));
254 if (tmp == NULL) {
255 ERROR("uc_check_timeout: realloc failed.");
256 continue;
257 }
258 keys = tmp;
260 tmp_time = realloc(keys_time, (keys_len + 1) * sizeof(*keys_time));
261 if (tmp_time == NULL) {
262 ERROR("uc_check_timeout: realloc failed.");
263 continue;
264 }
265 keys_time = tmp_time;
267 tmp_time = realloc(keys_interval, (keys_len + 1) * sizeof(*keys_interval));
268 if (tmp_time == NULL) {
269 ERROR("uc_check_timeout: realloc failed.");
270 continue;
271 }
272 keys_interval = tmp_time;
274 keys[keys_len] = strdup(key);
275 if (keys[keys_len] == NULL) {
276 ERROR("uc_check_timeout: strdup failed.");
277 continue;
278 }
279 keys_time[keys_len] = ce->last_time;
280 keys_interval[keys_len] = ce->interval;
282 keys_len++;
283 } /* while (c_avl_iterator_next) */
285 c_avl_iterator_destroy(iter);
286 pthread_mutex_unlock(&cache_lock);
288 if (keys_len == 0) {
289 /* realloc() may have been called for these. */
290 sfree(keys);
291 sfree(keys_time);
292 sfree(keys_interval);
293 return (0);
294 }
296 /* Call the "missing" callback for each value. Do this before removing the
297 * value from the cache, so that callbacks can still access the data stored,
298 * including plugin specific meta data, rates, history, …. This must be done
299 * without holding the lock, otherwise we will run into a deadlock if a
300 * plugin calls the cache interface. */
301 for (int i = 0; i < keys_len; i++) {
302 value_list_t vl = VALUE_LIST_INIT;
304 vl.values = NULL;
305 vl.values_len = 0;
306 vl.meta = NULL;
308 status = parse_identifier_vl(keys[i], &vl);
309 if (status != 0) {
310 ERROR("uc_check_timeout: parse_identifier_vl (\"%s\") failed.", keys[i]);
311 continue;
312 }
314 vl.time = keys_time[i];
315 vl.interval = keys_interval[i];
317 plugin_dispatch_missing(&vl);
318 } /* for (i = 0; i < keys_len; i++) */
320 /* Now actually remove all the values from the cache. We don't re-evaluate
321 * the timestamp again, so in theory it is possible we remove a value after
322 * it is updated here. */
323 pthread_mutex_lock(&cache_lock);
324 for (int i = 0; i < keys_len; i++) {
325 key = NULL;
326 ce = NULL;
328 status = c_avl_remove(cache_tree, keys[i], (void *)&key, (void *)&ce);
329 if (status != 0) {
330 ERROR("uc_check_timeout: c_avl_remove (\"%s\") failed.", keys[i]);
331 sfree(keys[i]);
332 continue;
333 }
335 sfree(keys[i]);
336 sfree(key);
337 cache_free(ce);
338 } /* for (i = 0; i < keys_len; i++) */
339 pthread_mutex_unlock(&cache_lock);
341 sfree(keys);
342 sfree(keys_time);
343 sfree(keys_interval);
345 return (0);
346 } /* int uc_check_timeout */
348 int uc_update(const data_set_t *ds, const value_list_t *vl) {
349 char name[6 * DATA_MAX_NAME_LEN];
350 cache_entry_t *ce = NULL;
351 int status;
353 if (FORMAT_VL(name, sizeof(name), vl) != 0) {
354 ERROR("uc_update: FORMAT_VL failed.");
355 return (-1);
356 }
358 pthread_mutex_lock(&cache_lock);
360 status = c_avl_get(cache_tree, name, (void *)&ce);
361 if (status != 0) /* entry does not yet exist */
362 {
363 status = uc_insert(ds, vl, name);
364 pthread_mutex_unlock(&cache_lock);
365 return (status);
366 }
368 assert(ce != NULL);
369 assert(ce->values_num == ds->ds_num);
371 if (ce->last_time >= vl->time) {
372 pthread_mutex_unlock(&cache_lock);
373 NOTICE("uc_update: Value too old: name = %s; value time = %.3f; "
374 "last cache update = %.3f;",
375 name, CDTIME_T_TO_DOUBLE(vl->time),
376 CDTIME_T_TO_DOUBLE(ce->last_time));
377 return (-1);
378 }
380 for (size_t i = 0; i < ds->ds_num; i++) {
381 switch (ds->ds[i].type) {
382 case DS_TYPE_COUNTER: {
383 counter_t diff =
384 counter_diff(ce->values_raw[i].counter, vl->values[i].counter);
385 ce->values_gauge[i] =
386 ((double)diff) / (CDTIME_T_TO_DOUBLE(vl->time - ce->last_time));
387 ce->values_raw[i].counter = vl->values[i].counter;
388 } break;
390 case DS_TYPE_GAUGE:
391 ce->values_raw[i].gauge = vl->values[i].gauge;
392 ce->values_gauge[i] = vl->values[i].gauge;
393 break;
395 case DS_TYPE_DERIVE: {
396 derive_t diff = vl->values[i].derive - ce->values_raw[i].derive;
398 ce->values_gauge[i] =
399 ((double)diff) / (CDTIME_T_TO_DOUBLE(vl->time - ce->last_time));
400 ce->values_raw[i].derive = vl->values[i].derive;
401 } break;
403 case DS_TYPE_ABSOLUTE:
404 ce->values_gauge[i] = ((double)vl->values[i].absolute) /
405 (CDTIME_T_TO_DOUBLE(vl->time - ce->last_time));
406 ce->values_raw[i].absolute = vl->values[i].absolute;
407 break;
409 default:
410 /* This shouldn't happen. */
411 pthread_mutex_unlock(&cache_lock);
412 ERROR("uc_update: Don't know how to handle data source type %i.",
413 ds->ds[i].type);
414 return (-1);
415 } /* switch (ds->ds[i].type) */
417 DEBUG("uc_update: %s: ds[%zu] = %lf", name, i, ce->values_gauge[i]);
418 } /* for (i) */
420 /* Update the history if it exists. */
421 if (ce->history != NULL) {
422 assert(ce->history_index < ce->history_length);
423 for (size_t i = 0; i < ce->values_num; i++) {
424 size_t hist_idx = (ce->values_num * ce->history_index) + i;
425 ce->history[hist_idx] = ce->values_gauge[i];
426 }
428 assert(ce->history_length > 0);
429 ce->history_index = (ce->history_index + 1) % ce->history_length;
430 }
432 /* Prune invalid gauge data */
433 uc_check_range(ds, ce);
435 ce->last_time = vl->time;
436 ce->last_update = cdtime();
437 ce->interval = vl->interval;
439 pthread_mutex_unlock(&cache_lock);
441 return (0);
442 } /* int uc_update */
444 int uc_get_rate_by_name(const char *name, gauge_t **ret_values,
445 size_t *ret_values_num) {
446 gauge_t *ret = NULL;
447 size_t ret_num = 0;
448 cache_entry_t *ce = NULL;
449 int status = 0;
451 pthread_mutex_lock(&cache_lock);
453 if (c_avl_get(cache_tree, name, (void *)&ce) == 0) {
454 assert(ce != NULL);
456 /* remove missing values from getval */
457 if (ce->state == STATE_MISSING) {
458 status = -1;
459 } else {
460 ret_num = ce->values_num;
461 ret = malloc(ret_num * sizeof(*ret));
462 if (ret == NULL) {
463 ERROR("utils_cache: uc_get_rate_by_name: malloc failed.");
464 status = -1;
465 } else {
466 memcpy(ret, ce->values_gauge, ret_num * sizeof(gauge_t));
467 }
468 }
469 } else {
470 DEBUG("utils_cache: uc_get_rate_by_name: No such value: %s", name);
471 status = -1;
472 }
474 pthread_mutex_unlock(&cache_lock);
476 if (status == 0) {
477 *ret_values = ret;
478 *ret_values_num = ret_num;
479 }
481 return (status);
482 } /* gauge_t *uc_get_rate_by_name */
484 gauge_t *uc_get_rate(const data_set_t *ds, const value_list_t *vl) {
485 char name[6 * DATA_MAX_NAME_LEN];
486 gauge_t *ret = NULL;
487 size_t ret_num = 0;
488 int status;
490 if (FORMAT_VL(name, sizeof(name), vl) != 0) {
491 ERROR("utils_cache: uc_get_rate: FORMAT_VL failed.");
492 return (NULL);
493 }
495 status = uc_get_rate_by_name(name, &ret, &ret_num);
496 if (status != 0)
497 return (NULL);
499 /* This is important - the caller has no other way of knowing how many
500 * values are returned. */
501 if (ret_num != (size_t)ds->ds_num) {
502 ERROR("utils_cache: uc_get_rate: ds[%s] has %zu values, "
503 "but uc_get_rate_by_name returned %zu.",
504 ds->type, ds->ds_num, ret_num);
505 sfree(ret);
506 return (NULL);
507 }
509 return (ret);
510 } /* gauge_t *uc_get_rate */
512 size_t uc_get_size(void) {
513 size_t size_arrays = 0;
515 pthread_mutex_lock(&cache_lock);
516 size_arrays = (size_t)c_avl_size(cache_tree);
517 pthread_mutex_unlock(&cache_lock);
519 return (size_arrays);
520 }
522 int uc_get_names(char ***ret_names, cdtime_t **ret_times, size_t *ret_number) {
523 c_avl_iterator_t *iter;
524 char *key;
525 cache_entry_t *value;
527 char **names = NULL;
528 cdtime_t *times = NULL;
529 size_t number = 0;
530 size_t size_arrays = 0;
532 int status = 0;
534 if ((ret_names == NULL) || (ret_number == NULL))
535 return (-1);
537 pthread_mutex_lock(&cache_lock);
539 size_arrays = (size_t)c_avl_size(cache_tree);
540 if (size_arrays < 1) {
541 /* Handle the "no values" case here, to avoid the error message when
542 * calloc() returns NULL. */
543 pthread_mutex_unlock(&cache_lock);
544 return (0);
545 }
547 names = calloc(size_arrays, sizeof(*names));
548 times = calloc(size_arrays, sizeof(*times));
549 if ((names == NULL) || (times == NULL)) {
550 ERROR("uc_get_names: calloc failed.");
551 sfree(names);
552 sfree(times);
553 pthread_mutex_unlock(&cache_lock);
554 return (ENOMEM);
555 }
557 iter = c_avl_get_iterator(cache_tree);
558 while (c_avl_iterator_next(iter, (void *)&key, (void *)&value) == 0) {
559 /* remove missing values when list values */
560 if (value->state == STATE_MISSING)
561 continue;
563 /* c_avl_size does not return a number smaller than the number of elements
564 * returned by c_avl_iterator_next. */
565 assert(number < size_arrays);
567 if (ret_times != NULL)
568 times[number] = value->last_time;
570 names[number] = strdup(key);
571 if (names[number] == NULL) {
572 status = -1;
573 break;
574 }
576 number++;
577 } /* while (c_avl_iterator_next) */
579 c_avl_iterator_destroy(iter);
580 pthread_mutex_unlock(&cache_lock);
582 if (status != 0) {
583 for (size_t i = 0; i < number; i++) {
584 sfree(names[i]);
585 }
586 sfree(names);
587 sfree(times);
589 return (-1);
590 }
592 *ret_names = names;
593 if (ret_times != NULL)
594 *ret_times = times;
595 else
596 sfree(times);
597 *ret_number = number;
599 return (0);
600 } /* int uc_get_names */
602 int uc_get_state(const data_set_t *ds, const value_list_t *vl) {
603 char name[6 * DATA_MAX_NAME_LEN];
604 cache_entry_t *ce = NULL;
605 int ret = STATE_ERROR;
607 if (FORMAT_VL(name, sizeof(name), vl) != 0) {
608 ERROR("uc_get_state: FORMAT_VL failed.");
609 return (STATE_ERROR);
610 }
612 pthread_mutex_lock(&cache_lock);
614 if (c_avl_get(cache_tree, name, (void *)&ce) == 0) {
615 assert(ce != NULL);
616 ret = ce->state;
617 }
619 pthread_mutex_unlock(&cache_lock);
621 return (ret);
622 } /* int uc_get_state */
624 int uc_set_state(const data_set_t *ds, const value_list_t *vl, int state) {
625 char name[6 * DATA_MAX_NAME_LEN];
626 cache_entry_t *ce = NULL;
627 int ret = -1;
629 if (FORMAT_VL(name, sizeof(name), vl) != 0) {
630 ERROR("uc_set_state: FORMAT_VL failed.");
631 return (STATE_ERROR);
632 }
634 pthread_mutex_lock(&cache_lock);
636 if (c_avl_get(cache_tree, name, (void *)&ce) == 0) {
637 assert(ce != NULL);
638 ret = ce->state;
639 ce->state = state;
640 }
642 pthread_mutex_unlock(&cache_lock);
644 return (ret);
645 } /* int uc_set_state */
647 int uc_get_history_by_name(const char *name, gauge_t *ret_history,
648 size_t num_steps, size_t num_ds) {
649 cache_entry_t *ce = NULL;
650 int status = 0;
652 pthread_mutex_lock(&cache_lock);
654 status = c_avl_get(cache_tree, name, (void *)&ce);
655 if (status != 0) {
656 pthread_mutex_unlock(&cache_lock);
657 return (-ENOENT);
658 }
660 if (((size_t)ce->values_num) != num_ds) {
661 pthread_mutex_unlock(&cache_lock);
662 return (-EINVAL);
663 }
665 /* Check if there are enough values available. If not, increase the buffer
666 * size. */
667 if (ce->history_length < num_steps) {
668 gauge_t *tmp;
670 tmp =
671 realloc(ce->history, sizeof(*ce->history) * num_steps * ce->values_num);
672 if (tmp == NULL) {
673 pthread_mutex_unlock(&cache_lock);
674 return (-ENOMEM);
675 }
677 for (size_t i = ce->history_length * ce->values_num;
678 i < (num_steps * ce->values_num); i++)
679 tmp[i] = NAN;
681 ce->history = tmp;
682 ce->history_length = num_steps;
683 } /* if (ce->history_length < num_steps) */
685 /* Copy the values to the output buffer. */
686 for (size_t i = 0; i < num_steps; i++) {
687 size_t src_index;
688 size_t dst_index;
690 if (i < ce->history_index)
691 src_index = ce->history_index - (i + 1);
692 else
693 src_index = ce->history_length + ce->history_index - (i + 1);
694 src_index = src_index * num_ds;
696 dst_index = i * num_ds;
698 memcpy(ret_history + dst_index, ce->history + src_index,
699 sizeof(*ret_history) * num_ds);
700 }
702 pthread_mutex_unlock(&cache_lock);
704 return (0);
705 } /* int uc_get_history_by_name */
707 int uc_get_history(const data_set_t *ds, const value_list_t *vl,
708 gauge_t *ret_history, size_t num_steps, size_t num_ds) {
709 char name[6 * DATA_MAX_NAME_LEN];
711 if (FORMAT_VL(name, sizeof(name), vl) != 0) {
712 ERROR("utils_cache: uc_get_history: FORMAT_VL failed.");
713 return (-1);
714 }
716 return (uc_get_history_by_name(name, ret_history, num_steps, num_ds));
717 } /* int uc_get_history */
719 int uc_get_hits(const data_set_t *ds, const value_list_t *vl) {
720 char name[6 * DATA_MAX_NAME_LEN];
721 cache_entry_t *ce = NULL;
722 int ret = STATE_ERROR;
724 if (FORMAT_VL(name, sizeof(name), vl) != 0) {
725 ERROR("uc_get_hits: FORMAT_VL failed.");
726 return (STATE_ERROR);
727 }
729 pthread_mutex_lock(&cache_lock);
731 if (c_avl_get(cache_tree, name, (void *)&ce) == 0) {
732 assert(ce != NULL);
733 ret = ce->hits;
734 }
736 pthread_mutex_unlock(&cache_lock);
738 return (ret);
739 } /* int uc_get_hits */
741 int uc_set_hits(const data_set_t *ds, const value_list_t *vl, int hits) {
742 char name[6 * DATA_MAX_NAME_LEN];
743 cache_entry_t *ce = NULL;
744 int ret = -1;
746 if (FORMAT_VL(name, sizeof(name), vl) != 0) {
747 ERROR("uc_set_hits: FORMAT_VL failed.");
748 return (STATE_ERROR);
749 }
751 pthread_mutex_lock(&cache_lock);
753 if (c_avl_get(cache_tree, name, (void *)&ce) == 0) {
754 assert(ce != NULL);
755 ret = ce->hits;
756 ce->hits = hits;
757 }
759 pthread_mutex_unlock(&cache_lock);
761 return (ret);
762 } /* int uc_set_hits */
764 int uc_inc_hits(const data_set_t *ds, const value_list_t *vl, int step) {
765 char name[6 * DATA_MAX_NAME_LEN];
766 cache_entry_t *ce = NULL;
767 int ret = -1;
769 if (FORMAT_VL(name, sizeof(name), vl) != 0) {
770 ERROR("uc_inc_hits: FORMAT_VL failed.");
771 return (STATE_ERROR);
772 }
774 pthread_mutex_lock(&cache_lock);
776 if (c_avl_get(cache_tree, name, (void *)&ce) == 0) {
777 assert(ce != NULL);
778 ret = ce->hits;
779 ce->hits = ret + step;
780 }
782 pthread_mutex_unlock(&cache_lock);
784 return (ret);
785 } /* int uc_inc_hits */
787 /*
788 * Iterator interface
789 */
790 uc_iter_t *uc_get_iterator(void) {
791 uc_iter_t *iter;
793 iter = (uc_iter_t *)calloc(1, sizeof(*iter));
794 if (iter == NULL)
795 return (NULL);
797 pthread_mutex_lock(&cache_lock);
799 iter->iter = c_avl_get_iterator(cache_tree);
800 if (iter->iter == NULL) {
801 free(iter);
802 return (NULL);
803 }
805 return (iter);
806 } /* uc_iter_t *uc_get_iterator */
808 int uc_iterator_next(uc_iter_t *iter, char **ret_name) {
809 int status;
811 if (iter == NULL)
812 return (-1);
814 while ((status = c_avl_iterator_next(iter->iter, (void *)&iter->name,
815 (void *)&iter->entry)) == 0) {
816 if (iter->entry->state == STATE_MISSING)
817 continue;
819 break;
820 }
821 if (status != 0) {
822 iter->name = NULL;
823 iter->entry = NULL;
824 return (-1);
825 }
827 if (ret_name != NULL)
828 *ret_name = iter->name;
830 return (0);
831 } /* int uc_iterator_next */
833 void uc_iterator_destroy(uc_iter_t *iter) {
834 if (iter == NULL)
835 return;
837 c_avl_iterator_destroy(iter->iter);
838 pthread_mutex_unlock(&cache_lock);
840 free(iter);
841 } /* void uc_iterator_destroy */
843 int uc_iterator_get_time(uc_iter_t *iter, cdtime_t *ret_time) {
844 if ((iter == NULL) || (iter->entry == NULL) || (ret_time == NULL))
845 return (-1);
847 *ret_time = iter->entry->last_time;
848 return (0);
849 } /* int uc_iterator_get_name */
851 int uc_iterator_get_values(uc_iter_t *iter, value_t **ret_values,
852 size_t *ret_num) {
853 if ((iter == NULL) || (iter->entry == NULL) || (ret_values == NULL) ||
854 (ret_num == NULL))
855 return (-1);
857 *ret_values =
858 calloc(iter->entry->values_num, sizeof(*iter->entry->values_raw));
859 if (*ret_values == NULL)
860 return (-1);
861 for (size_t i = 0; i < iter->entry->values_num; ++i)
862 *ret_values[i] = iter->entry->values_raw[i];
864 *ret_num = iter->entry->values_num;
866 return (0);
867 } /* int uc_iterator_get_values */
869 int uc_iterator_get_interval(uc_iter_t *iter, cdtime_t *ret_interval) {
870 if ((iter == NULL) || (iter->entry == NULL) || (ret_interval == NULL))
871 return (-1);
873 *ret_interval = iter->entry->interval;
874 return (0);
875 } /* int uc_iterator_get_name */
877 /*
878 * Meta data interface
879 */
880 /* XXX: This function will acquire `cache_lock' but will not free it! */
881 static meta_data_t *uc_get_meta(const value_list_t *vl) /* {{{ */
882 {
883 char name[6 * DATA_MAX_NAME_LEN];
884 cache_entry_t *ce = NULL;
885 int status;
887 status = FORMAT_VL(name, sizeof(name), vl);
888 if (status != 0) {
889 ERROR("utils_cache: uc_get_meta: FORMAT_VL failed.");
890 return (NULL);
891 }
893 pthread_mutex_lock(&cache_lock);
895 status = c_avl_get(cache_tree, name, (void *)&ce);
896 if (status != 0) {
897 pthread_mutex_unlock(&cache_lock);
898 return (NULL);
899 }
900 assert(ce != NULL);
902 if (ce->meta == NULL)
903 ce->meta = meta_data_create();
905 if (ce->meta == NULL)
906 pthread_mutex_unlock(&cache_lock);
908 return (ce->meta);
909 } /* }}} meta_data_t *uc_get_meta */
911 /* Sorry about this preprocessor magic, but it really makes this file much
912 * shorter.. */
913 #define UC_WRAP(wrap_function) \
914 { \
915 meta_data_t *meta; \
916 int status; \
917 meta = uc_get_meta(vl); \
918 if (meta == NULL) \
919 return (-1); \
920 status = wrap_function(meta, key); \
921 pthread_mutex_unlock(&cache_lock); \
922 return (status); \
923 }
924 int uc_meta_data_exists(const value_list_t *vl,
925 const char *key) UC_WRAP(meta_data_exists)
927 int uc_meta_data_delete(const value_list_t *vl,
928 const char *key) UC_WRAP(meta_data_delete)
929 #undef UC_WRAP
931 /* We need a new version of this macro because the following functions take
932 * two argumetns. */
933 #define UC_WRAP(wrap_function) \
934 { \
935 meta_data_t *meta; \
936 int status; \
937 meta = uc_get_meta(vl); \
938 if (meta == NULL) \
939 return (-1); \
940 status = wrap_function(meta, key, value); \
941 pthread_mutex_unlock(&cache_lock); \
942 return (status); \
943 }
944 int uc_meta_data_add_string(const value_list_t *vl, const char *key,
945 const char *value)
946 UC_WRAP(meta_data_add_string) int uc_meta_data_add_signed_int(
947 const value_list_t *vl, const char *key, int64_t value)
948 UC_WRAP(meta_data_add_signed_int) int uc_meta_data_add_unsigned_int(
949 const value_list_t *vl, const char *key, uint64_t value)
950 UC_WRAP(meta_data_add_unsigned_int) int uc_meta_data_add_double(
951 const value_list_t *vl, const char *key, double value)
952 UC_WRAP(meta_data_add_double) int uc_meta_data_add_boolean(
953 const value_list_t *vl, const char *key,
954 _Bool value) UC_WRAP(meta_data_add_boolean)
956 int uc_meta_data_get_string(const value_list_t *vl,
957 const char *key,
958 char **value)
959 UC_WRAP(meta_data_get_string) int uc_meta_data_get_signed_int(
960 const value_list_t *vl, const char *key,
961 int64_t *value)
962 UC_WRAP(meta_data_get_signed_int) int uc_meta_data_get_unsigned_int(
963 const value_list_t *vl, const char *key,
964 uint64_t *value)
965 UC_WRAP(meta_data_get_unsigned_int) int uc_meta_data_get_double(
966 const value_list_t *vl,
967 const char *key, double *value)
968 UC_WRAP(meta_data_get_double) int uc_meta_data_get_boolean(
969 const value_list_t *vl,
970 const char *key, _Bool *value)
971 UC_WRAP(meta_data_get_boolean)
972 #undef UC_WRAP
974 /* vim: set sw=2 ts=8 sts=2 tw=78 : */