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 PTHREAD_RWLOCK_INITIALIZER
59 #define rrdset_index_add(st) avl_insert(&rrdset_root_index, (avl *)(st))
60 #define rrdset_index_del(st) avl_remove(&rrdset_root_index, (avl *)(st))
62 static RRDSET *rrdset_index_find(const char *id, uint32_t hash) {
63 RRDSET *result = NULL, tmp;
64 strncpy(tmp.id, id, RRD_ID_LENGTH_MAX);
65 tmp.id[RRD_ID_LENGTH_MAX] = '\0';
66 tmp.hash = (hash)?hash:simple_hash(tmp.id);
68 avl_search(&(rrdset_root_index), (avl *)&tmp, rrdset_iterator, (avl **)&result);
72 // ----------------------------------------------------------------------------
75 #define rrdset_from_avlname(avlname_ptr) ((RRDSET *)((avlname_ptr) - offsetof(RRDSET, avlname)))
77 static int rrdset_iterator_name(avl *a) { if(a) {}; return 0; }
79 static int rrdset_compare_name(void* a, void* b) {
80 RRDSET *A = rrdset_from_avlname(a);
81 RRDSET *B = rrdset_from_avlname(b);
83 // fprintf(stderr, "COMPARING: %s with %s\n", A->name, B->name);
85 if(A->hash_name < B->hash_name) return -1;
86 else if(A->hash_name > B->hash_name) return 1;
87 else return strcmp(A->name, B->name);
90 avl_tree rrdset_root_index_name = {
93 PTHREAD_RWLOCK_INITIALIZER
96 int rrdset_index_add_name(RRDSET *st) {
97 // fprintf(stderr, "ADDING: %s (name: %s)\n", st->id, st->name);
98 return avl_insert(&rrdset_root_index_name, (avl *)(&st->avlname));
101 #define rrdset_index_del_name(st) avl_remove(&rrdset_root_index_name, (avl *)(&st->avlname))
103 static RRDSET *rrdset_index_find_name(const char *name, uint32_t hash) {
107 tmp.hash_name = (hash)?hash:simple_hash(tmp.name);
109 // fprintf(stderr, "SEARCHING: %s\n", name);
110 avl_search(&(rrdset_root_index_name), (avl *)(&(tmp.avlname)), rrdset_iterator_name, (avl **)&result);
112 RRDSET *st = rrdset_from_avlname(result);
113 if(strcmp(st->magic, RRDSET_MAGIC))
114 error("Search for RRDSET %s returned an invalid RRDSET %s (name %s)", name, st->id, st->name);
116 // fprintf(stderr, "FOUND: %s\n", name);
117 return rrdset_from_avlname(result);
119 // fprintf(stderr, "NOT FOUND: %s\n", name);
124 // ----------------------------------------------------------------------------
127 static int rrddim_iterator(avl *a) { if(a) {}; return 0; }
129 static int rrddim_compare(void* a, void* b) {
130 if(((RRDDIM *)a)->hash < ((RRDDIM *)b)->hash) return -1;
131 else if(((RRDDIM *)a)->hash > ((RRDDIM *)b)->hash) return 1;
132 else return strcmp(((RRDDIM *)a)->id, ((RRDDIM *)b)->id);
135 #define rrddim_index_add(st, rd) avl_insert(&((st)->dimensions_index), (avl *)(rd))
136 #define rrddim_index_del(st,rd ) avl_remove(&((st)->dimensions_index), (avl *)(rd))
138 static RRDDIM *rrddim_index_find(RRDSET *st, const char *id, uint32_t hash) {
139 RRDDIM *result = NULL, tmp;
140 strncpy(tmp.id, id, RRD_ID_LENGTH_MAX);
141 tmp.id[RRD_ID_LENGTH_MAX] = '\0';
142 tmp.hash = (hash)?hash:simple_hash(tmp.id);
144 avl_search(&(st->dimensions_index), (avl *)&tmp, rrddim_iterator, (avl **)&result);
148 // ----------------------------------------------------------------------------
151 int rrdset_type_id(const char *name)
153 if(unlikely(strcmp(name, RRDSET_TYPE_AREA_NAME) == 0)) return RRDSET_TYPE_AREA;
154 else if(unlikely(strcmp(name, RRDSET_TYPE_STACKED_NAME) == 0)) return RRDSET_TYPE_STACKED;
155 else if(unlikely(strcmp(name, RRDSET_TYPE_LINE_NAME) == 0)) return RRDSET_TYPE_LINE;
156 return RRDSET_TYPE_LINE;
159 const char *rrdset_type_name(int chart_type)
161 static char line[] = RRDSET_TYPE_LINE_NAME;
162 static char area[] = RRDSET_TYPE_AREA_NAME;
163 static char stacked[] = RRDSET_TYPE_STACKED_NAME;
166 case RRDSET_TYPE_LINE:
169 case RRDSET_TYPE_AREA:
172 case RRDSET_TYPE_STACKED:
178 // ----------------------------------------------------------------------------
181 const char *rrd_memory_mode_name(int id)
183 static const char ram[] = RRD_MEMORY_MODE_RAM_NAME;
184 static const char map[] = RRD_MEMORY_MODE_MAP_NAME;
185 static const char save[] = RRD_MEMORY_MODE_SAVE_NAME;
188 case RRD_MEMORY_MODE_RAM:
191 case RRD_MEMORY_MODE_MAP:
194 case RRD_MEMORY_MODE_SAVE:
202 int rrd_memory_mode_id(const char *name)
204 if(unlikely(!strcmp(name, RRD_MEMORY_MODE_RAM_NAME)))
205 return RRD_MEMORY_MODE_RAM;
206 else if(unlikely(!strcmp(name, RRD_MEMORY_MODE_MAP_NAME)))
207 return RRD_MEMORY_MODE_MAP;
209 return RRD_MEMORY_MODE_SAVE;
212 // ----------------------------------------------------------------------------
215 int rrddim_algorithm_id(const char *name)
217 if(strcmp(name, RRDDIM_ABSOLUTE_NAME) == 0) return RRDDIM_ABSOLUTE;
218 if(strcmp(name, RRDDIM_INCREMENTAL_NAME) == 0) return RRDDIM_INCREMENTAL;
219 if(strcmp(name, RRDDIM_PCENT_OVER_ROW_TOTAL_NAME) == 0) return RRDDIM_PCENT_OVER_ROW_TOTAL;
220 if(strcmp(name, RRDDIM_PCENT_OVER_DIFF_TOTAL_NAME) == 0) return RRDDIM_PCENT_OVER_DIFF_TOTAL;
221 return RRDDIM_ABSOLUTE;
224 const char *rrddim_algorithm_name(int chart_type)
226 static char absolute[] = RRDDIM_ABSOLUTE_NAME;
227 static char incremental[] = RRDDIM_INCREMENTAL_NAME;
228 static char percentage_of_absolute_row[] = RRDDIM_PCENT_OVER_ROW_TOTAL_NAME;
229 static char percentage_of_incremental_row[] = RRDDIM_PCENT_OVER_DIFF_TOTAL_NAME;
232 case RRDDIM_ABSOLUTE:
235 case RRDDIM_INCREMENTAL:
238 case RRDDIM_PCENT_OVER_ROW_TOTAL:
239 return percentage_of_absolute_row;
241 case RRDDIM_PCENT_OVER_DIFF_TOTAL:
242 return percentage_of_incremental_row;
247 // ----------------------------------------------------------------------------
250 char *rrdset_strncpy_name(char *to, const char *from, int length)
253 for(i = 0; i < length && from[i] ;i++) {
254 if(from[i] == '.' || isalpha(from[i]) || isdigit(from[i])) to[i] = from[i];
257 if(i < length) to[i] = '\0';
258 to[length - 1] = '\0';
263 void rrdset_set_name(RRDSET *st, const char *name)
265 debug(D_RRD_CALLS, "rrdset_set_name() old: %s, new: %s", st->name, name);
267 if(st->name) rrdset_index_del_name(st);
269 char b[CONFIG_MAX_VALUE + 1];
270 char n[RRD_ID_LENGTH_MAX + 1];
272 snprintf(n, RRD_ID_LENGTH_MAX, "%s.%s", st->type, name);
273 rrdset_strncpy_name(b, n, CONFIG_MAX_VALUE);
274 st->name = config_get(st->id, "name", b);
275 st->hash_name = simple_hash(st->name);
277 rrdset_index_add_name(st);
280 // ----------------------------------------------------------------------------
283 char *rrdset_cache_dir(const char *id)
287 static char *cache_dir = NULL;
288 if(!cache_dir) cache_dir = config_get("global", "cache directory", CACHE_DIR);
290 char b[FILENAME_MAX + 1];
291 char n[FILENAME_MAX + 1];
292 rrdset_strncpy_name(b, id, FILENAME_MAX);
294 snprintf(n, FILENAME_MAX, "%s/%s", cache_dir, b);
295 ret = config_get(id, "cache directory", n);
297 if(rrd_memory_mode == RRD_MEMORY_MODE_MAP || rrd_memory_mode == RRD_MEMORY_MODE_SAVE) {
298 int r = mkdir(ret, 0775);
299 if(r != 0 && errno != EEXIST)
300 error("Cannot create directory '%s'", ret);
306 // ----------------------------------------------------------------------------
309 void rrdset_reset(RRDSET *st)
311 debug(D_RRD_CALLS, "rrdset_reset() %s", st->name);
313 st->last_collected_time.tv_sec = 0;
314 st->last_collected_time.tv_usec = 0;
315 st->last_updated.tv_sec = 0;
316 st->last_updated.tv_usec = 0;
317 st->current_entry = 0;
319 st->counter_done = 0;
322 for(rd = st->dimensions; rd ; rd = rd->next) {
323 rd->last_collected_time.tv_sec = 0;
324 rd->last_collected_time.tv_usec = 0;
326 bzero(rd->values, rd->entries * sizeof(storage_number));
330 RRDSET *rrdset_create(const char *type, const char *id, const char *name, const char *family, const char *title, const char *units, long priority, int update_every, int chart_type)
333 fatal("Cannot create rrd stats without an id.");
337 char fullid[RRD_ID_LENGTH_MAX + 1];
338 char fullfilename[FILENAME_MAX + 1];
341 snprintf(fullid, RRD_ID_LENGTH_MAX, "%s.%s", type, id);
343 long entries = config_get_number(fullid, "history", rrd_default_history_entries);
344 if(entries < 5) entries = config_set_number(fullid, "history", 5);
345 if(entries > RRD_HISTORY_ENTRIES_MAX) entries = config_set_number(fullid, "history", RRD_HISTORY_ENTRIES_MAX);
347 int enabled = config_get_boolean(fullid, "enabled", 1);
348 if(!enabled) entries = 5;
350 unsigned long size = sizeof(RRDSET);
351 char *cache_dir = rrdset_cache_dir(fullid);
353 debug(D_RRD_CALLS, "Creating RRD_STATS for '%s.%s'.", type, id);
355 snprintf(fullfilename, FILENAME_MAX, "%s/main.db", cache_dir);
356 if(rrd_memory_mode != RRD_MEMORY_MODE_RAM) st = (RRDSET *)mymmap(fullfilename, size, ((rrd_memory_mode == RRD_MEMORY_MODE_MAP)?MAP_SHARED:MAP_PRIVATE));
358 if(strcmp(st->magic, RRDSET_MAGIC) != 0) {
360 info("Initializing file %s.", fullfilename);
363 else if(strcmp(st->id, fullid) != 0) {
365 error("File %s contents are not for chart %s. Clearing it.", fullfilename, fullid);
370 else if(st->memsize != size || st->entries != entries) {
372 error("File %s does not have the desired size. Clearing it.", fullfilename);
375 else if(st->update_every != update_every) {
377 error("File %s does not have the desired update frequency. Clearing it.", fullfilename);
380 else if((time(NULL) - st->last_updated.tv_sec) > update_every * entries) {
382 error("File %s is too old. Clearing it.", fullfilename);
393 st->dimensions = NULL;
395 st->mapped = rrd_memory_mode;
398 st = calloc(1, size);
400 fatal("Cannot allocate memory for RRD_STATS %s.%s", type, id);
403 st->mapped = RRD_MEMORY_MODE_RAM;
406 st->entries = entries;
407 st->update_every = update_every;
409 strcpy(st->cache_filename, fullfilename);
410 strcpy(st->magic, RRDSET_MAGIC);
412 strcpy(st->id, fullid);
413 st->hash = simple_hash(st->id);
415 st->cache_dir = cache_dir;
417 st->family = config_get(st->id, "family", family?family:st->id);
418 st->units = config_get(st->id, "units", units?units:"");
419 st->type = config_get(st->id, "type", type);
420 st->chart_type = rrdset_type_id(config_get(st->id, "chart type", rrdset_type_name(chart_type)));
422 st->priority = config_get_number(st->id, "priority", priority);
423 st->enabled = enabled;
428 st->last_collected_time.tv_sec = 0;
429 st->last_collected_time.tv_usec = 0;
430 st->counter_done = 0;
432 st->gap_when_lost_iterations_above = config_get_number(st->id, "gap when lost iterations above", RRD_DEFAULT_GAP_INTERPOLATIONS) + 2;
434 avl_init(&st->dimensions_index, rrddim_compare);
436 pthread_rwlock_init(&st->rwlock, NULL);
437 pthread_rwlock_wrlock(&rrdset_root_rwlock);
439 if(name && *name) rrdset_set_name(st, name);
440 else rrdset_set_name(st, id);
443 char varvalue[CONFIG_MAX_VALUE + 1];
444 snprintf(varvalue, CONFIG_MAX_VALUE, "%s (%s)", title?title:"", st->name);
445 st->title = config_get(st->id, "title", varvalue);
448 st->next = rrdset_root;
451 rrdset_index_add(st);
453 pthread_rwlock_unlock(&rrdset_root_rwlock);
458 RRDDIM *rrddim_add(RRDSET *st, const char *id, const char *name, long multiplier, long divisor, int algorithm)
460 char filename[FILENAME_MAX + 1];
461 char fullfilename[FILENAME_MAX + 1];
463 char varname[CONFIG_MAX_NAME + 1];
465 unsigned long size = sizeof(RRDDIM) + (st->entries * sizeof(storage_number));
467 debug(D_RRD_CALLS, "Adding dimension '%s/%s'.", st->id, id);
469 rrdset_strncpy_name(filename, id, FILENAME_MAX);
470 snprintf(fullfilename, FILENAME_MAX, "%s/%s.db", st->cache_dir, filename);
471 if(rrd_memory_mode != RRD_MEMORY_MODE_RAM) rd = (RRDDIM *)mymmap(fullfilename, size, ((rrd_memory_mode == RRD_MEMORY_MODE_MAP)?MAP_SHARED:MAP_PRIVATE));
474 gettimeofday(&now, NULL);
476 if(strcmp(rd->magic, RRDDIMENSION_MAGIC) != 0) {
478 info("Initializing file %s.", fullfilename);
481 else if(rd->memsize != size) {
483 error("File %s does not have the desired size. Clearing it.", fullfilename);
486 else if(rd->multiplier != multiplier) {
488 error("File %s does not have the same multiplier. Clearing it.", fullfilename);
491 else if(rd->divisor != divisor) {
493 error("File %s does not have the same divisor. Clearing it.", fullfilename);
496 else if(rd->algorithm != algorithm) {
498 error("File %s does not have the same algorithm. Clearing it.", fullfilename);
501 else if(rd->update_every != st->update_every) {
503 error("File %s does not have the same refresh frequency. Clearing it.", fullfilename);
506 else if(usecdiff(&now, &rd->last_collected_time) > (rd->entries * rd->update_every * 1000000ULL)) {
508 error("File %s is too old. Clearing it.", fullfilename);
511 else if(strcmp(rd->id, id) != 0) {
513 error("File %s contents are not for dimension %s. Clearing it.", fullfilename, id);
521 // we have a file mapped for rd
522 rd->mapped = rrd_memory_mode;
523 rd->flags = 0x00000000;
528 // if we didn't manage to get a mmap'd dimension, just create one
530 rd = calloc(1, size);
532 fatal("Cannot allocate RRD_DIMENSION %s/%s.", st->id, id);
536 rd->mapped = RRD_MEMORY_MODE_RAM;
540 strcpy(rd->magic, RRDDIMENSION_MAGIC);
541 strcpy(rd->cache_filename, fullfilename);
542 strncpy(rd->id, id, RRD_ID_LENGTH_MAX);
543 rd->hash = simple_hash(rd->id);
545 snprintf(varname, CONFIG_MAX_NAME, "dim %s name", rd->id);
546 rd->name = config_get(st->id, varname, (name && *name)?name:rd->id);
548 snprintf(varname, CONFIG_MAX_NAME, "dim %s algorithm", rd->id);
549 rd->algorithm = rrddim_algorithm_id(config_get(st->id, varname, rrddim_algorithm_name(algorithm)));
551 snprintf(varname, CONFIG_MAX_NAME, "dim %s multiplier", rd->id);
552 rd->multiplier = config_get_number(st->id, varname, multiplier);
554 snprintf(varname, CONFIG_MAX_NAME, "dim %s divisor", rd->id);
555 rd->divisor = config_get_number(st->id, varname, divisor);
556 if(!rd->divisor) rd->divisor = 1;
558 rd->entries = st->entries;
559 rd->update_every = st->update_every;
561 // prevent incremental calculation spikes
564 // append this dimension
568 RRDDIM *td = st->dimensions;
569 for(; td->next; td = td->next) ;
573 rrddim_index_add(st, rd);
578 void rrddim_set_name(RRDSET *st, RRDDIM *rd, const char *name)
580 debug(D_RRD_CALLS, "rrddim_set_name() %s.%s", st->name, rd->name);
582 char varname[CONFIG_MAX_NAME + 1];
583 snprintf(varname, CONFIG_MAX_NAME, "dim %s name", rd->id);
584 config_get(st->id, varname, name);
587 void rrddim_free(RRDSET *st, RRDDIM *rd)
589 debug(D_RRD_CALLS, "rrddim_free() %s.%s", st->name, rd->name);
591 RRDDIM *i = st->dimensions, *last = NULL;
592 for(i = st->dimensions; i && i != rd ; i = i->next) last = i;
595 error("Request to free dimension %s.%s but it is not linked.", st->id, rd->name);
599 if(last) last = i->next;
600 else st->dimensions = i->next;
602 rrddim_index_del(st, rd);
604 // free(rd->annotations);
605 if(rd->mapped == RRD_MEMORY_MODE_SAVE) {
606 debug(D_RRD_CALLS, "Saving dimension '%s' to '%s'.", rd->name, rd->cache_filename);
607 savememory(rd->cache_filename, rd, rd->memsize);
609 debug(D_RRD_CALLS, "Unmapping dimension '%s'.", rd->name);
610 munmap(rd, rd->memsize);
612 else if(rd->mapped == RRD_MEMORY_MODE_MAP) {
613 debug(D_RRD_CALLS, "Unmapping dimension '%s'.", rd->name);
614 munmap(rd, rd->memsize);
617 debug(D_RRD_CALLS, "Removing dimension '%s'.", rd->name);
622 void rrdset_free_all(void)
624 info("Freeing all memory...");
627 for(st = rrdset_root; st ;) {
628 RRDSET *next = st->next;
630 while(st->dimensions)
631 rrddim_free(st, st->dimensions);
633 rrdset_index_del(st);
635 if(st->mapped == RRD_MEMORY_MODE_SAVE) {
636 debug(D_RRD_CALLS, "Saving stats '%s' to '%s'.", st->name, st->cache_filename);
637 savememory(st->cache_filename, st, st->memsize);
639 debug(D_RRD_CALLS, "Unmapping stats '%s'.", st->name);
640 munmap(st, st->memsize);
642 else if(st->mapped == RRD_MEMORY_MODE_MAP) {
643 debug(D_RRD_CALLS, "Unmapping stats '%s'.", st->name);
644 munmap(st, st->memsize);
653 info("Memory cleanup completed...");
656 void rrdset_save_all(void)
658 debug(D_RRD_CALLS, "rrdset_save_all()");
663 pthread_rwlock_wrlock(&rrdset_root_rwlock);
664 for(st = rrdset_root; st ; st = st->next) {
665 pthread_rwlock_wrlock(&st->rwlock);
667 if(st->mapped == RRD_MEMORY_MODE_SAVE) {
668 debug(D_RRD_CALLS, "Saving stats '%s' to '%s'.", st->name, st->cache_filename);
669 savememory(st->cache_filename, st, st->memsize);
672 for(rd = st->dimensions; rd ; rd = rd->next) {
673 if(likely(rd->mapped == RRD_MEMORY_MODE_SAVE)) {
674 debug(D_RRD_CALLS, "Saving dimension '%s' to '%s'.", rd->name, rd->cache_filename);
675 savememory(rd->cache_filename, rd, rd->memsize);
679 pthread_rwlock_unlock(&st->rwlock);
681 pthread_rwlock_unlock(&rrdset_root_rwlock);
685 RRDSET *rrdset_find(const char *id)
687 debug(D_RRD_CALLS, "rrdset_find() for chart %s", id);
689 pthread_rwlock_rdlock(&rrdset_root_rwlock);
690 RRDSET *st = rrdset_index_find(id, 0);
691 pthread_rwlock_unlock(&rrdset_root_rwlock);
696 RRDSET *rrdset_find_bytype(const char *type, const char *id)
698 debug(D_RRD_CALLS, "rrdset_find_bytype() for chart %s.%s", type, id);
700 char buf[RRD_ID_LENGTH_MAX + 1];
702 strncpy(buf, type, RRD_ID_LENGTH_MAX - 1);
703 buf[RRD_ID_LENGTH_MAX - 1] = '\0';
705 int len = strlen(buf);
706 strncpy(&buf[len], id, RRD_ID_LENGTH_MAX - len);
707 buf[RRD_ID_LENGTH_MAX] = '\0';
709 return(rrdset_find(buf));
712 RRDSET *rrdset_find_byname(const char *name)
714 debug(D_RRD_CALLS, "rrdset_find_byname() for chart %s", name);
716 pthread_rwlock_rdlock(&rrdset_root_rwlock);
717 RRDSET *st = rrdset_index_find_name(name, 0);
718 pthread_rwlock_unlock(&rrdset_root_rwlock);
723 RRDDIM *rrddim_find(RRDSET *st, const char *id)
725 debug(D_RRD_CALLS, "rrddim_find() for chart %s, dimension %s", st->name, id);
727 return rrddim_index_find(st, id, 0);
730 int rrddim_hide(RRDSET *st, const char *id)
732 debug(D_RRD_CALLS, "rrddim_hide() for chart %s, dimension %s", st->name, id);
734 RRDDIM *rd = rrddim_find(st, id);
736 error("Cannot find dimension with id '%s' on stats '%s' (%s).", id, st->name, st->id);
740 rd->flags |= RRDDIM_FLAG_HIDDEN;
744 int rrddim_unhide(RRDSET *st, const char *id)
746 debug(D_RRD_CALLS, "rrddim_unhide() for chart %s, dimension %s", st->name, id);
748 RRDDIM *rd = rrddim_find(st, id);
750 error("Cannot find dimension with id '%s' on stats '%s' (%s).", id, st->name, st->id);
754 if(rd->flags & RRDDIM_FLAG_HIDDEN) rd->flags ^= RRDDIM_FLAG_HIDDEN;
758 collected_number rrddim_set_by_pointer(RRDSET *st, RRDDIM *rd, collected_number value)
760 debug(D_RRD_CALLS, "rrddim_set_by_pointer() for chart %s, dimension %s, value " COLLECTED_NUMBER_FORMAT, st->name, rd->name, value);
762 gettimeofday(&rd->last_collected_time, NULL);
763 rd->collected_value = value;
767 return rd->last_collected_value;
770 collected_number rrddim_set(RRDSET *st, const char *id, collected_number value)
772 RRDDIM *rd = rrddim_find(st, id);
774 error("Cannot find dimension with id '%s' on stats '%s' (%s).", id, st->name, st->id);
778 return rrddim_set_by_pointer(st, rd, value);
781 void rrdset_next_usec(RRDSET *st, unsigned long long microseconds)
783 if(!microseconds) rrdset_next(st);
785 debug(D_RRD_CALLS, "rrdset_next_usec() for chart %s with microseconds %llu", st->name, microseconds);
787 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s: NEXT: %llu microseconds", st->name, microseconds);
788 st->usec_since_last_update = microseconds;
792 void rrdset_next(RRDSET *st)
794 unsigned long long microseconds = 0;
796 if(likely(st->last_collected_time.tv_sec)) {
798 gettimeofday(&now, NULL);
799 microseconds = usecdiff(&now, &st->last_collected_time);
802 rrdset_next_usec(st, microseconds);
805 void rrdset_next_plugins(RRDSET *st)
810 unsigned long long rrdset_done(RRDSET *st)
812 debug(D_RRD_CALLS, "rrdset_done() for chart %s", st->name);
815 int oldstate, store_this_entry = 1;
817 if(unlikely(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate) != 0))
818 error("Cannot set pthread cancel state to DISABLE.");
820 // a read lock is OK here
821 pthread_rwlock_rdlock(&st->rwlock);
823 // enable the chart, if it was disabled
826 // check if the chart has a long time to be refreshed
827 if(unlikely(st->usec_since_last_update > st->entries * st->update_every * 1000000ULL)) {
828 info("%s: took too long to be updated (%0.3Lf secs). Reseting it.", st->name, (long double)(st->usec_since_last_update / 1000000.0));
830 st->usec_since_last_update = st->update_every * 1000000ULL;
832 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s: microseconds since last update: %llu", st->name, st->usec_since_last_update);
834 // set last_collected_time
835 if(unlikely(!st->last_collected_time.tv_sec)) {
836 // it is the first entry
837 // set the last_collected_time to now
838 gettimeofday(&st->last_collected_time, NULL);
840 // the first entry should not be stored
841 store_this_entry = 0;
843 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s: initializing last_collected to now. Will not store the next entry.", st->name);
846 // it is not the first entry
847 // calculate the proper last_collected_time, using usec_since_last_update
848 unsigned long long ut = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec + st->usec_since_last_update;
849 st->last_collected_time.tv_sec = ut / 1000000ULL;
850 st->last_collected_time.tv_usec = ut % 1000000ULL;
853 // if this set has not been updated in the past
854 // we fake the last_update time to be = now - usec_since_last_update
855 if(unlikely(!st->last_updated.tv_sec)) {
856 // it has never been updated before
857 // set a fake last_updated, in the past using usec_since_last_update
858 unsigned long long ut = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec - st->usec_since_last_update;
859 st->last_updated.tv_sec = ut / 1000000ULL;
860 st->last_updated.tv_usec = ut % 1000000ULL;
862 // the first entry should not be stored
863 store_this_entry = 0;
865 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);
868 // check if we will re-write the entire data set
869 if(unlikely(usecdiff(&st->last_collected_time, &st->last_updated) > st->update_every * st->entries * 1000000ULL)) {
870 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);
873 st->usec_since_last_update = st->update_every * 1000000ULL;
875 gettimeofday(&st->last_collected_time, NULL);
877 unsigned long long ut = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec - st->usec_since_last_update;
878 st->last_updated.tv_sec = ut / 1000000ULL;
879 st->last_updated.tv_usec = ut % 1000000ULL;
881 // the first entry should not be stored
882 store_this_entry = 0;
885 // these are the 3 variables that will help us in interpolation
886 // last_ut = the last time we added a value to the storage
887 // now_ut = the time the current value is taken at
888 // next_ut = the time of the next interpolation point
889 unsigned long long last_ut = st->last_updated.tv_sec * 1000000ULL + st->last_updated.tv_usec;
890 unsigned long long now_ut = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec;
891 unsigned long long next_ut = (st->last_updated.tv_sec + st->update_every) * 1000000ULL;
893 if(unlikely(st->debug)) {
894 debug(D_RRD_STATS, "%s: last ut = %0.3Lf (last updated time)", st->name, (long double)last_ut/1000000.0);
895 debug(D_RRD_STATS, "%s: now ut = %0.3Lf (current update time)", st->name, (long double)now_ut/1000000.0);
896 debug(D_RRD_STATS, "%s: next ut = %0.3Lf (next interpolation point)", st->name, (long double)next_ut/1000000.0);
899 if(unlikely(!st->counter_done)) {
900 store_this_entry = 0;
901 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s: Will not store the next entry.", st->name);
905 // calculate totals and count the dimensions
907 st->collected_total = 0;
908 for( rd = st->dimensions, dimensions = 0 ; likely(rd) ; rd = rd->next, dimensions++ )
909 st->collected_total += rd->collected_value;
911 uint32_t storage_flags = SN_EXISTS;
913 // process all dimensions to calculate their values
914 // based on the collected figures only
915 // at this stage we do not interpolate anything
916 for( rd = st->dimensions ; likely(rd) ; rd = rd->next ) {
918 if(unlikely(!rd->updated || rd->counter <= 1)) {
919 rd->calculated_value = 0;
923 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s/%s: "
924 " last_collected_value = " COLLECTED_NUMBER_FORMAT
925 " collected_value = " COLLECTED_NUMBER_FORMAT
926 " last_calculated_value = " CALCULATED_NUMBER_FORMAT
927 " calculated_value = " CALCULATED_NUMBER_FORMAT
929 , rd->last_collected_value
930 , rd->collected_value
931 , rd->last_calculated_value
932 , rd->calculated_value
935 switch(rd->algorithm) {
936 case RRDDIM_PCENT_OVER_DIFF_TOTAL:
937 // the percentage of the current increment
938 // over the increment of all dimensions together
939 if(unlikely(st->collected_total == st->last_collected_total)) rd->calculated_value = rd->last_calculated_value;
940 else rd->calculated_value =
941 (calculated_number)100
942 * (calculated_number)(rd->collected_value - rd->last_collected_value)
943 / (calculated_number)(st->collected_total - st->last_collected_total);
945 if(unlikely(st->debug))
946 debug(D_RRD_STATS, "%s/%s: CALC PCENT-DIFF "
947 CALCULATED_NUMBER_FORMAT " = 100"
948 " * (" COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT ")"
949 " / (" COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT ")"
951 , rd->calculated_value
952 , rd->collected_value, rd->last_collected_value
953 , st->collected_total, st->last_collected_total
957 case RRDDIM_PCENT_OVER_ROW_TOTAL:
958 if(unlikely(!st->collected_total)) rd->calculated_value = 0;
960 // the percentage of the current value
961 // over the total of all dimensions
962 rd->calculated_value =
963 (calculated_number)100
964 * (calculated_number)rd->collected_value
965 / (calculated_number)st->collected_total;
967 if(unlikely(st->debug))
968 debug(D_RRD_STATS, "%s/%s: CALC PCENT-ROW "
969 CALCULATED_NUMBER_FORMAT " = 100"
970 " * " COLLECTED_NUMBER_FORMAT
971 " / " COLLECTED_NUMBER_FORMAT
973 , rd->calculated_value
974 , rd->collected_value
975 , st->collected_total
979 case RRDDIM_INCREMENTAL:
980 // if the new is smaller than the old (an overflow, or reset), set the old equal to the new
981 // to reset the calculation (it will give zero as the calculation for this second)
982 if(unlikely(rd->last_collected_value > rd->collected_value)) {
983 debug(D_RRD_STATS, "%s.%s: RESET or OVERFLOW. Last collected value = " COLLECTED_NUMBER_FORMAT ", current = " COLLECTED_NUMBER_FORMAT
985 , rd->last_collected_value
986 , rd->collected_value);
987 if(!(rd->flags & RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS)) storage_flags = SN_EXISTS_RESET;
988 rd->last_collected_value = rd->collected_value;
991 rd->calculated_value += (calculated_number)(rd->collected_value - rd->last_collected_value)
992 * (calculated_number)rd->multiplier
993 / (calculated_number)rd->divisor;
995 if(unlikely(st->debug))
996 debug(D_RRD_STATS, "%s/%s: CALC INC "
997 CALCULATED_NUMBER_FORMAT " += ("
998 COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT
1003 , rd->calculated_value
1004 , rd->collected_value, rd->last_collected_value
1005 , (calculated_number)rd->multiplier
1006 , (calculated_number)rd->divisor
1010 case RRDDIM_ABSOLUTE:
1011 rd->calculated_value = (calculated_number)rd->collected_value
1012 * (calculated_number)rd->multiplier
1013 / (calculated_number)rd->divisor;
1015 if(unlikely(st->debug))
1016 debug(D_RRD_STATS, "%s/%s: CALC ABS/ABS-NO-IN "
1017 CALCULATED_NUMBER_FORMAT " = "
1018 COLLECTED_NUMBER_FORMAT
1022 , rd->calculated_value
1023 , rd->collected_value
1024 , (calculated_number)rd->multiplier
1025 , (calculated_number)rd->divisor
1030 // make the default zero, to make sure
1031 // it gets noticed when we add new types
1032 rd->calculated_value = 0;
1034 if(unlikely(st->debug))
1035 debug(D_RRD_STATS, "%s/%s: CALC "
1036 CALCULATED_NUMBER_FORMAT " = 0"
1038 , rd->calculated_value
1044 // at this point we have all the calculated values ready
1045 // it is now time to interpolate values on a second boundary
1047 unsigned long long first_ut = last_ut;
1048 int iterations = (now_ut - last_ut) / (st->update_every * 1000000ULL);
1050 for( ; likely(next_ut <= now_ut) ; next_ut += st->update_every * 1000000ULL, iterations-- ) {
1051 if(iterations < 0) error("iterations calculation wrapped!");
1053 if(unlikely(st->debug)) {
1054 debug(D_RRD_STATS, "%s: last ut = %0.3Lf (last updated time)", st->name, (long double)last_ut/1000000.0);
1055 debug(D_RRD_STATS, "%s: next ut = %0.3Lf (next interpolation point)", st->name, (long double)next_ut/1000000.0);
1058 st->last_updated.tv_sec = next_ut / 1000000ULL;
1059 st->last_updated.tv_usec = 0;
1061 for( rd = st->dimensions ; likely(rd) ; rd = rd->next ) {
1062 calculated_number new_value;
1064 switch(rd->algorithm) {
1065 case RRDDIM_INCREMENTAL:
1066 new_value = (calculated_number)
1067 ( rd->calculated_value
1068 * (calculated_number)(next_ut - last_ut)
1069 / (calculated_number)(now_ut - last_ut)
1072 if(unlikely(st->debug))
1073 debug(D_RRD_STATS, "%s/%s: CALC2 INC "
1074 CALCULATED_NUMBER_FORMAT " = "
1075 CALCULATED_NUMBER_FORMAT
1080 , rd->calculated_value
1081 , (unsigned long long)(next_ut - last_ut)
1082 , (unsigned long long)(now_ut - last_ut)
1085 rd->calculated_value -= new_value;
1086 new_value /= (calculated_number)st->update_every;
1089 case RRDDIM_ABSOLUTE:
1090 case RRDDIM_PCENT_OVER_ROW_TOTAL:
1091 case RRDDIM_PCENT_OVER_DIFF_TOTAL:
1093 new_value = (calculated_number)
1094 ( ( (rd->calculated_value - rd->last_calculated_value)
1095 * (calculated_number)(next_ut - first_ut)
1096 / (calculated_number)(now_ut - first_ut)
1098 + rd->last_calculated_value
1101 if(unlikely(st->debug))
1102 debug(D_RRD_STATS, "%s/%s: CALC2 DEF "
1103 CALCULATED_NUMBER_FORMAT " = ((("
1104 "(" CALCULATED_NUMBER_FORMAT " - " CALCULATED_NUMBER_FORMAT ")"
1106 " / %llu) + " CALCULATED_NUMBER_FORMAT
1109 , rd->calculated_value, rd->last_calculated_value
1110 , (next_ut - first_ut)
1111 , (now_ut - first_ut), rd->last_calculated_value
1114 if(likely(next_ut + st->update_every * 1000000ULL > now_ut)) rd->calculated_value = new_value;
1118 if(unlikely(!store_this_entry)) {
1119 store_this_entry = 1;
1123 if(likely(rd->updated && rd->counter > 1 && iterations < st->gap_when_lost_iterations_above)) {
1124 rd->values[st->current_entry] = pack_storage_number(new_value, storage_flags );
1126 if(unlikely(st->debug))
1127 debug(D_RRD_STATS, "%s/%s: STORE[%ld] "
1128 CALCULATED_NUMBER_FORMAT " = " CALCULATED_NUMBER_FORMAT
1131 , unpack_storage_number(rd->values[st->current_entry]), new_value
1135 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s/%s: STORE[%ld] = NON EXISTING "
1139 rd->values[st->current_entry] = pack_storage_number(0, SN_NOT_EXISTS);
1142 if(unlikely(st->debug)) {
1143 calculated_number t1 = new_value * (calculated_number)rd->multiplier / (calculated_number)rd->divisor;
1144 calculated_number t2 = unpack_storage_number(rd->values[st->current_entry]);
1145 calculated_number accuracy = accuracy_loss(t1, t2);
1146 debug(D_RRD_STATS, "%s/%s: UNPACK[%ld] = " CALCULATED_NUMBER_FORMAT " FLAGS=0x%08x (original = " CALCULATED_NUMBER_FORMAT ", accuracy loss = " CALCULATED_NUMBER_FORMAT "%%%s)"
1150 , get_storage_number_flags(rd->values[st->current_entry])
1153 , (accuracy > ACCURACY_LOSS) ? " **TOO BIG** " : ""
1156 rd->collected_volume += t1;
1157 rd->stored_volume += t2;
1158 accuracy = accuracy_loss(rd->collected_volume, rd->stored_volume);
1159 debug(D_RRD_STATS, "%s/%s: VOLUME[%ld] = " CALCULATED_NUMBER_FORMAT ", calculated = " CALCULATED_NUMBER_FORMAT ", accuracy loss = " CALCULATED_NUMBER_FORMAT "%%%s"
1163 , rd->collected_volume
1165 , (accuracy > ACCURACY_LOSS) ? " **TOO BIG** " : ""
1170 // reset the storage flags for the next point, if any;
1171 storage_flags = SN_EXISTS;
1174 st->current_entry = ((st->current_entry + 1) >= st->entries) ? 0 : st->current_entry + 1;
1178 for( rd = st->dimensions; likely(rd) ; rd = rd->next ) {
1179 if(unlikely(!rd->updated)) continue;
1180 rd->last_collected_value = rd->collected_value;
1181 rd->last_calculated_value = rd->calculated_value;
1182 rd->collected_value = 0;
1185 // if this is the first entry of incremental dimensions
1186 // we have to set the first calculated_value to zero
1187 // to eliminate the first spike
1188 if(unlikely(st->counter_done == 1)) switch(rd->algorithm) {
1189 case RRDDIM_PCENT_OVER_DIFF_TOTAL:
1190 case RRDDIM_INCREMENTAL:
1191 rd->calculated_value = 0;
1192 // the next time, a new incremental total will be calculated
1196 st->last_collected_total = st->collected_total;
1198 // ALL DONE ABOUT THE DATA UPDATE
1199 // --------------------------------------------------------------------
1201 // find if there are any obsolete dimensions (not updated recently)
1202 if(unlikely(rrd_delete_unupdated_dimensions)) {
1204 for( rd = st->dimensions; likely(rd) ; rd = rd->next )
1205 if((rd->last_collected_time.tv_sec + (rrd_delete_unupdated_dimensions * st->update_every)) < st->last_collected_time.tv_sec)
1209 // there is dimension to free
1210 // upgrade our read lock to a write lock
1211 pthread_rwlock_unlock(&st->rwlock);
1212 pthread_rwlock_wrlock(&st->rwlock);
1214 for( rd = st->dimensions, last = NULL ; likely(rd) ; ) {
1215 // remove it only it is not updated in rrd_delete_unupdated_dimensions seconds
1217 if(unlikely((rd->last_collected_time.tv_sec + (rrd_delete_unupdated_dimensions * st->update_every)) < st->last_collected_time.tv_sec)) {
1218 info("Removing obsolete dimension '%s' (%s) of '%s' (%s).", rd->name, rd->id, st->name, st->id);
1220 if(unlikely(!last)) {
1221 st->dimensions = rd->next;
1223 rrddim_free(st, rd);
1224 rd = st->dimensions;
1228 last->next = rd->next;
1230 rrddim_free(st, rd);
1240 if(unlikely(!st->dimensions)) {
1241 info("Disabling chart %s (%s) since it does not have any dimensions", st->name, st->id);
1247 pthread_rwlock_unlock(&st->rwlock);
1249 if(unlikely(pthread_setcancelstate(oldstate, NULL) != 0))
1250 error("Cannot set pthread cancel state to RESTORE (%d).", oldstate);
1252 return(st->usec_since_last_update);