1 #define NETDATA_RRD_INTERNALS 1
4 #define RRD_DEFAULT_GAP_INTERPOLATIONS 1
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);
9 int ret = pthread_rwlock_trywrlock(&st->rrdset_rwlock);
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);
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);
17 int ret = pthread_rwlock_tryrdlock(&st->rrdset_rwlock);
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);
23 // ----------------------------------------------------------------------------
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);
32 static RRDSET *rrdset_index_find(RRDHOST *host, const char *id, uint32_t hash) {
34 strncpyz(tmp.id, id, RRD_ID_LENGTH_MAX);
35 tmp.hash = (hash)?hash:simple_hash(tmp.id);
37 return (RRDSET *)avl_search_lock(&(host->rrdset_root_index), (avl *) &tmp);
40 // ----------------------------------------------------------------------------
43 #define rrdset_from_avlname(avlname_ptr) ((RRDSET *)((avlname_ptr) - offsetof(RRDSET, avlname)))
45 int rrdset_compare_name(void* a, void* b) {
46 RRDSET *A = rrdset_from_avlname(a);
47 RRDSET *B = rrdset_from_avlname(b);
49 // fprintf(stderr, "COMPARING: %s with %s\n", A->name, B->name);
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);
56 RRDSET *rrdset_index_add_name(RRDHOST *host, RRDSET *st) {
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);
64 RRDSET *rrdset_index_del_name(RRDHOST *host, RRDSET *st) {
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);
73 // ----------------------------------------------------------------------------
74 // RRDSET - find charts
76 static inline RRDSET *rrdset_index_find_name(RRDHOST *host, const char *name, uint32_t hash) {
80 tmp.hash_name = (hash)?hash:simple_hash(tmp.name);
82 // fprintf(stderr, "SEARCHING: %s\n", name);
83 result = avl_search_lock(&host->rrdset_root_index_name, (avl *) (&(tmp.avlname)));
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);
89 // fprintf(stderr, "FOUND: %s\n", name);
90 return rrdset_from_avlname(result);
92 // fprintf(stderr, "NOT FOUND: %s\n", name);
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);
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);
105 char buf[RRD_ID_LENGTH_MAX + 1];
106 strncpyz(buf, type, RRD_ID_LENGTH_MAX - 1);
108 int len = (int) strlen(buf);
109 strncpyz(&buf[len], id, (size_t) (RRD_ID_LENGTH_MAX - len));
111 return(rrdset_find(host, buf));
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);
120 // ----------------------------------------------------------------------------
121 // RRDSET - rename charts
123 char *rrdset_strncpyz_name(char *to, const char *from, size_t length) {
126 while (length-- && (c = *from++)) {
127 if(c != '.' && !isalnum(c))
138 void rrdset_set_name(RRDSET *st, const char *name) {
139 if(unlikely(st->name && !strcmp(st->name, name)))
142 debug(D_RRD_CALLS, "rrdset_set_name() old: %s, new: %s", st->name, name);
144 char b[CONFIG_MAX_VALUE + 1];
145 char n[RRD_ID_LENGTH_MAX + 1];
147 snprintfz(n, RRD_ID_LENGTH_MAX, "%s.%s", st->type, name);
148 rrdset_strncpyz_name(b, n, CONFIG_MAX_VALUE);
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);
157 st->name = config_get(st->config_section, "name", b);
158 st->hash_name = simple_hash(st->name);
163 rrddim_foreach_write(rd, st)
164 rrddimvar_rename_all(rd);
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);
172 // ----------------------------------------------------------------------------
173 // RRDSET - reset a chart
175 void rrdset_reset(RRDSET *st) {
176 debug(D_RRD_CALLS, "rrdset_reset() %s", st->name);
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;
184 st->counter_done = 0;
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));
195 // ----------------------------------------------------------------------------
196 // RRDSET - helpers for rrdset_create()
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;
202 if(unlikely(mode == RRD_MEMORY_MODE_NONE || mode == RRD_MEMORY_MODE_RAM))
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);
211 long n = (size - sizeof(RRDDIM)) / sizeof(storage_number);
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;
223 static inline void last_updated_time_align(struct timeval *tv, int update_every) {
224 tv->tv_sec -= tv->tv_sec % update_every;
228 // ----------------------------------------------------------------------------
229 // RRDSET - free a chart
231 void rrdset_free(RRDSET *st) {
232 if(unlikely(!st)) return;
234 rrdhost_check_wrlock(st->rrdhost); // make sure we have a write lock on the host
235 rrdset_wrlock(st); // lock this RRDSET
237 // info("Removing chart '%s' ('%s')", st->id, st->name);
239 // ------------------------------------------------------------------------
240 // remove it from the indexes
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);
245 rrdset_index_del_name(st->rrdhost, st);
247 // ------------------------------------------------------------------------
248 // free its children structures
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);
254 rrdfamily_free(st->rrdhost, st->rrdfamily);
256 // ------------------------------------------------------------------------
257 // unlink it from the host
259 if(st == st->rrdhost->rrdset_root) {
260 st->rrdhost->rrdset_root = st->next;
263 // find the previous one
265 for(s = st->rrdhost->rrdset_root; s && s->next != st ; s = s->next) ;
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);
274 // ------------------------------------------------------------------------
277 // free directly allocated members
278 freez(st->config_section);
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);
288 void rrdset_save(RRDSET *st) {
291 rrdset_check_rdlock(st);
293 // info("Saving chart '%s' ('%s')", st->id, st->name);
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);
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);
308 // ----------------------------------------------------------------------------
309 // RRDSET - create a chart
311 RRDSET *rrdset_create(
317 , const char *context
322 , RRDSET_TYPE chart_type
324 if(!type || !type[0]) {
325 fatal("Cannot create rrd stats without a type.");
330 fatal("Cannot create rrd stats without an id.");
334 // ------------------------------------------------------------------------
335 // check if it already exists
337 char fullid[RRD_ID_LENGTH_MAX + 1];
338 snprintfz(fullid, RRD_ID_LENGTH_MAX, "%s.%s", type, id);
340 RRDSET *st = rrdset_find(host, fullid);
342 rrdset_flag_clear(st, RRDSET_FLAG_OBSOLETE);
343 debug(D_RRD_CALLS, "RRDSET '%s', already exists.", fullid);
347 char fullfilename[FILENAME_MAX + 1];
349 // ------------------------------------------------------------------------
350 // compose the config_section for this chart
352 char config_section[RRD_ID_LENGTH_MAX + 1];
353 if(host == localhost)
354 strcpy(config_section, fullid);
356 snprintfz(config_section, RRD_ID_LENGTH_MAX, "%s/%s", host->machine_guid, fullid);
358 // ------------------------------------------------------------------------
359 // get the options from the config, we need to create it
361 long rentries = config_get_number(config_section, "history", host->rrd_history_entries);
362 long entries = align_entries_to_pagesize(host->rrd_memory_mode, rentries);
363 if(entries != rentries) entries = config_set_number(config_section, "history", entries);
365 if(host->rrd_memory_mode == RRD_MEMORY_MODE_NONE && entries != rentries)
366 entries = config_set_number(config_section, "history", 10);
368 int enabled = config_get_boolean(config_section, "enabled", 1);
369 if(!enabled) entries = 5;
371 unsigned long size = sizeof(RRDSET);
372 char *cache_dir = rrdset_cache_dir(host, fullid, config_section);
374 time_t now = now_realtime_sec();
376 // ------------------------------------------------------------------------
377 // load it or allocate it
379 debug(D_RRD_CALLS, "Creating RRD_STATS for '%s.%s'.", type, id);
381 snprintfz(fullfilename, FILENAME_MAX, "%s/main.db", cache_dir);
382 if(host->rrd_memory_mode == RRD_MEMORY_MODE_SAVE || host->rrd_memory_mode == RRD_MEMORY_MODE_MAP) {
383 st = (RRDSET *) mymmap(fullfilename, size, ((host->rrd_memory_mode == RRD_MEMORY_MODE_MAP) ? MAP_SHARED : MAP_PRIVATE), 0);
385 memset(&st->avl, 0, sizeof(avl));
386 memset(&st->avlname, 0, sizeof(avl));
387 memset(&st->variables_root_index, 0, sizeof(avl_tree_lock));
388 memset(&st->dimensions_index, 0, sizeof(avl_tree_lock));
389 memset(&st->rrdset_rwlock, 0, sizeof(pthread_rwlock_t));
397 st->dimensions = NULL;
399 st->variables = NULL;
401 st->flags = 0x00000000;
403 if(strcmp(st->magic, RRDSET_MAGIC) != 0) {
405 info("Initializing file %s.", fullfilename);
408 else if(strcmp(st->id, fullid) != 0) {
410 error("File %s contents are not for chart %s. Clearing it.", fullfilename, fullid);
415 else if(st->memsize != size || st->entries != entries) {
417 error("File %s does not have the desired size. Clearing it.", fullfilename);
420 else if(st->update_every != update_every) {
422 error("File %s does not have the desired update frequency. Clearing it.", fullfilename);
425 else if((now - st->last_updated.tv_sec) > update_every * entries) {
427 error("File %s is too old. Clearing it.", fullfilename);
430 else if(st->last_updated.tv_sec > now + update_every) {
432 error("File %s refers to the future. Clearing it.", fullfilename);
436 // make sure the database is aligned
437 if(st->last_updated.tv_sec)
438 last_updated_time_align(&st->last_updated, update_every);
441 // make sure we have the right memory mode
442 // even if we cleared the memory
443 st->rrd_memory_mode = host->rrd_memory_mode;
448 st = callocz(1, size);
449 st->rrd_memory_mode = (host->rrd_memory_mode == RRD_MEMORY_MODE_NONE) ? RRD_MEMORY_MODE_NONE : RRD_MEMORY_MODE_RAM;
452 st->config_section = strdup(config_section);
455 st->entries = entries;
456 st->update_every = update_every;
458 if(st->current_entry >= st->entries) st->current_entry = 0;
460 strcpy(st->cache_filename, fullfilename);
461 strcpy(st->magic, RRDSET_MAGIC);
463 strcpy(st->id, fullid);
464 st->hash = simple_hash(st->id);
466 st->cache_dir = cache_dir;
468 st->chart_type = rrdset_type_id(config_get(st->config_section, "chart type", rrdset_type_name(chart_type)));
469 st->type = config_get(st->config_section, "type", type);
470 st->family = config_get(st->config_section, "family", family?family:st->type);
471 st->units = config_get(st->config_section, "units", units?units:"");
473 st->context = config_get(st->config_section, "context", context?context:st->id);
474 st->hash_context = simple_hash(st->context);
476 st->priority = config_get_number(st->config_section, "priority", priority);
478 rrdset_flag_set(st, RRDSET_FLAG_ENABLED);
480 rrdset_flag_clear(st, RRDSET_FLAG_ENABLED);
482 rrdset_flag_clear(st, RRDSET_FLAG_DETAIL);
483 rrdset_flag_clear(st, RRDSET_FLAG_DEBUG);
484 rrdset_flag_clear(st, RRDSET_FLAG_OBSOLETE);
486 // if(!strcmp(st->id, "disk_util.dm-0")) {
488 // error("enabled debugging for '%s'", st->id);
490 // else error("not enabled debugging for '%s'", st->id);
495 st->last_collected_time.tv_sec = 0;
496 st->last_collected_time.tv_usec = 0;
497 st->counter_done = 0;
499 st->gap_when_lost_iterations_above = (int) (
500 config_get_number(st->config_section, "gap when lost iterations above", RRD_DEFAULT_GAP_INTERPOLATIONS) + 2);
502 avl_init_lock(&st->dimensions_index, rrddim_compare);
503 avl_init_lock(&st->variables_root_index, rrdvar_compare);
505 pthread_rwlock_init(&st->rrdset_rwlock, NULL);
506 rrdhost_wrlock(host);
508 if(name && *name) rrdset_set_name(st, name);
509 else rrdset_set_name(st, id);
512 char varvalue[CONFIG_MAX_VALUE + 1];
513 char varvalue2[CONFIG_MAX_VALUE + 1];
514 snprintfz(varvalue, CONFIG_MAX_VALUE, "%s (%s)", title?title:"", st->name);
515 json_escape_string(varvalue2, varvalue, sizeof(varvalue2));
516 st->title = config_get(st->config_section, "title", varvalue2);
519 st->rrdfamily = rrdfamily_create(host, st->family);
521 st->next = host->rrdset_root;
522 host->rrdset_root = st;
524 if(host->health_enabled) {
525 rrdsetvar_create(st, "last_collected_t", RRDVAR_TYPE_TIME_T, &st->last_collected_time.tv_sec, 0);
526 rrdsetvar_create(st, "collected_total_raw", RRDVAR_TYPE_TOTAL, &st->last_collected_total, 0);
527 rrdsetvar_create(st, "green", RRDVAR_TYPE_CALCULATED, &st->green, 0);
528 rrdsetvar_create(st, "red", RRDVAR_TYPE_CALCULATED, &st->red, 0);
529 rrdsetvar_create(st, "update_every", RRDVAR_TYPE_INT, &st->update_every, 0);
532 if(unlikely(rrdset_index_add(host, st) != st))
533 error("RRDSET: INTERNAL ERROR: attempt to index duplicate chart '%s'", st->id);
535 rrdsetcalc_link_matching(st);
536 rrdcalctemplate_link_matching(st);
538 rrdhost_cleanup(host);
540 rrdhost_unlock(host);
546 // ----------------------------------------------------------------------------
547 // RRDSET - data collection iteration control
549 inline void rrdset_next_usec_unfiltered(RRDSET *st, usec_t microseconds) {
551 if(unlikely(!st->last_collected_time.tv_sec)) {
553 microseconds = st->update_every * USEC_PER_SEC;
555 else if(unlikely(!microseconds)) {
556 // no dt given by the plugin
558 now_realtime_timeval(&now);
559 microseconds = dt_usec(&now, &st->last_collected_time);
562 st->usec_since_last_update = microseconds;
565 inline void rrdset_next_usec(RRDSET *st, usec_t microseconds) {
567 now_realtime_timeval(&now);
569 if(unlikely(!st->last_collected_time.tv_sec)) {
571 microseconds = st->update_every * USEC_PER_SEC;
573 else if(unlikely(!microseconds)) {
574 // no dt given by the plugin
575 microseconds = dt_usec(&now, &st->last_collected_time);
578 // microseconds has the time since the last collection
579 //#ifdef NETDATA_INTERNAL_CHECKS
580 // usec_t now_usec = timeval_usec(&now);
581 // usec_t last_usec = timeval_usec(&st->last_collected_time);
583 susec_t since_last_usec = dt_usec_signed(&now, &st->last_collected_time);
585 if(unlikely(since_last_usec < 0)) {
586 // oops! the database is in the future
587 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);
589 st->last_collected_time.tv_sec = now.tv_sec - st->update_every;
590 st->last_collected_time.tv_usec = now.tv_usec;
591 last_collected_time_align(&st->last_collected_time, st->update_every);
593 st->last_updated.tv_sec = now.tv_sec - st->update_every;
594 st->last_updated.tv_usec = now.tv_usec;
595 last_updated_time_align(&st->last_updated, st->update_every);
597 microseconds = st->update_every * USEC_PER_SEC;
598 since_last_usec = st->update_every * USEC_PER_SEC;
601 // verify the microseconds given is good
602 if(unlikely(microseconds > (usec_t)since_last_usec)) {
603 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);
605 //#ifdef NETDATA_INTERNAL_CHECKS
606 // if(unlikely(last_usec + microseconds > now_usec + 1000))
607 // 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);
610 microseconds = (usec_t)since_last_usec;
612 else if(unlikely(microseconds < (usec_t)since_last_usec * 0.8)) {
613 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);
615 //#ifdef NETDATA_INTERNAL_CHECKS
616 // 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);
618 microseconds = (usec_t)since_last_usec;
621 debug(D_RRD_CALLS, "rrdset_next_usec() for chart %s with microseconds %llu", st->name, microseconds);
623 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
624 debug(D_RRD_STATS, "%s: NEXT: %llu microseconds", st->name, microseconds);
626 st->usec_since_last_update = microseconds;
630 // ----------------------------------------------------------------------------
631 // RRDSET - process the collected values for all dimensions of a chart
633 static inline void rrdset_init_last_collected_time(RRDSET *st) {
634 now_realtime_timeval(&st->last_collected_time);
635 last_collected_time_align(&st->last_collected_time, st->update_every);
638 static inline usec_t rrdset_update_last_collected_time(RRDSET *st) {
639 usec_t last_collect_ut = st->last_collected_time.tv_sec * USEC_PER_SEC + st->last_collected_time.tv_usec;
640 usec_t ut = last_collect_ut + st->usec_since_last_update;
641 st->last_collected_time.tv_sec = (time_t) (ut / USEC_PER_SEC);
642 st->last_collected_time.tv_usec = (suseconds_t) (ut % USEC_PER_SEC);
643 return last_collect_ut;
646 static inline void rrdset_init_last_updated_time(RRDSET *st) {
647 // copy the last collected time to last updated time
648 st->last_updated.tv_sec = st->last_collected_time.tv_sec;
649 st->last_updated.tv_usec = st->last_collected_time.tv_usec;
650 last_updated_time_align(&st->last_updated, st->update_every);
653 static inline void rrdset_done_push_exclusive(RRDSET *st) {
654 if(unlikely(!st->last_collected_time.tv_sec)) {
655 // it is the first entry
656 // set the last_collected_time to now
657 rrdset_init_last_collected_time(st);
660 // it is not the first entry
661 // calculate the proper last_collected_time, using usec_since_last_update
662 rrdset_update_last_collected_time(st);
668 rrdset_done_push(st);
672 void rrdset_done(RRDSET *st) {
673 if(unlikely(netdata_exit)) return;
675 if(unlikely(st->rrd_memory_mode == RRD_MEMORY_MODE_NONE)) {
676 if(unlikely(st->rrdhost->rrdpush_enabled))
677 rrdset_done_push_exclusive(st);
682 debug(D_RRD_CALLS, "rrdset_done() for chart %s", st->name);
687 pthreadoldcancelstate; // store the old cancelable pthread state, to restore it at the end
690 store_this_entry = 1, // boolean: 1 = store this entry, 0 = don't store this entry
691 first_entry = 0; // boolean: 1 = this is the first entry seen for this chart, 0 = all other entries
694 stored_entries = 0; // the number of entries we have stored in the db, during this call to rrdset_done()
697 last_collect_ut, // the timestamp in microseconds, of the last collected value
698 now_collect_ut, // the timestamp in microseconds, of this collected value (this is NOW)
699 last_stored_ut, // the timestamp in microseconds, of the last stored entry in the db
700 next_store_ut, // the timestamp in microseconds, of the next entry to store in the db
701 update_every_ut = st->update_every * USEC_PER_SEC; // st->update_every in microseconds
703 if(unlikely(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &pthreadoldcancelstate) != 0))
704 error("Cannot set pthread cancel state to DISABLE.");
706 // a read lock is OK here
710 // enable the chart, if it was disabled
711 if(unlikely(rrd_delete_unupdated_dimensions) && !st->enabled)
715 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_OBSOLETE))) {
716 error("Chart '%s' has the OBSOLETE flag set, but it is collected.", st->id);
717 rrdset_flag_clear(st, RRDSET_FLAG_OBSOLETE);
720 // check if the chart has a long time to be updated
721 if(unlikely(st->usec_since_last_update > st->entries * update_every_ut)) {
722 info("%s: took too long to be updated (%0.3Lf secs). Resetting it.", st->name, (long double)(st->usec_since_last_update / 1000000.0));
724 st->usec_since_last_update = update_every_ut;
728 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
729 debug(D_RRD_STATS, "%s: microseconds since last update: %llu", st->name, st->usec_since_last_update);
731 // set last_collected_time
732 if(unlikely(!st->last_collected_time.tv_sec)) {
733 // it is the first entry
734 // set the last_collected_time to now
735 rrdset_init_last_collected_time(st);
737 last_collect_ut = st->last_collected_time.tv_sec * USEC_PER_SEC + st->last_collected_time.tv_usec - update_every_ut;
739 // the first entry should not be stored
740 store_this_entry = 0;
743 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
744 debug(D_RRD_STATS, "%s: has not set last_collected_time. Setting it now. Will not store the next entry.", st->name);
747 // it is not the first entry
748 // calculate the proper last_collected_time, using usec_since_last_update
749 last_collect_ut = rrdset_update_last_collected_time(st);
752 // if this set has not been updated in the past
753 // we fake the last_update time to be = now - usec_since_last_update
754 if(unlikely(!st->last_updated.tv_sec)) {
755 // it has never been updated before
756 // set a fake last_updated, in the past using usec_since_last_update
757 rrdset_init_last_updated_time(st);
759 // the first entry should not be stored
760 store_this_entry = 0;
763 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
764 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);
767 // check if we will re-write the entire data set
768 if(unlikely(dt_usec(&st->last_collected_time, &st->last_updated) > st->entries * update_every_ut)) {
769 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);
771 rrdset_init_last_updated_time(st);
773 st->usec_since_last_update = update_every_ut;
775 // the first entry should not be stored
776 store_this_entry = 0;
780 // these are the 3 variables that will help us in interpolation
781 // last_stored_ut = the last time we added a value to the storage
782 // now_collect_ut = the time the current value has been collected
783 // next_store_ut = the time of the next interpolation point
784 last_stored_ut = st->last_updated.tv_sec * USEC_PER_SEC + st->last_updated.tv_usec;
785 now_collect_ut = st->last_collected_time.tv_sec * USEC_PER_SEC + st->last_collected_time.tv_usec;
786 next_store_ut = (st->last_updated.tv_sec + st->update_every) * USEC_PER_SEC;
788 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG))) {
789 debug(D_RRD_STATS, "%s: last_collect_ut = %0.3Lf (last collection time)", st->name, (long double)last_collect_ut/1000000.0);
790 debug(D_RRD_STATS, "%s: now_collect_ut = %0.3Lf (current collection time)", st->name, (long double)now_collect_ut/1000000.0);
791 debug(D_RRD_STATS, "%s: last_stored_ut = %0.3Lf (last updated time)", st->name, (long double)last_stored_ut/1000000.0);
792 debug(D_RRD_STATS, "%s: next_store_ut = %0.3Lf (next interpolation point)", st->name, (long double)next_store_ut/1000000.0);
795 if(unlikely(!st->counter_done)) {
796 store_this_entry = 0;
797 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
798 debug(D_RRD_STATS, "%s: Will not store the next entry.", st->name);
802 if(unlikely(st->rrdhost->rrdpush_enabled))
803 rrdset_done_push(st);
805 // calculate totals and count the dimensions
807 st->collected_total = 0;
808 rrddim_foreach_read(rd, st) {
810 if(likely(rd->updated))
811 st->collected_total += rd->collected_value;
814 uint32_t storage_flags = SN_EXISTS;
816 // process all dimensions to calculate their values
817 // based on the collected figures only
818 // at this stage we do not interpolate anything
819 rrddim_foreach_read(rd, st) {
821 if(unlikely(!rd->updated)) {
822 rd->calculated_value = 0;
826 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
827 debug(D_RRD_STATS, "%s/%s: START "
828 " last_collected_value = " COLLECTED_NUMBER_FORMAT
829 " collected_value = " COLLECTED_NUMBER_FORMAT
830 " last_calculated_value = " CALCULATED_NUMBER_FORMAT
831 " calculated_value = " CALCULATED_NUMBER_FORMAT
833 , rd->last_collected_value
834 , rd->collected_value
835 , rd->last_calculated_value
836 , rd->calculated_value
839 switch(rd->algorithm) {
840 case RRD_ALGORITHM_ABSOLUTE:
841 rd->calculated_value = (calculated_number)rd->collected_value
842 * (calculated_number)rd->multiplier
843 / (calculated_number)rd->divisor;
845 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
846 debug(D_RRD_STATS, "%s/%s: CALC ABS/ABS-NO-IN "
847 CALCULATED_NUMBER_FORMAT " = "
848 COLLECTED_NUMBER_FORMAT
849 " * " CALCULATED_NUMBER_FORMAT
850 " / " CALCULATED_NUMBER_FORMAT
852 , rd->calculated_value
853 , rd->collected_value
854 , (calculated_number)rd->multiplier
855 , (calculated_number)rd->divisor
859 case RRD_ALGORITHM_PCENT_OVER_ROW_TOTAL:
860 if(unlikely(!st->collected_total))
861 rd->calculated_value = 0;
863 // the percentage of the current value
864 // over the total of all dimensions
865 rd->calculated_value =
866 (calculated_number)100
867 * (calculated_number)rd->collected_value
868 / (calculated_number)st->collected_total;
870 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
871 debug(D_RRD_STATS, "%s/%s: CALC PCENT-ROW "
872 CALCULATED_NUMBER_FORMAT " = 100"
873 " * " COLLECTED_NUMBER_FORMAT
874 " / " COLLECTED_NUMBER_FORMAT
876 , rd->calculated_value
877 , rd->collected_value
878 , st->collected_total
882 case RRD_ALGORITHM_INCREMENTAL:
883 if(unlikely(rd->collections_counter <= 1)) {
884 rd->calculated_value = 0;
888 // if the new is smaller than the old (an overflow, or reset), set the old equal to the new
889 // to reset the calculation (it will give zero as the calculation for this second)
890 if(unlikely(rd->last_collected_value > rd->collected_value)) {
891 debug(D_RRD_STATS, "%s.%s: RESET or OVERFLOW. Last collected value = " COLLECTED_NUMBER_FORMAT ", current = " COLLECTED_NUMBER_FORMAT
893 , rd->last_collected_value
894 , rd->collected_value);
896 if(!(rrddim_flag_check(rd, RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS)))
897 storage_flags = SN_EXISTS_RESET;
899 rd->last_collected_value = rd->collected_value;
902 rd->calculated_value +=
903 (calculated_number)(rd->collected_value - rd->last_collected_value)
904 * (calculated_number)rd->multiplier
905 / (calculated_number)rd->divisor;
907 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
908 debug(D_RRD_STATS, "%s/%s: CALC INC PRE "
909 CALCULATED_NUMBER_FORMAT " = ("
910 COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT
912 " * " CALCULATED_NUMBER_FORMAT
913 " / " CALCULATED_NUMBER_FORMAT
915 , rd->calculated_value
916 , rd->collected_value, rd->last_collected_value
917 , (calculated_number)rd->multiplier
918 , (calculated_number)rd->divisor
922 case RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL:
923 if(unlikely(rd->collections_counter <= 1)) {
924 rd->calculated_value = 0;
928 // if the new is smaller than the old (an overflow, or reset), set the old equal to the new
929 // to reset the calculation (it will give zero as the calculation for this second)
930 if(unlikely(rd->last_collected_value > rd->collected_value)) {
931 debug(D_RRD_STATS, "%s.%s: RESET or OVERFLOW. Last collected value = " COLLECTED_NUMBER_FORMAT ", current = " COLLECTED_NUMBER_FORMAT
933 , rd->last_collected_value
934 , rd->collected_value);
936 if(!(rrddim_flag_check(rd, RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS)))
937 storage_flags = SN_EXISTS_RESET;
939 rd->last_collected_value = rd->collected_value;
942 // the percentage of the current increment
943 // over the increment of all dimensions together
944 if(unlikely(st->collected_total == st->last_collected_total))
945 rd->calculated_value = 0;
947 rd->calculated_value =
948 (calculated_number)100
949 * (calculated_number)(rd->collected_value - rd->last_collected_value)
950 / (calculated_number)(st->collected_total - st->last_collected_total);
952 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
953 debug(D_RRD_STATS, "%s/%s: CALC PCENT-DIFF "
954 CALCULATED_NUMBER_FORMAT " = 100"
955 " * (" COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT ")"
956 " / (" COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT ")"
958 , rd->calculated_value
959 , rd->collected_value, rd->last_collected_value
960 , st->collected_total, st->last_collected_total
965 // make the default zero, to make sure
966 // it gets noticed when we add new types
967 rd->calculated_value = 0;
969 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
970 debug(D_RRD_STATS, "%s/%s: CALC "
971 CALCULATED_NUMBER_FORMAT " = 0"
973 , rd->calculated_value
978 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
979 debug(D_RRD_STATS, "%s/%s: PHASE2 "
980 " last_collected_value = " COLLECTED_NUMBER_FORMAT
981 " collected_value = " COLLECTED_NUMBER_FORMAT
982 " last_calculated_value = " CALCULATED_NUMBER_FORMAT
983 " calculated_value = " CALCULATED_NUMBER_FORMAT
985 , rd->last_collected_value
986 , rd->collected_value
987 , rd->last_calculated_value
988 , rd->calculated_value
993 // at this point we have all the calculated values ready
994 // it is now time to interpolate values on a second boundary
996 if(unlikely(now_collect_ut < next_store_ut)) {
997 // this is collected in the same interpolation point
999 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
1000 debug(D_RRD_STATS, "%s: THIS IS IN THE SAME INTERPOLATION POINT", st->name);
1002 //#ifdef NETDATA_INTERNAL_CHECKS
1003 // info("%s is collected in the same interpolation point: short by %llu microseconds", st->name, next_store_ut - now_collect_ut);
1007 usec_t first_ut = last_stored_ut;
1008 long long iterations = (now_collect_ut - last_stored_ut) / (update_every_ut);
1009 if((now_collect_ut % (update_every_ut)) == 0) iterations++;
1011 for( ; next_store_ut <= now_collect_ut ; last_collect_ut = next_store_ut, next_store_ut += update_every_ut, iterations-- ) {
1012 //#ifdef NETDATA_INTERNAL_CHECKS
1013 // 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); }
1016 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG))) {
1017 debug(D_RRD_STATS, "%s: last_stored_ut = %0.3Lf (last updated time)", st->name, (long double)last_stored_ut/1000000.0);
1018 debug(D_RRD_STATS, "%s: next_store_ut = %0.3Lf (next interpolation point)", st->name, (long double)next_store_ut/1000000.0);
1021 st->last_updated.tv_sec = (time_t) (next_store_ut / USEC_PER_SEC);
1022 st->last_updated.tv_usec = 0;
1024 rrddim_foreach_read(rd, st) {
1025 calculated_number new_value;
1027 switch(rd->algorithm) {
1028 case RRD_ALGORITHM_INCREMENTAL:
1029 new_value = (calculated_number)
1030 ( rd->calculated_value
1031 * (calculated_number)(next_store_ut - last_collect_ut)
1032 / (calculated_number)(now_collect_ut - last_collect_ut)
1035 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
1036 debug(D_RRD_STATS, "%s/%s: CALC2 INC "
1037 CALCULATED_NUMBER_FORMAT " = "
1038 CALCULATED_NUMBER_FORMAT
1043 , rd->calculated_value
1044 , (next_store_ut - last_stored_ut)
1045 , (now_collect_ut - last_stored_ut)
1048 rd->calculated_value -= new_value;
1049 new_value += rd->last_calculated_value;
1050 rd->last_calculated_value = 0;
1051 new_value /= (calculated_number)st->update_every;
1053 if(unlikely(next_store_ut - last_stored_ut < update_every_ut)) {
1054 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
1055 debug(D_RRD_STATS, "%s/%s: COLLECTION POINT IS SHORT " CALCULATED_NUMBER_FORMAT " - EXTRAPOLATING",
1057 , (calculated_number)(next_store_ut - last_stored_ut)
1059 new_value = new_value * (calculated_number)(st->update_every * 1000000) / (calculated_number)(next_store_ut - last_stored_ut);
1063 case RRD_ALGORITHM_ABSOLUTE:
1064 case RRD_ALGORITHM_PCENT_OVER_ROW_TOTAL:
1065 case RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL:
1067 if(iterations == 1) {
1068 // this is the last iteration
1069 // do not interpolate
1070 // just show the calculated value
1072 new_value = rd->calculated_value;
1075 // we have missed an update
1076 // interpolate in the middle values
1078 new_value = (calculated_number)
1079 ( ( (rd->calculated_value - rd->last_calculated_value)
1080 * (calculated_number)(next_store_ut - last_collect_ut)
1081 / (calculated_number)(now_collect_ut - last_collect_ut)
1083 + rd->last_calculated_value
1086 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
1087 debug(D_RRD_STATS, "%s/%s: CALC2 DEF "
1088 CALCULATED_NUMBER_FORMAT " = ((("
1089 "(" CALCULATED_NUMBER_FORMAT " - " CALCULATED_NUMBER_FORMAT ")"
1091 " / %llu) + " CALCULATED_NUMBER_FORMAT
1094 , rd->calculated_value, rd->last_calculated_value
1095 , (next_store_ut - first_ut)
1096 , (now_collect_ut - first_ut), rd->last_calculated_value
1102 if(unlikely(!store_this_entry)) {
1103 rd->values[st->current_entry] = pack_storage_number(0, SN_NOT_EXISTS);
1107 if(likely(rd->updated && rd->collections_counter > 1 && iterations < st->gap_when_lost_iterations_above)) {
1108 rd->values[st->current_entry] = pack_storage_number(new_value, storage_flags );
1109 rd->last_stored_value = new_value;
1111 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
1112 debug(D_RRD_STATS, "%s/%s: STORE[%ld] "
1113 CALCULATED_NUMBER_FORMAT " = " CALCULATED_NUMBER_FORMAT
1116 , unpack_storage_number(rd->values[st->current_entry]), new_value
1120 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
1121 debug(D_RRD_STATS, "%s/%s: STORE[%ld] = NON EXISTING "
1125 rd->values[st->current_entry] = pack_storage_number(0, SN_NOT_EXISTS);
1126 rd->last_stored_value = NAN;
1131 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG))) {
1132 calculated_number t1 = new_value * (calculated_number)rd->multiplier / (calculated_number)rd->divisor;
1133 calculated_number t2 = unpack_storage_number(rd->values[st->current_entry]);
1134 calculated_number accuracy = accuracy_loss(t1, t2);
1135 debug(D_RRD_STATS, "%s/%s: UNPACK[%ld] = " CALCULATED_NUMBER_FORMAT " FLAGS=0x%08x (original = " CALCULATED_NUMBER_FORMAT ", accuracy loss = " CALCULATED_NUMBER_FORMAT "%%%s)"
1139 , get_storage_number_flags(rd->values[st->current_entry])
1142 , (accuracy > ACCURACY_LOSS) ? " **TOO BIG** " : ""
1145 rd->collected_volume += t1;
1146 rd->stored_volume += t2;
1147 accuracy = accuracy_loss(rd->collected_volume, rd->stored_volume);
1148 debug(D_RRD_STATS, "%s/%s: VOLUME[%ld] = " CALCULATED_NUMBER_FORMAT ", calculated = " CALCULATED_NUMBER_FORMAT ", accuracy loss = " CALCULATED_NUMBER_FORMAT "%%%s"
1152 , rd->collected_volume
1154 , (accuracy > ACCURACY_LOSS) ? " **TOO BIG** " : ""
1159 // reset the storage flags for the next point, if any;
1160 storage_flags = SN_EXISTS;
1163 st->current_entry = ((st->current_entry + 1) >= st->entries) ? 0 : st->current_entry + 1;
1164 last_stored_ut = next_store_ut;
1167 st->last_collected_total = st->collected_total;
1169 rrddim_foreach_read(rd, st) {
1170 if(unlikely(!rd->updated))
1173 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
1174 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);
1176 rd->last_collected_value = rd->collected_value;
1178 switch(rd->algorithm) {
1179 case RRD_ALGORITHM_INCREMENTAL:
1180 if(unlikely(!first_entry)) {
1181 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
1182 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);
1183 rd->last_calculated_value += rd->calculated_value;
1186 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
1187 debug(D_RRD_STATS, "%s: THIS IS THE FIRST POINT", st->name);
1191 case RRD_ALGORITHM_ABSOLUTE:
1192 case RRD_ALGORITHM_PCENT_OVER_ROW_TOTAL:
1193 case RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL:
1194 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
1195 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);
1196 rd->last_calculated_value = rd->calculated_value;
1200 rd->calculated_value = 0;
1201 rd->collected_value = 0;
1204 if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
1205 debug(D_RRD_STATS, "%s/%s: END "
1206 " last_collected_value = " COLLECTED_NUMBER_FORMAT
1207 " collected_value = " COLLECTED_NUMBER_FORMAT
1208 " last_calculated_value = " CALCULATED_NUMBER_FORMAT
1209 " calculated_value = " CALCULATED_NUMBER_FORMAT
1211 , rd->last_collected_value
1212 , rd->collected_value
1213 , rd->last_calculated_value
1214 , rd->calculated_value
1218 // ALL DONE ABOUT THE DATA UPDATE
1219 // --------------------------------------------------------------------
1222 // find if there are any obsolete dimensions (not updated recently)
1223 if(unlikely(rrd_delete_unupdated_dimensions)) {
1225 for( rd = st->dimensions; likely(rd) ; rd = rd->next )
1226 if((rd->last_collected_time.tv_sec + (rrd_delete_unupdated_dimensions * st->update_every)) < st->last_collected_time.tv_sec)
1231 // there is dimension to free
1232 // upgrade our read lock to a write lock
1233 pthread_rwlock_unlock(&st->rrdset_rwlock);
1234 pthread_rwlock_wrlock(&st->rrdset_rwlock);
1236 for( rd = st->dimensions, last = NULL ; likely(rd) ; ) {
1237 // remove it only it is not updated in rrd_delete_unupdated_dimensions seconds
1239 if(unlikely((rd->last_collected_time.tv_sec + (rrd_delete_unupdated_dimensions * st->update_every)) < st->last_collected_time.tv_sec)) {
1240 info("Removing obsolete dimension '%s' (%s) of '%s' (%s).", rd->name, rd->id, st->name, st->id);
1242 if(unlikely(!last)) {
1243 st->dimensions = rd->next;
1245 rrddim_free(st, rd);
1246 rd = st->dimensions;
1250 last->next = rd->next;
1252 rrddim_free(st, rd);
1262 if(unlikely(!st->dimensions)) {
1263 info("Disabling chart %s (%s) since it does not have any dimensions", st->name, st->id);
1272 if(unlikely(pthread_setcancelstate(pthreadoldcancelstate, NULL) != 0))
1273 error("Cannot set pthread cancel state to RESTORE (%d).", pthreadoldcancelstate);