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