]> arthur.barton.de Git - netdata.git/blob - src/rrdset.c
properly lock all sensitive linked lists
[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(long entries) {
199     if(central_netdata_to_push_data)
200         return entries;
201
202     if(entries < 5) entries = 5;
203     if(entries > RRD_HISTORY_ENTRIES_MAX) entries = RRD_HISTORY_ENTRIES_MAX;
204
205     long page = (size_t)sysconf(_SC_PAGESIZE);
206     long size = sizeof(RRDDIM) + entries * sizeof(storage_number);
207     if(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(rentries);
326     if(entries != rentries) entries = config_set_number(config_section, "history", entries);
327
328     int enabled = config_get_boolean(config_section, "enabled", 1);
329     if(!enabled) entries = 5;
330
331     unsigned long size = sizeof(RRDSET);
332     char *cache_dir = rrdset_cache_dir(host, fullid, config_section);
333
334     // ------------------------------------------------------------------------
335     // load it or allocate it
336
337     debug(D_RRD_CALLS, "Creating RRD_STATS for '%s.%s'.", type, id);
338
339     snprintfz(fullfilename, FILENAME_MAX, "%s/main.db", cache_dir);
340     if(host->rrd_memory_mode != RRD_MEMORY_MODE_RAM) {
341         st = (RRDSET *) mymmap(fullfilename, size, ((host->rrd_memory_mode == RRD_MEMORY_MODE_MAP) ? MAP_SHARED : MAP_PRIVATE), 0);
342         if(st) {
343             memset(&st->avl, 0, sizeof(avl));
344             memset(&st->avlname, 0, sizeof(avl));
345             memset(&st->variables_root_index, 0, sizeof(avl_tree_lock));
346             memset(&st->dimensions_index, 0, sizeof(avl_tree_lock));
347             memset(&st->rrdset_rwlock, 0, sizeof(pthread_rwlock_t));
348
349             st->name = NULL;
350             st->type = NULL;
351             st->family = NULL;
352             st->context = NULL;
353             st->title = NULL;
354             st->units = NULL;
355             st->dimensions = NULL;
356             st->next = NULL;
357             st->variables = NULL;
358             st->alarms = NULL;
359             st->flags = 0x00000000;
360
361             if(strcmp(st->magic, RRDSET_MAGIC) != 0) {
362                 errno = 0;
363                 info("Initializing file %s.", fullfilename);
364                 memset(st, 0, size);
365             }
366             else if(strcmp(st->id, fullid) != 0) {
367                 errno = 0;
368                 error("File %s contents are not for chart %s. Clearing it.", fullfilename, fullid);
369                 // munmap(st, size);
370                 // st = NULL;
371                 memset(st, 0, size);
372             }
373             else if(st->memsize != size || st->entries != entries) {
374                 errno = 0;
375                 error("File %s does not have the desired size. Clearing it.", fullfilename);
376                 memset(st, 0, size);
377             }
378             else if(st->update_every != update_every) {
379                 errno = 0;
380                 error("File %s does not have the desired update frequency. Clearing it.", fullfilename);
381                 memset(st, 0, size);
382             }
383             else if((now_realtime_sec() - st->last_updated.tv_sec) > update_every * entries) {
384                 errno = 0;
385                 error("File %s is too old. Clearing it.", fullfilename);
386                 memset(st, 0, size);
387             }
388
389             // make sure the database is aligned
390             if(st->last_updated.tv_sec)
391                 timeval_align(&st->last_updated, update_every);
392
393             // make sure we have the right memory mode
394             // even if we cleared the memory
395             st->rrd_memory_mode = host->rrd_memory_mode;
396         }
397     }
398
399     if(unlikely(!st)) {
400         st = callocz(1, size);
401         st->rrd_memory_mode = RRD_MEMORY_MODE_RAM;
402     }
403
404     st->config_section = strdup(config_section);
405     st->rrdhost = host;
406     st->memsize = size;
407     st->entries = entries;
408     st->update_every = update_every;
409
410     if(st->current_entry >= st->entries) st->current_entry = 0;
411
412     strcpy(st->cache_filename, fullfilename);
413     strcpy(st->magic, RRDSET_MAGIC);
414
415     strcpy(st->id, fullid);
416     st->hash = simple_hash(st->id);
417
418     st->cache_dir = cache_dir;
419
420     st->chart_type = rrdset_type_id(config_get(st->config_section, "chart type", rrdset_type_name(chart_type)));
421     st->type       = config_get(st->config_section, "type", type);
422     st->family     = config_get(st->config_section, "family", family?family:st->type);
423     st->units      = config_get(st->config_section, "units", units?units:"");
424
425     st->context    = config_get(st->config_section, "context", context?context:st->id);
426     st->hash_context = simple_hash(st->context);
427
428     st->priority = config_get_number(st->config_section, "priority", priority);
429     if(enabled)
430         rrdset_flag_set(st, RRDSET_FLAG_ENABLED);
431     else
432         rrdset_flag_clear(st, RRDSET_FLAG_ENABLED);
433
434     rrdset_flag_clear(st, RRDSET_FLAG_DETAIL);
435     rrdset_flag_clear(st, RRDSET_FLAG_DEBUG);
436
437     // if(!strcmp(st->id, "disk_util.dm-0")) {
438     //     st->debug = 1;
439     //     error("enabled debugging for '%s'", st->id);
440     // }
441     // else error("not enabled debugging for '%s'", st->id);
442
443     st->green = NAN;
444     st->red = NAN;
445
446     st->last_collected_time.tv_sec = 0;
447     st->last_collected_time.tv_usec = 0;
448     st->counter_done = 0;
449
450     st->gap_when_lost_iterations_above = (int) (
451             config_get_number(st->config_section, "gap when lost iterations above", RRD_DEFAULT_GAP_INTERPOLATIONS) + 2);
452
453     avl_init_lock(&st->dimensions_index, rrddim_compare);
454     avl_init_lock(&st->variables_root_index, rrdvar_compare);
455
456     pthread_rwlock_init(&st->rrdset_rwlock, NULL);
457     rrdhost_wrlock(host);
458
459     if(name && *name) rrdset_set_name(st, name);
460     else rrdset_set_name(st, id);
461
462     {
463         char varvalue[CONFIG_MAX_VALUE + 1];
464         char varvalue2[CONFIG_MAX_VALUE + 1];
465         snprintfz(varvalue, CONFIG_MAX_VALUE, "%s (%s)", title?title:"", st->name);
466         json_escape_string(varvalue2, varvalue, sizeof(varvalue2));
467         st->title = config_get(st->config_section, "title", varvalue2);
468     }
469
470     st->rrdfamily = rrdfamily_create(host, st->family);
471
472     st->next = host->rrdset_root;
473     host->rrdset_root = st;
474
475     if(host->health_enabled) {
476         rrdsetvar_create(st, "last_collected_t", RRDVAR_TYPE_TIME_T, &st->last_collected_time.tv_sec, 0);
477         rrdsetvar_create(st, "collected_total_raw", RRDVAR_TYPE_TOTAL, &st->last_collected_total, 0);
478         rrdsetvar_create(st, "green", RRDVAR_TYPE_CALCULATED, &st->green, 0);
479         rrdsetvar_create(st, "red", RRDVAR_TYPE_CALCULATED, &st->red, 0);
480         rrdsetvar_create(st, "update_every", RRDVAR_TYPE_INT, &st->update_every, 0);
481     }
482
483     if(unlikely(rrdset_index_add(host, st) != st))
484         error("RRDSET: INTERNAL ERROR: attempt to index duplicate chart '%s'", st->id);
485
486     rrdsetcalc_link_matching(st);
487     rrdcalctemplate_link_matching(st);
488
489     rrdhost_unlock(host);
490
491     return(st);
492 }
493
494
495 // ----------------------------------------------------------------------------
496 // RRDSET - data collection iteration control
497
498 void rrdset_next_usec_unfiltered(RRDSET *st, usec_t microseconds) {
499     if(unlikely(!st->last_collected_time.tv_sec || !microseconds)) {
500         // the first entry
501         microseconds = st->update_every * USEC_PER_SEC;
502     }
503     st->usec_since_last_update = microseconds;
504 }
505
506 void rrdset_next_usec(RRDSET *st, usec_t microseconds)
507 {
508     struct timeval now;
509     now_realtime_timeval(&now);
510
511     if(unlikely(!st->last_collected_time.tv_sec)) {
512         // the first entry
513         microseconds = st->update_every * USEC_PER_SEC;
514     }
515     else if(unlikely(!microseconds)) {
516         // no dt given by the plugin
517         microseconds = dt_usec(&now, &st->last_collected_time);
518     }
519     else {
520         // microseconds has the time since the last collection
521 //#ifdef NETDATA_INTERNAL_CHECKS
522 //        usec_t now_usec = timeval_usec(&now);
523 //        usec_t last_usec = timeval_usec(&st->last_collected_time);
524 //#endif
525         usec_t since_last_usec = dt_usec(&now, &st->last_collected_time);
526
527         // verify the microseconds given is good
528         if(unlikely(microseconds > since_last_usec)) {
529             debug(D_RRD_CALLS, "dt %llu usec given is too big - it leads %llu usec to the future, for chart '%s' (%s).", microseconds, microseconds - since_last_usec, st->name, st->id);
530
531 //#ifdef NETDATA_INTERNAL_CHECKS
532 //            if(unlikely(last_usec + microseconds > now_usec + 1000))
533 //                error("dt %llu usec given is too big - it leads %llu usec to the future, for chart '%s' (%s).", microseconds, microseconds - since_last_usec, st->name, st->id);
534 //#endif
535
536             microseconds = since_last_usec;
537         }
538         else if(unlikely(microseconds < since_last_usec * 0.8)) {
539             debug(D_RRD_CALLS, "dt %llu usec given is too small - expected %llu usec up to -20%%, for chart '%s' (%s).", microseconds, since_last_usec, st->name, st->id);
540
541 //#ifdef NETDATA_INTERNAL_CHECKS
542 //            error("dt %llu usec given is too small - expected %llu usec up to -20%%, for chart '%s' (%s).", microseconds, since_last_usec, st->name, st->id);
543 //#endif
544             microseconds = since_last_usec;
545         }
546     }
547     debug(D_RRD_CALLS, "rrdset_next_usec() for chart %s with microseconds %llu", st->name, microseconds);
548
549     if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
550         debug(D_RRD_STATS, "%s: NEXT: %llu microseconds", st->name, microseconds);
551
552     st->usec_since_last_update = microseconds;
553 }
554
555
556 // ----------------------------------------------------------------------------
557 // RRDSET - process the collected values for all dimensions of a chart
558
559 void rrdset_done(RRDSET *st) {
560     if(unlikely(netdata_exit)) return;
561     if(unlikely(central_netdata_to_push_data)) {
562         rrdset_done_push(st);
563         return;
564     }
565
566     debug(D_RRD_CALLS, "rrdset_done() for chart %s", st->name);
567
568     RRDDIM *rd;
569
570     int
571             pthreadoldcancelstate;  // store the old cancelable pthread state, to restore it at the end
572
573     char
574             store_this_entry = 1,   // boolean: 1 = store this entry, 0 = don't store this entry
575             first_entry = 0;        // boolean: 1 = this is the first entry seen for this chart, 0 = all other entries
576
577     unsigned int
578             stored_entries = 0;     // the number of entries we have stored in the db, during this call to rrdset_done()
579
580     usec_t
581             last_collect_ut,        // the timestamp in microseconds, of the last collected value
582             now_collect_ut,         // the timestamp in microseconds, of this collected value (this is NOW)
583             last_stored_ut,         // the timestamp in microseconds, of the last stored entry in the db
584             next_store_ut,          // the timestamp in microseconds, of the next entry to store in the db
585             update_every_ut = st->update_every * USEC_PER_SEC; // st->update_every in microseconds
586
587     if(unlikely(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &pthreadoldcancelstate) != 0))
588         error("Cannot set pthread cancel state to DISABLE.");
589
590     // a read lock is OK here
591     rrdset_rdlock(st);
592
593 /*
594     // enable the chart, if it was disabled
595     if(unlikely(rrd_delete_unupdated_dimensions) && !st->enabled)
596         st->enabled = 1;
597 */
598
599     // check if the chart has a long time to be updated
600     if(unlikely(st->usec_since_last_update > st->entries * update_every_ut)) {
601         info("%s: took too long to be updated (%0.3Lf secs). Resetting it.", st->name, (long double)(st->usec_since_last_update / 1000000.0));
602         rrdset_reset(st);
603         st->usec_since_last_update = update_every_ut;
604         first_entry = 1;
605     }
606
607     if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
608         debug(D_RRD_STATS, "%s: microseconds since last update: %llu", st->name, st->usec_since_last_update);
609
610     // set last_collected_time
611     if(unlikely(!st->last_collected_time.tv_sec)) {
612         // it is the first entry
613         // set the last_collected_time to now
614         now_realtime_timeval(&st->last_collected_time);
615         timeval_align(&st->last_collected_time, st->update_every);
616
617         last_collect_ut = st->last_collected_time.tv_sec * USEC_PER_SEC + st->last_collected_time.tv_usec - update_every_ut;
618
619         // the first entry should not be stored
620         store_this_entry = 0;
621         first_entry = 1;
622
623         if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
624             debug(D_RRD_STATS, "%s: has not set last_collected_time. Setting it now. Will not store the next entry.", st->name);
625     }
626     else {
627         // it is not the first entry
628         // calculate the proper last_collected_time, using usec_since_last_update
629         last_collect_ut = st->last_collected_time.tv_sec * USEC_PER_SEC + st->last_collected_time.tv_usec;
630         usec_t ut = last_collect_ut + st->usec_since_last_update;
631         st->last_collected_time.tv_sec = (time_t) (ut / USEC_PER_SEC);
632         st->last_collected_time.tv_usec = (suseconds_t) (ut % USEC_PER_SEC);
633     }
634
635     // if this set has not been updated in the past
636     // we fake the last_update time to be = now - usec_since_last_update
637     if(unlikely(!st->last_updated.tv_sec)) {
638         // it has never been updated before
639         // set a fake last_updated, in the past using usec_since_last_update
640         usec_t ut = st->last_collected_time.tv_sec * USEC_PER_SEC + st->last_collected_time.tv_usec - st->usec_since_last_update;
641         st->last_updated.tv_sec = (time_t) (ut / USEC_PER_SEC);
642         st->last_updated.tv_usec = (suseconds_t) (ut % USEC_PER_SEC);
643
644         // the first entry should not be stored
645         store_this_entry = 0;
646         first_entry = 1;
647
648         if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
649             debug(D_RRD_STATS, "%s: initializing last_updated to now - %llu microseconds (%0.3Lf). Will not store the next entry.", st->name, st->usec_since_last_update, (long double)ut/1000000.0);
650     }
651
652     // check if we will re-write the entire data set
653     if(unlikely(dt_usec(&st->last_collected_time, &st->last_updated) > st->entries * update_every_ut)) {
654         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);
655         rrdset_reset(st);
656
657         st->usec_since_last_update = update_every_ut;
658
659         now_realtime_timeval(&st->last_collected_time);
660         timeval_align(&st->last_collected_time, st->update_every);
661
662         usec_t ut = st->last_collected_time.tv_sec * USEC_PER_SEC + st->last_collected_time.tv_usec - st->usec_since_last_update;
663         st->last_updated.tv_sec = (time_t) (ut / USEC_PER_SEC);
664         st->last_updated.tv_usec = (suseconds_t) (ut % USEC_PER_SEC);
665
666         // the first entry should not be stored
667         store_this_entry = 0;
668         first_entry = 1;
669     }
670
671     // these are the 3 variables that will help us in interpolation
672     // last_stored_ut = the last time we added a value to the storage
673     // now_collect_ut = the time the current value has been collected
674     // next_store_ut  = the time of the next interpolation point
675     last_stored_ut = st->last_updated.tv_sec * USEC_PER_SEC + st->last_updated.tv_usec;
676     now_collect_ut = st->last_collected_time.tv_sec * USEC_PER_SEC + st->last_collected_time.tv_usec;
677     next_store_ut  = (st->last_updated.tv_sec + st->update_every) * USEC_PER_SEC;
678
679     if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG))) {
680         debug(D_RRD_STATS, "%s: last_collect_ut = %0.3Lf (last collection time)", st->name, (long double)last_collect_ut/1000000.0);
681         debug(D_RRD_STATS, "%s: now_collect_ut  = %0.3Lf (current collection time)", st->name, (long double)now_collect_ut/1000000.0);
682         debug(D_RRD_STATS, "%s: last_stored_ut  = %0.3Lf (last updated time)", st->name, (long double)last_stored_ut/1000000.0);
683         debug(D_RRD_STATS, "%s: next_store_ut   = %0.3Lf (next interpolation point)", st->name, (long double)next_store_ut/1000000.0);
684     }
685
686     if(unlikely(!st->counter_done)) {
687         store_this_entry = 0;
688         if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
689             debug(D_RRD_STATS, "%s: Will not store the next entry.", st->name);
690     }
691     st->counter_done++;
692
693     // calculate totals and count the dimensions
694     int dimensions = 0;
695     st->collected_total = 0;
696     rrddim_foreach_read(rd, st) {
697         dimensions++;
698         if(likely(rrddim_flag_check(rd, RRDDIM_FLAG_UPDATED)))
699             st->collected_total += rd->collected_value;
700     }
701
702     uint32_t storage_flags = SN_EXISTS;
703
704     // process all dimensions to calculate their values
705     // based on the collected figures only
706     // at this stage we do not interpolate anything
707     rrddim_foreach_read(rd, st) {
708
709         if(unlikely(!rrddim_flag_check(rd, RRDDIM_FLAG_UPDATED))) {
710             rd->calculated_value = 0;
711             continue;
712         }
713
714         if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
715             debug(D_RRD_STATS, "%s/%s: START "
716                     " last_collected_value = " COLLECTED_NUMBER_FORMAT
717                     " collected_value = " COLLECTED_NUMBER_FORMAT
718                     " last_calculated_value = " CALCULATED_NUMBER_FORMAT
719                     " calculated_value = " CALCULATED_NUMBER_FORMAT
720                                       , st->id, rd->name
721                                       , rd->last_collected_value
722                                       , rd->collected_value
723                                       , rd->last_calculated_value
724                                       , rd->calculated_value
725             );
726
727         switch(rd->algorithm) {
728             case RRD_ALGORITHM_ABSOLUTE:
729                 rd->calculated_value = (calculated_number)rd->collected_value
730                                        * (calculated_number)rd->multiplier
731                                        / (calculated_number)rd->divisor;
732
733                 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
734                     debug(D_RRD_STATS, "%s/%s: CALC ABS/ABS-NO-IN "
735                             CALCULATED_NUMBER_FORMAT " = "
736                             COLLECTED_NUMBER_FORMAT
737                             " * " CALCULATED_NUMBER_FORMAT
738                             " / " CALCULATED_NUMBER_FORMAT
739                           , st->id, rd->name
740                           , rd->calculated_value
741                           , rd->collected_value
742                           , (calculated_number)rd->multiplier
743                           , (calculated_number)rd->divisor
744                     );
745                 break;
746
747             case RRD_ALGORITHM_PCENT_OVER_ROW_TOTAL:
748                 if(unlikely(!st->collected_total))
749                     rd->calculated_value = 0;
750                 else
751                     // the percentage of the current value
752                     // over the total of all dimensions
753                     rd->calculated_value =
754                             (calculated_number)100
755                             * (calculated_number)rd->collected_value
756                             / (calculated_number)st->collected_total;
757
758                 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
759                     debug(D_RRD_STATS, "%s/%s: CALC PCENT-ROW "
760                             CALCULATED_NUMBER_FORMAT " = 100"
761                                     " * " COLLECTED_NUMBER_FORMAT
762                             " / " COLLECTED_NUMBER_FORMAT
763                           , st->id, rd->name
764                           , rd->calculated_value
765                           , rd->collected_value
766                           , st->collected_total
767                     );
768                 break;
769
770             case RRD_ALGORITHM_INCREMENTAL:
771                 if(unlikely(rd->counter <= 1)) {
772                     rd->calculated_value = 0;
773                     continue;
774                 }
775
776                 // if the new is smaller than the old (an overflow, or reset), set the old equal to the new
777                 // to reset the calculation (it will give zero as the calculation for this second)
778                 if(unlikely(rd->last_collected_value > rd->collected_value)) {
779                     debug(D_RRD_STATS, "%s.%s: RESET or OVERFLOW. Last collected value = " COLLECTED_NUMBER_FORMAT ", current = " COLLECTED_NUMBER_FORMAT
780                           , st->name, rd->name
781                           , rd->last_collected_value
782                           , rd->collected_value);
783
784                     if(!(rrddim_flag_check(rd, RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS)))
785                         storage_flags = SN_EXISTS_RESET;
786
787                     rd->last_collected_value = rd->collected_value;
788                 }
789
790                 rd->calculated_value +=
791                         (calculated_number)(rd->collected_value - rd->last_collected_value)
792                         * (calculated_number)rd->multiplier
793                         / (calculated_number)rd->divisor;
794
795                 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
796                     debug(D_RRD_STATS, "%s/%s: CALC INC PRE "
797                             CALCULATED_NUMBER_FORMAT " = ("
798                             COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT
799                             ")"
800                                     " * " CALCULATED_NUMBER_FORMAT
801                             " / " CALCULATED_NUMBER_FORMAT
802                           , st->id, rd->name
803                           , rd->calculated_value
804                           , rd->collected_value, rd->last_collected_value
805                           , (calculated_number)rd->multiplier
806                           , (calculated_number)rd->divisor
807                     );
808                 break;
809
810             case RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL:
811                 if(unlikely(rd->counter <= 1)) {
812                     rd->calculated_value = 0;
813                     continue;
814                 }
815
816                 // if the new is smaller than the old (an overflow, or reset), set the old equal to the new
817                 // to reset the calculation (it will give zero as the calculation for this second)
818                 if(unlikely(rd->last_collected_value > rd->collected_value)) {
819                     debug(D_RRD_STATS, "%s.%s: RESET or OVERFLOW. Last collected value = " COLLECTED_NUMBER_FORMAT ", current = " COLLECTED_NUMBER_FORMAT
820                           , st->name, rd->name
821                           , rd->last_collected_value
822                           , rd->collected_value);
823
824                     if(!(rrddim_flag_check(rd, RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS)))
825                         storage_flags = SN_EXISTS_RESET;
826
827                     rd->last_collected_value = rd->collected_value;
828                 }
829
830                 // the percentage of the current increment
831                 // over the increment of all dimensions together
832                 if(unlikely(st->collected_total == st->last_collected_total))
833                     rd->calculated_value = 0;
834                 else
835                     rd->calculated_value =
836                             (calculated_number)100
837                             * (calculated_number)(rd->collected_value - rd->last_collected_value)
838                             / (calculated_number)(st->collected_total - st->last_collected_total);
839
840                 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
841                     debug(D_RRD_STATS, "%s/%s: CALC PCENT-DIFF "
842                             CALCULATED_NUMBER_FORMAT " = 100"
843                                     " * (" COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT ")"
844                                     " / (" COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT ")"
845                           , st->id, rd->name
846                           , rd->calculated_value
847                           , rd->collected_value, rd->last_collected_value
848                           , st->collected_total, st->last_collected_total
849                     );
850                 break;
851
852             default:
853                 // make the default zero, to make sure
854                 // it gets noticed when we add new types
855                 rd->calculated_value = 0;
856
857                 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
858                     debug(D_RRD_STATS, "%s/%s: CALC "
859                             CALCULATED_NUMBER_FORMAT " = 0"
860                           , st->id, rd->name
861                           , rd->calculated_value
862                     );
863                 break;
864         }
865
866         if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
867             debug(D_RRD_STATS, "%s/%s: PHASE2 "
868                     " last_collected_value = " COLLECTED_NUMBER_FORMAT
869                     " collected_value = " COLLECTED_NUMBER_FORMAT
870                     " last_calculated_value = " CALCULATED_NUMBER_FORMAT
871                     " calculated_value = " CALCULATED_NUMBER_FORMAT
872                                       , st->id, rd->name
873                                       , rd->last_collected_value
874                                       , rd->collected_value
875                                       , rd->last_calculated_value
876                                       , rd->calculated_value
877             );
878
879     }
880
881     // at this point we have all the calculated values ready
882     // it is now time to interpolate values on a second boundary
883
884     if(unlikely(now_collect_ut < next_store_ut)) {
885         // this is collected in the same interpolation point
886
887         if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
888             debug(D_RRD_STATS, "%s: THIS IS IN THE SAME INTERPOLATION POINT", st->name);
889
890 //#ifdef NETDATA_INTERNAL_CHECKS
891 //        info("%s is collected in the same interpolation point: short by %llu microseconds", st->name, next_store_ut - now_collect_ut);
892 //#endif
893     }
894
895     usec_t first_ut = last_stored_ut;
896     long long iterations = (now_collect_ut - last_stored_ut) / (update_every_ut);
897     if((now_collect_ut % (update_every_ut)) == 0) iterations++;
898
899     for( ; next_store_ut <= now_collect_ut ; last_collect_ut = next_store_ut, next_store_ut += update_every_ut, iterations-- ) {
900 //#ifdef NETDATA_INTERNAL_CHECKS
901 //        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); }
902 //#endif
903
904         if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG))) {
905             debug(D_RRD_STATS, "%s: last_stored_ut = %0.3Lf (last updated time)", st->name, (long double)last_stored_ut/1000000.0);
906             debug(D_RRD_STATS, "%s: next_store_ut  = %0.3Lf (next interpolation point)", st->name, (long double)next_store_ut/1000000.0);
907         }
908
909         st->last_updated.tv_sec = (time_t) (next_store_ut / USEC_PER_SEC);
910         st->last_updated.tv_usec = 0;
911
912         rrddim_foreach_read(rd, st) {
913             calculated_number new_value;
914
915             switch(rd->algorithm) {
916                 case RRD_ALGORITHM_INCREMENTAL:
917                     new_value = (calculated_number)
918                             (      rd->calculated_value
919                                    * (calculated_number)(next_store_ut - last_collect_ut)
920                                    / (calculated_number)(now_collect_ut - last_collect_ut)
921                             );
922
923                     if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
924                         debug(D_RRD_STATS, "%s/%s: CALC2 INC "
925                                 CALCULATED_NUMBER_FORMAT " = "
926                                 CALCULATED_NUMBER_FORMAT
927                                 " * %llu"
928                                         " / %llu"
929                               , st->id, rd->name
930                               , new_value
931                               , rd->calculated_value
932                               , (next_store_ut - last_stored_ut)
933                               , (now_collect_ut - last_stored_ut)
934                         );
935
936                     rd->calculated_value -= new_value;
937                     new_value += rd->last_calculated_value;
938                     rd->last_calculated_value = 0;
939                     new_value /= (calculated_number)st->update_every;
940
941                     if(unlikely(next_store_ut - last_stored_ut < update_every_ut)) {
942                         if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
943                             debug(D_RRD_STATS, "%s/%s: COLLECTION POINT IS SHORT " CALCULATED_NUMBER_FORMAT " - EXTRAPOLATING",
944                                     st->id, rd->name
945                                   , (calculated_number)(next_store_ut - last_stored_ut)
946                             );
947                         new_value = new_value * (calculated_number)(st->update_every * 1000000) / (calculated_number)(next_store_ut - last_stored_ut);
948                     }
949                     break;
950
951                 case RRD_ALGORITHM_ABSOLUTE:
952                 case RRD_ALGORITHM_PCENT_OVER_ROW_TOTAL:
953                 case RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL:
954                 default:
955                     if(iterations == 1) {
956                         // this is the last iteration
957                         // do not interpolate
958                         // just show the calculated value
959
960                         new_value = rd->calculated_value;
961                     }
962                     else {
963                         // we have missed an update
964                         // interpolate in the middle values
965
966                         new_value = (calculated_number)
967                                 (   (     (rd->calculated_value - rd->last_calculated_value)
968                                           * (calculated_number)(next_store_ut - last_collect_ut)
969                                           / (calculated_number)(now_collect_ut - last_collect_ut)
970                                     )
971                                     +  rd->last_calculated_value
972                                 );
973
974                         if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
975                             debug(D_RRD_STATS, "%s/%s: CALC2 DEF "
976                                     CALCULATED_NUMBER_FORMAT " = ((("
977                                             "(" CALCULATED_NUMBER_FORMAT " - " CALCULATED_NUMBER_FORMAT ")"
978                                             " * %llu"
979                                             " / %llu) + " CALCULATED_NUMBER_FORMAT
980                                   , st->id, rd->name
981                                   , new_value
982                                   , rd->calculated_value, rd->last_calculated_value
983                                   , (next_store_ut - first_ut)
984                                   , (now_collect_ut - first_ut), rd->last_calculated_value
985                             );
986                     }
987                     break;
988             }
989
990             if(unlikely(!store_this_entry)) {
991                 rd->values[st->current_entry] = pack_storage_number(0, SN_NOT_EXISTS);
992                 continue;
993             }
994
995             if(likely(rrddim_flag_check(rd, RRDDIM_FLAG_UPDATED) && rd->counter > 1 && iterations < st->gap_when_lost_iterations_above)) {
996                 rd->values[st->current_entry] = pack_storage_number(new_value, storage_flags );
997                 rd->last_stored_value = new_value;
998
999                 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
1000                     debug(D_RRD_STATS, "%s/%s: STORE[%ld] "
1001                             CALCULATED_NUMBER_FORMAT " = " CALCULATED_NUMBER_FORMAT
1002                           , st->id, rd->name
1003                           , st->current_entry
1004                           , unpack_storage_number(rd->values[st->current_entry]), new_value
1005                     );
1006             }
1007             else {
1008                 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
1009                     debug(D_RRD_STATS, "%s/%s: STORE[%ld] = NON EXISTING "
1010                           , st->id, rd->name
1011                           , st->current_entry
1012                     );
1013                 rd->values[st->current_entry] = pack_storage_number(0, SN_NOT_EXISTS);
1014                 rd->last_stored_value = NAN;
1015             }
1016
1017             stored_entries++;
1018
1019             if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG))) {
1020                 calculated_number t1 = new_value * (calculated_number)rd->multiplier / (calculated_number)rd->divisor;
1021                 calculated_number t2 = unpack_storage_number(rd->values[st->current_entry]);
1022                 calculated_number accuracy = accuracy_loss(t1, t2);
1023                 debug(D_RRD_STATS, "%s/%s: UNPACK[%ld] = " CALCULATED_NUMBER_FORMAT " FLAGS=0x%08x (original = " CALCULATED_NUMBER_FORMAT ", accuracy loss = " CALCULATED_NUMBER_FORMAT "%%%s)"
1024                       , st->id, rd->name
1025                       , st->current_entry
1026                       , t2
1027                       , get_storage_number_flags(rd->values[st->current_entry])
1028                       , t1
1029                       , accuracy
1030                       , (accuracy > ACCURACY_LOSS) ? " **TOO BIG** " : ""
1031                 );
1032
1033                 rd->collected_volume += t1;
1034                 rd->stored_volume += t2;
1035                 accuracy = accuracy_loss(rd->collected_volume, rd->stored_volume);
1036                 debug(D_RRD_STATS, "%s/%s: VOLUME[%ld] = " CALCULATED_NUMBER_FORMAT ", calculated  = " CALCULATED_NUMBER_FORMAT ", accuracy loss = " CALCULATED_NUMBER_FORMAT "%%%s"
1037                       , st->id, rd->name
1038                       , st->current_entry
1039                       , rd->stored_volume
1040                       , rd->collected_volume
1041                       , accuracy
1042                       , (accuracy > ACCURACY_LOSS) ? " **TOO BIG** " : ""
1043                 );
1044
1045             }
1046         }
1047         // reset the storage flags for the next point, if any;
1048         storage_flags = SN_EXISTS;
1049
1050         st->counter++;
1051         st->current_entry = ((st->current_entry + 1) >= st->entries) ? 0 : st->current_entry + 1;
1052         last_stored_ut = next_store_ut;
1053     }
1054
1055     st->last_collected_total  = st->collected_total;
1056
1057     rrddim_foreach_read(rd, st) {
1058         if(unlikely(!rrddim_flag_check(rd, RRDDIM_FLAG_UPDATED)))
1059             continue;
1060
1061         if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
1062             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);
1063
1064         rd->last_collected_value = rd->collected_value;
1065
1066         switch(rd->algorithm) {
1067             case RRD_ALGORITHM_INCREMENTAL:
1068                 if(unlikely(!first_entry)) {
1069                     if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
1070                         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);
1071                     rd->last_calculated_value += rd->calculated_value;
1072                 }
1073                 else {
1074                     if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
1075                         debug(D_RRD_STATS, "%s: THIS IS THE FIRST POINT", st->name);
1076                 }
1077                 break;
1078
1079             case RRD_ALGORITHM_ABSOLUTE:
1080             case RRD_ALGORITHM_PCENT_OVER_ROW_TOTAL:
1081             case RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL:
1082                 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
1083                     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);
1084                 rd->last_calculated_value = rd->calculated_value;
1085                 break;
1086         }
1087
1088         rd->calculated_value = 0;
1089         rd->collected_value = 0;
1090         rrddim_flag_clear(rd, RRDDIM_FLAG_UPDATED);
1091
1092         if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
1093             debug(D_RRD_STATS, "%s/%s: END "
1094                     " last_collected_value = " COLLECTED_NUMBER_FORMAT
1095                     " collected_value = " COLLECTED_NUMBER_FORMAT
1096                     " last_calculated_value = " CALCULATED_NUMBER_FORMAT
1097                     " calculated_value = " CALCULATED_NUMBER_FORMAT
1098                                       , st->id, rd->name
1099                                       , rd->last_collected_value
1100                                       , rd->collected_value
1101                                       , rd->last_calculated_value
1102                                       , rd->calculated_value
1103             );
1104     }
1105
1106     // ALL DONE ABOUT THE DATA UPDATE
1107     // --------------------------------------------------------------------
1108
1109 /*
1110     // find if there are any obsolete dimensions (not updated recently)
1111     if(unlikely(rrd_delete_unupdated_dimensions)) {
1112
1113         for( rd = st->dimensions; likely(rd) ; rd = rd->next )
1114             if((rd->last_collected_time.tv_sec + (rrd_delete_unupdated_dimensions * st->update_every)) < st->last_collected_time.tv_sec)
1115                 break;
1116
1117         if(unlikely(rd)) {
1118             RRDDIM *last;
1119             // there is dimension to free
1120             // upgrade our read lock to a write lock
1121             pthread_rwlock_unlock(&st->rrdset_rwlock);
1122             pthread_rwlock_wrlock(&st->rrdset_rwlock);
1123
1124             for( rd = st->dimensions, last = NULL ; likely(rd) ; ) {
1125                 // remove it only it is not updated in rrd_delete_unupdated_dimensions seconds
1126
1127                 if(unlikely((rd->last_collected_time.tv_sec + (rrd_delete_unupdated_dimensions * st->update_every)) < st->last_collected_time.tv_sec)) {
1128                     info("Removing obsolete dimension '%s' (%s) of '%s' (%s).", rd->name, rd->id, st->name, st->id);
1129
1130                     if(unlikely(!last)) {
1131                         st->dimensions = rd->next;
1132                         rd->next = NULL;
1133                         rrddim_free(st, rd);
1134                         rd = st->dimensions;
1135                         continue;
1136                     }
1137                     else {
1138                         last->next = rd->next;
1139                         rd->next = NULL;
1140                         rrddim_free(st, rd);
1141                         rd = last->next;
1142                         continue;
1143                     }
1144                 }
1145
1146                 last = rd;
1147                 rd = rd->next;
1148             }
1149
1150             if(unlikely(!st->dimensions)) {
1151                 info("Disabling chart %s (%s) since it does not have any dimensions", st->name, st->id);
1152                 st->enabled = 0;
1153             }
1154         }
1155     }
1156 */
1157
1158     rrdset_unlock(st);
1159
1160     if(unlikely(pthread_setcancelstate(pthreadoldcancelstate, NULL) != 0))
1161         error("Cannot set pthread cancel state to RESTORE (%d).", pthreadoldcancelstate);
1162 }
1163