1 #define NETDATA_RRD_INTERNALS 1
4 #define RRD_DEFAULT_GAP_INTERPOLATIONS 1
6 // ----------------------------------------------------------------------------
9 int rrdset_compare(void* a, void* b) {
10 if(((RRDSET *)a)->hash < ((RRDSET *)b)->hash) return -1;
11 else if(((RRDSET *)a)->hash > ((RRDSET *)b)->hash) return 1;
12 else return strcmp(((RRDSET *)a)->id, ((RRDSET *)b)->id);
15 static RRDSET *rrdset_index_find(RRDHOST *host, const char *id, uint32_t hash) {
17 strncpyz(tmp.id, id, RRD_ID_LENGTH_MAX);
18 tmp.hash = (hash)?hash:simple_hash(tmp.id);
20 return (RRDSET *)avl_search_lock(&(host->rrdset_root_index), (avl *) &tmp);
23 // ----------------------------------------------------------------------------
26 #define rrdset_from_avlname(avlname_ptr) ((RRDSET *)((avlname_ptr) - offsetof(RRDSET, avlname)))
28 int rrdset_compare_name(void* a, void* b) {
29 RRDSET *A = rrdset_from_avlname(a);
30 RRDSET *B = rrdset_from_avlname(b);
32 // fprintf(stderr, "COMPARING: %s with %s\n", A->name, B->name);
34 if(A->hash_name < B->hash_name) return -1;
35 else if(A->hash_name > B->hash_name) return 1;
36 else return strcmp(A->name, B->name);
39 RRDSET *rrdset_index_add_name(RRDHOST *host, RRDSET *st) {
41 // fprintf(stderr, "ADDING: %s (name: %s)\n", st->id, st->name);
42 result = avl_insert_lock(&host->rrdset_root_index_name, (avl *) (&st->avlname));
43 if(result) return rrdset_from_avlname(result);
47 RRDSET *rrdset_index_del_name(RRDHOST *host, RRDSET *st) {
49 // fprintf(stderr, "DELETING: %s (name: %s)\n", st->id, st->name);
50 result = (RRDSET *)avl_remove_lock(&((host)->rrdset_root_index_name), (avl *)(&st->avlname));
51 if(result) return rrdset_from_avlname(result);
56 // ----------------------------------------------------------------------------
57 // RRDSET - find charts
59 static inline RRDSET *rrdset_index_find_name(RRDHOST *host, const char *name, uint32_t hash) {
63 tmp.hash_name = (hash)?hash:simple_hash(tmp.name);
65 // fprintf(stderr, "SEARCHING: %s\n", name);
66 result = avl_search_lock(&host->rrdset_root_index_name, (avl *) (&(tmp.avlname)));
68 RRDSET *st = rrdset_from_avlname(result);
69 if(strcmp(st->magic, RRDSET_MAGIC))
70 error("Search for RRDSET %s returned an invalid RRDSET %s (name %s)", name, st->id, st->name);
72 // fprintf(stderr, "FOUND: %s\n", name);
73 return rrdset_from_avlname(result);
75 // fprintf(stderr, "NOT FOUND: %s\n", name);
79 inline RRDSET *rrdset_find(RRDHOST *host, const char *id) {
80 debug(D_RRD_CALLS, "rrdset_find() for chart '%s' in host '%s'", id, host->hostname);
81 RRDSET *st = rrdset_index_find(host, id, 0);
85 inline RRDSET *rrdset_find_bytype(RRDHOST *host, const char *type, const char *id) {
86 debug(D_RRD_CALLS, "rrdset_find_bytype() for chart '%s.%s' in host '%s'", type, id, host->hostname);
88 char buf[RRD_ID_LENGTH_MAX + 1];
89 strncpyz(buf, type, RRD_ID_LENGTH_MAX - 1);
91 int len = (int) strlen(buf);
92 strncpyz(&buf[len], id, (size_t) (RRD_ID_LENGTH_MAX - len));
94 return(rrdset_find(host, buf));
97 inline RRDSET *rrdset_find_byname(RRDHOST *host, const char *name) {
98 debug(D_RRD_CALLS, "rrdset_find_byname() for chart '%s' in host '%s'", name, host->hostname);
99 RRDSET *st = rrdset_index_find_name(host, name, 0);
103 // ----------------------------------------------------------------------------
104 // RRDSET - rename charts
106 char *rrdset_strncpyz_name(char *to, const char *from, size_t length) {
109 while (length-- && (c = *from++)) {
110 if(c != '.' && !isalnum(c))
121 void rrdset_set_name(RRDSET *st, const char *name) {
122 if(unlikely(st->name && !strcmp(st->name, name)))
125 debug(D_RRD_CALLS, "rrdset_set_name() old: %s, new: %s", st->name, name);
127 char b[CONFIG_MAX_VALUE + 1];
128 char n[RRD_ID_LENGTH_MAX + 1];
130 snprintfz(n, RRD_ID_LENGTH_MAX, "%s.%s", st->type, name);
131 rrdset_strncpyz_name(b, n, CONFIG_MAX_VALUE);
134 rrdset_index_del_name(st->rrdhost, st);
135 st->name = config_set_default(st->config_section, "name", b);
136 st->hash_name = simple_hash(st->name);
137 rrdsetvar_rename_all(st);
140 st->name = config_get(st->config_section, "name", b);
141 st->hash_name = simple_hash(st->name);
146 for(rd = st->dimensions; rd ;rd = rd->next)
147 rrddimvar_rename_all(rd);
150 if(unlikely(rrdset_index_add_name(st->rrdhost, st) != st))
151 error("RRDSET: INTERNAL ERROR: attempted to index duplicate chart name '%s'", st->name);
155 // ----------------------------------------------------------------------------
156 // RRDSET - reset a chart
158 void rrdset_reset(RRDSET *st) {
159 debug(D_RRD_CALLS, "rrdset_reset() %s", st->name);
161 st->last_collected_time.tv_sec = 0;
162 st->last_collected_time.tv_usec = 0;
163 st->last_updated.tv_sec = 0;
164 st->last_updated.tv_usec = 0;
165 st->current_entry = 0;
167 st->counter_done = 0;
170 for(rd = st->dimensions; rd ; rd = rd->next) {
171 rd->last_collected_time.tv_sec = 0;
172 rd->last_collected_time.tv_usec = 0;
174 memset(rd->values, 0, rd->entries * sizeof(storage_number));
178 // ----------------------------------------------------------------------------
179 // RRDSET - helpers for rrdset_create()
181 static inline long align_entries_to_pagesize(long entries) {
182 if(entries < 5) entries = 5;
183 if(entries > RRD_HISTORY_ENTRIES_MAX) entries = RRD_HISTORY_ENTRIES_MAX;
185 #ifdef NETDATA_LOG_ALLOCATIONS
186 long page = (size_t)sysconf(_SC_PAGESIZE);
188 long size = sizeof(RRDDIM) + entries * sizeof(storage_number);
190 size -= (size % page);
193 long n = (size - sizeof(RRDDIM)) / sizeof(storage_number);
203 static inline void timeval_align(struct timeval *tv, int update_every) {
204 tv->tv_sec -= tv->tv_sec % update_every;
205 tv->tv_usec = 500000;
208 // ----------------------------------------------------------------------------
209 // RRDSET - free a chart
211 void rrdset_free(RRDSET *st) {
214 while(st->variables) rrdsetvar_free(st->variables);
215 while(st->alarms) rrdsetcalc_unlink(st->alarms);
216 while(st->dimensions) rrddim_free(st, st->dimensions);
218 if(unlikely(rrdset_index_del(st->rrdhost, st) != st))
219 error("RRDSET: INTERNAL ERROR: attempt to remove from index chart '%s', removed a different chart.", st->id);
221 rrdset_index_del_name(st->rrdhost, st);
223 st->rrdfamily->use_count--;
224 if(!st->rrdfamily->use_count)
225 rrdfamily_free(st->rrdhost, st->rrdfamily);
229 // free directly allocated memory
230 freez(st->config_section);
232 if(st->rrd_memory_mode == RRD_MEMORY_MODE_SAVE || st->rrd_memory_mode == RRD_MEMORY_MODE_MAP) {
233 debug(D_RRD_CALLS, "Unmapping stats '%s'.", st->name);
234 munmap(st, st->memsize);
240 // ----------------------------------------------------------------------------
241 // RRDSET - create a chart
243 RRDSET *rrdset_create(RRDHOST *host, const char *type, const char *id, const char *name, const char *family
244 , const char *context, const char *title, const char *units, long priority
245 , int update_every, RRDSET_TYPE chart_type) {
247 if(!type || !type[0]) {
248 fatal("Cannot create rrd stats without a type.");
253 fatal("Cannot create rrd stats without an id.");
257 // ------------------------------------------------------------------------
258 // check if it already exists
260 char fullid[RRD_ID_LENGTH_MAX + 1];
261 snprintfz(fullid, RRD_ID_LENGTH_MAX, "%s.%s", type, id);
263 RRDSET *st = rrdset_find(host, fullid);
265 debug(D_RRD_CALLS, "RRDSET '%s', already exists.", fullid);
269 char fullfilename[FILENAME_MAX + 1];
271 // ------------------------------------------------------------------------
272 // compose the config_section for this chart
274 char config_section[RRD_ID_LENGTH_MAX + 1];
275 if(host == localhost)
276 strcpy(config_section, fullid);
278 snprintfz(config_section, RRD_ID_LENGTH_MAX, "%s/%s", host->machine_guid, fullid);
280 // ------------------------------------------------------------------------
281 // get the options from the config, we need to create it
283 long rentries = config_get_number(config_section, "history", host->rrd_history_entries);
284 long entries = align_entries_to_pagesize(rentries);
285 if(entries != rentries) entries = config_set_number(config_section, "history", entries);
287 int enabled = config_get_boolean(config_section, "enabled", 1);
288 if(!enabled) entries = 5;
290 unsigned long size = sizeof(RRDSET);
291 char *cache_dir = rrdset_cache_dir(host, fullid, config_section);
293 // ------------------------------------------------------------------------
294 // load it or allocate it
296 debug(D_RRD_CALLS, "Creating RRD_STATS for '%s.%s'.", type, id);
298 snprintfz(fullfilename, FILENAME_MAX, "%s/main.db", cache_dir);
299 if(host->rrd_memory_mode != RRD_MEMORY_MODE_RAM) {
300 st = (RRDSET *) mymmap(fullfilename, size, ((host->rrd_memory_mode == RRD_MEMORY_MODE_MAP) ? MAP_SHARED : MAP_PRIVATE), 0);
302 memset(&st->avl, 0, sizeof(avl));
303 memset(&st->avlname, 0, sizeof(avl));
304 memset(&st->variables_root_index, 0, sizeof(avl_tree_lock));
305 memset(&st->dimensions_index, 0, sizeof(avl_tree_lock));
306 memset(&st->rrdset_rwlock, 0, sizeof(pthread_rwlock_t));
314 st->dimensions = NULL;
316 st->variables = NULL;
318 st->flags = 0x00000000;
320 if(strcmp(st->magic, RRDSET_MAGIC) != 0) {
322 info("Initializing file %s.", fullfilename);
325 else if(strcmp(st->id, fullid) != 0) {
327 error("File %s contents are not for chart %s. Clearing it.", fullfilename, fullid);
332 else if(st->memsize != size || st->entries != entries) {
334 error("File %s does not have the desired size. Clearing it.", fullfilename);
337 else if(st->update_every != update_every) {
339 error("File %s does not have the desired update frequency. Clearing it.", fullfilename);
342 else if((now_realtime_sec() - st->last_updated.tv_sec) > update_every * entries) {
344 error("File %s is too old. Clearing it.", fullfilename);
348 // make sure the database is aligned
349 if(st->last_updated.tv_sec)
350 timeval_align(&st->last_updated, update_every);
352 // make sure we have the right memory mode
353 // even if we cleared the memory
354 st->rrd_memory_mode = host->rrd_memory_mode;
359 st = callocz(1, size);
360 st->rrd_memory_mode = RRD_MEMORY_MODE_RAM;
363 st->config_section = strdup(config_section);
366 st->entries = entries;
367 st->update_every = update_every;
369 if(st->current_entry >= st->entries) st->current_entry = 0;
371 strcpy(st->cache_filename, fullfilename);
372 strcpy(st->magic, RRDSET_MAGIC);
374 strcpy(st->id, fullid);
375 st->hash = simple_hash(st->id);
377 st->cache_dir = cache_dir;
379 st->chart_type = rrdset_type_id(config_get(st->config_section, "chart type", rrdset_type_name(chart_type)));
380 st->type = config_get(st->config_section, "type", type);
381 st->family = config_get(st->config_section, "family", family?family:st->type);
382 st->units = config_get(st->config_section, "units", units?units:"");
384 st->context = config_get(st->config_section, "context", context?context:st->id);
385 st->hash_context = simple_hash(st->context);
387 st->priority = config_get_number(st->config_section, "priority", priority);
389 rrdset_flag_set(st, RRDSET_FLAG_ENABLED);
391 rrdset_flag_clear(st, RRDSET_FLAG_ENABLED);
393 rrdset_flag_clear(st, RRDSET_FLAG_DETAIL);
394 rrdset_flag_clear(st, RRDSET_FLAG_DEBUG);
396 // if(!strcmp(st->id, "disk_util.dm-0")) {
398 // error("enabled debugging for '%s'", st->id);
400 // else error("not enabled debugging for '%s'", st->id);
405 st->last_collected_time.tv_sec = 0;
406 st->last_collected_time.tv_usec = 0;
407 st->counter_done = 0;
409 st->gap_when_lost_iterations_above = (int) (
410 config_get_number(st->config_section, "gap when lost iterations above", RRD_DEFAULT_GAP_INTERPOLATIONS) + 2);
412 avl_init_lock(&st->dimensions_index, rrddim_compare);
413 avl_init_lock(&st->variables_root_index, rrdvar_compare);
415 pthread_rwlock_init(&st->rrdset_rwlock, NULL);
416 rrdhost_wrlock(host);
418 if(name && *name) rrdset_set_name(st, name);
419 else rrdset_set_name(st, id);
422 char varvalue[CONFIG_MAX_VALUE + 1];
423 char varvalue2[CONFIG_MAX_VALUE + 1];
424 snprintfz(varvalue, CONFIG_MAX_VALUE, "%s (%s)", title?title:"", st->name);
425 json_escape_string(varvalue2, varvalue, sizeof(varvalue2));
426 st->title = config_get(st->config_section, "title", varvalue2);
429 st->rrdfamily = rrdfamily_create(host, st->family);
431 st->next = host->rrdset_root;
432 host->rrdset_root = st;
434 if(host->health_enabled) {
435 rrdsetvar_create(st, "last_collected_t", RRDVAR_TYPE_TIME_T, &st->last_collected_time.tv_sec, 0);
436 rrdsetvar_create(st, "collected_total_raw", RRDVAR_TYPE_TOTAL, &st->last_collected_total, 0);
437 rrdsetvar_create(st, "green", RRDVAR_TYPE_CALCULATED, &st->green, 0);
438 rrdsetvar_create(st, "red", RRDVAR_TYPE_CALCULATED, &st->red, 0);
439 rrdsetvar_create(st, "update_every", RRDVAR_TYPE_INT, &st->update_every, 0);
442 if(unlikely(rrdset_index_add(host, st) != st))
443 error("RRDSET: INTERNAL ERROR: attempt to index duplicate chart '%s'", st->id);
445 rrdsetcalc_link_matching(st);
446 rrdcalctemplate_link_matching(st);
448 rrdhost_unlock(host);
454 // ----------------------------------------------------------------------------
455 // RRDSET - data collection iteration control
457 void rrdset_next_usec_unfiltered(RRDSET *st, usec_t microseconds) {
458 if(unlikely(!st->last_collected_time.tv_sec || !microseconds)) {
460 microseconds = st->update_every * USEC_PER_SEC;
462 st->usec_since_last_update = microseconds;
465 void rrdset_next_usec(RRDSET *st, usec_t microseconds)
468 now_realtime_timeval(&now);
470 if(unlikely(!st->last_collected_time.tv_sec)) {
472 microseconds = st->update_every * USEC_PER_SEC;
474 else if(unlikely(!microseconds)) {
475 // no dt given by the plugin
476 microseconds = dt_usec(&now, &st->last_collected_time);
479 // microseconds has the time since the last collection
480 #ifdef NETDATA_INTERNAL_CHECKS
481 usec_t now_usec = timeval_usec(&now);
482 usec_t last_usec = timeval_usec(&st->last_collected_time);
484 usec_t since_last_usec = dt_usec(&now, &st->last_collected_time);
486 // verify the microseconds given is good
487 if(unlikely(microseconds > since_last_usec)) {
488 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);
490 #ifdef NETDATA_INTERNAL_CHECKS
491 if(unlikely(last_usec + microseconds > now_usec + 1000))
492 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);
495 microseconds = since_last_usec;
497 else if(unlikely(microseconds < since_last_usec * 0.8)) {
498 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);
500 #ifdef NETDATA_INTERNAL_CHECKS
501 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);
503 microseconds = since_last_usec;
506 debug(D_RRD_CALLS, "rrdset_next_usec() for chart %s with microseconds %llu", st->name, microseconds);
508 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
509 debug(D_RRD_STATS, "%s: NEXT: %llu microseconds", st->name, microseconds);
511 st->usec_since_last_update = microseconds;
515 // ----------------------------------------------------------------------------
516 // RRDSET - process the collected values for all dimensions of a chart
518 usec_t rrdset_done(RRDSET *st) {
519 if(unlikely(netdata_exit)) return 0;
521 debug(D_RRD_CALLS, "rrdset_done() for chart %s", st->name);
526 pthreadoldcancelstate; // store the old cancelable pthread state, to restore it at the end
529 store_this_entry = 1, // boolean: 1 = store this entry, 0 = don't store this entry
530 first_entry = 0; // boolean: 1 = this is the first entry seen for this chart, 0 = all other entries
533 stored_entries = 0; // the number of entries we have stored in the db, during this call to rrdset_done()
536 last_collect_ut, // the timestamp in microseconds, of the last collected value
537 now_collect_ut, // the timestamp in microseconds, of this collected value (this is NOW)
538 last_stored_ut, // the timestamp in microseconds, of the last stored entry in the db
539 next_store_ut, // the timestamp in microseconds, of the next entry to store in the db
540 update_every_ut = st->update_every * USEC_PER_SEC; // st->update_every in microseconds
542 if(unlikely(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &pthreadoldcancelstate) != 0))
543 error("Cannot set pthread cancel state to DISABLE.");
545 // a read lock is OK here
549 // enable the chart, if it was disabled
550 if(unlikely(rrd_delete_unupdated_dimensions) && !st->enabled)
554 // check if the chart has a long time to be updated
555 if(unlikely(st->usec_since_last_update > st->entries * update_every_ut)) {
556 info("%s: took too long to be updated (%0.3Lf secs). Resetting it.", st->name, (long double)(st->usec_since_last_update / 1000000.0));
558 st->usec_since_last_update = update_every_ut;
562 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
563 debug(D_RRD_STATS, "%s: microseconds since last update: %llu", st->name, st->usec_since_last_update);
565 // set last_collected_time
566 if(unlikely(!st->last_collected_time.tv_sec)) {
567 // it is the first entry
568 // set the last_collected_time to now
569 now_realtime_timeval(&st->last_collected_time);
570 timeval_align(&st->last_collected_time, st->update_every);
572 last_collect_ut = st->last_collected_time.tv_sec * USEC_PER_SEC + st->last_collected_time.tv_usec - update_every_ut;
574 // the first entry should not be stored
575 store_this_entry = 0;
578 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
579 debug(D_RRD_STATS, "%s: has not set last_collected_time. Setting it now. Will not store the next entry.", st->name);
582 // it is not the first entry
583 // calculate the proper last_collected_time, using usec_since_last_update
584 last_collect_ut = st->last_collected_time.tv_sec * USEC_PER_SEC + st->last_collected_time.tv_usec;
585 usec_t ut = last_collect_ut + st->usec_since_last_update;
586 st->last_collected_time.tv_sec = (time_t) (ut / USEC_PER_SEC);
587 st->last_collected_time.tv_usec = (suseconds_t) (ut % USEC_PER_SEC);
590 // if this set has not been updated in the past
591 // we fake the last_update time to be = now - usec_since_last_update
592 if(unlikely(!st->last_updated.tv_sec)) {
593 // it has never been updated before
594 // set a fake last_updated, in the past using usec_since_last_update
595 usec_t ut = st->last_collected_time.tv_sec * USEC_PER_SEC + st->last_collected_time.tv_usec - st->usec_since_last_update;
596 st->last_updated.tv_sec = (time_t) (ut / USEC_PER_SEC);
597 st->last_updated.tv_usec = (suseconds_t) (ut % USEC_PER_SEC);
599 // the first entry should not be stored
600 store_this_entry = 0;
603 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
604 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);
607 // check if we will re-write the entire data set
608 if(unlikely(dt_usec(&st->last_collected_time, &st->last_updated) > st->entries * update_every_ut)) {
609 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);
612 st->usec_since_last_update = update_every_ut;
614 now_realtime_timeval(&st->last_collected_time);
615 timeval_align(&st->last_collected_time, st->update_every);
617 usec_t ut = st->last_collected_time.tv_sec * USEC_PER_SEC + st->last_collected_time.tv_usec - st->usec_since_last_update;
618 st->last_updated.tv_sec = (time_t) (ut / USEC_PER_SEC);
619 st->last_updated.tv_usec = (suseconds_t) (ut % USEC_PER_SEC);
621 // the first entry should not be stored
622 store_this_entry = 0;
626 // these are the 3 variables that will help us in interpolation
627 // last_stored_ut = the last time we added a value to the storage
628 // now_collect_ut = the time the current value has been collected
629 // next_store_ut = the time of the next interpolation point
630 last_stored_ut = st->last_updated.tv_sec * USEC_PER_SEC + st->last_updated.tv_usec;
631 now_collect_ut = st->last_collected_time.tv_sec * USEC_PER_SEC + st->last_collected_time.tv_usec;
632 next_store_ut = (st->last_updated.tv_sec + st->update_every) * USEC_PER_SEC;
634 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG))) {
635 debug(D_RRD_STATS, "%s: last_collect_ut = %0.3Lf (last collection time)", st->name, (long double)last_collect_ut/1000000.0);
636 debug(D_RRD_STATS, "%s: now_collect_ut = %0.3Lf (current collection time)", st->name, (long double)now_collect_ut/1000000.0);
637 debug(D_RRD_STATS, "%s: last_stored_ut = %0.3Lf (last updated time)", st->name, (long double)last_stored_ut/1000000.0);
638 debug(D_RRD_STATS, "%s: next_store_ut = %0.3Lf (next interpolation point)", st->name, (long double)next_store_ut/1000000.0);
641 if(unlikely(!st->counter_done)) {
642 store_this_entry = 0;
643 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
644 debug(D_RRD_STATS, "%s: Will not store the next entry.", st->name);
648 // calculate totals and count the dimensions
650 st->collected_total = 0;
651 for( rd = st->dimensions, dimensions = 0 ; rd ; rd = rd->next, dimensions++ )
652 if(likely(rrddim_flag_check(rd, RRDDIM_FLAG_UPDATED)))
653 st->collected_total += rd->collected_value;
655 uint32_t storage_flags = SN_EXISTS;
657 // process all dimensions to calculate their values
658 // based on the collected figures only
659 // at this stage we do not interpolate anything
660 for( rd = st->dimensions ; rd ; rd = rd->next ) {
662 if(unlikely(!rrddim_flag_check(rd, RRDDIM_FLAG_UPDATED))) {
663 rd->calculated_value = 0;
667 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
668 debug(D_RRD_STATS, "%s/%s: START "
669 " last_collected_value = " COLLECTED_NUMBER_FORMAT
670 " collected_value = " COLLECTED_NUMBER_FORMAT
671 " last_calculated_value = " CALCULATED_NUMBER_FORMAT
672 " calculated_value = " CALCULATED_NUMBER_FORMAT
674 , rd->last_collected_value
675 , rd->collected_value
676 , rd->last_calculated_value
677 , rd->calculated_value
680 switch(rd->algorithm) {
681 case RRD_ALGORITHM_ABSOLUTE:
682 rd->calculated_value = (calculated_number)rd->collected_value
683 * (calculated_number)rd->multiplier
684 / (calculated_number)rd->divisor;
686 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
687 debug(D_RRD_STATS, "%s/%s: CALC ABS/ABS-NO-IN "
688 CALCULATED_NUMBER_FORMAT " = "
689 COLLECTED_NUMBER_FORMAT
690 " * " CALCULATED_NUMBER_FORMAT
691 " / " CALCULATED_NUMBER_FORMAT
693 , rd->calculated_value
694 , rd->collected_value
695 , (calculated_number)rd->multiplier
696 , (calculated_number)rd->divisor
700 case RRD_ALGORITHM_PCENT_OVER_ROW_TOTAL:
701 if(unlikely(!st->collected_total))
702 rd->calculated_value = 0;
704 // the percentage of the current value
705 // over the total of all dimensions
706 rd->calculated_value =
707 (calculated_number)100
708 * (calculated_number)rd->collected_value
709 / (calculated_number)st->collected_total;
711 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
712 debug(D_RRD_STATS, "%s/%s: CALC PCENT-ROW "
713 CALCULATED_NUMBER_FORMAT " = 100"
714 " * " COLLECTED_NUMBER_FORMAT
715 " / " COLLECTED_NUMBER_FORMAT
717 , rd->calculated_value
718 , rd->collected_value
719 , st->collected_total
723 case RRD_ALGORITHM_INCREMENTAL:
724 if(unlikely(rd->counter <= 1)) {
725 rd->calculated_value = 0;
729 // if the new is smaller than the old (an overflow, or reset), set the old equal to the new
730 // to reset the calculation (it will give zero as the calculation for this second)
731 if(unlikely(rd->last_collected_value > rd->collected_value)) {
732 debug(D_RRD_STATS, "%s.%s: RESET or OVERFLOW. Last collected value = " COLLECTED_NUMBER_FORMAT ", current = " COLLECTED_NUMBER_FORMAT
734 , rd->last_collected_value
735 , rd->collected_value);
737 if(!(rrddim_flag_check(rd, RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS)))
738 storage_flags = SN_EXISTS_RESET;
740 rd->last_collected_value = rd->collected_value;
743 rd->calculated_value +=
744 (calculated_number)(rd->collected_value - rd->last_collected_value)
745 * (calculated_number)rd->multiplier
746 / (calculated_number)rd->divisor;
748 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
749 debug(D_RRD_STATS, "%s/%s: CALC INC PRE "
750 CALCULATED_NUMBER_FORMAT " = ("
751 COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT
753 " * " CALCULATED_NUMBER_FORMAT
754 " / " CALCULATED_NUMBER_FORMAT
756 , rd->calculated_value
757 , rd->collected_value, rd->last_collected_value
758 , (calculated_number)rd->multiplier
759 , (calculated_number)rd->divisor
763 case RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL:
764 if(unlikely(rd->counter <= 1)) {
765 rd->calculated_value = 0;
769 // if the new is smaller than the old (an overflow, or reset), set the old equal to the new
770 // to reset the calculation (it will give zero as the calculation for this second)
771 if(unlikely(rd->last_collected_value > rd->collected_value)) {
772 debug(D_RRD_STATS, "%s.%s: RESET or OVERFLOW. Last collected value = " COLLECTED_NUMBER_FORMAT ", current = " COLLECTED_NUMBER_FORMAT
774 , rd->last_collected_value
775 , rd->collected_value);
777 if(!(rrddim_flag_check(rd, RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS)))
778 storage_flags = SN_EXISTS_RESET;
780 rd->last_collected_value = rd->collected_value;
783 // the percentage of the current increment
784 // over the increment of all dimensions together
785 if(unlikely(st->collected_total == st->last_collected_total))
786 rd->calculated_value = 0;
788 rd->calculated_value =
789 (calculated_number)100
790 * (calculated_number)(rd->collected_value - rd->last_collected_value)
791 / (calculated_number)(st->collected_total - st->last_collected_total);
793 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
794 debug(D_RRD_STATS, "%s/%s: CALC PCENT-DIFF "
795 CALCULATED_NUMBER_FORMAT " = 100"
796 " * (" COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT ")"
797 " / (" COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT ")"
799 , rd->calculated_value
800 , rd->collected_value, rd->last_collected_value
801 , st->collected_total, st->last_collected_total
806 // make the default zero, to make sure
807 // it gets noticed when we add new types
808 rd->calculated_value = 0;
810 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
811 debug(D_RRD_STATS, "%s/%s: CALC "
812 CALCULATED_NUMBER_FORMAT " = 0"
814 , rd->calculated_value
819 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
820 debug(D_RRD_STATS, "%s/%s: PHASE2 "
821 " last_collected_value = " COLLECTED_NUMBER_FORMAT
822 " collected_value = " COLLECTED_NUMBER_FORMAT
823 " last_calculated_value = " CALCULATED_NUMBER_FORMAT
824 " calculated_value = " CALCULATED_NUMBER_FORMAT
826 , rd->last_collected_value
827 , rd->collected_value
828 , rd->last_calculated_value
829 , rd->calculated_value
834 // at this point we have all the calculated values ready
835 // it is now time to interpolate values on a second boundary
837 if(unlikely(now_collect_ut < next_store_ut)) {
838 // this is collected in the same interpolation point
840 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
841 debug(D_RRD_STATS, "%s: THIS IS IN THE SAME INTERPOLATION POINT", st->name);
843 #ifdef NETDATA_INTERNAL_CHECKS
844 info("%s is collected in the same interpolation point: short by %llu microseconds", st->name, next_store_ut - now_collect_ut);
848 usec_t first_ut = last_stored_ut;
849 long long iterations = (now_collect_ut - last_stored_ut) / (update_every_ut);
850 if((now_collect_ut % (update_every_ut)) == 0) iterations++;
852 for( ; next_store_ut <= now_collect_ut ; last_collect_ut = next_store_ut, next_store_ut += update_every_ut, iterations-- ) {
853 #ifdef NETDATA_INTERNAL_CHECKS
854 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); }
857 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG))) {
858 debug(D_RRD_STATS, "%s: last_stored_ut = %0.3Lf (last updated time)", st->name, (long double)last_stored_ut/1000000.0);
859 debug(D_RRD_STATS, "%s: next_store_ut = %0.3Lf (next interpolation point)", st->name, (long double)next_store_ut/1000000.0);
862 st->last_updated.tv_sec = (time_t) (next_store_ut / USEC_PER_SEC);
863 st->last_updated.tv_usec = 0;
865 for( rd = st->dimensions ; likely(rd) ; rd = rd->next ) {
866 calculated_number new_value;
868 switch(rd->algorithm) {
869 case RRD_ALGORITHM_INCREMENTAL:
870 new_value = (calculated_number)
871 ( rd->calculated_value
872 * (calculated_number)(next_store_ut - last_collect_ut)
873 / (calculated_number)(now_collect_ut - last_collect_ut)
876 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
877 debug(D_RRD_STATS, "%s/%s: CALC2 INC "
878 CALCULATED_NUMBER_FORMAT " = "
879 CALCULATED_NUMBER_FORMAT
884 , rd->calculated_value
885 , (next_store_ut - last_stored_ut)
886 , (now_collect_ut - last_stored_ut)
889 rd->calculated_value -= new_value;
890 new_value += rd->last_calculated_value;
891 rd->last_calculated_value = 0;
892 new_value /= (calculated_number)st->update_every;
894 if(unlikely(next_store_ut - last_stored_ut < update_every_ut)) {
895 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
896 debug(D_RRD_STATS, "%s/%s: COLLECTION POINT IS SHORT " CALCULATED_NUMBER_FORMAT " - EXTRAPOLATING",
898 , (calculated_number)(next_store_ut - last_stored_ut)
900 new_value = new_value * (calculated_number)(st->update_every * 1000000) / (calculated_number)(next_store_ut - last_stored_ut);
904 case RRD_ALGORITHM_ABSOLUTE:
905 case RRD_ALGORITHM_PCENT_OVER_ROW_TOTAL:
906 case RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL:
908 if(iterations == 1) {
909 // this is the last iteration
910 // do not interpolate
911 // just show the calculated value
913 new_value = rd->calculated_value;
916 // we have missed an update
917 // interpolate in the middle values
919 new_value = (calculated_number)
920 ( ( (rd->calculated_value - rd->last_calculated_value)
921 * (calculated_number)(next_store_ut - last_collect_ut)
922 / (calculated_number)(now_collect_ut - last_collect_ut)
924 + rd->last_calculated_value
927 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
928 debug(D_RRD_STATS, "%s/%s: CALC2 DEF "
929 CALCULATED_NUMBER_FORMAT " = ((("
930 "(" CALCULATED_NUMBER_FORMAT " - " CALCULATED_NUMBER_FORMAT ")"
932 " / %llu) + " CALCULATED_NUMBER_FORMAT
935 , rd->calculated_value, rd->last_calculated_value
936 , (next_store_ut - first_ut)
937 , (now_collect_ut - first_ut), rd->last_calculated_value
943 if(unlikely(!store_this_entry)) {
944 rd->values[st->current_entry] = pack_storage_number(0, SN_NOT_EXISTS);
948 if(likely(rrddim_flag_check(rd, RRDDIM_FLAG_UPDATED) && rd->counter > 1 && iterations < st->gap_when_lost_iterations_above)) {
949 rd->values[st->current_entry] = pack_storage_number(new_value, storage_flags );
950 rd->last_stored_value = new_value;
952 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
953 debug(D_RRD_STATS, "%s/%s: STORE[%ld] "
954 CALCULATED_NUMBER_FORMAT " = " CALCULATED_NUMBER_FORMAT
957 , unpack_storage_number(rd->values[st->current_entry]), new_value
961 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
962 debug(D_RRD_STATS, "%s/%s: STORE[%ld] = NON EXISTING "
966 rd->values[st->current_entry] = pack_storage_number(0, SN_NOT_EXISTS);
967 rd->last_stored_value = NAN;
972 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG))) {
973 calculated_number t1 = new_value * (calculated_number)rd->multiplier / (calculated_number)rd->divisor;
974 calculated_number t2 = unpack_storage_number(rd->values[st->current_entry]);
975 calculated_number accuracy = accuracy_loss(t1, t2);
976 debug(D_RRD_STATS, "%s/%s: UNPACK[%ld] = " CALCULATED_NUMBER_FORMAT " FLAGS=0x%08x (original = " CALCULATED_NUMBER_FORMAT ", accuracy loss = " CALCULATED_NUMBER_FORMAT "%%%s)"
980 , get_storage_number_flags(rd->values[st->current_entry])
983 , (accuracy > ACCURACY_LOSS) ? " **TOO BIG** " : ""
986 rd->collected_volume += t1;
987 rd->stored_volume += t2;
988 accuracy = accuracy_loss(rd->collected_volume, rd->stored_volume);
989 debug(D_RRD_STATS, "%s/%s: VOLUME[%ld] = " CALCULATED_NUMBER_FORMAT ", calculated = " CALCULATED_NUMBER_FORMAT ", accuracy loss = " CALCULATED_NUMBER_FORMAT "%%%s"
993 , rd->collected_volume
995 , (accuracy > ACCURACY_LOSS) ? " **TOO BIG** " : ""
1000 // reset the storage flags for the next point, if any;
1001 storage_flags = SN_EXISTS;
1004 st->current_entry = ((st->current_entry + 1) >= st->entries) ? 0 : st->current_entry + 1;
1005 last_stored_ut = next_store_ut;
1008 st->last_collected_total = st->collected_total;
1010 for( rd = st->dimensions; rd ; rd = rd->next ) {
1011 if(unlikely(!rrddim_flag_check(rd, RRDDIM_FLAG_UPDATED)))
1014 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
1015 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);
1017 rd->last_collected_value = rd->collected_value;
1019 switch(rd->algorithm) {
1020 case RRD_ALGORITHM_INCREMENTAL:
1021 if(unlikely(!first_entry)) {
1022 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
1023 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);
1024 rd->last_calculated_value += rd->calculated_value;
1027 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
1028 debug(D_RRD_STATS, "%s: THIS IS THE FIRST POINT", st->name);
1032 case RRD_ALGORITHM_ABSOLUTE:
1033 case RRD_ALGORITHM_PCENT_OVER_ROW_TOTAL:
1034 case RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL:
1035 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
1036 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);
1037 rd->last_calculated_value = rd->calculated_value;
1041 rd->calculated_value = 0;
1042 rd->collected_value = 0;
1043 rrddim_flag_clear(rd, RRDDIM_FLAG_UPDATED);
1045 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
1046 debug(D_RRD_STATS, "%s/%s: END "
1047 " last_collected_value = " COLLECTED_NUMBER_FORMAT
1048 " collected_value = " COLLECTED_NUMBER_FORMAT
1049 " last_calculated_value = " CALCULATED_NUMBER_FORMAT
1050 " calculated_value = " CALCULATED_NUMBER_FORMAT
1052 , rd->last_collected_value
1053 , rd->collected_value
1054 , rd->last_calculated_value
1055 , rd->calculated_value
1059 // ALL DONE ABOUT THE DATA UPDATE
1060 // --------------------------------------------------------------------
1063 // find if there are any obsolete dimensions (not updated recently)
1064 if(unlikely(rrd_delete_unupdated_dimensions)) {
1066 for( rd = st->dimensions; likely(rd) ; rd = rd->next )
1067 if((rd->last_collected_time.tv_sec + (rrd_delete_unupdated_dimensions * st->update_every)) < st->last_collected_time.tv_sec)
1072 // there is dimension to free
1073 // upgrade our read lock to a write lock
1074 pthread_rwlock_unlock(&st->rrdset_rwlock);
1075 pthread_rwlock_wrlock(&st->rrdset_rwlock);
1077 for( rd = st->dimensions, last = NULL ; likely(rd) ; ) {
1078 // remove it only it is not updated in rrd_delete_unupdated_dimensions seconds
1080 if(unlikely((rd->last_collected_time.tv_sec + (rrd_delete_unupdated_dimensions * st->update_every)) < st->last_collected_time.tv_sec)) {
1081 info("Removing obsolete dimension '%s' (%s) of '%s' (%s).", rd->name, rd->id, st->name, st->id);
1083 if(unlikely(!last)) {
1084 st->dimensions = rd->next;
1086 rrddim_free(st, rd);
1087 rd = st->dimensions;
1091 last->next = rd->next;
1093 rrddim_free(st, rd);
1103 if(unlikely(!st->dimensions)) {
1104 info("Disabling chart %s (%s) since it does not have any dimensions", st->name, st->id);
1113 if(unlikely(pthread_setcancelstate(pthreadoldcancelstate, NULL) != 0))
1114 error("Cannot set pthread cancel state to RESTORE (%d).", pthreadoldcancelstate);
1116 return(st->usec_since_last_update);