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