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