2 * collectd - src/utils_cache.c
3 * Copyright (C) 2007 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Florian octo Forster <octo at verplant.org>
25 #include "utils_avltree.h"
30 typedef struct cache_entry_s
32 char name[6 * DATA_MAX_NAME_LEN];
34 gauge_t *values_gauge;
35 counter_t *values_counter;
39 static avl_tree_t *cache_tree = NULL;
40 static pthread_mutex_t cache_lock;
42 static int cache_compare (const cache_entry_t *a, const cache_entry_t *b)
44 assert ((a != NULL) && (b != NULL));
45 return (strcmp (a->name, b->name));
46 } /* int cache_compare */
50 if (cache_tree == NULL)
51 cache_tree = avl_create ((int (*) (const void *, const void *))
57 int uc_update (const data_set_t *ds, const value_list_t *vl)
59 char name[6 * DATA_MAX_NAME_LEN];
60 cache_entry_t *ce = NULL;
62 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
64 ERROR ("uc_insert: FORMAT_VL failed.");
68 pthread_mutex_lock (&cache_lock);
70 if (avl_get (cache_tree, name, (void *) &ce) == 0)
75 assert (ce->values_num == ds->ds_num);
77 if (ce->last_update >= vl->time)
79 pthread_mutex_unlock (&cache_lock);
80 NOTICE ("uc_insert: Value too old: name = %s; value time = %u; "
81 "last cache update = %u;",
82 name, (unsigned int) vl->time, (unsigned int) ce->last_update);
86 for (i = 0; i < ds->ds_num; i++)
88 if (ds->ds[i].type == DS_TYPE_COUNTER)
90 ce->values_gauge[i] = ((double) (vl->values[i].counter
91 - ce->values_counter[i]))
92 / ((double) (vl->time - ce->last_update));
93 ce->values_counter[i] = vl->values[i].counter;
95 else /* if (ds->ds[i].type == DS_TYPE_GAUGE) */
97 ce->values_gauge[i] = vl->values[i].gauge;
99 DEBUG ("uc_insert: %s: ds[%i] = %lf", name, i, ce->values_gauge[i]);
102 ce->last_update = vl->time;
104 else /* key is not found */
107 size_t ce_size = sizeof (cache_entry_t)
108 + ds->ds_num * (sizeof (counter_t) + sizeof (gauge_t));
114 pthread_mutex_unlock (&cache_lock);
115 ERROR ("uc_insert: strdup failed.");
119 ce = (cache_entry_t *) malloc (ce_size);
122 pthread_mutex_unlock (&cache_lock);
123 ERROR ("uc_insert: malloc (%u) failed.", (unsigned int) ce_size);
127 memset (ce, '\0', ce_size);
129 strncpy (ce->name, name, sizeof (ce->name));
130 ce->name[sizeof (ce->name) - 1] = '\0';
132 ce->values_num = ds->ds_num;
133 ce->values_gauge = (gauge_t *) (ce + 1);
134 ce->values_counter = (counter_t *) (ce->values_gauge + ce->values_num);
136 for (i = 0; i < ds->ds_num; i++)
138 if (ds->ds[i].type == DS_TYPE_COUNTER)
140 ce->values_gauge[i] = NAN;
141 ce->values_counter[i] = vl->values[i].counter;
143 else /* if (ds->ds[i].type == DS_TYPE_GAUGE) */
145 ce->values_gauge[i] = vl->values[i].gauge;
149 ce->last_update = vl->time;
151 if (avl_insert (cache_tree, key, ce) != 0)
153 pthread_mutex_unlock (&cache_lock);
154 ERROR ("uc_insert: avl_insert failed.");
158 DEBUG ("uc_insert: Added %s to the cache.", name);
159 } /* if (key is not found) */
161 pthread_mutex_unlock (&cache_lock);
164 } /* int uc_insert */
166 gauge_t *uc_get_rate (const data_set_t *ds, const value_list_t *vl)
168 char name[6 * DATA_MAX_NAME_LEN];
170 cache_entry_t *ce = NULL;
172 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
174 ERROR ("uc_insert: FORMAT_VL failed.");
178 pthread_mutex_lock (&cache_lock);
180 if (avl_get (cache_tree, name, (void *) &ce) == 0)
183 assert (ce->values_num == ds->ds_num);
185 ret = (gauge_t *) malloc (ce->values_num * sizeof (gauge_t));
188 ERROR ("uc_get_rate: malloc failed.");
192 memcpy (ret, ce->values_gauge, ce->values_num * sizeof (gauge_t));
196 pthread_mutex_unlock (&cache_lock);
199 } /* gauge_t *uc_get_rate */
201 /* vim: set sw=2 ts=8 sts=2 tw=78 : */