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