321747dee17451f53e569e10a92035cadf780d91
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"
30 #include "common.h"
31 #include "plugin.h"
32 #include "utils_avltree.h"
33 #include "utils_cache.h"
34 #include "meta_data.h"
36 #include <assert.h>
37 #include <pthread.h>
39 typedef struct cache_entry_s
40 {
41 char name[6 * DATA_MAX_NAME_LEN];
42 size_t values_num;
43 gauge_t *values_gauge;
44 value_t *values_raw;
45 /* Time contained in the package
46 * (for calculating rates) */
47 cdtime_t last_time;
48 /* Time according to the local clock
49 * (for purging old entries) */
50 cdtime_t last_update;
51 /* Interval in which the data is collected
52 * (for purging old entries) */
53 cdtime_t interval;
54 int state;
55 int hits;
57 /*
58 * +-----+-----+-----+-----+-----+-----+-----+-----+-----+----
59 * ! 0 ! 1 ! 2 ! 3 ! 4 ! 5 ! 6 ! 7 ! 8 ! ...
60 * +-----+-----+-----+-----+-----+-----+-----+-----+-----+----
61 * ! ds0 ! ds1 ! ds2 ! ds0 ! ds1 ! ds2 ! ds0 ! ds1 ! ds2 ! ...
62 * +-----+-----+-----+-----+-----+-----+-----+-----+-----+----
63 * ! t = 0 ! t = 1 ! t = 2 ! ...
64 * +-----------------+-----------------+-----------------+----
65 */
66 gauge_t *history;
67 size_t history_index; /* points to the next position to write to. */
68 size_t history_length;
70 meta_data_t *meta;
71 } cache_entry_t;
73 struct uc_iter_s {
74 c_avl_iterator_t *iter;
76 char *name;
77 cache_entry_t *entry;
78 };
80 static c_avl_tree_t *cache_tree = NULL;
81 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
83 static int cache_compare (const cache_entry_t *a, const cache_entry_t *b)
84 {
85 #if COLLECT_DEBUG
86 assert ((a != NULL) && (b != NULL));
87 #endif
88 return (strcmp (a->name, b->name));
89 } /* int cache_compare */
91 static cache_entry_t *cache_alloc (size_t values_num)
92 {
93 cache_entry_t *ce;
95 ce = calloc (1, sizeof (*ce));
96 if (ce == NULL)
97 {
98 ERROR ("utils_cache: cache_alloc: calloc failed.");
99 return (NULL);
100 }
101 ce->values_num = values_num;
103 ce->values_gauge = calloc (values_num, sizeof (*ce->values_gauge));
104 ce->values_raw = calloc (values_num, sizeof (*ce->values_raw));
105 if ((ce->values_gauge == NULL) || (ce->values_raw == NULL))
106 {
107 sfree (ce->values_gauge);
108 sfree (ce->values_raw);
109 sfree (ce);
110 ERROR ("utils_cache: cache_alloc: calloc failed.");
111 return (NULL);
112 }
114 ce->history = NULL;
115 ce->history_length = 0;
116 ce->meta = NULL;
118 return (ce);
119 } /* cache_entry_t *cache_alloc */
121 static void cache_free (cache_entry_t *ce)
122 {
123 if (ce == NULL)
124 return;
126 sfree (ce->values_gauge);
127 sfree (ce->values_raw);
128 sfree (ce->history);
129 if (ce->meta != NULL)
130 {
131 meta_data_destroy (ce->meta);
132 ce->meta = NULL;
133 }
134 sfree (ce);
135 } /* void cache_free */
137 static void uc_check_range (const data_set_t *ds, cache_entry_t *ce)
138 {
139 size_t i;
141 for (i = 0; i < ds->ds_num; i++)
142 {
143 if (isnan (ce->values_gauge[i]))
144 continue;
145 else if (ce->values_gauge[i] < ds->ds[i].min)
146 ce->values_gauge[i] = NAN;
147 else if (ce->values_gauge[i] > ds->ds[i].max)
148 ce->values_gauge[i] = NAN;
149 }
150 } /* void uc_check_range */
152 static int uc_insert (const data_set_t *ds, const value_list_t *vl,
153 const char *key)
154 {
155 char *key_copy;
156 cache_entry_t *ce;
157 size_t i;
159 /* `cache_lock' has been locked by `uc_update' */
161 key_copy = strdup (key);
162 if (key_copy == NULL)
163 {
164 ERROR ("uc_insert: strdup failed.");
165 return (-1);
166 }
168 ce = cache_alloc (ds->ds_num);
169 if (ce == NULL)
170 {
171 sfree (key_copy);
172 ERROR ("uc_insert: cache_alloc (%zu) failed.", ds->ds_num);
173 return (-1);
174 }
176 sstrncpy (ce->name, key, sizeof (ce->name));
178 for (i = 0; i < ds->ds_num; i++)
179 {
180 switch (ds->ds[i].type)
181 {
182 case DS_TYPE_COUNTER:
183 ce->values_gauge[i] = NAN;
184 ce->values_raw[i].counter = vl->values[i].counter;
185 break;
187 case DS_TYPE_GAUGE:
188 ce->values_gauge[i] = vl->values[i].gauge;
189 ce->values_raw[i].gauge = vl->values[i].gauge;
190 break;
192 case DS_TYPE_DERIVE:
193 ce->values_gauge[i] = NAN;
194 ce->values_raw[i].derive = vl->values[i].derive;
195 break;
197 case DS_TYPE_ABSOLUTE:
198 ce->values_gauge[i] = NAN;
199 if (vl->interval > 0)
200 ce->values_gauge[i] = ((double) vl->values[i].absolute)
201 / CDTIME_T_TO_DOUBLE (vl->interval);
202 ce->values_raw[i].absolute = vl->values[i].absolute;
203 break;
205 default:
206 /* This shouldn't happen. */
207 ERROR ("uc_insert: Don't know how to handle data source type %i.",
208 ds->ds[i].type);
209 sfree (key_copy);
210 cache_free (ce);
211 return (-1);
212 } /* switch (ds->ds[i].type) */
213 } /* for (i) */
215 /* Prune invalid gauge data */
216 uc_check_range (ds, ce);
218 ce->last_time = vl->time;
219 ce->last_update = cdtime ();
220 ce->interval = vl->interval;
221 ce->state = STATE_OKAY;
223 if (c_avl_insert (cache_tree, key_copy, ce) != 0)
224 {
225 sfree (key_copy);
226 ERROR ("uc_insert: c_avl_insert failed.");
227 return (-1);
228 }
230 DEBUG ("uc_insert: Added %s to the cache.", key);
231 return (0);
232 } /* int uc_insert */
234 int uc_init (void)
235 {
236 if (cache_tree == NULL)
237 cache_tree = c_avl_create ((int (*) (const void *, const void *))
238 cache_compare);
240 return (0);
241 } /* int uc_init */
243 int uc_check_timeout (void)
244 {
245 cdtime_t now;
246 cache_entry_t *ce;
248 char **keys = NULL;
249 cdtime_t *keys_time = NULL;
250 cdtime_t *keys_interval = NULL;
251 int keys_len = 0;
253 char *key;
254 c_avl_iterator_t *iter;
256 int status;
257 int i;
259 pthread_mutex_lock (&cache_lock);
261 now = cdtime ();
263 /* Build a list of entries to be flushed */
264 iter = c_avl_get_iterator (cache_tree);
265 while (c_avl_iterator_next (iter, (void *) &key, (void *) &ce) == 0)
266 {
267 char **tmp;
268 cdtime_t *tmp_time;
270 /* If the entry is fresh enough, continue. */
271 if ((now - ce->last_update) < (ce->interval * timeout_g))
272 continue;
274 /* If entry has not been updated, add to `keys' array */
275 tmp = realloc ((void *) keys,
276 (keys_len + 1) * sizeof (char *));
277 if (tmp == NULL)
278 {
279 ERROR ("uc_check_timeout: realloc failed.");
280 continue;
281 }
282 keys = tmp;
284 tmp_time = realloc (keys_time, (keys_len + 1) * sizeof (*keys_time));
285 if (tmp_time == NULL)
286 {
287 ERROR ("uc_check_timeout: realloc failed.");
288 continue;
289 }
290 keys_time = tmp_time;
292 tmp_time = realloc (keys_interval, (keys_len + 1) * sizeof (*keys_interval));
293 if (tmp_time == NULL)
294 {
295 ERROR ("uc_check_timeout: realloc failed.");
296 continue;
297 }
298 keys_interval = tmp_time;
300 keys[keys_len] = strdup (key);
301 if (keys[keys_len] == NULL)
302 {
303 ERROR ("uc_check_timeout: strdup failed.");
304 continue;
305 }
306 keys_time[keys_len] = ce->last_time;
307 keys_interval[keys_len] = ce->interval;
309 keys_len++;
310 } /* while (c_avl_iterator_next) */
312 c_avl_iterator_destroy (iter);
313 pthread_mutex_unlock (&cache_lock);
315 if (keys_len == 0)
316 {
317 /* realloc() may have been called for these. */
318 sfree (keys);
319 sfree (keys_time);
320 sfree (keys_interval);
321 return (0);
322 }
324 /* Call the "missing" callback for each value. Do this before removing the
325 * value from the cache, so that callbacks can still access the data stored,
326 * including plugin specific meta data, rates, history, …. This must be done
327 * without holding the lock, otherwise we will run into a deadlock if a
328 * plugin calls the cache interface. */
329 for (i = 0; i < keys_len; i++)
330 {
331 value_list_t vl = VALUE_LIST_INIT;
333 vl.values = NULL;
334 vl.values_len = 0;
335 vl.meta = NULL;
337 status = parse_identifier_vl (keys[i], &vl);
338 if (status != 0)
339 {
340 ERROR ("uc_check_timeout: parse_identifier_vl (\"%s\") failed.", keys[i]);
341 continue;
342 }
344 vl.time = keys_time[i];
345 vl.interval = keys_interval[i];
347 plugin_dispatch_missing (&vl);
348 } /* for (i = 0; i < keys_len; i++) */
350 /* Now actually remove all the values from the cache. We don't re-evaluate
351 * the timestamp again, so in theory it is possible we remove a value after
352 * it is updated here. */
353 pthread_mutex_lock (&cache_lock);
354 for (i = 0; i < keys_len; i++)
355 {
356 key = NULL;
357 ce = NULL;
359 status = c_avl_remove (cache_tree, keys[i],
360 (void *) &key, (void *) &ce);
361 if (status != 0)
362 {
363 ERROR ("uc_check_timeout: c_avl_remove (\"%s\") failed.", keys[i]);
364 sfree (keys[i]);
365 continue;
366 }
368 sfree (keys[i]);
369 sfree (key);
370 cache_free (ce);
371 } /* for (i = 0; i < keys_len; i++) */
372 pthread_mutex_unlock (&cache_lock);
374 sfree (keys);
375 sfree (keys_time);
376 sfree (keys_interval);
378 return (0);
379 } /* int uc_check_timeout */
381 int uc_update (const data_set_t *ds, const value_list_t *vl)
382 {
383 char name[6 * DATA_MAX_NAME_LEN];
384 cache_entry_t *ce = NULL;
385 int status;
386 size_t i;
388 if (FORMAT_VL (name, sizeof (name), vl) != 0)
389 {
390 ERROR ("uc_update: FORMAT_VL failed.");
391 return (-1);
392 }
394 pthread_mutex_lock (&cache_lock);
396 status = c_avl_get (cache_tree, name, (void *) &ce);
397 if (status != 0) /* entry does not yet exist */
398 {
399 status = uc_insert (ds, vl, name);
400 pthread_mutex_unlock (&cache_lock);
401 return (status);
402 }
404 assert (ce != NULL);
405 assert (ce->values_num == ds->ds_num);
407 if (ce->last_time >= vl->time)
408 {
409 pthread_mutex_unlock (&cache_lock);
410 NOTICE ("uc_update: Value too old: name = %s; value time = %.3f; "
411 "last cache update = %.3f;",
412 name,
413 CDTIME_T_TO_DOUBLE (vl->time),
414 CDTIME_T_TO_DOUBLE (ce->last_time));
415 return (-1);
416 }
418 for (i = 0; i < ds->ds_num; i++)
419 {
420 switch (ds->ds[i].type)
421 {
422 case DS_TYPE_COUNTER:
423 {
424 counter_t diff = counter_diff (ce->values_raw[i].counter, vl->values[i].counter);
425 ce->values_gauge[i] = ((double) diff)
426 / (CDTIME_T_TO_DOUBLE (vl->time - ce->last_time));
427 ce->values_raw[i].counter = vl->values[i].counter;
428 }
429 break;
431 case DS_TYPE_GAUGE:
432 ce->values_raw[i].gauge = vl->values[i].gauge;
433 ce->values_gauge[i] = vl->values[i].gauge;
434 break;
436 case DS_TYPE_DERIVE:
437 {
438 derive_t diff = vl->values[i].derive - ce->values_raw[i].derive;
440 ce->values_gauge[i] = ((double) diff)
441 / (CDTIME_T_TO_DOUBLE (vl->time - ce->last_time));
442 ce->values_raw[i].derive = vl->values[i].derive;
443 }
444 break;
446 case DS_TYPE_ABSOLUTE:
447 ce->values_gauge[i] = ((double) vl->values[i].absolute)
448 / (CDTIME_T_TO_DOUBLE (vl->time - ce->last_time));
449 ce->values_raw[i].absolute = vl->values[i].absolute;
450 break;
452 default:
453 /* This shouldn't happen. */
454 pthread_mutex_unlock (&cache_lock);
455 ERROR ("uc_update: Don't know how to handle data source type %i.",
456 ds->ds[i].type);
457 return (-1);
458 } /* switch (ds->ds[i].type) */
460 DEBUG ("uc_update: %s: ds[%zu] = %lf", name, i, ce->values_gauge[i]);
461 } /* for (i) */
463 /* Update the history if it exists. */
464 if (ce->history != NULL)
465 {
466 assert (ce->history_index < ce->history_length);
467 for (i = 0; i < ce->values_num; i++)
468 {
469 size_t hist_idx = (ce->values_num * ce->history_index) + i;
470 ce->history[hist_idx] = ce->values_gauge[i];
471 }
473 assert (ce->history_length > 0);
474 ce->history_index = (ce->history_index + 1) % ce->history_length;
475 }
477 /* Prune invalid gauge data */
478 uc_check_range (ds, ce);
480 ce->last_time = vl->time;
481 ce->last_update = cdtime ();
482 ce->interval = vl->interval;
484 pthread_mutex_unlock (&cache_lock);
486 return (0);
487 } /* int uc_update */
489 int uc_get_rate_by_name (const char *name, gauge_t **ret_values, size_t *ret_values_num)
490 {
491 gauge_t *ret = NULL;
492 size_t ret_num = 0;
493 cache_entry_t *ce = NULL;
494 int status = 0;
496 pthread_mutex_lock (&cache_lock);
498 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
499 {
500 assert (ce != NULL);
502 /* remove missing values from getval */
503 if (ce->state == STATE_MISSING)
504 {
505 status = -1;
506 }
507 else
508 {
509 ret_num = ce->values_num;
510 ret = malloc (ret_num * sizeof (*ret));
511 if (ret == NULL)
512 {
513 ERROR ("utils_cache: uc_get_rate_by_name: malloc failed.");
514 status = -1;
515 }
516 else
517 {
518 memcpy (ret, ce->values_gauge, ret_num * sizeof (gauge_t));
519 }
520 }
521 }
522 else
523 {
524 DEBUG ("utils_cache: uc_get_rate_by_name: No such value: %s", name);
525 status = -1;
526 }
528 pthread_mutex_unlock (&cache_lock);
530 if (status == 0)
531 {
532 *ret_values = ret;
533 *ret_values_num = ret_num;
534 }
536 return (status);
537 } /* gauge_t *uc_get_rate_by_name */
539 gauge_t *uc_get_rate (const data_set_t *ds, const value_list_t *vl)
540 {
541 char name[6 * DATA_MAX_NAME_LEN];
542 gauge_t *ret = NULL;
543 size_t ret_num = 0;
544 int status;
546 if (FORMAT_VL (name, sizeof (name), vl) != 0)
547 {
548 ERROR ("utils_cache: uc_get_rate: FORMAT_VL failed.");
549 return (NULL);
550 }
552 status = uc_get_rate_by_name (name, &ret, &ret_num);
553 if (status != 0)
554 return (NULL);
556 /* This is important - the caller has no other way of knowing how many
557 * values are returned. */
558 if (ret_num != (size_t) ds->ds_num)
559 {
560 ERROR ("utils_cache: uc_get_rate: ds[%s] has %zu values, "
561 "but uc_get_rate_by_name returned %zu.",
562 ds->type, ds->ds_num, ret_num);
563 sfree (ret);
564 return (NULL);
565 }
567 return (ret);
568 } /* gauge_t *uc_get_rate */
570 size_t uc_get_size (void) {
571 size_t size_arrays = 0;
573 pthread_mutex_lock (&cache_lock);
574 size_arrays = (size_t) c_avl_size (cache_tree);
575 pthread_mutex_unlock (&cache_lock);
577 return (size_arrays);
578 }
580 int uc_get_names (char ***ret_names, cdtime_t **ret_times, size_t *ret_number)
581 {
582 c_avl_iterator_t *iter;
583 char *key;
584 cache_entry_t *value;
586 char **names = NULL;
587 cdtime_t *times = NULL;
588 size_t number = 0;
589 size_t size_arrays = 0;
591 int status = 0;
593 if ((ret_names == NULL) || (ret_number == NULL))
594 return (-1);
596 pthread_mutex_lock (&cache_lock);
598 size_arrays = (size_t) c_avl_size (cache_tree);
599 if (size_arrays < 1)
600 {
601 /* Handle the "no values" case here, to avoid the error message when
602 * calloc() returns NULL. */
603 pthread_mutex_unlock (&cache_lock);
604 return (0);
605 }
607 names = calloc (size_arrays, sizeof (*names));
608 times = calloc (size_arrays, sizeof (*times));
609 if ((names == NULL) || (times == NULL))
610 {
611 ERROR ("uc_get_names: calloc failed.");
612 sfree (names);
613 sfree (times);
614 pthread_mutex_unlock (&cache_lock);
615 return (ENOMEM);
616 }
618 iter = c_avl_get_iterator (cache_tree);
619 while (c_avl_iterator_next (iter, (void *) &key, (void *) &value) == 0)
620 {
621 /* remove missing values when list values */
622 if (value->state == STATE_MISSING)
623 continue;
625 /* c_avl_size does not return a number smaller than the number of elements
626 * returned by c_avl_iterator_next. */
627 assert (number < size_arrays);
629 if (ret_times != NULL)
630 times[number] = value->last_time;
632 names[number] = strdup (key);
633 if (names[number] == NULL)
634 {
635 status = -1;
636 break;
637 }
639 number++;
640 } /* while (c_avl_iterator_next) */
642 c_avl_iterator_destroy (iter);
643 pthread_mutex_unlock (&cache_lock);
645 if (status != 0)
646 {
647 size_t i;
649 for (i = 0; i < number; i++)
650 {
651 sfree (names[i]);
652 }
653 sfree (names);
654 sfree (times);
656 return (-1);
657 }
659 *ret_names = names;
660 if (ret_times != NULL)
661 *ret_times = times;
662 else
663 sfree (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_set_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;
748 tmp = realloc (ce->history, sizeof (*ce->history)
749 * num_steps * ce->values_num);
750 if (tmp == NULL)
751 {
752 pthread_mutex_unlock (&cache_lock);
753 return (-ENOMEM);
754 }
756 for (i = ce->history_length * ce->values_num;
757 i < (num_steps * ce->values_num);
758 i++)
759 tmp[i] = NAN;
761 ce->history = tmp;
762 ce->history_length = num_steps;
763 } /* if (ce->history_length < num_steps) */
765 /* Copy the values to the output buffer. */
766 for (i = 0; i < num_steps; i++)
767 {
768 size_t src_index;
769 size_t dst_index;
771 if (i < ce->history_index)
772 src_index = ce->history_index - (i + 1);
773 else
774 src_index = ce->history_length + ce->history_index - (i + 1);
775 src_index = src_index * num_ds;
777 dst_index = i * num_ds;
779 memcpy (ret_history + dst_index, ce->history + src_index,
780 sizeof (*ret_history) * num_ds);
781 }
783 pthread_mutex_unlock (&cache_lock);
785 return (0);
786 } /* int uc_get_history_by_name */
788 int uc_get_history (const data_set_t *ds, const value_list_t *vl,
789 gauge_t *ret_history, size_t num_steps, size_t num_ds)
790 {
791 char name[6 * DATA_MAX_NAME_LEN];
793 if (FORMAT_VL (name, sizeof (name), vl) != 0)
794 {
795 ERROR ("utils_cache: uc_get_history: FORMAT_VL failed.");
796 return (-1);
797 }
799 return (uc_get_history_by_name (name, ret_history, num_steps, num_ds));
800 } /* int uc_get_history */
802 int uc_get_hits (const data_set_t *ds, const value_list_t *vl)
803 {
804 char name[6 * DATA_MAX_NAME_LEN];
805 cache_entry_t *ce = NULL;
806 int ret = STATE_ERROR;
808 if (FORMAT_VL (name, sizeof (name), vl) != 0)
809 {
810 ERROR ("uc_get_hits: FORMAT_VL failed.");
811 return (STATE_ERROR);
812 }
814 pthread_mutex_lock (&cache_lock);
816 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
817 {
818 assert (ce != NULL);
819 ret = ce->hits;
820 }
822 pthread_mutex_unlock (&cache_lock);
824 return (ret);
825 } /* int uc_get_hits */
827 int uc_set_hits (const data_set_t *ds, const value_list_t *vl, int hits)
828 {
829 char name[6 * DATA_MAX_NAME_LEN];
830 cache_entry_t *ce = NULL;
831 int ret = -1;
833 if (FORMAT_VL (name, sizeof (name), vl) != 0)
834 {
835 ERROR ("uc_set_hits: FORMAT_VL failed.");
836 return (STATE_ERROR);
837 }
839 pthread_mutex_lock (&cache_lock);
841 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
842 {
843 assert (ce != NULL);
844 ret = ce->hits;
845 ce->hits = hits;
846 }
848 pthread_mutex_unlock (&cache_lock);
850 return (ret);
851 } /* int uc_set_hits */
853 int uc_inc_hits (const data_set_t *ds, const value_list_t *vl, int step)
854 {
855 char name[6 * DATA_MAX_NAME_LEN];
856 cache_entry_t *ce = NULL;
857 int ret = -1;
859 if (FORMAT_VL (name, sizeof (name), vl) != 0)
860 {
861 ERROR ("uc_inc_hits: FORMAT_VL failed.");
862 return (STATE_ERROR);
863 }
865 pthread_mutex_lock (&cache_lock);
867 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
868 {
869 assert (ce != NULL);
870 ret = ce->hits;
871 ce->hits = ret + step;
872 }
874 pthread_mutex_unlock (&cache_lock);
876 return (ret);
877 } /* int uc_inc_hits */
879 /*
880 * Iterator interface
881 */
882 uc_iter_t *uc_get_iterator ()
883 {
884 uc_iter_t *iter;
886 iter = (uc_iter_t *) calloc(1, sizeof (*iter));
887 if (iter == NULL)
888 return (NULL);
890 pthread_mutex_lock (&cache_lock);
892 iter->iter = c_avl_get_iterator (cache_tree);
893 if (iter->iter == NULL)
894 {
895 free (iter);
896 return (NULL);
897 }
899 return (iter);
900 } /* uc_iter_t *uc_get_iterator */
902 int uc_iterator_next (uc_iter_t *iter, char **ret_name)
903 {
904 int status;
906 if (iter == NULL)
907 return (-1);
909 while ((status = c_avl_iterator_next (iter->iter,
910 (void *) &iter->name, (void *) &iter->entry)) == 0)
911 {
912 if (iter->entry->state == STATE_MISSING)
913 continue;
915 break;
916 }
917 if (status != 0) {
918 iter->name = NULL;
919 iter->entry = NULL;
920 return (-1);
921 }
923 if (ret_name != NULL)
924 *ret_name = iter->name;
926 return (0);
927 } /* int uc_iterator_next */
929 void uc_iterator_destroy (uc_iter_t *iter)
930 {
931 if (iter == NULL)
932 return;
934 c_avl_iterator_destroy (iter->iter);
935 pthread_mutex_unlock (&cache_lock);
937 free (iter);
938 } /* void uc_iterator_destroy */
940 int uc_iterator_get_time (uc_iter_t *iter, cdtime_t *ret_time)
941 {
942 if ((iter == NULL) || (iter->entry == NULL) || (ret_time == NULL))
943 return (-1);
945 *ret_time = iter->entry->last_time;
946 return (0);
947 } /* int uc_iterator_get_name */
949 int uc_iterator_get_values (uc_iter_t *iter, value_t **ret_values, size_t *ret_num)
950 {
951 size_t i;
953 if ((iter == NULL) || (iter->entry == NULL)
954 || (ret_values == NULL) || (ret_num == NULL))
955 return (-1);
957 *ret_values = calloc (iter->entry->values_num, sizeof(*iter->entry->values_raw));
958 if (*ret_values == NULL)
959 return (-1);
960 for (i = 0; i < iter->entry->values_num; ++i)
961 *ret_values[i] = iter->entry->values_raw[i];
963 *ret_num = iter->entry->values_num;
965 return (0);
966 } /* int uc_iterator_get_values */
968 int uc_iterator_get_interval (uc_iter_t *iter, cdtime_t *ret_interval)
969 {
970 if ((iter == NULL) || (iter->entry == NULL) || (ret_interval == NULL))
971 return (-1);
973 *ret_interval = iter->entry->interval;
974 return (0);
975 } /* int uc_iterator_get_name */
977 /*
978 * Meta data interface
979 */
980 /* XXX: This function will acquire `cache_lock' but will not free it! */
981 static meta_data_t *uc_get_meta (const value_list_t *vl) /* {{{ */
982 {
983 char name[6 * DATA_MAX_NAME_LEN];
984 cache_entry_t *ce = NULL;
985 int status;
987 status = FORMAT_VL (name, sizeof (name), vl);
988 if (status != 0)
989 {
990 ERROR ("utils_cache: uc_get_meta: FORMAT_VL failed.");
991 return (NULL);
992 }
994 pthread_mutex_lock (&cache_lock);
996 status = c_avl_get (cache_tree, name, (void *) &ce);
997 if (status != 0)
998 {
999 pthread_mutex_unlock (&cache_lock);
1000 return (NULL);
1001 }
1002 assert (ce != NULL);
1004 if (ce->meta == NULL)
1005 ce->meta = meta_data_create ();
1007 if (ce->meta == NULL)
1008 pthread_mutex_unlock (&cache_lock);
1010 return (ce->meta);
1011 } /* }}} meta_data_t *uc_get_meta */
1013 /* Sorry about this preprocessor magic, but it really makes this file much
1014 * shorter.. */
1015 #define UC_WRAP(wrap_function) { \
1016 meta_data_t *meta; \
1017 int status; \
1018 meta = uc_get_meta (vl); \
1019 if (meta == NULL) return (-1); \
1020 status = wrap_function (meta, key); \
1021 pthread_mutex_unlock (&cache_lock); \
1022 return (status); \
1023 }
1024 int uc_meta_data_exists (const value_list_t *vl, const char *key)
1025 UC_WRAP (meta_data_exists)
1027 int uc_meta_data_delete (const value_list_t *vl, const char *key)
1028 UC_WRAP (meta_data_delete)
1029 #undef UC_WRAP
1031 /* We need a new version of this macro because the following functions take
1032 * two argumetns. */
1033 #define UC_WRAP(wrap_function) { \
1034 meta_data_t *meta; \
1035 int status; \
1036 meta = uc_get_meta (vl); \
1037 if (meta == NULL) return (-1); \
1038 status = wrap_function (meta, key, value); \
1039 pthread_mutex_unlock (&cache_lock); \
1040 return (status); \
1041 }
1042 int uc_meta_data_add_string (const value_list_t *vl,
1043 const char *key,
1044 const char *value)
1045 UC_WRAP(meta_data_add_string)
1046 int uc_meta_data_add_signed_int (const value_list_t *vl,
1047 const char *key,
1048 int64_t value)
1049 UC_WRAP(meta_data_add_signed_int)
1050 int uc_meta_data_add_unsigned_int (const value_list_t *vl,
1051 const char *key,
1052 uint64_t value)
1053 UC_WRAP(meta_data_add_unsigned_int)
1054 int uc_meta_data_add_double (const value_list_t *vl,
1055 const char *key,
1056 double value)
1057 UC_WRAP(meta_data_add_double)
1058 int uc_meta_data_add_boolean (const value_list_t *vl,
1059 const char *key,
1060 _Bool value)
1061 UC_WRAP(meta_data_add_boolean)
1063 int uc_meta_data_get_string (const value_list_t *vl,
1064 const char *key,
1065 char **value)
1066 UC_WRAP(meta_data_get_string)
1067 int uc_meta_data_get_signed_int (const value_list_t *vl,
1068 const char *key,
1069 int64_t *value)
1070 UC_WRAP(meta_data_get_signed_int)
1071 int uc_meta_data_get_unsigned_int (const value_list_t *vl,
1072 const char *key,
1073 uint64_t *value)
1074 UC_WRAP(meta_data_get_unsigned_int)
1075 int uc_meta_data_get_double (const value_list_t *vl,
1076 const char *key,
1077 double *value)
1078 UC_WRAP(meta_data_get_double)
1079 int uc_meta_data_get_boolean (const value_list_t *vl,
1080 const char *key,
1081 _Bool *value)
1082 UC_WRAP(meta_data_get_boolean)
1083 #undef UC_WRAP
1085 /* vim: set sw=2 ts=8 sts=2 tw=78 : */