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 = cdtime();
227 struct {
228 char *key;
229 cdtime_t time;
230 cdtime_t interval;
231 } *expired = NULL;
232 size_t expired_num = 0;
234 pthread_mutex_lock(&cache_lock);
236 /* Build a list of entries to be flushed */
237 c_avl_iterator_t *iter = c_avl_get_iterator(cache_tree);
238 char *key = NULL;
239 cache_entry_t *ce = NULL;
240 while (c_avl_iterator_next(iter, (void *)&key, (void *)&ce) == 0) {
241 /* If the entry is fresh enough, continue. */
242 if ((now - ce->last_update) < (ce->interval * timeout_g))
243 continue;
245 void *tmp = realloc(expired, (expired_num + 1) * sizeof(*expired));
246 if (tmp == NULL) {
247 ERROR("uc_check_timeout: realloc failed.");
248 continue;
249 }
250 expired = tmp;
252 expired[expired_num].key = strdup(key);
253 expired[expired_num].time = ce->last_time;
254 expired[expired_num].interval = ce->interval;
256 if (expired[expired_num].key == NULL) {
257 ERROR("uc_check_timeout: strdup failed.");
258 continue;
259 }
261 expired_num++;
262 } /* while (c_avl_iterator_next) */
264 c_avl_iterator_destroy(iter);
265 pthread_mutex_unlock(&cache_lock);
267 if (expired_num == 0) {
268 sfree(expired);
269 return (0);
270 }
272 /* Call the "missing" callback for each value. Do this before removing the
273 * value from the cache, so that callbacks can still access the data stored,
274 * including plugin specific meta data, rates, history, …. This must be done
275 * without holding the lock, otherwise we will run into a deadlock if a
276 * plugin calls the cache interface. */
277 for (size_t i = 0; i < expired_num; i++) {
278 value_list_t vl = {
279 .time = expired[i].time, .interval = expired[i].interval,
280 };
282 if (parse_identifier_vl(expired[i].key, &vl) != 0) {
283 ERROR("uc_check_timeout: parse_identifier_vl (\"%s\") failed.",
284 expired[i].key);
285 continue;
286 }
288 plugin_dispatch_missing(&vl);
289 } /* for (i = 0; i < expired_num; i++) */
291 /* Now actually remove all the values from the cache. We don't re-evaluate
292 * the timestamp again, so in theory it is possible we remove a value after
293 * it is updated here. */
294 pthread_mutex_lock(&cache_lock);
295 for (size_t i = 0; i < expired_num; i++) {
296 char *key = NULL;
297 cache_entry_t *value = NULL;
299 if (c_avl_remove(cache_tree, expired[i].key, (void *)&key,
300 (void *)&value) != 0) {
301 ERROR("uc_check_timeout: c_avl_remove (\"%s\") failed.", expired[i].key);
302 sfree(expired[i].key);
303 continue;
304 }
305 sfree(key);
306 cache_free(value);
308 sfree(expired[i].key);
309 } /* for (i = 0; i < expired_num; i++) */
310 pthread_mutex_unlock(&cache_lock);
312 sfree(expired);
313 return (0);
314 } /* int uc_check_timeout */
316 int uc_update(const data_set_t *ds, const value_list_t *vl) {
317 char name[6 * DATA_MAX_NAME_LEN];
318 cache_entry_t *ce = NULL;
319 int status;
321 if (FORMAT_VL(name, sizeof(name), vl) != 0) {
322 ERROR("uc_update: FORMAT_VL failed.");
323 return (-1);
324 }
326 pthread_mutex_lock(&cache_lock);
328 status = c_avl_get(cache_tree, name, (void *)&ce);
329 if (status != 0) /* entry does not yet exist */
330 {
331 status = uc_insert(ds, vl, name);
332 pthread_mutex_unlock(&cache_lock);
333 return (status);
334 }
336 assert(ce != NULL);
337 assert(ce->values_num == ds->ds_num);
339 if (ce->last_time >= vl->time) {
340 pthread_mutex_unlock(&cache_lock);
341 NOTICE("uc_update: Value too old: name = %s; value time = %.3f; "
342 "last cache update = %.3f;",
343 name, CDTIME_T_TO_DOUBLE(vl->time),
344 CDTIME_T_TO_DOUBLE(ce->last_time));
345 return (-1);
346 }
348 for (size_t i = 0; i < ds->ds_num; i++) {
349 switch (ds->ds[i].type) {
350 case DS_TYPE_COUNTER: {
351 counter_t diff =
352 counter_diff(ce->values_raw[i].counter, vl->values[i].counter);
353 ce->values_gauge[i] =
354 ((double)diff) / (CDTIME_T_TO_DOUBLE(vl->time - ce->last_time));
355 ce->values_raw[i].counter = vl->values[i].counter;
356 } break;
358 case DS_TYPE_GAUGE:
359 ce->values_raw[i].gauge = vl->values[i].gauge;
360 ce->values_gauge[i] = vl->values[i].gauge;
361 break;
363 case DS_TYPE_DERIVE: {
364 derive_t diff = vl->values[i].derive - ce->values_raw[i].derive;
366 ce->values_gauge[i] =
367 ((double)diff) / (CDTIME_T_TO_DOUBLE(vl->time - ce->last_time));
368 ce->values_raw[i].derive = vl->values[i].derive;
369 } break;
371 case DS_TYPE_ABSOLUTE:
372 ce->values_gauge[i] = ((double)vl->values[i].absolute) /
373 (CDTIME_T_TO_DOUBLE(vl->time - ce->last_time));
374 ce->values_raw[i].absolute = vl->values[i].absolute;
375 break;
377 default:
378 /* This shouldn't happen. */
379 pthread_mutex_unlock(&cache_lock);
380 ERROR("uc_update: Don't know how to handle data source type %i.",
381 ds->ds[i].type);
382 return (-1);
383 } /* switch (ds->ds[i].type) */
385 DEBUG("uc_update: %s: ds[%zu] = %lf", name, i, ce->values_gauge[i]);
386 } /* for (i) */
388 /* Update the history if it exists. */
389 if (ce->history != NULL) {
390 assert(ce->history_index < ce->history_length);
391 for (size_t i = 0; i < ce->values_num; i++) {
392 size_t hist_idx = (ce->values_num * ce->history_index) + i;
393 ce->history[hist_idx] = ce->values_gauge[i];
394 }
396 assert(ce->history_length > 0);
397 ce->history_index = (ce->history_index + 1) % ce->history_length;
398 }
400 /* Prune invalid gauge data */
401 uc_check_range(ds, ce);
403 ce->last_time = vl->time;
404 ce->last_update = cdtime();
405 ce->interval = vl->interval;
407 pthread_mutex_unlock(&cache_lock);
409 return (0);
410 } /* int uc_update */
412 int uc_get_rate_by_name(const char *name, gauge_t **ret_values,
413 size_t *ret_values_num) {
414 gauge_t *ret = NULL;
415 size_t ret_num = 0;
416 cache_entry_t *ce = NULL;
417 int status = 0;
419 pthread_mutex_lock(&cache_lock);
421 if (c_avl_get(cache_tree, name, (void *)&ce) == 0) {
422 assert(ce != NULL);
424 /* remove missing values from getval */
425 if (ce->state == STATE_MISSING) {
426 status = -1;
427 } else {
428 ret_num = ce->values_num;
429 ret = malloc(ret_num * sizeof(*ret));
430 if (ret == NULL) {
431 ERROR("utils_cache: uc_get_rate_by_name: malloc failed.");
432 status = -1;
433 } else {
434 memcpy(ret, ce->values_gauge, ret_num * sizeof(gauge_t));
435 }
436 }
437 } else {
438 DEBUG("utils_cache: uc_get_rate_by_name: No such value: %s", name);
439 status = -1;
440 }
442 pthread_mutex_unlock(&cache_lock);
444 if (status == 0) {
445 *ret_values = ret;
446 *ret_values_num = ret_num;
447 }
449 return (status);
450 } /* gauge_t *uc_get_rate_by_name */
452 gauge_t *uc_get_rate(const data_set_t *ds, const value_list_t *vl) {
453 char name[6 * DATA_MAX_NAME_LEN];
454 gauge_t *ret = NULL;
455 size_t ret_num = 0;
456 int status;
458 if (FORMAT_VL(name, sizeof(name), vl) != 0) {
459 ERROR("utils_cache: uc_get_rate: FORMAT_VL failed.");
460 return (NULL);
461 }
463 status = uc_get_rate_by_name(name, &ret, &ret_num);
464 if (status != 0)
465 return (NULL);
467 /* This is important - the caller has no other way of knowing how many
468 * values are returned. */
469 if (ret_num != (size_t)ds->ds_num) {
470 ERROR("utils_cache: uc_get_rate: ds[%s] has %zu values, "
471 "but uc_get_rate_by_name returned %zu.",
472 ds->type, ds->ds_num, ret_num);
473 sfree(ret);
474 return (NULL);
475 }
477 return (ret);
478 } /* gauge_t *uc_get_rate */
480 size_t uc_get_size(void) {
481 size_t size_arrays = 0;
483 pthread_mutex_lock(&cache_lock);
484 size_arrays = (size_t)c_avl_size(cache_tree);
485 pthread_mutex_unlock(&cache_lock);
487 return (size_arrays);
488 }
490 int uc_get_names(char ***ret_names, cdtime_t **ret_times, size_t *ret_number) {
491 c_avl_iterator_t *iter;
492 char *key;
493 cache_entry_t *value;
495 char **names = NULL;
496 cdtime_t *times = NULL;
497 size_t number = 0;
498 size_t size_arrays = 0;
500 int status = 0;
502 if ((ret_names == NULL) || (ret_number == NULL))
503 return (-1);
505 pthread_mutex_lock(&cache_lock);
507 size_arrays = (size_t)c_avl_size(cache_tree);
508 if (size_arrays < 1) {
509 /* Handle the "no values" case here, to avoid the error message when
510 * calloc() returns NULL. */
511 pthread_mutex_unlock(&cache_lock);
512 return (0);
513 }
515 names = calloc(size_arrays, sizeof(*names));
516 times = calloc(size_arrays, sizeof(*times));
517 if ((names == NULL) || (times == NULL)) {
518 ERROR("uc_get_names: calloc failed.");
519 sfree(names);
520 sfree(times);
521 pthread_mutex_unlock(&cache_lock);
522 return (ENOMEM);
523 }
525 iter = c_avl_get_iterator(cache_tree);
526 while (c_avl_iterator_next(iter, (void *)&key, (void *)&value) == 0) {
527 /* remove missing values when list values */
528 if (value->state == STATE_MISSING)
529 continue;
531 /* c_avl_size does not return a number smaller than the number of elements
532 * returned by c_avl_iterator_next. */
533 assert(number < size_arrays);
535 if (ret_times != NULL)
536 times[number] = value->last_time;
538 names[number] = strdup(key);
539 if (names[number] == NULL) {
540 status = -1;
541 break;
542 }
544 number++;
545 } /* while (c_avl_iterator_next) */
547 c_avl_iterator_destroy(iter);
548 pthread_mutex_unlock(&cache_lock);
550 if (status != 0) {
551 for (size_t i = 0; i < number; i++) {
552 sfree(names[i]);
553 }
554 sfree(names);
555 sfree(times);
557 return (-1);
558 }
560 *ret_names = names;
561 if (ret_times != NULL)
562 *ret_times = times;
563 else
564 sfree(times);
565 *ret_number = number;
567 return (0);
568 } /* int uc_get_names */
570 int uc_get_state(const data_set_t *ds, const value_list_t *vl) {
571 char name[6 * DATA_MAX_NAME_LEN];
572 cache_entry_t *ce = NULL;
573 int ret = STATE_ERROR;
575 if (FORMAT_VL(name, sizeof(name), vl) != 0) {
576 ERROR("uc_get_state: FORMAT_VL failed.");
577 return (STATE_ERROR);
578 }
580 pthread_mutex_lock(&cache_lock);
582 if (c_avl_get(cache_tree, name, (void *)&ce) == 0) {
583 assert(ce != NULL);
584 ret = ce->state;
585 }
587 pthread_mutex_unlock(&cache_lock);
589 return (ret);
590 } /* int uc_get_state */
592 int uc_set_state(const data_set_t *ds, const value_list_t *vl, int state) {
593 char name[6 * DATA_MAX_NAME_LEN];
594 cache_entry_t *ce = NULL;
595 int ret = -1;
597 if (FORMAT_VL(name, sizeof(name), vl) != 0) {
598 ERROR("uc_set_state: FORMAT_VL failed.");
599 return (STATE_ERROR);
600 }
602 pthread_mutex_lock(&cache_lock);
604 if (c_avl_get(cache_tree, name, (void *)&ce) == 0) {
605 assert(ce != NULL);
606 ret = ce->state;
607 ce->state = state;
608 }
610 pthread_mutex_unlock(&cache_lock);
612 return (ret);
613 } /* int uc_set_state */
615 int uc_get_history_by_name(const char *name, gauge_t *ret_history,
616 size_t num_steps, size_t num_ds) {
617 cache_entry_t *ce = NULL;
618 int status = 0;
620 pthread_mutex_lock(&cache_lock);
622 status = c_avl_get(cache_tree, name, (void *)&ce);
623 if (status != 0) {
624 pthread_mutex_unlock(&cache_lock);
625 return (-ENOENT);
626 }
628 if (((size_t)ce->values_num) != num_ds) {
629 pthread_mutex_unlock(&cache_lock);
630 return (-EINVAL);
631 }
633 /* Check if there are enough values available. If not, increase the buffer
634 * size. */
635 if (ce->history_length < num_steps) {
636 gauge_t *tmp;
638 tmp =
639 realloc(ce->history, sizeof(*ce->history) * num_steps * ce->values_num);
640 if (tmp == NULL) {
641 pthread_mutex_unlock(&cache_lock);
642 return (-ENOMEM);
643 }
645 for (size_t i = ce->history_length * ce->values_num;
646 i < (num_steps * ce->values_num); i++)
647 tmp[i] = NAN;
649 ce->history = tmp;
650 ce->history_length = num_steps;
651 } /* if (ce->history_length < num_steps) */
653 /* Copy the values to the output buffer. */
654 for (size_t i = 0; i < num_steps; i++) {
655 size_t src_index;
656 size_t dst_index;
658 if (i < ce->history_index)
659 src_index = ce->history_index - (i + 1);
660 else
661 src_index = ce->history_length + ce->history_index - (i + 1);
662 src_index = src_index * num_ds;
664 dst_index = i * num_ds;
666 memcpy(ret_history + dst_index, ce->history + src_index,
667 sizeof(*ret_history) * num_ds);
668 }
670 pthread_mutex_unlock(&cache_lock);
672 return (0);
673 } /* int uc_get_history_by_name */
675 int uc_get_history(const data_set_t *ds, const value_list_t *vl,
676 gauge_t *ret_history, size_t num_steps, size_t num_ds) {
677 char name[6 * DATA_MAX_NAME_LEN];
679 if (FORMAT_VL(name, sizeof(name), vl) != 0) {
680 ERROR("utils_cache: uc_get_history: FORMAT_VL failed.");
681 return (-1);
682 }
684 return (uc_get_history_by_name(name, ret_history, num_steps, num_ds));
685 } /* int uc_get_history */
687 int uc_get_hits(const data_set_t *ds, const value_list_t *vl) {
688 char name[6 * DATA_MAX_NAME_LEN];
689 cache_entry_t *ce = NULL;
690 int ret = STATE_ERROR;
692 if (FORMAT_VL(name, sizeof(name), vl) != 0) {
693 ERROR("uc_get_hits: FORMAT_VL failed.");
694 return (STATE_ERROR);
695 }
697 pthread_mutex_lock(&cache_lock);
699 if (c_avl_get(cache_tree, name, (void *)&ce) == 0) {
700 assert(ce != NULL);
701 ret = ce->hits;
702 }
704 pthread_mutex_unlock(&cache_lock);
706 return (ret);
707 } /* int uc_get_hits */
709 int uc_set_hits(const data_set_t *ds, const value_list_t *vl, int hits) {
710 char name[6 * DATA_MAX_NAME_LEN];
711 cache_entry_t *ce = NULL;
712 int ret = -1;
714 if (FORMAT_VL(name, sizeof(name), vl) != 0) {
715 ERROR("uc_set_hits: FORMAT_VL failed.");
716 return (STATE_ERROR);
717 }
719 pthread_mutex_lock(&cache_lock);
721 if (c_avl_get(cache_tree, name, (void *)&ce) == 0) {
722 assert(ce != NULL);
723 ret = ce->hits;
724 ce->hits = hits;
725 }
727 pthread_mutex_unlock(&cache_lock);
729 return (ret);
730 } /* int uc_set_hits */
732 int uc_inc_hits(const data_set_t *ds, const value_list_t *vl, int step) {
733 char name[6 * DATA_MAX_NAME_LEN];
734 cache_entry_t *ce = NULL;
735 int ret = -1;
737 if (FORMAT_VL(name, sizeof(name), vl) != 0) {
738 ERROR("uc_inc_hits: FORMAT_VL failed.");
739 return (STATE_ERROR);
740 }
742 pthread_mutex_lock(&cache_lock);
744 if (c_avl_get(cache_tree, name, (void *)&ce) == 0) {
745 assert(ce != NULL);
746 ret = ce->hits;
747 ce->hits = ret + step;
748 }
750 pthread_mutex_unlock(&cache_lock);
752 return (ret);
753 } /* int uc_inc_hits */
755 /*
756 * Iterator interface
757 */
758 uc_iter_t *uc_get_iterator(void) {
759 uc_iter_t *iter;
761 iter = (uc_iter_t *)calloc(1, sizeof(*iter));
762 if (iter == NULL)
763 return (NULL);
765 pthread_mutex_lock(&cache_lock);
767 iter->iter = c_avl_get_iterator(cache_tree);
768 if (iter->iter == NULL) {
769 free(iter);
770 return (NULL);
771 }
773 return (iter);
774 } /* uc_iter_t *uc_get_iterator */
776 int uc_iterator_next(uc_iter_t *iter, char **ret_name) {
777 int status;
779 if (iter == NULL)
780 return (-1);
782 while ((status = c_avl_iterator_next(iter->iter, (void *)&iter->name,
783 (void *)&iter->entry)) == 0) {
784 if (iter->entry->state == STATE_MISSING)
785 continue;
787 break;
788 }
789 if (status != 0) {
790 iter->name = NULL;
791 iter->entry = NULL;
792 return (-1);
793 }
795 if (ret_name != NULL)
796 *ret_name = iter->name;
798 return (0);
799 } /* int uc_iterator_next */
801 void uc_iterator_destroy(uc_iter_t *iter) {
802 if (iter == NULL)
803 return;
805 c_avl_iterator_destroy(iter->iter);
806 pthread_mutex_unlock(&cache_lock);
808 free(iter);
809 } /* void uc_iterator_destroy */
811 int uc_iterator_get_time(uc_iter_t *iter, cdtime_t *ret_time) {
812 if ((iter == NULL) || (iter->entry == NULL) || (ret_time == NULL))
813 return (-1);
815 *ret_time = iter->entry->last_time;
816 return (0);
817 } /* int uc_iterator_get_name */
819 int uc_iterator_get_values(uc_iter_t *iter, value_t **ret_values,
820 size_t *ret_num) {
821 if ((iter == NULL) || (iter->entry == NULL) || (ret_values == NULL) ||
822 (ret_num == NULL))
823 return (-1);
825 *ret_values =
826 calloc(iter->entry->values_num, sizeof(*iter->entry->values_raw));
827 if (*ret_values == NULL)
828 return (-1);
829 for (size_t i = 0; i < iter->entry->values_num; ++i)
830 *ret_values[i] = iter->entry->values_raw[i];
832 *ret_num = iter->entry->values_num;
834 return (0);
835 } /* int uc_iterator_get_values */
837 int uc_iterator_get_interval(uc_iter_t *iter, cdtime_t *ret_interval) {
838 if ((iter == NULL) || (iter->entry == NULL) || (ret_interval == NULL))
839 return (-1);
841 *ret_interval = iter->entry->interval;
842 return (0);
843 } /* int uc_iterator_get_name */
845 /*
846 * Meta data interface
847 */
848 /* XXX: This function will acquire `cache_lock' but will not free it! */
849 static meta_data_t *uc_get_meta(const value_list_t *vl) /* {{{ */
850 {
851 char name[6 * DATA_MAX_NAME_LEN];
852 cache_entry_t *ce = NULL;
853 int status;
855 status = FORMAT_VL(name, sizeof(name), vl);
856 if (status != 0) {
857 ERROR("utils_cache: uc_get_meta: FORMAT_VL failed.");
858 return (NULL);
859 }
861 pthread_mutex_lock(&cache_lock);
863 status = c_avl_get(cache_tree, name, (void *)&ce);
864 if (status != 0) {
865 pthread_mutex_unlock(&cache_lock);
866 return (NULL);
867 }
868 assert(ce != NULL);
870 if (ce->meta == NULL)
871 ce->meta = meta_data_create();
873 if (ce->meta == NULL)
874 pthread_mutex_unlock(&cache_lock);
876 return (ce->meta);
877 } /* }}} meta_data_t *uc_get_meta */
879 /* Sorry about this preprocessor magic, but it really makes this file much
880 * shorter.. */
881 #define UC_WRAP(wrap_function) \
882 { \
883 meta_data_t *meta; \
884 int status; \
885 meta = uc_get_meta(vl); \
886 if (meta == NULL) \
887 return (-1); \
888 status = wrap_function(meta, key); \
889 pthread_mutex_unlock(&cache_lock); \
890 return (status); \
891 }
892 int uc_meta_data_exists(const value_list_t *vl,
893 const char *key) UC_WRAP(meta_data_exists)
895 int uc_meta_data_delete(const value_list_t *vl,
896 const char *key) UC_WRAP(meta_data_delete)
897 #undef UC_WRAP
899 /* We need a new version of this macro because the following functions take
900 * two argumetns. */
901 #define UC_WRAP(wrap_function) \
902 { \
903 meta_data_t *meta; \
904 int status; \
905 meta = uc_get_meta(vl); \
906 if (meta == NULL) \
907 return (-1); \
908 status = wrap_function(meta, key, value); \
909 pthread_mutex_unlock(&cache_lock); \
910 return (status); \
911 }
912 int uc_meta_data_add_string(const value_list_t *vl, const char *key,
913 const char *value)
914 UC_WRAP(meta_data_add_string) int uc_meta_data_add_signed_int(
915 const value_list_t *vl, const char *key, int64_t value)
916 UC_WRAP(meta_data_add_signed_int) int uc_meta_data_add_unsigned_int(
917 const value_list_t *vl, const char *key, uint64_t value)
918 UC_WRAP(meta_data_add_unsigned_int) int uc_meta_data_add_double(
919 const value_list_t *vl, const char *key, double value)
920 UC_WRAP(meta_data_add_double) int uc_meta_data_add_boolean(
921 const value_list_t *vl, const char *key,
922 _Bool value) UC_WRAP(meta_data_add_boolean)
924 int uc_meta_data_get_string(const value_list_t *vl,
925 const char *key,
926 char **value)
927 UC_WRAP(meta_data_get_string) int uc_meta_data_get_signed_int(
928 const value_list_t *vl, const char *key,
929 int64_t *value)
930 UC_WRAP(meta_data_get_signed_int) int uc_meta_data_get_unsigned_int(
931 const value_list_t *vl, const char *key,
932 uint64_t *value)
933 UC_WRAP(meta_data_get_unsigned_int) int uc_meta_data_get_double(
934 const value_list_t *vl,
935 const char *key, double *value)
936 UC_WRAP(meta_data_get_double) int uc_meta_data_get_boolean(
937 const value_list_t *vl,
938 const char *key, _Bool *value)
939 UC_WRAP(meta_data_get_boolean)
940 #undef UC_WRAP