5ecb83cfa015cc6b4cb606c1ad499676d6ebb2f6
[collectd.git] / src / utils_cache.c
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  **/
21
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25 #include "utils_avltree.h"
26
27 #include <assert.h>
28 #include <pthread.h>
29
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;
38
39 static avl_tree_t     *cache_tree = NULL;
40 static pthread_mutex_t cache_lock;
41
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 */
47
48 int uc_init (void)
49 {
50   if (cache_tree == NULL)
51     cache_tree = avl_create ((int (*) (const void *, const void *))
52         cache_compare);
53
54   return (0);
55 } /* int uc_init */
56
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;
61
62   if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
63   {
64     ERROR ("uc_insert: FORMAT_VL failed.");
65     return (-1);
66   }
67
68   pthread_mutex_lock (&cache_lock);
69
70   if (avl_get (cache_tree, name, (void *) &ce) == 0)
71   {
72     int i;
73
74     assert (ce != NULL);
75     assert (ce->values_num == ds->ds_num);
76
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     }
85
86     for (i = 0; i < ds->ds_num; i++)
87     {
88       if (ds->ds[i].type == DS_TYPE_COUNTER)
89       {
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;
94       }
95       else /* if (ds->ds[i].type == DS_TYPE_GAUGE) */
96       {
97         ce->values_gauge[i] = vl->values[i].gauge;
98       }
99       DEBUG ("uc_insert: %s: ds[%i] = %lf", name, i, ce->values_gauge[i]);
100     } /* for (i) */
101
102     ce->last_update = vl->time;
103   }
104   else /* key is not found */
105   {
106     int i;
107     size_t ce_size = sizeof (cache_entry_t)
108       + ds->ds_num * (sizeof (counter_t) + sizeof (gauge_t));
109     char *key;
110     
111     key = strdup (name);
112     if (key == NULL)
113     {
114       pthread_mutex_unlock (&cache_lock);
115       ERROR ("uc_insert: strdup failed.");
116       return (-1);
117     }
118
119     ce = (cache_entry_t *) malloc (ce_size);
120     if (ce == NULL)
121     {
122       pthread_mutex_unlock (&cache_lock);
123       ERROR ("uc_insert: malloc (%u) failed.", (unsigned int) ce_size);
124       return (-1);
125     }
126
127     memset (ce, '\0', ce_size);
128
129     strncpy (ce->name, name, sizeof (ce->name));
130     ce->name[sizeof (ce->name) - 1] = '\0';
131
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);
135
136     for (i = 0; i < ds->ds_num; i++)
137     {
138       if (ds->ds[i].type == DS_TYPE_COUNTER)
139       {
140         ce->values_gauge[i] = NAN;
141         ce->values_counter[i] = vl->values[i].counter;
142       }
143       else /* if (ds->ds[i].type == DS_TYPE_GAUGE) */
144       {
145         ce->values_gauge[i] = vl->values[i].gauge;
146       }
147     } /* for (i) */
148
149     ce->last_update = vl->time;
150
151     if (avl_insert (cache_tree, key, ce) != 0)
152     {
153       pthread_mutex_unlock (&cache_lock);
154       ERROR ("uc_insert: avl_insert failed.");
155       return (-1);
156     }
157
158     DEBUG ("uc_insert: Added %s to the cache.", name);
159   } /* if (key is not found) */
160
161   pthread_mutex_unlock (&cache_lock);
162
163   return (0);
164 } /* int uc_insert */
165
166 gauge_t *uc_get_rate (const data_set_t *ds, const value_list_t *vl)
167 {
168   char name[6 * DATA_MAX_NAME_LEN];
169   gauge_t *ret = NULL;
170   cache_entry_t *ce = NULL;
171
172   if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
173   {
174     ERROR ("uc_insert: FORMAT_VL failed.");
175     return (NULL);
176   }
177
178   pthread_mutex_lock (&cache_lock);
179
180   if (avl_get (cache_tree, name, (void *) &ce) == 0)
181   {
182     assert (ce != NULL);
183     assert (ce->values_num == ds->ds_num);
184
185     ret = (gauge_t *) malloc (ce->values_num * sizeof (gauge_t));
186     if (ret == NULL)
187     {
188       ERROR ("uc_get_rate: malloc failed.");
189     }
190     else
191     {
192       memcpy (ret, ce->values_gauge, ce->values_num * sizeof (gauge_t));
193     }
194   }
195
196   pthread_mutex_unlock (&cache_lock);
197
198   return (ret);
199 } /* gauge_t *uc_get_rate */
200
201 /* vim: set sw=2 ts=8 sts=2 tw=78 : */