14 #include <sys/types.h>
19 #include "appconfig.h"
23 #define RRD_DEFAULT_GAP_INTERPOLATIONS 1
25 // ----------------------------------------------------------------------------
28 // if not zero it gives the time (in seconds) to remove un-updated dimensions
30 // if dimensions are removed, the chart generation will have to run again
31 int rrd_delete_unupdated_dimensions = 0;
33 int rrd_update_every = UPDATE_EVERY;
34 int rrd_default_history_entries = RRD_DEFAULT_HISTORY_ENTRIES;
36 RRDSET *rrdset_root = NULL;
37 pthread_rwlock_t rrdset_root_rwlock = PTHREAD_RWLOCK_INITIALIZER;
39 int rrd_memory_mode = RRD_MEMORY_MODE_SAVE;
42 // ----------------------------------------------------------------------------
45 static int rrdset_compare(void* a, void* b) {
46 if(((RRDSET *)a)->hash < ((RRDSET *)b)->hash) return -1;
47 else if(((RRDSET *)a)->hash > ((RRDSET *)b)->hash) return 1;
48 else return strcmp(((RRDSET *)a)->id, ((RRDSET *)b)->id);
51 avl_tree_lock rrdset_root_index = {
52 { NULL, rrdset_compare },
56 #define rrdset_index_add(st) avl_insert_lock(&rrdset_root_index, (avl *)(st))
57 #define rrdset_index_del(st) avl_remove_lock(&rrdset_root_index, (avl *)(st))
59 static RRDSET *rrdset_index_find(const char *id, uint32_t hash) {
61 strncpyz(tmp.id, id, RRD_ID_LENGTH_MAX);
62 tmp.hash = (hash)?hash:simple_hash(tmp.id);
64 return (RRDSET *)avl_search_lock(&(rrdset_root_index), (avl *) &tmp);
67 // ----------------------------------------------------------------------------
70 #define rrdset_from_avlname(avlname_ptr) ((RRDSET *)((avlname_ptr) - offsetof(RRDSET, avlname)))
72 static int rrdset_compare_name(void* a, void* b) {
73 RRDSET *A = rrdset_from_avlname(a);
74 RRDSET *B = rrdset_from_avlname(b);
76 // fprintf(stderr, "COMPARING: %s with %s\n", A->name, B->name);
78 if(A->hash_name < B->hash_name) return -1;
79 else if(A->hash_name > B->hash_name) return 1;
80 else return strcmp(A->name, B->name);
83 avl_tree_lock rrdset_root_index_name = {
84 { NULL, rrdset_compare_name },
88 int rrdset_index_add_name(RRDSET *st) {
89 // fprintf(stderr, "ADDING: %s (name: %s)\n", st->id, st->name);
90 return avl_insert_lock(&rrdset_root_index_name, (avl *) (&st->avlname));
93 #define rrdset_index_del_name(st) avl_remove_lock(&rrdset_root_index_name, (avl *)(&st->avlname))
95 static RRDSET *rrdset_index_find_name(const char *name, uint32_t hash) {
99 tmp.hash_name = (hash)?hash:simple_hash(tmp.name);
101 // fprintf(stderr, "SEARCHING: %s\n", name);
102 result = avl_search_lock(&(rrdset_root_index_name), (avl *) (&(tmp.avlname)));
104 RRDSET *st = rrdset_from_avlname(result);
105 if(strcmp(st->magic, RRDSET_MAGIC))
106 error("Search for RRDSET %s returned an invalid RRDSET %s (name %s)", name, st->id, st->name);
108 // fprintf(stderr, "FOUND: %s\n", name);
109 return rrdset_from_avlname(result);
111 // fprintf(stderr, "NOT FOUND: %s\n", name);
116 // ----------------------------------------------------------------------------
119 static int rrddim_compare(void* a, void* b) {
120 if(((RRDDIM *)a)->hash < ((RRDDIM *)b)->hash) return -1;
121 else if(((RRDDIM *)a)->hash > ((RRDDIM *)b)->hash) return 1;
122 else return strcmp(((RRDDIM *)a)->id, ((RRDDIM *)b)->id);
125 #define rrddim_index_add(st, rd) avl_insert_lock(&((st)->dimensions_index), (avl *)(rd))
126 #define rrddim_index_del(st,rd ) avl_remove_lock(&((st)->dimensions_index), (avl *)(rd))
128 static RRDDIM *rrddim_index_find(RRDSET *st, const char *id, uint32_t hash) {
130 strncpyz(tmp.id, id, RRD_ID_LENGTH_MAX);
131 tmp.hash = (hash)?hash:simple_hash(tmp.id);
133 return (RRDDIM *)avl_search_lock(&(st->dimensions_index), (avl *) &tmp);
136 // ----------------------------------------------------------------------------
139 int rrdset_type_id(const char *name)
141 if(unlikely(strcmp(name, RRDSET_TYPE_AREA_NAME) == 0)) return RRDSET_TYPE_AREA;
142 else if(unlikely(strcmp(name, RRDSET_TYPE_STACKED_NAME) == 0)) return RRDSET_TYPE_STACKED;
143 else if(unlikely(strcmp(name, RRDSET_TYPE_LINE_NAME) == 0)) return RRDSET_TYPE_LINE;
144 return RRDSET_TYPE_LINE;
147 const char *rrdset_type_name(int chart_type)
149 static char line[] = RRDSET_TYPE_LINE_NAME;
150 static char area[] = RRDSET_TYPE_AREA_NAME;
151 static char stacked[] = RRDSET_TYPE_STACKED_NAME;
154 case RRDSET_TYPE_LINE:
157 case RRDSET_TYPE_AREA:
160 case RRDSET_TYPE_STACKED:
166 // ----------------------------------------------------------------------------
169 const char *rrd_memory_mode_name(int id)
171 static const char ram[] = RRD_MEMORY_MODE_RAM_NAME;
172 static const char map[] = RRD_MEMORY_MODE_MAP_NAME;
173 static const char save[] = RRD_MEMORY_MODE_SAVE_NAME;
176 case RRD_MEMORY_MODE_RAM:
179 case RRD_MEMORY_MODE_MAP:
182 case RRD_MEMORY_MODE_SAVE:
190 int rrd_memory_mode_id(const char *name)
192 if(unlikely(!strcmp(name, RRD_MEMORY_MODE_RAM_NAME)))
193 return RRD_MEMORY_MODE_RAM;
194 else if(unlikely(!strcmp(name, RRD_MEMORY_MODE_MAP_NAME)))
195 return RRD_MEMORY_MODE_MAP;
197 return RRD_MEMORY_MODE_SAVE;
200 // ----------------------------------------------------------------------------
203 int rrddim_algorithm_id(const char *name)
205 if(strcmp(name, RRDDIM_INCREMENTAL_NAME) == 0) return RRDDIM_INCREMENTAL;
206 if(strcmp(name, RRDDIM_ABSOLUTE_NAME) == 0) return RRDDIM_ABSOLUTE;
207 if(strcmp(name, RRDDIM_PCENT_OVER_ROW_TOTAL_NAME) == 0) return RRDDIM_PCENT_OVER_ROW_TOTAL;
208 if(strcmp(name, RRDDIM_PCENT_OVER_DIFF_TOTAL_NAME) == 0) return RRDDIM_PCENT_OVER_DIFF_TOTAL;
209 return RRDDIM_ABSOLUTE;
212 const char *rrddim_algorithm_name(int chart_type)
214 static char absolute[] = RRDDIM_ABSOLUTE_NAME;
215 static char incremental[] = RRDDIM_INCREMENTAL_NAME;
216 static char percentage_of_absolute_row[] = RRDDIM_PCENT_OVER_ROW_TOTAL_NAME;
217 static char percentage_of_incremental_row[] = RRDDIM_PCENT_OVER_DIFF_TOTAL_NAME;
220 case RRDDIM_ABSOLUTE:
223 case RRDDIM_INCREMENTAL:
226 case RRDDIM_PCENT_OVER_ROW_TOTAL:
227 return percentage_of_absolute_row;
229 case RRDDIM_PCENT_OVER_DIFF_TOTAL:
230 return percentage_of_incremental_row;
235 // ----------------------------------------------------------------------------
238 char *rrdset_strncpyz_name(char *to, const char *from, size_t length)
242 while (length-- && (c = *from++)) {
243 if(c != '.' && !isalnum(c))
254 void rrdset_set_name(RRDSET *st, const char *name)
256 debug(D_RRD_CALLS, "rrdset_set_name() old: %s, new: %s", st->name, name);
258 if(st->name) rrdset_index_del_name(st);
260 char b[CONFIG_MAX_VALUE + 1];
261 char n[RRD_ID_LENGTH_MAX + 1];
263 snprintfz(n, RRD_ID_LENGTH_MAX, "%s.%s", st->type, name);
264 rrdset_strncpyz_name(b, n, CONFIG_MAX_VALUE);
265 st->name = config_get(st->id, "name", b);
266 st->hash_name = simple_hash(st->name);
268 rrdset_index_add_name(st);
271 // ----------------------------------------------------------------------------
274 char *rrdset_cache_dir(const char *id)
278 static char *cache_dir = NULL;
279 if(!cache_dir) cache_dir = config_get("global", "cache directory", CACHE_DIR);
281 char b[FILENAME_MAX + 1];
282 char n[FILENAME_MAX + 1];
283 rrdset_strncpyz_name(b, id, FILENAME_MAX);
285 snprintfz(n, FILENAME_MAX, "%s/%s", cache_dir, b);
286 ret = config_get(id, "cache directory", n);
288 if(rrd_memory_mode == RRD_MEMORY_MODE_MAP || rrd_memory_mode == RRD_MEMORY_MODE_SAVE) {
289 int r = mkdir(ret, 0775);
290 if(r != 0 && errno != EEXIST)
291 error("Cannot create directory '%s'", ret);
297 // ----------------------------------------------------------------------------
300 void rrdset_reset(RRDSET *st)
302 debug(D_RRD_CALLS, "rrdset_reset() %s", st->name);
304 st->last_collected_time.tv_sec = 0;
305 st->last_collected_time.tv_usec = 0;
306 st->last_updated.tv_sec = 0;
307 st->last_updated.tv_usec = 0;
308 st->current_entry = 0;
310 st->counter_done = 0;
313 for(rd = st->dimensions; rd ; rd = rd->next) {
314 rd->last_collected_time.tv_sec = 0;
315 rd->last_collected_time.tv_usec = 0;
317 bzero(rd->values, rd->entries * sizeof(storage_number));
321 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)
323 if(!type || !type[0]) {
324 fatal("Cannot create rrd stats without a type.");
329 fatal("Cannot create rrd stats without an id.");
333 char fullid[RRD_ID_LENGTH_MAX + 1];
334 char fullfilename[FILENAME_MAX + 1];
337 snprintfz(fullid, RRD_ID_LENGTH_MAX, "%s.%s", type, id);
339 st = rrdset_find(fullid);
341 error("Cannot create rrd stats for '%s', it already exists.", fullid);
345 long entries = config_get_number(fullid, "history", rrd_default_history_entries);
346 if(entries < 5) entries = config_set_number(fullid, "history", 5);
347 if(entries > RRD_HISTORY_ENTRIES_MAX) entries = config_set_number(fullid, "history", RRD_HISTORY_ENTRIES_MAX);
349 int enabled = config_get_boolean(fullid, "enabled", 1);
350 if(!enabled) entries = 5;
352 unsigned long size = sizeof(RRDSET);
353 char *cache_dir = rrdset_cache_dir(fullid);
355 debug(D_RRD_CALLS, "Creating RRD_STATS for '%s.%s'.", type, id);
357 snprintfz(fullfilename, FILENAME_MAX, "%s/main.db", cache_dir);
358 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);
360 if(strcmp(st->magic, RRDSET_MAGIC) != 0) {
362 info("Initializing file %s.", fullfilename);
365 else if(strcmp(st->id, fullid) != 0) {
367 error("File %s contents are not for chart %s. Clearing it.", fullfilename, fullid);
372 else if(st->memsize != size || st->entries != entries) {
374 error("File %s does not have the desired size. Clearing it.", fullfilename);
377 else if(st->update_every != update_every) {
379 error("File %s does not have the desired update frequency. Clearing it.", fullfilename);
382 else if((time(NULL) - st->last_updated.tv_sec) > update_every * entries) {
384 error("File %s is too old. Clearing it.", fullfilename);
396 st->dimensions = NULL;
398 st->mapped = rrd_memory_mode;
401 st = calloc(1, size);
403 fatal("Cannot allocate memory for RRD_STATS %s.%s", type, id);
406 st->mapped = RRD_MEMORY_MODE_RAM;
409 st->entries = entries;
410 st->update_every = update_every;
412 strcpy(st->cache_filename, fullfilename);
413 strcpy(st->magic, RRDSET_MAGIC);
415 strcpy(st->id, fullid);
416 st->hash = simple_hash(st->id);
418 st->cache_dir = cache_dir;
420 st->chart_type = rrdset_type_id(config_get(st->id, "chart type", rrdset_type_name(chart_type)));
421 st->type = config_get(st->id, "type", type);
422 st->family = config_get(st->id, "family", family?family:st->type);
423 st->context = config_get(st->id, "context", context?context:st->id);
424 st->units = config_get(st->id, "units", units?units:"");
426 st->priority = config_get_number(st->id, "priority", priority);
427 st->enabled = enabled;
432 st->last_collected_time.tv_sec = 0;
433 st->last_collected_time.tv_usec = 0;
434 st->counter_done = 0;
436 st->gap_when_lost_iterations_above = (int) (
437 config_get_number(st->id, "gap when lost iterations above", RRD_DEFAULT_GAP_INTERPOLATIONS) + 2);
439 avl_init_lock(&st->dimensions_index, rrddim_compare);
441 pthread_rwlock_init(&st->rwlock, NULL);
442 pthread_rwlock_wrlock(&rrdset_root_rwlock);
444 if(name && *name) rrdset_set_name(st, name);
445 else rrdset_set_name(st, id);
448 char varvalue[CONFIG_MAX_VALUE + 1];
449 snprintfz(varvalue, CONFIG_MAX_VALUE, "%s (%s)", title?title:"", st->name);
450 st->title = config_get(st->id, "title", varvalue);
453 st->next = rrdset_root;
456 rrdset_index_add(st);
458 pthread_rwlock_unlock(&rrdset_root_rwlock);
463 RRDDIM *rrddim_add(RRDSET *st, const char *id, const char *name, long multiplier, long divisor, int algorithm)
465 char filename[FILENAME_MAX + 1];
466 char fullfilename[FILENAME_MAX + 1];
468 char varname[CONFIG_MAX_NAME + 1];
470 unsigned long size = sizeof(RRDDIM) + (st->entries * sizeof(storage_number));
472 debug(D_RRD_CALLS, "Adding dimension '%s/%s'.", st->id, id);
474 rrdset_strncpyz_name(filename, id, FILENAME_MAX);
475 snprintfz(fullfilename, FILENAME_MAX, "%s/%s.db", st->cache_dir, filename);
476 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);
479 gettimeofday(&now, NULL);
481 if(strcmp(rd->magic, RRDDIMENSION_MAGIC) != 0) {
483 info("Initializing file %s.", fullfilename);
486 else if(rd->memsize != size) {
488 error("File %s does not have the desired size. Clearing it.", fullfilename);
491 else if(rd->multiplier != multiplier) {
493 error("File %s does not have the same multiplier. Clearing it.", fullfilename);
496 else if(rd->divisor != divisor) {
498 error("File %s does not have the same divisor. Clearing it.", fullfilename);
501 else if(rd->algorithm != algorithm) {
503 error("File %s does not have the same algorithm. Clearing it.", fullfilename);
506 else if(rd->update_every != st->update_every) {
508 error("File %s does not have the same refresh frequency. Clearing it.", fullfilename);
511 else if(usecdiff(&now, &rd->last_collected_time) > (rd->entries * rd->update_every * 1000000ULL)) {
513 error("File %s is too old. Clearing it.", fullfilename);
516 else if(strcmp(rd->id, id) != 0) {
518 error("File %s contents are not for dimension %s. Clearing it.", fullfilename, id);
526 // we have a file mapped for rd
527 rd->mapped = rrd_memory_mode;
528 rd->flags = 0x00000000;
533 // if we didn't manage to get a mmap'd dimension, just create one
535 rd = calloc(1, size);
537 fatal("Cannot allocate RRD_DIMENSION %s/%s.", st->id, id);
541 rd->mapped = RRD_MEMORY_MODE_RAM;
545 strcpy(rd->magic, RRDDIMENSION_MAGIC);
546 strcpy(rd->cache_filename, fullfilename);
547 strncpyz(rd->id, id, RRD_ID_LENGTH_MAX);
548 rd->hash = simple_hash(rd->id);
550 snprintfz(varname, CONFIG_MAX_NAME, "dim %s name", rd->id);
551 rd->name = config_get(st->id, varname, (name && *name)?name:rd->id);
553 snprintfz(varname, CONFIG_MAX_NAME, "dim %s algorithm", rd->id);
554 rd->algorithm = rrddim_algorithm_id(config_get(st->id, varname, rrddim_algorithm_name(algorithm)));
556 snprintfz(varname, CONFIG_MAX_NAME, "dim %s multiplier", rd->id);
557 rd->multiplier = config_get_number(st->id, varname, multiplier);
559 snprintfz(varname, CONFIG_MAX_NAME, "dim %s divisor", rd->id);
560 rd->divisor = config_get_number(st->id, varname, divisor);
561 if(!rd->divisor) rd->divisor = 1;
563 rd->entries = st->entries;
564 rd->update_every = st->update_every;
566 // prevent incremental calculation spikes
569 // append this dimension
570 pthread_rwlock_wrlock(&st->rwlock);
574 RRDDIM *td = st->dimensions;
575 for(; td->next; td = td->next) ;
578 pthread_rwlock_unlock(&st->rwlock);
580 rrddim_index_add(st, rd);
585 void rrddim_set_name(RRDSET *st, RRDDIM *rd, const char *name)
587 debug(D_RRD_CALLS, "rrddim_set_name() %s.%s", st->name, rd->name);
589 char varname[CONFIG_MAX_NAME + 1];
590 snprintfz(varname, CONFIG_MAX_NAME, "dim %s name", rd->id);
591 config_set_default(st->id, varname, name);
594 void rrddim_free(RRDSET *st, RRDDIM *rd)
596 debug(D_RRD_CALLS, "rrddim_free() %s.%s", st->name, rd->name);
598 RRDDIM *i, *last = NULL;
599 for(i = st->dimensions; i && i != rd ; i = i->next) last = i;
602 error("Request to free dimension %s.%s but it is not linked.", st->id, rd->name);
606 if(last) last->next = rd->next;
607 else st->dimensions = rd->next;
610 rrddim_index_del(st, rd);
612 // free(rd->annotations);
613 if(rd->mapped == RRD_MEMORY_MODE_SAVE) {
614 debug(D_RRD_CALLS, "Saving dimension '%s' to '%s'.", rd->name, rd->cache_filename);
615 savememory(rd->cache_filename, rd, rd->memsize);
617 debug(D_RRD_CALLS, "Unmapping dimension '%s'.", rd->name);
618 munmap(rd, rd->memsize);
620 else if(rd->mapped == RRD_MEMORY_MODE_MAP) {
621 debug(D_RRD_CALLS, "Unmapping dimension '%s'.", rd->name);
622 munmap(rd, rd->memsize);
625 debug(D_RRD_CALLS, "Removing dimension '%s'.", rd->name);
630 void rrdset_free_all(void)
632 info("Freeing all memory...");
635 for(st = rrdset_root; st ;) {
636 RRDSET *next = st->next;
638 while(st->dimensions)
639 rrddim_free(st, st->dimensions);
641 rrdset_index_del(st);
643 if(st->mapped == RRD_MEMORY_MODE_SAVE) {
644 debug(D_RRD_CALLS, "Saving stats '%s' to '%s'.", st->name, st->cache_filename);
645 savememory(st->cache_filename, st, st->memsize);
647 debug(D_RRD_CALLS, "Unmapping stats '%s'.", st->name);
648 munmap(st, st->memsize);
650 else if(st->mapped == RRD_MEMORY_MODE_MAP) {
651 debug(D_RRD_CALLS, "Unmapping stats '%s'.", st->name);
652 munmap(st, st->memsize);
661 info("Memory cleanup completed...");
664 void rrdset_save_all(void) {
665 info("Saving database...");
670 pthread_rwlock_wrlock(&rrdset_root_rwlock);
671 for(st = rrdset_root; st ; st = st->next) {
672 pthread_rwlock_wrlock(&st->rwlock);
674 if(st->mapped == RRD_MEMORY_MODE_SAVE) {
675 debug(D_RRD_CALLS, "Saving stats '%s' to '%s'.", st->name, st->cache_filename);
676 savememory(st->cache_filename, st, st->memsize);
679 for(rd = st->dimensions; rd ; rd = rd->next) {
680 if(likely(rd->mapped == RRD_MEMORY_MODE_SAVE)) {
681 debug(D_RRD_CALLS, "Saving dimension '%s' to '%s'.", rd->name, rd->cache_filename);
682 savememory(rd->cache_filename, rd, rd->memsize);
686 pthread_rwlock_unlock(&st->rwlock);
688 pthread_rwlock_unlock(&rrdset_root_rwlock);
692 RRDSET *rrdset_find(const char *id)
694 debug(D_RRD_CALLS, "rrdset_find() for chart %s", id);
696 RRDSET *st = rrdset_index_find(id, 0);
700 RRDSET *rrdset_find_bytype(const char *type, const char *id)
702 debug(D_RRD_CALLS, "rrdset_find_bytype() for chart %s.%s", type, id);
704 char buf[RRD_ID_LENGTH_MAX + 1];
706 strncpyz(buf, type, RRD_ID_LENGTH_MAX - 1);
708 int len = (int) strlen(buf);
709 strncpyz(&buf[len], id, (size_t) (RRD_ID_LENGTH_MAX - len));
711 return(rrdset_find(buf));
714 RRDSET *rrdset_find_byname(const char *name)
716 debug(D_RRD_CALLS, "rrdset_find_byname() for chart %s", name);
718 RRDSET *st = rrdset_index_find_name(name, 0);
722 RRDDIM *rrddim_find(RRDSET *st, const char *id)
724 debug(D_RRD_CALLS, "rrddim_find() for chart %s, dimension %s", st->name, id);
726 return rrddim_index_find(st, id, 0);
729 int rrddim_hide(RRDSET *st, const char *id)
731 debug(D_RRD_CALLS, "rrddim_hide() for chart %s, dimension %s", st->name, id);
733 RRDDIM *rd = rrddim_find(st, id);
735 error("Cannot find dimension with id '%s' on stats '%s' (%s).", id, st->name, st->id);
739 rd->flags |= RRDDIM_FLAG_HIDDEN;
743 int rrddim_unhide(RRDSET *st, const char *id)
745 debug(D_RRD_CALLS, "rrddim_unhide() for chart %s, dimension %s", st->name, id);
747 RRDDIM *rd = rrddim_find(st, id);
749 error("Cannot find dimension with id '%s' on stats '%s' (%s).", id, st->name, st->id);
753 if(rd->flags & RRDDIM_FLAG_HIDDEN) rd->flags ^= RRDDIM_FLAG_HIDDEN;
757 collected_number rrddim_set_by_pointer(RRDSET *st, RRDDIM *rd, collected_number value)
759 debug(D_RRD_CALLS, "rrddim_set_by_pointer() for chart %s, dimension %s, value " COLLECTED_NUMBER_FORMAT, st->name, rd->name, value);
761 gettimeofday(&rd->last_collected_time, NULL);
762 rd->collected_value = value;
766 return rd->last_collected_value;
769 collected_number rrddim_set(RRDSET *st, const char *id, collected_number value)
771 RRDDIM *rd = rrddim_find(st, id);
773 error("Cannot find dimension with id '%s' on stats '%s' (%s).", id, st->name, st->id);
777 return rrddim_set_by_pointer(st, rd, value);
780 void rrdset_next_usec(RRDSET *st, unsigned long long microseconds)
782 if(!microseconds) rrdset_next(st);
784 debug(D_RRD_CALLS, "rrdset_next_usec() for chart %s with microseconds %llu", st->name, microseconds);
786 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s: NEXT: %llu microseconds", st->name, microseconds);
787 st->usec_since_last_update = microseconds;
791 void rrdset_next(RRDSET *st)
793 unsigned long long microseconds = 0;
795 if(likely(st->last_collected_time.tv_sec)) {
797 gettimeofday(&now, NULL);
798 microseconds = usecdiff(&now, &st->last_collected_time);
800 // prevent infinite loop
801 else microseconds = st->update_every * 1000000ULL;
803 rrdset_next_usec(st, microseconds);
806 void rrdset_next_plugins(RRDSET *st)
811 unsigned long long rrdset_done(RRDSET *st)
813 debug(D_RRD_CALLS, "rrdset_done() for chart %s", st->name);
816 int oldstate, store_this_entry = 1, first_entry = 0;
817 unsigned long long last_ut, now_ut, next_ut, stored_entries = 0;
819 if(unlikely(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate) != 0))
820 error("Cannot set pthread cancel state to DISABLE.");
822 // a read lock is OK here
823 pthread_rwlock_rdlock(&st->rwlock);
825 // enable the chart, if it was disabled
826 if(unlikely(rrd_delete_unupdated_dimensions) && !st->enabled)
829 // check if the chart has a long time to be updated
830 if(unlikely(st->usec_since_last_update > st->entries * st->update_every * 1000000ULL)) {
831 info("%s: took too long to be updated (%0.3Lf secs). Reseting it.", st->name, (long double)(st->usec_since_last_update / 1000000.0));
833 st->usec_since_last_update = st->update_every * 1000000ULL;
836 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s: microseconds since last update: %llu", st->name, st->usec_since_last_update);
838 // set last_collected_time
839 if(unlikely(!st->last_collected_time.tv_sec)) {
840 // it is the first entry
841 // set the last_collected_time to now
842 gettimeofday(&st->last_collected_time, NULL);
844 // the first entry should not be stored
845 store_this_entry = 0;
848 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);
851 // it is not the first entry
852 // calculate the proper last_collected_time, using usec_since_last_update
853 unsigned long long ut = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec + st->usec_since_last_update;
854 st->last_collected_time.tv_sec = (time_t) (ut / 1000000ULL);
855 st->last_collected_time.tv_usec = (useconds_t) (ut % 1000000ULL);
858 // if this set has not been updated in the past
859 // we fake the last_update time to be = now - usec_since_last_update
860 if(unlikely(!st->last_updated.tv_sec)) {
861 // it has never been updated before
862 // set a fake last_updated, in the past using usec_since_last_update
863 unsigned long long ut = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec - st->usec_since_last_update;
864 st->last_updated.tv_sec = (time_t) (ut / 1000000ULL);
865 st->last_updated.tv_usec = (useconds_t) (ut % 1000000ULL);
867 // the first entry should not be stored
868 store_this_entry = 0;
871 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);
874 // check if we will re-write the entire data set
875 if(unlikely(usecdiff(&st->last_collected_time, &st->last_updated) > st->update_every * st->entries * 1000000ULL)) {
876 info("%s: too old data (last updated at %u.%u, last collected at %u.%u). 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);
879 st->usec_since_last_update = st->update_every * 1000000ULL;
881 gettimeofday(&st->last_collected_time, NULL);
883 unsigned long long ut = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec - st->usec_since_last_update;
884 st->last_updated.tv_sec = (time_t) (ut / 1000000ULL);
885 st->last_updated.tv_usec = (useconds_t) (ut % 1000000ULL);
887 // the first entry should not be stored
888 store_this_entry = 0;
892 // these are the 3 variables that will help us in interpolation
893 // last_ut = the last time we added a value to the storage
894 // now_ut = the time the current value is taken at
895 // next_ut = the time of the next interpolation point
896 last_ut = st->last_updated.tv_sec * 1000000ULL + st->last_updated.tv_usec;
897 now_ut = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec;
898 next_ut = (st->last_updated.tv_sec + st->update_every) * 1000000ULL;
900 if(unlikely(!first_entry && now_ut < next_ut)) {
901 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s: THIS IS IN THE SAME INTERPOLATION POINT", st->name);
904 if(unlikely(st->debug)) {
905 debug(D_RRD_STATS, "%s: last ut = %0.3Lf (last updated time)", st->name, (long double)last_ut/1000000.0);
906 debug(D_RRD_STATS, "%s: now ut = %0.3Lf (current update time)", st->name, (long double)now_ut/1000000.0);
907 debug(D_RRD_STATS, "%s: next ut = %0.3Lf (next interpolation point)", st->name, (long double)next_ut/1000000.0);
910 if(unlikely(!st->counter_done)) {
911 store_this_entry = 0;
912 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s: Will not store the next entry.", st->name);
916 // calculate totals and count the dimensions
918 st->collected_total = 0;
919 for( rd = st->dimensions, dimensions = 0 ; likely(rd) ; rd = rd->next, dimensions++ )
920 st->collected_total += rd->collected_value;
922 uint32_t storage_flags = SN_EXISTS;
924 // process all dimensions to calculate their values
925 // based on the collected figures only
926 // at this stage we do not interpolate anything
927 for( rd = st->dimensions ; likely(rd) ; rd = rd->next ) {
929 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s/%s: START "
930 " last_collected_value = " COLLECTED_NUMBER_FORMAT
931 " collected_value = " COLLECTED_NUMBER_FORMAT
932 " last_calculated_value = " CALCULATED_NUMBER_FORMAT
933 " calculated_value = " CALCULATED_NUMBER_FORMAT
935 , rd->last_collected_value
936 , rd->collected_value
937 , rd->last_calculated_value
938 , rd->calculated_value
941 switch(rd->algorithm) {
942 case RRDDIM_ABSOLUTE:
943 rd->calculated_value = (calculated_number)rd->collected_value
944 * (calculated_number)rd->multiplier
945 / (calculated_number)rd->divisor;
947 if(unlikely(st->debug))
948 debug(D_RRD_STATS, "%s/%s: CALC ABS/ABS-NO-IN "
949 CALCULATED_NUMBER_FORMAT " = "
950 COLLECTED_NUMBER_FORMAT
951 " * " CALCULATED_NUMBER_FORMAT
952 " / " CALCULATED_NUMBER_FORMAT
954 , rd->calculated_value
955 , rd->collected_value
956 , (calculated_number)rd->multiplier
957 , (calculated_number)rd->divisor
961 case RRDDIM_PCENT_OVER_ROW_TOTAL:
962 if(unlikely(!st->collected_total)) rd->calculated_value = 0;
964 // the percentage of the current value
965 // over the total of all dimensions
966 rd->calculated_value =
967 (calculated_number)100
968 * (calculated_number)rd->collected_value
969 / (calculated_number)st->collected_total;
971 if(unlikely(st->debug))
972 debug(D_RRD_STATS, "%s/%s: CALC PCENT-ROW "
973 CALCULATED_NUMBER_FORMAT " = 100"
974 " * " COLLECTED_NUMBER_FORMAT
975 " / " COLLECTED_NUMBER_FORMAT
977 , rd->calculated_value
978 , rd->collected_value
979 , st->collected_total
983 case RRDDIM_INCREMENTAL:
984 if(unlikely(!rd->updated || rd->counter <= 1)) {
985 rd->calculated_value = 0;
989 // if the new is smaller than the old (an overflow, or reset), set the old equal to the new
990 // to reset the calculation (it will give zero as the calculation for this second)
991 if(unlikely(rd->last_collected_value > rd->collected_value)) {
992 debug(D_RRD_STATS, "%s.%s: RESET or OVERFLOW. Last collected value = " COLLECTED_NUMBER_FORMAT ", current = " COLLECTED_NUMBER_FORMAT
994 , rd->last_collected_value
995 , rd->collected_value);
996 if(!(rd->flags & RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS)) storage_flags = SN_EXISTS_RESET;
997 rd->last_collected_value = rd->collected_value;
1000 rd->calculated_value = (calculated_number)(rd->collected_value - rd->last_collected_value)
1001 * (calculated_number)rd->multiplier
1002 / (calculated_number)rd->divisor;
1004 if(unlikely(st->debug))
1005 debug(D_RRD_STATS, "%s/%s: CALC INC PRE "
1006 CALCULATED_NUMBER_FORMAT " = ("
1007 COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT
1009 " * " CALCULATED_NUMBER_FORMAT
1010 " / " CALCULATED_NUMBER_FORMAT
1012 , rd->calculated_value
1013 , rd->collected_value, rd->last_collected_value
1014 , (calculated_number)rd->multiplier
1015 , (calculated_number)rd->divisor
1019 case RRDDIM_PCENT_OVER_DIFF_TOTAL:
1020 if(unlikely(!rd->updated || rd->counter <= 1)) {
1021 rd->calculated_value = 0;
1025 // the percentage of the current increment
1026 // over the increment of all dimensions together
1027 if(unlikely(st->collected_total == st->last_collected_total)) rd->calculated_value = rd->last_calculated_value;
1028 else rd->calculated_value =
1029 (calculated_number)100
1030 * (calculated_number)(rd->collected_value - rd->last_collected_value)
1031 / (calculated_number)(st->collected_total - st->last_collected_total);
1033 if(unlikely(st->debug))
1034 debug(D_RRD_STATS, "%s/%s: CALC PCENT-DIFF "
1035 CALCULATED_NUMBER_FORMAT " = 100"
1036 " * (" COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT ")"
1037 " / (" COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT ")"
1039 , rd->calculated_value
1040 , rd->collected_value, rd->last_collected_value
1041 , st->collected_total, st->last_collected_total
1046 // make the default zero, to make sure
1047 // it gets noticed when we add new types
1048 rd->calculated_value = 0;
1050 if(unlikely(st->debug))
1051 debug(D_RRD_STATS, "%s/%s: CALC "
1052 CALCULATED_NUMBER_FORMAT " = 0"
1054 , rd->calculated_value
1059 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s/%s: PHASE2 "
1060 " last_collected_value = " COLLECTED_NUMBER_FORMAT
1061 " collected_value = " COLLECTED_NUMBER_FORMAT
1062 " last_calculated_value = " CALCULATED_NUMBER_FORMAT
1063 " calculated_value = " CALCULATED_NUMBER_FORMAT
1065 , rd->last_collected_value
1066 , rd->collected_value
1067 , rd->last_calculated_value
1068 , rd->calculated_value
1073 // at this point we have all the calculated values ready
1074 // it is now time to interpolate values on a second boundary
1076 unsigned long long first_ut = last_ut;
1077 long long iterations = (now_ut - last_ut) / (st->update_every * 1000000ULL);
1078 if((now_ut % (st->update_every * 1000000ULL)) == 0) iterations++;
1080 for( ; likely(next_ut <= now_ut) ; next_ut += st->update_every * 1000000ULL, iterations-- ) {
1081 #ifdef NETDATA_INTERNAL_CHECKS
1082 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); }
1085 if(unlikely(st->debug)) {
1086 debug(D_RRD_STATS, "%s: last ut = %0.3Lf (last updated time)", st->name, (long double)last_ut/1000000.0);
1087 debug(D_RRD_STATS, "%s: next ut = %0.3Lf (next interpolation point)", st->name, (long double)next_ut/1000000.0);
1090 st->last_updated.tv_sec = (time_t) (next_ut / 1000000ULL);
1091 st->last_updated.tv_usec = 0;
1093 for( rd = st->dimensions ; likely(rd) ; rd = rd->next ) {
1094 calculated_number new_value;
1096 switch(rd->algorithm) {
1097 case RRDDIM_INCREMENTAL:
1098 new_value = (calculated_number)
1099 ( rd->calculated_value
1100 * (calculated_number)(next_ut - last_ut)
1101 / (calculated_number)(now_ut - last_ut)
1104 if(unlikely(st->debug))
1105 debug(D_RRD_STATS, "%s/%s: CALC2 INC "
1106 CALCULATED_NUMBER_FORMAT " = "
1107 CALCULATED_NUMBER_FORMAT
1112 , rd->calculated_value
1113 , (next_ut - last_ut)
1114 , (now_ut - last_ut)
1117 rd->calculated_value -= new_value;
1118 new_value += rd->last_calculated_value;
1119 rd->last_calculated_value = 0;
1120 new_value /= (calculated_number)st->update_every;
1123 case RRDDIM_ABSOLUTE:
1124 case RRDDIM_PCENT_OVER_ROW_TOTAL:
1125 case RRDDIM_PCENT_OVER_DIFF_TOTAL:
1127 if(iterations == 1) {
1128 // this is the last iteration
1129 // do not interpolate
1130 // just show the calculated value
1132 new_value = rd->calculated_value;
1135 // we have missed an update
1136 // interpolate in the middle values
1138 new_value = (calculated_number)
1139 ( ( (rd->calculated_value - rd->last_calculated_value)
1140 * (calculated_number)(next_ut - first_ut)
1141 / (calculated_number)(now_ut - first_ut)
1143 + rd->last_calculated_value
1146 if(unlikely(st->debug))
1147 debug(D_RRD_STATS, "%s/%s: CALC2 DEF "
1148 CALCULATED_NUMBER_FORMAT " = ((("
1149 "(" CALCULATED_NUMBER_FORMAT " - " CALCULATED_NUMBER_FORMAT ")"
1151 " / %llu) + " CALCULATED_NUMBER_FORMAT
1154 , rd->calculated_value, rd->last_calculated_value
1155 , (next_ut - first_ut)
1156 , (now_ut - first_ut), rd->last_calculated_value
1160 // it fades the value towards the target
1161 // while we know the calculated value is different
1162 // if(likely(next_ut + st->update_every * 1000000ULL > now_ut)) rd->calculated_value = new_value;
1167 if(unlikely(!store_this_entry)) {
1168 store_this_entry = 1;
1172 if(likely(rd->updated && rd->counter > 1 && iterations < st->gap_when_lost_iterations_above)) {
1173 rd->values[st->current_entry] = pack_storage_number(new_value, storage_flags );
1175 if(unlikely(st->debug))
1176 debug(D_RRD_STATS, "%s/%s: STORE[%ld] "
1177 CALCULATED_NUMBER_FORMAT " = " CALCULATED_NUMBER_FORMAT
1180 , unpack_storage_number(rd->values[st->current_entry]), new_value
1184 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s/%s: STORE[%ld] = NON EXISTING "
1188 rd->values[st->current_entry] = pack_storage_number(0, SN_NOT_EXISTS);
1193 if(unlikely(st->debug)) {
1194 calculated_number t1 = new_value * (calculated_number)rd->multiplier / (calculated_number)rd->divisor;
1195 calculated_number t2 = unpack_storage_number(rd->values[st->current_entry]);
1196 calculated_number accuracy = accuracy_loss(t1, t2);
1197 debug(D_RRD_STATS, "%s/%s: UNPACK[%ld] = " CALCULATED_NUMBER_FORMAT " FLAGS=0x%08x (original = " CALCULATED_NUMBER_FORMAT ", accuracy loss = " CALCULATED_NUMBER_FORMAT "%%%s)"
1201 , get_storage_number_flags(rd->values[st->current_entry])
1204 , (accuracy > ACCURACY_LOSS) ? " **TOO BIG** " : ""
1207 rd->collected_volume += t1;
1208 rd->stored_volume += t2;
1209 accuracy = accuracy_loss(rd->collected_volume, rd->stored_volume);
1210 debug(D_RRD_STATS, "%s/%s: VOLUME[%ld] = " CALCULATED_NUMBER_FORMAT ", calculated = " CALCULATED_NUMBER_FORMAT ", accuracy loss = " CALCULATED_NUMBER_FORMAT "%%%s"
1214 , rd->collected_volume
1216 , (accuracy > ACCURACY_LOSS) ? " **TOO BIG** " : ""
1221 // reset the storage flags for the next point, if any;
1222 storage_flags = SN_EXISTS;
1225 st->current_entry = ((st->current_entry + 1) >= st->entries) ? 0 : st->current_entry + 1;
1229 // align next interpolation to last collection point
1230 if(likely(stored_entries || !store_this_entry)) {
1231 st->last_updated.tv_sec = st->last_collected_time.tv_sec;
1232 st->last_updated.tv_usec = st->last_collected_time.tv_usec;
1235 for( rd = st->dimensions; likely(rd) ; rd = rd->next ) {
1236 if(unlikely(!rd->updated)) continue;
1238 if(likely(stored_entries || !store_this_entry)) {
1239 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);
1240 rd->last_collected_value = rd->collected_value;
1242 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);
1243 rd->last_calculated_value = rd->calculated_value;
1246 rd->calculated_value = 0;
1247 rd->collected_value = 0;
1250 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s/%s: END "
1251 " last_collected_value = " COLLECTED_NUMBER_FORMAT
1252 " collected_value = " COLLECTED_NUMBER_FORMAT
1253 " last_calculated_value = " CALCULATED_NUMBER_FORMAT
1254 " calculated_value = " CALCULATED_NUMBER_FORMAT
1256 , rd->last_collected_value
1257 , rd->collected_value
1258 , rd->last_calculated_value
1259 , rd->calculated_value
1262 st->last_collected_total = st->collected_total;
1264 // ALL DONE ABOUT THE DATA UPDATE
1265 // --------------------------------------------------------------------
1267 // find if there are any obsolete dimensions (not updated recently)
1268 if(unlikely(rrd_delete_unupdated_dimensions)) {
1270 for( rd = st->dimensions; likely(rd) ; rd = rd->next )
1271 if((rd->last_collected_time.tv_sec + (rrd_delete_unupdated_dimensions * st->update_every)) < st->last_collected_time.tv_sec)
1275 // there is dimension to free
1276 // upgrade our read lock to a write lock
1277 pthread_rwlock_unlock(&st->rwlock);
1278 pthread_rwlock_wrlock(&st->rwlock);
1280 for( rd = st->dimensions, last = NULL ; likely(rd) ; ) {
1281 // remove it only it is not updated in rrd_delete_unupdated_dimensions seconds
1283 if(unlikely((rd->last_collected_time.tv_sec + (rrd_delete_unupdated_dimensions * st->update_every)) < st->last_collected_time.tv_sec)) {
1284 info("Removing obsolete dimension '%s' (%s) of '%s' (%s).", rd->name, rd->id, st->name, st->id);
1286 if(unlikely(!last)) {
1287 st->dimensions = rd->next;
1289 rrddim_free(st, rd);
1290 rd = st->dimensions;
1294 last->next = rd->next;
1296 rrddim_free(st, rd);
1306 if(unlikely(!st->dimensions)) {
1307 info("Disabling chart %s (%s) since it does not have any dimensions", st->name, st->id);
1313 pthread_rwlock_unlock(&st->rwlock);
1315 if(unlikely(pthread_setcancelstate(oldstate, NULL) != 0))
1316 error("Cannot set pthread cancel state to RESTORE (%d).", oldstate);
1318 return(st->usec_since_last_update);