11 #include <sys/types.h>
20 #define RRD_DEFAULT_GAP_INTERPOLATIONS 10
22 // ----------------------------------------------------------------------------
25 int rrd_delete_unupdated_dimensions = (86400 * 7);
27 int rrd_update_every = UPDATE_EVERY;
28 int rrd_default_history_entries = RRD_DEFAULT_HISTORY_ENTRIES;
30 RRDSET *rrdset_root = NULL;
31 pthread_rwlock_t rrdset_root_rwlock = PTHREAD_RWLOCK_INITIALIZER;
33 int rrd_memory_mode = RRD_MEMORY_MODE_SAVE;
36 // ----------------------------------------------------------------------------
39 static int rrdset_iterator(avl *a) { if(a) {}; return 0; }
41 static int rrdset_compare(void* a, void* b) {
42 if(((RRDSET *)a)->hash < ((RRDSET *)b)->hash) return -1;
43 else if(((RRDSET *)a)->hash > ((RRDSET *)b)->hash) return 1;
44 else return strcmp(((RRDSET *)a)->id, ((RRDSET *)b)->id);
47 avl_tree rrdset_root_index = {
52 #define rrdset_index_add(st) avl_insert(&rrdset_root_index, (avl *)(st))
53 #define rrdset_index_del(st) avl_remove(&rrdset_root_index, (avl *)(st))
55 static RRDSET *rrdset_index_find(const char *id, uint32_t hash) {
56 RRDSET *result = NULL, tmp;
57 strncpy(tmp.id, id, RRD_ID_LENGTH_MAX);
58 tmp.id[RRD_ID_LENGTH_MAX] = '\0';
59 tmp.hash = (hash)?hash:simple_hash(tmp.id);
61 avl_search(&(rrdset_root_index), (avl *)&tmp, rrdset_iterator, (avl **)&result);
65 // ----------------------------------------------------------------------------
68 #define rrdset_from_avlname(avlname_ptr) ((RRDSET *)((avlname_ptr) - offsetof(RRDSET, avlname)))
70 static int rrdset_iterator_name(avl *a) { if(a) {}; return 0; }
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 rrdset_root_index_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(&rrdset_root_index_name, (avl *)(&st->avlname));
93 #define rrdset_index_del_name(st) avl_remove(&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 avl_search(&(rrdset_root_index_name), (avl *)(&(tmp.avlname)), rrdset_iterator_name, (avl **)&result);
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_iterator(avl *a) { if(a) {}; return 0; }
121 static int rrddim_compare(void* a, void* b) {
122 if(((RRDDIM *)a)->hash < ((RRDDIM *)b)->hash) return -1;
123 else if(((RRDDIM *)a)->hash > ((RRDDIM *)b)->hash) return 1;
124 else return strcmp(((RRDDIM *)a)->id, ((RRDDIM *)b)->id);
127 #define rrddim_index_add(st, rd) avl_insert(&((st)->dimensions_index), (avl *)(rd))
128 #define rrddim_index_del(st,rd ) avl_remove(&((st)->dimensions_index), (avl *)(rd))
130 static RRDDIM *rrddim_index_find(RRDSET *st, const char *id, uint32_t hash) {
131 RRDDIM *result = NULL, tmp;
132 strncpy(tmp.id, id, RRD_ID_LENGTH_MAX);
133 tmp.id[RRD_ID_LENGTH_MAX] = '\0';
134 tmp.hash = (hash)?hash:simple_hash(tmp.id);
136 avl_search(&(st->dimensions_index), (avl *)&tmp, rrddim_iterator, (avl **)&result);
140 // ----------------------------------------------------------------------------
143 int rrdset_type_id(const char *name)
145 if(strcmp(name, RRDSET_TYPE_AREA_NAME) == 0) return RRDSET_TYPE_AREA;
146 if(strcmp(name, RRDSET_TYPE_STACKED_NAME) == 0) return RRDSET_TYPE_STACKED;
147 if(strcmp(name, RRDSET_TYPE_LINE_NAME) == 0) return RRDSET_TYPE_LINE;
148 return RRDSET_TYPE_LINE;
151 const char *rrdset_type_name(int chart_type)
153 static char line[] = RRDSET_TYPE_LINE_NAME;
154 static char area[] = RRDSET_TYPE_AREA_NAME;
155 static char stacked[] = RRDSET_TYPE_STACKED_NAME;
158 case RRDSET_TYPE_LINE:
161 case RRDSET_TYPE_AREA:
164 case RRDSET_TYPE_STACKED:
170 // ----------------------------------------------------------------------------
173 const char *rrd_memory_mode_name(int id)
175 static const char ram[] = RRD_MEMORY_MODE_RAM_NAME;
176 static const char map[] = RRD_MEMORY_MODE_MAP_NAME;
177 static const char save[] = RRD_MEMORY_MODE_SAVE_NAME;
180 case RRD_MEMORY_MODE_RAM:
183 case RRD_MEMORY_MODE_MAP:
186 case RRD_MEMORY_MODE_SAVE:
194 int rrd_memory_mode_id(const char *name)
196 if(!strcmp(name, RRD_MEMORY_MODE_RAM_NAME))
197 return RRD_MEMORY_MODE_RAM;
198 else if(!strcmp(name, RRD_MEMORY_MODE_MAP_NAME))
199 return RRD_MEMORY_MODE_MAP;
201 return RRD_MEMORY_MODE_SAVE;
204 // ----------------------------------------------------------------------------
207 int rrddim_algorithm_id(const char *name)
209 if(strcmp(name, RRDDIM_ABSOLUTE_NAME) == 0) return RRDDIM_ABSOLUTE;
210 if(strcmp(name, RRDDIM_INCREMENTAL_NAME) == 0) return RRDDIM_INCREMENTAL;
211 if(strcmp(name, RRDDIM_PCENT_OVER_ROW_TOTAL_NAME) == 0) return RRDDIM_PCENT_OVER_ROW_TOTAL;
212 if(strcmp(name, RRDDIM_PCENT_OVER_DIFF_TOTAL_NAME) == 0) return RRDDIM_PCENT_OVER_DIFF_TOTAL;
213 return RRDDIM_ABSOLUTE;
216 const char *rrddim_algorithm_name(int chart_type)
218 static char absolute[] = RRDDIM_ABSOLUTE_NAME;
219 static char incremental[] = RRDDIM_INCREMENTAL_NAME;
220 static char percentage_of_absolute_row[] = RRDDIM_PCENT_OVER_ROW_TOTAL_NAME;
221 static char percentage_of_incremental_row[] = RRDDIM_PCENT_OVER_DIFF_TOTAL_NAME;
224 case RRDDIM_ABSOLUTE:
227 case RRDDIM_INCREMENTAL:
230 case RRDDIM_PCENT_OVER_ROW_TOTAL:
231 return percentage_of_absolute_row;
233 case RRDDIM_PCENT_OVER_DIFF_TOTAL:
234 return percentage_of_incremental_row;
239 // ----------------------------------------------------------------------------
242 char *rrdset_strncpy_name(char *to, const char *from, int length)
245 for(i = 0; i < length && from[i] ;i++) {
246 if(from[i] == '.' || isalpha(from[i]) || isdigit(from[i])) to[i] = from[i];
249 if(i < length) to[i] = '\0';
250 to[length - 1] = '\0';
255 void rrdset_set_name(RRDSET *st, const char *name)
257 debug(D_RRD_CALLS, "rrdset_set_name() old: %s, new: %s", st->name, name);
259 if(st->name) rrdset_index_del_name(st);
261 char b[CONFIG_MAX_VALUE + 1];
262 char n[RRD_ID_LENGTH_MAX + 1];
264 snprintf(n, RRD_ID_LENGTH_MAX, "%s.%s", st->type, name);
265 rrdset_strncpy_name(b, n, CONFIG_MAX_VALUE);
266 st->name = config_get(st->id, "name", b);
267 st->hash_name = simple_hash(st->name);
269 rrdset_index_add_name(st);
272 // ----------------------------------------------------------------------------
275 char *rrdset_cache_dir(const char *id)
279 static char *cache_dir = NULL;
280 if(!cache_dir) cache_dir = config_get("global", "database directory", "cache");
282 char b[FILENAME_MAX + 1];
283 char n[FILENAME_MAX + 1];
284 rrdset_strncpy_name(b, id, FILENAME_MAX);
286 snprintf(n, FILENAME_MAX, "%s/%s", cache_dir, b);
287 ret = config_get(id, "database directory", n);
289 if(rrd_memory_mode == RRD_MEMORY_MODE_MAP || rrd_memory_mode == RRD_MEMORY_MODE_SAVE) {
290 int r = mkdir(ret, 0775);
291 if(r != 0 && errno != EEXIST)
292 error("Cannot create directory '%s'", ret);
298 // ----------------------------------------------------------------------------
301 void rrdset_reset(RRDSET *st)
303 debug(D_RRD_CALLS, "rrdset_reset() %s", st->name);
305 st->last_collected_time.tv_sec = 0;
306 st->last_collected_time.tv_usec = 0;
307 st->last_updated.tv_sec = 0;
308 st->last_updated.tv_usec = 0;
309 st->current_entry = 0;
311 st->counter_done = 0;
314 for(rd = st->dimensions; rd ; rd = rd->next) {
315 rd->last_collected_time.tv_sec = 0;
316 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 *title, const char *units, long priority, int update_every, int chart_type)
324 fatal("Cannot create rrd stats without an id.");
328 char fullid[RRD_ID_LENGTH_MAX + 1];
329 char fullfilename[FILENAME_MAX + 1];
332 snprintf(fullid, RRD_ID_LENGTH_MAX, "%s.%s", type, id);
334 long entries = config_get_number(fullid, "history", rrd_default_history_entries);
335 if(entries < 5) entries = config_set_number(fullid, "history", 5);
336 if(entries > RRD_HISTORY_ENTRIES_MAX) entries = config_set_number(fullid, "history", RRD_HISTORY_ENTRIES_MAX);
338 int enabled = config_get_boolean(fullid, "enabled", 1);
339 if(!enabled) entries = 5;
341 unsigned long size = sizeof(RRDSET);
342 char *cache_dir = rrdset_cache_dir(fullid);
344 debug(D_RRD_CALLS, "Creating RRD_STATS for '%s.%s'.", type, id);
346 snprintf(fullfilename, FILENAME_MAX, "%s/main.db", cache_dir);
347 if(rrd_memory_mode != RRD_MEMORY_MODE_RAM) st = (RRDSET *)mymmap(fullfilename, size, ((rrd_memory_mode == RRD_MEMORY_MODE_MAP)?MAP_SHARED:MAP_PRIVATE));
349 if(strcmp(st->magic, RRDSET_MAGIC) != 0) {
351 info("Initializing file %s.", fullfilename);
354 else if(strcmp(st->id, fullid) != 0) {
356 error("File %s contents are not for chart %s. Clearing it.", fullfilename, fullid);
361 else if(st->memsize != size || st->entries != entries) {
363 error("File %s does not have the desired size. Clearing it.", fullfilename);
366 else if(st->update_every != update_every) {
368 error("File %s does not have the desired update frequency. Clearing it.", fullfilename);
371 else if((time(NULL) - st->last_updated.tv_sec) > update_every * entries) {
373 error("File %s is too old. Clearing it.", fullfilename);
384 st->dimensions = NULL;
386 st->mapped = rrd_memory_mode;
389 st = calloc(1, size);
391 fatal("Cannot allocate memory for RRD_STATS %s.%s", type, id);
394 st->mapped = RRD_MEMORY_MODE_RAM;
397 st->entries = entries;
398 st->update_every = update_every;
400 strcpy(st->cache_filename, fullfilename);
401 strcpy(st->magic, RRDSET_MAGIC);
403 strcpy(st->id, fullid);
404 st->hash = simple_hash(st->id);
406 st->cache_dir = cache_dir;
408 st->family = config_get(st->id, "family", family?family:st->id);
409 st->units = config_get(st->id, "units", units?units:"");
410 st->type = config_get(st->id, "type", type);
411 st->chart_type = rrdset_type_id(config_get(st->id, "chart type", rrdset_type_name(chart_type)));
413 st->priority = config_get_number(st->id, "priority", priority);
414 st->enabled = enabled;
419 st->last_collected_time.tv_sec = 0;
420 st->last_collected_time.tv_usec = 0;
421 st->counter_done = 0;
423 st->gap_when_lost_iterations_above = config_get_number(st->id, "gap when lost iterations above", RRD_DEFAULT_GAP_INTERPOLATIONS);
425 st->dimensions_index.root = NULL;
426 st->dimensions_index.compar = rrddim_compare;
428 pthread_rwlock_init(&st->rwlock, NULL);
429 pthread_rwlock_wrlock(&rrdset_root_rwlock);
431 if(name && *name) rrdset_set_name(st, name);
432 else rrdset_set_name(st, id);
435 char varvalue[CONFIG_MAX_VALUE + 1];
436 snprintf(varvalue, CONFIG_MAX_VALUE, "%s (%s)", title?title:"", st->name);
437 st->title = config_get(st->id, "title", varvalue);
440 st->next = rrdset_root;
443 rrdset_index_add(st);
445 pthread_rwlock_unlock(&rrdset_root_rwlock);
450 RRDDIM *rrddim_add(RRDSET *st, const char *id, const char *name, long multiplier, long divisor, int algorithm)
452 char filename[FILENAME_MAX + 1];
453 char fullfilename[FILENAME_MAX + 1];
455 char varname[CONFIG_MAX_NAME + 1];
457 unsigned long size = sizeof(RRDDIM) + (st->entries * sizeof(storage_number));
459 debug(D_RRD_CALLS, "Adding dimension '%s/%s'.", st->id, id);
461 rrdset_strncpy_name(filename, id, FILENAME_MAX);
462 snprintf(fullfilename, FILENAME_MAX, "%s/%s.db", st->cache_dir, filename);
463 if(rrd_memory_mode != RRD_MEMORY_MODE_RAM) rd = (RRDDIM *)mymmap(fullfilename, size, ((rrd_memory_mode == RRD_MEMORY_MODE_MAP)?MAP_SHARED:MAP_PRIVATE));
466 gettimeofday(&now, NULL);
468 if(strcmp(rd->magic, RRDDIMENSION_MAGIC) != 0) {
470 info("Initializing file %s.", fullfilename);
473 else if(rd->memsize != size) {
475 error("File %s does not have the desired size. Clearing it.", fullfilename);
478 else if(rd->multiplier != multiplier) {
480 error("File %s does not have the same multiplier. Clearing it.", fullfilename);
483 else if(rd->divisor != divisor) {
485 error("File %s does not have the same divisor. Clearing it.", fullfilename);
488 else if(rd->algorithm != algorithm) {
490 error("File %s does not have the same algorithm. Clearing it.", fullfilename);
493 else if(rd->update_every != st->update_every) {
495 error("File %s does not have the same refresh frequency. Clearing it.", fullfilename);
498 else if(usecdiff(&now, &rd->last_collected_time) > (rd->entries * rd->update_every * 1000000ULL)) {
500 error("File %s is too old. Clearing it.", fullfilename);
503 else if(strcmp(rd->id, id) != 0) {
505 error("File %s contents are not for dimension %s. Clearing it.", fullfilename, id);
513 // we have a file mapped for rd
514 rd->mapped = rrd_memory_mode;
520 // if we didn't manage to get a mmap'd dimension, just create one
522 rd = calloc(1, size);
524 fatal("Cannot allocate RRD_DIMENSION %s/%s.", st->id, id);
528 rd->mapped = RRD_MEMORY_MODE_RAM;
532 strcpy(rd->magic, RRDDIMENSION_MAGIC);
533 strcpy(rd->cache_filename, fullfilename);
534 strncpy(rd->id, id, RRD_ID_LENGTH_MAX);
535 rd->hash = simple_hash(rd->id);
537 snprintf(varname, CONFIG_MAX_NAME, "dim %s name", rd->id);
538 rd->name = config_get(st->id, varname, (name && *name)?name:rd->id);
540 snprintf(varname, CONFIG_MAX_NAME, "dim %s algorithm", rd->id);
541 rd->algorithm = rrddim_algorithm_id(config_get(st->id, varname, rrddim_algorithm_name(algorithm)));
543 snprintf(varname, CONFIG_MAX_NAME, "dim %s multiplier", rd->id);
544 rd->multiplier = config_get_number(st->id, varname, multiplier);
546 snprintf(varname, CONFIG_MAX_NAME, "dim %s divisor", rd->id);
547 rd->divisor = config_get_number(st->id, varname, divisor);
548 if(!rd->divisor) rd->divisor = 1;
550 rd->entries = st->entries;
551 rd->update_every = st->update_every;
553 // append this dimension
557 RRDDIM *td = st->dimensions;
558 for(; td->next; td = td->next) ;
562 rrddim_index_add(st, rd);
567 void rrddim_set_name(RRDSET *st, RRDDIM *rd, const char *name)
569 debug(D_RRD_CALLS, "rrddim_set_name() %s.%s", st->name, rd->name);
571 char varname[CONFIG_MAX_NAME + 1];
572 snprintf(varname, CONFIG_MAX_NAME, "dim %s name", rd->id);
573 config_get(st->id, varname, name);
576 void rrddim_free(RRDSET *st, RRDDIM *rd)
578 debug(D_RRD_CALLS, "rrddim_free() %s.%s", st->name, rd->name);
580 RRDDIM *i = st->dimensions, *last = NULL;
581 for(i = st->dimensions; i && i != rd ; i = i->next) last = i;
584 error("Request to free dimension %s.%s but it is not linked.", st->id, rd->name);
588 if(last) last = i->next;
589 else st->dimensions = i->next;
591 rrddim_index_del(st, rd);
593 // free(rd->annotations);
594 if(rd->mapped == RRD_MEMORY_MODE_SAVE) {
595 debug(D_RRD_CALLS, "Saving dimension '%s' to '%s'.", rd->name, rd->cache_filename);
596 savememory(rd->cache_filename, rd, rd->memsize);
598 debug(D_RRD_CALLS, "Unmapping dimension '%s'.", rd->name);
599 munmap(rd, rd->memsize);
601 else if(rd->mapped == RRD_MEMORY_MODE_MAP) {
602 debug(D_RRD_CALLS, "Unmapping dimension '%s'.", rd->name);
603 munmap(rd, rd->memsize);
606 debug(D_RRD_CALLS, "Removing dimension '%s'.", rd->name);
611 void rrdset_free_all(void)
613 info("Freeing all memory...");
616 for(st = rrdset_root; st ;) {
617 RRDSET *next = st->next;
619 while(st->dimensions)
620 rrddim_free(st, st->dimensions);
622 rrdset_index_del(st);
624 if(st->mapped == RRD_MEMORY_MODE_SAVE) {
625 debug(D_RRD_CALLS, "Saving stats '%s' to '%s'.", st->name, st->cache_filename);
626 savememory(st->cache_filename, st, st->memsize);
628 debug(D_RRD_CALLS, "Unmapping stats '%s'.", st->name);
629 munmap(st, st->memsize);
631 else if(st->mapped == RRD_MEMORY_MODE_MAP) {
632 debug(D_RRD_CALLS, "Unmapping stats '%s'.", st->name);
633 munmap(st, st->memsize);
642 info("Memory cleanup completed...");
645 void rrdset_save_all(void)
647 debug(D_RRD_CALLS, "rrdset_save_all()");
652 pthread_rwlock_wrlock(&rrdset_root_rwlock);
653 for(st = rrdset_root; st ; st = st->next) {
654 pthread_rwlock_wrlock(&st->rwlock);
656 if(st->mapped == RRD_MEMORY_MODE_SAVE) {
657 debug(D_RRD_CALLS, "Saving stats '%s' to '%s'.", st->name, st->cache_filename);
658 savememory(st->cache_filename, st, st->memsize);
661 for(rd = st->dimensions; rd ; rd = rd->next) {
662 if(rd->mapped == RRD_MEMORY_MODE_SAVE) {
663 debug(D_RRD_CALLS, "Saving dimension '%s' to '%s'.", rd->name, rd->cache_filename);
664 savememory(rd->cache_filename, rd, rd->memsize);
668 pthread_rwlock_unlock(&st->rwlock);
670 pthread_rwlock_unlock(&rrdset_root_rwlock);
674 RRDSET *rrdset_find(const char *id)
676 debug(D_RRD_CALLS, "rrdset_find() for chart %s", id);
678 pthread_rwlock_rdlock(&rrdset_root_rwlock);
679 RRDSET *st = rrdset_index_find(id, 0);
680 pthread_rwlock_unlock(&rrdset_root_rwlock);
685 RRDSET *rrdset_find_bytype(const char *type, const char *id)
687 debug(D_RRD_CALLS, "rrdset_find_bytype() for chart %s.%s", type, id);
689 char buf[RRD_ID_LENGTH_MAX + 1];
691 strncpy(buf, type, RRD_ID_LENGTH_MAX - 1);
692 buf[RRD_ID_LENGTH_MAX - 1] = '\0';
694 int len = strlen(buf);
695 strncpy(&buf[len], id, RRD_ID_LENGTH_MAX - len);
696 buf[RRD_ID_LENGTH_MAX] = '\0';
698 return(rrdset_find(buf));
701 RRDSET *rrdset_find_byname(const char *name)
703 debug(D_RRD_CALLS, "rrdset_find_byname() for chart %s", name);
705 pthread_rwlock_rdlock(&rrdset_root_rwlock);
706 RRDSET *st = rrdset_index_find_name(name, 0);
707 pthread_rwlock_unlock(&rrdset_root_rwlock);
712 RRDDIM *rrddim_find(RRDSET *st, const char *id)
714 debug(D_RRD_CALLS, "rrddim_find() for chart %s, dimension %s", st->name, id);
716 return rrddim_index_find(st, id, 0);
719 int rrddim_hide(RRDSET *st, const char *id)
721 debug(D_RRD_CALLS, "rrddim_hide() for chart %s, dimension %s", st->name, id);
723 RRDDIM *rd = rrddim_find(st, id);
725 error("Cannot find dimension with id '%s' on stats '%s' (%s).", id, st->name, st->id);
733 int rrddim_unhide(RRDSET *st, const char *id)
735 debug(D_RRD_CALLS, "rrddim_unhide() for chart %s, dimension %s", st->name, id);
737 RRDDIM *rd = rrddim_find(st, id);
739 error("Cannot find dimension with id '%s' on stats '%s' (%s).", id, st->name, st->id);
747 void rrddim_set_by_pointer(RRDSET *st, RRDDIM *rd, collected_number value)
749 debug(D_RRD_CALLS, "rrddim_set_by_pointer() for chart %s, dimension %s, value " COLLECTED_NUMBER_FORMAT, st->name, rd->name, value);
751 gettimeofday(&rd->last_collected_time, NULL);
752 rd->collected_value = value;
756 int rrddim_set(RRDSET *st, const char *id, collected_number value)
758 RRDDIM *rd = rrddim_find(st, id);
760 error("Cannot find dimension with id '%s' on stats '%s' (%s).", id, st->name, st->id);
764 rrddim_set_by_pointer(st, rd, value);
768 void rrdset_next_usec(RRDSET *st, unsigned long long microseconds)
770 debug(D_RRD_CALLS, "rrdset_next_usec() for chart %s with microseconds %llu", st->name, microseconds);
772 if(st->debug) debug(D_RRD_STATS, "%s: NEXT: %llu microseconds", st->name, microseconds);
773 st->usec_since_last_update = microseconds;
776 void rrdset_next(RRDSET *st)
778 unsigned long long microseconds = 0;
780 if(st->last_collected_time.tv_sec) {
782 gettimeofday(&now, NULL);
783 microseconds = usecdiff(&now, &st->last_collected_time);
786 rrdset_next_usec(st, microseconds);
789 void rrdset_next_plugins(RRDSET *st)
794 unsigned long long rrdset_done(RRDSET *st)
796 debug(D_RRD_CALLS, "rrdset_done() for chart %s", st->name);
799 int oldstate, store_this_entry = 1;
801 if(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate) != 0)
802 error("Cannot set pthread cancel state to DISABLE.");
804 // a read lock is OK here
805 pthread_rwlock_rdlock(&st->rwlock);
807 // check if the chart has a long time to be refreshed
808 if(st->usec_since_last_update > st->entries * st->update_every * 1000000ULL) {
809 info("%s: took too long to be updated (%0.3Lf secs). Reseting it.", st->name, (long double)(st->usec_since_last_update / 1000000.0));
811 st->usec_since_last_update = st->update_every * 1000000ULL;
813 if(st->debug) debug(D_RRD_STATS, "%s: microseconds since last update: %llu", st->name, st->usec_since_last_update);
815 // set last_collected_time
816 if(!st->last_collected_time.tv_sec) {
817 // it is the first entry
818 // set the last_collected_time to now
819 gettimeofday(&st->last_collected_time, NULL);
821 // the first entry should not be stored
822 store_this_entry = 0;
824 if(st->debug) debug(D_RRD_STATS, "%s: initializing last_collected to now. Will not store the next entry.", st->name);
827 // it is not the first entry
828 // calculate the proper last_collected_time, using usec_since_last_update
829 unsigned long long ut = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec + st->usec_since_last_update;
830 st->last_collected_time.tv_sec = ut / 1000000ULL;
831 st->last_collected_time.tv_usec = ut % 1000000ULL;
834 // if this set has not been updated in the past
835 // we fake the last_update time to be = now - usec_since_last_update
836 if(!st->last_updated.tv_sec) {
837 // it has never been updated before
838 // set a fake last_updated, in the past using usec_since_last_update
839 unsigned long long ut = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec - st->usec_since_last_update;
840 st->last_updated.tv_sec = ut / 1000000ULL;
841 st->last_updated.tv_usec = ut % 1000000ULL;
843 // the first entry should not be stored
844 store_this_entry = 0;
846 if(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);
849 // check if we will re-write the entire data set
850 if(usecdiff(&st->last_collected_time, &st->last_updated) > st->update_every * st->entries * 1000000ULL) {
851 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);
854 st->usec_since_last_update = st->update_every * 1000000ULL;
856 gettimeofday(&st->last_collected_time, NULL);
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;
866 // these are the 3 variables that will help us in interpolation
867 // last_ut = the last time we added a value to the storage
868 // now_ut = the time the current value is taken at
869 // next_ut = the time of the next interpolation point
870 unsigned long long last_ut = st->last_updated.tv_sec * 1000000ULL + st->last_updated.tv_usec;
871 unsigned long long now_ut = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec;
872 unsigned long long next_ut = (st->last_updated.tv_sec + st->update_every) * 1000000ULL;
874 if(st->debug) debug(D_RRD_STATS, "%s: last ut = %0.3Lf (last updated time)", st->name, (long double)last_ut/1000000.0);
875 if(st->debug) debug(D_RRD_STATS, "%s: now ut = %0.3Lf (current update time)", st->name, (long double)now_ut/1000000.0);
876 if(st->debug) debug(D_RRD_STATS, "%s: next ut = %0.3Lf (next interpolation point)", st->name, (long double)next_ut/1000000.0);
878 if(!st->counter_done) {
879 store_this_entry = 0;
880 if(st->debug) debug(D_RRD_STATS, "%s: Will not store the next entry.", st->name);
884 // calculate totals and count the dimensions
886 st->collected_total = 0;
887 for( rd = st->dimensions, dimensions = 0 ; rd ; rd = rd->next, dimensions++ )
888 st->collected_total += rd->collected_value;
890 uint32_t storage_flags = SN_EXISTS;
892 // process all dimensions to calculate their values
893 // based on the collected figures only
894 // at this stage we do not interpolate anything
895 for( rd = st->dimensions ; rd ; rd = rd->next ) {
897 if(st->debug) debug(D_RRD_STATS, "%s/%s: "
898 " last_collected_value = " COLLECTED_NUMBER_FORMAT
899 " collected_value = " COLLECTED_NUMBER_FORMAT
900 " last_calculated_value = " CALCULATED_NUMBER_FORMAT
901 " calculated_value = " CALCULATED_NUMBER_FORMAT
903 , rd->last_collected_value
904 , rd->collected_value
905 , rd->last_calculated_value
906 , rd->calculated_value
909 switch(rd->algorithm) {
910 case RRDDIM_PCENT_OVER_DIFF_TOTAL:
911 // the percentage of the current increment
912 // over the increment of all dimensions together
913 if(st->collected_total == st->last_collected_total) rd->calculated_value = rd->last_calculated_value;
914 else rd->calculated_value =
915 (calculated_number)100
916 * (calculated_number)(rd->collected_value - rd->last_collected_value)
917 / (calculated_number)(st->collected_total - st->last_collected_total);
920 debug(D_RRD_STATS, "%s/%s: CALC PCENT-DIFF "
921 CALCULATED_NUMBER_FORMAT " = 100"
922 " * (" COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT ")"
923 " / (" COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT ")"
925 , rd->calculated_value
926 , rd->collected_value, rd->last_collected_value
927 , st->collected_total, st->last_collected_total
931 case RRDDIM_PCENT_OVER_ROW_TOTAL:
932 if(!st->collected_total) rd->calculated_value = 0;
934 // the percentage of the current value
935 // over the total of all dimensions
936 rd->calculated_value =
937 (calculated_number)100
938 * (calculated_number)rd->collected_value
939 / (calculated_number)st->collected_total;
942 debug(D_RRD_STATS, "%s/%s: CALC PCENT-ROW "
943 CALCULATED_NUMBER_FORMAT " = 100"
944 " * " COLLECTED_NUMBER_FORMAT
945 " / " COLLECTED_NUMBER_FORMAT
947 , rd->calculated_value
948 , rd->collected_value
949 , st->collected_total
953 case RRDDIM_INCREMENTAL:
954 // if the new is smaller than the old (an overflow, or reset), set the old equal to the new
955 // to reset the calculation (it will give zero as the calculation for this second)
956 if(rd->last_collected_value > rd->collected_value) {
957 info("%s.%s: Detect RESET or OVERFLOW. Last collected value = " COLLECTED_NUMBER_FORMAT ", current = " COLLECTED_NUMBER_FORMAT
959 , rd->last_collected_value
960 , rd->collected_value);
961 storage_flags = SN_EXISTS_RESET;
962 rd->last_collected_value = rd->collected_value;
965 rd->calculated_value += (calculated_number)(rd->collected_value - rd->last_collected_value);
968 debug(D_RRD_STATS, "%s/%s: CALC INC "
969 CALCULATED_NUMBER_FORMAT " += "
970 COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT
972 , rd->calculated_value
973 , rd->collected_value, rd->last_collected_value
977 case RRDDIM_ABSOLUTE:
978 rd->calculated_value = (calculated_number)rd->collected_value;
981 debug(D_RRD_STATS, "%s/%s: CALC ABS/ABS-NO-IN "
982 CALCULATED_NUMBER_FORMAT " = "
983 COLLECTED_NUMBER_FORMAT
985 , rd->calculated_value
986 , rd->collected_value
991 // make the default zero, to make sure
992 // it gets noticed when we add new types
993 rd->calculated_value = 0;
996 debug(D_RRD_STATS, "%s/%s: CALC "
997 CALCULATED_NUMBER_FORMAT " = 0"
999 , rd->calculated_value
1005 // at this point we have all the calculated values ready
1006 // it is now time to interpolate values on a second boundary
1008 unsigned long long first_ut = last_ut;
1009 int iterations = (now_ut - last_ut) / (st->update_every * 1000000ULL);
1011 for( ; next_ut <= now_ut ; next_ut += st->update_every * 1000000ULL, iterations-- ) {
1012 if(iterations < 0) error("iterations calculation wrapped!");
1014 if(st->debug) debug(D_RRD_STATS, "%s: last ut = %0.3Lf (last updated time)", st->name, (long double)last_ut/1000000.0);
1015 if(st->debug) debug(D_RRD_STATS, "%s: next ut = %0.3Lf (next interpolation point)", st->name, (long double)next_ut/1000000.0);
1017 st->last_updated.tv_sec = next_ut / 1000000ULL;
1018 st->last_updated.tv_usec = 0;
1020 for( rd = st->dimensions ; rd ; rd = rd->next ) {
1021 calculated_number new_value;
1023 switch(rd->algorithm) {
1024 case RRDDIM_INCREMENTAL:
1025 new_value = (calculated_number)
1026 ( rd->calculated_value
1027 * (calculated_number)(next_ut - last_ut)
1028 / (calculated_number)(now_ut - last_ut)
1032 debug(D_RRD_STATS, "%s/%s: CALC2 INC "
1033 CALCULATED_NUMBER_FORMAT " = "
1034 CALCULATED_NUMBER_FORMAT
1039 , rd->calculated_value
1040 , (unsigned long long)(next_ut - last_ut)
1041 , (unsigned long long)(now_ut - last_ut)
1044 rd->calculated_value -= new_value;
1047 case RRDDIM_ABSOLUTE:
1048 case RRDDIM_PCENT_OVER_ROW_TOTAL:
1049 case RRDDIM_PCENT_OVER_DIFF_TOTAL:
1051 new_value = (calculated_number)
1052 ( ( (rd->calculated_value - rd->last_calculated_value)
1053 * (calculated_number)(next_ut - first_ut)
1054 / (calculated_number)(now_ut - first_ut)
1056 + rd->last_calculated_value
1060 debug(D_RRD_STATS, "%s/%s: CALC2 DEF "
1061 CALCULATED_NUMBER_FORMAT " = ((("
1062 "(" CALCULATED_NUMBER_FORMAT " - " CALCULATED_NUMBER_FORMAT ")"
1064 " / %llu) + " CALCULATED_NUMBER_FORMAT
1067 , rd->calculated_value, rd->last_calculated_value
1068 , (next_ut - first_ut)
1069 , (now_ut - first_ut), rd->last_calculated_value
1072 if(next_ut + st->update_every * 1000000ULL > now_ut) rd->calculated_value = new_value;
1076 if(!store_this_entry) {
1077 store_this_entry = 1;
1081 if(rd->updated && iterations < st->gap_when_lost_iterations_above) {
1082 rd->values[st->current_entry] = pack_storage_number(
1084 * (calculated_number)rd->multiplier
1085 / (calculated_number)rd->divisor
1089 debug(D_RRD_STATS, "%s/%s: STORE[%ld] "
1090 CALCULATED_NUMBER_FORMAT " = " CALCULATED_NUMBER_FORMAT
1095 , unpack_storage_number(rd->values[st->current_entry]), new_value
1101 if(st->debug) debug(D_RRD_STATS, "%s/%s: STORE[%ld] = NON EXISTING "
1105 rd->values[st->current_entry] = pack_storage_number(0, SN_NOT_EXISTS);
1109 calculated_number t1 = new_value * (calculated_number)rd->multiplier / (calculated_number)rd->divisor;
1110 calculated_number t2 = unpack_storage_number(rd->values[st->current_entry]);
1111 calculated_number accuracy = accuracy_loss(t1, t2);
1112 debug(D_RRD_STATS, "%s/%s: UNPACK[%ld] = " CALCULATED_NUMBER_FORMAT " FLAGS=0x%08x (original = " CALCULATED_NUMBER_FORMAT ", accuracy loss = " CALCULATED_NUMBER_FORMAT "%%%s)"
1116 , get_storage_number_flags(rd->values[st->current_entry])
1119 , (accuracy > ACCURACY_LOSS) ? " **TOO BIG** " : ""
1122 rd->collected_volume += t1;
1123 rd->stored_volume += t2;
1124 accuracy = accuracy_loss(rd->collected_volume, rd->stored_volume);
1125 debug(D_RRD_STATS, "%s/%s: VOLUME[%ld] = " CALCULATED_NUMBER_FORMAT ", calculated = " CALCULATED_NUMBER_FORMAT ", accuracy loss = " CALCULATED_NUMBER_FORMAT "%%%s"
1129 , rd->collected_volume
1131 , (accuracy > ACCURACY_LOSS) ? " **TOO BIG** " : ""
1136 // reset the storage flags for the next point, if any;
1137 storage_flags = SN_EXISTS;
1139 if(st->first_entry_t && st->counter >= (unsigned long long)st->entries) {
1140 // the db is overwriting values
1141 // add the value we will overwrite
1142 st->first_entry_t += st->update_every * 1000000ULL;
1146 st->current_entry = ((st->current_entry + 1) >= st->entries) ? 0 : st->current_entry + 1;
1147 if(!st->first_entry_t) st->first_entry_t = next_ut;
1151 for( rd = st->dimensions; rd ; rd = rd->next ) {
1152 if(!rd->updated) continue;
1153 rd->last_collected_value = rd->collected_value;
1154 rd->last_calculated_value = rd->calculated_value;
1155 rd->collected_value = 0;
1158 // if this is the first entry of incremental dimensions
1159 // we have to set the first calculated_value to zero
1160 // to eliminate the first spike
1161 if(st->counter_done == 1) switch(rd->algorithm) {
1162 case RRDDIM_PCENT_OVER_DIFF_TOTAL:
1163 case RRDDIM_INCREMENTAL:
1164 rd->calculated_value = 0;
1165 // the next time, a new incremental total will be calculated
1169 st->last_collected_total = st->collected_total;
1171 // ALL DONE ABOUT THE DATA UPDATE
1172 // --------------------------------------------------------------------
1174 // find if there are any obsolete dimensions (not updated recently)
1175 if(rrd_delete_unupdated_dimensions) {
1177 for( rd = st->dimensions; rd ; rd = rd->next )
1178 if((rd->last_collected_time.tv_sec + (rrd_delete_unupdated_dimensions * st->update_every)) < st->last_collected_time.tv_sec)
1182 // there is dimension to free
1183 // upgrade our read lock to a write lock
1184 pthread_rwlock_unlock(&st->rwlock);
1185 pthread_rwlock_wrlock(&st->rwlock);
1187 for( rd = st->dimensions, last = NULL ; rd ; ) {
1188 // remove it only it is not updated in rrd_delete_unupdated_dimensions seconds
1190 if((rd->last_collected_time.tv_sec + (rrd_delete_unupdated_dimensions * st->update_every)) < st->last_collected_time.tv_sec) {
1191 debug(D_RRD_STATS, "Removing obsolete dimension '%s' (%s) of '%s' (%s).", rd->name, rd->id, st->name, st->id);
1194 st->dimensions = rd->next;
1196 rrddim_free(st, rd);
1197 rd = st->dimensions;
1201 last->next = rd->next;
1203 rrddim_free(st, rd);
1213 if(!st->dimensions) st->enabled = 0;
1217 pthread_rwlock_unlock(&st->rwlock);
1219 if(pthread_setcancelstate(oldstate, NULL) != 0)
1220 error("Cannot set pthread cancel state to RESTORE (%d).", oldstate);
1222 return(st->usec_since_last_update);
1226 // find the oldest entry in the data, skipping all empty slots
1227 time_t rrdset_first_entry_t(RRDSET *st)
1229 if(!st->first_entry_t) return st->last_updated.tv_sec;
1231 return st->first_entry_t / 1000000;