]> arthur.barton.de Git - netdata.git/blob - src/rrd.c
Merge remote-tracking branch 'fredericopissarra/changes' into registry
[netdata.git] / src / rrd.c
1 #ifdef HAVE_CONFIG_H
2 #include <config.h>
3 #endif
4 #include <stddef.h>
5 #include <string.h>
6 #include <unistd.h>
7 #include <time.h>
8 #include <sys/time.h>
9 #include <sys/mman.h>
10 #include <pthread.h>
11 #include <errno.h>
12 #include <ctype.h>
13 #include <sys/stat.h>
14 #include <sys/types.h>
15 #include <stdlib.h>
16
17 #include "common.h"
18 #include "log.h"
19 #include "appconfig.h"
20
21 #include "rrd.h"
22
23 #define RRD_DEFAULT_GAP_INTERPOLATIONS 1
24
25 // ----------------------------------------------------------------------------
26 // globals
27
28 // if not zero it gives the time (in seconds) to remove un-updated dimensions
29 // DO NOT ENABLE
30 // if dimensions are removed, the chart generation will have to run again
31 int rrd_delete_unupdated_dimensions = 0;
32
33 int rrd_update_every = UPDATE_EVERY;
34 int rrd_default_history_entries = RRD_DEFAULT_HISTORY_ENTRIES;
35
36 RRDSET *rrdset_root = NULL;
37 pthread_rwlock_t rrdset_root_rwlock = PTHREAD_RWLOCK_INITIALIZER;
38
39 int rrd_memory_mode = RRD_MEMORY_MODE_SAVE;
40
41
42 // ----------------------------------------------------------------------------
43 // RRDSET index
44
45 static int rrdset_iterator(avl *a) { if(a) {}; return 0; }
46
47 static int rrdset_compare(void* a, void* b) {
48         if(((RRDSET *)a)->hash < ((RRDSET *)b)->hash) return -1;
49         else if(((RRDSET *)a)->hash > ((RRDSET *)b)->hash) return 1;
50         else return strcmp(((RRDSET *)a)->id, ((RRDSET *)b)->id);
51 }
52
53 avl_tree_lock rrdset_root_index = {
54                 { NULL, rrdset_compare },
55                 AVL_LOCK_INITIALIZER
56 };
57
58 #define rrdset_index_add(st) avl_insert_lock(&rrdset_root_index, (avl *)(st))
59 #define rrdset_index_del(st) avl_remove_lock(&rrdset_root_index, (avl *)(st))
60
61 static RRDSET *rrdset_index_find(const char *id, uint32_t hash) {
62         RRDSET *result = NULL, tmp;
63         strncpyz(tmp.id, id, RRD_ID_LENGTH_MAX);
64         tmp.hash = (hash)?hash:simple_hash(tmp.id);
65
66         avl_search_lock(&(rrdset_root_index), (avl *) &tmp, rrdset_iterator, (avl **) &result);
67         return result;
68 }
69
70 // ----------------------------------------------------------------------------
71 // RRDSET name index
72
73 #define rrdset_from_avlname(avlname_ptr) ((RRDSET *)((avlname_ptr) - offsetof(RRDSET, avlname)))
74
75 static int rrdset_iterator_name(avl *a) { if(a) {}; return 0; }
76
77 static int rrdset_compare_name(void* a, void* b) {
78         RRDSET *A = rrdset_from_avlname(a);
79         RRDSET *B = rrdset_from_avlname(b);
80
81         // fprintf(stderr, "COMPARING: %s with %s\n", A->name, B->name);
82
83         if(A->hash_name < B->hash_name) return -1;
84         else if(A->hash_name > B->hash_name) return 1;
85         else return strcmp(A->name, B->name);
86 }
87
88 avl_tree_lock rrdset_root_index_name = {
89                 { NULL, rrdset_compare_name },
90                 AVL_LOCK_INITIALIZER
91 };
92
93 int rrdset_index_add_name(RRDSET *st) {
94         // fprintf(stderr, "ADDING: %s (name: %s)\n", st->id, st->name);
95         return avl_insert_lock(&rrdset_root_index_name, (avl *) (&st->avlname));
96 }
97
98 #define rrdset_index_del_name(st) avl_remove_lock(&rrdset_root_index_name, (avl *)(&st->avlname))
99
100 static RRDSET *rrdset_index_find_name(const char *name, uint32_t hash) {
101         void *result = NULL;
102         RRDSET tmp;
103         tmp.name = name;
104         tmp.hash_name = (hash)?hash:simple_hash(tmp.name);
105
106         // fprintf(stderr, "SEARCHING: %s\n", name);
107         avl_search_lock(&(rrdset_root_index_name), (avl *) (&(tmp.avlname)), rrdset_iterator_name, (avl **) &result);
108         if(result) {
109                 RRDSET *st = rrdset_from_avlname(result);
110                 if(strcmp(st->magic, RRDSET_MAGIC))
111                         error("Search for RRDSET %s returned an invalid RRDSET %s (name %s)", name, st->id, st->name);
112
113                 // fprintf(stderr, "FOUND: %s\n", name);
114                 return rrdset_from_avlname(result);
115         }
116         // fprintf(stderr, "NOT FOUND: %s\n", name);
117         return NULL;
118 }
119
120
121 // ----------------------------------------------------------------------------
122 // RRDDIM index
123
124 static int rrddim_iterator(avl *a) { if(a) {}; return 0; }
125
126 static int rrddim_compare(void* a, void* b) {
127         if(((RRDDIM *)a)->hash < ((RRDDIM *)b)->hash) return -1;
128         else if(((RRDDIM *)a)->hash > ((RRDDIM *)b)->hash) return 1;
129         else return strcmp(((RRDDIM *)a)->id, ((RRDDIM *)b)->id);
130 }
131
132 #define rrddim_index_add(st, rd) avl_insert_lock(&((st)->dimensions_index), (avl *)(rd))
133 #define rrddim_index_del(st,rd ) avl_remove_lock(&((st)->dimensions_index), (avl *)(rd))
134
135 static RRDDIM *rrddim_index_find(RRDSET *st, const char *id, uint32_t hash) {
136         RRDDIM *result = NULL, tmp;
137         strncpyz(tmp.id, id, RRD_ID_LENGTH_MAX);
138         tmp.hash = (hash)?hash:simple_hash(tmp.id);
139
140         avl_search_lock(&(st->dimensions_index), (avl *) &tmp, rrddim_iterator, (avl **) &result);
141         return result;
142 }
143
144 // ----------------------------------------------------------------------------
145 // chart types
146
147 int rrdset_type_id(const char *name)
148 {
149         if(unlikely(strcmp(name, RRDSET_TYPE_AREA_NAME) == 0)) return RRDSET_TYPE_AREA;
150         else if(unlikely(strcmp(name, RRDSET_TYPE_STACKED_NAME) == 0)) return RRDSET_TYPE_STACKED;
151         else if(unlikely(strcmp(name, RRDSET_TYPE_LINE_NAME) == 0)) return RRDSET_TYPE_LINE;
152         return RRDSET_TYPE_LINE;
153 }
154
155 const char *rrdset_type_name(int chart_type)
156 {
157         static char line[] = RRDSET_TYPE_LINE_NAME;
158         static char area[] = RRDSET_TYPE_AREA_NAME;
159         static char stacked[] = RRDSET_TYPE_STACKED_NAME;
160
161         switch(chart_type) {
162                 case RRDSET_TYPE_LINE:
163                         return line;
164
165                 case RRDSET_TYPE_AREA:
166                         return area;
167
168                 case RRDSET_TYPE_STACKED:
169                         return stacked;
170         }
171         return line;
172 }
173
174 // ----------------------------------------------------------------------------
175 // load / save
176
177 const char *rrd_memory_mode_name(int id)
178 {
179         static const char ram[] = RRD_MEMORY_MODE_RAM_NAME;
180         static const char map[] = RRD_MEMORY_MODE_MAP_NAME;
181         static const char save[] = RRD_MEMORY_MODE_SAVE_NAME;
182
183         switch(id) {
184                 case RRD_MEMORY_MODE_RAM:
185                         return ram;
186
187                 case RRD_MEMORY_MODE_MAP:
188                         return map;
189
190                 case RRD_MEMORY_MODE_SAVE:
191                 default:
192                         return save;
193         }
194
195         return save;
196 }
197
198 int rrd_memory_mode_id(const char *name)
199 {
200         if(unlikely(!strcmp(name, RRD_MEMORY_MODE_RAM_NAME)))
201                 return RRD_MEMORY_MODE_RAM;
202         else if(unlikely(!strcmp(name, RRD_MEMORY_MODE_MAP_NAME)))
203                 return RRD_MEMORY_MODE_MAP;
204
205         return RRD_MEMORY_MODE_SAVE;
206 }
207
208 // ----------------------------------------------------------------------------
209 // algorithms types
210
211 int rrddim_algorithm_id(const char *name)
212 {
213         if(strcmp(name, RRDDIM_INCREMENTAL_NAME) == 0)                  return RRDDIM_INCREMENTAL;
214         if(strcmp(name, RRDDIM_ABSOLUTE_NAME) == 0)                     return RRDDIM_ABSOLUTE;
215         if(strcmp(name, RRDDIM_PCENT_OVER_ROW_TOTAL_NAME) == 0)                 return RRDDIM_PCENT_OVER_ROW_TOTAL;
216         if(strcmp(name, RRDDIM_PCENT_OVER_DIFF_TOTAL_NAME) == 0)        return RRDDIM_PCENT_OVER_DIFF_TOTAL;
217         return RRDDIM_ABSOLUTE;
218 }
219
220 const char *rrddim_algorithm_name(int chart_type)
221 {
222         static char absolute[] = RRDDIM_ABSOLUTE_NAME;
223         static char incremental[] = RRDDIM_INCREMENTAL_NAME;
224         static char percentage_of_absolute_row[] = RRDDIM_PCENT_OVER_ROW_TOTAL_NAME;
225         static char percentage_of_incremental_row[] = RRDDIM_PCENT_OVER_DIFF_TOTAL_NAME;
226
227         switch(chart_type) {
228                 case RRDDIM_ABSOLUTE:
229                         return absolute;
230
231                 case RRDDIM_INCREMENTAL:
232                         return incremental;
233
234                 case RRDDIM_PCENT_OVER_ROW_TOTAL:
235                         return percentage_of_absolute_row;
236
237                 case RRDDIM_PCENT_OVER_DIFF_TOTAL:
238                         return percentage_of_incremental_row;
239         }
240         return absolute;
241 }
242
243 // ----------------------------------------------------------------------------
244 // chart names
245
246 char *rrdset_strncpy_name(char *to, const char *from, int length)
247 {
248         int i;
249         for(i = 0; i < length && from[i] ;i++) {
250                 if(from[i] == '.' || isalpha(from[i]) || isdigit(from[i])) to[i] = from[i];
251                 else to[i] = '_';
252         }
253         if(i < length) to[i] = '\0';
254         to[length - 1] = '\0';
255
256         return to;
257 }
258
259 void rrdset_set_name(RRDSET *st, const char *name)
260 {
261         debug(D_RRD_CALLS, "rrdset_set_name() old: %s, new: %s", st->name, name);
262
263         if(st->name) rrdset_index_del_name(st);
264
265         char b[CONFIG_MAX_VALUE + 1];
266         char n[RRD_ID_LENGTH_MAX + 1];
267
268         snprintfz(n, RRD_ID_LENGTH_MAX, "%s.%s", st->type, name);
269         rrdset_strncpy_name(b, n, CONFIG_MAX_VALUE);
270         st->name = config_get(st->id, "name", b);
271         st->hash_name = simple_hash(st->name);
272
273         rrdset_index_add_name(st);
274 }
275
276 // ----------------------------------------------------------------------------
277 // cache directory
278
279 char *rrdset_cache_dir(const char *id)
280 {
281         char *ret = NULL;
282
283         static char *cache_dir = NULL;
284         if(!cache_dir) cache_dir = config_get("global", "cache directory", CACHE_DIR);
285
286         char b[FILENAME_MAX + 1];
287         char n[FILENAME_MAX + 1];
288         rrdset_strncpy_name(b, id, FILENAME_MAX);
289
290         snprintfz(n, FILENAME_MAX, "%s/%s", cache_dir, b);
291         ret = config_get(id, "cache directory", n);
292
293         if(rrd_memory_mode == RRD_MEMORY_MODE_MAP || rrd_memory_mode == RRD_MEMORY_MODE_SAVE) {
294                 int r = mkdir(ret, 0775);
295                 if(r != 0 && errno != EEXIST)
296                         error("Cannot create directory '%s'", ret);
297         }
298
299         return ret;
300 }
301
302 // ----------------------------------------------------------------------------
303 // core functions
304
305 void rrdset_reset(RRDSET *st)
306 {
307         debug(D_RRD_CALLS, "rrdset_reset() %s", st->name);
308
309         st->last_collected_time.tv_sec = 0;
310         st->last_collected_time.tv_usec = 0;
311         st->last_updated.tv_sec = 0;
312         st->last_updated.tv_usec = 0;
313         st->current_entry = 0;
314         st->counter = 0;
315         st->counter_done = 0;
316
317         RRDDIM *rd;
318         for(rd = st->dimensions; rd ; rd = rd->next) {
319                 rd->last_collected_time.tv_sec = 0;
320                 rd->last_collected_time.tv_usec = 0;
321                 rd->counter = 0;
322                 bzero(rd->values, rd->entries * sizeof(storage_number));
323         }
324 }
325
326 RRDSET *rrdset_create(const char *type, const char *id, const char *name, const char *family, const char *context, const char *title, const char *units, long priority, int update_every, int chart_type)
327 {
328         if(!type || !type[0]) {
329                 fatal("Cannot create rrd stats without a type.");
330                 return NULL;
331         }
332
333         if(!id || !id[0]) {
334                 fatal("Cannot create rrd stats without an id.");
335                 return NULL;
336         }
337
338         char fullid[RRD_ID_LENGTH_MAX + 1];
339         char fullfilename[FILENAME_MAX + 1];
340         RRDSET *st = NULL;
341
342         snprintfz(fullid, RRD_ID_LENGTH_MAX, "%s.%s", type, id);
343
344         st = rrdset_find(fullid);
345         if(st) {
346                 error("Cannot create rrd stats for '%s', it already exists.", fullid);
347                 return st;
348         }
349
350         long entries = config_get_number(fullid, "history", rrd_default_history_entries);
351         if(entries < 5) entries = config_set_number(fullid, "history", 5);
352         if(entries > RRD_HISTORY_ENTRIES_MAX) entries = config_set_number(fullid, "history", RRD_HISTORY_ENTRIES_MAX);
353
354         int enabled = config_get_boolean(fullid, "enabled", 1);
355         if(!enabled) entries = 5;
356
357         unsigned long size = sizeof(RRDSET);
358         char *cache_dir = rrdset_cache_dir(fullid);
359
360         debug(D_RRD_CALLS, "Creating RRD_STATS for '%s.%s'.", type, id);
361
362         snprintfz(fullfilename, FILENAME_MAX, "%s/main.db", cache_dir);
363         if(rrd_memory_mode != RRD_MEMORY_MODE_RAM) st = (RRDSET *)mymmap(fullfilename, size, ((rrd_memory_mode == RRD_MEMORY_MODE_MAP)?MAP_SHARED:MAP_PRIVATE), 0);
364         if(st) {
365                 if(strcmp(st->magic, RRDSET_MAGIC) != 0) {
366                         errno = 0;
367                         info("Initializing file %s.", fullfilename);
368                         bzero(st, size);
369                 }
370                 else if(strcmp(st->id, fullid) != 0) {
371                         errno = 0;
372                         error("File %s contents are not for chart %s. Clearing it.", fullfilename, fullid);
373                         // munmap(st, size);
374                         // st = NULL;
375                         bzero(st, size);
376                 }
377                 else if(st->memsize != size || st->entries != entries) {
378                         errno = 0;
379                         error("File %s does not have the desired size. Clearing it.", fullfilename);
380                         bzero(st, size);
381                 }
382                 else if(st->update_every != update_every) {
383                         errno = 0;
384                         error("File %s does not have the desired update frequency. Clearing it.", fullfilename);
385                         bzero(st, size);
386                 }
387                 else if((time(NULL) - st->last_updated.tv_sec) > update_every * entries) {
388                         errno = 0;
389                         error("File %s is too old. Clearing it.", fullfilename);
390                         bzero(st, size);
391                 }
392         }
393
394         if(st) {
395                 st->name = NULL;
396                 st->type = NULL;
397                 st->family = NULL;
398                 st->context = NULL;
399                 st->title = NULL;
400                 st->units = NULL;
401                 st->dimensions = NULL;
402                 st->next = NULL;
403                 st->mapped = rrd_memory_mode;
404         }
405         else {
406                 st = calloc(1, size);
407                 if(!st) {
408                         fatal("Cannot allocate memory for RRD_STATS %s.%s", type, id);
409                         return NULL;
410                 }
411                 st->mapped = RRD_MEMORY_MODE_RAM;
412         }
413         st->memsize = size;
414         st->entries = entries;
415         st->update_every = update_every;
416
417         strcpy(st->cache_filename, fullfilename);
418         strcpy(st->magic, RRDSET_MAGIC);
419
420         strcpy(st->id, fullid);
421         st->hash = simple_hash(st->id);
422
423         st->cache_dir = cache_dir;
424
425         st->chart_type = rrdset_type_id(config_get(st->id, "chart type", rrdset_type_name(chart_type)));
426         st->type       = config_get(st->id, "type", type);
427         st->family     = config_get(st->id, "family", family?family:st->type);
428         st->context    = config_get(st->id, "context", context?context:st->id);
429         st->units      = config_get(st->id, "units", units?units:"");
430
431         st->priority = config_get_number(st->id, "priority", priority);
432         st->enabled = enabled;
433
434         st->isdetail = 0;
435         st->debug = 0;
436
437         st->last_collected_time.tv_sec = 0;
438         st->last_collected_time.tv_usec = 0;
439         st->counter_done = 0;
440
441         st->gap_when_lost_iterations_above = (int) (
442                         config_get_number(st->id, "gap when lost iterations above", RRD_DEFAULT_GAP_INTERPOLATIONS) + 2);
443
444         avl_init_lock(&st->dimensions_index, rrddim_compare);
445
446         pthread_rwlock_init(&st->rwlock, NULL);
447         pthread_rwlock_wrlock(&rrdset_root_rwlock);
448
449         if(name && *name) rrdset_set_name(st, name);
450         else rrdset_set_name(st, id);
451
452         {
453                 char varvalue[CONFIG_MAX_VALUE + 1];
454                 snprintfz(varvalue, CONFIG_MAX_VALUE, "%s (%s)", title?title:"", st->name);
455                 st->title = config_get(st->id, "title", varvalue);
456         }
457
458         st->next = rrdset_root;
459         rrdset_root = st;
460
461         rrdset_index_add(st);
462
463         pthread_rwlock_unlock(&rrdset_root_rwlock);
464
465         return(st);
466 }
467
468 RRDDIM *rrddim_add(RRDSET *st, const char *id, const char *name, long multiplier, long divisor, int algorithm)
469 {
470         char filename[FILENAME_MAX + 1];
471         char fullfilename[FILENAME_MAX + 1];
472
473         char varname[CONFIG_MAX_NAME + 1];
474         RRDDIM *rd = NULL;
475         unsigned long size = sizeof(RRDDIM) + (st->entries * sizeof(storage_number));
476
477         debug(D_RRD_CALLS, "Adding dimension '%s/%s'.", st->id, id);
478
479         rrdset_strncpy_name(filename, id, FILENAME_MAX);
480         snprintfz(fullfilename, FILENAME_MAX, "%s/%s.db", st->cache_dir, filename);
481         if(rrd_memory_mode != RRD_MEMORY_MODE_RAM) rd = (RRDDIM *)mymmap(fullfilename, size, ((rrd_memory_mode == RRD_MEMORY_MODE_MAP)?MAP_SHARED:MAP_PRIVATE), 1);
482         if(rd) {
483                 struct timeval now;
484                 gettimeofday(&now, NULL);
485
486                 if(strcmp(rd->magic, RRDDIMENSION_MAGIC) != 0) {
487                         errno = 0;
488                         info("Initializing file %s.", fullfilename);
489                         bzero(rd, size);
490                 }
491                 else if(rd->memsize != size) {
492                         errno = 0;
493                         error("File %s does not have the desired size. Clearing it.", fullfilename);
494                         bzero(rd, size);
495                 }
496                 else if(rd->multiplier != multiplier) {
497                         errno = 0;
498                         error("File %s does not have the same multiplier. Clearing it.", fullfilename);
499                         bzero(rd, size);
500                 }
501                 else if(rd->divisor != divisor) {
502                         errno = 0;
503                         error("File %s does not have the same divisor. Clearing it.", fullfilename);
504                         bzero(rd, size);
505                 }
506                 else if(rd->algorithm != algorithm) {
507                         errno = 0;
508                         error("File %s does not have the same algorithm. Clearing it.", fullfilename);
509                         bzero(rd, size);
510                 }
511                 else if(rd->update_every != st->update_every) {
512                         errno = 0;
513                         error("File %s does not have the same refresh frequency. Clearing it.", fullfilename);
514                         bzero(rd, size);
515                 }
516                 else if(usecdiff(&now, &rd->last_collected_time) > (rd->entries * rd->update_every * 1000000ULL)) {
517                         errno = 0;
518                         error("File %s is too old. Clearing it.", fullfilename);
519                         bzero(rd, size);
520                 }
521                 else if(strcmp(rd->id, id) != 0) {
522                         errno = 0;
523                         error("File %s contents are not for dimension %s. Clearing it.", fullfilename, id);
524                         // munmap(rd, size);
525                         // rd = NULL;
526                         bzero(rd, size);
527                 }
528         }
529
530         if(rd) {
531                 // we have a file mapped for rd
532                 rd->mapped = rrd_memory_mode;
533                 rd->flags = 0x00000000;
534                 rd->next = NULL;
535                 rd->name = NULL;
536         }
537         else {
538                 // if we didn't manage to get a mmap'd dimension, just create one
539
540                 rd = calloc(1, size);
541                 if(!rd) {
542                         fatal("Cannot allocate RRD_DIMENSION %s/%s.", st->id, id);
543                         return NULL;
544                 }
545
546                 rd->mapped = RRD_MEMORY_MODE_RAM;
547         }
548         rd->memsize = size;
549
550         strcpy(rd->magic, RRDDIMENSION_MAGIC);
551         strcpy(rd->cache_filename, fullfilename);
552         strncpyz(rd->id, id, RRD_ID_LENGTH_MAX);
553         rd->hash = simple_hash(rd->id);
554
555         snprintfz(varname, CONFIG_MAX_NAME, "dim %s name", rd->id);
556         rd->name = config_get(st->id, varname, (name && *name)?name:rd->id);
557
558         snprintfz(varname, CONFIG_MAX_NAME, "dim %s algorithm", rd->id);
559         rd->algorithm = rrddim_algorithm_id(config_get(st->id, varname, rrddim_algorithm_name(algorithm)));
560
561         snprintfz(varname, CONFIG_MAX_NAME, "dim %s multiplier", rd->id);
562         rd->multiplier = config_get_number(st->id, varname, multiplier);
563
564         snprintfz(varname, CONFIG_MAX_NAME, "dim %s divisor", rd->id);
565         rd->divisor = config_get_number(st->id, varname, divisor);
566         if(!rd->divisor) rd->divisor = 1;
567
568         rd->entries = st->entries;
569         rd->update_every = st->update_every;
570
571         // prevent incremental calculation spikes
572         rd->counter = 0;
573
574         // append this dimension
575         pthread_rwlock_wrlock(&st->rwlock);
576         if(!st->dimensions)
577                 st->dimensions = rd;
578         else {
579                 RRDDIM *td = st->dimensions;
580                 for(; td->next; td = td->next) ;
581                 td->next = rd;
582         }
583         pthread_rwlock_unlock(&st->rwlock);
584
585         rrddim_index_add(st, rd);
586
587         return(rd);
588 }
589
590 void rrddim_set_name(RRDSET *st, RRDDIM *rd, const char *name)
591 {
592         debug(D_RRD_CALLS, "rrddim_set_name() %s.%s", st->name, rd->name);
593
594         char varname[CONFIG_MAX_NAME + 1];
595         snprintfz(varname, CONFIG_MAX_NAME, "dim %s name", rd->id);
596         config_set_default(st->id, varname, name);
597 }
598
599 void rrddim_free(RRDSET *st, RRDDIM *rd)
600 {
601         debug(D_RRD_CALLS, "rrddim_free() %s.%s", st->name, rd->name);
602
603         RRDDIM *i, *last = NULL;
604         for(i = st->dimensions; i && i != rd ; i = i->next) last = i;
605
606         if(!i) {
607                 error("Request to free dimension %s.%s but it is not linked.", st->id, rd->name);
608                 return;
609         }
610
611         if(last) last->next = rd->next;
612         else st->dimensions = rd->next;
613         rd->next = NULL;
614
615         rrddim_index_del(st, rd);
616
617         // free(rd->annotations);
618         if(rd->mapped == RRD_MEMORY_MODE_SAVE) {
619                 debug(D_RRD_CALLS, "Saving dimension '%s' to '%s'.", rd->name, rd->cache_filename);
620                 savememory(rd->cache_filename, rd, rd->memsize);
621
622                 debug(D_RRD_CALLS, "Unmapping dimension '%s'.", rd->name);
623                 munmap(rd, rd->memsize);
624         }
625         else if(rd->mapped == RRD_MEMORY_MODE_MAP) {
626                 debug(D_RRD_CALLS, "Unmapping dimension '%s'.", rd->name);
627                 munmap(rd, rd->memsize);
628         }
629         else {
630                 debug(D_RRD_CALLS, "Removing dimension '%s'.", rd->name);
631                 free(rd);
632         }
633 }
634
635 void rrdset_free_all(void)
636 {
637         info("Freeing all memory...");
638
639         RRDSET *st;
640         for(st = rrdset_root; st ;) {
641                 RRDSET *next = st->next;
642
643                 while(st->dimensions)
644                         rrddim_free(st, st->dimensions);
645
646                 rrdset_index_del(st);
647
648                 if(st->mapped == RRD_MEMORY_MODE_SAVE) {
649                         debug(D_RRD_CALLS, "Saving stats '%s' to '%s'.", st->name, st->cache_filename);
650                         savememory(st->cache_filename, st, st->memsize);
651
652                         debug(D_RRD_CALLS, "Unmapping stats '%s'.", st->name);
653                         munmap(st, st->memsize);
654                 }
655                 else if(st->mapped == RRD_MEMORY_MODE_MAP) {
656                         debug(D_RRD_CALLS, "Unmapping stats '%s'.", st->name);
657                         munmap(st, st->memsize);
658                 }
659                 else
660                         free(st);
661
662                 st = next;
663         }
664         rrdset_root = NULL;
665
666         info("Memory cleanup completed...");
667 }
668
669 void rrdset_save_all(void)
670 {
671         debug(D_RRD_CALLS, "rrdset_save_all()");
672
673         // let it log a few error messages
674         error_log_limit_reset();
675
676         RRDSET *st;
677         RRDDIM *rd;
678
679         pthread_rwlock_wrlock(&rrdset_root_rwlock);
680         for(st = rrdset_root; st ; st = st->next) {
681                 pthread_rwlock_wrlock(&st->rwlock);
682
683                 if(st->mapped == RRD_MEMORY_MODE_SAVE) {
684                         debug(D_RRD_CALLS, "Saving stats '%s' to '%s'.", st->name, st->cache_filename);
685                         savememory(st->cache_filename, st, st->memsize);
686                 }
687
688                 for(rd = st->dimensions; rd ; rd = rd->next) {
689                         if(likely(rd->mapped == RRD_MEMORY_MODE_SAVE)) {
690                                 debug(D_RRD_CALLS, "Saving dimension '%s' to '%s'.", rd->name, rd->cache_filename);
691                                 savememory(rd->cache_filename, rd, rd->memsize);
692                         }
693                 }
694
695                 pthread_rwlock_unlock(&st->rwlock);
696         }
697         pthread_rwlock_unlock(&rrdset_root_rwlock);
698 }
699
700
701 RRDSET *rrdset_find(const char *id)
702 {
703         debug(D_RRD_CALLS, "rrdset_find() for chart %s", id);
704
705         RRDSET *st = rrdset_index_find(id, 0);
706         return(st);
707 }
708
709 RRDSET *rrdset_find_bytype(const char *type, const char *id)
710 {
711         debug(D_RRD_CALLS, "rrdset_find_bytype() for chart %s.%s", type, id);
712
713         char buf[RRD_ID_LENGTH_MAX + 1];
714
715         strncpyz(buf, type, RRD_ID_LENGTH_MAX - 1);
716         strcat(buf, ".");
717         int len = (int) strlen(buf);
718         strncpyz(&buf[len], id, (size_t) (RRD_ID_LENGTH_MAX - len));
719
720         return(rrdset_find(buf));
721 }
722
723 RRDSET *rrdset_find_byname(const char *name)
724 {
725         debug(D_RRD_CALLS, "rrdset_find_byname() for chart %s", name);
726
727         RRDSET *st = rrdset_index_find_name(name, 0);
728         return(st);
729 }
730
731 RRDDIM *rrddim_find(RRDSET *st, const char *id)
732 {
733         debug(D_RRD_CALLS, "rrddim_find() for chart %s, dimension %s", st->name, id);
734
735         return rrddim_index_find(st, id, 0);
736 }
737
738 int rrddim_hide(RRDSET *st, const char *id)
739 {
740         debug(D_RRD_CALLS, "rrddim_hide() for chart %s, dimension %s", st->name, id);
741
742         RRDDIM *rd = rrddim_find(st, id);
743         if(unlikely(!rd)) {
744                 error("Cannot find dimension with id '%s' on stats '%s' (%s).", id, st->name, st->id);
745                 return 1;
746         }
747
748         rd->flags |= RRDDIM_FLAG_HIDDEN;
749         return 0;
750 }
751
752 int rrddim_unhide(RRDSET *st, const char *id)
753 {
754         debug(D_RRD_CALLS, "rrddim_unhide() for chart %s, dimension %s", st->name, id);
755
756         RRDDIM *rd = rrddim_find(st, id);
757         if(unlikely(!rd)) {
758                 error("Cannot find dimension with id '%s' on stats '%s' (%s).", id, st->name, st->id);
759                 return 1;
760         }
761
762         if(rd->flags & RRDDIM_FLAG_HIDDEN) rd->flags ^= RRDDIM_FLAG_HIDDEN;
763         return 0;
764 }
765
766 collected_number rrddim_set_by_pointer(RRDSET *st, RRDDIM *rd, collected_number value)
767 {
768         debug(D_RRD_CALLS, "rrddim_set_by_pointer() for chart %s, dimension %s, value " COLLECTED_NUMBER_FORMAT, st->name, rd->name, value);
769
770         gettimeofday(&rd->last_collected_time, NULL);
771         rd->collected_value = value;
772         rd->updated = 1;
773         rd->counter++;
774
775         return rd->last_collected_value;
776 }
777
778 collected_number rrddim_set(RRDSET *st, const char *id, collected_number value)
779 {
780         RRDDIM *rd = rrddim_find(st, id);
781         if(unlikely(!rd)) {
782                 error("Cannot find dimension with id '%s' on stats '%s' (%s).", id, st->name, st->id);
783                 return 0;
784         }
785
786         return rrddim_set_by_pointer(st, rd, value);
787 }
788
789 void rrdset_next_usec(RRDSET *st, unsigned long long microseconds)
790 {
791         if(!microseconds) rrdset_next(st);
792         else {
793                 debug(D_RRD_CALLS, "rrdset_next_usec() for chart %s with microseconds %llu", st->name, microseconds);
794
795                 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s: NEXT: %llu microseconds", st->name, microseconds);
796                 st->usec_since_last_update = microseconds;
797         }
798 }
799
800 void rrdset_next(RRDSET *st)
801 {
802         unsigned long long microseconds = 0;
803
804         if(likely(st->last_collected_time.tv_sec)) {
805                 struct timeval now;
806                 gettimeofday(&now, NULL);
807                 microseconds = usecdiff(&now, &st->last_collected_time);
808         }
809         // prevent infinite loop
810         else microseconds = st->update_every * 1000000ULL;
811
812         rrdset_next_usec(st, microseconds);
813 }
814
815 void rrdset_next_plugins(RRDSET *st)
816 {
817         rrdset_next(st);
818 }
819
820 unsigned long long rrdset_done(RRDSET *st)
821 {
822         debug(D_RRD_CALLS, "rrdset_done() for chart %s", st->name);
823
824         RRDDIM *rd, *last;
825         int oldstate, store_this_entry = 1, first_entry = 0;
826         unsigned long long last_ut, now_ut, next_ut, stored_entries = 0;
827
828         if(unlikely(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate) != 0))
829                 error("Cannot set pthread cancel state to DISABLE.");
830
831         // a read lock is OK here
832         pthread_rwlock_rdlock(&st->rwlock);
833
834         // enable the chart, if it was disabled
835         if(unlikely(rrd_delete_unupdated_dimensions) && !st->enabled)
836                 st->enabled = 1;
837
838         // check if the chart has a long time to be updated
839         if(unlikely(st->usec_since_last_update > st->entries * st->update_every * 1000000ULL)) {
840                 info("%s: took too long to be updated (%0.3Lf secs). Reseting it.", st->name, (long double)(st->usec_since_last_update / 1000000.0));
841                 rrdset_reset(st);
842                 st->usec_since_last_update = st->update_every * 1000000ULL;
843                 first_entry = 1;
844         }
845         if(unlikely(st->debug)) debug(D_RRD_STATS, "%s: microseconds since last update: %llu", st->name, st->usec_since_last_update);
846
847         // set last_collected_time
848         if(unlikely(!st->last_collected_time.tv_sec)) {
849                 // it is the first entry
850                 // set the last_collected_time to now
851                 gettimeofday(&st->last_collected_time, NULL);
852
853                 // the first entry should not be stored
854                 store_this_entry = 0;
855                 first_entry = 1;
856
857                 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s: has not set last_collected_time. Setting it now. Will not store the next entry.", st->name);
858         }
859         else {
860                 // it is not the first entry
861                 // calculate the proper last_collected_time, using usec_since_last_update
862                 unsigned long long ut = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec + st->usec_since_last_update;
863                 st->last_collected_time.tv_sec = (time_t) (ut / 1000000ULL);
864                 st->last_collected_time.tv_usec = (useconds_t) (ut % 1000000ULL);
865         }
866
867         // if this set has not been updated in the past
868         // we fake the last_update time to be = now - usec_since_last_update
869         if(unlikely(!st->last_updated.tv_sec)) {
870                 // it has never been updated before
871                 // set a fake last_updated, in the past using usec_since_last_update
872                 unsigned long long ut = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec - st->usec_since_last_update;
873                 st->last_updated.tv_sec = (time_t) (ut / 1000000ULL);
874                 st->last_updated.tv_usec = (useconds_t) (ut % 1000000ULL);
875
876                 // the first entry should not be stored
877                 store_this_entry = 0;
878                 first_entry = 1;
879
880                 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s: initializing last_updated to now - %llu microseconds (%0.3Lf). Will not store the next entry.", st->name, st->usec_since_last_update, (long double)ut/1000000.0);
881         }
882
883         // check if we will re-write the entire data set
884         if(unlikely(usecdiff(&st->last_collected_time, &st->last_updated) > st->update_every * st->entries * 1000000ULL)) {
885                 info("%s: too old data (last updated at %u.%u, last collected at %u.%u). Reseting it. Will not store the next entry.", st->name, st->last_updated.tv_sec, st->last_updated.tv_usec, st->last_collected_time.tv_sec, st->last_collected_time.tv_usec);
886                 rrdset_reset(st);
887
888                 st->usec_since_last_update = st->update_every * 1000000ULL;
889
890                 gettimeofday(&st->last_collected_time, NULL);
891
892                 unsigned long long ut = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec - st->usec_since_last_update;
893                 st->last_updated.tv_sec = (time_t) (ut / 1000000ULL);
894                 st->last_updated.tv_usec = (useconds_t) (ut % 1000000ULL);
895
896                 // the first entry should not be stored
897                 store_this_entry = 0;
898                 first_entry = 1;
899         }
900
901         // these are the 3 variables that will help us in interpolation
902         // last_ut = the last time we added a value to the storage
903         //  now_ut = the time the current value is taken at
904         // next_ut = the time of the next interpolation point
905         last_ut = st->last_updated.tv_sec * 1000000ULL + st->last_updated.tv_usec;
906         now_ut  = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec;
907         next_ut = (st->last_updated.tv_sec + st->update_every) * 1000000ULL;
908
909         if(unlikely(!first_entry && now_ut < next_ut)) {
910                 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s: THIS IS IN THE SAME INTERPOLATION POINT", st->name);
911         }
912
913         if(unlikely(st->debug)) {
914                 debug(D_RRD_STATS, "%s: last ut = %0.3Lf (last updated time)", st->name, (long double)last_ut/1000000.0);
915                 debug(D_RRD_STATS, "%s: now  ut = %0.3Lf (current update time)", st->name, (long double)now_ut/1000000.0);
916                 debug(D_RRD_STATS, "%s: next ut = %0.3Lf (next interpolation point)", st->name, (long double)next_ut/1000000.0);
917         }
918
919         if(unlikely(!st->counter_done)) {
920                 store_this_entry = 0;
921                 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s: Will not store the next entry.", st->name);
922         }
923         st->counter_done++;
924
925         // calculate totals and count the dimensions
926         int dimensions;
927         st->collected_total = 0;
928         for( rd = st->dimensions, dimensions = 0 ; likely(rd) ; rd = rd->next, dimensions++ )
929                 st->collected_total += rd->collected_value;
930
931         uint32_t storage_flags = SN_EXISTS;
932
933         // process all dimensions to calculate their values
934         // based on the collected figures only
935         // at this stage we do not interpolate anything
936         for( rd = st->dimensions ; likely(rd) ; rd = rd->next ) {
937
938                 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s/%s: START "
939                         " last_collected_value = " COLLECTED_NUMBER_FORMAT
940                         " collected_value = " COLLECTED_NUMBER_FORMAT
941                         " last_calculated_value = " CALCULATED_NUMBER_FORMAT
942                         " calculated_value = " CALCULATED_NUMBER_FORMAT
943                         , st->id, rd->name
944                         , rd->last_collected_value
945                         , rd->collected_value
946                         , rd->last_calculated_value
947                         , rd->calculated_value
948                         );
949
950                 switch(rd->algorithm) {
951                 case RRDDIM_ABSOLUTE:
952                         rd->calculated_value = (calculated_number)rd->collected_value
953                                 * (calculated_number)rd->multiplier
954                                 / (calculated_number)rd->divisor;
955
956                         if(unlikely(st->debug))
957                                 debug(D_RRD_STATS, "%s/%s: CALC ABS/ABS-NO-IN "
958                                         CALCULATED_NUMBER_FORMAT " = "
959                                         COLLECTED_NUMBER_FORMAT
960                                         " * " CALCULATED_NUMBER_FORMAT
961                                         " / " CALCULATED_NUMBER_FORMAT
962                                         , st->id, rd->name
963                                         , rd->calculated_value
964                                         , rd->collected_value
965                                         , (calculated_number)rd->multiplier
966                                         , (calculated_number)rd->divisor
967                                         );
968                         break;
969
970                         case RRDDIM_PCENT_OVER_ROW_TOTAL:
971                                 if(unlikely(!st->collected_total)) rd->calculated_value = 0;
972                                 else
973                                 // the percentage of the current value
974                                 // over the total of all dimensions
975                                 rd->calculated_value =
976                                           (calculated_number)100
977                                         * (calculated_number)rd->collected_value
978                                         / (calculated_number)st->collected_total;
979
980                                 if(unlikely(st->debug))
981                                         debug(D_RRD_STATS, "%s/%s: CALC PCENT-ROW "
982                                                 CALCULATED_NUMBER_FORMAT " = 100"
983                                                 " * " COLLECTED_NUMBER_FORMAT
984                                                 " / " COLLECTED_NUMBER_FORMAT
985                                                 , st->id, rd->name
986                                                 , rd->calculated_value
987                                                 , rd->collected_value
988                                                 , st->collected_total
989                                                 );
990                                 break;
991
992                         case RRDDIM_INCREMENTAL:
993                                 if(unlikely(!rd->updated || rd->counter <= 1)) {
994                                         rd->calculated_value = 0;
995                                         continue;
996                                 }
997
998                                 // if the new is smaller than the old (an overflow, or reset), set the old equal to the new
999                                 // to reset the calculation (it will give zero as the calculation for this second)
1000                                 if(unlikely(rd->last_collected_value > rd->collected_value)) {
1001                                         debug(D_RRD_STATS, "%s.%s: RESET or OVERFLOW. Last collected value = " COLLECTED_NUMBER_FORMAT ", current = " COLLECTED_NUMBER_FORMAT
1002                                                         , st->name, rd->name
1003                                                         , rd->last_collected_value
1004                                                         , rd->collected_value);
1005                                         if(!(rd->flags & RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS)) storage_flags = SN_EXISTS_RESET;
1006                                         rd->last_collected_value = rd->collected_value;
1007                                 }
1008
1009                                 rd->calculated_value = (calculated_number)(rd->collected_value - rd->last_collected_value)
1010                                         * (calculated_number)rd->multiplier
1011                                         / (calculated_number)rd->divisor;
1012
1013                                 if(unlikely(st->debug))
1014                                         debug(D_RRD_STATS, "%s/%s: CALC INC PRE "
1015                                                 CALCULATED_NUMBER_FORMAT " = ("
1016                                                 COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT
1017                                                 ")"
1018                                                 " * " CALCULATED_NUMBER_FORMAT
1019                                                 " / " CALCULATED_NUMBER_FORMAT
1020                                                 , st->id, rd->name
1021                                                 , rd->calculated_value
1022                                                 , rd->collected_value, rd->last_collected_value
1023                                                 , (calculated_number)rd->multiplier
1024                                                 , (calculated_number)rd->divisor
1025                                                 );
1026                                 break;
1027
1028                         case RRDDIM_PCENT_OVER_DIFF_TOTAL:
1029                                 if(unlikely(!rd->updated || rd->counter <= 1)) {
1030                                         rd->calculated_value = 0;
1031                                         continue;
1032                                 }
1033
1034                                 // the percentage of the current increment
1035                                 // over the increment of all dimensions together
1036                                 if(unlikely(st->collected_total == st->last_collected_total)) rd->calculated_value = rd->last_calculated_value;
1037                                 else rd->calculated_value =
1038                                           (calculated_number)100
1039                                         * (calculated_number)(rd->collected_value - rd->last_collected_value)
1040                                         / (calculated_number)(st->collected_total  - st->last_collected_total);
1041
1042                                 if(unlikely(st->debug))
1043                                         debug(D_RRD_STATS, "%s/%s: CALC PCENT-DIFF "
1044                                                 CALCULATED_NUMBER_FORMAT " = 100"
1045                                                 " * (" COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT ")"
1046                                                 " / (" COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT ")"
1047                                                 , st->id, rd->name
1048                                                 , rd->calculated_value
1049                                                 , rd->collected_value, rd->last_collected_value
1050                                                 , st->collected_total, st->last_collected_total
1051                                                 );
1052                                 break;
1053
1054                         default:
1055                                 // make the default zero, to make sure
1056                                 // it gets noticed when we add new types
1057                                 rd->calculated_value = 0;
1058
1059                                 if(unlikely(st->debug))
1060                                         debug(D_RRD_STATS, "%s/%s: CALC "
1061                                                 CALCULATED_NUMBER_FORMAT " = 0"
1062                                                 , st->id, rd->name
1063                                                 , rd->calculated_value
1064                                                 );
1065                                 break;
1066                 }
1067
1068                 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s/%s: PHASE2 "
1069                         " last_collected_value = " COLLECTED_NUMBER_FORMAT
1070                         " collected_value = " COLLECTED_NUMBER_FORMAT
1071                         " last_calculated_value = " CALCULATED_NUMBER_FORMAT
1072                         " calculated_value = " CALCULATED_NUMBER_FORMAT
1073                         , st->id, rd->name
1074                         , rd->last_collected_value
1075                         , rd->collected_value
1076                         , rd->last_calculated_value
1077                         , rd->calculated_value
1078                         );
1079
1080         }
1081
1082         // at this point we have all the calculated values ready
1083         // it is now time to interpolate values on a second boundary
1084
1085         unsigned long long first_ut = last_ut;
1086         long long iterations = (now_ut - last_ut) / (st->update_every * 1000000ULL);
1087         if((now_ut % (st->update_every * 1000000ULL)) == 0) iterations++;
1088
1089         for( ; likely(next_ut <= now_ut) ; next_ut += st->update_every * 1000000ULL, iterations-- ) {
1090 #ifdef NETDATA_INTERNAL_CHECKS
1091                 if(iterations < 0) { error("%s: iterations calculation wrapped! first_ut = %llu, last_ut = %llu, next_ut = %llu, now_ut = %llu", st->name, first_ut, last_ut, next_ut, now_ut); }
1092 #endif
1093
1094                 if(unlikely(st->debug)) {
1095                         debug(D_RRD_STATS, "%s: last ut = %0.3Lf (last updated time)", st->name, (long double)last_ut/1000000.0);
1096                         debug(D_RRD_STATS, "%s: next ut = %0.3Lf (next interpolation point)", st->name, (long double)next_ut/1000000.0);
1097                 }
1098
1099                 st->last_updated.tv_sec = (time_t) (next_ut / 1000000ULL);
1100                 st->last_updated.tv_usec = 0;
1101
1102                 for( rd = st->dimensions ; likely(rd) ; rd = rd->next ) {
1103                         calculated_number new_value;
1104
1105                         switch(rd->algorithm) {
1106                                 case RRDDIM_INCREMENTAL:
1107                                         new_value = (calculated_number)
1108                                                 (          rd->calculated_value
1109                                                         * (calculated_number)(next_ut - last_ut)
1110                                                         / (calculated_number)(now_ut - last_ut)
1111                                                 );
1112
1113                                         if(unlikely(st->debug))
1114                                                 debug(D_RRD_STATS, "%s/%s: CALC2 INC "
1115                                                         CALCULATED_NUMBER_FORMAT " = "
1116                                                         CALCULATED_NUMBER_FORMAT
1117                                                         " * %llu"
1118                                                         " / %llu"
1119                                                         , st->id, rd->name
1120                                                         , new_value
1121                                                         , rd->calculated_value
1122                                                         , (next_ut - last_ut)
1123                                                         , (now_ut - last_ut)
1124                                                         );
1125
1126                                         rd->calculated_value -= new_value;
1127                                         new_value += rd->last_calculated_value;
1128                                         rd->last_calculated_value = 0;
1129                                         new_value /= (calculated_number)st->update_every;
1130                                         break;
1131
1132                                 case RRDDIM_ABSOLUTE:
1133                                 case RRDDIM_PCENT_OVER_ROW_TOTAL:
1134                                 case RRDDIM_PCENT_OVER_DIFF_TOTAL:
1135                                 default:
1136                                         if(iterations == 1) {
1137                                                 // this is the last iteration
1138                                                 // do not interpolate
1139                                                 // just show the calculated value
1140
1141                                                 new_value = rd->calculated_value;
1142                                         }
1143                                         else {
1144                                                 // we have missed an update
1145                                                 // interpolate in the middle values
1146
1147                                                 new_value = (calculated_number)
1148                                                         (       (         (rd->calculated_value - rd->last_calculated_value)
1149                                                                         * (calculated_number)(next_ut - first_ut)
1150                                                                         / (calculated_number)(now_ut - first_ut)
1151                                                                 )
1152                                                                 +  rd->last_calculated_value
1153                                                         );
1154
1155                                                 if(unlikely(st->debug))
1156                                                         debug(D_RRD_STATS, "%s/%s: CALC2 DEF "
1157                                                                 CALCULATED_NUMBER_FORMAT " = ((("
1158                                                                 "(" CALCULATED_NUMBER_FORMAT " - " CALCULATED_NUMBER_FORMAT ")"
1159                                                                 " * %llu"
1160                                                                 " / %llu) + " CALCULATED_NUMBER_FORMAT
1161                                                                 , st->id, rd->name
1162                                                                 , new_value
1163                                                                 , rd->calculated_value, rd->last_calculated_value
1164                                                                 , (next_ut - first_ut)
1165                                                                 , (now_ut - first_ut), rd->last_calculated_value
1166                                                                 );
1167
1168                                                 // this is wrong
1169                                                 // it fades the value towards the target
1170                                                 // while we know the calculated value is different
1171                                                 // if(likely(next_ut + st->update_every * 1000000ULL > now_ut)) rd->calculated_value = new_value;
1172                                         }
1173                                         break;
1174                         }
1175
1176                         if(unlikely(!store_this_entry)) {
1177                                 store_this_entry = 1;
1178                                 continue;
1179                         }
1180
1181                         if(likely(rd->updated && rd->counter > 1 && iterations < st->gap_when_lost_iterations_above)) {
1182                                 rd->values[st->current_entry] = pack_storage_number(new_value, storage_flags );
1183
1184                                 if(unlikely(st->debug))
1185                                         debug(D_RRD_STATS, "%s/%s: STORE[%ld] "
1186                                                 CALCULATED_NUMBER_FORMAT " = " CALCULATED_NUMBER_FORMAT
1187                                                 , st->id, rd->name
1188                                                 , st->current_entry
1189                                                 , unpack_storage_number(rd->values[st->current_entry]), new_value
1190                                                 );
1191                         }
1192                         else {
1193                                 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s/%s: STORE[%ld] = NON EXISTING "
1194                                                 , st->id, rd->name
1195                                                 , st->current_entry
1196                                                 );
1197                                 rd->values[st->current_entry] = pack_storage_number(0, SN_NOT_EXISTS);
1198                         }
1199
1200                         stored_entries++;
1201
1202                         if(unlikely(st->debug)) {
1203                                 calculated_number t1 = new_value * (calculated_number)rd->multiplier / (calculated_number)rd->divisor;
1204                                 calculated_number t2 = unpack_storage_number(rd->values[st->current_entry]);
1205                                 calculated_number accuracy = accuracy_loss(t1, t2);
1206                                 debug(D_RRD_STATS, "%s/%s: UNPACK[%ld] = " CALCULATED_NUMBER_FORMAT " FLAGS=0x%08x (original = " CALCULATED_NUMBER_FORMAT ", accuracy loss = " CALCULATED_NUMBER_FORMAT "%%%s)"
1207                                                 , st->id, rd->name
1208                                                 , st->current_entry
1209                                                 , t2
1210                                                 , get_storage_number_flags(rd->values[st->current_entry])
1211                                                 , t1
1212                                                 , accuracy
1213                                                 , (accuracy > ACCURACY_LOSS) ? " **TOO BIG** " : ""
1214                                                 );
1215
1216                                 rd->collected_volume += t1;
1217                                 rd->stored_volume += t2;
1218                                 accuracy = accuracy_loss(rd->collected_volume, rd->stored_volume);
1219                                 debug(D_RRD_STATS, "%s/%s: VOLUME[%ld] = " CALCULATED_NUMBER_FORMAT ", calculated  = " CALCULATED_NUMBER_FORMAT ", accuracy loss = " CALCULATED_NUMBER_FORMAT "%%%s"
1220                                                 , st->id, rd->name
1221                                                 , st->current_entry
1222                                                 , rd->stored_volume
1223                                                 , rd->collected_volume
1224                                                 , accuracy
1225                                                 , (accuracy > ACCURACY_LOSS) ? " **TOO BIG** " : ""
1226                                                 );
1227
1228                         }
1229                 }
1230                 // reset the storage flags for the next point, if any;
1231                 storage_flags = SN_EXISTS;
1232
1233                 st->counter++;
1234                 st->current_entry = ((st->current_entry + 1) >= st->entries) ? 0 : st->current_entry + 1;
1235                 last_ut = next_ut;
1236         }
1237
1238         // align next interpolation to last collection point
1239         if(likely(stored_entries || !store_this_entry)) {
1240                 st->last_updated.tv_sec = st->last_collected_time.tv_sec;
1241                 st->last_updated.tv_usec = st->last_collected_time.tv_usec;
1242         }
1243
1244         for( rd = st->dimensions; likely(rd) ; rd = rd->next ) {
1245                 if(unlikely(!rd->updated)) continue;
1246
1247                 if(likely(stored_entries || !store_this_entry)) {
1248                         if(unlikely(st->debug)) debug(D_RRD_STATS, "%s/%s: setting last_collected_value (old: " COLLECTED_NUMBER_FORMAT ") to last_collected_value (new: " COLLECTED_NUMBER_FORMAT ")", st->id, rd->name, rd->last_collected_value, rd->collected_value);
1249                         rd->last_collected_value = rd->collected_value;
1250
1251                         if(unlikely(st->debug)) debug(D_RRD_STATS, "%s/%s: setting last_calculated_value (old: " CALCULATED_NUMBER_FORMAT ") to last_calculated_value (new: " CALCULATED_NUMBER_FORMAT ")", st->id, rd->name, rd->last_calculated_value, rd->calculated_value);
1252                         rd->last_calculated_value = rd->calculated_value;
1253                 }
1254
1255                 rd->calculated_value = 0;
1256                 rd->collected_value = 0;
1257                 rd->updated = 0;
1258
1259                 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s/%s: END "
1260                         " last_collected_value = " COLLECTED_NUMBER_FORMAT
1261                         " collected_value = " COLLECTED_NUMBER_FORMAT
1262                         " last_calculated_value = " CALCULATED_NUMBER_FORMAT
1263                         " calculated_value = " CALCULATED_NUMBER_FORMAT
1264                         , st->id, rd->name
1265                         , rd->last_collected_value
1266                         , rd->collected_value
1267                         , rd->last_calculated_value
1268                         , rd->calculated_value
1269                         );
1270         }
1271         st->last_collected_total  = st->collected_total;
1272
1273         // ALL DONE ABOUT THE DATA UPDATE
1274         // --------------------------------------------------------------------
1275
1276         // find if there are any obsolete dimensions (not updated recently)
1277         if(unlikely(rrd_delete_unupdated_dimensions)) {
1278
1279                 for( rd = st->dimensions; likely(rd) ; rd = rd->next )
1280                         if((rd->last_collected_time.tv_sec + (rrd_delete_unupdated_dimensions * st->update_every)) < st->last_collected_time.tv_sec)
1281                                 break;
1282
1283                 if(unlikely(rd)) {
1284                         // there is dimension to free
1285                         // upgrade our read lock to a write lock
1286                         pthread_rwlock_unlock(&st->rwlock);
1287                         pthread_rwlock_wrlock(&st->rwlock);
1288
1289                         for( rd = st->dimensions, last = NULL ; likely(rd) ; ) {
1290                                 // remove it only it is not updated in rrd_delete_unupdated_dimensions seconds
1291
1292                                 if(unlikely((rd->last_collected_time.tv_sec + (rrd_delete_unupdated_dimensions * st->update_every)) < st->last_collected_time.tv_sec)) {
1293                                         info("Removing obsolete dimension '%s' (%s) of '%s' (%s).", rd->name, rd->id, st->name, st->id);
1294
1295                                         if(unlikely(!last)) {
1296                                                 st->dimensions = rd->next;
1297                                                 rd->next = NULL;
1298                                                 rrddim_free(st, rd);
1299                                                 rd = st->dimensions;
1300                                                 continue;
1301                                         }
1302                                         else {
1303                                                 last->next = rd->next;
1304                                                 rd->next = NULL;
1305                                                 rrddim_free(st, rd);
1306                                                 rd = last->next;
1307                                                 continue;
1308                                         }
1309                                 }
1310
1311                                 last = rd;
1312                                 rd = rd->next;
1313                         }
1314
1315                         if(unlikely(!st->dimensions)) {
1316                                 info("Disabling chart %s (%s) since it does not have any dimensions", st->name, st->id);
1317                                 st->enabled = 0;
1318                         }
1319                 }
1320         }
1321
1322         pthread_rwlock_unlock(&st->rwlock);
1323
1324         if(unlikely(pthread_setcancelstate(oldstate, NULL) != 0))
1325                 error("Cannot set pthread cancel state to RESTORE (%d).", oldstate);
1326
1327         return(st->usec_since_last_update);
1328 }