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