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