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_iterator(avl *a) { if(a) {}; return 0; }
47 static int rrdset_compare(void* a, void* b) {
48 if(((RRDSET *)a)->hash < ((RRDSET *)b)->hash) return -1;
49 else if(((RRDSET *)a)->hash > ((RRDSET *)b)->hash) return 1;
50 else return strcmp(((RRDSET *)a)->id, ((RRDSET *)b)->id);
53 avl_tree rrdset_root_index = {
56 #ifdef AVL_LOCK_WITH_MUTEX
57 PTHREAD_MUTEX_INITIALIZER
59 PTHREAD_RWLOCK_INITIALIZER
63 #define rrdset_index_add(st) avl_insert(&rrdset_root_index, (avl *)(st))
64 #define rrdset_index_del(st) avl_remove(&rrdset_root_index, (avl *)(st))
66 static RRDSET *rrdset_index_find(const char *id, uint32_t hash) {
67 RRDSET *result = NULL, tmp;
68 strncpy(tmp.id, id, RRD_ID_LENGTH_MAX);
69 tmp.id[RRD_ID_LENGTH_MAX] = '\0';
70 tmp.hash = (hash)?hash:simple_hash(tmp.id);
72 avl_search(&(rrdset_root_index), (avl *)&tmp, rrdset_iterator, (avl **)&result);
76 // ----------------------------------------------------------------------------
79 #define rrdset_from_avlname(avlname_ptr) ((RRDSET *)((avlname_ptr) - offsetof(RRDSET, avlname)))
81 static int rrdset_iterator_name(avl *a) { if(a) {}; return 0; }
83 static int rrdset_compare_name(void* a, void* b) {
84 RRDSET *A = rrdset_from_avlname(a);
85 RRDSET *B = rrdset_from_avlname(b);
87 // fprintf(stderr, "COMPARING: %s with %s\n", A->name, B->name);
89 if(A->hash_name < B->hash_name) return -1;
90 else if(A->hash_name > B->hash_name) return 1;
91 else return strcmp(A->name, B->name);
94 avl_tree rrdset_root_index_name = {
97 #ifdef AVL_LOCK_WITH_MUTEX
98 PTHREAD_MUTEX_INITIALIZER
100 PTHREAD_RWLOCK_INITIALIZER
104 int rrdset_index_add_name(RRDSET *st) {
105 // fprintf(stderr, "ADDING: %s (name: %s)\n", st->id, st->name);
106 return avl_insert(&rrdset_root_index_name, (avl *)(&st->avlname));
109 #define rrdset_index_del_name(st) avl_remove(&rrdset_root_index_name, (avl *)(&st->avlname))
111 static RRDSET *rrdset_index_find_name(const char *name, uint32_t hash) {
115 tmp.hash_name = (hash)?hash:simple_hash(tmp.name);
117 // fprintf(stderr, "SEARCHING: %s\n", name);
118 avl_search(&(rrdset_root_index_name), (avl *)(&(tmp.avlname)), rrdset_iterator_name, (avl **)&result);
120 RRDSET *st = rrdset_from_avlname(result);
121 if(strcmp(st->magic, RRDSET_MAGIC))
122 error("Search for RRDSET %s returned an invalid RRDSET %s (name %s)", name, st->id, st->name);
124 // fprintf(stderr, "FOUND: %s\n", name);
125 return rrdset_from_avlname(result);
127 // fprintf(stderr, "NOT FOUND: %s\n", name);
132 // ----------------------------------------------------------------------------
135 static int rrddim_iterator(avl *a) { if(a) {}; return 0; }
137 static int rrddim_compare(void* a, void* b) {
138 if(((RRDDIM *)a)->hash < ((RRDDIM *)b)->hash) return -1;
139 else if(((RRDDIM *)a)->hash > ((RRDDIM *)b)->hash) return 1;
140 else return strcmp(((RRDDIM *)a)->id, ((RRDDIM *)b)->id);
143 #define rrddim_index_add(st, rd) avl_insert(&((st)->dimensions_index), (avl *)(rd))
144 #define rrddim_index_del(st,rd ) avl_remove(&((st)->dimensions_index), (avl *)(rd))
146 static RRDDIM *rrddim_index_find(RRDSET *st, const char *id, uint32_t hash) {
147 RRDDIM *result = NULL, tmp;
148 strncpy(tmp.id, id, RRD_ID_LENGTH_MAX);
149 tmp.id[RRD_ID_LENGTH_MAX] = '\0';
150 tmp.hash = (hash)?hash:simple_hash(tmp.id);
152 avl_search(&(st->dimensions_index), (avl *)&tmp, rrddim_iterator, (avl **)&result);
156 // ----------------------------------------------------------------------------
159 int rrdset_type_id(const char *name)
161 if(unlikely(strcmp(name, RRDSET_TYPE_AREA_NAME) == 0)) return RRDSET_TYPE_AREA;
162 else if(unlikely(strcmp(name, RRDSET_TYPE_STACKED_NAME) == 0)) return RRDSET_TYPE_STACKED;
163 else if(unlikely(strcmp(name, RRDSET_TYPE_LINE_NAME) == 0)) return RRDSET_TYPE_LINE;
164 return RRDSET_TYPE_LINE;
167 const char *rrdset_type_name(int chart_type)
169 static char line[] = RRDSET_TYPE_LINE_NAME;
170 static char area[] = RRDSET_TYPE_AREA_NAME;
171 static char stacked[] = RRDSET_TYPE_STACKED_NAME;
174 case RRDSET_TYPE_LINE:
177 case RRDSET_TYPE_AREA:
180 case RRDSET_TYPE_STACKED:
186 // ----------------------------------------------------------------------------
189 const char *rrd_memory_mode_name(int id)
191 static const char ram[] = RRD_MEMORY_MODE_RAM_NAME;
192 static const char map[] = RRD_MEMORY_MODE_MAP_NAME;
193 static const char save[] = RRD_MEMORY_MODE_SAVE_NAME;
196 case RRD_MEMORY_MODE_RAM:
199 case RRD_MEMORY_MODE_MAP:
202 case RRD_MEMORY_MODE_SAVE:
210 int rrd_memory_mode_id(const char *name)
212 if(unlikely(!strcmp(name, RRD_MEMORY_MODE_RAM_NAME)))
213 return RRD_MEMORY_MODE_RAM;
214 else if(unlikely(!strcmp(name, RRD_MEMORY_MODE_MAP_NAME)))
215 return RRD_MEMORY_MODE_MAP;
217 return RRD_MEMORY_MODE_SAVE;
220 // ----------------------------------------------------------------------------
223 int rrddim_algorithm_id(const char *name)
225 if(strcmp(name, RRDDIM_ABSOLUTE_NAME) == 0) return RRDDIM_ABSOLUTE;
226 if(strcmp(name, RRDDIM_INCREMENTAL_NAME) == 0) return RRDDIM_INCREMENTAL;
227 if(strcmp(name, RRDDIM_PCENT_OVER_ROW_TOTAL_NAME) == 0) return RRDDIM_PCENT_OVER_ROW_TOTAL;
228 if(strcmp(name, RRDDIM_PCENT_OVER_DIFF_TOTAL_NAME) == 0) return RRDDIM_PCENT_OVER_DIFF_TOTAL;
229 return RRDDIM_ABSOLUTE;
232 const char *rrddim_algorithm_name(int chart_type)
234 static char absolute[] = RRDDIM_ABSOLUTE_NAME;
235 static char incremental[] = RRDDIM_INCREMENTAL_NAME;
236 static char percentage_of_absolute_row[] = RRDDIM_PCENT_OVER_ROW_TOTAL_NAME;
237 static char percentage_of_incremental_row[] = RRDDIM_PCENT_OVER_DIFF_TOTAL_NAME;
240 case RRDDIM_ABSOLUTE:
243 case RRDDIM_INCREMENTAL:
246 case RRDDIM_PCENT_OVER_ROW_TOTAL:
247 return percentage_of_absolute_row;
249 case RRDDIM_PCENT_OVER_DIFF_TOTAL:
250 return percentage_of_incremental_row;
255 // ----------------------------------------------------------------------------
258 char *rrdset_strncpy_name(char *to, const char *from, int length)
261 for(i = 0; i < length && from[i] ;i++) {
262 if(from[i] == '.' || isalpha(from[i]) || isdigit(from[i])) to[i] = from[i];
265 if(i < length) to[i] = '\0';
266 to[length - 1] = '\0';
271 void rrdset_set_name(RRDSET *st, const char *name)
273 debug(D_RRD_CALLS, "rrdset_set_name() old: %s, new: %s", st->name, name);
275 if(st->name) rrdset_index_del_name(st);
277 char b[CONFIG_MAX_VALUE + 1];
278 char n[RRD_ID_LENGTH_MAX + 1];
280 snprintf(n, RRD_ID_LENGTH_MAX, "%s.%s", st->type, name);
281 rrdset_strncpy_name(b, n, CONFIG_MAX_VALUE);
282 st->name = config_get(st->id, "name", b);
283 st->hash_name = simple_hash(st->name);
285 rrdset_index_add_name(st);
288 // ----------------------------------------------------------------------------
291 char *rrdset_cache_dir(const char *id)
295 static char *cache_dir = NULL;
296 if(!cache_dir) cache_dir = config_get("global", "cache directory", CACHE_DIR);
298 char b[FILENAME_MAX + 1];
299 char n[FILENAME_MAX + 1];
300 rrdset_strncpy_name(b, id, FILENAME_MAX);
302 snprintf(n, FILENAME_MAX, "%s/%s", cache_dir, b);
303 ret = config_get(id, "cache directory", n);
305 if(rrd_memory_mode == RRD_MEMORY_MODE_MAP || rrd_memory_mode == RRD_MEMORY_MODE_SAVE) {
306 int r = mkdir(ret, 0775);
307 if(r != 0 && errno != EEXIST)
308 error("Cannot create directory '%s'", ret);
314 // ----------------------------------------------------------------------------
317 void rrdset_reset(RRDSET *st)
319 debug(D_RRD_CALLS, "rrdset_reset() %s", st->name);
321 st->last_collected_time.tv_sec = 0;
322 st->last_collected_time.tv_usec = 0;
323 st->last_updated.tv_sec = 0;
324 st->last_updated.tv_usec = 0;
325 st->current_entry = 0;
327 st->counter_done = 0;
330 for(rd = st->dimensions; rd ; rd = rd->next) {
331 rd->last_collected_time.tv_sec = 0;
332 rd->last_collected_time.tv_usec = 0;
334 bzero(rd->values, rd->entries * sizeof(storage_number));
338 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)
340 if(!type || !type[0]) {
341 fatal("Cannot create rrd stats without a type.");
346 fatal("Cannot create rrd stats without an id.");
350 char fullid[RRD_ID_LENGTH_MAX + 1];
351 char fullfilename[FILENAME_MAX + 1];
354 snprintf(fullid, RRD_ID_LENGTH_MAX, "%s.%s", type, id);
356 st = rrdset_find(fullid);
358 error("Cannot create rrd stats for '%s', it already exists.", fullid);
362 long entries = config_get_number(fullid, "history", rrd_default_history_entries);
363 if(entries < 5) entries = config_set_number(fullid, "history", 5);
364 if(entries > RRD_HISTORY_ENTRIES_MAX) entries = config_set_number(fullid, "history", RRD_HISTORY_ENTRIES_MAX);
366 int enabled = config_get_boolean(fullid, "enabled", 1);
367 if(!enabled) entries = 5;
369 unsigned long size = sizeof(RRDSET);
370 char *cache_dir = rrdset_cache_dir(fullid);
372 debug(D_RRD_CALLS, "Creating RRD_STATS for '%s.%s'.", type, id);
374 snprintf(fullfilename, FILENAME_MAX, "%s/main.db", cache_dir);
375 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);
377 if(strcmp(st->magic, RRDSET_MAGIC) != 0) {
379 info("Initializing file %s.", fullfilename);
382 else if(strcmp(st->id, fullid) != 0) {
384 error("File %s contents are not for chart %s. Clearing it.", fullfilename, fullid);
389 else if(st->memsize != size || st->entries != entries) {
391 error("File %s does not have the desired size. Clearing it.", fullfilename);
394 else if(st->update_every != update_every) {
396 error("File %s does not have the desired update frequency. Clearing it.", fullfilename);
399 else if((time(NULL) - st->last_updated.tv_sec) > update_every * entries) {
401 error("File %s is too old. Clearing it.", fullfilename);
413 st->dimensions = NULL;
415 st->mapped = rrd_memory_mode;
418 st = calloc(1, size);
420 fatal("Cannot allocate memory for RRD_STATS %s.%s", type, id);
423 st->mapped = RRD_MEMORY_MODE_RAM;
426 st->entries = entries;
427 st->update_every = update_every;
429 strcpy(st->cache_filename, fullfilename);
430 strcpy(st->magic, RRDSET_MAGIC);
432 strcpy(st->id, fullid);
433 st->hash = simple_hash(st->id);
435 st->cache_dir = cache_dir;
437 st->chart_type = rrdset_type_id(config_get(st->id, "chart type", rrdset_type_name(chart_type)));
438 st->type = config_get(st->id, "type", type);
439 st->family = config_get(st->id, "family", family?family:st->type);
440 st->context = config_get(st->id, "context", context?context:st->id);
441 st->units = config_get(st->id, "units", units?units:"");
443 st->priority = config_get_number(st->id, "priority", priority);
444 st->enabled = enabled;
449 st->last_collected_time.tv_sec = 0;
450 st->last_collected_time.tv_usec = 0;
451 st->counter_done = 0;
453 st->gap_when_lost_iterations_above = (int) (
454 config_get_number(st->id, "gap when lost iterations above", RRD_DEFAULT_GAP_INTERPOLATIONS) + 2);
456 avl_init(&st->dimensions_index, rrddim_compare);
458 pthread_rwlock_init(&st->rwlock, NULL);
459 pthread_rwlock_wrlock(&rrdset_root_rwlock);
461 if(name && *name) rrdset_set_name(st, name);
462 else rrdset_set_name(st, id);
465 char varvalue[CONFIG_MAX_VALUE + 1];
466 snprintf(varvalue, CONFIG_MAX_VALUE, "%s (%s)", title?title:"", st->name);
467 st->title = config_get(st->id, "title", varvalue);
470 st->next = rrdset_root;
473 rrdset_index_add(st);
475 pthread_rwlock_unlock(&rrdset_root_rwlock);
480 RRDDIM *rrddim_add(RRDSET *st, const char *id, const char *name, long multiplier, long divisor, int algorithm)
482 char filename[FILENAME_MAX + 1];
483 char fullfilename[FILENAME_MAX + 1];
485 char varname[CONFIG_MAX_NAME + 1];
487 unsigned long size = sizeof(RRDDIM) + (st->entries * sizeof(storage_number));
489 debug(D_RRD_CALLS, "Adding dimension '%s/%s'.", st->id, id);
491 rrdset_strncpy_name(filename, id, FILENAME_MAX);
492 snprintf(fullfilename, FILENAME_MAX, "%s/%s.db", st->cache_dir, filename);
493 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);
496 gettimeofday(&now, NULL);
498 if(strcmp(rd->magic, RRDDIMENSION_MAGIC) != 0) {
500 info("Initializing file %s.", fullfilename);
503 else if(rd->memsize != size) {
505 error("File %s does not have the desired size. Clearing it.", fullfilename);
508 else if(rd->multiplier != multiplier) {
510 error("File %s does not have the same multiplier. Clearing it.", fullfilename);
513 else if(rd->divisor != divisor) {
515 error("File %s does not have the same divisor. Clearing it.", fullfilename);
518 else if(rd->algorithm != algorithm) {
520 error("File %s does not have the same algorithm. Clearing it.", fullfilename);
523 else if(rd->update_every != st->update_every) {
525 error("File %s does not have the same refresh frequency. Clearing it.", fullfilename);
528 else if(usecdiff(&now, &rd->last_collected_time) > (rd->entries * rd->update_every * 1000000ULL)) {
530 error("File %s is too old. Clearing it.", fullfilename);
533 else if(strcmp(rd->id, id) != 0) {
535 error("File %s contents are not for dimension %s. Clearing it.", fullfilename, id);
543 // we have a file mapped for rd
544 rd->mapped = rrd_memory_mode;
545 rd->flags = 0x00000000;
550 // if we didn't manage to get a mmap'd dimension, just create one
552 rd = calloc(1, size);
554 fatal("Cannot allocate RRD_DIMENSION %s/%s.", st->id, id);
558 rd->mapped = RRD_MEMORY_MODE_RAM;
562 strcpy(rd->magic, RRDDIMENSION_MAGIC);
563 strcpy(rd->cache_filename, fullfilename);
564 strncpy(rd->id, id, RRD_ID_LENGTH_MAX);
565 rd->hash = simple_hash(rd->id);
567 snprintf(varname, CONFIG_MAX_NAME, "dim %s name", rd->id);
568 rd->name = config_get(st->id, varname, (name && *name)?name:rd->id);
570 snprintf(varname, CONFIG_MAX_NAME, "dim %s algorithm", rd->id);
571 rd->algorithm = rrddim_algorithm_id(config_get(st->id, varname, rrddim_algorithm_name(algorithm)));
573 snprintf(varname, CONFIG_MAX_NAME, "dim %s multiplier", rd->id);
574 rd->multiplier = config_get_number(st->id, varname, multiplier);
576 snprintf(varname, CONFIG_MAX_NAME, "dim %s divisor", rd->id);
577 rd->divisor = config_get_number(st->id, varname, divisor);
578 if(!rd->divisor) rd->divisor = 1;
580 rd->entries = st->entries;
581 rd->update_every = st->update_every;
583 // prevent incremental calculation spikes
586 // append this dimension
587 pthread_rwlock_wrlock(&st->rwlock);
591 RRDDIM *td = st->dimensions;
592 for(; td->next; td = td->next) ;
595 pthread_rwlock_unlock(&st->rwlock);
597 rrddim_index_add(st, rd);
602 void rrddim_set_name(RRDSET *st, RRDDIM *rd, const char *name)
604 debug(D_RRD_CALLS, "rrddim_set_name() %s.%s", st->name, rd->name);
606 char varname[CONFIG_MAX_NAME + 1];
607 snprintf(varname, CONFIG_MAX_NAME, "dim %s name", rd->id);
608 config_set_default(st->id, varname, name);
611 void rrddim_free(RRDSET *st, RRDDIM *rd)
613 debug(D_RRD_CALLS, "rrddim_free() %s.%s", st->name, rd->name);
615 RRDDIM *i, *last = NULL;
616 for(i = st->dimensions; i && i != rd ; i = i->next) last = i;
619 error("Request to free dimension %s.%s but it is not linked.", st->id, rd->name);
623 if(last) last->next = rd->next;
624 else st->dimensions = rd->next;
627 rrddim_index_del(st, rd);
629 // free(rd->annotations);
630 if(rd->mapped == RRD_MEMORY_MODE_SAVE) {
631 debug(D_RRD_CALLS, "Saving dimension '%s' to '%s'.", rd->name, rd->cache_filename);
632 savememory(rd->cache_filename, rd, rd->memsize);
634 debug(D_RRD_CALLS, "Unmapping dimension '%s'.", rd->name);
635 munmap(rd, rd->memsize);
637 else if(rd->mapped == RRD_MEMORY_MODE_MAP) {
638 debug(D_RRD_CALLS, "Unmapping dimension '%s'.", rd->name);
639 munmap(rd, rd->memsize);
642 debug(D_RRD_CALLS, "Removing dimension '%s'.", rd->name);
647 void rrdset_free_all(void)
649 info("Freeing all memory...");
652 for(st = rrdset_root; st ;) {
653 RRDSET *next = st->next;
655 while(st->dimensions)
656 rrddim_free(st, st->dimensions);
658 rrdset_index_del(st);
660 if(st->mapped == RRD_MEMORY_MODE_SAVE) {
661 debug(D_RRD_CALLS, "Saving stats '%s' to '%s'.", st->name, st->cache_filename);
662 savememory(st->cache_filename, st, st->memsize);
664 debug(D_RRD_CALLS, "Unmapping stats '%s'.", st->name);
665 munmap(st, st->memsize);
667 else if(st->mapped == RRD_MEMORY_MODE_MAP) {
668 debug(D_RRD_CALLS, "Unmapping stats '%s'.", st->name);
669 munmap(st, st->memsize);
678 info("Memory cleanup completed...");
681 void rrdset_save_all(void)
683 debug(D_RRD_CALLS, "rrdset_save_all()");
685 // let it log a few error messages
686 error_log_limit_reset();
691 pthread_rwlock_wrlock(&rrdset_root_rwlock);
692 for(st = rrdset_root; st ; st = st->next) {
693 pthread_rwlock_wrlock(&st->rwlock);
695 if(st->mapped == RRD_MEMORY_MODE_SAVE) {
696 debug(D_RRD_CALLS, "Saving stats '%s' to '%s'.", st->name, st->cache_filename);
697 savememory(st->cache_filename, st, st->memsize);
700 for(rd = st->dimensions; rd ; rd = rd->next) {
701 if(likely(rd->mapped == RRD_MEMORY_MODE_SAVE)) {
702 debug(D_RRD_CALLS, "Saving dimension '%s' to '%s'.", rd->name, rd->cache_filename);
703 savememory(rd->cache_filename, rd, rd->memsize);
707 pthread_rwlock_unlock(&st->rwlock);
709 pthread_rwlock_unlock(&rrdset_root_rwlock);
713 RRDSET *rrdset_find(const char *id)
715 debug(D_RRD_CALLS, "rrdset_find() for chart %s", id);
717 RRDSET *st = rrdset_index_find(id, 0);
721 RRDSET *rrdset_find_bytype(const char *type, const char *id)
723 debug(D_RRD_CALLS, "rrdset_find_bytype() for chart %s.%s", type, id);
725 char buf[RRD_ID_LENGTH_MAX + 1];
727 strncpy(buf, type, RRD_ID_LENGTH_MAX - 1);
728 buf[RRD_ID_LENGTH_MAX - 1] = '\0';
730 int len = (int) strlen(buf);
731 strncpy(&buf[len], id, (size_t) (RRD_ID_LENGTH_MAX - len));
732 buf[RRD_ID_LENGTH_MAX] = '\0';
734 return(rrdset_find(buf));
737 RRDSET *rrdset_find_byname(const char *name)
739 debug(D_RRD_CALLS, "rrdset_find_byname() for chart %s", name);
741 RRDSET *st = rrdset_index_find_name(name, 0);
745 RRDDIM *rrddim_find(RRDSET *st, const char *id)
747 debug(D_RRD_CALLS, "rrddim_find() for chart %s, dimension %s", st->name, id);
749 return rrddim_index_find(st, id, 0);
752 int rrddim_hide(RRDSET *st, const char *id)
754 debug(D_RRD_CALLS, "rrddim_hide() for chart %s, dimension %s", st->name, id);
756 RRDDIM *rd = rrddim_find(st, id);
758 error("Cannot find dimension with id '%s' on stats '%s' (%s).", id, st->name, st->id);
762 rd->flags |= RRDDIM_FLAG_HIDDEN;
766 int rrddim_unhide(RRDSET *st, const char *id)
768 debug(D_RRD_CALLS, "rrddim_unhide() for chart %s, dimension %s", st->name, id);
770 RRDDIM *rd = rrddim_find(st, id);
772 error("Cannot find dimension with id '%s' on stats '%s' (%s).", id, st->name, st->id);
776 if(rd->flags & RRDDIM_FLAG_HIDDEN) rd->flags ^= RRDDIM_FLAG_HIDDEN;
780 collected_number rrddim_set_by_pointer(RRDSET *st, RRDDIM *rd, collected_number value)
782 debug(D_RRD_CALLS, "rrddim_set_by_pointer() for chart %s, dimension %s, value " COLLECTED_NUMBER_FORMAT, st->name, rd->name, value);
784 gettimeofday(&rd->last_collected_time, NULL);
785 rd->collected_value = value;
789 return rd->last_collected_value;
792 collected_number rrddim_set(RRDSET *st, const char *id, collected_number value)
794 RRDDIM *rd = rrddim_find(st, id);
796 error("Cannot find dimension with id '%s' on stats '%s' (%s).", id, st->name, st->id);
800 return rrddim_set_by_pointer(st, rd, value);
803 void rrdset_next_usec(RRDSET *st, unsigned long long microseconds)
805 if(!microseconds) rrdset_next(st);
807 debug(D_RRD_CALLS, "rrdset_next_usec() for chart %s with microseconds %llu", st->name, microseconds);
809 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s: NEXT: %llu microseconds", st->name, microseconds);
810 st->usec_since_last_update = microseconds;
814 void rrdset_next(RRDSET *st)
816 unsigned long long microseconds = 0;
818 if(likely(st->last_collected_time.tv_sec)) {
820 gettimeofday(&now, NULL);
821 microseconds = usecdiff(&now, &st->last_collected_time);
823 // prevent infinite loop
824 else microseconds = st->update_every * 1000000ULL;
826 rrdset_next_usec(st, microseconds);
829 void rrdset_next_plugins(RRDSET *st)
834 unsigned long long rrdset_done(RRDSET *st)
836 debug(D_RRD_CALLS, "rrdset_done() for chart %s", st->name);
839 int oldstate, store_this_entry = 1, first_entry = 0;
840 unsigned long long last_ut, now_ut, next_ut, stored_entries = 0;
842 if(unlikely(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate) != 0))
843 error("Cannot set pthread cancel state to DISABLE.");
845 // a read lock is OK here
846 pthread_rwlock_rdlock(&st->rwlock);
848 // enable the chart, if it was disabled
851 // check if the chart has a long time to be updated
852 if(unlikely(st->usec_since_last_update > st->entries * st->update_every * 1000000ULL)) {
853 info("%s: took too long to be updated (%0.3Lf secs). Reseting it.", st->name, (long double)(st->usec_since_last_update / 1000000.0));
855 st->usec_since_last_update = st->update_every * 1000000ULL;
858 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s: microseconds since last update: %llu", st->name, st->usec_since_last_update);
860 // set last_collected_time
861 if(unlikely(!st->last_collected_time.tv_sec)) {
862 // it is the first entry
863 // set the last_collected_time to now
864 gettimeofday(&st->last_collected_time, NULL);
866 // the first entry should not be stored
867 store_this_entry = 0;
870 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);
873 // it is not the first entry
874 // calculate the proper last_collected_time, using usec_since_last_update
875 unsigned long long ut = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec + st->usec_since_last_update;
876 st->last_collected_time.tv_sec = (time_t) (ut / 1000000ULL);
877 st->last_collected_time.tv_usec = (useconds_t) (ut % 1000000ULL);
880 // if this set has not been updated in the past
881 // we fake the last_update time to be = now - usec_since_last_update
882 if(unlikely(!st->last_updated.tv_sec)) {
883 // it has never been updated before
884 // set a fake last_updated, in the past using usec_since_last_update
885 unsigned long long ut = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec - st->usec_since_last_update;
886 st->last_updated.tv_sec = (time_t) (ut / 1000000ULL);
887 st->last_updated.tv_usec = (useconds_t) (ut % 1000000ULL);
889 // the first entry should not be stored
890 store_this_entry = 0;
893 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);
896 // check if we will re-write the entire data set
897 if(unlikely(usecdiff(&st->last_collected_time, &st->last_updated) > st->update_every * st->entries * 1000000ULL)) {
898 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);
901 st->usec_since_last_update = st->update_every * 1000000ULL;
903 gettimeofday(&st->last_collected_time, NULL);
905 unsigned long long ut = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec - st->usec_since_last_update;
906 st->last_updated.tv_sec = (time_t) (ut / 1000000ULL);
907 st->last_updated.tv_usec = (useconds_t) (ut % 1000000ULL);
909 // the first entry should not be stored
910 store_this_entry = 0;
914 // these are the 3 variables that will help us in interpolation
915 // last_ut = the last time we added a value to the storage
916 // now_ut = the time the current value is taken at
917 // next_ut = the time of the next interpolation point
918 last_ut = st->last_updated.tv_sec * 1000000ULL + st->last_updated.tv_usec;
919 now_ut = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec;
920 next_ut = (st->last_updated.tv_sec + st->update_every) * 1000000ULL;
922 if(unlikely(!first_entry && now_ut < next_ut)) {
923 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s: THIS IS IN THE SAME INTERPOLATION POINT", st->name);
926 if(unlikely(st->debug)) {
927 debug(D_RRD_STATS, "%s: last ut = %0.3Lf (last updated time)", st->name, (long double)last_ut/1000000.0);
928 debug(D_RRD_STATS, "%s: now ut = %0.3Lf (current update time)", st->name, (long double)now_ut/1000000.0);
929 debug(D_RRD_STATS, "%s: next ut = %0.3Lf (next interpolation point)", st->name, (long double)next_ut/1000000.0);
932 if(unlikely(!st->counter_done)) {
933 store_this_entry = 0;
934 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s: Will not store the next entry.", st->name);
938 // calculate totals and count the dimensions
940 st->collected_total = 0;
941 for( rd = st->dimensions, dimensions = 0 ; likely(rd) ; rd = rd->next, dimensions++ )
942 st->collected_total += rd->collected_value;
944 uint32_t storage_flags = SN_EXISTS;
946 // process all dimensions to calculate their values
947 // based on the collected figures only
948 // at this stage we do not interpolate anything
949 for( rd = st->dimensions ; likely(rd) ; rd = rd->next ) {
951 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s/%s: START "
952 " last_collected_value = " COLLECTED_NUMBER_FORMAT
953 " collected_value = " COLLECTED_NUMBER_FORMAT
954 " last_calculated_value = " CALCULATED_NUMBER_FORMAT
955 " calculated_value = " CALCULATED_NUMBER_FORMAT
957 , rd->last_collected_value
958 , rd->collected_value
959 , rd->last_calculated_value
960 , rd->calculated_value
963 switch(rd->algorithm) {
964 case RRDDIM_ABSOLUTE:
965 rd->calculated_value = (calculated_number)rd->collected_value
966 * (calculated_number)rd->multiplier
967 / (calculated_number)rd->divisor;
969 if(unlikely(st->debug))
970 debug(D_RRD_STATS, "%s/%s: CALC ABS/ABS-NO-IN "
971 CALCULATED_NUMBER_FORMAT " = "
972 COLLECTED_NUMBER_FORMAT
973 " * " CALCULATED_NUMBER_FORMAT
974 " / " CALCULATED_NUMBER_FORMAT
976 , rd->calculated_value
977 , rd->collected_value
978 , (calculated_number)rd->multiplier
979 , (calculated_number)rd->divisor
983 case RRDDIM_PCENT_OVER_ROW_TOTAL:
984 if(unlikely(!st->collected_total)) rd->calculated_value = 0;
986 // the percentage of the current value
987 // over the total of all dimensions
988 rd->calculated_value =
989 (calculated_number)100
990 * (calculated_number)rd->collected_value
991 / (calculated_number)st->collected_total;
993 if(unlikely(st->debug))
994 debug(D_RRD_STATS, "%s/%s: CALC PCENT-ROW "
995 CALCULATED_NUMBER_FORMAT " = 100"
996 " * " COLLECTED_NUMBER_FORMAT
997 " / " COLLECTED_NUMBER_FORMAT
999 , rd->calculated_value
1000 , rd->collected_value
1001 , st->collected_total
1005 case RRDDIM_INCREMENTAL:
1006 if(unlikely(!rd->updated || rd->counter <= 1)) {
1007 rd->calculated_value = 0;
1011 // if the new is smaller than the old (an overflow, or reset), set the old equal to the new
1012 // to reset the calculation (it will give zero as the calculation for this second)
1013 if(unlikely(rd->last_collected_value > rd->collected_value)) {
1014 debug(D_RRD_STATS, "%s.%s: RESET or OVERFLOW. Last collected value = " COLLECTED_NUMBER_FORMAT ", current = " COLLECTED_NUMBER_FORMAT
1015 , st->name, rd->name
1016 , rd->last_collected_value
1017 , rd->collected_value);
1018 if(!(rd->flags & RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS)) storage_flags = SN_EXISTS_RESET;
1019 rd->last_collected_value = rd->collected_value;
1022 rd->calculated_value = (calculated_number)(rd->collected_value - rd->last_collected_value)
1023 * (calculated_number)rd->multiplier
1024 / (calculated_number)rd->divisor;
1026 if(unlikely(st->debug))
1027 debug(D_RRD_STATS, "%s/%s: CALC INC PRE "
1028 CALCULATED_NUMBER_FORMAT " = ("
1029 COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT
1031 " * " CALCULATED_NUMBER_FORMAT
1032 " / " CALCULATED_NUMBER_FORMAT
1034 , rd->calculated_value
1035 , rd->collected_value, rd->last_collected_value
1036 , (calculated_number)rd->multiplier
1037 , (calculated_number)rd->divisor
1041 case RRDDIM_PCENT_OVER_DIFF_TOTAL:
1042 if(unlikely(!rd->updated || rd->counter <= 1)) {
1043 rd->calculated_value = 0;
1047 // the percentage of the current increment
1048 // over the increment of all dimensions together
1049 if(unlikely(st->collected_total == st->last_collected_total)) rd->calculated_value = rd->last_calculated_value;
1050 else rd->calculated_value =
1051 (calculated_number)100
1052 * (calculated_number)(rd->collected_value - rd->last_collected_value)
1053 / (calculated_number)(st->collected_total - st->last_collected_total);
1055 if(unlikely(st->debug))
1056 debug(D_RRD_STATS, "%s/%s: CALC PCENT-DIFF "
1057 CALCULATED_NUMBER_FORMAT " = 100"
1058 " * (" COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT ")"
1059 " / (" COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT ")"
1061 , rd->calculated_value
1062 , rd->collected_value, rd->last_collected_value
1063 , st->collected_total, st->last_collected_total
1068 // make the default zero, to make sure
1069 // it gets noticed when we add new types
1070 rd->calculated_value = 0;
1072 if(unlikely(st->debug))
1073 debug(D_RRD_STATS, "%s/%s: CALC "
1074 CALCULATED_NUMBER_FORMAT " = 0"
1076 , rd->calculated_value
1081 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s/%s: PHASE2 "
1082 " last_collected_value = " COLLECTED_NUMBER_FORMAT
1083 " collected_value = " COLLECTED_NUMBER_FORMAT
1084 " last_calculated_value = " CALCULATED_NUMBER_FORMAT
1085 " calculated_value = " CALCULATED_NUMBER_FORMAT
1087 , rd->last_collected_value
1088 , rd->collected_value
1089 , rd->last_calculated_value
1090 , rd->calculated_value
1095 // at this point we have all the calculated values ready
1096 // it is now time to interpolate values on a second boundary
1098 unsigned long long first_ut = last_ut;
1099 long long iterations = (now_ut - last_ut) / (st->update_every * 1000000ULL);
1100 if((now_ut % (st->update_every * 1000000ULL)) == 0) iterations++;
1102 for( ; likely(next_ut <= now_ut) ; next_ut += st->update_every * 1000000ULL, iterations-- ) {
1103 #ifdef NETDATA_INTERNAL_CHECKS
1104 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); }
1107 if(unlikely(st->debug)) {
1108 debug(D_RRD_STATS, "%s: last ut = %0.3Lf (last updated time)", st->name, (long double)last_ut/1000000.0);
1109 debug(D_RRD_STATS, "%s: next ut = %0.3Lf (next interpolation point)", st->name, (long double)next_ut/1000000.0);
1112 st->last_updated.tv_sec = (time_t) (next_ut / 1000000ULL);
1113 st->last_updated.tv_usec = 0;
1115 for( rd = st->dimensions ; likely(rd) ; rd = rd->next ) {
1116 calculated_number new_value;
1118 switch(rd->algorithm) {
1119 case RRDDIM_INCREMENTAL:
1120 new_value = (calculated_number)
1121 ( rd->calculated_value
1122 * (calculated_number)(next_ut - last_ut)
1123 / (calculated_number)(now_ut - last_ut)
1126 if(unlikely(st->debug))
1127 debug(D_RRD_STATS, "%s/%s: CALC2 INC "
1128 CALCULATED_NUMBER_FORMAT " = "
1129 CALCULATED_NUMBER_FORMAT
1134 , rd->calculated_value
1135 , (next_ut - last_ut)
1136 , (now_ut - last_ut)
1139 rd->calculated_value -= new_value;
1140 new_value += rd->last_calculated_value;
1141 rd->last_calculated_value = 0;
1142 new_value /= (calculated_number)st->update_every;
1145 case RRDDIM_ABSOLUTE:
1146 case RRDDIM_PCENT_OVER_ROW_TOTAL:
1147 case RRDDIM_PCENT_OVER_DIFF_TOTAL:
1149 if(iterations == 1) {
1150 // this is the last iteration
1151 // do not interpolate
1152 // just show the calculated value
1154 new_value = rd->calculated_value;
1157 // we have missed an update
1158 // interpolate in the middle values
1160 new_value = (calculated_number)
1161 ( ( (rd->calculated_value - rd->last_calculated_value)
1162 * (calculated_number)(next_ut - first_ut)
1163 / (calculated_number)(now_ut - first_ut)
1165 + rd->last_calculated_value
1168 if(unlikely(st->debug))
1169 debug(D_RRD_STATS, "%s/%s: CALC2 DEF "
1170 CALCULATED_NUMBER_FORMAT " = ((("
1171 "(" CALCULATED_NUMBER_FORMAT " - " CALCULATED_NUMBER_FORMAT ")"
1173 " / %llu) + " CALCULATED_NUMBER_FORMAT
1176 , rd->calculated_value, rd->last_calculated_value
1177 , (next_ut - first_ut)
1178 , (now_ut - first_ut), rd->last_calculated_value
1182 // it fades the value towards the target
1183 // while we know the calculated value is different
1184 // if(likely(next_ut + st->update_every * 1000000ULL > now_ut)) rd->calculated_value = new_value;
1189 if(unlikely(!store_this_entry)) {
1190 store_this_entry = 1;
1194 if(likely(rd->updated && rd->counter > 1 && iterations < st->gap_when_lost_iterations_above)) {
1195 rd->values[st->current_entry] = pack_storage_number(new_value, storage_flags );
1197 if(unlikely(st->debug))
1198 debug(D_RRD_STATS, "%s/%s: STORE[%ld] "
1199 CALCULATED_NUMBER_FORMAT " = " CALCULATED_NUMBER_FORMAT
1202 , unpack_storage_number(rd->values[st->current_entry]), new_value
1206 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s/%s: STORE[%ld] = NON EXISTING "
1210 rd->values[st->current_entry] = pack_storage_number(0, SN_NOT_EXISTS);
1215 if(unlikely(st->debug)) {
1216 calculated_number t1 = new_value * (calculated_number)rd->multiplier / (calculated_number)rd->divisor;
1217 calculated_number t2 = unpack_storage_number(rd->values[st->current_entry]);
1218 calculated_number accuracy = accuracy_loss(t1, t2);
1219 debug(D_RRD_STATS, "%s/%s: UNPACK[%ld] = " CALCULATED_NUMBER_FORMAT " FLAGS=0x%08x (original = " CALCULATED_NUMBER_FORMAT ", accuracy loss = " CALCULATED_NUMBER_FORMAT "%%%s)"
1223 , get_storage_number_flags(rd->values[st->current_entry])
1226 , (accuracy > ACCURACY_LOSS) ? " **TOO BIG** " : ""
1229 rd->collected_volume += t1;
1230 rd->stored_volume += t2;
1231 accuracy = accuracy_loss(rd->collected_volume, rd->stored_volume);
1232 debug(D_RRD_STATS, "%s/%s: VOLUME[%ld] = " CALCULATED_NUMBER_FORMAT ", calculated = " CALCULATED_NUMBER_FORMAT ", accuracy loss = " CALCULATED_NUMBER_FORMAT "%%%s"
1236 , rd->collected_volume
1238 , (accuracy > ACCURACY_LOSS) ? " **TOO BIG** " : ""
1243 // reset the storage flags for the next point, if any;
1244 storage_flags = SN_EXISTS;
1247 st->current_entry = ((st->current_entry + 1) >= st->entries) ? 0 : st->current_entry + 1;
1251 // align next interpolation to last collection point
1252 if(likely(stored_entries || !store_this_entry)) {
1253 st->last_updated.tv_sec = st->last_collected_time.tv_sec;
1254 st->last_updated.tv_usec = st->last_collected_time.tv_usec;
1257 for( rd = st->dimensions; likely(rd) ; rd = rd->next ) {
1258 if(unlikely(!rd->updated)) continue;
1260 if(likely(stored_entries || !store_this_entry)) {
1261 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);
1262 rd->last_collected_value = rd->collected_value;
1264 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);
1265 rd->last_calculated_value = rd->calculated_value;
1268 rd->calculated_value = 0;
1269 rd->collected_value = 0;
1272 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s/%s: END "
1273 " last_collected_value = " COLLECTED_NUMBER_FORMAT
1274 " collected_value = " COLLECTED_NUMBER_FORMAT
1275 " last_calculated_value = " CALCULATED_NUMBER_FORMAT
1276 " calculated_value = " CALCULATED_NUMBER_FORMAT
1278 , rd->last_collected_value
1279 , rd->collected_value
1280 , rd->last_calculated_value
1281 , rd->calculated_value
1284 st->last_collected_total = st->collected_total;
1286 // ALL DONE ABOUT THE DATA UPDATE
1287 // --------------------------------------------------------------------
1289 // find if there are any obsolete dimensions (not updated recently)
1290 if(unlikely(rrd_delete_unupdated_dimensions)) {
1292 for( rd = st->dimensions; likely(rd) ; rd = rd->next )
1293 if((rd->last_collected_time.tv_sec + (rrd_delete_unupdated_dimensions * st->update_every)) < st->last_collected_time.tv_sec)
1297 // there is dimension to free
1298 // upgrade our read lock to a write lock
1299 pthread_rwlock_unlock(&st->rwlock);
1300 pthread_rwlock_wrlock(&st->rwlock);
1302 for( rd = st->dimensions, last = NULL ; likely(rd) ; ) {
1303 // remove it only it is not updated in rrd_delete_unupdated_dimensions seconds
1305 if(unlikely((rd->last_collected_time.tv_sec + (rrd_delete_unupdated_dimensions * st->update_every)) < st->last_collected_time.tv_sec)) {
1306 info("Removing obsolete dimension '%s' (%s) of '%s' (%s).", rd->name, rd->id, st->name, st->id);
1308 if(unlikely(!last)) {
1309 st->dimensions = rd->next;
1311 rrddim_free(st, rd);
1312 rd = st->dimensions;
1316 last->next = rd->next;
1318 rrddim_free(st, rd);
1328 if(unlikely(!st->dimensions)) {
1329 info("Disabling chart %s (%s) since it does not have any dimensions", st->name, st->id);
1335 pthread_rwlock_unlock(&st->rwlock);
1337 if(unlikely(pthread_setcancelstate(oldstate, NULL) != 0))
1338 error("Cannot set pthread cancel state to RESTORE (%d).", oldstate);
1340 return(st->usec_since_last_update);