11 #include <sys/types.h>
20 // ----------------------------------------------------------------------------
23 int rrd_update_every = UPDATE_EVERY;
24 int rrd_default_history_entries = RRD_DEFAULT_HISTORY_ENTRIES;
26 RRDSET *rrdset_root = NULL;
27 pthread_rwlock_t rrdset_root_rwlock = PTHREAD_RWLOCK_INITIALIZER;
29 int rrd_memory_mode = RRD_MEMORY_MODE_SAVE;
32 // ----------------------------------------------------------------------------
35 static int rrdset_iterator(avl *a) { if(a) {}; return 0; }
37 static int rrdset_compare(void* a, void* b) {
38 if(((RRDSET *)a)->hash < ((RRDSET *)b)->hash) return -1;
39 else if(((RRDSET *)a)->hash > ((RRDSET *)b)->hash) return 1;
40 else return strcmp(((RRDSET *)a)->id, ((RRDSET *)b)->id);
43 avl_tree rrdset_root_index = {
48 #define rrdset_index_add(st) avl_insert(&rrdset_root_index, (avl *)(st))
49 #define rrdset_index_del(st) avl_remove(&rrdset_root_index, (avl *)(st))
51 static RRDSET *rrdset_index_find(const char *id, unsigned long hash) {
52 RRDSET *result = NULL, tmp;
53 strncpy(tmp.id, id, RRD_ID_LENGTH_MAX);
54 tmp.id[RRD_ID_LENGTH_MAX] = '\0';
55 tmp.hash = (hash)?hash:simple_hash(tmp.id);
57 avl_search(&(rrdset_root_index), (avl *)&tmp, rrdset_iterator, (avl **)&result);
61 // ----------------------------------------------------------------------------
64 #define rrdset_from_avlname(avlname_ptr) ((RRDSET *)((avlname_ptr) - offsetof(RRDSET, avlname)))
66 static int rrdset_iterator_name(avl *a) { if(a) {}; return 0; }
68 static int rrdset_compare_name(void* a, void* b) {
69 RRDSET *A = rrdset_from_avlname(a);
70 RRDSET *B = rrdset_from_avlname(b);
72 // fprintf(stderr, "COMPARING: %s with %s\n", A->name, B->name);
74 if(A->hash_name < B->hash_name) return -1;
75 else if(A->hash_name > B->hash_name) return 1;
76 else return strcmp(A->name, B->name);
79 avl_tree rrdset_root_index_name = {
84 int rrdset_index_add_name(RRDSET *st) {
85 // fprintf(stderr, "ADDING: %s (name: %s)\n", st->id, st->name);
86 return avl_insert(&rrdset_root_index_name, (avl *)(&st->avlname));
89 #define rrdset_index_del_name(st) avl_remove(&rrdset_root_index_name, (avl *)(&st->avlname))
91 static RRDSET *rrdset_index_find_name(const char *name, unsigned long hash) {
95 tmp.hash_name = (hash)?hash:simple_hash(tmp.name);
97 // fprintf(stderr, "SEARCHING: %s\n", name);
98 avl_search(&(rrdset_root_index_name), (avl *)(&(tmp.avlname)), rrdset_iterator_name, (avl **)&result);
100 RRDSET *st = rrdset_from_avlname(result);
101 if(strcmp(st->magic, RRDSET_MAGIC))
102 error("Search for RRDSET %s returned an invalid RRDSET %s (name %s)", name, st->id, st->name);
104 // fprintf(stderr, "FOUND: %s\n", name);
105 return rrdset_from_avlname(result);
107 // fprintf(stderr, "NOT FOUND: %s\n", name);
112 // ----------------------------------------------------------------------------
115 static int rrddim_iterator(avl *a) { if(a) {}; return 0; }
117 static int rrddim_compare(void* a, void* b) {
118 if(((RRDDIM *)a)->hash < ((RRDDIM *)b)->hash) return -1;
119 else if(((RRDDIM *)a)->hash > ((RRDDIM *)b)->hash) return 1;
120 else return strcmp(((RRDDIM *)a)->id, ((RRDDIM *)b)->id);
123 #define rrddim_index_add(st, rd) avl_insert(&((st)->dimensions_index), (avl *)(rd))
124 #define rrddim_index_del(st,rd ) avl_remove(&((st)->dimensions_index), (avl *)(rd))
126 static RRDDIM *rrddim_index_find(RRDSET *st, const char *id, unsigned long hash) {
127 RRDDIM *result = NULL, tmp;
128 tmp.hash = (hash)?hash:simple_hash(id);
129 strncpy(tmp.id, id, RRD_ID_LENGTH_MAX);
130 tmp.id[RRD_ID_LENGTH_MAX] = '\0';
131 tmp.hash = (hash)?hash:simple_hash(tmp.id);
133 avl_search(&(st->dimensions_index), (avl *)&tmp, rrddim_iterator, (avl **)&result);
137 // ----------------------------------------------------------------------------
140 int rrdset_type_id(const char *name)
142 if(strcmp(name, RRDSET_TYPE_AREA_NAME) == 0) return RRDSET_TYPE_AREA;
143 if(strcmp(name, RRDSET_TYPE_STACKED_NAME) == 0) return RRDSET_TYPE_STACKED;
144 if(strcmp(name, RRDSET_TYPE_LINE_NAME) == 0) return RRDSET_TYPE_LINE;
145 return RRDSET_TYPE_LINE;
148 const char *rrdset_type_name(int chart_type)
150 static char line[] = RRDSET_TYPE_LINE_NAME;
151 static char area[] = RRDSET_TYPE_AREA_NAME;
152 static char stacked[] = RRDSET_TYPE_STACKED_NAME;
155 case RRDSET_TYPE_LINE:
158 case RRDSET_TYPE_AREA:
161 case RRDSET_TYPE_STACKED:
167 // ----------------------------------------------------------------------------
170 const char *rrd_memory_mode_name(int id)
172 static const char ram[] = RRD_MEMORY_MODE_RAM_NAME;
173 static const char map[] = RRD_MEMORY_MODE_MAP_NAME;
174 static const char save[] = RRD_MEMORY_MODE_SAVE_NAME;
177 case RRD_MEMORY_MODE_RAM:
180 case RRD_MEMORY_MODE_MAP:
183 case RRD_MEMORY_MODE_SAVE:
191 int rrd_memory_mode_id(const char *name)
193 if(!strcmp(name, RRD_MEMORY_MODE_RAM_NAME))
194 return RRD_MEMORY_MODE_RAM;
195 else if(!strcmp(name, RRD_MEMORY_MODE_MAP_NAME))
196 return RRD_MEMORY_MODE_MAP;
198 return RRD_MEMORY_MODE_SAVE;
201 // ----------------------------------------------------------------------------
204 int rrddim_algorithm_id(const char *name)
206 if(strcmp(name, RRDDIM_ABSOLUTE_NAME) == 0) return RRDDIM_ABSOLUTE;
207 if(strcmp(name, RRDDIM_INCREMENTAL_NAME) == 0) return RRDDIM_INCREMENTAL;
208 if(strcmp(name, RRDDIM_PCENT_OVER_ROW_TOTAL_NAME) == 0) return RRDDIM_PCENT_OVER_ROW_TOTAL;
209 if(strcmp(name, RRDDIM_PCENT_OVER_DIFF_TOTAL_NAME) == 0) return RRDDIM_PCENT_OVER_DIFF_TOTAL;
210 return RRDDIM_ABSOLUTE;
213 const char *rrddim_algorithm_name(int chart_type)
215 static char absolute[] = RRDDIM_ABSOLUTE_NAME;
216 static char incremental[] = RRDDIM_INCREMENTAL_NAME;
217 static char percentage_of_absolute_row[] = RRDDIM_PCENT_OVER_ROW_TOTAL_NAME;
218 static char percentage_of_incremental_row[] = RRDDIM_PCENT_OVER_DIFF_TOTAL_NAME;
221 case RRDDIM_ABSOLUTE:
224 case RRDDIM_INCREMENTAL:
227 case RRDDIM_PCENT_OVER_ROW_TOTAL:
228 return percentage_of_absolute_row;
230 case RRDDIM_PCENT_OVER_DIFF_TOTAL:
231 return percentage_of_incremental_row;
236 // ----------------------------------------------------------------------------
239 char *rrdset_strncpy_name(char *to, const char *from, int length)
242 for(i = 0; i < length && from[i] ;i++) {
243 if(from[i] == '.' || isalpha(from[i]) || isdigit(from[i])) to[i] = from[i];
246 if(i < length) to[i] = '\0';
247 to[length - 1] = '\0';
252 void rrdset_set_name(RRDSET *st, const char *name)
254 debug(D_RRD_CALLS, "rrdset_set_name() old: %s, new: %s", st->name, name);
256 if(st->name) rrdset_index_del_name(st);
258 char b[CONFIG_MAX_VALUE + 1];
259 char n[RRD_ID_LENGTH_MAX + 1];
261 snprintf(n, RRD_ID_LENGTH_MAX, "%s.%s", st->type, name);
262 rrdset_strncpy_name(b, n, CONFIG_MAX_VALUE);
263 st->name = config_get(st->id, "name", b);
264 st->hash_name = simple_hash(st->name);
266 rrdset_index_add_name(st);
269 // ----------------------------------------------------------------------------
272 char *rrdset_cache_dir(const char *id)
276 static char *cache_dir = NULL;
277 if(!cache_dir) cache_dir = config_get("global", "database directory", "cache");
279 char b[FILENAME_MAX + 1];
280 char n[FILENAME_MAX + 1];
281 rrdset_strncpy_name(b, id, FILENAME_MAX);
283 snprintf(n, FILENAME_MAX, "%s/%s", cache_dir, b);
284 ret = config_get(id, "database directory", n);
286 if(rrd_memory_mode == RRD_MEMORY_MODE_MAP || rrd_memory_mode == RRD_MEMORY_MODE_SAVE) {
287 int r = mkdir(ret, 0775);
288 if(r != 0 && errno != EEXIST)
289 error("Cannot create directory '%s'", ret);
295 // ----------------------------------------------------------------------------
298 void rrdset_reset(RRDSET *st)
300 debug(D_RRD_CALLS, "rrdset_reset() %s", st->name);
302 st->last_collected_time.tv_sec = 0;
303 st->last_collected_time.tv_usec = 0;
304 st->last_updated.tv_sec = 0;
305 st->last_updated.tv_usec = 0;
306 st->current_entry = 0;
308 st->counter_done = 0;
311 for(rd = st->dimensions; rd ; rd = rd->next) {
312 rd->last_collected_time.tv_sec = 0;
313 rd->last_collected_time.tv_usec = 0;
314 bzero(rd->values, rd->entries * sizeof(storage_number));
318 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)
321 fatal("Cannot create rrd stats without an id.");
325 char fullid[RRD_ID_LENGTH_MAX + 1];
326 char fullfilename[FILENAME_MAX + 1];
329 snprintf(fullid, RRD_ID_LENGTH_MAX, "%s.%s", type, id);
331 long entries = config_get_number(fullid, "history", rrd_default_history_entries);
332 if(entries < 5) entries = config_set_number(fullid, "history", 5);
333 if(entries > RRD_HISTORY_ENTRIES_MAX) entries = config_set_number(fullid, "history", RRD_HISTORY_ENTRIES_MAX);
335 int enabled = config_get_boolean(fullid, "enabled", 1);
336 if(!enabled) entries = 5;
338 unsigned long size = sizeof(RRDSET);
339 char *cache_dir = rrdset_cache_dir(fullid);
341 debug(D_RRD_CALLS, "Creating RRD_STATS for '%s.%s'.", type, id);
343 snprintf(fullfilename, FILENAME_MAX, "%s/main.db", cache_dir);
344 if(rrd_memory_mode != RRD_MEMORY_MODE_RAM) st = (RRDSET *)mymmap(fullfilename, size, ((rrd_memory_mode == RRD_MEMORY_MODE_MAP)?MAP_SHARED:MAP_PRIVATE));
346 if(strcmp(st->magic, RRDSET_MAGIC) != 0) {
348 info("Initializing file %s.", fullfilename);
351 else if(strcmp(st->id, fullid) != 0) {
353 error("File %s contents are not for chart %s. Clearing it.", fullfilename, fullid);
358 else if(st->memsize != size || st->entries != entries) {
360 error("File %s does not have the desired size. Clearing it.", fullfilename);
363 else if(st->update_every != update_every) {
365 error("File %s does not have the desired update frequency. Clearing it.", fullfilename);
368 else if((time(NULL) - st->last_updated.tv_sec) > update_every * entries) {
370 error("File %s is too old. Clearing it.", fullfilename);
381 st->dimensions = NULL;
383 st->mapped = rrd_memory_mode;
386 st = calloc(1, size);
388 fatal("Cannot allocate memory for RRD_STATS %s.%s", type, id);
391 st->mapped = RRD_MEMORY_MODE_RAM;
394 st->entries = entries;
395 st->update_every = update_every;
397 strcpy(st->cache_filename, fullfilename);
398 strcpy(st->magic, RRDSET_MAGIC);
400 strcpy(st->id, fullid);
401 st->hash = simple_hash(st->id);
403 st->cache_dir = cache_dir;
405 st->family = config_get(st->id, "family", family?family:st->id);
406 st->units = config_get(st->id, "units", units?units:"");
407 st->type = config_get(st->id, "type", type);
408 st->chart_type = rrdset_type_id(config_get(st->id, "chart type", rrdset_type_name(chart_type)));
410 st->priority = config_get_number(st->id, "priority", priority);
411 st->enabled = enabled;
416 st->last_collected_time.tv_sec = 0;
417 st->last_collected_time.tv_usec = 0;
418 st->counter_done = 0;
420 st->gap_when_lost_iterations_above = config_get_number(st->id, "gap when lost iterations above", RRD_DEFAULT_GAP_INTERPOLATIONS);
422 st->dimensions_index.root = NULL;
423 st->dimensions_index.compar = rrddim_compare;
425 pthread_rwlock_init(&st->rwlock, NULL);
426 pthread_rwlock_wrlock(&rrdset_root_rwlock);
428 if(name && *name) rrdset_set_name(st, name);
429 else rrdset_set_name(st, id);
432 char varvalue[CONFIG_MAX_VALUE + 1];
433 snprintf(varvalue, CONFIG_MAX_VALUE, "%s (%s)", title?title:"", st->name);
434 st->title = config_get(st->id, "title", varvalue);
437 st->next = rrdset_root;
440 rrdset_index_add(st);
442 pthread_rwlock_unlock(&rrdset_root_rwlock);
447 RRDDIM *rrddim_add(RRDSET *st, const char *id, const char *name, long multiplier, long divisor, int algorithm)
449 char filename[FILENAME_MAX + 1];
450 char fullfilename[FILENAME_MAX + 1];
452 char varname[CONFIG_MAX_NAME + 1];
454 unsigned long size = sizeof(RRDDIM) + (st->entries * sizeof(storage_number));
456 debug(D_RRD_CALLS, "Adding dimension '%s/%s'.", st->id, id);
458 rrdset_strncpy_name(filename, id, FILENAME_MAX);
459 snprintf(fullfilename, FILENAME_MAX, "%s/%s.db", st->cache_dir, filename);
460 if(rrd_memory_mode != RRD_MEMORY_MODE_RAM) rd = (RRDDIM *)mymmap(fullfilename, size, ((rrd_memory_mode == RRD_MEMORY_MODE_MAP)?MAP_SHARED:MAP_PRIVATE));
463 gettimeofday(&now, NULL);
465 if(strcmp(rd->magic, RRDDIMENSION_MAGIC) != 0) {
467 info("Initializing file %s.", fullfilename);
470 else if(rd->memsize != size) {
472 error("File %s does not have the desired size. Clearing it.", fullfilename);
475 else if(rd->multiplier != multiplier) {
477 error("File %s does not have the same multiplier. Clearing it.", fullfilename);
480 else if(rd->divisor != divisor) {
482 error("File %s does not have the same divisor. Clearing it.", fullfilename);
485 else if(rd->algorithm != algorithm) {
487 error("File %s does not have the same algorithm. Clearing it.", fullfilename);
490 else if(rd->update_every != st->update_every) {
492 error("File %s does not have the same refresh frequency. Clearing it.", fullfilename);
495 else if(usecdiff(&now, &rd->last_collected_time) > (rd->entries * rd->update_every * 1000000ULL)) {
497 error("File %s is too old. Clearing it.", fullfilename);
500 else if(strcmp(rd->id, id) != 0) {
502 error("File %s contents are not for dimension %s. Clearing it.", fullfilename, id);
510 // we have a file mapped for rd
511 rd->mapped = rrd_memory_mode;
517 // if we didn't manage to get a mmap'd dimension, just create one
519 rd = calloc(1, size);
521 fatal("Cannot allocate RRD_DIMENSION %s/%s.", st->id, id);
525 rd->mapped = RRD_MEMORY_MODE_RAM;
529 strcpy(rd->magic, RRDDIMENSION_MAGIC);
530 strcpy(rd->cache_filename, fullfilename);
531 strncpy(rd->id, id, RRD_ID_LENGTH_MAX);
532 rd->hash = simple_hash(rd->id);
534 snprintf(varname, CONFIG_MAX_NAME, "dim %s name", rd->id);
535 rd->name = config_get(st->id, varname, (name && *name)?name:rd->id);
537 snprintf(varname, CONFIG_MAX_NAME, "dim %s algorithm", rd->id);
538 rd->algorithm = rrddim_algorithm_id(config_get(st->id, varname, rrddim_algorithm_name(algorithm)));
540 snprintf(varname, CONFIG_MAX_NAME, "dim %s multiplier", rd->id);
541 rd->multiplier = config_get_number(st->id, varname, multiplier);
543 snprintf(varname, CONFIG_MAX_NAME, "dim %s divisor", rd->id);
544 rd->divisor = config_get_number(st->id, varname, divisor);
545 if(!rd->divisor) rd->divisor = 1;
547 rd->entries = st->entries;
548 rd->update_every = st->update_every;
550 // append this dimension
554 RRDDIM *td = st->dimensions;
555 for(; td->next; td = td->next) ;
559 rrddim_index_add(st, rd);
564 void rrddim_set_name(RRDSET *st, RRDDIM *rd, const char *name)
566 debug(D_RRD_CALLS, "rrddim_set_name() %s.%s", st->name, rd->name);
568 char varname[CONFIG_MAX_NAME + 1];
569 snprintf(varname, CONFIG_MAX_NAME, "dim %s name", rd->id);
570 config_get(st->id, varname, name);
573 void rrddim_free(RRDSET *st, RRDDIM *rd)
575 debug(D_RRD_CALLS, "rrddim_free() %s.%s", st->name, rd->name);
577 RRDDIM *i = st->dimensions, *last = NULL;
578 for(i = st->dimensions; i && i != rd ; i = i->next) last = i;
581 error("Request to free dimension %s.%s but it is not linked.", st->id, rd->name);
585 if(last) last = i->next;
586 else st->dimensions = i->next;
588 rrddim_index_del(st, rd);
590 // free(rd->annotations);
591 if(rd->mapped == RRD_MEMORY_MODE_SAVE) {
592 debug(D_RRD_CALLS, "Saving dimension '%s' to '%s'.", rd->name, rd->cache_filename);
593 savememory(rd->cache_filename, rd, rd->memsize);
595 debug(D_RRD_CALLS, "Unmapping dimension '%s'.", rd->name);
596 munmap(rd, rd->memsize);
598 else if(rd->mapped == RRD_MEMORY_MODE_MAP) {
599 debug(D_RRD_CALLS, "Unmapping dimension '%s'.", rd->name);
600 munmap(rd, rd->memsize);
603 debug(D_RRD_CALLS, "Removing dimension '%s'.", rd->name);
608 void rrdset_free_all(void)
610 info("Freeing all memory...");
613 for(st = rrdset_root; st ;) {
614 RRDSET *next = st->next;
616 while(st->dimensions)
617 rrddim_free(st, st->dimensions);
619 rrdset_index_del(st);
621 if(st->mapped == RRD_MEMORY_MODE_SAVE) {
622 debug(D_RRD_CALLS, "Saving stats '%s' to '%s'.", st->name, st->cache_filename);
623 savememory(st->cache_filename, st, st->memsize);
625 debug(D_RRD_CALLS, "Unmapping stats '%s'.", st->name);
626 munmap(st, st->memsize);
628 else if(st->mapped == RRD_MEMORY_MODE_MAP) {
629 debug(D_RRD_CALLS, "Unmapping stats '%s'.", st->name);
630 munmap(st, st->memsize);
639 info("Memory cleanup completed...");
642 void rrdset_save_all(void)
644 debug(D_RRD_CALLS, "rrdset_save_all()");
649 pthread_rwlock_wrlock(&rrdset_root_rwlock);
650 for(st = rrdset_root; st ; st = st->next) {
651 pthread_rwlock_wrlock(&st->rwlock);
653 if(st->mapped == RRD_MEMORY_MODE_SAVE) {
654 debug(D_RRD_CALLS, "Saving stats '%s' to '%s'.", st->name, st->cache_filename);
655 savememory(st->cache_filename, st, st->memsize);
658 for(rd = st->dimensions; rd ; rd = rd->next) {
659 if(rd->mapped == RRD_MEMORY_MODE_SAVE) {
660 debug(D_RRD_CALLS, "Saving dimension '%s' to '%s'.", rd->name, rd->cache_filename);
661 savememory(rd->cache_filename, rd, rd->memsize);
665 pthread_rwlock_unlock(&st->rwlock);
667 pthread_rwlock_unlock(&rrdset_root_rwlock);
671 RRDSET *rrdset_find(const char *id)
673 debug(D_RRD_CALLS, "rrdset_find() for chart %s", id);
675 pthread_rwlock_rdlock(&rrdset_root_rwlock);
676 RRDSET *st = rrdset_index_find(id, 0);
677 pthread_rwlock_unlock(&rrdset_root_rwlock);
682 RRDSET *rrdset_find_bytype(const char *type, const char *id)
684 debug(D_RRD_CALLS, "rrdset_find_bytype() for chart %s.%s", type, id);
686 char buf[RRD_ID_LENGTH_MAX + 1];
688 strncpy(buf, type, RRD_ID_LENGTH_MAX - 1);
689 buf[RRD_ID_LENGTH_MAX - 1] = '\0';
691 int len = strlen(buf);
692 strncpy(&buf[len], id, RRD_ID_LENGTH_MAX - len);
693 buf[RRD_ID_LENGTH_MAX] = '\0';
695 return(rrdset_find(buf));
698 RRDSET *rrdset_find_byname(const char *name)
700 debug(D_RRD_CALLS, "rrdset_find_byname() for chart %s", name);
702 pthread_rwlock_rdlock(&rrdset_root_rwlock);
703 RRDSET *st = rrdset_index_find_name(name, 0);
704 pthread_rwlock_unlock(&rrdset_root_rwlock);
709 RRDDIM *rrddim_find(RRDSET *st, const char *id)
711 debug(D_RRD_CALLS, "rrddim_find() for chart %s, dimension %s", st->name, id);
713 return rrddim_index_find(st, id, 0);
716 int rrddim_hide(RRDSET *st, const char *id)
718 debug(D_RRD_CALLS, "rrddim_hide() for chart %s, dimension %s", st->name, id);
720 RRDDIM *rd = rrddim_find(st, id);
722 error("Cannot find dimension with id '%s' on stats '%s' (%s).", id, st->name, st->id);
730 void rrddim_set_by_pointer(RRDSET *st, RRDDIM *rd, collected_number value)
732 debug(D_RRD_CALLS, "rrddim_set_by_pointer() for chart %s, dimension %s, value " COLLECTED_NUMBER_FORMAT, st->name, rd->name, value);
734 gettimeofday(&rd->last_collected_time, NULL);
735 rd->collected_value = value;
739 int rrddim_set(RRDSET *st, const char *id, collected_number value)
741 RRDDIM *rd = rrddim_find(st, id);
743 error("Cannot find dimension with id '%s' on stats '%s' (%s).", id, st->name, st->id);
747 rrddim_set_by_pointer(st, rd, value);
751 void rrdset_next_usec(RRDSET *st, unsigned long long microseconds)
753 debug(D_RRD_CALLS, "rrdset_next_usec() for chart %s with microseconds %llu", st->name, microseconds);
755 if(st->debug) debug(D_RRD_STATS, "%s: NEXT: %llu microseconds", st->name, microseconds);
756 st->usec_since_last_update = microseconds;
759 void rrdset_next(RRDSET *st)
761 unsigned long long microseconds = 0;
763 if(st->last_collected_time.tv_sec) {
765 gettimeofday(&now, NULL);
766 microseconds = usecdiff(&now, &st->last_collected_time);
769 rrdset_next_usec(st, microseconds);
772 void rrdset_next_plugins(RRDSET *st)
777 unsigned long long rrdset_done(RRDSET *st)
779 debug(D_RRD_CALLS, "rrdset_done() for chart %s", st->name);
782 int oldstate, store_this_entry = 1;
784 if(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate) != 0)
785 error("Cannot set pthread cancel state to DISABLE.");
787 // a read lock is OK here
788 pthread_rwlock_rdlock(&st->rwlock);
790 // check if the chart has a long time to be refreshed
791 if(st->usec_since_last_update > st->entries * st->update_every * 1000000ULL) {
792 info("%s: took too long to be updated (%0.3Lf secs). Reseting it.", st->name, (long double)(st->usec_since_last_update / 1000000.0));
794 st->usec_since_last_update = st->update_every * 1000000ULL;
796 if(st->debug) debug(D_RRD_STATS, "%s: microseconds since last update: %llu", st->name, st->usec_since_last_update);
798 // set last_collected_time
799 if(!st->last_collected_time.tv_sec) {
800 // it is the first entry
801 // set the last_collected_time to now
802 gettimeofday(&st->last_collected_time, NULL);
804 // the first entry should not be stored
805 store_this_entry = 0;
807 if(st->debug) debug(D_RRD_STATS, "%s: initializing last_collected to now. Will not store the next entry.", st->name);
810 // it is not the first entry
811 // calculate the proper last_collected_time, using usec_since_last_update
812 unsigned long long ut = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec + st->usec_since_last_update;
813 st->last_collected_time.tv_sec = ut / 1000000ULL;
814 st->last_collected_time.tv_usec = ut % 1000000ULL;
817 // if this set has not been updated in the past
818 // we fake the last_update time to be = now - usec_since_last_update
819 if(!st->last_updated.tv_sec) {
820 // it has never been updated before
821 // set a fake last_updated, in the past using usec_since_last_update
822 unsigned long long ut = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec - st->usec_since_last_update;
823 st->last_updated.tv_sec = ut / 1000000ULL;
824 st->last_updated.tv_usec = ut % 1000000ULL;
826 // the first entry should not be stored
827 store_this_entry = 0;
829 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);
832 // check if we will re-write the entire data set
833 if(usecdiff(&st->last_collected_time, &st->last_updated) > st->update_every * st->entries * 1000000ULL) {
834 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);
837 st->usec_since_last_update = st->update_every * 1000000ULL;
839 gettimeofday(&st->last_collected_time, NULL);
841 unsigned long long ut = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec - st->usec_since_last_update;
842 st->last_updated.tv_sec = ut / 1000000ULL;
843 st->last_updated.tv_usec = ut % 1000000ULL;
845 // the first entry should not be stored
846 store_this_entry = 0;
849 // these are the 3 variables that will help us in interpolation
850 // last_ut = the last time we added a value to the storage
851 // now_ut = the time the current value is taken at
852 // next_ut = the time of the next interpolation point
853 unsigned long long last_ut = st->last_updated.tv_sec * 1000000ULL + st->last_updated.tv_usec;
854 unsigned long long now_ut = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec;
855 unsigned long long next_ut = (st->last_updated.tv_sec + st->update_every) * 1000000ULL;
857 if(st->debug) debug(D_RRD_STATS, "%s: last ut = %0.3Lf (last updated time)", st->name, (long double)last_ut/1000000.0);
858 if(st->debug) debug(D_RRD_STATS, "%s: now ut = %0.3Lf (current update time)", st->name, (long double)now_ut/1000000.0);
859 if(st->debug) debug(D_RRD_STATS, "%s: next ut = %0.3Lf (next interpolation point)", st->name, (long double)next_ut/1000000.0);
861 if(!st->counter_done) {
862 store_this_entry = 0;
863 if(st->debug) debug(D_RRD_STATS, "%s: Will not store the next entry.", st->name);
867 // calculate totals and count the dimensions
869 st->collected_total = 0;
870 for( rd = st->dimensions, dimensions = 0 ; rd ; rd = rd->next, dimensions++ )
871 st->collected_total += rd->collected_value;
873 uint32_t storage_flags = SN_EXISTS;
875 // process all dimensions to calculate their values
876 // based on the collected figures only
877 // at this stage we do not interpolate anything
878 for( rd = st->dimensions ; rd ; rd = rd->next ) {
880 if(st->debug) debug(D_RRD_STATS, "%s/%s: "
881 " last_collected_value = " COLLECTED_NUMBER_FORMAT
882 " collected_value = " COLLECTED_NUMBER_FORMAT
883 " last_calculated_value = " CALCULATED_NUMBER_FORMAT
884 " calculated_value = " CALCULATED_NUMBER_FORMAT
886 , rd->last_collected_value
887 , rd->collected_value
888 , rd->last_calculated_value
889 , rd->calculated_value
892 switch(rd->algorithm) {
893 case RRDDIM_PCENT_OVER_DIFF_TOTAL:
894 // the percentage of the current increment
895 // over the increment of all dimensions together
896 if(st->collected_total == st->last_collected_total) rd->calculated_value = rd->last_calculated_value;
897 else rd->calculated_value =
898 (calculated_number)100
899 * (calculated_number)(rd->collected_value - rd->last_collected_value)
900 / (calculated_number)(st->collected_total - st->last_collected_total);
903 debug(D_RRD_STATS, "%s/%s: CALC PCENT-DIFF "
904 CALCULATED_NUMBER_FORMAT " = 100"
905 " * (" COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT ")"
906 " / (" COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT ")"
908 , rd->calculated_value
909 , rd->collected_value, rd->last_collected_value
910 , st->collected_total, st->last_collected_total
914 case RRDDIM_PCENT_OVER_ROW_TOTAL:
915 if(!st->collected_total) rd->calculated_value = 0;
917 // the percentage of the current value
918 // over the total of all dimensions
919 rd->calculated_value =
920 (calculated_number)100
921 * (calculated_number)rd->collected_value
922 / (calculated_number)st->collected_total;
925 debug(D_RRD_STATS, "%s/%s: CALC PCENT-ROW "
926 CALCULATED_NUMBER_FORMAT " = 100"
927 " * " COLLECTED_NUMBER_FORMAT
928 " / " COLLECTED_NUMBER_FORMAT
930 , rd->calculated_value
931 , rd->collected_value
932 , st->collected_total
936 case RRDDIM_INCREMENTAL:
937 // if the new is smaller than the old (an overflow, or reset), set the old equal to the new
938 // to reset the calculation (it will give zero as the calculation for this second)
939 if(rd->last_collected_value > rd->collected_value) {
940 info("%s.%s: Detect RESET or OVERFLOW. Last collected value = " COLLECTED_NUMBER_FORMAT ", current = " COLLECTED_NUMBER_FORMAT
942 , rd->last_collected_value
943 , rd->collected_value);
944 storage_flags = SN_EXISTS_RESET;
945 rd->last_collected_value = rd->collected_value;
948 rd->calculated_value += (calculated_number)(rd->collected_value - rd->last_collected_value);
951 debug(D_RRD_STATS, "%s/%s: CALC INC "
952 CALCULATED_NUMBER_FORMAT " += "
953 COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT
955 , rd->calculated_value
956 , rd->collected_value, rd->last_collected_value
960 case RRDDIM_ABSOLUTE:
961 rd->calculated_value = (calculated_number)rd->collected_value;
964 debug(D_RRD_STATS, "%s/%s: CALC ABS/ABS-NO-IN "
965 CALCULATED_NUMBER_FORMAT " = "
966 COLLECTED_NUMBER_FORMAT
968 , rd->calculated_value
969 , rd->collected_value
974 // make the default zero, to make sure
975 // it gets noticed when we add new types
976 rd->calculated_value = 0;
979 debug(D_RRD_STATS, "%s/%s: CALC "
980 CALCULATED_NUMBER_FORMAT " = 0"
982 , rd->calculated_value
988 // at this point we have all the calculated values ready
989 // it is now time to interpolate values on a second boundary
991 unsigned long long first_ut = last_ut;
992 int iterations = (now_ut - last_ut) / (st->update_every * 1000000ULL);
994 for( ; next_ut <= now_ut ; next_ut += st->update_every * 1000000ULL, iterations-- ) {
995 if(iterations < 0) error("iterations calculation wrapped!");
997 if(st->debug) debug(D_RRD_STATS, "%s: last ut = %0.3Lf (last updated time)", st->name, (long double)last_ut/1000000.0);
998 if(st->debug) debug(D_RRD_STATS, "%s: next ut = %0.3Lf (next interpolation point)", st->name, (long double)next_ut/1000000.0);
1000 st->last_updated.tv_sec = next_ut / 1000000ULL;
1001 st->last_updated.tv_usec = 0;
1003 for( rd = st->dimensions ; rd ; rd = rd->next ) {
1004 calculated_number new_value;
1006 switch(rd->algorithm) {
1007 case RRDDIM_INCREMENTAL:
1008 new_value = (calculated_number)
1009 ( rd->calculated_value
1010 * (calculated_number)(next_ut - last_ut)
1011 / (calculated_number)(now_ut - last_ut)
1015 debug(D_RRD_STATS, "%s/%s: CALC2 INC "
1016 CALCULATED_NUMBER_FORMAT " = "
1017 CALCULATED_NUMBER_FORMAT
1022 , rd->calculated_value
1023 , (unsigned long long)(next_ut - last_ut)
1024 , (unsigned long long)(now_ut - last_ut)
1027 rd->calculated_value -= new_value;
1030 case RRDDIM_ABSOLUTE:
1031 case RRDDIM_PCENT_OVER_ROW_TOTAL:
1032 case RRDDIM_PCENT_OVER_DIFF_TOTAL:
1034 new_value = (calculated_number)
1035 ( ( (rd->calculated_value - rd->last_calculated_value)
1036 * (calculated_number)(next_ut - first_ut)
1037 / (calculated_number)(now_ut - first_ut)
1039 + rd->last_calculated_value
1043 debug(D_RRD_STATS, "%s/%s: CALC2 DEF "
1044 CALCULATED_NUMBER_FORMAT " = ((("
1045 "(" CALCULATED_NUMBER_FORMAT " - " CALCULATED_NUMBER_FORMAT ")"
1047 " / %llu) + " CALCULATED_NUMBER_FORMAT
1050 , rd->calculated_value, rd->last_calculated_value
1051 , (next_ut - first_ut)
1052 , (now_ut - first_ut), rd->last_calculated_value
1055 if(next_ut + st->update_every * 1000000ULL > now_ut) rd->calculated_value = new_value;
1059 if(!store_this_entry) {
1060 store_this_entry = 1;
1064 if(rd->updated && iterations < st->gap_when_lost_iterations_above) {
1065 rd->values[st->current_entry] = pack_storage_number(
1067 * (calculated_number)rd->multiplier
1068 / (calculated_number)rd->divisor
1072 debug(D_RRD_STATS, "%s/%s: STORE[%ld] "
1073 CALCULATED_NUMBER_FORMAT " = " CALCULATED_NUMBER_FORMAT
1078 , unpack_storage_number(rd->values[st->current_entry]), new_value
1084 if(st->debug) debug(D_RRD_STATS, "%s/%s: STORE[%ld] = NON EXISTING "
1088 rd->values[st->current_entry] = pack_storage_number(0, SN_NOT_EXISTS);
1092 calculated_number t1 = new_value * (calculated_number)rd->multiplier / (calculated_number)rd->divisor;
1093 calculated_number t2 = unpack_storage_number(rd->values[st->current_entry]);
1094 calculated_number accuracy = accuracy_loss(t1, t2);
1095 debug(D_RRD_STATS, "%s/%s: UNPACK[%ld] = " CALCULATED_NUMBER_FORMAT " FLAGS=0x%08x (original = " CALCULATED_NUMBER_FORMAT ", accuracy loss = " CALCULATED_NUMBER_FORMAT "%%%s)"
1099 , get_storage_number_flags(rd->values[st->current_entry])
1102 , (accuracy > ACCURACY_LOSS) ? " **TOO BIG** " : ""
1105 rd->collected_volume += t1;
1106 rd->stored_volume += t2;
1107 accuracy = accuracy_loss(rd->collected_volume, rd->stored_volume);
1108 debug(D_RRD_STATS, "%s/%s: VOLUME[%ld] = " CALCULATED_NUMBER_FORMAT ", calculated = " CALCULATED_NUMBER_FORMAT ", accuracy loss = " CALCULATED_NUMBER_FORMAT "%%%s"
1112 , rd->collected_volume
1114 , (accuracy > ACCURACY_LOSS) ? " **TOO BIG** " : ""
1119 // reset the storage flags for the next point, if any;
1120 storage_flags = SN_EXISTS;
1122 if(st->first_entry_t && st->counter >= (unsigned long long)st->entries) {
1123 // the db is overwriting values
1124 // add the value we will overwrite
1125 st->first_entry_t += st->update_every * 1000000ULL;
1129 st->current_entry = ((st->current_entry + 1) >= st->entries) ? 0 : st->current_entry + 1;
1130 if(!st->first_entry_t) st->first_entry_t = next_ut;
1134 for( rd = st->dimensions; rd ; rd = rd->next ) {
1135 if(!rd->updated) continue;
1136 rd->last_collected_value = rd->collected_value;
1137 rd->last_calculated_value = rd->calculated_value;
1138 rd->collected_value = 0;
1141 // if this is the first entry of incremental dimensions
1142 // we have to set the first calculated_value to zero
1143 // to eliminate the first spike
1144 if(st->counter_done == 1) switch(rd->algorithm) {
1145 case RRDDIM_PCENT_OVER_DIFF_TOTAL:
1146 case RRDDIM_INCREMENTAL:
1147 rd->calculated_value = 0;
1148 // the next time, a new incremental total will be calculated
1152 st->last_collected_total = st->collected_total;
1154 // ALL DONE ABOUT THE DATA UPDATE
1155 // --------------------------------------------------------------------
1157 // find if there are any obsolete dimensions (not updated recently)
1158 for( rd = st->dimensions; rd ; rd = rd->next )
1159 if((rd->last_collected_time.tv_sec + (10 * st->update_every)) < st->last_collected_time.tv_sec)
1163 // there is dimension to free
1164 // upgrade our read lock to a write lock
1165 pthread_rwlock_unlock(&st->rwlock);
1166 pthread_rwlock_wrlock(&st->rwlock);
1168 for( rd = st->dimensions, last = NULL ; rd ; ) {
1169 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
1170 debug(D_RRD_STATS, "Removing obsolete dimension '%s' (%s) of '%s' (%s).", rd->name, rd->id, st->name, st->id);
1173 st->dimensions = rd->next;
1175 rrddim_free(st, rd);
1176 rd = st->dimensions;
1180 last->next = rd->next;
1182 rrddim_free(st, rd);
1192 if(!st->dimensions) st->enabled = 0;
1195 pthread_rwlock_unlock(&st->rwlock);
1197 if(pthread_setcancelstate(oldstate, NULL) != 0)
1198 error("Cannot set pthread cancel state to RESTORE (%d).", oldstate);
1200 return(st->usec_since_last_update);
1204 // find the oldest entry in the data, skipping all empty slots
1205 time_t rrdset_first_entry_t(RRDSET *st)
1207 if(!st->first_entry_t) return st->last_updated.tv_sec;
1209 return st->first_entry_t / 1000000;