14 #include <sys/types.h>
19 #include "appconfig.h"
24 #define RRD_DEFAULT_GAP_INTERPOLATIONS 1
26 // ----------------------------------------------------------------------------
29 // if not zero it gives the time (in seconds) to remove un-updated dimensions
31 // if dimensions are removed, the chart generation will have to run again
32 int rrd_delete_unupdated_dimensions = 0;
34 int rrd_update_every = UPDATE_EVERY;
35 int rrd_default_history_entries = RRD_DEFAULT_HISTORY_ENTRIES;
37 RRDSET *rrdset_root = NULL;
38 pthread_rwlock_t rrdset_root_rwlock = PTHREAD_RWLOCK_INITIALIZER;
40 int rrd_memory_mode = RRD_MEMORY_MODE_SAVE;
43 // ----------------------------------------------------------------------------
46 static int rrdset_compare(void* a, void* b) {
47 if(((RRDSET *)a)->hash < ((RRDSET *)b)->hash) return -1;
48 else if(((RRDSET *)a)->hash > ((RRDSET *)b)->hash) return 1;
49 else return strcmp(((RRDSET *)a)->id, ((RRDSET *)b)->id);
52 avl_tree_lock rrdset_root_index = {
53 { NULL, rrdset_compare },
57 #define rrdset_index_add(st) avl_insert_lock(&rrdset_root_index, (avl *)(st))
58 #define rrdset_index_del(st) avl_remove_lock(&rrdset_root_index, (avl *)(st))
60 static RRDSET *rrdset_index_find(const char *id, uint32_t hash) {
62 strncpyz(tmp.id, id, RRD_ID_LENGTH_MAX);
63 tmp.hash = (hash)?hash:simple_hash(tmp.id);
65 return (RRDSET *)avl_search_lock(&(rrdset_root_index), (avl *) &tmp);
68 // ----------------------------------------------------------------------------
71 #define rrdset_from_avlname(avlname_ptr) ((RRDSET *)((avlname_ptr) - offsetof(RRDSET, avlname)))
73 static int rrdset_compare_name(void* a, void* b) {
74 RRDSET *A = rrdset_from_avlname(a);
75 RRDSET *B = rrdset_from_avlname(b);
77 // fprintf(stderr, "COMPARING: %s with %s\n", A->name, B->name);
79 if(A->hash_name < B->hash_name) return -1;
80 else if(A->hash_name > B->hash_name) return 1;
81 else return strcmp(A->name, B->name);
84 avl_tree_lock rrdset_root_index_name = {
85 { NULL, rrdset_compare_name },
89 int rrdset_index_add_name(RRDSET *st) {
90 // fprintf(stderr, "ADDING: %s (name: %s)\n", st->id, st->name);
91 return avl_insert_lock(&rrdset_root_index_name, (avl *) (&st->avlname));
94 #define rrdset_index_del_name(st) avl_remove_lock(&rrdset_root_index_name, (avl *)(&st->avlname))
96 static RRDSET *rrdset_index_find_name(const char *name, uint32_t hash) {
100 tmp.hash_name = (hash)?hash:simple_hash(tmp.name);
102 // fprintf(stderr, "SEARCHING: %s\n", name);
103 result = avl_search_lock(&(rrdset_root_index_name), (avl *) (&(tmp.avlname)));
105 RRDSET *st = rrdset_from_avlname(result);
106 if(strcmp(st->magic, RRDSET_MAGIC))
107 error("Search for RRDSET %s returned an invalid RRDSET %s (name %s)", name, st->id, st->name);
109 // fprintf(stderr, "FOUND: %s\n", name);
110 return rrdset_from_avlname(result);
112 // fprintf(stderr, "NOT FOUND: %s\n", name);
117 // ----------------------------------------------------------------------------
120 static int rrddim_compare(void* a, void* b) {
121 if(((RRDDIM *)a)->hash < ((RRDDIM *)b)->hash) return -1;
122 else if(((RRDDIM *)a)->hash > ((RRDDIM *)b)->hash) return 1;
123 else return strcmp(((RRDDIM *)a)->id, ((RRDDIM *)b)->id);
126 #define rrddim_index_add(st, rd) avl_insert_lock(&((st)->dimensions_index), (avl *)(rd))
127 #define rrddim_index_del(st,rd ) avl_remove_lock(&((st)->dimensions_index), (avl *)(rd))
129 static RRDDIM *rrddim_index_find(RRDSET *st, const char *id, uint32_t hash) {
131 strncpyz(tmp.id, id, RRD_ID_LENGTH_MAX);
132 tmp.hash = (hash)?hash:simple_hash(tmp.id);
134 return (RRDDIM *)avl_search_lock(&(st->dimensions_index), (avl *) &tmp);
137 // ----------------------------------------------------------------------------
140 int rrdset_type_id(const char *name)
142 if(unlikely(strcmp(name, RRDSET_TYPE_AREA_NAME) == 0)) return RRDSET_TYPE_AREA;
143 else if(unlikely(strcmp(name, RRDSET_TYPE_STACKED_NAME) == 0)) return RRDSET_TYPE_STACKED;
144 else if(unlikely(strcmp(name, RRDSET_TYPE_LINE_NAME) == 0)) return RRDSET_TYPE_LINE;
145 return RRDSET_TYPE_LINE;
148 const char *rrdset_type_name(int chart_type)
150 static char line[] = RRDSET_TYPE_LINE_NAME;
151 static char area[] = RRDSET_TYPE_AREA_NAME;
152 static char stacked[] = RRDSET_TYPE_STACKED_NAME;
155 case RRDSET_TYPE_LINE:
158 case RRDSET_TYPE_AREA:
161 case RRDSET_TYPE_STACKED:
167 // ----------------------------------------------------------------------------
170 const char *rrd_memory_mode_name(int id)
172 static const char ram[] = RRD_MEMORY_MODE_RAM_NAME;
173 static const char map[] = RRD_MEMORY_MODE_MAP_NAME;
174 static const char save[] = RRD_MEMORY_MODE_SAVE_NAME;
177 case RRD_MEMORY_MODE_RAM:
180 case RRD_MEMORY_MODE_MAP:
183 case RRD_MEMORY_MODE_SAVE:
191 int rrd_memory_mode_id(const char *name)
193 if(unlikely(!strcmp(name, RRD_MEMORY_MODE_RAM_NAME)))
194 return RRD_MEMORY_MODE_RAM;
195 else if(unlikely(!strcmp(name, RRD_MEMORY_MODE_MAP_NAME)))
196 return RRD_MEMORY_MODE_MAP;
198 return RRD_MEMORY_MODE_SAVE;
201 // ----------------------------------------------------------------------------
204 int rrddim_algorithm_id(const char *name)
206 if(strcmp(name, RRDDIM_INCREMENTAL_NAME) == 0) return RRDDIM_INCREMENTAL;
207 if(strcmp(name, RRDDIM_ABSOLUTE_NAME) == 0) return RRDDIM_ABSOLUTE;
208 if(strcmp(name, RRDDIM_PCENT_OVER_ROW_TOTAL_NAME) == 0) return RRDDIM_PCENT_OVER_ROW_TOTAL;
209 if(strcmp(name, RRDDIM_PCENT_OVER_DIFF_TOTAL_NAME) == 0) return RRDDIM_PCENT_OVER_DIFF_TOTAL;
210 return RRDDIM_ABSOLUTE;
213 const char *rrddim_algorithm_name(int chart_type)
215 static char absolute[] = RRDDIM_ABSOLUTE_NAME;
216 static char incremental[] = RRDDIM_INCREMENTAL_NAME;
217 static char percentage_of_absolute_row[] = RRDDIM_PCENT_OVER_ROW_TOTAL_NAME;
218 static char percentage_of_incremental_row[] = RRDDIM_PCENT_OVER_DIFF_TOTAL_NAME;
221 case RRDDIM_ABSOLUTE:
224 case RRDDIM_INCREMENTAL:
227 case RRDDIM_PCENT_OVER_ROW_TOTAL:
228 return percentage_of_absolute_row;
230 case RRDDIM_PCENT_OVER_DIFF_TOTAL:
231 return percentage_of_incremental_row;
236 // ----------------------------------------------------------------------------
239 char *rrdset_strncpyz_name(char *to, const char *from, size_t length)
243 while (length-- && (c = *from++)) {
244 if(c != '.' && !isalnum(c))
255 void rrdset_set_name(RRDSET *st, const char *name)
257 debug(D_RRD_CALLS, "rrdset_set_name() old: %s, new: %s", st->name, name);
259 if(st->name) rrdset_index_del_name(st);
261 char b[CONFIG_MAX_VALUE + 1];
262 char n[RRD_ID_LENGTH_MAX + 1];
264 snprintfz(n, RRD_ID_LENGTH_MAX, "%s.%s", st->type, name);
265 rrdset_strncpyz_name(b, n, CONFIG_MAX_VALUE);
266 st->name = config_get(st->id, "name", b);
267 st->hash_name = simple_hash(st->name);
269 rrdset_index_add_name(st);
272 // ----------------------------------------------------------------------------
275 char *rrdset_cache_dir(const char *id)
279 static char *cache_dir = NULL;
281 cache_dir = config_get("global", "cache directory", CACHE_DIR);
282 int r = mkdir(cache_dir, 0755);
283 if(r != 0 && errno != EEXIST)
284 error("Cannot create directory '%s'", cache_dir);
287 char b[FILENAME_MAX + 1];
288 char n[FILENAME_MAX + 1];
289 rrdset_strncpyz_name(b, id, FILENAME_MAX);
291 snprintfz(n, FILENAME_MAX, "%s/%s", cache_dir, b);
292 ret = config_get(id, "cache directory", n);
294 if(rrd_memory_mode == RRD_MEMORY_MODE_MAP || rrd_memory_mode == RRD_MEMORY_MODE_SAVE) {
295 int r = mkdir(ret, 0775);
296 if(r != 0 && errno != EEXIST)
297 error("Cannot create directory '%s'", ret);
303 // ----------------------------------------------------------------------------
306 void rrdset_reset(RRDSET *st)
308 debug(D_RRD_CALLS, "rrdset_reset() %s", st->name);
310 st->last_collected_time.tv_sec = 0;
311 st->last_collected_time.tv_usec = 0;
312 st->last_updated.tv_sec = 0;
313 st->last_updated.tv_usec = 0;
314 st->current_entry = 0;
316 st->counter_done = 0;
319 for(rd = st->dimensions; rd ; rd = rd->next) {
320 rd->last_collected_time.tv_sec = 0;
321 rd->last_collected_time.tv_usec = 0;
323 bzero(rd->values, rd->entries * sizeof(storage_number));
327 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)
329 if(!type || !type[0]) {
330 fatal("Cannot create rrd stats without a type.");
335 fatal("Cannot create rrd stats without an id.");
339 char fullid[RRD_ID_LENGTH_MAX + 1];
340 char fullfilename[FILENAME_MAX + 1];
343 snprintfz(fullid, RRD_ID_LENGTH_MAX, "%s.%s", type, id);
345 st = rrdset_find(fullid);
347 error("Cannot create rrd stats for '%s', it already exists.", fullid);
351 long entries = config_get_number(fullid, "history", rrd_default_history_entries);
352 if(entries < 5) entries = config_set_number(fullid, "history", 5);
353 if(entries > RRD_HISTORY_ENTRIES_MAX) entries = config_set_number(fullid, "history", RRD_HISTORY_ENTRIES_MAX);
355 int enabled = config_get_boolean(fullid, "enabled", 1);
356 if(!enabled) entries = 5;
358 unsigned long size = sizeof(RRDSET);
359 char *cache_dir = rrdset_cache_dir(fullid);
361 debug(D_RRD_CALLS, "Creating RRD_STATS for '%s.%s'.", type, id);
363 snprintfz(fullfilename, FILENAME_MAX, "%s/main.db", cache_dir);
364 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);
366 if(strcmp(st->magic, RRDSET_MAGIC) != 0) {
368 info("Initializing file %s.", fullfilename);
371 else if(strcmp(st->id, fullid) != 0) {
373 error("File %s contents are not for chart %s. Clearing it.", fullfilename, fullid);
378 else if(st->memsize != size || st->entries != entries) {
380 error("File %s does not have the desired size. Clearing it.", fullfilename);
383 else if(st->update_every != update_every) {
385 error("File %s does not have the desired update frequency. Clearing it.", fullfilename);
388 else if((time(NULL) - st->last_updated.tv_sec) > update_every * entries) {
390 error("File %s is too old. Clearing it.", fullfilename);
402 st->dimensions = NULL;
404 st->mapped = rrd_memory_mode;
407 st = calloc(1, size);
409 fatal("Cannot allocate memory for RRD_STATS %s.%s", type, id);
412 st->mapped = RRD_MEMORY_MODE_RAM;
415 st->entries = entries;
416 st->update_every = update_every;
418 strcpy(st->cache_filename, fullfilename);
419 strcpy(st->magic, RRDSET_MAGIC);
421 strcpy(st->id, fullid);
422 st->hash = simple_hash(st->id);
424 st->cache_dir = cache_dir;
426 st->chart_type = rrdset_type_id(config_get(st->id, "chart type", rrdset_type_name(chart_type)));
427 st->type = config_get(st->id, "type", type);
428 st->family = config_get(st->id, "family", family?family:st->type);
429 st->context = config_get(st->id, "context", context?context:st->id);
430 st->units = config_get(st->id, "units", units?units:"");
432 st->priority = config_get_number(st->id, "priority", priority);
433 st->enabled = enabled;
438 st->last_collected_time.tv_sec = 0;
439 st->last_collected_time.tv_usec = 0;
440 st->counter_done = 0;
442 st->gap_when_lost_iterations_above = (int) (
443 config_get_number(st->id, "gap when lost iterations above", RRD_DEFAULT_GAP_INTERPOLATIONS) + 2);
445 avl_init_lock(&st->dimensions_index, rrddim_compare);
447 pthread_rwlock_init(&st->rwlock, NULL);
448 pthread_rwlock_wrlock(&rrdset_root_rwlock);
450 if(name && *name) rrdset_set_name(st, name);
451 else rrdset_set_name(st, id);
454 char varvalue[CONFIG_MAX_VALUE + 1];
455 snprintfz(varvalue, CONFIG_MAX_VALUE, "%s (%s)", title?title:"", st->name);
456 st->title = config_get(st->id, "title", varvalue);
459 st->next = rrdset_root;
462 rrdset_index_add(st);
464 pthread_rwlock_unlock(&rrdset_root_rwlock);
469 RRDDIM *rrddim_add(RRDSET *st, const char *id, const char *name, long multiplier, long divisor, int algorithm)
471 char filename[FILENAME_MAX + 1];
472 char fullfilename[FILENAME_MAX + 1];
474 char varname[CONFIG_MAX_NAME + 1];
476 unsigned long size = sizeof(RRDDIM) + (st->entries * sizeof(storage_number));
478 debug(D_RRD_CALLS, "Adding dimension '%s/%s'.", st->id, id);
480 rrdset_strncpyz_name(filename, id, FILENAME_MAX);
481 snprintfz(fullfilename, FILENAME_MAX, "%s/%s.db", st->cache_dir, filename);
482 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);
485 gettimeofday(&now, NULL);
487 if(strcmp(rd->magic, RRDDIMENSION_MAGIC) != 0) {
489 info("Initializing file %s.", fullfilename);
492 else if(rd->memsize != size) {
494 error("File %s does not have the desired size. Clearing it.", fullfilename);
497 else if(rd->multiplier != multiplier) {
499 error("File %s does not have the same multiplier. Clearing it.", fullfilename);
502 else if(rd->divisor != divisor) {
504 error("File %s does not have the same divisor. Clearing it.", fullfilename);
507 else if(rd->algorithm != algorithm) {
509 error("File %s does not have the same algorithm. Clearing it.", fullfilename);
512 else if(rd->update_every != st->update_every) {
514 error("File %s does not have the same refresh frequency. Clearing it.", fullfilename);
517 else if(usecdiff(&now, &rd->last_collected_time) > (rd->entries * rd->update_every * 1000000ULL)) {
519 error("File %s is too old. Clearing it.", fullfilename);
522 else if(strcmp(rd->id, id) != 0) {
524 error("File %s contents are not for dimension %s. Clearing it.", fullfilename, id);
532 // we have a file mapped for rd
533 rd->mapped = rrd_memory_mode;
534 rd->flags = 0x00000000;
539 // if we didn't manage to get a mmap'd dimension, just create one
541 rd = calloc(1, size);
543 fatal("Cannot allocate RRD_DIMENSION %s/%s.", st->id, id);
547 rd->mapped = RRD_MEMORY_MODE_RAM;
551 strcpy(rd->magic, RRDDIMENSION_MAGIC);
552 strcpy(rd->cache_filename, fullfilename);
553 strncpyz(rd->id, id, RRD_ID_LENGTH_MAX);
554 rd->hash = simple_hash(rd->id);
556 snprintfz(varname, CONFIG_MAX_NAME, "dim %s name", rd->id);
557 rd->name = config_get(st->id, varname, (name && *name)?name:rd->id);
559 snprintfz(varname, CONFIG_MAX_NAME, "dim %s algorithm", rd->id);
560 rd->algorithm = rrddim_algorithm_id(config_get(st->id, varname, rrddim_algorithm_name(algorithm)));
562 snprintfz(varname, CONFIG_MAX_NAME, "dim %s multiplier", rd->id);
563 rd->multiplier = config_get_number(st->id, varname, multiplier);
565 snprintfz(varname, CONFIG_MAX_NAME, "dim %s divisor", rd->id);
566 rd->divisor = config_get_number(st->id, varname, divisor);
567 if(!rd->divisor) rd->divisor = 1;
569 rd->entries = st->entries;
570 rd->update_every = st->update_every;
572 // prevent incremental calculation spikes
575 // append this dimension
576 pthread_rwlock_wrlock(&st->rwlock);
580 RRDDIM *td = st->dimensions;
581 for(; td->next; td = td->next) ;
584 pthread_rwlock_unlock(&st->rwlock);
586 rrddim_index_add(st, rd);
591 void rrddim_set_name(RRDSET *st, RRDDIM *rd, const char *name)
593 debug(D_RRD_CALLS, "rrddim_set_name() %s.%s", st->name, rd->name);
595 char varname[CONFIG_MAX_NAME + 1];
596 snprintfz(varname, CONFIG_MAX_NAME, "dim %s name", rd->id);
597 config_set_default(st->id, varname, name);
600 void rrddim_free(RRDSET *st, RRDDIM *rd)
602 debug(D_RRD_CALLS, "rrddim_free() %s.%s", st->name, rd->name);
604 RRDDIM *i, *last = NULL;
605 for(i = st->dimensions; i && i != rd ; i = i->next) last = i;
608 error("Request to free dimension %s.%s but it is not linked.", st->id, rd->name);
612 if(last) last->next = rd->next;
613 else st->dimensions = rd->next;
616 rrddim_index_del(st, rd);
618 // free(rd->annotations);
619 if(rd->mapped == RRD_MEMORY_MODE_SAVE) {
620 debug(D_RRD_CALLS, "Saving dimension '%s' to '%s'.", rd->name, rd->cache_filename);
621 savememory(rd->cache_filename, rd, rd->memsize);
623 debug(D_RRD_CALLS, "Unmapping dimension '%s'.", rd->name);
624 munmap(rd, rd->memsize);
626 else if(rd->mapped == RRD_MEMORY_MODE_MAP) {
627 debug(D_RRD_CALLS, "Unmapping dimension '%s'.", rd->name);
628 munmap(rd, rd->memsize);
631 debug(D_RRD_CALLS, "Removing dimension '%s'.", rd->name);
636 void rrdset_free_all(void)
638 info("Freeing all memory...");
641 for(st = rrdset_root; st ;) {
642 RRDSET *next = st->next;
644 while(st->dimensions)
645 rrddim_free(st, st->dimensions);
647 rrdset_index_del(st);
649 if(st->mapped == RRD_MEMORY_MODE_SAVE) {
650 debug(D_RRD_CALLS, "Saving stats '%s' to '%s'.", st->name, st->cache_filename);
651 savememory(st->cache_filename, st, st->memsize);
653 debug(D_RRD_CALLS, "Unmapping stats '%s'.", st->name);
654 munmap(st, st->memsize);
656 else if(st->mapped == RRD_MEMORY_MODE_MAP) {
657 debug(D_RRD_CALLS, "Unmapping stats '%s'.", st->name);
658 munmap(st, st->memsize);
667 info("Memory cleanup completed...");
670 void rrdset_save_all(void) {
671 info("Saving database...");
676 pthread_rwlock_wrlock(&rrdset_root_rwlock);
677 for(st = rrdset_root; st ; st = st->next) {
678 pthread_rwlock_wrlock(&st->rwlock);
680 if(st->mapped == RRD_MEMORY_MODE_SAVE) {
681 debug(D_RRD_CALLS, "Saving stats '%s' to '%s'.", st->name, st->cache_filename);
682 savememory(st->cache_filename, st, st->memsize);
685 for(rd = st->dimensions; rd ; rd = rd->next) {
686 if(likely(rd->mapped == RRD_MEMORY_MODE_SAVE)) {
687 debug(D_RRD_CALLS, "Saving dimension '%s' to '%s'.", rd->name, rd->cache_filename);
688 savememory(rd->cache_filename, rd, rd->memsize);
692 pthread_rwlock_unlock(&st->rwlock);
694 pthread_rwlock_unlock(&rrdset_root_rwlock);
698 RRDSET *rrdset_find(const char *id)
700 debug(D_RRD_CALLS, "rrdset_find() for chart %s", id);
702 RRDSET *st = rrdset_index_find(id, 0);
706 RRDSET *rrdset_find_bytype(const char *type, const char *id)
708 debug(D_RRD_CALLS, "rrdset_find_bytype() for chart %s.%s", type, id);
710 char buf[RRD_ID_LENGTH_MAX + 1];
712 strncpyz(buf, type, RRD_ID_LENGTH_MAX - 1);
714 int len = (int) strlen(buf);
715 strncpyz(&buf[len], id, (size_t) (RRD_ID_LENGTH_MAX - len));
717 return(rrdset_find(buf));
720 RRDSET *rrdset_find_byname(const char *name)
722 debug(D_RRD_CALLS, "rrdset_find_byname() for chart %s", name);
724 RRDSET *st = rrdset_index_find_name(name, 0);
728 RRDDIM *rrddim_find(RRDSET *st, const char *id)
730 debug(D_RRD_CALLS, "rrddim_find() for chart %s, dimension %s", st->name, id);
732 return rrddim_index_find(st, id, 0);
735 int rrddim_hide(RRDSET *st, const char *id)
737 debug(D_RRD_CALLS, "rrddim_hide() for chart %s, dimension %s", st->name, id);
739 RRDDIM *rd = rrddim_find(st, id);
741 error("Cannot find dimension with id '%s' on stats '%s' (%s).", id, st->name, st->id);
745 rd->flags |= RRDDIM_FLAG_HIDDEN;
749 int rrddim_unhide(RRDSET *st, const char *id)
751 debug(D_RRD_CALLS, "rrddim_unhide() for chart %s, dimension %s", st->name, id);
753 RRDDIM *rd = rrddim_find(st, id);
755 error("Cannot find dimension with id '%s' on stats '%s' (%s).", id, st->name, st->id);
759 if(rd->flags & RRDDIM_FLAG_HIDDEN) rd->flags ^= RRDDIM_FLAG_HIDDEN;
763 collected_number rrddim_set_by_pointer(RRDSET *st, RRDDIM *rd, collected_number value)
765 debug(D_RRD_CALLS, "rrddim_set_by_pointer() for chart %s, dimension %s, value " COLLECTED_NUMBER_FORMAT, st->name, rd->name, value);
767 gettimeofday(&rd->last_collected_time, NULL);
768 rd->collected_value = value;
772 return rd->last_collected_value;
775 collected_number rrddim_set(RRDSET *st, const char *id, collected_number value)
777 RRDDIM *rd = rrddim_find(st, id);
779 error("Cannot find dimension with id '%s' on stats '%s' (%s).", id, st->name, st->id);
783 return rrddim_set_by_pointer(st, rd, value);
786 void rrdset_next_usec(RRDSET *st, unsigned long long microseconds)
788 if(!microseconds) rrdset_next(st);
790 debug(D_RRD_CALLS, "rrdset_next_usec() for chart %s with microseconds %llu", st->name, microseconds);
792 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s: NEXT: %llu microseconds", st->name, microseconds);
793 st->usec_since_last_update = microseconds;
797 void rrdset_next(RRDSET *st)
799 unsigned long long microseconds = 0;
801 if(likely(st->last_collected_time.tv_sec)) {
803 gettimeofday(&now, NULL);
804 microseconds = usecdiff(&now, &st->last_collected_time);
806 // prevent infinite loop
807 else microseconds = st->update_every * 1000000ULL;
809 rrdset_next_usec(st, microseconds);
812 void rrdset_next_plugins(RRDSET *st)
817 unsigned long long rrdset_done(RRDSET *st)
819 if(unlikely(netdata_exit)) return 0;
821 debug(D_RRD_CALLS, "rrdset_done() for chart %s", st->name);
824 int oldstate, store_this_entry = 1, first_entry = 0;
825 unsigned long long last_ut, now_ut, next_ut, stored_entries = 0;
827 if(unlikely(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate) != 0))
828 error("Cannot set pthread cancel state to DISABLE.");
830 // a read lock is OK here
831 pthread_rwlock_rdlock(&st->rwlock);
833 // enable the chart, if it was disabled
834 if(unlikely(rrd_delete_unupdated_dimensions) && !st->enabled)
837 // check if the chart has a long time to be updated
838 if(unlikely(st->usec_since_last_update > st->entries * st->update_every * 1000000ULL)) {
839 info("%s: took too long to be updated (%0.3Lf secs). Reseting it.", st->name, (long double)(st->usec_since_last_update / 1000000.0));
841 st->usec_since_last_update = st->update_every * 1000000ULL;
844 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s: microseconds since last update: %llu", st->name, st->usec_since_last_update);
846 // set last_collected_time
847 if(unlikely(!st->last_collected_time.tv_sec)) {
848 // it is the first entry
849 // set the last_collected_time to now
850 gettimeofday(&st->last_collected_time, NULL);
852 // the first entry should not be stored
853 store_this_entry = 0;
856 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);
859 // it is not the first entry
860 // calculate the proper last_collected_time, using usec_since_last_update
861 unsigned long long ut = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec + st->usec_since_last_update;
862 st->last_collected_time.tv_sec = (time_t) (ut / 1000000ULL);
863 st->last_collected_time.tv_usec = (suseconds_t) (ut % 1000000ULL);
866 // if this set has not been updated in the past
867 // we fake the last_update time to be = now - usec_since_last_update
868 if(unlikely(!st->last_updated.tv_sec)) {
869 // it has never been updated before
870 // set a fake last_updated, in the past using usec_since_last_update
871 unsigned long long ut = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec - st->usec_since_last_update;
872 st->last_updated.tv_sec = (time_t) (ut / 1000000ULL);
873 st->last_updated.tv_usec = (suseconds_t) (ut % 1000000ULL);
875 // the first entry should not be stored
876 store_this_entry = 0;
879 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);
882 // check if we will re-write the entire data set
883 if(unlikely(usecdiff(&st->last_collected_time, &st->last_updated) > st->update_every * st->entries * 1000000ULL)) {
884 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);
887 st->usec_since_last_update = st->update_every * 1000000ULL;
889 gettimeofday(&st->last_collected_time, NULL);
891 unsigned long long ut = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec - st->usec_since_last_update;
892 st->last_updated.tv_sec = (time_t) (ut / 1000000ULL);
893 st->last_updated.tv_usec = (suseconds_t) (ut % 1000000ULL);
895 // the first entry should not be stored
896 store_this_entry = 0;
900 // these are the 3 variables that will help us in interpolation
901 // last_ut = the last time we added a value to the storage
902 // now_ut = the time the current value is taken at
903 // next_ut = the time of the next interpolation point
904 last_ut = st->last_updated.tv_sec * 1000000ULL + st->last_updated.tv_usec;
905 now_ut = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec;
906 next_ut = (st->last_updated.tv_sec + st->update_every) * 1000000ULL;
908 if(unlikely(!first_entry && now_ut < next_ut)) {
909 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s: THIS IS IN THE SAME INTERPOLATION POINT", st->name);
912 if(unlikely(st->debug)) {
913 debug(D_RRD_STATS, "%s: last ut = %0.3Lf (last updated time)", st->name, (long double)last_ut/1000000.0);
914 debug(D_RRD_STATS, "%s: now ut = %0.3Lf (current update time)", st->name, (long double)now_ut/1000000.0);
915 debug(D_RRD_STATS, "%s: next ut = %0.3Lf (next interpolation point)", st->name, (long double)next_ut/1000000.0);
918 if(unlikely(!st->counter_done)) {
919 store_this_entry = 0;
920 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s: Will not store the next entry.", st->name);
924 // calculate totals and count the dimensions
926 st->collected_total = 0;
927 for( rd = st->dimensions, dimensions = 0 ; likely(rd) ; rd = rd->next, dimensions++ )
928 if(likely(rd->updated)) st->collected_total += rd->collected_value;
930 uint32_t storage_flags = SN_EXISTS;
932 // process all dimensions to calculate their values
933 // based on the collected figures only
934 // at this stage we do not interpolate anything
935 for( rd = st->dimensions ; likely(rd) ; rd = rd->next ) {
937 if(unlikely(!rd->updated)) {
938 rd->calculated_value = 0;
942 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s/%s: START "
943 " last_collected_value = " COLLECTED_NUMBER_FORMAT
944 " collected_value = " COLLECTED_NUMBER_FORMAT
945 " last_calculated_value = " CALCULATED_NUMBER_FORMAT
946 " calculated_value = " CALCULATED_NUMBER_FORMAT
948 , rd->last_collected_value
949 , rd->collected_value
950 , rd->last_calculated_value
951 , rd->calculated_value
954 switch(rd->algorithm) {
955 case RRDDIM_ABSOLUTE:
956 rd->calculated_value = (calculated_number)rd->collected_value
957 * (calculated_number)rd->multiplier
958 / (calculated_number)rd->divisor;
960 if(unlikely(st->debug))
961 debug(D_RRD_STATS, "%s/%s: CALC ABS/ABS-NO-IN "
962 CALCULATED_NUMBER_FORMAT " = "
963 COLLECTED_NUMBER_FORMAT
964 " * " CALCULATED_NUMBER_FORMAT
965 " / " CALCULATED_NUMBER_FORMAT
967 , rd->calculated_value
968 , rd->collected_value
969 , (calculated_number)rd->multiplier
970 , (calculated_number)rd->divisor
974 case RRDDIM_PCENT_OVER_ROW_TOTAL:
975 if(unlikely(!st->collected_total))
976 rd->calculated_value = 0;
978 // the percentage of the current value
979 // over the total of all dimensions
980 rd->calculated_value =
981 (calculated_number)100
982 * (calculated_number)rd->collected_value
983 / (calculated_number)st->collected_total;
985 if(unlikely(st->debug))
986 debug(D_RRD_STATS, "%s/%s: CALC PCENT-ROW "
987 CALCULATED_NUMBER_FORMAT " = 100"
988 " * " COLLECTED_NUMBER_FORMAT
989 " / " COLLECTED_NUMBER_FORMAT
991 , rd->calculated_value
992 , rd->collected_value
993 , st->collected_total
997 case RRDDIM_INCREMENTAL:
998 if(unlikely(rd->counter <= 1)) {
999 rd->calculated_value = 0;
1003 // if the new is smaller than the old (an overflow, or reset), set the old equal to the new
1004 // to reset the calculation (it will give zero as the calculation for this second)
1005 if(unlikely(rd->last_collected_value > rd->collected_value)) {
1006 debug(D_RRD_STATS, "%s.%s: RESET or OVERFLOW. Last collected value = " COLLECTED_NUMBER_FORMAT ", current = " COLLECTED_NUMBER_FORMAT
1007 , st->name, rd->name
1008 , rd->last_collected_value
1009 , rd->collected_value);
1010 if(!(rd->flags & RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS)) storage_flags = SN_EXISTS_RESET;
1011 rd->last_collected_value = rd->collected_value;
1014 rd->calculated_value =
1015 (calculated_number)(rd->collected_value - rd->last_collected_value)
1016 * (calculated_number)rd->multiplier
1017 / (calculated_number)rd->divisor;
1019 if(unlikely(st->debug))
1020 debug(D_RRD_STATS, "%s/%s: CALC INC PRE "
1021 CALCULATED_NUMBER_FORMAT " = ("
1022 COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT
1024 " * " CALCULATED_NUMBER_FORMAT
1025 " / " CALCULATED_NUMBER_FORMAT
1027 , rd->calculated_value
1028 , rd->collected_value, rd->last_collected_value
1029 , (calculated_number)rd->multiplier
1030 , (calculated_number)rd->divisor
1034 case RRDDIM_PCENT_OVER_DIFF_TOTAL:
1035 if(unlikely(rd->counter <= 1)) {
1036 rd->calculated_value = 0;
1040 // the percentage of the current increment
1041 // over the increment of all dimensions together
1042 if(unlikely(st->collected_total == st->last_collected_total))
1043 rd->calculated_value = 0;
1045 rd->calculated_value =
1046 (calculated_number)100
1047 * (calculated_number)(rd->collected_value - rd->last_collected_value)
1048 / (calculated_number)(st->collected_total - st->last_collected_total);
1050 if(unlikely(st->debug))
1051 debug(D_RRD_STATS, "%s/%s: CALC PCENT-DIFF "
1052 CALCULATED_NUMBER_FORMAT " = 100"
1053 " * (" COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT ")"
1054 " / (" COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT ")"
1056 , rd->calculated_value
1057 , rd->collected_value, rd->last_collected_value
1058 , st->collected_total, st->last_collected_total
1063 // make the default zero, to make sure
1064 // it gets noticed when we add new types
1065 rd->calculated_value = 0;
1067 if(unlikely(st->debug))
1068 debug(D_RRD_STATS, "%s/%s: CALC "
1069 CALCULATED_NUMBER_FORMAT " = 0"
1071 , rd->calculated_value
1076 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s/%s: PHASE2 "
1077 " last_collected_value = " COLLECTED_NUMBER_FORMAT
1078 " collected_value = " COLLECTED_NUMBER_FORMAT
1079 " last_calculated_value = " CALCULATED_NUMBER_FORMAT
1080 " calculated_value = " CALCULATED_NUMBER_FORMAT
1082 , rd->last_collected_value
1083 , rd->collected_value
1084 , rd->last_calculated_value
1085 , rd->calculated_value
1090 // at this point we have all the calculated values ready
1091 // it is now time to interpolate values on a second boundary
1093 unsigned long long first_ut = last_ut;
1094 long long iterations = (now_ut - last_ut) / (st->update_every * 1000000ULL);
1095 if((now_ut % (st->update_every * 1000000ULL)) == 0) iterations++;
1097 for( ; likely(next_ut <= now_ut) ; next_ut += st->update_every * 1000000ULL, iterations-- ) {
1098 #ifdef NETDATA_INTERNAL_CHECKS
1099 if(iterations < 0) { error("%s: iterations calculation wrapped! first_ut = %llu, last_ut = %llu, next_ut = %llu, now_ut = %llu", st->name, first_ut, last_ut, next_ut, now_ut); }
1102 if(unlikely(st->debug)) {
1103 debug(D_RRD_STATS, "%s: last ut = %0.3Lf (last updated time)", st->name, (long double)last_ut/1000000.0);
1104 debug(D_RRD_STATS, "%s: next ut = %0.3Lf (next interpolation point)", st->name, (long double)next_ut/1000000.0);
1107 st->last_updated.tv_sec = (time_t) (next_ut / 1000000ULL);
1108 st->last_updated.tv_usec = 0;
1110 for( rd = st->dimensions ; likely(rd) ; rd = rd->next ) {
1111 calculated_number new_value;
1113 switch(rd->algorithm) {
1114 case RRDDIM_INCREMENTAL:
1115 new_value = (calculated_number)
1116 ( rd->calculated_value
1117 * (calculated_number)(next_ut - last_ut)
1118 / (calculated_number)(now_ut - last_ut)
1121 if(unlikely(st->debug))
1122 debug(D_RRD_STATS, "%s/%s: CALC2 INC "
1123 CALCULATED_NUMBER_FORMAT " = "
1124 CALCULATED_NUMBER_FORMAT
1129 , rd->calculated_value
1130 , (next_ut - last_ut)
1131 , (now_ut - last_ut)
1134 rd->calculated_value -= new_value;
1135 new_value += rd->last_calculated_value;
1136 rd->last_calculated_value = 0;
1137 new_value /= (calculated_number)st->update_every;
1140 case RRDDIM_ABSOLUTE:
1141 case RRDDIM_PCENT_OVER_ROW_TOTAL:
1142 case RRDDIM_PCENT_OVER_DIFF_TOTAL:
1144 if(iterations == 1) {
1145 // this is the last iteration
1146 // do not interpolate
1147 // just show the calculated value
1149 new_value = rd->calculated_value;
1152 // we have missed an update
1153 // interpolate in the middle values
1155 new_value = (calculated_number)
1156 ( ( (rd->calculated_value - rd->last_calculated_value)
1157 * (calculated_number)(next_ut - first_ut)
1158 / (calculated_number)(now_ut - first_ut)
1160 + rd->last_calculated_value
1163 if(unlikely(st->debug))
1164 debug(D_RRD_STATS, "%s/%s: CALC2 DEF "
1165 CALCULATED_NUMBER_FORMAT " = ((("
1166 "(" CALCULATED_NUMBER_FORMAT " - " CALCULATED_NUMBER_FORMAT ")"
1168 " / %llu) + " CALCULATED_NUMBER_FORMAT
1171 , rd->calculated_value, rd->last_calculated_value
1172 , (next_ut - first_ut)
1173 , (now_ut - first_ut), rd->last_calculated_value
1177 // it fades the value towards the target
1178 // while we know the calculated value is different
1179 // if(likely(next_ut + st->update_every * 1000000ULL > now_ut)) rd->calculated_value = new_value;
1184 if(unlikely(!store_this_entry)) {
1185 store_this_entry = 1;
1189 if(likely(rd->updated && rd->counter > 1 && iterations < st->gap_when_lost_iterations_above)) {
1190 rd->values[st->current_entry] = pack_storage_number(new_value, storage_flags );
1192 if(unlikely(st->debug))
1193 debug(D_RRD_STATS, "%s/%s: STORE[%ld] "
1194 CALCULATED_NUMBER_FORMAT " = " CALCULATED_NUMBER_FORMAT
1197 , unpack_storage_number(rd->values[st->current_entry]), new_value
1201 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s/%s: STORE[%ld] = NON EXISTING "
1205 rd->values[st->current_entry] = pack_storage_number(0, SN_NOT_EXISTS);
1210 if(unlikely(st->debug)) {
1211 calculated_number t1 = new_value * (calculated_number)rd->multiplier / (calculated_number)rd->divisor;
1212 calculated_number t2 = unpack_storage_number(rd->values[st->current_entry]);
1213 calculated_number accuracy = accuracy_loss(t1, t2);
1214 debug(D_RRD_STATS, "%s/%s: UNPACK[%ld] = " CALCULATED_NUMBER_FORMAT " FLAGS=0x%08x (original = " CALCULATED_NUMBER_FORMAT ", accuracy loss = " CALCULATED_NUMBER_FORMAT "%%%s)"
1218 , get_storage_number_flags(rd->values[st->current_entry])
1221 , (accuracy > ACCURACY_LOSS) ? " **TOO BIG** " : ""
1224 rd->collected_volume += t1;
1225 rd->stored_volume += t2;
1226 accuracy = accuracy_loss(rd->collected_volume, rd->stored_volume);
1227 debug(D_RRD_STATS, "%s/%s: VOLUME[%ld] = " CALCULATED_NUMBER_FORMAT ", calculated = " CALCULATED_NUMBER_FORMAT ", accuracy loss = " CALCULATED_NUMBER_FORMAT "%%%s"
1231 , rd->collected_volume
1233 , (accuracy > ACCURACY_LOSS) ? " **TOO BIG** " : ""
1238 // reset the storage flags for the next point, if any;
1239 storage_flags = SN_EXISTS;
1242 st->current_entry = ((st->current_entry + 1) >= st->entries) ? 0 : st->current_entry + 1;
1246 // align next interpolation to last collection point
1247 if(likely(stored_entries || !store_this_entry)) {
1248 st->last_updated.tv_sec = st->last_collected_time.tv_sec;
1249 st->last_updated.tv_usec = st->last_collected_time.tv_usec;
1250 st->last_collected_total = st->collected_total;
1253 for( rd = st->dimensions; likely(rd) ; rd = rd->next ) {
1254 if(unlikely(!rd->updated)) continue;
1256 if(likely(stored_entries || !store_this_entry)) {
1257 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);
1258 rd->last_collected_value = rd->collected_value;
1260 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);
1261 rd->last_calculated_value = rd->calculated_value;
1264 rd->calculated_value = 0;
1265 rd->collected_value = 0;
1268 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s/%s: END "
1269 " last_collected_value = " COLLECTED_NUMBER_FORMAT
1270 " collected_value = " COLLECTED_NUMBER_FORMAT
1271 " last_calculated_value = " CALCULATED_NUMBER_FORMAT
1272 " calculated_value = " CALCULATED_NUMBER_FORMAT
1274 , rd->last_collected_value
1275 , rd->collected_value
1276 , rd->last_calculated_value
1277 , rd->calculated_value
1281 // ALL DONE ABOUT THE DATA UPDATE
1282 // --------------------------------------------------------------------
1284 // find if there are any obsolete dimensions (not updated recently)
1285 if(unlikely(rrd_delete_unupdated_dimensions)) {
1287 for( rd = st->dimensions; likely(rd) ; rd = rd->next )
1288 if((rd->last_collected_time.tv_sec + (rrd_delete_unupdated_dimensions * st->update_every)) < st->last_collected_time.tv_sec)
1292 // there is dimension to free
1293 // upgrade our read lock to a write lock
1294 pthread_rwlock_unlock(&st->rwlock);
1295 pthread_rwlock_wrlock(&st->rwlock);
1297 for( rd = st->dimensions, last = NULL ; likely(rd) ; ) {
1298 // remove it only it is not updated in rrd_delete_unupdated_dimensions seconds
1300 if(unlikely((rd->last_collected_time.tv_sec + (rrd_delete_unupdated_dimensions * st->update_every)) < st->last_collected_time.tv_sec)) {
1301 info("Removing obsolete dimension '%s' (%s) of '%s' (%s).", rd->name, rd->id, st->name, st->id);
1303 if(unlikely(!last)) {
1304 st->dimensions = rd->next;
1306 rrddim_free(st, rd);
1307 rd = st->dimensions;
1311 last->next = rd->next;
1313 rrddim_free(st, rd);
1323 if(unlikely(!st->dimensions)) {
1324 info("Disabling chart %s (%s) since it does not have any dimensions", st->name, st->id);
1330 pthread_rwlock_unlock(&st->rwlock);
1332 if(unlikely(pthread_setcancelstate(oldstate, NULL) != 0))
1333 error("Cannot set pthread cancel state to RESTORE (%d).", oldstate);
1335 return(st->usec_since_last_update);