11 #include <sys/types.h>
20 #define RRD_DEFAULT_GAP_INTERPOLATIONS 1
22 // ----------------------------------------------------------------------------
25 // if not zero it gives the time (in seconds) to remove un-updated dimensions
26 int rrd_delete_unupdated_dimensions = 0;
28 int rrd_update_every = UPDATE_EVERY;
29 int rrd_default_history_entries = RRD_DEFAULT_HISTORY_ENTRIES;
31 RRDSET *rrdset_root = NULL;
32 pthread_rwlock_t rrdset_root_rwlock = PTHREAD_RWLOCK_INITIALIZER;
34 int rrd_memory_mode = RRD_MEMORY_MODE_SAVE;
37 // ----------------------------------------------------------------------------
40 static int rrdset_iterator(avl *a) { if(a) {}; return 0; }
42 static int rrdset_compare(void* a, void* b) {
43 if(((RRDSET *)a)->hash < ((RRDSET *)b)->hash) return -1;
44 else if(((RRDSET *)a)->hash > ((RRDSET *)b)->hash) return 1;
45 else return strcmp(((RRDSET *)a)->id, ((RRDSET *)b)->id);
48 avl_tree rrdset_root_index = {
53 #define rrdset_index_add(st) avl_insert(&rrdset_root_index, (avl *)(st))
54 #define rrdset_index_del(st) avl_remove(&rrdset_root_index, (avl *)(st))
56 static RRDSET *rrdset_index_find(const char *id, uint32_t hash) {
57 RRDSET *result = NULL, tmp;
58 strncpy(tmp.id, id, RRD_ID_LENGTH_MAX);
59 tmp.id[RRD_ID_LENGTH_MAX] = '\0';
60 tmp.hash = (hash)?hash:simple_hash(tmp.id);
62 avl_search(&(rrdset_root_index), (avl *)&tmp, rrdset_iterator, (avl **)&result);
66 // ----------------------------------------------------------------------------
69 #define rrdset_from_avlname(avlname_ptr) ((RRDSET *)((avlname_ptr) - offsetof(RRDSET, avlname)))
71 static int rrdset_iterator_name(avl *a) { if(a) {}; return 0; }
73 static int rrdset_compare_name(void* a, void* b) {
74 RRDSET *A = rrdset_from_avlname(a);
75 RRDSET *B = rrdset_from_avlname(b);
77 // fprintf(stderr, "COMPARING: %s with %s\n", A->name, B->name);
79 if(A->hash_name < B->hash_name) return -1;
80 else if(A->hash_name > B->hash_name) return 1;
81 else return strcmp(A->name, B->name);
84 avl_tree rrdset_root_index_name = {
89 int rrdset_index_add_name(RRDSET *st) {
90 // fprintf(stderr, "ADDING: %s (name: %s)\n", st->id, st->name);
91 return avl_insert(&rrdset_root_index_name, (avl *)(&st->avlname));
94 #define rrdset_index_del_name(st) avl_remove(&rrdset_root_index_name, (avl *)(&st->avlname))
96 static RRDSET *rrdset_index_find_name(const char *name, uint32_t hash) {
100 tmp.hash_name = (hash)?hash:simple_hash(tmp.name);
102 // fprintf(stderr, "SEARCHING: %s\n", name);
103 avl_search(&(rrdset_root_index_name), (avl *)(&(tmp.avlname)), rrdset_iterator_name, (avl **)&result);
105 RRDSET *st = rrdset_from_avlname(result);
106 if(strcmp(st->magic, RRDSET_MAGIC))
107 error("Search for RRDSET %s returned an invalid RRDSET %s (name %s)", name, st->id, st->name);
109 // fprintf(stderr, "FOUND: %s\n", name);
110 return rrdset_from_avlname(result);
112 // fprintf(stderr, "NOT FOUND: %s\n", name);
117 // ----------------------------------------------------------------------------
120 static int rrddim_iterator(avl *a) { if(a) {}; return 0; }
122 static int rrddim_compare(void* a, void* b) {
123 if(((RRDDIM *)a)->hash < ((RRDDIM *)b)->hash) return -1;
124 else if(((RRDDIM *)a)->hash > ((RRDDIM *)b)->hash) return 1;
125 else return strcmp(((RRDDIM *)a)->id, ((RRDDIM *)b)->id);
128 #define rrddim_index_add(st, rd) avl_insert(&((st)->dimensions_index), (avl *)(rd))
129 #define rrddim_index_del(st,rd ) avl_remove(&((st)->dimensions_index), (avl *)(rd))
131 static RRDDIM *rrddim_index_find(RRDSET *st, const char *id, uint32_t hash) {
132 RRDDIM *result = NULL, tmp;
133 strncpy(tmp.id, id, RRD_ID_LENGTH_MAX);
134 tmp.id[RRD_ID_LENGTH_MAX] = '\0';
135 tmp.hash = (hash)?hash:simple_hash(tmp.id);
137 avl_search(&(st->dimensions_index), (avl *)&tmp, rrddim_iterator, (avl **)&result);
141 // ----------------------------------------------------------------------------
144 int rrdset_type_id(const char *name)
146 if(unlikely(strcmp(name, RRDSET_TYPE_AREA_NAME) == 0)) return RRDSET_TYPE_AREA;
147 else if(unlikely(strcmp(name, RRDSET_TYPE_STACKED_NAME) == 0)) return RRDSET_TYPE_STACKED;
148 else if(unlikely(strcmp(name, RRDSET_TYPE_LINE_NAME) == 0)) return RRDSET_TYPE_LINE;
149 return RRDSET_TYPE_LINE;
152 const char *rrdset_type_name(int chart_type)
154 static char line[] = RRDSET_TYPE_LINE_NAME;
155 static char area[] = RRDSET_TYPE_AREA_NAME;
156 static char stacked[] = RRDSET_TYPE_STACKED_NAME;
159 case RRDSET_TYPE_LINE:
162 case RRDSET_TYPE_AREA:
165 case RRDSET_TYPE_STACKED:
171 // ----------------------------------------------------------------------------
174 const char *rrd_memory_mode_name(int id)
176 static const char ram[] = RRD_MEMORY_MODE_RAM_NAME;
177 static const char map[] = RRD_MEMORY_MODE_MAP_NAME;
178 static const char save[] = RRD_MEMORY_MODE_SAVE_NAME;
181 case RRD_MEMORY_MODE_RAM:
184 case RRD_MEMORY_MODE_MAP:
187 case RRD_MEMORY_MODE_SAVE:
195 int rrd_memory_mode_id(const char *name)
197 if(unlikely(!strcmp(name, RRD_MEMORY_MODE_RAM_NAME)))
198 return RRD_MEMORY_MODE_RAM;
199 else if(unlikely(!strcmp(name, RRD_MEMORY_MODE_MAP_NAME)))
200 return RRD_MEMORY_MODE_MAP;
202 return RRD_MEMORY_MODE_SAVE;
205 // ----------------------------------------------------------------------------
208 int rrddim_algorithm_id(const char *name)
210 if(strcmp(name, RRDDIM_ABSOLUTE_NAME) == 0) return RRDDIM_ABSOLUTE;
211 if(strcmp(name, RRDDIM_INCREMENTAL_NAME) == 0) return RRDDIM_INCREMENTAL;
212 if(strcmp(name, RRDDIM_PCENT_OVER_ROW_TOTAL_NAME) == 0) return RRDDIM_PCENT_OVER_ROW_TOTAL;
213 if(strcmp(name, RRDDIM_PCENT_OVER_DIFF_TOTAL_NAME) == 0) return RRDDIM_PCENT_OVER_DIFF_TOTAL;
214 return RRDDIM_ABSOLUTE;
217 const char *rrddim_algorithm_name(int chart_type)
219 static char absolute[] = RRDDIM_ABSOLUTE_NAME;
220 static char incremental[] = RRDDIM_INCREMENTAL_NAME;
221 static char percentage_of_absolute_row[] = RRDDIM_PCENT_OVER_ROW_TOTAL_NAME;
222 static char percentage_of_incremental_row[] = RRDDIM_PCENT_OVER_DIFF_TOTAL_NAME;
225 case RRDDIM_ABSOLUTE:
228 case RRDDIM_INCREMENTAL:
231 case RRDDIM_PCENT_OVER_ROW_TOTAL:
232 return percentage_of_absolute_row;
234 case RRDDIM_PCENT_OVER_DIFF_TOTAL:
235 return percentage_of_incremental_row;
240 // ----------------------------------------------------------------------------
243 char *rrdset_strncpy_name(char *to, const char *from, int length)
246 for(i = 0; i < length && from[i] ;i++) {
247 if(from[i] == '.' || isalpha(from[i]) || isdigit(from[i])) to[i] = from[i];
250 if(i < length) to[i] = '\0';
251 to[length - 1] = '\0';
256 void rrdset_set_name(RRDSET *st, const char *name)
258 debug(D_RRD_CALLS, "rrdset_set_name() old: %s, new: %s", st->name, name);
260 if(st->name) rrdset_index_del_name(st);
262 char b[CONFIG_MAX_VALUE + 1];
263 char n[RRD_ID_LENGTH_MAX + 1];
265 snprintf(n, RRD_ID_LENGTH_MAX, "%s.%s", st->type, name);
266 rrdset_strncpy_name(b, n, CONFIG_MAX_VALUE);
267 st->name = config_get(st->id, "name", b);
268 st->hash_name = simple_hash(st->name);
270 rrdset_index_add_name(st);
273 // ----------------------------------------------------------------------------
276 char *rrdset_cache_dir(const char *id)
280 static char *cache_dir = NULL;
281 if(!cache_dir) cache_dir = config_get("global", "database directory", "cache");
283 char b[FILENAME_MAX + 1];
284 char n[FILENAME_MAX + 1];
285 rrdset_strncpy_name(b, id, FILENAME_MAX);
287 snprintf(n, FILENAME_MAX, "%s/%s", cache_dir, b);
288 ret = config_get(id, "database directory", n);
290 if(rrd_memory_mode == RRD_MEMORY_MODE_MAP || rrd_memory_mode == RRD_MEMORY_MODE_SAVE) {
291 int r = mkdir(ret, 0775);
292 if(r != 0 && errno != EEXIST)
293 error("Cannot create directory '%s'", ret);
299 // ----------------------------------------------------------------------------
302 void rrdset_reset(RRDSET *st)
304 debug(D_RRD_CALLS, "rrdset_reset() %s", st->name);
306 st->last_collected_time.tv_sec = 0;
307 st->last_collected_time.tv_usec = 0;
308 st->last_updated.tv_sec = 0;
309 st->last_updated.tv_usec = 0;
310 st->current_entry = 0;
312 st->counter_done = 0;
315 for(rd = st->dimensions; rd ; rd = rd->next) {
316 rd->last_collected_time.tv_sec = 0;
317 rd->last_collected_time.tv_usec = 0;
318 bzero(rd->values, rd->entries * sizeof(storage_number));
322 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)
325 fatal("Cannot create rrd stats without an id.");
329 char fullid[RRD_ID_LENGTH_MAX + 1];
330 char fullfilename[FILENAME_MAX + 1];
333 snprintf(fullid, RRD_ID_LENGTH_MAX, "%s.%s", type, id);
335 long entries = config_get_number(fullid, "history", rrd_default_history_entries);
336 if(entries < 5) entries = config_set_number(fullid, "history", 5);
337 if(entries > RRD_HISTORY_ENTRIES_MAX) entries = config_set_number(fullid, "history", RRD_HISTORY_ENTRIES_MAX);
339 int enabled = config_get_boolean(fullid, "enabled", 1);
340 if(!enabled) entries = 5;
342 unsigned long size = sizeof(RRDSET);
343 char *cache_dir = rrdset_cache_dir(fullid);
345 debug(D_RRD_CALLS, "Creating RRD_STATS for '%s.%s'.", type, id);
347 snprintf(fullfilename, FILENAME_MAX, "%s/main.db", cache_dir);
348 if(rrd_memory_mode != RRD_MEMORY_MODE_RAM) st = (RRDSET *)mymmap(fullfilename, size, ((rrd_memory_mode == RRD_MEMORY_MODE_MAP)?MAP_SHARED:MAP_PRIVATE));
350 if(strcmp(st->magic, RRDSET_MAGIC) != 0) {
352 info("Initializing file %s.", fullfilename);
355 else if(strcmp(st->id, fullid) != 0) {
357 error("File %s contents are not for chart %s. Clearing it.", fullfilename, fullid);
362 else if(st->memsize != size || st->entries != entries) {
364 error("File %s does not have the desired size. Clearing it.", fullfilename);
367 else if(st->update_every != update_every) {
369 error("File %s does not have the desired update frequency. Clearing it.", fullfilename);
372 else if((time(NULL) - st->last_updated.tv_sec) > update_every * entries) {
374 error("File %s is too old. Clearing it.", fullfilename);
385 st->dimensions = NULL;
387 st->mapped = rrd_memory_mode;
390 st = calloc(1, size);
392 fatal("Cannot allocate memory for RRD_STATS %s.%s", type, id);
395 st->mapped = RRD_MEMORY_MODE_RAM;
398 st->entries = entries;
399 st->update_every = update_every;
401 strcpy(st->cache_filename, fullfilename);
402 strcpy(st->magic, RRDSET_MAGIC);
404 strcpy(st->id, fullid);
405 st->hash = simple_hash(st->id);
407 st->cache_dir = cache_dir;
409 st->family = config_get(st->id, "family", family?family:st->id);
410 st->units = config_get(st->id, "units", units?units:"");
411 st->type = config_get(st->id, "type", type);
412 st->chart_type = rrdset_type_id(config_get(st->id, "chart type", rrdset_type_name(chart_type)));
414 st->priority = config_get_number(st->id, "priority", priority);
415 st->enabled = enabled;
420 st->last_collected_time.tv_sec = 0;
421 st->last_collected_time.tv_usec = 0;
422 st->counter_done = 0;
424 st->gap_when_lost_iterations_above = config_get_number(st->id, "gap when lost iterations above", RRD_DEFAULT_GAP_INTERPOLATIONS) + 2;
426 st->dimensions_index.root = NULL;
427 st->dimensions_index.compar = rrddim_compare;
429 pthread_rwlock_init(&st->rwlock, NULL);
430 pthread_rwlock_wrlock(&rrdset_root_rwlock);
432 if(name && *name) rrdset_set_name(st, name);
433 else rrdset_set_name(st, id);
436 char varvalue[CONFIG_MAX_VALUE + 1];
437 snprintf(varvalue, CONFIG_MAX_VALUE, "%s (%s)", title?title:"", st->name);
438 st->title = config_get(st->id, "title", varvalue);
441 st->next = rrdset_root;
444 rrdset_index_add(st);
446 pthread_rwlock_unlock(&rrdset_root_rwlock);
451 RRDDIM *rrddim_add(RRDSET *st, const char *id, const char *name, long multiplier, long divisor, int algorithm)
453 char filename[FILENAME_MAX + 1];
454 char fullfilename[FILENAME_MAX + 1];
456 char varname[CONFIG_MAX_NAME + 1];
458 unsigned long size = sizeof(RRDDIM) + (st->entries * sizeof(storage_number));
460 debug(D_RRD_CALLS, "Adding dimension '%s/%s'.", st->id, id);
462 rrdset_strncpy_name(filename, id, FILENAME_MAX);
463 snprintf(fullfilename, FILENAME_MAX, "%s/%s.db", st->cache_dir, filename);
464 if(rrd_memory_mode != RRD_MEMORY_MODE_RAM) rd = (RRDDIM *)mymmap(fullfilename, size, ((rrd_memory_mode == RRD_MEMORY_MODE_MAP)?MAP_SHARED:MAP_PRIVATE));
467 gettimeofday(&now, NULL);
469 if(strcmp(rd->magic, RRDDIMENSION_MAGIC) != 0) {
471 info("Initializing file %s.", fullfilename);
474 else if(rd->memsize != size) {
476 error("File %s does not have the desired size. Clearing it.", fullfilename);
479 else if(rd->multiplier != multiplier) {
481 error("File %s does not have the same multiplier. Clearing it.", fullfilename);
484 else if(rd->divisor != divisor) {
486 error("File %s does not have the same divisor. Clearing it.", fullfilename);
489 else if(rd->algorithm != algorithm) {
491 error("File %s does not have the same algorithm. Clearing it.", fullfilename);
494 else if(rd->update_every != st->update_every) {
496 error("File %s does not have the same refresh frequency. Clearing it.", fullfilename);
499 else if(usecdiff(&now, &rd->last_collected_time) > (rd->entries * rd->update_every * 1000000ULL)) {
501 error("File %s is too old. Clearing it.", fullfilename);
504 else if(strcmp(rd->id, id) != 0) {
506 error("File %s contents are not for dimension %s. Clearing it.", fullfilename, id);
514 // we have a file mapped for rd
515 rd->mapped = rrd_memory_mode;
521 // if we didn't manage to get a mmap'd dimension, just create one
523 rd = calloc(1, size);
525 fatal("Cannot allocate RRD_DIMENSION %s/%s.", st->id, id);
529 rd->mapped = RRD_MEMORY_MODE_RAM;
533 strcpy(rd->magic, RRDDIMENSION_MAGIC);
534 strcpy(rd->cache_filename, fullfilename);
535 strncpy(rd->id, id, RRD_ID_LENGTH_MAX);
536 rd->hash = simple_hash(rd->id);
538 snprintf(varname, CONFIG_MAX_NAME, "dim %s name", rd->id);
539 rd->name = config_get(st->id, varname, (name && *name)?name:rd->id);
541 snprintf(varname, CONFIG_MAX_NAME, "dim %s algorithm", rd->id);
542 rd->algorithm = rrddim_algorithm_id(config_get(st->id, varname, rrddim_algorithm_name(algorithm)));
544 snprintf(varname, CONFIG_MAX_NAME, "dim %s multiplier", rd->id);
545 rd->multiplier = config_get_number(st->id, varname, multiplier);
547 snprintf(varname, CONFIG_MAX_NAME, "dim %s divisor", rd->id);
548 rd->divisor = config_get_number(st->id, varname, divisor);
549 if(!rd->divisor) rd->divisor = 1;
551 rd->entries = st->entries;
552 rd->update_every = st->update_every;
554 // append this dimension
558 RRDDIM *td = st->dimensions;
559 for(; td->next; td = td->next) ;
563 rrddim_index_add(st, rd);
568 void rrddim_set_name(RRDSET *st, RRDDIM *rd, const char *name)
570 debug(D_RRD_CALLS, "rrddim_set_name() %s.%s", st->name, rd->name);
572 char varname[CONFIG_MAX_NAME + 1];
573 snprintf(varname, CONFIG_MAX_NAME, "dim %s name", rd->id);
574 config_get(st->id, varname, name);
577 void rrddim_free(RRDSET *st, RRDDIM *rd)
579 debug(D_RRD_CALLS, "rrddim_free() %s.%s", st->name, rd->name);
581 RRDDIM *i = st->dimensions, *last = NULL;
582 for(i = st->dimensions; i && i != rd ; i = i->next) last = i;
585 error("Request to free dimension %s.%s but it is not linked.", st->id, rd->name);
589 if(last) last = i->next;
590 else st->dimensions = i->next;
592 rrddim_index_del(st, rd);
594 // free(rd->annotations);
595 if(rd->mapped == RRD_MEMORY_MODE_SAVE) {
596 debug(D_RRD_CALLS, "Saving dimension '%s' to '%s'.", rd->name, rd->cache_filename);
597 savememory(rd->cache_filename, rd, rd->memsize);
599 debug(D_RRD_CALLS, "Unmapping dimension '%s'.", rd->name);
600 munmap(rd, rd->memsize);
602 else if(rd->mapped == RRD_MEMORY_MODE_MAP) {
603 debug(D_RRD_CALLS, "Unmapping dimension '%s'.", rd->name);
604 munmap(rd, rd->memsize);
607 debug(D_RRD_CALLS, "Removing dimension '%s'.", rd->name);
612 void rrdset_free_all(void)
614 info("Freeing all memory...");
617 for(st = rrdset_root; st ;) {
618 RRDSET *next = st->next;
620 while(st->dimensions)
621 rrddim_free(st, st->dimensions);
623 rrdset_index_del(st);
625 if(st->mapped == RRD_MEMORY_MODE_SAVE) {
626 debug(D_RRD_CALLS, "Saving stats '%s' to '%s'.", st->name, st->cache_filename);
627 savememory(st->cache_filename, st, st->memsize);
629 debug(D_RRD_CALLS, "Unmapping stats '%s'.", st->name);
630 munmap(st, st->memsize);
632 else if(st->mapped == RRD_MEMORY_MODE_MAP) {
633 debug(D_RRD_CALLS, "Unmapping stats '%s'.", st->name);
634 munmap(st, st->memsize);
643 info("Memory cleanup completed...");
646 void rrdset_save_all(void)
648 debug(D_RRD_CALLS, "rrdset_save_all()");
653 pthread_rwlock_wrlock(&rrdset_root_rwlock);
654 for(st = rrdset_root; st ; st = st->next) {
655 pthread_rwlock_wrlock(&st->rwlock);
657 if(st->mapped == RRD_MEMORY_MODE_SAVE) {
658 debug(D_RRD_CALLS, "Saving stats '%s' to '%s'.", st->name, st->cache_filename);
659 savememory(st->cache_filename, st, st->memsize);
662 for(rd = st->dimensions; rd ; rd = rd->next) {
663 if(likely(rd->mapped == RRD_MEMORY_MODE_SAVE)) {
664 debug(D_RRD_CALLS, "Saving dimension '%s' to '%s'.", rd->name, rd->cache_filename);
665 savememory(rd->cache_filename, rd, rd->memsize);
669 pthread_rwlock_unlock(&st->rwlock);
671 pthread_rwlock_unlock(&rrdset_root_rwlock);
675 RRDSET *rrdset_find(const char *id)
677 debug(D_RRD_CALLS, "rrdset_find() for chart %s", id);
679 pthread_rwlock_rdlock(&rrdset_root_rwlock);
680 RRDSET *st = rrdset_index_find(id, 0);
681 pthread_rwlock_unlock(&rrdset_root_rwlock);
686 RRDSET *rrdset_find_bytype(const char *type, const char *id)
688 debug(D_RRD_CALLS, "rrdset_find_bytype() for chart %s.%s", type, id);
690 char buf[RRD_ID_LENGTH_MAX + 1];
692 strncpy(buf, type, RRD_ID_LENGTH_MAX - 1);
693 buf[RRD_ID_LENGTH_MAX - 1] = '\0';
695 int len = strlen(buf);
696 strncpy(&buf[len], id, RRD_ID_LENGTH_MAX - len);
697 buf[RRD_ID_LENGTH_MAX] = '\0';
699 return(rrdset_find(buf));
702 RRDSET *rrdset_find_byname(const char *name)
704 debug(D_RRD_CALLS, "rrdset_find_byname() for chart %s", name);
706 pthread_rwlock_rdlock(&rrdset_root_rwlock);
707 RRDSET *st = rrdset_index_find_name(name, 0);
708 pthread_rwlock_unlock(&rrdset_root_rwlock);
713 RRDDIM *rrddim_find(RRDSET *st, const char *id)
715 debug(D_RRD_CALLS, "rrddim_find() for chart %s, dimension %s", st->name, id);
717 return rrddim_index_find(st, id, 0);
720 int rrddim_hide(RRDSET *st, const char *id)
722 debug(D_RRD_CALLS, "rrddim_hide() for chart %s, dimension %s", st->name, id);
724 RRDDIM *rd = rrddim_find(st, id);
726 error("Cannot find dimension with id '%s' on stats '%s' (%s).", id, st->name, st->id);
734 int rrddim_unhide(RRDSET *st, const char *id)
736 debug(D_RRD_CALLS, "rrddim_unhide() for chart %s, dimension %s", st->name, id);
738 RRDDIM *rd = rrddim_find(st, id);
740 error("Cannot find dimension with id '%s' on stats '%s' (%s).", id, st->name, st->id);
748 collected_number rrddim_set_by_pointer(RRDSET *st, RRDDIM *rd, collected_number value)
750 debug(D_RRD_CALLS, "rrddim_set_by_pointer() for chart %s, dimension %s, value " COLLECTED_NUMBER_FORMAT, st->name, rd->name, value);
752 gettimeofday(&rd->last_collected_time, NULL);
753 rd->collected_value = value;
756 return rd->last_collected_value;
759 collected_number rrddim_set(RRDSET *st, const char *id, collected_number value)
761 RRDDIM *rd = rrddim_find(st, id);
763 error("Cannot find dimension with id '%s' on stats '%s' (%s).", id, st->name, st->id);
767 return rrddim_set_by_pointer(st, rd, value);
770 void rrdset_next_usec(RRDSET *st, unsigned long long microseconds)
772 if(!microseconds) rrdset_next(st);
774 debug(D_RRD_CALLS, "rrdset_next_usec() for chart %s with microseconds %llu", st->name, microseconds);
776 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s: NEXT: %llu microseconds", st->name, microseconds);
777 st->usec_since_last_update = microseconds;
781 void rrdset_next(RRDSET *st)
783 unsigned long long microseconds = 0;
785 if(likely(st->last_collected_time.tv_sec)) {
787 gettimeofday(&now, NULL);
788 microseconds = usecdiff(&now, &st->last_collected_time);
791 rrdset_next_usec(st, microseconds);
794 void rrdset_next_plugins(RRDSET *st)
799 unsigned long long rrdset_done(RRDSET *st)
801 debug(D_RRD_CALLS, "rrdset_done() for chart %s", st->name);
804 int oldstate, store_this_entry = 1;
806 if(unlikely(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate) != 0))
807 error("Cannot set pthread cancel state to DISABLE.");
809 // a read lock is OK here
810 pthread_rwlock_rdlock(&st->rwlock);
812 // check if the chart has a long time to be refreshed
813 if(unlikely(st->usec_since_last_update > st->entries * st->update_every * 1000000ULL)) {
814 info("%s: took too long to be updated (%0.3Lf secs). Reseting it.", st->name, (long double)(st->usec_since_last_update / 1000000.0));
816 st->usec_since_last_update = st->update_every * 1000000ULL;
818 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s: microseconds since last update: %llu", st->name, st->usec_since_last_update);
820 // set last_collected_time
821 if(unlikely(!st->last_collected_time.tv_sec)) {
822 // it is the first entry
823 // set the last_collected_time to now
824 gettimeofday(&st->last_collected_time, NULL);
826 // the first entry should not be stored
827 store_this_entry = 0;
829 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s: initializing last_collected to now. Will not store the next entry.", st->name);
832 // it is not the first entry
833 // calculate the proper last_collected_time, using usec_since_last_update
834 unsigned long long ut = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec + st->usec_since_last_update;
835 st->last_collected_time.tv_sec = ut / 1000000ULL;
836 st->last_collected_time.tv_usec = ut % 1000000ULL;
839 // if this set has not been updated in the past
840 // we fake the last_update time to be = now - usec_since_last_update
841 if(unlikely(!st->last_updated.tv_sec)) {
842 // it has never been updated before
843 // set a fake last_updated, in the past using usec_since_last_update
844 unsigned long long ut = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec - st->usec_since_last_update;
845 st->last_updated.tv_sec = ut / 1000000ULL;
846 st->last_updated.tv_usec = ut % 1000000ULL;
848 // the first entry should not be stored
849 store_this_entry = 0;
851 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);
854 // check if we will re-write the entire data set
855 if(unlikely(usecdiff(&st->last_collected_time, &st->last_updated) > st->update_every * st->entries * 1000000ULL)) {
856 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);
859 st->usec_since_last_update = st->update_every * 1000000ULL;
861 gettimeofday(&st->last_collected_time, NULL);
863 unsigned long long ut = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec - st->usec_since_last_update;
864 st->last_updated.tv_sec = ut / 1000000ULL;
865 st->last_updated.tv_usec = ut % 1000000ULL;
867 // the first entry should not be stored
868 store_this_entry = 0;
871 // these are the 3 variables that will help us in interpolation
872 // last_ut = the last time we added a value to the storage
873 // now_ut = the time the current value is taken at
874 // next_ut = the time of the next interpolation point
875 unsigned long long last_ut = st->last_updated.tv_sec * 1000000ULL + st->last_updated.tv_usec;
876 unsigned long long now_ut = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec;
877 unsigned long long next_ut = (st->last_updated.tv_sec + st->update_every) * 1000000ULL;
879 if(unlikely(st->debug)) {
880 debug(D_RRD_STATS, "%s: last ut = %0.3Lf (last updated time)", st->name, (long double)last_ut/1000000.0);
881 debug(D_RRD_STATS, "%s: now ut = %0.3Lf (current update time)", st->name, (long double)now_ut/1000000.0);
882 debug(D_RRD_STATS, "%s: next ut = %0.3Lf (next interpolation point)", st->name, (long double)next_ut/1000000.0);
885 if(unlikely(!st->counter_done)) {
886 store_this_entry = 0;
887 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s: Will not store the next entry.", st->name);
891 // calculate totals and count the dimensions
893 st->collected_total = 0;
894 for( rd = st->dimensions, dimensions = 0 ; likely(rd) ; rd = rd->next, dimensions++ )
895 st->collected_total += rd->collected_value;
897 uint32_t storage_flags = SN_EXISTS;
899 // process all dimensions to calculate their values
900 // based on the collected figures only
901 // at this stage we do not interpolate anything
902 for( rd = st->dimensions ; likely(rd) ; rd = rd->next ) {
904 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s/%s: "
905 " last_collected_value = " COLLECTED_NUMBER_FORMAT
906 " collected_value = " COLLECTED_NUMBER_FORMAT
907 " last_calculated_value = " CALCULATED_NUMBER_FORMAT
908 " calculated_value = " CALCULATED_NUMBER_FORMAT
910 , rd->last_collected_value
911 , rd->collected_value
912 , rd->last_calculated_value
913 , rd->calculated_value
916 switch(rd->algorithm) {
917 case RRDDIM_PCENT_OVER_DIFF_TOTAL:
918 // the percentage of the current increment
919 // over the increment of all dimensions together
920 if(unlikely(st->collected_total == st->last_collected_total)) rd->calculated_value = rd->last_calculated_value;
921 else rd->calculated_value =
922 (calculated_number)100
923 * (calculated_number)(rd->collected_value - rd->last_collected_value)
924 / (calculated_number)(st->collected_total - st->last_collected_total);
926 if(unlikely(st->debug))
927 debug(D_RRD_STATS, "%s/%s: CALC PCENT-DIFF "
928 CALCULATED_NUMBER_FORMAT " = 100"
929 " * (" COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT ")"
930 " / (" COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT ")"
932 , rd->calculated_value
933 , rd->collected_value, rd->last_collected_value
934 , st->collected_total, st->last_collected_total
938 case RRDDIM_PCENT_OVER_ROW_TOTAL:
939 if(unlikely(!st->collected_total)) rd->calculated_value = 0;
941 // the percentage of the current value
942 // over the total of all dimensions
943 rd->calculated_value =
944 (calculated_number)100
945 * (calculated_number)rd->collected_value
946 / (calculated_number)st->collected_total;
948 if(unlikely(st->debug))
949 debug(D_RRD_STATS, "%s/%s: CALC PCENT-ROW "
950 CALCULATED_NUMBER_FORMAT " = 100"
951 " * " COLLECTED_NUMBER_FORMAT
952 " / " COLLECTED_NUMBER_FORMAT
954 , rd->calculated_value
955 , rd->collected_value
956 , st->collected_total
960 case RRDDIM_INCREMENTAL:
961 // if the new is smaller than the old (an overflow, or reset), set the old equal to the new
962 // to reset the calculation (it will give zero as the calculation for this second)
963 if(unlikely(rd->last_collected_value > rd->collected_value)) {
964 info("%s.%s: Detect RESET or OVERFLOW. Last collected value = " COLLECTED_NUMBER_FORMAT ", current = " COLLECTED_NUMBER_FORMAT
966 , rd->last_collected_value
967 , rd->collected_value);
968 storage_flags = SN_EXISTS_RESET;
969 rd->last_collected_value = rd->collected_value;
972 rd->calculated_value += (calculated_number)(rd->collected_value - rd->last_collected_value);
974 if(unlikely(st->debug))
975 debug(D_RRD_STATS, "%s/%s: CALC INC "
976 CALCULATED_NUMBER_FORMAT " += "
977 COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT
979 , rd->calculated_value
980 , rd->collected_value, rd->last_collected_value
984 case RRDDIM_ABSOLUTE:
985 rd->calculated_value = (calculated_number)rd->collected_value;
987 if(unlikely(st->debug))
988 debug(D_RRD_STATS, "%s/%s: CALC ABS/ABS-NO-IN "
989 CALCULATED_NUMBER_FORMAT " = "
990 COLLECTED_NUMBER_FORMAT
992 , rd->calculated_value
993 , rd->collected_value
998 // make the default zero, to make sure
999 // it gets noticed when we add new types
1000 rd->calculated_value = 0;
1002 if(unlikely(st->debug))
1003 debug(D_RRD_STATS, "%s/%s: CALC "
1004 CALCULATED_NUMBER_FORMAT " = 0"
1006 , rd->calculated_value
1012 // at this point we have all the calculated values ready
1013 // it is now time to interpolate values on a second boundary
1015 unsigned long long first_ut = last_ut;
1016 int iterations = (now_ut - last_ut) / (st->update_every * 1000000ULL);
1018 for( ; likely(next_ut <= now_ut) ; next_ut += st->update_every * 1000000ULL, iterations-- ) {
1019 if(iterations < 0) error("iterations calculation wrapped!");
1021 if(unlikely(st->debug)) {
1022 debug(D_RRD_STATS, "%s: last ut = %0.3Lf (last updated time)", st->name, (long double)last_ut/1000000.0);
1023 debug(D_RRD_STATS, "%s: next ut = %0.3Lf (next interpolation point)", st->name, (long double)next_ut/1000000.0);
1026 st->last_updated.tv_sec = next_ut / 1000000ULL;
1027 st->last_updated.tv_usec = 0;
1029 for( rd = st->dimensions ; likely(rd) ; rd = rd->next ) {
1030 calculated_number new_value;
1032 switch(rd->algorithm) {
1033 case RRDDIM_INCREMENTAL:
1034 new_value = (calculated_number)
1035 ( rd->calculated_value
1036 * (calculated_number)(next_ut - last_ut)
1037 / (calculated_number)(now_ut - last_ut)
1040 if(unlikely(st->debug))
1041 debug(D_RRD_STATS, "%s/%s: CALC2 INC "
1042 CALCULATED_NUMBER_FORMAT " = "
1043 CALCULATED_NUMBER_FORMAT
1048 , rd->calculated_value
1049 , (unsigned long long)(next_ut - last_ut)
1050 , (unsigned long long)(now_ut - last_ut)
1053 rd->calculated_value -= new_value;
1056 case RRDDIM_ABSOLUTE:
1057 case RRDDIM_PCENT_OVER_ROW_TOTAL:
1058 case RRDDIM_PCENT_OVER_DIFF_TOTAL:
1060 new_value = (calculated_number)
1061 ( ( (rd->calculated_value - rd->last_calculated_value)
1062 * (calculated_number)(next_ut - first_ut)
1063 / (calculated_number)(now_ut - first_ut)
1065 + rd->last_calculated_value
1068 if(unlikely(st->debug))
1069 debug(D_RRD_STATS, "%s/%s: CALC2 DEF "
1070 CALCULATED_NUMBER_FORMAT " = ((("
1071 "(" CALCULATED_NUMBER_FORMAT " - " CALCULATED_NUMBER_FORMAT ")"
1073 " / %llu) + " CALCULATED_NUMBER_FORMAT
1076 , rd->calculated_value, rd->last_calculated_value
1077 , (next_ut - first_ut)
1078 , (now_ut - first_ut), rd->last_calculated_value
1081 if(likely(next_ut + st->update_every * 1000000ULL > now_ut)) rd->calculated_value = new_value;
1085 if(unlikely(!store_this_entry)) {
1086 store_this_entry = 1;
1090 if(likely(rd->updated && iterations < st->gap_when_lost_iterations_above)) {
1091 rd->values[st->current_entry] = pack_storage_number(
1093 * (calculated_number)rd->multiplier
1094 / (calculated_number)rd->divisor
1097 if(unlikely(st->debug))
1098 debug(D_RRD_STATS, "%s/%s: STORE[%ld] "
1099 CALCULATED_NUMBER_FORMAT " = " CALCULATED_NUMBER_FORMAT
1104 , unpack_storage_number(rd->values[st->current_entry]), new_value
1110 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s/%s: STORE[%ld] = NON EXISTING "
1114 rd->values[st->current_entry] = pack_storage_number(0, SN_NOT_EXISTS);
1117 if(unlikely(st->debug)) {
1118 calculated_number t1 = new_value * (calculated_number)rd->multiplier / (calculated_number)rd->divisor;
1119 calculated_number t2 = unpack_storage_number(rd->values[st->current_entry]);
1120 calculated_number accuracy = accuracy_loss(t1, t2);
1121 debug(D_RRD_STATS, "%s/%s: UNPACK[%ld] = " CALCULATED_NUMBER_FORMAT " FLAGS=0x%08x (original = " CALCULATED_NUMBER_FORMAT ", accuracy loss = " CALCULATED_NUMBER_FORMAT "%%%s)"
1125 , get_storage_number_flags(rd->values[st->current_entry])
1128 , (accuracy > ACCURACY_LOSS) ? " **TOO BIG** " : ""
1131 rd->collected_volume += t1;
1132 rd->stored_volume += t2;
1133 accuracy = accuracy_loss(rd->collected_volume, rd->stored_volume);
1134 debug(D_RRD_STATS, "%s/%s: VOLUME[%ld] = " CALCULATED_NUMBER_FORMAT ", calculated = " CALCULATED_NUMBER_FORMAT ", accuracy loss = " CALCULATED_NUMBER_FORMAT "%%%s"
1138 , rd->collected_volume
1140 , (accuracy > ACCURACY_LOSS) ? " **TOO BIG** " : ""
1145 // reset the storage flags for the next point, if any;
1146 storage_flags = SN_EXISTS;
1149 st->current_entry = ((st->current_entry + 1) >= st->entries) ? 0 : st->current_entry + 1;
1153 for( rd = st->dimensions; likely(rd) ; rd = rd->next ) {
1154 if(unlikely(!rd->updated)) continue;
1155 rd->last_collected_value = rd->collected_value;
1156 rd->last_calculated_value = rd->calculated_value;
1157 rd->collected_value = 0;
1160 // if this is the first entry of incremental dimensions
1161 // we have to set the first calculated_value to zero
1162 // to eliminate the first spike
1163 if(unlikely(st->counter_done == 1)) switch(rd->algorithm) {
1164 case RRDDIM_PCENT_OVER_DIFF_TOTAL:
1165 case RRDDIM_INCREMENTAL:
1166 rd->calculated_value = 0;
1167 // the next time, a new incremental total will be calculated
1171 st->last_collected_total = st->collected_total;
1173 // ALL DONE ABOUT THE DATA UPDATE
1174 // --------------------------------------------------------------------
1176 // find if there are any obsolete dimensions (not updated recently)
1177 if(unlikely(rrd_delete_unupdated_dimensions)) {
1179 for( rd = st->dimensions; likely(rd) ; rd = rd->next )
1180 if((rd->last_collected_time.tv_sec + (rrd_delete_unupdated_dimensions * st->update_every)) < st->last_collected_time.tv_sec)
1184 // there is dimension to free
1185 // upgrade our read lock to a write lock
1186 pthread_rwlock_unlock(&st->rwlock);
1187 pthread_rwlock_wrlock(&st->rwlock);
1189 for( rd = st->dimensions, last = NULL ; likely(rd) ; ) {
1190 // remove it only it is not updated in rrd_delete_unupdated_dimensions seconds
1192 if(unlikely((rd->last_collected_time.tv_sec + (rrd_delete_unupdated_dimensions * st->update_every)) < st->last_collected_time.tv_sec)) {
1193 info("Removing obsolete dimension '%s' (%s) of '%s' (%s).", rd->name, rd->id, st->name, st->id);
1195 if(unlikely(!last)) {
1196 st->dimensions = rd->next;
1198 rrddim_free(st, rd);
1199 rd = st->dimensions;
1203 last->next = rd->next;
1205 rrddim_free(st, rd);
1215 if(unlikely(!st->dimensions)) {
1216 info("Disabling chart %s (%s) since it does not have any dimensions", st->name, st->id);
1222 pthread_rwlock_unlock(&st->rwlock);
1224 if(unlikely(pthread_setcancelstate(oldstate, NULL) != 0))
1225 error("Cannot set pthread cancel state to RESTORE (%d).", oldstate);
1227 return(st->usec_since_last_update);