]> arthur.barton.de Git - netdata.git/blob - src/rrddim.c
memory mode and health configuration per host
[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->id, 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(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->rrdhost->rrd_memory_mode != RRD_MEMORY_MODE_RAM) {
75         rd = (RRDDIM *)mymmap(fullfilename, size, ((st->rrdhost->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             rd->id = NULL;
80             rd->name = NULL;
81             rd->cache_filename = NULL;
82             rd->memory_mode = st->rrdhost->rrd_memory_mode;
83             rd->flags = 0x00000000;
84             rd->variables = NULL;
85             rd->next = NULL;
86             rd->rrdset = NULL;
87             memset(&rd->avl, 0, sizeof(avl));
88
89             struct timeval now;
90             now_realtime_timeval(&now);
91
92             if(strcmp(rd->magic, RRDDIMENSION_MAGIC) != 0) {
93                 errno = 0;
94                 info("Initializing file %s.", fullfilename);
95                 memset(rd, 0, size);
96             }
97             else if(rd->memsize != size) {
98                 errno = 0;
99                 error("File %s does not have the desired size. Clearing it.", fullfilename);
100                 memset(rd, 0, size);
101             }
102             else if(rd->multiplier != multiplier) {
103                 errno = 0;
104                 error("File %s does not have the same multiplier. Clearing it.", fullfilename);
105                 memset(rd, 0, size);
106             }
107             else if(rd->divisor != divisor) {
108                 errno = 0;
109                 error("File %s does not have the same divisor. Clearing it.", fullfilename);
110                 memset(rd, 0, size);
111             }
112             else if(rd->update_every != st->update_every) {
113                 errno = 0;
114                 error("File %s does not have the same refresh frequency. Clearing it.", fullfilename);
115                 memset(rd, 0, size);
116             }
117             else if(dt_usec(&now, &rd->last_collected_time) > (rd->entries * rd->update_every * USEC_PER_SEC)) {
118                 errno = 0;
119                 error("File %s is too old. Clearing it.", fullfilename);
120                 memset(rd, 0, size);
121             }
122
123             if(rd->algorithm && rd->algorithm != algorithm)
124                 error("File %s does not have the expected algorithm (expected %u '%s', found %u '%s'). Previous values may be wrong."
125                       , fullfilename, algorithm, rrd_algorithm_name(algorithm), rd->algorithm,
126                         rrd_algorithm_name(rd->algorithm));
127         }
128     }
129
130     if(unlikely(!rd)) {
131         // if we didn't manage to get a mmap'd dimension, just create one
132         rd = callocz(1, size);
133         rd->memory_mode = RRD_MEMORY_MODE_RAM;
134     }
135
136     rd->memsize = size;
137
138     strcpy(rd->magic, RRDDIMENSION_MAGIC);
139
140     rd->id = strdupz(id);
141     rd->hash = simple_hash(rd->id);
142
143     rd->cache_filename = strdupz(fullfilename);
144
145     snprintfz(varname, CONFIG_MAX_NAME, "dim %s name", rd->id);
146     rd->name = config_get(st->id, varname, (name && *name)?name:rd->id);
147     rd->hash_name = simple_hash(rd->name);
148
149     snprintfz(varname, CONFIG_MAX_NAME, "dim %s algorithm", rd->id);
150     rd->algorithm = rrd_algorithm_id(config_get(st->id, varname, rrd_algorithm_name(algorithm)));
151
152     snprintfz(varname, CONFIG_MAX_NAME, "dim %s multiplier", rd->id);
153     rd->multiplier = config_get_number(st->id, varname, multiplier);
154
155     snprintfz(varname, CONFIG_MAX_NAME, "dim %s divisor", rd->id);
156     rd->divisor = config_get_number(st->id, varname, divisor);
157     if(!rd->divisor) rd->divisor = 1;
158
159     rd->entries = st->entries;
160     rd->update_every = st->update_every;
161
162     // prevent incremental calculation spikes
163     rd->counter = 0;
164     rrddim_flag_clear(rd, RRDDIM_FLAG_UPDATED);
165     rd->calculated_value = 0;
166     rd->last_calculated_value = 0;
167     rd->collected_value = 0;
168     rd->last_collected_value = 0;
169     rd->collected_volume = 0;
170     rd->stored_volume = 0;
171     rd->last_stored_value = 0;
172     rd->values[st->current_entry] = pack_storage_number(0, SN_NOT_EXISTS);
173     rd->last_collected_time.tv_sec = 0;
174     rd->last_collected_time.tv_usec = 0;
175     rd->rrdset = st;
176
177     // append this dimension
178     pthread_rwlock_wrlock(&st->rwlock);
179     if(!st->dimensions)
180         st->dimensions = rd;
181     else {
182         RRDDIM *td = st->dimensions;
183         for(; td->next; td = td->next) ;
184         td->next = rd;
185     }
186
187     if(st->rrdhost->health_enabled) {
188         rrddimvar_create(rd, RRDVAR_TYPE_CALCULATED, NULL, NULL, &rd->last_stored_value, 0);
189         rrddimvar_create(rd, RRDVAR_TYPE_COLLECTED, NULL, "_raw", &rd->last_collected_value, 0);
190         rrddimvar_create(rd, RRDVAR_TYPE_TIME_T, NULL, "_last_collected_t", &rd->last_collected_time.tv_sec, 0);
191     }
192
193     pthread_rwlock_unlock(&st->rwlock);
194
195     if(unlikely(rrddim_index_add(st, rd) != rd))
196         error("RRDDIM: INTERNAL ERROR: attempt to index duplicate dimension '%s' on chart '%s'", rd->id, st->id);
197
198     return(rd);
199 }
200
201
202 // ----------------------------------------------------------------------------
203 // RRDDIM remove / free a dimension
204
205 void rrddim_free(RRDSET *st, RRDDIM *rd)
206 {
207     debug(D_RRD_CALLS, "rrddim_free() %s.%s", st->name, rd->name);
208
209     if(rd == st->dimensions)
210         st->dimensions = rd->next;
211     else {
212         RRDDIM *i;
213         for (i = st->dimensions; i && i->next != rd; i = i->next) ;
214
215         if (i && i->next == rd)
216             i->next = rd->next;
217         else
218             error("Request to free dimension '%s.%s' but it is not linked.", st->id, rd->name);
219     }
220     rd->next = NULL;
221
222     while(rd->variables)
223         rrddimvar_free(rd->variables);
224
225     if(unlikely(rrddim_index_del(st, rd) != rd))
226         error("RRDDIM: INTERNAL ERROR: attempt to remove from index dimension '%s' on chart '%s', removed a different dimension.", rd->id, st->id);
227
228     // free(rd->annotations);
229
230     switch(rd->memory_mode) {
231         case RRD_MEMORY_MODE_SAVE:
232             debug(D_RRD_CALLS, "Saving dimension '%s' to '%s'.", rd->name, rd->cache_filename);
233             savememory(rd->cache_filename, rd, rd->memsize);
234             // continue to map mode - no break;
235
236         case RRD_MEMORY_MODE_MAP:
237             debug(D_RRD_CALLS, "Unmapping dimension '%s'.", rd->name);
238             freez((void *)rd->id);
239             freez(rd->cache_filename);
240             munmap(rd, rd->memsize);
241             break;
242
243         case RRD_MEMORY_MODE_RAM:
244             debug(D_RRD_CALLS, "Removing dimension '%s'.", rd->name);
245             freez((void *)rd->id);
246             freez(rd->cache_filename);
247             freez(rd);
248             break;
249     }
250 }
251
252
253 // ----------------------------------------------------------------------------
254 // RRDDIM - set dimension options
255
256 int rrddim_hide(RRDSET *st, const char *id) {
257     debug(D_RRD_CALLS, "rrddim_hide() for chart %s, dimension %s", st->name, id);
258
259     RRDDIM *rd = rrddim_find(st, id);
260     if(unlikely(!rd)) {
261         error("Cannot find dimension with id '%s' on stats '%s' (%s).", id, st->name, st->id);
262         return 1;
263     }
264
265     rrddim_flag_set(rd, RRDDIM_FLAG_HIDDEN);
266     return 0;
267 }
268
269 int rrddim_unhide(RRDSET *st, const char *id) {
270     debug(D_RRD_CALLS, "rrddim_unhide() for chart %s, dimension %s", st->name, id);
271
272     RRDDIM *rd = rrddim_find(st, id);
273     if(unlikely(!rd)) {
274         error("Cannot find dimension with id '%s' on stats '%s' (%s).", id, st->name, st->id);
275         return 1;
276     }
277
278     rrddim_flag_clear(rd, RRDDIM_FLAG_HIDDEN);
279     return 0;
280 }
281
282
283 // ----------------------------------------------------------------------------
284 // RRDDIM - collect values for a dimension
285
286 inline collected_number rrddim_set_by_pointer(RRDSET *st, RRDDIM *rd, collected_number value) {
287     debug(D_RRD_CALLS, "rrddim_set_by_pointer() for chart %s, dimension %s, value " COLLECTED_NUMBER_FORMAT, st->name, rd->name, value);
288
289     now_realtime_timeval(&rd->last_collected_time);
290     rd->collected_value = value;
291     rrddim_flag_set(rd, RRDDIM_FLAG_UPDATED);
292     rd->counter++;
293
294     // 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));
295
296     return rd->last_collected_value;
297 }
298
299 collected_number rrddim_set(RRDSET *st, const char *id, collected_number value) {
300     RRDDIM *rd = rrddim_find(st, id);
301     if(unlikely(!rd)) {
302         error("Cannot find dimension with id '%s' on stats '%s' (%s).", id, st->name, st->id);
303         return 0;
304     }
305
306     return rrddim_set_by_pointer(st, rd, value);
307 }