11 #include <sys/types.h>
20 #define RRD_DEFAULT_GAP_INTERPOLATIONS 10
22 // ----------------------------------------------------------------------------
25 int rrd_update_every = UPDATE_EVERY;
26 int rrd_default_history_entries = RRD_DEFAULT_HISTORY_ENTRIES;
28 RRDSET *rrdset_root = NULL;
29 pthread_rwlock_t rrdset_root_rwlock = PTHREAD_RWLOCK_INITIALIZER;
31 int rrd_memory_mode = RRD_MEMORY_MODE_SAVE;
34 // ----------------------------------------------------------------------------
37 static int rrdset_iterator(avl *a) { if(a) {}; return 0; }
39 static int rrdset_compare(void* a, void* b) {
40 if(((RRDSET *)a)->hash < ((RRDSET *)b)->hash) return -1;
41 else if(((RRDSET *)a)->hash > ((RRDSET *)b)->hash) return 1;
42 else return strcmp(((RRDSET *)a)->id, ((RRDSET *)b)->id);
45 avl_tree rrdset_root_index = {
50 #define rrdset_index_add(st) avl_insert(&rrdset_root_index, (avl *)(st))
51 #define rrdset_index_del(st) avl_remove(&rrdset_root_index, (avl *)(st))
53 static RRDSET *rrdset_index_find(const char *id, uint32_t hash) {
54 RRDSET *result = NULL, tmp;
55 strncpy(tmp.id, id, RRD_ID_LENGTH_MAX);
56 tmp.id[RRD_ID_LENGTH_MAX] = '\0';
57 tmp.hash = (hash)?hash:simple_hash(tmp.id);
59 avl_search(&(rrdset_root_index), (avl *)&tmp, rrdset_iterator, (avl **)&result);
63 // ----------------------------------------------------------------------------
66 #define rrdset_from_avlname(avlname_ptr) ((RRDSET *)((avlname_ptr) - offsetof(RRDSET, avlname)))
68 static int rrdset_iterator_name(avl *a) { if(a) {}; return 0; }
70 static int rrdset_compare_name(void* a, void* b) {
71 RRDSET *A = rrdset_from_avlname(a);
72 RRDSET *B = rrdset_from_avlname(b);
74 // fprintf(stderr, "COMPARING: %s with %s\n", A->name, B->name);
76 if(A->hash_name < B->hash_name) return -1;
77 else if(A->hash_name > B->hash_name) return 1;
78 else return strcmp(A->name, B->name);
81 avl_tree rrdset_root_index_name = {
86 int rrdset_index_add_name(RRDSET *st) {
87 // fprintf(stderr, "ADDING: %s (name: %s)\n", st->id, st->name);
88 return avl_insert(&rrdset_root_index_name, (avl *)(&st->avlname));
91 #define rrdset_index_del_name(st) avl_remove(&rrdset_root_index_name, (avl *)(&st->avlname))
93 static RRDSET *rrdset_index_find_name(const char *name, uint32_t hash) {
97 tmp.hash_name = (hash)?hash:simple_hash(tmp.name);
99 // fprintf(stderr, "SEARCHING: %s\n", name);
100 avl_search(&(rrdset_root_index_name), (avl *)(&(tmp.avlname)), rrdset_iterator_name, (avl **)&result);
102 RRDSET *st = rrdset_from_avlname(result);
103 if(strcmp(st->magic, RRDSET_MAGIC))
104 error("Search for RRDSET %s returned an invalid RRDSET %s (name %s)", name, st->id, st->name);
106 // fprintf(stderr, "FOUND: %s\n", name);
107 return rrdset_from_avlname(result);
109 // fprintf(stderr, "NOT FOUND: %s\n", name);
114 // ----------------------------------------------------------------------------
117 static int rrddim_iterator(avl *a) { if(a) {}; return 0; }
119 static int rrddim_compare(void* a, void* b) {
120 if(((RRDDIM *)a)->hash < ((RRDDIM *)b)->hash) return -1;
121 else if(((RRDDIM *)a)->hash > ((RRDDIM *)b)->hash) return 1;
122 else return strcmp(((RRDDIM *)a)->id, ((RRDDIM *)b)->id);
125 #define rrddim_index_add(st, rd) avl_insert(&((st)->dimensions_index), (avl *)(rd))
126 #define rrddim_index_del(st,rd ) avl_remove(&((st)->dimensions_index), (avl *)(rd))
128 static RRDDIM *rrddim_index_find(RRDSET *st, const char *id, uint32_t hash) {
129 RRDDIM *result = NULL, tmp;
130 strncpy(tmp.id, id, RRD_ID_LENGTH_MAX);
131 tmp.id[RRD_ID_LENGTH_MAX] = '\0';
132 tmp.hash = (hash)?hash:simple_hash(tmp.id);
134 avl_search(&(st->dimensions_index), (avl *)&tmp, rrddim_iterator, (avl **)&result);
138 // ----------------------------------------------------------------------------
141 int rrdset_type_id(const char *name)
143 if(strcmp(name, RRDSET_TYPE_AREA_NAME) == 0) return RRDSET_TYPE_AREA;
144 if(strcmp(name, RRDSET_TYPE_STACKED_NAME) == 0) return RRDSET_TYPE_STACKED;
145 if(strcmp(name, RRDSET_TYPE_LINE_NAME) == 0) return RRDSET_TYPE_LINE;
146 return RRDSET_TYPE_LINE;
149 const char *rrdset_type_name(int chart_type)
151 static char line[] = RRDSET_TYPE_LINE_NAME;
152 static char area[] = RRDSET_TYPE_AREA_NAME;
153 static char stacked[] = RRDSET_TYPE_STACKED_NAME;
156 case RRDSET_TYPE_LINE:
159 case RRDSET_TYPE_AREA:
162 case RRDSET_TYPE_STACKED:
168 // ----------------------------------------------------------------------------
171 const char *rrd_memory_mode_name(int id)
173 static const char ram[] = RRD_MEMORY_MODE_RAM_NAME;
174 static const char map[] = RRD_MEMORY_MODE_MAP_NAME;
175 static const char save[] = RRD_MEMORY_MODE_SAVE_NAME;
178 case RRD_MEMORY_MODE_RAM:
181 case RRD_MEMORY_MODE_MAP:
184 case RRD_MEMORY_MODE_SAVE:
192 int rrd_memory_mode_id(const char *name)
194 if(!strcmp(name, RRD_MEMORY_MODE_RAM_NAME))
195 return RRD_MEMORY_MODE_RAM;
196 else if(!strcmp(name, RRD_MEMORY_MODE_MAP_NAME))
197 return RRD_MEMORY_MODE_MAP;
199 return RRD_MEMORY_MODE_SAVE;
202 // ----------------------------------------------------------------------------
205 int rrddim_algorithm_id(const char *name)
207 if(strcmp(name, RRDDIM_ABSOLUTE_NAME) == 0) return RRDDIM_ABSOLUTE;
208 if(strcmp(name, RRDDIM_INCREMENTAL_NAME) == 0) return RRDDIM_INCREMENTAL;
209 if(strcmp(name, RRDDIM_PCENT_OVER_ROW_TOTAL_NAME) == 0) return RRDDIM_PCENT_OVER_ROW_TOTAL;
210 if(strcmp(name, RRDDIM_PCENT_OVER_DIFF_TOTAL_NAME) == 0) return RRDDIM_PCENT_OVER_DIFF_TOTAL;
211 return RRDDIM_ABSOLUTE;
214 const char *rrddim_algorithm_name(int chart_type)
216 static char absolute[] = RRDDIM_ABSOLUTE_NAME;
217 static char incremental[] = RRDDIM_INCREMENTAL_NAME;
218 static char percentage_of_absolute_row[] = RRDDIM_PCENT_OVER_ROW_TOTAL_NAME;
219 static char percentage_of_incremental_row[] = RRDDIM_PCENT_OVER_DIFF_TOTAL_NAME;
222 case RRDDIM_ABSOLUTE:
225 case RRDDIM_INCREMENTAL:
228 case RRDDIM_PCENT_OVER_ROW_TOTAL:
229 return percentage_of_absolute_row;
231 case RRDDIM_PCENT_OVER_DIFF_TOTAL:
232 return percentage_of_incremental_row;
237 // ----------------------------------------------------------------------------
240 char *rrdset_strncpy_name(char *to, const char *from, int length)
243 for(i = 0; i < length && from[i] ;i++) {
244 if(from[i] == '.' || isalpha(from[i]) || isdigit(from[i])) to[i] = from[i];
247 if(i < length) to[i] = '\0';
248 to[length - 1] = '\0';
253 void rrdset_set_name(RRDSET *st, const char *name)
255 debug(D_RRD_CALLS, "rrdset_set_name() old: %s, new: %s", st->name, name);
257 if(st->name) rrdset_index_del_name(st);
259 char b[CONFIG_MAX_VALUE + 1];
260 char n[RRD_ID_LENGTH_MAX + 1];
262 snprintf(n, RRD_ID_LENGTH_MAX, "%s.%s", st->type, name);
263 rrdset_strncpy_name(b, n, CONFIG_MAX_VALUE);
264 st->name = config_get(st->id, "name", b);
265 st->hash_name = simple_hash(st->name);
267 rrdset_index_add_name(st);
270 // ----------------------------------------------------------------------------
273 char *rrdset_cache_dir(const char *id)
277 static char *cache_dir = NULL;
278 if(!cache_dir) cache_dir = config_get("global", "database directory", "cache");
280 char b[FILENAME_MAX + 1];
281 char n[FILENAME_MAX + 1];
282 rrdset_strncpy_name(b, id, FILENAME_MAX);
284 snprintf(n, FILENAME_MAX, "%s/%s", cache_dir, b);
285 ret = config_get(id, "database directory", n);
287 if(rrd_memory_mode == RRD_MEMORY_MODE_MAP || rrd_memory_mode == RRD_MEMORY_MODE_SAVE) {
288 int r = mkdir(ret, 0775);
289 if(r != 0 && errno != EEXIST)
290 error("Cannot create directory '%s'", ret);
296 // ----------------------------------------------------------------------------
299 void rrdset_reset(RRDSET *st)
301 debug(D_RRD_CALLS, "rrdset_reset() %s", st->name);
303 st->last_collected_time.tv_sec = 0;
304 st->last_collected_time.tv_usec = 0;
305 st->last_updated.tv_sec = 0;
306 st->last_updated.tv_usec = 0;
307 st->current_entry = 0;
309 st->counter_done = 0;
312 for(rd = st->dimensions; rd ; rd = rd->next) {
313 rd->last_collected_time.tv_sec = 0;
314 rd->last_collected_time.tv_usec = 0;
315 bzero(rd->values, rd->entries * sizeof(storage_number));
319 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)
322 fatal("Cannot create rrd stats without an id.");
326 char fullid[RRD_ID_LENGTH_MAX + 1];
327 char fullfilename[FILENAME_MAX + 1];
330 snprintf(fullid, RRD_ID_LENGTH_MAX, "%s.%s", type, id);
332 long entries = config_get_number(fullid, "history", rrd_default_history_entries);
333 if(entries < 5) entries = config_set_number(fullid, "history", 5);
334 if(entries > RRD_HISTORY_ENTRIES_MAX) entries = config_set_number(fullid, "history", RRD_HISTORY_ENTRIES_MAX);
336 int enabled = config_get_boolean(fullid, "enabled", 1);
337 if(!enabled) entries = 5;
339 unsigned long size = sizeof(RRDSET);
340 char *cache_dir = rrdset_cache_dir(fullid);
342 debug(D_RRD_CALLS, "Creating RRD_STATS for '%s.%s'.", type, id);
344 snprintf(fullfilename, FILENAME_MAX, "%s/main.db", cache_dir);
345 if(rrd_memory_mode != RRD_MEMORY_MODE_RAM) st = (RRDSET *)mymmap(fullfilename, size, ((rrd_memory_mode == RRD_MEMORY_MODE_MAP)?MAP_SHARED:MAP_PRIVATE));
347 if(strcmp(st->magic, RRDSET_MAGIC) != 0) {
349 info("Initializing file %s.", fullfilename);
352 else if(strcmp(st->id, fullid) != 0) {
354 error("File %s contents are not for chart %s. Clearing it.", fullfilename, fullid);
359 else if(st->memsize != size || st->entries != entries) {
361 error("File %s does not have the desired size. Clearing it.", fullfilename);
364 else if(st->update_every != update_every) {
366 error("File %s does not have the desired update frequency. Clearing it.", fullfilename);
369 else if((time(NULL) - st->last_updated.tv_sec) > update_every * entries) {
371 error("File %s is too old. Clearing it.", fullfilename);
382 st->dimensions = NULL;
384 st->mapped = rrd_memory_mode;
387 st = calloc(1, size);
389 fatal("Cannot allocate memory for RRD_STATS %s.%s", type, id);
392 st->mapped = RRD_MEMORY_MODE_RAM;
395 st->entries = entries;
396 st->update_every = update_every;
398 strcpy(st->cache_filename, fullfilename);
399 strcpy(st->magic, RRDSET_MAGIC);
401 strcpy(st->id, fullid);
402 st->hash = simple_hash(st->id);
404 st->cache_dir = cache_dir;
406 st->family = config_get(st->id, "family", family?family:st->id);
407 st->units = config_get(st->id, "units", units?units:"");
408 st->type = config_get(st->id, "type", type);
409 st->chart_type = rrdset_type_id(config_get(st->id, "chart type", rrdset_type_name(chart_type)));
411 st->priority = config_get_number(st->id, "priority", priority);
412 st->enabled = enabled;
417 st->last_collected_time.tv_sec = 0;
418 st->last_collected_time.tv_usec = 0;
419 st->counter_done = 0;
421 st->gap_when_lost_iterations_above = config_get_number(st->id, "gap when lost iterations above", RRD_DEFAULT_GAP_INTERPOLATIONS);
423 st->dimensions_index.root = NULL;
424 st->dimensions_index.compar = rrddim_compare;
426 pthread_rwlock_init(&st->rwlock, NULL);
427 pthread_rwlock_wrlock(&rrdset_root_rwlock);
429 if(name && *name) rrdset_set_name(st, name);
430 else rrdset_set_name(st, id);
433 char varvalue[CONFIG_MAX_VALUE + 1];
434 snprintf(varvalue, CONFIG_MAX_VALUE, "%s (%s)", title?title:"", st->name);
435 st->title = config_get(st->id, "title", varvalue);
438 st->next = rrdset_root;
441 rrdset_index_add(st);
443 pthread_rwlock_unlock(&rrdset_root_rwlock);
448 RRDDIM *rrddim_add(RRDSET *st, const char *id, const char *name, long multiplier, long divisor, int algorithm)
450 char filename[FILENAME_MAX + 1];
451 char fullfilename[FILENAME_MAX + 1];
453 char varname[CONFIG_MAX_NAME + 1];
455 unsigned long size = sizeof(RRDDIM) + (st->entries * sizeof(storage_number));
457 debug(D_RRD_CALLS, "Adding dimension '%s/%s'.", st->id, id);
459 rrdset_strncpy_name(filename, id, FILENAME_MAX);
460 snprintf(fullfilename, FILENAME_MAX, "%s/%s.db", st->cache_dir, filename);
461 if(rrd_memory_mode != RRD_MEMORY_MODE_RAM) rd = (RRDDIM *)mymmap(fullfilename, size, ((rrd_memory_mode == RRD_MEMORY_MODE_MAP)?MAP_SHARED:MAP_PRIVATE));
464 gettimeofday(&now, NULL);
466 if(strcmp(rd->magic, RRDDIMENSION_MAGIC) != 0) {
468 info("Initializing file %s.", fullfilename);
471 else if(rd->memsize != size) {
473 error("File %s does not have the desired size. Clearing it.", fullfilename);
476 else if(rd->multiplier != multiplier) {
478 error("File %s does not have the same multiplier. Clearing it.", fullfilename);
481 else if(rd->divisor != divisor) {
483 error("File %s does not have the same divisor. Clearing it.", fullfilename);
486 else if(rd->algorithm != algorithm) {
488 error("File %s does not have the same algorithm. Clearing it.", fullfilename);
491 else if(rd->update_every != st->update_every) {
493 error("File %s does not have the same refresh frequency. Clearing it.", fullfilename);
496 else if(usecdiff(&now, &rd->last_collected_time) > (rd->entries * rd->update_every * 1000000ULL)) {
498 error("File %s is too old. Clearing it.", fullfilename);
501 else if(strcmp(rd->id, id) != 0) {
503 error("File %s contents are not for dimension %s. Clearing it.", fullfilename, id);
511 // we have a file mapped for rd
512 rd->mapped = rrd_memory_mode;
518 // if we didn't manage to get a mmap'd dimension, just create one
520 rd = calloc(1, size);
522 fatal("Cannot allocate RRD_DIMENSION %s/%s.", st->id, id);
526 rd->mapped = RRD_MEMORY_MODE_RAM;
530 strcpy(rd->magic, RRDDIMENSION_MAGIC);
531 strcpy(rd->cache_filename, fullfilename);
532 strncpy(rd->id, id, RRD_ID_LENGTH_MAX);
533 rd->hash = simple_hash(rd->id);
535 snprintf(varname, CONFIG_MAX_NAME, "dim %s name", rd->id);
536 rd->name = config_get(st->id, varname, (name && *name)?name:rd->id);
538 snprintf(varname, CONFIG_MAX_NAME, "dim %s algorithm", rd->id);
539 rd->algorithm = rrddim_algorithm_id(config_get(st->id, varname, rrddim_algorithm_name(algorithm)));
541 snprintf(varname, CONFIG_MAX_NAME, "dim %s multiplier", rd->id);
542 rd->multiplier = config_get_number(st->id, varname, multiplier);
544 snprintf(varname, CONFIG_MAX_NAME, "dim %s divisor", rd->id);
545 rd->divisor = config_get_number(st->id, varname, divisor);
546 if(!rd->divisor) rd->divisor = 1;
548 rd->entries = st->entries;
549 rd->update_every = st->update_every;
551 // append this dimension
555 RRDDIM *td = st->dimensions;
556 for(; td->next; td = td->next) ;
560 rrddim_index_add(st, rd);
565 void rrddim_set_name(RRDSET *st, RRDDIM *rd, const char *name)
567 debug(D_RRD_CALLS, "rrddim_set_name() %s.%s", st->name, rd->name);
569 char varname[CONFIG_MAX_NAME + 1];
570 snprintf(varname, CONFIG_MAX_NAME, "dim %s name", rd->id);
571 config_get(st->id, varname, name);
574 void rrddim_free(RRDSET *st, RRDDIM *rd)
576 debug(D_RRD_CALLS, "rrddim_free() %s.%s", st->name, rd->name);
578 RRDDIM *i = st->dimensions, *last = NULL;
579 for(i = st->dimensions; i && i != rd ; i = i->next) last = i;
582 error("Request to free dimension %s.%s but it is not linked.", st->id, rd->name);
586 if(last) last = i->next;
587 else st->dimensions = i->next;
589 rrddim_index_del(st, rd);
591 // free(rd->annotations);
592 if(rd->mapped == RRD_MEMORY_MODE_SAVE) {
593 debug(D_RRD_CALLS, "Saving dimension '%s' to '%s'.", rd->name, rd->cache_filename);
594 savememory(rd->cache_filename, rd, rd->memsize);
596 debug(D_RRD_CALLS, "Unmapping dimension '%s'.", rd->name);
597 munmap(rd, rd->memsize);
599 else if(rd->mapped == RRD_MEMORY_MODE_MAP) {
600 debug(D_RRD_CALLS, "Unmapping dimension '%s'.", rd->name);
601 munmap(rd, rd->memsize);
604 debug(D_RRD_CALLS, "Removing dimension '%s'.", rd->name);
609 void rrdset_free_all(void)
611 info("Freeing all memory...");
614 for(st = rrdset_root; st ;) {
615 RRDSET *next = st->next;
617 while(st->dimensions)
618 rrddim_free(st, st->dimensions);
620 rrdset_index_del(st);
622 if(st->mapped == RRD_MEMORY_MODE_SAVE) {
623 debug(D_RRD_CALLS, "Saving stats '%s' to '%s'.", st->name, st->cache_filename);
624 savememory(st->cache_filename, st, st->memsize);
626 debug(D_RRD_CALLS, "Unmapping stats '%s'.", st->name);
627 munmap(st, st->memsize);
629 else if(st->mapped == RRD_MEMORY_MODE_MAP) {
630 debug(D_RRD_CALLS, "Unmapping stats '%s'.", st->name);
631 munmap(st, st->memsize);
640 info("Memory cleanup completed...");
643 void rrdset_save_all(void)
645 debug(D_RRD_CALLS, "rrdset_save_all()");
650 pthread_rwlock_wrlock(&rrdset_root_rwlock);
651 for(st = rrdset_root; st ; st = st->next) {
652 pthread_rwlock_wrlock(&st->rwlock);
654 if(st->mapped == RRD_MEMORY_MODE_SAVE) {
655 debug(D_RRD_CALLS, "Saving stats '%s' to '%s'.", st->name, st->cache_filename);
656 savememory(st->cache_filename, st, st->memsize);
659 for(rd = st->dimensions; rd ; rd = rd->next) {
660 if(rd->mapped == RRD_MEMORY_MODE_SAVE) {
661 debug(D_RRD_CALLS, "Saving dimension '%s' to '%s'.", rd->name, rd->cache_filename);
662 savememory(rd->cache_filename, rd, rd->memsize);
666 pthread_rwlock_unlock(&st->rwlock);
668 pthread_rwlock_unlock(&rrdset_root_rwlock);
672 RRDSET *rrdset_find(const char *id)
674 debug(D_RRD_CALLS, "rrdset_find() for chart %s", id);
676 pthread_rwlock_rdlock(&rrdset_root_rwlock);
677 RRDSET *st = rrdset_index_find(id, 0);
678 pthread_rwlock_unlock(&rrdset_root_rwlock);
683 RRDSET *rrdset_find_bytype(const char *type, const char *id)
685 debug(D_RRD_CALLS, "rrdset_find_bytype() for chart %s.%s", type, id);
687 char buf[RRD_ID_LENGTH_MAX + 1];
689 strncpy(buf, type, RRD_ID_LENGTH_MAX - 1);
690 buf[RRD_ID_LENGTH_MAX - 1] = '\0';
692 int len = strlen(buf);
693 strncpy(&buf[len], id, RRD_ID_LENGTH_MAX - len);
694 buf[RRD_ID_LENGTH_MAX] = '\0';
696 return(rrdset_find(buf));
699 RRDSET *rrdset_find_byname(const char *name)
701 debug(D_RRD_CALLS, "rrdset_find_byname() for chart %s", name);
703 pthread_rwlock_rdlock(&rrdset_root_rwlock);
704 RRDSET *st = rrdset_index_find_name(name, 0);
705 pthread_rwlock_unlock(&rrdset_root_rwlock);
710 RRDDIM *rrddim_find(RRDSET *st, const char *id)
712 debug(D_RRD_CALLS, "rrddim_find() for chart %s, dimension %s", st->name, id);
714 return rrddim_index_find(st, id, 0);
717 int rrddim_hide(RRDSET *st, const char *id)
719 debug(D_RRD_CALLS, "rrddim_hide() for chart %s, dimension %s", st->name, id);
721 RRDDIM *rd = rrddim_find(st, id);
723 error("Cannot find dimension with id '%s' on stats '%s' (%s).", id, st->name, st->id);
731 void rrddim_set_by_pointer(RRDSET *st, RRDDIM *rd, collected_number value)
733 debug(D_RRD_CALLS, "rrddim_set_by_pointer() for chart %s, dimension %s, value " COLLECTED_NUMBER_FORMAT, st->name, rd->name, value);
735 gettimeofday(&rd->last_collected_time, NULL);
736 rd->collected_value = value;
740 int rrddim_set(RRDSET *st, const char *id, collected_number value)
742 RRDDIM *rd = rrddim_find(st, id);
744 error("Cannot find dimension with id '%s' on stats '%s' (%s).", id, st->name, st->id);
748 rrddim_set_by_pointer(st, rd, value);
752 void rrdset_next_usec(RRDSET *st, unsigned long long microseconds)
754 debug(D_RRD_CALLS, "rrdset_next_usec() for chart %s with microseconds %llu", st->name, microseconds);
756 if(st->debug) debug(D_RRD_STATS, "%s: NEXT: %llu microseconds", st->name, microseconds);
757 st->usec_since_last_update = microseconds;
760 void rrdset_next(RRDSET *st)
762 unsigned long long microseconds = 0;
764 if(st->last_collected_time.tv_sec) {
766 gettimeofday(&now, NULL);
767 microseconds = usecdiff(&now, &st->last_collected_time);
770 rrdset_next_usec(st, microseconds);
773 void rrdset_next_plugins(RRDSET *st)
778 unsigned long long rrdset_done(RRDSET *st)
780 debug(D_RRD_CALLS, "rrdset_done() for chart %s", st->name);
783 int oldstate, store_this_entry = 1;
785 if(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate) != 0)
786 error("Cannot set pthread cancel state to DISABLE.");
788 // a read lock is OK here
789 pthread_rwlock_rdlock(&st->rwlock);
791 // check if the chart has a long time to be refreshed
792 if(st->usec_since_last_update > st->entries * st->update_every * 1000000ULL) {
793 info("%s: took too long to be updated (%0.3Lf secs). Reseting it.", st->name, (long double)(st->usec_since_last_update / 1000000.0));
795 st->usec_since_last_update = st->update_every * 1000000ULL;
797 if(st->debug) debug(D_RRD_STATS, "%s: microseconds since last update: %llu", st->name, st->usec_since_last_update);
799 // set last_collected_time
800 if(!st->last_collected_time.tv_sec) {
801 // it is the first entry
802 // set the last_collected_time to now
803 gettimeofday(&st->last_collected_time, NULL);
805 // the first entry should not be stored
806 store_this_entry = 0;
808 if(st->debug) debug(D_RRD_STATS, "%s: initializing last_collected to now. Will not store the next entry.", st->name);
811 // it is not the first entry
812 // calculate the proper last_collected_time, using usec_since_last_update
813 unsigned long long ut = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec + st->usec_since_last_update;
814 st->last_collected_time.tv_sec = ut / 1000000ULL;
815 st->last_collected_time.tv_usec = ut % 1000000ULL;
818 // if this set has not been updated in the past
819 // we fake the last_update time to be = now - usec_since_last_update
820 if(!st->last_updated.tv_sec) {
821 // it has never been updated before
822 // set a fake last_updated, in the past using usec_since_last_update
823 unsigned long long ut = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec - st->usec_since_last_update;
824 st->last_updated.tv_sec = ut / 1000000ULL;
825 st->last_updated.tv_usec = ut % 1000000ULL;
827 // the first entry should not be stored
828 store_this_entry = 0;
830 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);
833 // check if we will re-write the entire data set
834 if(usecdiff(&st->last_collected_time, &st->last_updated) > st->update_every * st->entries * 1000000ULL) {
835 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);
838 st->usec_since_last_update = st->update_every * 1000000ULL;
840 gettimeofday(&st->last_collected_time, NULL);
842 unsigned long long ut = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec - st->usec_since_last_update;
843 st->last_updated.tv_sec = ut / 1000000ULL;
844 st->last_updated.tv_usec = ut % 1000000ULL;
846 // the first entry should not be stored
847 store_this_entry = 0;
850 // these are the 3 variables that will help us in interpolation
851 // last_ut = the last time we added a value to the storage
852 // now_ut = the time the current value is taken at
853 // next_ut = the time of the next interpolation point
854 unsigned long long last_ut = st->last_updated.tv_sec * 1000000ULL + st->last_updated.tv_usec;
855 unsigned long long now_ut = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec;
856 unsigned long long next_ut = (st->last_updated.tv_sec + st->update_every) * 1000000ULL;
858 if(st->debug) debug(D_RRD_STATS, "%s: last ut = %0.3Lf (last updated time)", st->name, (long double)last_ut/1000000.0);
859 if(st->debug) debug(D_RRD_STATS, "%s: now ut = %0.3Lf (current update time)", st->name, (long double)now_ut/1000000.0);
860 if(st->debug) debug(D_RRD_STATS, "%s: next ut = %0.3Lf (next interpolation point)", st->name, (long double)next_ut/1000000.0);
862 if(!st->counter_done) {
863 store_this_entry = 0;
864 if(st->debug) debug(D_RRD_STATS, "%s: Will not store the next entry.", st->name);
868 // calculate totals and count the dimensions
870 st->collected_total = 0;
871 for( rd = st->dimensions, dimensions = 0 ; rd ; rd = rd->next, dimensions++ )
872 st->collected_total += rd->collected_value;
874 uint32_t storage_flags = SN_EXISTS;
876 // process all dimensions to calculate their values
877 // based on the collected figures only
878 // at this stage we do not interpolate anything
879 for( rd = st->dimensions ; rd ; rd = rd->next ) {
881 if(st->debug) debug(D_RRD_STATS, "%s/%s: "
882 " last_collected_value = " COLLECTED_NUMBER_FORMAT
883 " collected_value = " COLLECTED_NUMBER_FORMAT
884 " last_calculated_value = " CALCULATED_NUMBER_FORMAT
885 " calculated_value = " CALCULATED_NUMBER_FORMAT
887 , rd->last_collected_value
888 , rd->collected_value
889 , rd->last_calculated_value
890 , rd->calculated_value
893 switch(rd->algorithm) {
894 case RRDDIM_PCENT_OVER_DIFF_TOTAL:
895 // the percentage of the current increment
896 // over the increment of all dimensions together
897 if(st->collected_total == st->last_collected_total) rd->calculated_value = rd->last_calculated_value;
898 else rd->calculated_value =
899 (calculated_number)100
900 * (calculated_number)(rd->collected_value - rd->last_collected_value)
901 / (calculated_number)(st->collected_total - st->last_collected_total);
904 debug(D_RRD_STATS, "%s/%s: CALC PCENT-DIFF "
905 CALCULATED_NUMBER_FORMAT " = 100"
906 " * (" COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT ")"
907 " / (" COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT ")"
909 , rd->calculated_value
910 , rd->collected_value, rd->last_collected_value
911 , st->collected_total, st->last_collected_total
915 case RRDDIM_PCENT_OVER_ROW_TOTAL:
916 if(!st->collected_total) rd->calculated_value = 0;
918 // the percentage of the current value
919 // over the total of all dimensions
920 rd->calculated_value =
921 (calculated_number)100
922 * (calculated_number)rd->collected_value
923 / (calculated_number)st->collected_total;
926 debug(D_RRD_STATS, "%s/%s: CALC PCENT-ROW "
927 CALCULATED_NUMBER_FORMAT " = 100"
928 " * " COLLECTED_NUMBER_FORMAT
929 " / " COLLECTED_NUMBER_FORMAT
931 , rd->calculated_value
932 , rd->collected_value
933 , st->collected_total
937 case RRDDIM_INCREMENTAL:
938 // if the new is smaller than the old (an overflow, or reset), set the old equal to the new
939 // to reset the calculation (it will give zero as the calculation for this second)
940 if(rd->last_collected_value > rd->collected_value) {
941 info("%s.%s: Detect RESET or OVERFLOW. Last collected value = " COLLECTED_NUMBER_FORMAT ", current = " COLLECTED_NUMBER_FORMAT
943 , rd->last_collected_value
944 , rd->collected_value);
945 storage_flags = SN_EXISTS_RESET;
946 rd->last_collected_value = rd->collected_value;
949 rd->calculated_value += (calculated_number)(rd->collected_value - rd->last_collected_value);
952 debug(D_RRD_STATS, "%s/%s: CALC INC "
953 CALCULATED_NUMBER_FORMAT " += "
954 COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT
956 , rd->calculated_value
957 , rd->collected_value, rd->last_collected_value
961 case RRDDIM_ABSOLUTE:
962 rd->calculated_value = (calculated_number)rd->collected_value;
965 debug(D_RRD_STATS, "%s/%s: CALC ABS/ABS-NO-IN "
966 CALCULATED_NUMBER_FORMAT " = "
967 COLLECTED_NUMBER_FORMAT
969 , rd->calculated_value
970 , rd->collected_value
975 // make the default zero, to make sure
976 // it gets noticed when we add new types
977 rd->calculated_value = 0;
980 debug(D_RRD_STATS, "%s/%s: CALC "
981 CALCULATED_NUMBER_FORMAT " = 0"
983 , rd->calculated_value
989 // at this point we have all the calculated values ready
990 // it is now time to interpolate values on a second boundary
992 unsigned long long first_ut = last_ut;
993 int iterations = (now_ut - last_ut) / (st->update_every * 1000000ULL);
995 for( ; next_ut <= now_ut ; next_ut += st->update_every * 1000000ULL, iterations-- ) {
996 if(iterations < 0) error("iterations calculation wrapped!");
998 if(st->debug) debug(D_RRD_STATS, "%s: last ut = %0.3Lf (last updated time)", st->name, (long double)last_ut/1000000.0);
999 if(st->debug) debug(D_RRD_STATS, "%s: next ut = %0.3Lf (next interpolation point)", st->name, (long double)next_ut/1000000.0);
1001 st->last_updated.tv_sec = next_ut / 1000000ULL;
1002 st->last_updated.tv_usec = 0;
1004 for( rd = st->dimensions ; rd ; rd = rd->next ) {
1005 calculated_number new_value;
1007 switch(rd->algorithm) {
1008 case RRDDIM_INCREMENTAL:
1009 new_value = (calculated_number)
1010 ( rd->calculated_value
1011 * (calculated_number)(next_ut - last_ut)
1012 / (calculated_number)(now_ut - last_ut)
1016 debug(D_RRD_STATS, "%s/%s: CALC2 INC "
1017 CALCULATED_NUMBER_FORMAT " = "
1018 CALCULATED_NUMBER_FORMAT
1023 , rd->calculated_value
1024 , (unsigned long long)(next_ut - last_ut)
1025 , (unsigned long long)(now_ut - last_ut)
1028 rd->calculated_value -= new_value;
1031 case RRDDIM_ABSOLUTE:
1032 case RRDDIM_PCENT_OVER_ROW_TOTAL:
1033 case RRDDIM_PCENT_OVER_DIFF_TOTAL:
1035 new_value = (calculated_number)
1036 ( ( (rd->calculated_value - rd->last_calculated_value)
1037 * (calculated_number)(next_ut - first_ut)
1038 / (calculated_number)(now_ut - first_ut)
1040 + rd->last_calculated_value
1044 debug(D_RRD_STATS, "%s/%s: CALC2 DEF "
1045 CALCULATED_NUMBER_FORMAT " = ((("
1046 "(" CALCULATED_NUMBER_FORMAT " - " CALCULATED_NUMBER_FORMAT ")"
1048 " / %llu) + " CALCULATED_NUMBER_FORMAT
1051 , rd->calculated_value, rd->last_calculated_value
1052 , (next_ut - first_ut)
1053 , (now_ut - first_ut), rd->last_calculated_value
1056 if(next_ut + st->update_every * 1000000ULL > now_ut) rd->calculated_value = new_value;
1060 if(!store_this_entry) {
1061 store_this_entry = 1;
1065 if(rd->updated && iterations < st->gap_when_lost_iterations_above) {
1066 rd->values[st->current_entry] = pack_storage_number(
1068 * (calculated_number)rd->multiplier
1069 / (calculated_number)rd->divisor
1073 debug(D_RRD_STATS, "%s/%s: STORE[%ld] "
1074 CALCULATED_NUMBER_FORMAT " = " CALCULATED_NUMBER_FORMAT
1079 , unpack_storage_number(rd->values[st->current_entry]), new_value
1085 if(st->debug) debug(D_RRD_STATS, "%s/%s: STORE[%ld] = NON EXISTING "
1089 rd->values[st->current_entry] = pack_storage_number(0, SN_NOT_EXISTS);
1093 calculated_number t1 = new_value * (calculated_number)rd->multiplier / (calculated_number)rd->divisor;
1094 calculated_number t2 = unpack_storage_number(rd->values[st->current_entry]);
1095 calculated_number accuracy = accuracy_loss(t1, t2);
1096 debug(D_RRD_STATS, "%s/%s: UNPACK[%ld] = " CALCULATED_NUMBER_FORMAT " FLAGS=0x%08x (original = " CALCULATED_NUMBER_FORMAT ", accuracy loss = " CALCULATED_NUMBER_FORMAT "%%%s)"
1100 , get_storage_number_flags(rd->values[st->current_entry])
1103 , (accuracy > ACCURACY_LOSS) ? " **TOO BIG** " : ""
1106 rd->collected_volume += t1;
1107 rd->stored_volume += t2;
1108 accuracy = accuracy_loss(rd->collected_volume, rd->stored_volume);
1109 debug(D_RRD_STATS, "%s/%s: VOLUME[%ld] = " CALCULATED_NUMBER_FORMAT ", calculated = " CALCULATED_NUMBER_FORMAT ", accuracy loss = " CALCULATED_NUMBER_FORMAT "%%%s"
1113 , rd->collected_volume
1115 , (accuracy > ACCURACY_LOSS) ? " **TOO BIG** " : ""
1120 // reset the storage flags for the next point, if any;
1121 storage_flags = SN_EXISTS;
1123 if(st->first_entry_t && st->counter >= (unsigned long long)st->entries) {
1124 // the db is overwriting values
1125 // add the value we will overwrite
1126 st->first_entry_t += st->update_every * 1000000ULL;
1130 st->current_entry = ((st->current_entry + 1) >= st->entries) ? 0 : st->current_entry + 1;
1131 if(!st->first_entry_t) st->first_entry_t = next_ut;
1135 for( rd = st->dimensions; rd ; rd = rd->next ) {
1136 if(!rd->updated) continue;
1137 rd->last_collected_value = rd->collected_value;
1138 rd->last_calculated_value = rd->calculated_value;
1139 rd->collected_value = 0;
1142 // if this is the first entry of incremental dimensions
1143 // we have to set the first calculated_value to zero
1144 // to eliminate the first spike
1145 if(st->counter_done == 1) switch(rd->algorithm) {
1146 case RRDDIM_PCENT_OVER_DIFF_TOTAL:
1147 case RRDDIM_INCREMENTAL:
1148 rd->calculated_value = 0;
1149 // the next time, a new incremental total will be calculated
1153 st->last_collected_total = st->collected_total;
1155 // ALL DONE ABOUT THE DATA UPDATE
1156 // --------------------------------------------------------------------
1158 // find if there are any obsolete dimensions (not updated recently)
1159 for( rd = st->dimensions; rd ; rd = rd->next )
1160 if((rd->last_collected_time.tv_sec + (10 * st->update_every)) < st->last_collected_time.tv_sec)
1164 // there is dimension to free
1165 // upgrade our read lock to a write lock
1166 pthread_rwlock_unlock(&st->rwlock);
1167 pthread_rwlock_wrlock(&st->rwlock);
1169 for( rd = st->dimensions, last = NULL ; rd ; ) {
1170 if((rd->last_collected_time.tv_sec + (10 * st->update_every)) < st->last_collected_time.tv_sec) { // remove it only it is not updated in 10 seconds
1171 debug(D_RRD_STATS, "Removing obsolete dimension '%s' (%s) of '%s' (%s).", rd->name, rd->id, st->name, st->id);
1174 st->dimensions = rd->next;
1176 rrddim_free(st, rd);
1177 rd = st->dimensions;
1181 last->next = rd->next;
1183 rrddim_free(st, rd);
1193 if(!st->dimensions) st->enabled = 0;
1196 pthread_rwlock_unlock(&st->rwlock);
1198 if(pthread_setcancelstate(oldstate, NULL) != 0)
1199 error("Cannot set pthread cancel state to RESTORE (%d).", oldstate);
1201 return(st->usec_since_last_update);
1205 // find the oldest entry in the data, skipping all empty slots
1206 time_t rrdset_first_entry_t(RRDSET *st)
1208 if(!st->first_entry_t) return st->last_updated.tv_sec;
1210 return st->first_entry_t / 1000000;