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