]> arthur.barton.de Git - netdata.git/blob - src/rrddim.c
Merge pull request #1998 from ktsaou/master
[netdata.git] / src / rrddim.c
1 #define NETDATA_RRD_INTERNALS 1
2 #include "common.h"
3
4 // ----------------------------------------------------------------------------
5 // RRDDIM index
6
7 int rrddim_compare(void* a, void* b) {
8     if(((RRDDIM *)a)->hash < ((RRDDIM *)b)->hash) return -1;
9     else if(((RRDDIM *)a)->hash > ((RRDDIM *)b)->hash) return 1;
10     else return strcmp(((RRDDIM *)a)->id, ((RRDDIM *)b)->id);
11 }
12
13 #define rrddim_index_add(st, rd) (RRDDIM *)avl_insert_lock(&((st)->dimensions_index), (avl *)(rd))
14 #define rrddim_index_del(st,rd ) (RRDDIM *)avl_remove_lock(&((st)->dimensions_index), (avl *)(rd))
15
16 static inline RRDDIM *rrddim_index_find(RRDSET *st, const char *id, uint32_t hash) {
17     RRDDIM tmp = {
18             .id = id,
19             .hash = (hash)?hash:simple_hash(id)
20     };
21     return (RRDDIM *)avl_search_lock(&(st->dimensions_index), (avl *) &tmp);
22 }
23
24
25 // ----------------------------------------------------------------------------
26 // RRDDIM - find a dimension
27
28 inline RRDDIM *rrddim_find(RRDSET *st, const char *id) {
29     debug(D_RRD_CALLS, "rrddim_find() for chart %s, dimension %s", st->name, id);
30
31     return rrddim_index_find(st, id, 0);
32 }
33
34
35 // ----------------------------------------------------------------------------
36 // RRDDIM rename a dimension
37
38 inline void rrddim_set_name(RRDSET *st, RRDDIM *rd, const char *name) {
39     if(unlikely(!strcmp(rd->name, name)))
40         return;
41
42     debug(D_RRD_CALLS, "rrddim_set_name() from %s.%s to %s.%s", st->name, rd->name, st->name, name);
43
44     char varname[CONFIG_MAX_NAME + 1];
45     snprintfz(varname, CONFIG_MAX_NAME, "dim %s name", rd->id);
46     rd->name = config_set_default(st->config_section, varname, name);
47     rd->hash_name = simple_hash(rd->name);
48
49     rrddimvar_rename_all(rd);
50 }
51
52
53 // ----------------------------------------------------------------------------
54 // RRDDIM create a dimension
55
56 RRDDIM *rrddim_add(RRDSET *st, const char *id, const char *name, collected_number multiplier, collected_number divisor, RRD_ALGORITHM algorithm) {
57     RRDDIM *rd = rrddim_find(st, id);
58     if(unlikely(rd)) {
59         debug(D_RRD_CALLS, "Cannot create rrd dimension '%s/%s', it already exists.", st->id, name?name:"<NONAME>");
60         return rd;
61     }
62
63     char filename[FILENAME_MAX + 1];
64     char fullfilename[FILENAME_MAX + 1];
65
66     char varname[CONFIG_MAX_NAME + 1];
67     unsigned long size = sizeof(RRDDIM) + (st->entries * sizeof(storage_number));
68
69     debug(D_RRD_CALLS, "Adding dimension '%s/%s'.", st->id, id);
70
71     rrdset_strncpyz_name(filename, id, FILENAME_MAX);
72     snprintfz(fullfilename, FILENAME_MAX, "%s/%s.db", st->cache_dir, filename);
73
74     if(st->rrd_memory_mode == RRD_MEMORY_MODE_SAVE || st->rrd_memory_mode == RRD_MEMORY_MODE_MAP) {
75         rd = (RRDDIM *)mymmap(fullfilename, size, ((st->rrd_memory_mode == RRD_MEMORY_MODE_MAP) ? MAP_SHARED : MAP_PRIVATE), 1);
76         if(likely(rd)) {
77             // we have a file mapped for rd
78
79             memset(&rd->avl, 0, sizeof(avl));
80             rd->id = NULL;
81             rd->name = NULL;
82             rd->cache_filename = NULL;
83             rd->variables = NULL;
84             rd->next = NULL;
85             rd->rrdset = NULL;
86
87             struct timeval now;
88             now_realtime_timeval(&now);
89
90             if(strcmp(rd->magic, RRDDIMENSION_MAGIC) != 0) {
91                 errno = 0;
92                 info("Initializing file %s.", fullfilename);
93                 memset(rd, 0, size);
94             }
95             else if(rd->memsize != size) {
96                 errno = 0;
97                 error("File %s does not have the desired size. Clearing it.", fullfilename);
98                 memset(rd, 0, size);
99             }
100             else if(rd->multiplier != multiplier) {
101                 errno = 0;
102                 error("File %s does not have the same multiplier. Clearing it.", fullfilename);
103                 memset(rd, 0, size);
104             }
105             else if(rd->divisor != divisor) {
106                 errno = 0;
107                 error("File %s does not have the same divisor. Clearing it.", fullfilename);
108                 memset(rd, 0, size);
109             }
110             else if(rd->update_every != st->update_every) {
111                 errno = 0;
112                 error("File %s does not have the same refresh frequency. Clearing it.", fullfilename);
113                 memset(rd, 0, size);
114             }
115             else if(dt_usec(&now, &rd->last_collected_time) > (rd->entries * rd->update_every * USEC_PER_SEC)) {
116                 errno = 0;
117                 error("File %s is too old. Clearing it.", fullfilename);
118                 memset(rd, 0, size);
119             }
120
121             if(rd->algorithm && rd->algorithm != algorithm)
122                 error("File %s does not have the expected algorithm (expected %u '%s', found %u '%s'). Previous values may be wrong."
123                       , fullfilename, algorithm, rrd_algorithm_name(algorithm), rd->algorithm,
124                         rrd_algorithm_name(rd->algorithm));
125
126             // make sure we have the right memory mode
127             // even if we cleared the memory
128             rd->rrd_memory_mode = st->rrd_memory_mode;
129         }
130     }
131
132     if(unlikely(!rd)) {
133         // if we didn't manage to get a mmap'd dimension, just create one
134         rd = callocz(1, size);
135         rd->rrd_memory_mode = (st->rrd_memory_mode == RRD_MEMORY_MODE_NONE) ? RRD_MEMORY_MODE_NONE : RRD_MEMORY_MODE_RAM;
136     }
137
138     rd->memsize = size;
139
140     strcpy(rd->magic, RRDDIMENSION_MAGIC);
141
142     rd->id = strdupz(id);
143     rd->hash = simple_hash(rd->id);
144
145     rd->cache_filename = strdupz(fullfilename);
146
147     snprintfz(varname, CONFIG_MAX_NAME, "dim %s name", rd->id);
148     rd->name = config_get(st->config_section, varname, (name && *name)?name:rd->id);
149     rd->hash_name = simple_hash(rd->name);
150
151     snprintfz(varname, CONFIG_MAX_NAME, "dim %s algorithm", rd->id);
152     rd->algorithm = rrd_algorithm_id(config_get(st->config_section, varname, rrd_algorithm_name(algorithm)));
153
154     snprintfz(varname, CONFIG_MAX_NAME, "dim %s multiplier", rd->id);
155     rd->multiplier = config_get_number(st->config_section, varname, multiplier);
156
157     snprintfz(varname, CONFIG_MAX_NAME, "dim %s divisor", rd->id);
158     rd->divisor = config_get_number(st->config_section, varname, divisor);
159     if(!rd->divisor) rd->divisor = 1;
160
161     rd->entries = st->entries;
162     rd->update_every = st->update_every;
163
164     // prevent incremental calculation spikes
165     rd->collections_counter = 0;
166     rd->updated = 0;
167     rd->flags = 0x00000000;
168
169     rd->calculated_value = 0;
170     rd->last_calculated_value = 0;
171     rd->collected_value = 0;
172     rd->last_collected_value = 0;
173     rd->collected_volume = 0;
174     rd->stored_volume = 0;
175     rd->last_stored_value = 0;
176     rd->values[st->current_entry] = pack_storage_number(0, SN_NOT_EXISTS);
177     rd->last_collected_time.tv_sec = 0;
178     rd->last_collected_time.tv_usec = 0;
179     rd->rrdset = st;
180
181     // append this dimension
182     rrdset_wrlock(st);
183     if(!st->dimensions)
184         st->dimensions = rd;
185     else {
186         RRDDIM *td = st->dimensions;
187         for(; td->next; td = td->next) ;
188         td->next = rd;
189     }
190
191     if(st->rrdhost->health_enabled) {
192         rrddimvar_create(rd, RRDVAR_TYPE_CALCULATED, NULL, NULL, &rd->last_stored_value, 0);
193         rrddimvar_create(rd, RRDVAR_TYPE_COLLECTED, NULL, "_raw", &rd->last_collected_value, 0);
194         rrddimvar_create(rd, RRDVAR_TYPE_TIME_T, NULL, "_last_collected_t", &rd->last_collected_time.tv_sec, 0);
195     }
196
197     rrdset_unlock(st);
198
199     if(unlikely(rrddim_index_add(st, rd) != rd))
200         error("RRDDIM: INTERNAL ERROR: attempt to index duplicate dimension '%s' on chart '%s'", rd->id, st->id);
201
202     return(rd);
203 }
204
205
206 // ----------------------------------------------------------------------------
207 // RRDDIM remove / free a dimension
208
209 void rrddim_free(RRDSET *st, RRDDIM *rd)
210 {
211     debug(D_RRD_CALLS, "rrddim_free() %s.%s", st->name, rd->name);
212
213     if(rd == st->dimensions)
214         st->dimensions = rd->next;
215     else {
216         RRDDIM *i;
217         for (i = st->dimensions; i && i->next != rd; i = i->next) ;
218
219         if (i && i->next == rd)
220             i->next = rd->next;
221         else
222             error("Request to free dimension '%s.%s' but it is not linked.", st->id, rd->name);
223     }
224     rd->next = NULL;
225
226     while(rd->variables)
227         rrddimvar_free(rd->variables);
228
229     if(unlikely(rrddim_index_del(st, rd) != rd))
230         error("RRDDIM: INTERNAL ERROR: attempt to remove from index dimension '%s' on chart '%s', removed a different dimension.", rd->id, st->id);
231
232     // free(rd->annotations);
233
234     switch(rd->rrd_memory_mode) {
235         case RRD_MEMORY_MODE_SAVE:
236             debug(D_RRD_CALLS, "Saving dimension '%s' to '%s'.", rd->name, rd->cache_filename);
237             savememory(rd->cache_filename, rd, rd->memsize);
238             // continue to map mode - no break;
239
240         case RRD_MEMORY_MODE_MAP:
241             debug(D_RRD_CALLS, "Unmapping dimension '%s'.", rd->name);
242             freez((void *)rd->id);
243             freez(rd->cache_filename);
244             munmap(rd, rd->memsize);
245             break;
246
247         case RRD_MEMORY_MODE_NONE:
248         case RRD_MEMORY_MODE_RAM:
249             debug(D_RRD_CALLS, "Removing dimension '%s'.", rd->name);
250             freez((void *)rd->id);
251             freez(rd->cache_filename);
252             freez(rd);
253             break;
254     }
255 }
256
257
258 // ----------------------------------------------------------------------------
259 // RRDDIM - set dimension options
260
261 int rrddim_hide(RRDSET *st, const char *id) {
262     debug(D_RRD_CALLS, "rrddim_hide() for chart %s, dimension %s", st->name, id);
263
264     RRDDIM *rd = rrddim_find(st, id);
265     if(unlikely(!rd)) {
266         error("Cannot find dimension with id '%s' on stats '%s' (%s).", id, st->name, st->id);
267         return 1;
268     }
269
270     rrddim_flag_set(rd, RRDDIM_FLAG_HIDDEN);
271     return 0;
272 }
273
274 int rrddim_unhide(RRDSET *st, const char *id) {
275     debug(D_RRD_CALLS, "rrddim_unhide() for chart %s, dimension %s", st->name, id);
276
277     RRDDIM *rd = rrddim_find(st, id);
278     if(unlikely(!rd)) {
279         error("Cannot find dimension with id '%s' on stats '%s' (%s).", id, st->name, st->id);
280         return 1;
281     }
282
283     rrddim_flag_clear(rd, RRDDIM_FLAG_HIDDEN);
284     return 0;
285 }
286
287
288 // ----------------------------------------------------------------------------
289 // RRDDIM - collect values for a dimension
290
291 inline collected_number rrddim_set_by_pointer(RRDSET *st, RRDDIM *rd, collected_number value) {
292     debug(D_RRD_CALLS, "rrddim_set_by_pointer() for chart %s, dimension %s, value " COLLECTED_NUMBER_FORMAT, st->name, rd->name, value);
293
294     now_realtime_timeval(&rd->last_collected_time);
295     rd->collected_value = value;
296     rd->updated = 1;
297
298     rd->collections_counter++;
299
300     // fprintf(stderr, "%s.%s %llu " COLLECTED_NUMBER_FORMAT " dt %0.6f" " rate " CALCULATED_NUMBER_FORMAT "\n", st->name, rd->name, st->usec_since_last_update, value, (float)((double)st->usec_since_last_update / (double)1000000), (calculated_number)((value - rd->last_collected_value) * (calculated_number)rd->multiplier / (calculated_number)rd->divisor * 1000000.0 / (calculated_number)st->usec_since_last_update));
301
302     return rd->last_collected_value;
303 }
304
305 collected_number rrddim_set(RRDSET *st, const char *id, collected_number value) {
306     RRDDIM *rd = rrddim_find(st, id);
307     if(unlikely(!rd)) {
308         error("Cannot find dimension with id '%s' on stats '%s' (%s).", id, st->name, st->id);
309         return 0;
310     }
311
312     return rrddim_set_by_pointer(st, rd, value);
313 }