3 #define RRD_DEFAULT_GAP_INTERPOLATIONS 1
5 // ----------------------------------------------------------------------------
9 // if not zero it gives the time (in seconds) to remove un-updated dimensions
11 // if dimensions are removed, the chart generation will have to run again
12 int rrd_delete_unupdated_dimensions = 0;
15 int rrd_update_every = UPDATE_EVERY;
16 int rrd_default_history_entries = RRD_DEFAULT_HISTORY_ENTRIES;
17 int rrd_memory_mode = RRD_MEMORY_MODE_SAVE;
19 static int rrdset_compare(void* a, void* b);
20 static int rrdset_compare_name(void* a, void* b);
21 static int rrdfamily_compare(void *a, void *b);
23 // ----------------------------------------------------------------------------
27 .hostname = "localhost",
29 .rrdset_root_rwlock = PTHREAD_RWLOCK_INITIALIZER,
30 .rrdset_root_index = {
31 { NULL, rrdset_compare },
34 .rrdset_root_index_name = {
35 { NULL, rrdset_compare_name },
38 .rrdfamily_root_index = {
39 { NULL, rrdfamily_compare },
42 .variables_root_index = {
43 { NULL, rrdvar_compare },
52 .alarm_log_rwlock = PTHREAD_RWLOCK_INITIALIZER
56 void rrdhost_init(char *hostname) {
57 localhost.hostname = hostname;
58 localhost.health_log.next_log_id =
59 localhost.health_log.next_alarm_id = now_realtime_sec();
62 void rrdhost_rwlock(RRDHOST *host) {
63 pthread_rwlock_wrlock(&host->rrdset_root_rwlock);
66 void rrdhost_rdlock(RRDHOST *host) {
67 pthread_rwlock_rdlock(&host->rrdset_root_rwlock);
70 void rrdhost_unlock(RRDHOST *host) {
71 pthread_rwlock_unlock(&host->rrdset_root_rwlock);
74 void rrdhost_check_rdlock_int(RRDHOST *host, const char *file, const char *function, const unsigned long line) {
75 int ret = pthread_rwlock_trywrlock(&host->rrdset_root_rwlock);
78 fatal("RRDHOST '%s' should be read-locked, but it is not, at function %s() at line %lu of file '%s'", host->hostname, function, line, file);
81 void rrdhost_check_wrlock_int(RRDHOST *host, const char *file, const char *function, const unsigned long line) {
82 int ret = pthread_rwlock_tryrdlock(&host->rrdset_root_rwlock);
85 fatal("RRDHOST '%s' should be write-locked, but it is not, at function %s() at line %lu of file '%s'", host->hostname, function, line, file);
88 // ----------------------------------------------------------------------------
91 static int rrdfamily_compare(void *a, void *b) {
92 if(((RRDFAMILY *)a)->hash_family < ((RRDFAMILY *)b)->hash_family) return -1;
93 else if(((RRDFAMILY *)a)->hash_family > ((RRDFAMILY *)b)->hash_family) return 1;
94 else return strcmp(((RRDFAMILY *)a)->family, ((RRDFAMILY *)b)->family);
97 #define rrdfamily_index_add(host, rc) (RRDFAMILY *)avl_insert_lock(&((host)->rrdfamily_root_index), (avl *)(rc))
98 #define rrdfamily_index_del(host, rc) (RRDFAMILY *)avl_remove_lock(&((host)->rrdfamily_root_index), (avl *)(rc))
100 static RRDFAMILY *rrdfamily_index_find(RRDHOST *host, const char *id, uint32_t hash) {
103 tmp.hash_family = (hash)?hash:simple_hash(tmp.family);
105 return (RRDFAMILY *)avl_search_lock(&(host->rrdfamily_root_index), (avl *) &tmp);
108 RRDFAMILY *rrdfamily_create(const char *id) {
109 RRDFAMILY *rc = rrdfamily_index_find(&localhost, id, 0);
111 rc = callocz(1, sizeof(RRDFAMILY));
113 rc->family = strdupz(id);
114 rc->hash_family = simple_hash(rc->family);
116 // initialize the variables index
117 avl_init_lock(&rc->variables_root_index, rrdvar_compare);
119 RRDFAMILY *ret = rrdfamily_index_add(&localhost, rc);
121 fatal("INTERNAL ERROR: Expected to INSERT RRDFAMILY '%s' into index, but inserted '%s'.", rc->family, (ret)?ret->family:"NONE");
128 void rrdfamily_free(RRDFAMILY *rc) {
131 RRDFAMILY *ret = rrdfamily_index_del(&localhost, rc);
133 fatal("INTERNAL ERROR: Expected to DELETE RRDFAMILY '%s' from index, but deleted '%s'.", rc->family, (ret)?ret->family:"NONE");
135 if(rc->variables_root_index.avl_tree.root != NULL)
136 fatal("INTERNAL ERROR: Variables index of RRDFAMILY '%s' that is freed, is not empty.", rc->family);
138 freez((void *)rc->family);
143 // ----------------------------------------------------------------------------
146 static int rrdset_compare(void* a, void* b) {
147 if(((RRDSET *)a)->hash < ((RRDSET *)b)->hash) return -1;
148 else if(((RRDSET *)a)->hash > ((RRDSET *)b)->hash) return 1;
149 else return strcmp(((RRDSET *)a)->id, ((RRDSET *)b)->id);
152 #define rrdset_index_add(host, st) (RRDSET *)avl_insert_lock(&((host)->rrdset_root_index), (avl *)(st))
153 #define rrdset_index_del(host, st) (RRDSET *)avl_remove_lock(&((host)->rrdset_root_index), (avl *)(st))
155 static RRDSET *rrdset_index_find(RRDHOST *host, const char *id, uint32_t hash) {
157 strncpyz(tmp.id, id, RRD_ID_LENGTH_MAX);
158 tmp.hash = (hash)?hash:simple_hash(tmp.id);
160 return (RRDSET *)avl_search_lock(&(host->rrdset_root_index), (avl *) &tmp);
163 // ----------------------------------------------------------------------------
166 #define rrdset_from_avlname(avlname_ptr) ((RRDSET *)((avlname_ptr) - offsetof(RRDSET, avlname)))
168 static int rrdset_compare_name(void* a, void* b) {
169 RRDSET *A = rrdset_from_avlname(a);
170 RRDSET *B = rrdset_from_avlname(b);
172 // fprintf(stderr, "COMPARING: %s with %s\n", A->name, B->name);
174 if(A->hash_name < B->hash_name) return -1;
175 else if(A->hash_name > B->hash_name) return 1;
176 else return strcmp(A->name, B->name);
179 RRDSET *rrdset_index_add_name(RRDHOST *host, RRDSET *st) {
181 // fprintf(stderr, "ADDING: %s (name: %s)\n", st->id, st->name);
182 result = avl_insert_lock(&host->rrdset_root_index_name, (avl *) (&st->avlname));
183 if(result) return rrdset_from_avlname(result);
187 RRDSET *rrdset_index_del_name(RRDHOST *host, RRDSET *st) {
189 // fprintf(stderr, "DELETING: %s (name: %s)\n", st->id, st->name);
190 result = (RRDSET *)avl_remove_lock(&((host)->rrdset_root_index_name), (avl *)(&st->avlname));
191 if(result) return rrdset_from_avlname(result);
195 static RRDSET *rrdset_index_find_name(RRDHOST *host, const char *name, uint32_t hash) {
199 tmp.hash_name = (hash)?hash:simple_hash(tmp.name);
201 // fprintf(stderr, "SEARCHING: %s\n", name);
202 result = avl_search_lock(&host->rrdset_root_index_name, (avl *) (&(tmp.avlname)));
204 RRDSET *st = rrdset_from_avlname(result);
205 if(strcmp(st->magic, RRDSET_MAGIC))
206 error("Search for RRDSET %s returned an invalid RRDSET %s (name %s)", name, st->id, st->name);
208 // fprintf(stderr, "FOUND: %s\n", name);
209 return rrdset_from_avlname(result);
211 // fprintf(stderr, "NOT FOUND: %s\n", name);
216 // ----------------------------------------------------------------------------
219 static int rrddim_compare(void* a, void* b) {
220 if(((RRDDIM *)a)->hash < ((RRDDIM *)b)->hash) return -1;
221 else if(((RRDDIM *)a)->hash > ((RRDDIM *)b)->hash) return 1;
222 else return strcmp(((RRDDIM *)a)->id, ((RRDDIM *)b)->id);
225 #define rrddim_index_add(st, rd) avl_insert_lock(&((st)->dimensions_index), (avl *)(rd))
226 #define rrddim_index_del(st,rd ) avl_remove_lock(&((st)->dimensions_index), (avl *)(rd))
228 static RRDDIM *rrddim_index_find(RRDSET *st, const char *id, uint32_t hash) {
230 strncpyz(tmp.id, id, RRD_ID_LENGTH_MAX);
231 tmp.hash = (hash)?hash:simple_hash(tmp.id);
233 return (RRDDIM *)avl_search_lock(&(st->dimensions_index), (avl *) &tmp);
236 // ----------------------------------------------------------------------------
239 int rrdset_type_id(const char *name)
241 if(unlikely(strcmp(name, RRDSET_TYPE_AREA_NAME) == 0)) return RRDSET_TYPE_AREA;
242 else if(unlikely(strcmp(name, RRDSET_TYPE_STACKED_NAME) == 0)) return RRDSET_TYPE_STACKED;
243 else if(unlikely(strcmp(name, RRDSET_TYPE_LINE_NAME) == 0)) return RRDSET_TYPE_LINE;
244 return RRDSET_TYPE_LINE;
247 const char *rrdset_type_name(int chart_type)
249 static char line[] = RRDSET_TYPE_LINE_NAME;
250 static char area[] = RRDSET_TYPE_AREA_NAME;
251 static char stacked[] = RRDSET_TYPE_STACKED_NAME;
254 case RRDSET_TYPE_LINE:
257 case RRDSET_TYPE_AREA:
260 case RRDSET_TYPE_STACKED:
266 // ----------------------------------------------------------------------------
269 const char *rrd_memory_mode_name(int id)
271 static const char ram[] = RRD_MEMORY_MODE_RAM_NAME;
272 static const char map[] = RRD_MEMORY_MODE_MAP_NAME;
273 static const char save[] = RRD_MEMORY_MODE_SAVE_NAME;
276 case RRD_MEMORY_MODE_RAM:
279 case RRD_MEMORY_MODE_MAP:
282 case RRD_MEMORY_MODE_SAVE:
290 int rrd_memory_mode_id(const char *name)
292 if(unlikely(!strcmp(name, RRD_MEMORY_MODE_RAM_NAME)))
293 return RRD_MEMORY_MODE_RAM;
294 else if(unlikely(!strcmp(name, RRD_MEMORY_MODE_MAP_NAME)))
295 return RRD_MEMORY_MODE_MAP;
297 return RRD_MEMORY_MODE_SAVE;
300 // ----------------------------------------------------------------------------
303 int rrddim_algorithm_id(const char *name)
305 if(strcmp(name, RRDDIM_INCREMENTAL_NAME) == 0) return RRDDIM_INCREMENTAL;
306 if(strcmp(name, RRDDIM_ABSOLUTE_NAME) == 0) return RRDDIM_ABSOLUTE;
307 if(strcmp(name, RRDDIM_PCENT_OVER_ROW_TOTAL_NAME) == 0) return RRDDIM_PCENT_OVER_ROW_TOTAL;
308 if(strcmp(name, RRDDIM_PCENT_OVER_DIFF_TOTAL_NAME) == 0) return RRDDIM_PCENT_OVER_DIFF_TOTAL;
309 return RRDDIM_ABSOLUTE;
312 const char *rrddim_algorithm_name(int chart_type)
314 static char absolute[] = RRDDIM_ABSOLUTE_NAME;
315 static char incremental[] = RRDDIM_INCREMENTAL_NAME;
316 static char percentage_of_absolute_row[] = RRDDIM_PCENT_OVER_ROW_TOTAL_NAME;
317 static char percentage_of_incremental_row[] = RRDDIM_PCENT_OVER_DIFF_TOTAL_NAME;
320 case RRDDIM_ABSOLUTE:
323 case RRDDIM_INCREMENTAL:
326 case RRDDIM_PCENT_OVER_ROW_TOTAL:
327 return percentage_of_absolute_row;
329 case RRDDIM_PCENT_OVER_DIFF_TOTAL:
330 return percentage_of_incremental_row;
335 // ----------------------------------------------------------------------------
338 char *rrdset_strncpyz_name(char *to, const char *from, size_t length)
342 while (length-- && (c = *from++)) {
343 if(c != '.' && !isalnum(c))
354 void rrdset_set_name(RRDSET *st, const char *name)
356 if(unlikely(st->name && !strcmp(st->name, name)))
359 debug(D_RRD_CALLS, "rrdset_set_name() old: %s, new: %s", st->name, name);
361 char b[CONFIG_MAX_VALUE + 1];
362 char n[RRD_ID_LENGTH_MAX + 1];
364 snprintfz(n, RRD_ID_LENGTH_MAX, "%s.%s", st->type, name);
365 rrdset_strncpyz_name(b, n, CONFIG_MAX_VALUE);
368 rrdset_index_del_name(&localhost, st);
369 st->name = config_set_default(st->id, "name", b);
370 st->hash_name = simple_hash(st->name);
371 rrdsetvar_rename_all(st);
374 st->name = config_get(st->id, "name", b);
375 st->hash_name = simple_hash(st->name);
378 pthread_rwlock_wrlock(&st->rwlock);
380 for(rd = st->dimensions; rd ;rd = rd->next)
381 rrddimvar_rename_all(rd);
382 pthread_rwlock_unlock(&st->rwlock);
384 rrdset_index_add_name(&localhost, st);
387 // ----------------------------------------------------------------------------
390 char *rrdset_cache_dir(const char *id)
394 static char *cache_dir = NULL;
396 cache_dir = config_get("global", "cache directory", CACHE_DIR);
397 int r = mkdir(cache_dir, 0755);
398 if(r != 0 && errno != EEXIST)
399 error("Cannot create directory '%s'", cache_dir);
402 char b[FILENAME_MAX + 1];
403 char n[FILENAME_MAX + 1];
404 rrdset_strncpyz_name(b, id, FILENAME_MAX);
406 snprintfz(n, FILENAME_MAX, "%s/%s", cache_dir, b);
407 ret = config_get(id, "cache directory", n);
409 if(rrd_memory_mode == RRD_MEMORY_MODE_MAP || rrd_memory_mode == RRD_MEMORY_MODE_SAVE) {
410 int r = mkdir(ret, 0775);
411 if(r != 0 && errno != EEXIST)
412 error("Cannot create directory '%s'", ret);
418 // ----------------------------------------------------------------------------
421 void rrdset_reset(RRDSET *st)
423 debug(D_RRD_CALLS, "rrdset_reset() %s", st->name);
425 st->last_collected_time.tv_sec = 0;
426 st->last_collected_time.tv_usec = 0;
427 st->last_updated.tv_sec = 0;
428 st->last_updated.tv_usec = 0;
429 st->current_entry = 0;
431 st->counter_done = 0;
434 for(rd = st->dimensions; rd ; rd = rd->next) {
435 rd->last_collected_time.tv_sec = 0;
436 rd->last_collected_time.tv_usec = 0;
438 memset(rd->values, 0, rd->entries * sizeof(storage_number));
441 static inline long align_entries_to_pagesize(long entries) {
442 if(entries < 5) entries = 5;
443 if(entries > RRD_HISTORY_ENTRIES_MAX) entries = RRD_HISTORY_ENTRIES_MAX;
445 #ifdef NETDATA_LOG_ALLOCATIONS
446 long page = (size_t)sysconf(_SC_PAGESIZE);
448 long size = sizeof(RRDDIM) + entries * sizeof(storage_number);
450 size -= (size % page);
453 long n = (size - sizeof(RRDDIM)) / sizeof(storage_number);
463 static inline void timeval_align(struct timeval *tv, int update_every) {
464 tv->tv_sec -= tv->tv_sec % update_every;
465 tv->tv_usec = 500000;
468 RRDSET *rrdset_create(const char *type, const char *id, const char *name, const char *family, const char *context, const char *title, const char *units, long priority, int update_every, int chart_type)
470 if(!type || !type[0]) {
471 fatal("Cannot create rrd stats without a type.");
476 fatal("Cannot create rrd stats without an id.");
480 char fullid[RRD_ID_LENGTH_MAX + 1];
481 char fullfilename[FILENAME_MAX + 1];
483 snprintfz(fullid, RRD_ID_LENGTH_MAX, "%s.%s", type, id);
485 RRDSET *st = rrdset_find(fullid);
487 error("Cannot create rrd stats for '%s', it already exists.", fullid);
491 long rentries = config_get_number(fullid, "history", rrd_default_history_entries);
492 long entries = align_entries_to_pagesize(rentries);
493 if(entries != rentries) entries = config_set_number(fullid, "history", entries);
495 int enabled = config_get_boolean(fullid, "enabled", 1);
496 if(!enabled) entries = 5;
498 unsigned long size = sizeof(RRDSET);
499 char *cache_dir = rrdset_cache_dir(fullid);
501 debug(D_RRD_CALLS, "Creating RRD_STATS for '%s.%s'.", type, id);
503 snprintfz(fullfilename, FILENAME_MAX, "%s/main.db", cache_dir);
504 if(rrd_memory_mode != RRD_MEMORY_MODE_RAM) st = (RRDSET *)mymmap(fullfilename, size, ((rrd_memory_mode == RRD_MEMORY_MODE_MAP)?MAP_SHARED:MAP_PRIVATE), 0);
506 if(strcmp(st->magic, RRDSET_MAGIC) != 0) {
508 info("Initializing file %s.", fullfilename);
511 else if(strcmp(st->id, fullid) != 0) {
513 error("File %s contents are not for chart %s. Clearing it.", fullfilename, fullid);
518 else if(st->memsize != size || st->entries != entries) {
520 error("File %s does not have the desired size. Clearing it.", fullfilename);
523 else if(st->update_every != update_every) {
525 error("File %s does not have the desired update frequency. Clearing it.", fullfilename);
528 else if((now_realtime_sec() - st->last_updated.tv_sec) > update_every * entries) {
530 error("File %s is too old. Clearing it.", fullfilename);
534 // make sure the database is aligned
535 if(st->last_updated.tv_sec)
536 timeval_align(&st->last_updated, update_every);
546 st->dimensions = NULL;
548 st->mapped = rrd_memory_mode;
549 st->variables = NULL;
553 st = callocz(1, size);
554 st->mapped = RRD_MEMORY_MODE_RAM;
558 st->entries = entries;
559 st->update_every = update_every;
561 if(st->current_entry >= st->entries) st->current_entry = 0;
563 strcpy(st->cache_filename, fullfilename);
564 strcpy(st->magic, RRDSET_MAGIC);
566 strcpy(st->id, fullid);
567 st->hash = simple_hash(st->id);
569 st->cache_dir = cache_dir;
571 st->chart_type = rrdset_type_id(config_get(st->id, "chart type", rrdset_type_name(chart_type)));
572 st->type = config_get(st->id, "type", type);
573 st->family = config_get(st->id, "family", family?family:st->type);
574 st->units = config_get(st->id, "units", units?units:"");
576 st->context = config_get(st->id, "context", context?context:st->id);
577 st->hash_context = simple_hash(st->context);
579 st->priority = config_get_number(st->id, "priority", priority);
580 st->enabled = enabled;
585 // if(!strcmp(st->id, "disk_util.dm-0")) {
587 // error("enabled debugging for '%s'", st->id);
589 // else error("not enabled debugging for '%s'", st->id);
594 st->last_collected_time.tv_sec = 0;
595 st->last_collected_time.tv_usec = 0;
596 st->counter_done = 0;
598 st->gap_when_lost_iterations_above = (int) (
599 config_get_number(st->id, "gap when lost iterations above", RRD_DEFAULT_GAP_INTERPOLATIONS) + 2);
601 avl_init_lock(&st->dimensions_index, rrddim_compare);
602 avl_init_lock(&st->variables_root_index, rrdvar_compare);
604 pthread_rwlock_init(&st->rwlock, NULL);
605 rrdhost_rwlock(&localhost);
607 if(name && *name) rrdset_set_name(st, name);
608 else rrdset_set_name(st, id);
611 char varvalue[CONFIG_MAX_VALUE + 1];
612 char varvalue2[CONFIG_MAX_VALUE + 1];
613 snprintfz(varvalue, CONFIG_MAX_VALUE, "%s (%s)", title?title:"", st->name);
614 json_escape_string(varvalue2, varvalue, sizeof(varvalue2));
615 st->title = config_get(st->id, "title", varvalue2);
618 st->rrdfamily = rrdfamily_create(st->family);
619 st->rrdhost = &localhost;
621 st->next = localhost.rrdset_root;
622 localhost.rrdset_root = st;
625 rrdsetvar_create(st, "last_collected_t", RRDVAR_TYPE_TIME_T, &st->last_collected_time.tv_sec, 0);
626 rrdsetvar_create(st, "collected_total_raw", RRDVAR_TYPE_TOTAL, &st->last_collected_total, 0);
627 rrdsetvar_create(st, "green", RRDVAR_TYPE_CALCULATED, &st->green, 0);
628 rrdsetvar_create(st, "red", RRDVAR_TYPE_CALCULATED, &st->red, 0);
629 rrdsetvar_create(st, "update_every", RRDVAR_TYPE_INT, &st->update_every, 0);
632 rrdset_index_add(&localhost, st);
634 rrdsetcalc_link_matching(st);
635 rrdcalctemplate_link_matching(st);
637 rrdhost_unlock(&localhost);
642 RRDDIM *rrddim_add(RRDSET *st, const char *id, const char *name, long multiplier, long divisor, int algorithm)
644 char filename[FILENAME_MAX + 1];
645 char fullfilename[FILENAME_MAX + 1];
647 char varname[CONFIG_MAX_NAME + 1];
649 unsigned long size = sizeof(RRDDIM) + (st->entries * sizeof(storage_number));
651 debug(D_RRD_CALLS, "Adding dimension '%s/%s'.", st->id, id);
653 rrdset_strncpyz_name(filename, id, FILENAME_MAX);
654 snprintfz(fullfilename, FILENAME_MAX, "%s/%s.db", st->cache_dir, filename);
655 if(rrd_memory_mode != RRD_MEMORY_MODE_RAM) rd = (RRDDIM *)mymmap(fullfilename, size, ((rrd_memory_mode == RRD_MEMORY_MODE_MAP)?MAP_SHARED:MAP_PRIVATE), 1);
658 now_realtime_timeval(&now);
660 if(strcmp(rd->magic, RRDDIMENSION_MAGIC) != 0) {
662 info("Initializing file %s.", fullfilename);
665 else if(rd->memsize != size) {
667 error("File %s does not have the desired size. Clearing it.", fullfilename);
670 else if(rd->multiplier != multiplier) {
672 error("File %s does not have the same multiplier. Clearing it.", fullfilename);
675 else if(rd->divisor != divisor) {
677 error("File %s does not have the same divisor. Clearing it.", fullfilename);
680 else if(rd->algorithm != algorithm) {
682 error("File %s does not have the same algorithm. Clearing it.", fullfilename);
685 else if(rd->update_every != st->update_every) {
687 error("File %s does not have the same refresh frequency. Clearing it.", fullfilename);
690 else if(dt_usec(&now, &rd->last_collected_time) > (rd->entries * rd->update_every * USEC_PER_SEC)) {
692 error("File %s is too old. Clearing it.", fullfilename);
695 else if(strcmp(rd->id, id) != 0) {
697 error("File %s contents are not for dimension %s. Clearing it.", fullfilename, id);
705 // we have a file mapped for rd
706 rd->mapped = rrd_memory_mode;
707 rd->flags = 0x00000000;
708 rd->variables = NULL;
713 // if we didn't manage to get a mmap'd dimension, just create one
715 rd = callocz(1, size);
716 rd->mapped = RRD_MEMORY_MODE_RAM;
720 strcpy(rd->magic, RRDDIMENSION_MAGIC);
721 strcpy(rd->cache_filename, fullfilename);
722 strncpyz(rd->id, id, RRD_ID_LENGTH_MAX);
723 rd->hash = simple_hash(rd->id);
725 snprintfz(varname, CONFIG_MAX_NAME, "dim %s name", rd->id);
726 rd->name = config_get(st->id, varname, (name && *name)?name:rd->id);
728 snprintfz(varname, CONFIG_MAX_NAME, "dim %s algorithm", rd->id);
729 rd->algorithm = rrddim_algorithm_id(config_get(st->id, varname, rrddim_algorithm_name(algorithm)));
731 snprintfz(varname, CONFIG_MAX_NAME, "dim %s multiplier", rd->id);
732 rd->multiplier = config_get_number(st->id, varname, multiplier);
734 snprintfz(varname, CONFIG_MAX_NAME, "dim %s divisor", rd->id);
735 rd->divisor = config_get_number(st->id, varname, divisor);
736 if(!rd->divisor) rd->divisor = 1;
738 rd->entries = st->entries;
739 rd->update_every = st->update_every;
741 // prevent incremental calculation spikes
744 rd->calculated_value = 0;
745 rd->last_calculated_value = 0;
746 rd->collected_value = 0;
747 rd->last_collected_value = 0;
748 rd->collected_volume = 0;
749 rd->stored_volume = 0;
750 rd->last_stored_value = 0;
751 rd->values[st->current_entry] = pack_storage_number(0, SN_NOT_EXISTS);
752 rd->last_collected_time.tv_sec = 0;
753 rd->last_collected_time.tv_usec = 0;
756 // append this dimension
757 pthread_rwlock_wrlock(&st->rwlock);
761 RRDDIM *td = st->dimensions;
762 for(; td->next; td = td->next) ;
767 rrddimvar_create(rd, RRDVAR_TYPE_CALCULATED, NULL, NULL, &rd->last_stored_value, 0);
768 rrddimvar_create(rd, RRDVAR_TYPE_COLLECTED, NULL, "_raw", &rd->last_collected_value, 0);
769 rrddimvar_create(rd, RRDVAR_TYPE_TIME_T, NULL, "_last_collected_t", &rd->last_collected_time.tv_sec, 0);
772 pthread_rwlock_unlock(&st->rwlock);
774 rrddim_index_add(st, rd);
779 void rrddim_set_name(RRDSET *st, RRDDIM *rd, const char *name)
781 if(unlikely(rd->name && !strcmp(rd->name, name)))
784 debug(D_RRD_CALLS, "rrddim_set_name() from %s.%s to %s.%s", st->name, rd->name, st->name, name);
786 char varname[CONFIG_MAX_NAME + 1];
787 snprintfz(varname, CONFIG_MAX_NAME, "dim %s name", rd->id);
788 rd->name = config_set_default(st->id, varname, name);
790 rrddimvar_rename_all(rd);
793 void rrddim_free(RRDSET *st, RRDDIM *rd)
795 debug(D_RRD_CALLS, "rrddim_free() %s.%s", st->name, rd->name);
797 if(rd == st->dimensions)
798 st->dimensions = rd->next;
801 for (i = st->dimensions; i && i->next != rd; i = i->next) ;
803 if (i && i->next == rd)
806 error("Request to free dimension '%s.%s' but it is not linked.", st->id, rd->name);
811 rrddimvar_free(rd->variables);
813 rrddim_index_del(st, rd);
815 // free(rd->annotations);
816 if(rd->mapped == RRD_MEMORY_MODE_SAVE) {
817 debug(D_RRD_CALLS, "Saving dimension '%s' to '%s'.", rd->name, rd->cache_filename);
818 savememory(rd->cache_filename, rd, rd->memsize);
820 debug(D_RRD_CALLS, "Unmapping dimension '%s'.", rd->name);
821 munmap(rd, rd->memsize);
823 else if(rd->mapped == RRD_MEMORY_MODE_MAP) {
824 debug(D_RRD_CALLS, "Unmapping dimension '%s'.", rd->name);
825 munmap(rd, rd->memsize);
828 debug(D_RRD_CALLS, "Removing dimension '%s'.", rd->name);
833 void rrdset_free_all(void)
835 info("Freeing all memory...");
837 rrdhost_rwlock(&localhost);
840 for(st = localhost.rrdset_root; st ;) {
841 RRDSET *next = st->next;
843 pthread_rwlock_wrlock(&st->rwlock);
846 rrdsetvar_free(st->variables);
849 rrdsetcalc_unlink(st->alarms);
851 while(st->dimensions)
852 rrddim_free(st, st->dimensions);
854 rrdset_index_del(&localhost, st);
856 st->rrdfamily->use_count--;
857 if(!st->rrdfamily->use_count)
858 rrdfamily_free(st->rrdfamily);
860 pthread_rwlock_unlock(&st->rwlock);
862 if(st->mapped == RRD_MEMORY_MODE_SAVE) {
863 debug(D_RRD_CALLS, "Saving stats '%s' to '%s'.", st->name, st->cache_filename);
864 savememory(st->cache_filename, st, st->memsize);
866 debug(D_RRD_CALLS, "Unmapping stats '%s'.", st->name);
867 munmap(st, st->memsize);
869 else if(st->mapped == RRD_MEMORY_MODE_MAP) {
870 debug(D_RRD_CALLS, "Unmapping stats '%s'.", st->name);
871 munmap(st, st->memsize);
878 localhost.rrdset_root = NULL;
880 rrdhost_unlock(&localhost);
882 info("Memory cleanup completed...");
885 void rrdset_save_all(void) {
886 info("Saving database...");
891 rrdhost_rwlock(&localhost);
892 for(st = localhost.rrdset_root; st ; st = st->next) {
893 pthread_rwlock_wrlock(&st->rwlock);
895 if(st->mapped == RRD_MEMORY_MODE_SAVE) {
896 debug(D_RRD_CALLS, "Saving stats '%s' to '%s'.", st->name, st->cache_filename);
897 savememory(st->cache_filename, st, st->memsize);
900 for(rd = st->dimensions; rd ; rd = rd->next) {
901 if(likely(rd->mapped == RRD_MEMORY_MODE_SAVE)) {
902 debug(D_RRD_CALLS, "Saving dimension '%s' to '%s'.", rd->name, rd->cache_filename);
903 savememory(rd->cache_filename, rd, rd->memsize);
907 pthread_rwlock_unlock(&st->rwlock);
909 rrdhost_unlock(&localhost);
913 RRDSET *rrdset_find(const char *id)
915 debug(D_RRD_CALLS, "rrdset_find() for chart %s", id);
917 RRDSET *st = rrdset_index_find(&localhost, id, 0);
921 RRDSET *rrdset_find_bytype(const char *type, const char *id)
923 debug(D_RRD_CALLS, "rrdset_find_bytype() for chart %s.%s", type, id);
925 char buf[RRD_ID_LENGTH_MAX + 1];
927 strncpyz(buf, type, RRD_ID_LENGTH_MAX - 1);
929 int len = (int) strlen(buf);
930 strncpyz(&buf[len], id, (size_t) (RRD_ID_LENGTH_MAX - len));
932 return(rrdset_find(buf));
935 RRDSET *rrdset_find_byname(const char *name)
937 debug(D_RRD_CALLS, "rrdset_find_byname() for chart %s", name);
939 RRDSET *st = rrdset_index_find_name(&localhost, name, 0);
943 RRDDIM *rrddim_find(RRDSET *st, const char *id)
945 debug(D_RRD_CALLS, "rrddim_find() for chart %s, dimension %s", st->name, id);
947 return rrddim_index_find(st, id, 0);
950 int rrddim_hide(RRDSET *st, const char *id)
952 debug(D_RRD_CALLS, "rrddim_hide() for chart %s, dimension %s", st->name, id);
954 RRDDIM *rd = rrddim_find(st, id);
956 error("Cannot find dimension with id '%s' on stats '%s' (%s).", id, st->name, st->id);
960 rd->flags |= RRDDIM_FLAG_HIDDEN;
964 int rrddim_unhide(RRDSET *st, const char *id)
966 debug(D_RRD_CALLS, "rrddim_unhide() for chart %s, dimension %s", st->name, id);
968 RRDDIM *rd = rrddim_find(st, id);
970 error("Cannot find dimension with id '%s' on stats '%s' (%s).", id, st->name, st->id);
974 if(rd->flags & RRDDIM_FLAG_HIDDEN) rd->flags ^= RRDDIM_FLAG_HIDDEN;
978 collected_number rrddim_set_by_pointer(RRDSET *st, RRDDIM *rd, collected_number value)
980 debug(D_RRD_CALLS, "rrddim_set_by_pointer() for chart %s, dimension %s, value " COLLECTED_NUMBER_FORMAT, st->name, rd->name, value);
982 now_realtime_timeval(&rd->last_collected_time);
983 rd->collected_value = value;
987 // fprintf(stderr, "%s.%s %llu " COLLECTED_NUMBER_FORMAT " dt %0.6f" " rate " CALCULATED_NUMBER_FORMAT "\n", st->name, rd->name, st->usec_since_last_update, value, (float)((double)st->usec_since_last_update / (double)1000000), (calculated_number)((value - rd->last_collected_value) * (calculated_number)rd->multiplier / (calculated_number)rd->divisor * 1000000.0 / (calculated_number)st->usec_since_last_update));
989 return rd->last_collected_value;
992 collected_number rrddim_set(RRDSET *st, const char *id, collected_number value)
994 RRDDIM *rd = rrddim_find(st, id);
996 error("Cannot find dimension with id '%s' on stats '%s' (%s).", id, st->name, st->id);
1000 return rrddim_set_by_pointer(st, rd, value);
1003 void rrdset_next_usec_unfiltered(RRDSET *st, usec_t microseconds)
1005 if(unlikely(!st->last_collected_time.tv_sec || !microseconds)) {
1007 microseconds = st->update_every * USEC_PER_SEC;
1009 st->usec_since_last_update = microseconds;
1012 void rrdset_next_usec(RRDSET *st, usec_t microseconds)
1015 now_realtime_timeval(&now);
1017 if(unlikely(!st->last_collected_time.tv_sec)) {
1019 microseconds = st->update_every * USEC_PER_SEC;
1021 else if(unlikely(!microseconds)) {
1022 // no dt given by the plugin
1023 microseconds = dt_usec(&now, &st->last_collected_time);
1026 // microseconds has the time since the last collection
1027 usec_t now_usec = timeval_usec(&now);
1028 usec_t last_usec = timeval_usec(&st->last_collected_time);
1029 usec_t since_last_usec = dt_usec(&now, &st->last_collected_time);
1031 // verify the microseconds given is good
1032 if(unlikely(microseconds > since_last_usec)) {
1033 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);
1035 #ifdef NETDATA_INTERNAL_CHECKS
1036 if(unlikely(last_usec + microseconds > now_usec + 1000))
1037 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);
1040 microseconds = since_last_usec;
1042 else if(unlikely(microseconds < since_last_usec * 0.8)) {
1043 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);
1045 #ifdef NETDATA_INTERNAL_CHECKS
1046 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);
1048 microseconds = since_last_usec;
1051 debug(D_RRD_CALLS, "rrdset_next_usec() for chart %s with microseconds %llu", st->name, microseconds);
1053 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s: NEXT: %llu microseconds", st->name, microseconds);
1054 st->usec_since_last_update = microseconds;
1057 usec_t rrdset_done(RRDSET *st)
1059 if(unlikely(netdata_exit)) return 0;
1061 debug(D_RRD_CALLS, "rrdset_done() for chart %s", st->name);
1066 pthreadoldcancelstate; // store the old cancelable pthread state, to restore it at the end
1069 store_this_entry = 1, // boolean: 1 = store this entry, 0 = don't store this entry
1070 first_entry = 0; // boolean: 1 = this is the first entry seen for this chart, 0 = all other entries
1073 stored_entries = 0; // the number of entries we have stored in the db, during this call to rrdset_done()
1076 last_collect_ut, // the timestamp in microseconds, of the last collected value
1077 now_collect_ut, // the timestamp in microseconds, of this collected value (this is NOW)
1078 last_stored_ut, // the timestamp in microseconds, of the last stored entry in the db
1079 next_store_ut, // the timestamp in microseconds, of the next entry to store in the db
1080 update_every_ut = st->update_every * USEC_PER_SEC; // st->update_every in microseconds
1082 if(unlikely(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &pthreadoldcancelstate) != 0))
1083 error("Cannot set pthread cancel state to DISABLE.");
1085 // a read lock is OK here
1086 pthread_rwlock_rdlock(&st->rwlock);
1089 // enable the chart, if it was disabled
1090 if(unlikely(rrd_delete_unupdated_dimensions) && !st->enabled)
1094 // check if the chart has a long time to be updated
1095 if(unlikely(st->usec_since_last_update > st->entries * update_every_ut)) {
1096 info("%s: took too long to be updated (%0.3Lf secs). Reseting it.", st->name, (long double)(st->usec_since_last_update / 1000000.0));
1098 st->usec_since_last_update = update_every_ut;
1101 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s: microseconds since last update: %llu", st->name, st->usec_since_last_update);
1103 // set last_collected_time
1104 if(unlikely(!st->last_collected_time.tv_sec)) {
1105 // it is the first entry
1106 // set the last_collected_time to now
1107 now_realtime_timeval(&st->last_collected_time);
1108 timeval_align(&st->last_collected_time, st->update_every);
1110 last_collect_ut = st->last_collected_time.tv_sec * USEC_PER_SEC + st->last_collected_time.tv_usec - update_every_ut;
1112 // the first entry should not be stored
1113 store_this_entry = 0;
1116 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s: has not set last_collected_time. Setting it now. Will not store the next entry.", st->name);
1119 // it is not the first entry
1120 // calculate the proper last_collected_time, using usec_since_last_update
1121 last_collect_ut = st->last_collected_time.tv_sec * USEC_PER_SEC + st->last_collected_time.tv_usec;
1122 usec_t ut = last_collect_ut + st->usec_since_last_update;
1123 st->last_collected_time.tv_sec = (time_t) (ut / USEC_PER_SEC);
1124 st->last_collected_time.tv_usec = (suseconds_t) (ut % USEC_PER_SEC);
1127 // if this set has not been updated in the past
1128 // we fake the last_update time to be = now - usec_since_last_update
1129 if(unlikely(!st->last_updated.tv_sec)) {
1130 // it has never been updated before
1131 // set a fake last_updated, in the past using usec_since_last_update
1132 usec_t ut = st->last_collected_time.tv_sec * USEC_PER_SEC + st->last_collected_time.tv_usec - st->usec_since_last_update;
1133 st->last_updated.tv_sec = (time_t) (ut / USEC_PER_SEC);
1134 st->last_updated.tv_usec = (suseconds_t) (ut % USEC_PER_SEC);
1136 // the first entry should not be stored
1137 store_this_entry = 0;
1140 if(unlikely(st->debug)) 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);
1143 // check if we will re-write the entire data set
1144 if(unlikely(dt_usec(&st->last_collected_time, &st->last_updated) > st->entries * update_every_ut)) {
1145 info("%s: too old data (last updated at %ld.%ld, last collected at %ld.%ld). Reseting 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);
1148 st->usec_since_last_update = update_every_ut;
1150 now_realtime_timeval(&st->last_collected_time);
1151 timeval_align(&st->last_collected_time, st->update_every);
1153 usec_t ut = st->last_collected_time.tv_sec * USEC_PER_SEC + st->last_collected_time.tv_usec - st->usec_since_last_update;
1154 st->last_updated.tv_sec = (time_t) (ut / USEC_PER_SEC);
1155 st->last_updated.tv_usec = (suseconds_t) (ut % USEC_PER_SEC);
1157 // the first entry should not be stored
1158 store_this_entry = 0;
1162 // these are the 3 variables that will help us in interpolation
1163 // last_stored_ut = the last time we added a value to the storage
1164 // now_collect_ut = the time the current value has been collected
1165 // next_store_ut = the time of the next interpolation point
1166 last_stored_ut = st->last_updated.tv_sec * USEC_PER_SEC + st->last_updated.tv_usec;
1167 now_collect_ut = st->last_collected_time.tv_sec * USEC_PER_SEC + st->last_collected_time.tv_usec;
1168 next_store_ut = (st->last_updated.tv_sec + st->update_every) * USEC_PER_SEC;
1170 if(unlikely(st->debug)) {
1171 debug(D_RRD_STATS, "%s: last_collect_ut = %0.3Lf (last collection time)", st->name, (long double)last_collect_ut/1000000.0);
1172 debug(D_RRD_STATS, "%s: now_collect_ut = %0.3Lf (current collection time)", st->name, (long double)now_collect_ut/1000000.0);
1173 debug(D_RRD_STATS, "%s: last_stored_ut = %0.3Lf (last updated time)", st->name, (long double)last_stored_ut/1000000.0);
1174 debug(D_RRD_STATS, "%s: next_store_ut = %0.3Lf (next interpolation point)", st->name, (long double)next_store_ut/1000000.0);
1177 if(unlikely(!st->counter_done)) {
1178 store_this_entry = 0;
1179 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s: Will not store the next entry.", st->name);
1183 // calculate totals and count the dimensions
1185 st->collected_total = 0;
1186 for( rd = st->dimensions, dimensions = 0 ; rd ; rd = rd->next, dimensions++ )
1187 if(likely(rd->updated)) st->collected_total += rd->collected_value;
1189 uint32_t storage_flags = SN_EXISTS;
1191 // process all dimensions to calculate their values
1192 // based on the collected figures only
1193 // at this stage we do not interpolate anything
1194 for( rd = st->dimensions ; rd ; rd = rd->next ) {
1196 if(unlikely(!rd->updated)) {
1197 rd->calculated_value = 0;
1201 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s/%s: START "
1202 " last_collected_value = " COLLECTED_NUMBER_FORMAT
1203 " collected_value = " COLLECTED_NUMBER_FORMAT
1204 " last_calculated_value = " CALCULATED_NUMBER_FORMAT
1205 " calculated_value = " CALCULATED_NUMBER_FORMAT
1207 , rd->last_collected_value
1208 , rd->collected_value
1209 , rd->last_calculated_value
1210 , rd->calculated_value
1213 switch(rd->algorithm) {
1214 case RRDDIM_ABSOLUTE:
1215 rd->calculated_value = (calculated_number)rd->collected_value
1216 * (calculated_number)rd->multiplier
1217 / (calculated_number)rd->divisor;
1219 if(unlikely(st->debug))
1220 debug(D_RRD_STATS, "%s/%s: CALC ABS/ABS-NO-IN "
1221 CALCULATED_NUMBER_FORMAT " = "
1222 COLLECTED_NUMBER_FORMAT
1223 " * " CALCULATED_NUMBER_FORMAT
1224 " / " CALCULATED_NUMBER_FORMAT
1226 , rd->calculated_value
1227 , rd->collected_value
1228 , (calculated_number)rd->multiplier
1229 , (calculated_number)rd->divisor
1233 case RRDDIM_PCENT_OVER_ROW_TOTAL:
1234 if(unlikely(!st->collected_total))
1235 rd->calculated_value = 0;
1237 // the percentage of the current value
1238 // over the total of all dimensions
1239 rd->calculated_value =
1240 (calculated_number)100
1241 * (calculated_number)rd->collected_value
1242 / (calculated_number)st->collected_total;
1244 if(unlikely(st->debug))
1245 debug(D_RRD_STATS, "%s/%s: CALC PCENT-ROW "
1246 CALCULATED_NUMBER_FORMAT " = 100"
1247 " * " COLLECTED_NUMBER_FORMAT
1248 " / " COLLECTED_NUMBER_FORMAT
1250 , rd->calculated_value
1251 , rd->collected_value
1252 , st->collected_total
1256 case RRDDIM_INCREMENTAL:
1257 if(unlikely(rd->counter <= 1)) {
1258 rd->calculated_value = 0;
1262 // if the new is smaller than the old (an overflow, or reset), set the old equal to the new
1263 // to reset the calculation (it will give zero as the calculation for this second)
1264 if(unlikely(rd->last_collected_value > rd->collected_value)) {
1265 debug(D_RRD_STATS, "%s.%s: RESET or OVERFLOW. Last collected value = " COLLECTED_NUMBER_FORMAT ", current = " COLLECTED_NUMBER_FORMAT
1266 , st->name, rd->name
1267 , rd->last_collected_value
1268 , rd->collected_value);
1269 if(!(rd->flags & RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS)) storage_flags = SN_EXISTS_RESET;
1270 rd->last_collected_value = rd->collected_value;
1273 rd->calculated_value +=
1274 (calculated_number)(rd->collected_value - rd->last_collected_value)
1275 * (calculated_number)rd->multiplier
1276 / (calculated_number)rd->divisor;
1278 if(unlikely(st->debug))
1279 debug(D_RRD_STATS, "%s/%s: CALC INC PRE "
1280 CALCULATED_NUMBER_FORMAT " = ("
1281 COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT
1283 " * " CALCULATED_NUMBER_FORMAT
1284 " / " CALCULATED_NUMBER_FORMAT
1286 , rd->calculated_value
1287 , rd->collected_value, rd->last_collected_value
1288 , (calculated_number)rd->multiplier
1289 , (calculated_number)rd->divisor
1293 case RRDDIM_PCENT_OVER_DIFF_TOTAL:
1294 if(unlikely(rd->counter <= 1)) {
1295 rd->calculated_value = 0;
1299 // if the new is smaller than the old (an overflow, or reset), set the old equal to the new
1300 // to reset the calculation (it will give zero as the calculation for this second)
1301 if(unlikely(rd->last_collected_value > rd->collected_value)) {
1302 debug(D_RRD_STATS, "%s.%s: RESET or OVERFLOW. Last collected value = " COLLECTED_NUMBER_FORMAT ", current = " COLLECTED_NUMBER_FORMAT
1303 , st->name, rd->name
1304 , rd->last_collected_value
1305 , rd->collected_value);
1306 if(!(rd->flags & RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS)) storage_flags = SN_EXISTS_RESET;
1307 rd->last_collected_value = rd->collected_value;
1310 // the percentage of the current increment
1311 // over the increment of all dimensions together
1312 if(unlikely(st->collected_total == st->last_collected_total))
1313 rd->calculated_value = 0;
1315 rd->calculated_value =
1316 (calculated_number)100
1317 * (calculated_number)(rd->collected_value - rd->last_collected_value)
1318 / (calculated_number)(st->collected_total - st->last_collected_total);
1320 if(unlikely(st->debug))
1321 debug(D_RRD_STATS, "%s/%s: CALC PCENT-DIFF "
1322 CALCULATED_NUMBER_FORMAT " = 100"
1323 " * (" COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT ")"
1324 " / (" COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT ")"
1326 , rd->calculated_value
1327 , rd->collected_value, rd->last_collected_value
1328 , st->collected_total, st->last_collected_total
1333 // make the default zero, to make sure
1334 // it gets noticed when we add new types
1335 rd->calculated_value = 0;
1337 if(unlikely(st->debug))
1338 debug(D_RRD_STATS, "%s/%s: CALC "
1339 CALCULATED_NUMBER_FORMAT " = 0"
1341 , rd->calculated_value
1346 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s/%s: PHASE2 "
1347 " last_collected_value = " COLLECTED_NUMBER_FORMAT
1348 " collected_value = " COLLECTED_NUMBER_FORMAT
1349 " last_calculated_value = " CALCULATED_NUMBER_FORMAT
1350 " calculated_value = " CALCULATED_NUMBER_FORMAT
1352 , rd->last_collected_value
1353 , rd->collected_value
1354 , rd->last_calculated_value
1355 , rd->calculated_value
1360 // at this point we have all the calculated values ready
1361 // it is now time to interpolate values on a second boundary
1363 if(unlikely(now_collect_ut < next_store_ut)) {
1364 // this is collected in the same interpolation point
1365 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s: THIS IS IN THE SAME INTERPOLATION POINT", st->name);
1366 #ifdef NETDATA_INTERNAL_CHECKS
1367 info("%s is collected in the same interpolation point: short by %llu microseconds", st->name, next_store_ut - now_collect_ut);
1371 usec_t first_ut = last_stored_ut;
1372 long long iterations = (now_collect_ut - last_stored_ut) / (update_every_ut);
1373 if((now_collect_ut % (update_every_ut)) == 0) iterations++;
1375 for( ; next_store_ut <= now_collect_ut ; last_collect_ut = next_store_ut, next_store_ut += update_every_ut, iterations-- ) {
1376 #ifdef NETDATA_INTERNAL_CHECKS
1377 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); }
1380 if(unlikely(st->debug)) {
1381 debug(D_RRD_STATS, "%s: last_stored_ut = %0.3Lf (last updated time)", st->name, (long double)last_stored_ut/1000000.0);
1382 debug(D_RRD_STATS, "%s: next_store_ut = %0.3Lf (next interpolation point)", st->name, (long double)next_store_ut/1000000.0);
1385 st->last_updated.tv_sec = (time_t) (next_store_ut / USEC_PER_SEC);
1386 st->last_updated.tv_usec = 0;
1388 for( rd = st->dimensions ; likely(rd) ; rd = rd->next ) {
1389 calculated_number new_value;
1391 switch(rd->algorithm) {
1392 case RRDDIM_INCREMENTAL:
1393 new_value = (calculated_number)
1394 ( rd->calculated_value
1395 * (calculated_number)(next_store_ut - last_collect_ut)
1396 / (calculated_number)(now_collect_ut - last_collect_ut)
1399 if(unlikely(st->debug))
1400 debug(D_RRD_STATS, "%s/%s: CALC2 INC "
1401 CALCULATED_NUMBER_FORMAT " = "
1402 CALCULATED_NUMBER_FORMAT
1407 , rd->calculated_value
1408 , (next_store_ut - last_stored_ut)
1409 , (now_collect_ut - last_stored_ut)
1412 rd->calculated_value -= new_value;
1413 new_value += rd->last_calculated_value;
1414 rd->last_calculated_value = 0;
1415 new_value /= (calculated_number)st->update_every;
1417 if(unlikely(next_store_ut - last_stored_ut < update_every_ut)) {
1418 if(unlikely(st->debug))
1419 debug(D_RRD_STATS, "%s/%s: COLLECTION POINT IS SHORT " CALCULATED_NUMBER_FORMAT " - EXTRAPOLATING",
1421 , (calculated_number)(next_store_ut - last_stored_ut)
1423 new_value = new_value * (calculated_number)(st->update_every * 1000000) / (calculated_number)(next_store_ut - last_stored_ut);
1427 case RRDDIM_ABSOLUTE:
1428 case RRDDIM_PCENT_OVER_ROW_TOTAL:
1429 case RRDDIM_PCENT_OVER_DIFF_TOTAL:
1431 if(iterations == 1) {
1432 // this is the last iteration
1433 // do not interpolate
1434 // just show the calculated value
1436 new_value = rd->calculated_value;
1439 // we have missed an update
1440 // interpolate in the middle values
1442 new_value = (calculated_number)
1443 ( ( (rd->calculated_value - rd->last_calculated_value)
1444 * (calculated_number)(next_store_ut - last_collect_ut)
1445 / (calculated_number)(now_collect_ut - last_collect_ut)
1447 + rd->last_calculated_value
1450 if(unlikely(st->debug))
1451 debug(D_RRD_STATS, "%s/%s: CALC2 DEF "
1452 CALCULATED_NUMBER_FORMAT " = ((("
1453 "(" CALCULATED_NUMBER_FORMAT " - " CALCULATED_NUMBER_FORMAT ")"
1455 " / %llu) + " CALCULATED_NUMBER_FORMAT
1458 , rd->calculated_value, rd->last_calculated_value
1459 , (next_store_ut - first_ut)
1460 , (now_collect_ut - first_ut), rd->last_calculated_value
1466 if(unlikely(!store_this_entry)) {
1467 rd->values[st->current_entry] = pack_storage_number(0, SN_NOT_EXISTS);
1471 if(likely(rd->updated && rd->counter > 1 && iterations < st->gap_when_lost_iterations_above)) {
1472 rd->values[st->current_entry] = pack_storage_number(new_value, storage_flags );
1473 rd->last_stored_value = new_value;
1475 if(unlikely(st->debug))
1476 debug(D_RRD_STATS, "%s/%s: STORE[%ld] "
1477 CALCULATED_NUMBER_FORMAT " = " CALCULATED_NUMBER_FORMAT
1480 , unpack_storage_number(rd->values[st->current_entry]), new_value
1484 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s/%s: STORE[%ld] = NON EXISTING "
1488 rd->values[st->current_entry] = pack_storage_number(0, SN_NOT_EXISTS);
1489 rd->last_stored_value = NAN;
1494 if(unlikely(st->debug)) {
1495 calculated_number t1 = new_value * (calculated_number)rd->multiplier / (calculated_number)rd->divisor;
1496 calculated_number t2 = unpack_storage_number(rd->values[st->current_entry]);
1497 calculated_number accuracy = accuracy_loss(t1, t2);
1498 debug(D_RRD_STATS, "%s/%s: UNPACK[%ld] = " CALCULATED_NUMBER_FORMAT " FLAGS=0x%08x (original = " CALCULATED_NUMBER_FORMAT ", accuracy loss = " CALCULATED_NUMBER_FORMAT "%%%s)"
1502 , get_storage_number_flags(rd->values[st->current_entry])
1505 , (accuracy > ACCURACY_LOSS) ? " **TOO BIG** " : ""
1508 rd->collected_volume += t1;
1509 rd->stored_volume += t2;
1510 accuracy = accuracy_loss(rd->collected_volume, rd->stored_volume);
1511 debug(D_RRD_STATS, "%s/%s: VOLUME[%ld] = " CALCULATED_NUMBER_FORMAT ", calculated = " CALCULATED_NUMBER_FORMAT ", accuracy loss = " CALCULATED_NUMBER_FORMAT "%%%s"
1515 , rd->collected_volume
1517 , (accuracy > ACCURACY_LOSS) ? " **TOO BIG** " : ""
1522 // reset the storage flags for the next point, if any;
1523 storage_flags = SN_EXISTS;
1526 st->current_entry = ((st->current_entry + 1) >= st->entries) ? 0 : st->current_entry + 1;
1527 last_stored_ut = next_store_ut;
1530 st->last_collected_total = st->collected_total;
1532 for( rd = st->dimensions; rd ; rd = rd->next ) {
1533 if(unlikely(!rd->updated)) continue;
1535 if(unlikely(st->debug)) 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);
1536 rd->last_collected_value = rd->collected_value;
1538 switch(rd->algorithm) {
1539 case RRDDIM_INCREMENTAL:
1540 if(unlikely(!first_entry)) {
1541 if(unlikely(st->debug)) 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);
1542 rd->last_calculated_value += rd->calculated_value;
1545 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s: THIS IS THE FIRST POINT", st->name);
1549 case RRDDIM_ABSOLUTE:
1550 case RRDDIM_PCENT_OVER_ROW_TOTAL:
1551 case RRDDIM_PCENT_OVER_DIFF_TOTAL:
1552 if(unlikely(st->debug)) 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);
1553 rd->last_calculated_value = rd->calculated_value;
1557 rd->calculated_value = 0;
1558 rd->collected_value = 0;
1561 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s/%s: END "
1562 " last_collected_value = " COLLECTED_NUMBER_FORMAT
1563 " collected_value = " COLLECTED_NUMBER_FORMAT
1564 " last_calculated_value = " CALCULATED_NUMBER_FORMAT
1565 " calculated_value = " CALCULATED_NUMBER_FORMAT
1567 , rd->last_collected_value
1568 , rd->collected_value
1569 , rd->last_calculated_value
1570 , rd->calculated_value
1574 // ALL DONE ABOUT THE DATA UPDATE
1575 // --------------------------------------------------------------------
1578 // find if there are any obsolete dimensions (not updated recently)
1579 if(unlikely(rrd_delete_unupdated_dimensions)) {
1581 for( rd = st->dimensions; likely(rd) ; rd = rd->next )
1582 if((rd->last_collected_time.tv_sec + (rrd_delete_unupdated_dimensions * st->update_every)) < st->last_collected_time.tv_sec)
1587 // there is dimension to free
1588 // upgrade our read lock to a write lock
1589 pthread_rwlock_unlock(&st->rwlock);
1590 pthread_rwlock_wrlock(&st->rwlock);
1592 for( rd = st->dimensions, last = NULL ; likely(rd) ; ) {
1593 // remove it only it is not updated in rrd_delete_unupdated_dimensions seconds
1595 if(unlikely((rd->last_collected_time.tv_sec + (rrd_delete_unupdated_dimensions * st->update_every)) < st->last_collected_time.tv_sec)) {
1596 info("Removing obsolete dimension '%s' (%s) of '%s' (%s).", rd->name, rd->id, st->name, st->id);
1598 if(unlikely(!last)) {
1599 st->dimensions = rd->next;
1601 rrddim_free(st, rd);
1602 rd = st->dimensions;
1606 last->next = rd->next;
1608 rrddim_free(st, rd);
1618 if(unlikely(!st->dimensions)) {
1619 info("Disabling chart %s (%s) since it does not have any dimensions", st->name, st->id);
1626 pthread_rwlock_unlock(&st->rwlock);
1628 if(unlikely(pthread_setcancelstate(pthreadoldcancelstate, NULL) != 0))
1629 error("Cannot set pthread cancel state to RESTORE (%d).", pthreadoldcancelstate);
1631 return(st->usec_since_last_update);