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