1 /**
2 * collectd - src/utils_cache.c
3 * Copyright (C) 2007 Florian octo Forster
4 *
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.
8 *
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.
13 *
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
17 *
18 * Author:
19 * Florian octo Forster <octo at verplant.org>
20 **/
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25 #include "utils_avltree.h"
27 #include <assert.h>
28 #include <pthread.h>
30 typedef struct cache_entry_s
31 {
32 char name[6 * DATA_MAX_NAME_LEN];
33 int values_num;
34 gauge_t *values_gauge;
35 counter_t *values_counter;
36 time_t last_update;
37 } cache_entry_t;
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)
43 {
44 assert ((a != NULL) && (b != NULL));
45 return (strcmp (a->name, b->name));
46 } /* int cache_compare */
48 int uc_init (void)
49 {
50 if (cache_tree == NULL)
51 cache_tree = avl_create ((int (*) (const void *, const void *))
52 cache_compare);
54 return (0);
55 } /* int uc_init */
57 int uc_update (const data_set_t *ds, const value_list_t *vl)
58 {
59 char name[6 * DATA_MAX_NAME_LEN];
60 cache_entry_t *ce = NULL;
62 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
63 {
64 ERROR ("uc_insert: FORMAT_VL failed.");
65 return (-1);
66 }
68 pthread_mutex_lock (&cache_lock);
70 if (avl_get (cache_tree, name, (void *) &ce) == 0)
71 {
72 int i;
74 assert (ce != NULL);
75 assert (ce->values_num == ds->ds_num);
77 if (ce->last_update >= vl->time)
78 {
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);
83 return (-1);
84 }
86 for (i = 0; i < ds->ds_num; i++)
87 {
88 if (ds->ds[i].type == DS_TYPE_COUNTER)
89 {
90 counter_t diff;
92 /* check if the counter has wrapped around */
93 if (vl->values[i].counter < ce->values_counter[i])
94 {
95 if (ce->values_counter[i] <= 4294967295U)
96 diff = (4294967295U - ce->values_counter[i])
97 + vl->values[i].counter;
98 else
99 diff = (18446744073709551615ULL - ce->values_counter[i])
100 + vl->values[i].counter;
101 }
102 else /* counter has NOT wrapped around */
103 {
104 diff = vl->values[i].counter - ce->values_counter[i];
105 }
107 ce->values_gauge[i] = ((double) diff)
108 / ((double) (vl->time - ce->last_update));
109 ce->values_counter[i] = vl->values[i].counter;
110 }
111 else /* if (ds->ds[i].type == DS_TYPE_GAUGE) */
112 {
113 ce->values_gauge[i] = vl->values[i].gauge;
114 }
115 DEBUG ("uc_insert: %s: ds[%i] = %lf", name, i, ce->values_gauge[i]);
116 } /* for (i) */
118 ce->last_update = vl->time;
119 }
120 else /* key is not found */
121 {
122 int i;
123 size_t ce_size = sizeof (cache_entry_t)
124 + ds->ds_num * (sizeof (counter_t) + sizeof (gauge_t));
125 char *key;
127 key = strdup (name);
128 if (key == NULL)
129 {
130 pthread_mutex_unlock (&cache_lock);
131 ERROR ("uc_insert: strdup failed.");
132 return (-1);
133 }
135 ce = (cache_entry_t *) malloc (ce_size);
136 if (ce == NULL)
137 {
138 pthread_mutex_unlock (&cache_lock);
139 ERROR ("uc_insert: malloc (%u) failed.", (unsigned int) ce_size);
140 return (-1);
141 }
143 memset (ce, '\0', ce_size);
145 strncpy (ce->name, name, sizeof (ce->name));
146 ce->name[sizeof (ce->name) - 1] = '\0';
148 ce->values_num = ds->ds_num;
149 ce->values_gauge = (gauge_t *) (ce + 1);
150 ce->values_counter = (counter_t *) (ce->values_gauge + ce->values_num);
152 for (i = 0; i < ds->ds_num; i++)
153 {
154 if (ds->ds[i].type == DS_TYPE_COUNTER)
155 {
156 ce->values_gauge[i] = NAN;
157 ce->values_counter[i] = vl->values[i].counter;
158 }
159 else /* if (ds->ds[i].type == DS_TYPE_GAUGE) */
160 {
161 ce->values_gauge[i] = vl->values[i].gauge;
162 }
163 } /* for (i) */
165 ce->last_update = vl->time;
167 if (avl_insert (cache_tree, key, ce) != 0)
168 {
169 pthread_mutex_unlock (&cache_lock);
170 ERROR ("uc_insert: avl_insert failed.");
171 return (-1);
172 }
174 DEBUG ("uc_insert: Added %s to the cache.", name);
175 } /* if (key is not found) */
177 pthread_mutex_unlock (&cache_lock);
179 return (0);
180 } /* int uc_insert */
182 gauge_t *uc_get_rate (const data_set_t *ds, const value_list_t *vl)
183 {
184 char name[6 * DATA_MAX_NAME_LEN];
185 gauge_t *ret = NULL;
186 cache_entry_t *ce = NULL;
188 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
189 {
190 ERROR ("uc_insert: FORMAT_VL failed.");
191 return (NULL);
192 }
194 pthread_mutex_lock (&cache_lock);
196 if (avl_get (cache_tree, name, (void *) &ce) == 0)
197 {
198 assert (ce != NULL);
199 assert (ce->values_num == ds->ds_num);
201 ret = (gauge_t *) malloc (ce->values_num * sizeof (gauge_t));
202 if (ret == NULL)
203 {
204 ERROR ("uc_get_rate: malloc failed.");
205 }
206 else
207 {
208 memcpy (ret, ce->values_gauge, ce->values_num * sizeof (gauge_t));
209 }
210 }
212 pthread_mutex_unlock (&cache_lock);
214 return (ret);
215 } /* gauge_t *uc_get_rate */
217 /* vim: set sw=2 ts=8 sts=2 tw=78 : */