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 tmp.hash = (hash)?hash:simple_hash(id);
131 strncpy(tmp.id, id, RRD_ID_LENGTH_MAX);
132 tmp.id[RRD_ID_LENGTH_MAX] = '\0';
133 tmp.hash = (hash)?hash:simple_hash(tmp.id);
135 avl_search(&(st->dimensions_index), (avl *)&tmp, rrddim_iterator, (avl **)&result);
139 // ----------------------------------------------------------------------------
142 int rrdset_type_id(const char *name)
144 if(strcmp(name, RRDSET_TYPE_AREA_NAME) == 0) return RRDSET_TYPE_AREA;
145 if(strcmp(name, RRDSET_TYPE_STACKED_NAME) == 0) return RRDSET_TYPE_STACKED;
146 if(strcmp(name, RRDSET_TYPE_LINE_NAME) == 0) return RRDSET_TYPE_LINE;
147 return RRDSET_TYPE_LINE;
150 const char *rrdset_type_name(int chart_type)
152 static char line[] = RRDSET_TYPE_LINE_NAME;
153 static char area[] = RRDSET_TYPE_AREA_NAME;
154 static char stacked[] = RRDSET_TYPE_STACKED_NAME;
157 case RRDSET_TYPE_LINE:
160 case RRDSET_TYPE_AREA:
163 case RRDSET_TYPE_STACKED:
169 // ----------------------------------------------------------------------------
172 const char *rrd_memory_mode_name(int id)
174 static const char ram[] = RRD_MEMORY_MODE_RAM_NAME;
175 static const char map[] = RRD_MEMORY_MODE_MAP_NAME;
176 static const char save[] = RRD_MEMORY_MODE_SAVE_NAME;
179 case RRD_MEMORY_MODE_RAM:
182 case RRD_MEMORY_MODE_MAP:
185 case RRD_MEMORY_MODE_SAVE:
193 int rrd_memory_mode_id(const char *name)
195 if(!strcmp(name, RRD_MEMORY_MODE_RAM_NAME))
196 return RRD_MEMORY_MODE_RAM;
197 else if(!strcmp(name, RRD_MEMORY_MODE_MAP_NAME))
198 return RRD_MEMORY_MODE_MAP;
200 return RRD_MEMORY_MODE_SAVE;
203 // ----------------------------------------------------------------------------
206 int rrddim_algorithm_id(const char *name)
208 if(strcmp(name, RRDDIM_ABSOLUTE_NAME) == 0) return RRDDIM_ABSOLUTE;
209 if(strcmp(name, RRDDIM_INCREMENTAL_NAME) == 0) return RRDDIM_INCREMENTAL;
210 if(strcmp(name, RRDDIM_PCENT_OVER_ROW_TOTAL_NAME) == 0) return RRDDIM_PCENT_OVER_ROW_TOTAL;
211 if(strcmp(name, RRDDIM_PCENT_OVER_DIFF_TOTAL_NAME) == 0) return RRDDIM_PCENT_OVER_DIFF_TOTAL;
212 return RRDDIM_ABSOLUTE;
215 const char *rrddim_algorithm_name(int chart_type)
217 static char absolute[] = RRDDIM_ABSOLUTE_NAME;
218 static char incremental[] = RRDDIM_INCREMENTAL_NAME;
219 static char percentage_of_absolute_row[] = RRDDIM_PCENT_OVER_ROW_TOTAL_NAME;
220 static char percentage_of_incremental_row[] = RRDDIM_PCENT_OVER_DIFF_TOTAL_NAME;
223 case RRDDIM_ABSOLUTE:
226 case RRDDIM_INCREMENTAL:
229 case RRDDIM_PCENT_OVER_ROW_TOTAL:
230 return percentage_of_absolute_row;
232 case RRDDIM_PCENT_OVER_DIFF_TOTAL:
233 return percentage_of_incremental_row;
238 // ----------------------------------------------------------------------------
241 char *rrdset_strncpy_name(char *to, const char *from, int length)
244 for(i = 0; i < length && from[i] ;i++) {
245 if(from[i] == '.' || isalpha(from[i]) || isdigit(from[i])) to[i] = from[i];
248 if(i < length) to[i] = '\0';
249 to[length - 1] = '\0';
254 void rrdset_set_name(RRDSET *st, const char *name)
256 debug(D_RRD_CALLS, "rrdset_set_name() old: %s, new: %s", st->name, name);
258 if(st->name) rrdset_index_del_name(st);
260 char b[CONFIG_MAX_VALUE + 1];
261 char n[RRD_ID_LENGTH_MAX + 1];
263 snprintf(n, RRD_ID_LENGTH_MAX, "%s.%s", st->type, name);
264 rrdset_strncpy_name(b, n, CONFIG_MAX_VALUE);
265 st->name = config_get(st->id, "name", b);
266 st->hash_name = simple_hash(st->name);
268 rrdset_index_add_name(st);
271 // ----------------------------------------------------------------------------
274 char *rrdset_cache_dir(const char *id)
278 static char *cache_dir = NULL;
279 if(!cache_dir) cache_dir = config_get("global", "database directory", "cache");
281 char b[FILENAME_MAX + 1];
282 char n[FILENAME_MAX + 1];
283 rrdset_strncpy_name(b, id, FILENAME_MAX);
285 snprintf(n, FILENAME_MAX, "%s/%s", cache_dir, b);
286 ret = config_get(id, "database directory", n);
288 if(rrd_memory_mode == RRD_MEMORY_MODE_MAP || rrd_memory_mode == RRD_MEMORY_MODE_SAVE) {
289 int r = mkdir(ret, 0775);
290 if(r != 0 && errno != EEXIST)
291 error("Cannot create directory '%s'", ret);
297 // ----------------------------------------------------------------------------
300 void rrdset_reset(RRDSET *st)
302 debug(D_RRD_CALLS, "rrdset_reset() %s", st->name);
304 st->last_collected_time.tv_sec = 0;
305 st->last_collected_time.tv_usec = 0;
306 st->last_updated.tv_sec = 0;
307 st->last_updated.tv_usec = 0;
308 st->current_entry = 0;
310 st->counter_done = 0;
313 for(rd = st->dimensions; rd ; rd = rd->next) {
314 rd->last_collected_time.tv_sec = 0;
315 rd->last_collected_time.tv_usec = 0;
316 bzero(rd->values, rd->entries * sizeof(storage_number));
320 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)
323 fatal("Cannot create rrd stats without an id.");
327 char fullid[RRD_ID_LENGTH_MAX + 1];
328 char fullfilename[FILENAME_MAX + 1];
331 snprintf(fullid, RRD_ID_LENGTH_MAX, "%s.%s", type, id);
333 long entries = config_get_number(fullid, "history", rrd_default_history_entries);
334 if(entries < 5) entries = config_set_number(fullid, "history", 5);
335 if(entries > RRD_HISTORY_ENTRIES_MAX) entries = config_set_number(fullid, "history", RRD_HISTORY_ENTRIES_MAX);
337 int enabled = config_get_boolean(fullid, "enabled", 1);
338 if(!enabled) entries = 5;
340 unsigned long size = sizeof(RRDSET);
341 char *cache_dir = rrdset_cache_dir(fullid);
343 debug(D_RRD_CALLS, "Creating RRD_STATS for '%s.%s'.", type, id);
345 snprintf(fullfilename, FILENAME_MAX, "%s/main.db", cache_dir);
346 if(rrd_memory_mode != RRD_MEMORY_MODE_RAM) st = (RRDSET *)mymmap(fullfilename, size, ((rrd_memory_mode == RRD_MEMORY_MODE_MAP)?MAP_SHARED:MAP_PRIVATE));
348 if(strcmp(st->magic, RRDSET_MAGIC) != 0) {
350 info("Initializing file %s.", fullfilename);
353 else if(strcmp(st->id, fullid) != 0) {
355 error("File %s contents are not for chart %s. Clearing it.", fullfilename, fullid);
360 else if(st->memsize != size || st->entries != entries) {
362 error("File %s does not have the desired size. Clearing it.", fullfilename);
365 else if(st->update_every != update_every) {
367 error("File %s does not have the desired update frequency. Clearing it.", fullfilename);
370 else if((time(NULL) - st->last_updated.tv_sec) > update_every * entries) {
372 error("File %s is too old. Clearing it.", fullfilename);
383 st->dimensions = NULL;
385 st->mapped = rrd_memory_mode;
388 st = calloc(1, size);
390 fatal("Cannot allocate memory for RRD_STATS %s.%s", type, id);
393 st->mapped = RRD_MEMORY_MODE_RAM;
396 st->entries = entries;
397 st->update_every = update_every;
399 strcpy(st->cache_filename, fullfilename);
400 strcpy(st->magic, RRDSET_MAGIC);
402 strcpy(st->id, fullid);
403 st->hash = simple_hash(st->id);
405 st->cache_dir = cache_dir;
407 st->family = config_get(st->id, "family", family?family:st->id);
408 st->units = config_get(st->id, "units", units?units:"");
409 st->type = config_get(st->id, "type", type);
410 st->chart_type = rrdset_type_id(config_get(st->id, "chart type", rrdset_type_name(chart_type)));
412 st->priority = config_get_number(st->id, "priority", priority);
413 st->enabled = enabled;
418 st->last_collected_time.tv_sec = 0;
419 st->last_collected_time.tv_usec = 0;
420 st->counter_done = 0;
422 st->gap_when_lost_iterations_above = config_get_number(st->id, "gap when lost iterations above", RRD_DEFAULT_GAP_INTERPOLATIONS);
424 st->dimensions_index.root = NULL;
425 st->dimensions_index.compar = rrddim_compare;
427 pthread_rwlock_init(&st->rwlock, NULL);
428 pthread_rwlock_wrlock(&rrdset_root_rwlock);
430 if(name && *name) rrdset_set_name(st, name);
431 else rrdset_set_name(st, id);
434 char varvalue[CONFIG_MAX_VALUE + 1];
435 snprintf(varvalue, CONFIG_MAX_VALUE, "%s (%s)", title?title:"", st->name);
436 st->title = config_get(st->id, "title", varvalue);
439 st->next = rrdset_root;
442 rrdset_index_add(st);
444 pthread_rwlock_unlock(&rrdset_root_rwlock);
449 RRDDIM *rrddim_add(RRDSET *st, const char *id, const char *name, long multiplier, long divisor, int algorithm)
451 char filename[FILENAME_MAX + 1];
452 char fullfilename[FILENAME_MAX + 1];
454 char varname[CONFIG_MAX_NAME + 1];
456 unsigned long size = sizeof(RRDDIM) + (st->entries * sizeof(storage_number));
458 debug(D_RRD_CALLS, "Adding dimension '%s/%s'.", st->id, id);
460 rrdset_strncpy_name(filename, id, FILENAME_MAX);
461 snprintf(fullfilename, FILENAME_MAX, "%s/%s.db", st->cache_dir, filename);
462 if(rrd_memory_mode != RRD_MEMORY_MODE_RAM) rd = (RRDDIM *)mymmap(fullfilename, size, ((rrd_memory_mode == RRD_MEMORY_MODE_MAP)?MAP_SHARED:MAP_PRIVATE));
465 gettimeofday(&now, NULL);
467 if(strcmp(rd->magic, RRDDIMENSION_MAGIC) != 0) {
469 info("Initializing file %s.", fullfilename);
472 else if(rd->memsize != size) {
474 error("File %s does not have the desired size. Clearing it.", fullfilename);
477 else if(rd->multiplier != multiplier) {
479 error("File %s does not have the same multiplier. Clearing it.", fullfilename);
482 else if(rd->divisor != divisor) {
484 error("File %s does not have the same divisor. Clearing it.", fullfilename);
487 else if(rd->algorithm != algorithm) {
489 error("File %s does not have the same algorithm. Clearing it.", fullfilename);
492 else if(rd->update_every != st->update_every) {
494 error("File %s does not have the same refresh frequency. Clearing it.", fullfilename);
497 else if(usecdiff(&now, &rd->last_collected_time) > (rd->entries * rd->update_every * 1000000ULL)) {
499 error("File %s is too old. Clearing it.", fullfilename);
502 else if(strcmp(rd->id, id) != 0) {
504 error("File %s contents are not for dimension %s. Clearing it.", fullfilename, id);
512 // we have a file mapped for rd
513 rd->mapped = rrd_memory_mode;
519 // if we didn't manage to get a mmap'd dimension, just create one
521 rd = calloc(1, size);
523 fatal("Cannot allocate RRD_DIMENSION %s/%s.", st->id, id);
527 rd->mapped = RRD_MEMORY_MODE_RAM;
531 strcpy(rd->magic, RRDDIMENSION_MAGIC);
532 strcpy(rd->cache_filename, fullfilename);
533 strncpy(rd->id, id, RRD_ID_LENGTH_MAX);
534 rd->hash = simple_hash(rd->id);
536 snprintf(varname, CONFIG_MAX_NAME, "dim %s name", rd->id);
537 rd->name = config_get(st->id, varname, (name && *name)?name:rd->id);
539 snprintf(varname, CONFIG_MAX_NAME, "dim %s algorithm", rd->id);
540 rd->algorithm = rrddim_algorithm_id(config_get(st->id, varname, rrddim_algorithm_name(algorithm)));
542 snprintf(varname, CONFIG_MAX_NAME, "dim %s multiplier", rd->id);
543 rd->multiplier = config_get_number(st->id, varname, multiplier);
545 snprintf(varname, CONFIG_MAX_NAME, "dim %s divisor", rd->id);
546 rd->divisor = config_get_number(st->id, varname, divisor);
547 if(!rd->divisor) rd->divisor = 1;
549 rd->entries = st->entries;
550 rd->update_every = st->update_every;
552 // append this dimension
556 RRDDIM *td = st->dimensions;
557 for(; td->next; td = td->next) ;
561 rrddim_index_add(st, rd);
566 void rrddim_set_name(RRDSET *st, RRDDIM *rd, const char *name)
568 debug(D_RRD_CALLS, "rrddim_set_name() %s.%s", st->name, rd->name);
570 char varname[CONFIG_MAX_NAME + 1];
571 snprintf(varname, CONFIG_MAX_NAME, "dim %s name", rd->id);
572 config_get(st->id, varname, name);
575 void rrddim_free(RRDSET *st, RRDDIM *rd)
577 debug(D_RRD_CALLS, "rrddim_free() %s.%s", st->name, rd->name);
579 RRDDIM *i = st->dimensions, *last = NULL;
580 for(i = st->dimensions; i && i != rd ; i = i->next) last = i;
583 error("Request to free dimension %s.%s but it is not linked.", st->id, rd->name);
587 if(last) last = i->next;
588 else st->dimensions = i->next;
590 rrddim_index_del(st, rd);
592 // free(rd->annotations);
593 if(rd->mapped == RRD_MEMORY_MODE_SAVE) {
594 debug(D_RRD_CALLS, "Saving dimension '%s' to '%s'.", rd->name, rd->cache_filename);
595 savememory(rd->cache_filename, rd, rd->memsize);
597 debug(D_RRD_CALLS, "Unmapping dimension '%s'.", rd->name);
598 munmap(rd, rd->memsize);
600 else if(rd->mapped == RRD_MEMORY_MODE_MAP) {
601 debug(D_RRD_CALLS, "Unmapping dimension '%s'.", rd->name);
602 munmap(rd, rd->memsize);
605 debug(D_RRD_CALLS, "Removing dimension '%s'.", rd->name);
610 void rrdset_free_all(void)
612 info("Freeing all memory...");
615 for(st = rrdset_root; st ;) {
616 RRDSET *next = st->next;
618 while(st->dimensions)
619 rrddim_free(st, st->dimensions);
621 rrdset_index_del(st);
623 if(st->mapped == RRD_MEMORY_MODE_SAVE) {
624 debug(D_RRD_CALLS, "Saving stats '%s' to '%s'.", st->name, st->cache_filename);
625 savememory(st->cache_filename, st, st->memsize);
627 debug(D_RRD_CALLS, "Unmapping stats '%s'.", st->name);
628 munmap(st, st->memsize);
630 else if(st->mapped == RRD_MEMORY_MODE_MAP) {
631 debug(D_RRD_CALLS, "Unmapping stats '%s'.", st->name);
632 munmap(st, st->memsize);
641 info("Memory cleanup completed...");
644 void rrdset_save_all(void)
646 debug(D_RRD_CALLS, "rrdset_save_all()");
651 pthread_rwlock_wrlock(&rrdset_root_rwlock);
652 for(st = rrdset_root; st ; st = st->next) {
653 pthread_rwlock_wrlock(&st->rwlock);
655 if(st->mapped == RRD_MEMORY_MODE_SAVE) {
656 debug(D_RRD_CALLS, "Saving stats '%s' to '%s'.", st->name, st->cache_filename);
657 savememory(st->cache_filename, st, st->memsize);
660 for(rd = st->dimensions; rd ; rd = rd->next) {
661 if(rd->mapped == RRD_MEMORY_MODE_SAVE) {
662 debug(D_RRD_CALLS, "Saving dimension '%s' to '%s'.", rd->name, rd->cache_filename);
663 savememory(rd->cache_filename, rd, rd->memsize);
667 pthread_rwlock_unlock(&st->rwlock);
669 pthread_rwlock_unlock(&rrdset_root_rwlock);
673 RRDSET *rrdset_find(const char *id)
675 debug(D_RRD_CALLS, "rrdset_find() for chart %s", id);
677 pthread_rwlock_rdlock(&rrdset_root_rwlock);
678 RRDSET *st = rrdset_index_find(id, 0);
679 pthread_rwlock_unlock(&rrdset_root_rwlock);
684 RRDSET *rrdset_find_bytype(const char *type, const char *id)
686 debug(D_RRD_CALLS, "rrdset_find_bytype() for chart %s.%s", type, id);
688 char buf[RRD_ID_LENGTH_MAX + 1];
690 strncpy(buf, type, RRD_ID_LENGTH_MAX - 1);
691 buf[RRD_ID_LENGTH_MAX - 1] = '\0';
693 int len = strlen(buf);
694 strncpy(&buf[len], id, RRD_ID_LENGTH_MAX - len);
695 buf[RRD_ID_LENGTH_MAX] = '\0';
697 return(rrdset_find(buf));
700 RRDSET *rrdset_find_byname(const char *name)
702 debug(D_RRD_CALLS, "rrdset_find_byname() for chart %s", name);
704 pthread_rwlock_rdlock(&rrdset_root_rwlock);
705 RRDSET *st = rrdset_index_find_name(name, 0);
706 pthread_rwlock_unlock(&rrdset_root_rwlock);
711 RRDDIM *rrddim_find(RRDSET *st, const char *id)
713 debug(D_RRD_CALLS, "rrddim_find() for chart %s, dimension %s", st->name, id);
715 return rrddim_index_find(st, id, 0);
718 int rrddim_hide(RRDSET *st, const char *id)
720 debug(D_RRD_CALLS, "rrddim_hide() for chart %s, dimension %s", st->name, id);
722 RRDDIM *rd = rrddim_find(st, id);
724 error("Cannot find dimension with id '%s' on stats '%s' (%s).", id, st->name, st->id);
732 void rrddim_set_by_pointer(RRDSET *st, RRDDIM *rd, collected_number value)
734 debug(D_RRD_CALLS, "rrddim_set_by_pointer() for chart %s, dimension %s, value " COLLECTED_NUMBER_FORMAT, st->name, rd->name, value);
736 gettimeofday(&rd->last_collected_time, NULL);
737 rd->collected_value = value;
741 int rrddim_set(RRDSET *st, const char *id, collected_number value)
743 RRDDIM *rd = rrddim_find(st, id);
745 error("Cannot find dimension with id '%s' on stats '%s' (%s).", id, st->name, st->id);
749 rrddim_set_by_pointer(st, rd, value);
753 void rrdset_next_usec(RRDSET *st, unsigned long long microseconds)
755 debug(D_RRD_CALLS, "rrdset_next_usec() for chart %s with microseconds %llu", st->name, microseconds);
757 if(st->debug) debug(D_RRD_STATS, "%s: NEXT: %llu microseconds", st->name, microseconds);
758 st->usec_since_last_update = microseconds;
761 void rrdset_next(RRDSET *st)
763 unsigned long long microseconds = 0;
765 if(st->last_collected_time.tv_sec) {
767 gettimeofday(&now, NULL);
768 microseconds = usecdiff(&now, &st->last_collected_time);
771 rrdset_next_usec(st, microseconds);
774 void rrdset_next_plugins(RRDSET *st)
779 unsigned long long rrdset_done(RRDSET *st)
781 debug(D_RRD_CALLS, "rrdset_done() for chart %s", st->name);
784 int oldstate, store_this_entry = 1;
786 if(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate) != 0)
787 error("Cannot set pthread cancel state to DISABLE.");
789 // a read lock is OK here
790 pthread_rwlock_rdlock(&st->rwlock);
792 // check if the chart has a long time to be refreshed
793 if(st->usec_since_last_update > st->entries * st->update_every * 1000000ULL) {
794 info("%s: took too long to be updated (%0.3Lf secs). Reseting it.", st->name, (long double)(st->usec_since_last_update / 1000000.0));
796 st->usec_since_last_update = st->update_every * 1000000ULL;
798 if(st->debug) debug(D_RRD_STATS, "%s: microseconds since last update: %llu", st->name, st->usec_since_last_update);
800 // set last_collected_time
801 if(!st->last_collected_time.tv_sec) {
802 // it is the first entry
803 // set the last_collected_time to now
804 gettimeofday(&st->last_collected_time, NULL);
806 // the first entry should not be stored
807 store_this_entry = 0;
809 if(st->debug) debug(D_RRD_STATS, "%s: initializing last_collected to now. Will not store the next entry.", st->name);
812 // it is not the first entry
813 // calculate the proper last_collected_time, using usec_since_last_update
814 unsigned long long ut = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec + st->usec_since_last_update;
815 st->last_collected_time.tv_sec = ut / 1000000ULL;
816 st->last_collected_time.tv_usec = ut % 1000000ULL;
819 // if this set has not been updated in the past
820 // we fake the last_update time to be = now - usec_since_last_update
821 if(!st->last_updated.tv_sec) {
822 // it has never been updated before
823 // set a fake last_updated, in the past using usec_since_last_update
824 unsigned long long ut = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec - st->usec_since_last_update;
825 st->last_updated.tv_sec = ut / 1000000ULL;
826 st->last_updated.tv_usec = ut % 1000000ULL;
828 // the first entry should not be stored
829 store_this_entry = 0;
831 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);
834 // check if we will re-write the entire data set
835 if(usecdiff(&st->last_collected_time, &st->last_updated) > st->update_every * st->entries * 1000000ULL) {
836 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);
839 st->usec_since_last_update = st->update_every * 1000000ULL;
841 gettimeofday(&st->last_collected_time, NULL);
843 unsigned long long ut = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec - st->usec_since_last_update;
844 st->last_updated.tv_sec = ut / 1000000ULL;
845 st->last_updated.tv_usec = ut % 1000000ULL;
847 // the first entry should not be stored
848 store_this_entry = 0;
851 // these are the 3 variables that will help us in interpolation
852 // last_ut = the last time we added a value to the storage
853 // now_ut = the time the current value is taken at
854 // next_ut = the time of the next interpolation point
855 unsigned long long last_ut = st->last_updated.tv_sec * 1000000ULL + st->last_updated.tv_usec;
856 unsigned long long now_ut = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec;
857 unsigned long long next_ut = (st->last_updated.tv_sec + st->update_every) * 1000000ULL;
859 if(st->debug) debug(D_RRD_STATS, "%s: last ut = %0.3Lf (last updated time)", st->name, (long double)last_ut/1000000.0);
860 if(st->debug) debug(D_RRD_STATS, "%s: now ut = %0.3Lf (current update time)", st->name, (long double)now_ut/1000000.0);
861 if(st->debug) debug(D_RRD_STATS, "%s: next ut = %0.3Lf (next interpolation point)", st->name, (long double)next_ut/1000000.0);
863 if(!st->counter_done) {
864 store_this_entry = 0;
865 if(st->debug) debug(D_RRD_STATS, "%s: Will not store the next entry.", st->name);
869 // calculate totals and count the dimensions
871 st->collected_total = 0;
872 for( rd = st->dimensions, dimensions = 0 ; rd ; rd = rd->next, dimensions++ )
873 st->collected_total += rd->collected_value;
875 uint32_t storage_flags = SN_EXISTS;
877 // process all dimensions to calculate their values
878 // based on the collected figures only
879 // at this stage we do not interpolate anything
880 for( rd = st->dimensions ; rd ; rd = rd->next ) {
882 if(st->debug) debug(D_RRD_STATS, "%s/%s: "
883 " last_collected_value = " COLLECTED_NUMBER_FORMAT
884 " collected_value = " COLLECTED_NUMBER_FORMAT
885 " last_calculated_value = " CALCULATED_NUMBER_FORMAT
886 " calculated_value = " CALCULATED_NUMBER_FORMAT
888 , rd->last_collected_value
889 , rd->collected_value
890 , rd->last_calculated_value
891 , rd->calculated_value
894 switch(rd->algorithm) {
895 case RRDDIM_PCENT_OVER_DIFF_TOTAL:
896 // the percentage of the current increment
897 // over the increment of all dimensions together
898 if(st->collected_total == st->last_collected_total) rd->calculated_value = rd->last_calculated_value;
899 else rd->calculated_value =
900 (calculated_number)100
901 * (calculated_number)(rd->collected_value - rd->last_collected_value)
902 / (calculated_number)(st->collected_total - st->last_collected_total);
905 debug(D_RRD_STATS, "%s/%s: CALC PCENT-DIFF "
906 CALCULATED_NUMBER_FORMAT " = 100"
907 " * (" COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT ")"
908 " / (" COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT ")"
910 , rd->calculated_value
911 , rd->collected_value, rd->last_collected_value
912 , st->collected_total, st->last_collected_total
916 case RRDDIM_PCENT_OVER_ROW_TOTAL:
917 if(!st->collected_total) rd->calculated_value = 0;
919 // the percentage of the current value
920 // over the total of all dimensions
921 rd->calculated_value =
922 (calculated_number)100
923 * (calculated_number)rd->collected_value
924 / (calculated_number)st->collected_total;
927 debug(D_RRD_STATS, "%s/%s: CALC PCENT-ROW "
928 CALCULATED_NUMBER_FORMAT " = 100"
929 " * " COLLECTED_NUMBER_FORMAT
930 " / " COLLECTED_NUMBER_FORMAT
932 , rd->calculated_value
933 , rd->collected_value
934 , st->collected_total
938 case RRDDIM_INCREMENTAL:
939 // if the new is smaller than the old (an overflow, or reset), set the old equal to the new
940 // to reset the calculation (it will give zero as the calculation for this second)
941 if(rd->last_collected_value > rd->collected_value) {
942 info("%s.%s: Detect RESET or OVERFLOW. Last collected value = " COLLECTED_NUMBER_FORMAT ", current = " COLLECTED_NUMBER_FORMAT
944 , rd->last_collected_value
945 , rd->collected_value);
946 storage_flags = SN_EXISTS_RESET;
947 rd->last_collected_value = rd->collected_value;
950 rd->calculated_value += (calculated_number)(rd->collected_value - rd->last_collected_value);
953 debug(D_RRD_STATS, "%s/%s: CALC INC "
954 CALCULATED_NUMBER_FORMAT " += "
955 COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT
957 , rd->calculated_value
958 , rd->collected_value, rd->last_collected_value
962 case RRDDIM_ABSOLUTE:
963 rd->calculated_value = (calculated_number)rd->collected_value;
966 debug(D_RRD_STATS, "%s/%s: CALC ABS/ABS-NO-IN "
967 CALCULATED_NUMBER_FORMAT " = "
968 COLLECTED_NUMBER_FORMAT
970 , rd->calculated_value
971 , rd->collected_value
976 // make the default zero, to make sure
977 // it gets noticed when we add new types
978 rd->calculated_value = 0;
981 debug(D_RRD_STATS, "%s/%s: CALC "
982 CALCULATED_NUMBER_FORMAT " = 0"
984 , rd->calculated_value
990 // at this point we have all the calculated values ready
991 // it is now time to interpolate values on a second boundary
993 unsigned long long first_ut = last_ut;
994 int iterations = (now_ut - last_ut) / (st->update_every * 1000000ULL);
996 for( ; next_ut <= now_ut ; next_ut += st->update_every * 1000000ULL, iterations-- ) {
997 if(iterations < 0) error("iterations calculation wrapped!");
999 if(st->debug) debug(D_RRD_STATS, "%s: last ut = %0.3Lf (last updated time)", st->name, (long double)last_ut/1000000.0);
1000 if(st->debug) debug(D_RRD_STATS, "%s: next ut = %0.3Lf (next interpolation point)", st->name, (long double)next_ut/1000000.0);
1002 st->last_updated.tv_sec = next_ut / 1000000ULL;
1003 st->last_updated.tv_usec = 0;
1005 for( rd = st->dimensions ; rd ; rd = rd->next ) {
1006 calculated_number new_value;
1008 switch(rd->algorithm) {
1009 case RRDDIM_INCREMENTAL:
1010 new_value = (calculated_number)
1011 ( rd->calculated_value
1012 * (calculated_number)(next_ut - last_ut)
1013 / (calculated_number)(now_ut - last_ut)
1017 debug(D_RRD_STATS, "%s/%s: CALC2 INC "
1018 CALCULATED_NUMBER_FORMAT " = "
1019 CALCULATED_NUMBER_FORMAT
1024 , rd->calculated_value
1025 , (unsigned long long)(next_ut - last_ut)
1026 , (unsigned long long)(now_ut - last_ut)
1029 rd->calculated_value -= new_value;
1032 case RRDDIM_ABSOLUTE:
1033 case RRDDIM_PCENT_OVER_ROW_TOTAL:
1034 case RRDDIM_PCENT_OVER_DIFF_TOTAL:
1036 new_value = (calculated_number)
1037 ( ( (rd->calculated_value - rd->last_calculated_value)
1038 * (calculated_number)(next_ut - first_ut)
1039 / (calculated_number)(now_ut - first_ut)
1041 + rd->last_calculated_value
1045 debug(D_RRD_STATS, "%s/%s: CALC2 DEF "
1046 CALCULATED_NUMBER_FORMAT " = ((("
1047 "(" CALCULATED_NUMBER_FORMAT " - " CALCULATED_NUMBER_FORMAT ")"
1049 " / %llu) + " CALCULATED_NUMBER_FORMAT
1052 , rd->calculated_value, rd->last_calculated_value
1053 , (next_ut - first_ut)
1054 , (now_ut - first_ut), rd->last_calculated_value
1057 if(next_ut + st->update_every * 1000000ULL > now_ut) rd->calculated_value = new_value;
1061 if(!store_this_entry) {
1062 store_this_entry = 1;
1066 if(rd->updated && iterations < st->gap_when_lost_iterations_above) {
1067 rd->values[st->current_entry] = pack_storage_number(
1069 * (calculated_number)rd->multiplier
1070 / (calculated_number)rd->divisor
1074 debug(D_RRD_STATS, "%s/%s: STORE[%ld] "
1075 CALCULATED_NUMBER_FORMAT " = " CALCULATED_NUMBER_FORMAT
1080 , unpack_storage_number(rd->values[st->current_entry]), new_value
1086 if(st->debug) debug(D_RRD_STATS, "%s/%s: STORE[%ld] = NON EXISTING "
1090 rd->values[st->current_entry] = pack_storage_number(0, SN_NOT_EXISTS);
1094 calculated_number t1 = new_value * (calculated_number)rd->multiplier / (calculated_number)rd->divisor;
1095 calculated_number t2 = unpack_storage_number(rd->values[st->current_entry]);
1096 calculated_number accuracy = accuracy_loss(t1, t2);
1097 debug(D_RRD_STATS, "%s/%s: UNPACK[%ld] = " CALCULATED_NUMBER_FORMAT " FLAGS=0x%08x (original = " CALCULATED_NUMBER_FORMAT ", accuracy loss = " CALCULATED_NUMBER_FORMAT "%%%s)"
1101 , get_storage_number_flags(rd->values[st->current_entry])
1104 , (accuracy > ACCURACY_LOSS) ? " **TOO BIG** " : ""
1107 rd->collected_volume += t1;
1108 rd->stored_volume += t2;
1109 accuracy = accuracy_loss(rd->collected_volume, rd->stored_volume);
1110 debug(D_RRD_STATS, "%s/%s: VOLUME[%ld] = " CALCULATED_NUMBER_FORMAT ", calculated = " CALCULATED_NUMBER_FORMAT ", accuracy loss = " CALCULATED_NUMBER_FORMAT "%%%s"
1114 , rd->collected_volume
1116 , (accuracy > ACCURACY_LOSS) ? " **TOO BIG** " : ""
1121 // reset the storage flags for the next point, if any;
1122 storage_flags = SN_EXISTS;
1124 if(st->first_entry_t && st->counter >= (unsigned long long)st->entries) {
1125 // the db is overwriting values
1126 // add the value we will overwrite
1127 st->first_entry_t += st->update_every * 1000000ULL;
1131 st->current_entry = ((st->current_entry + 1) >= st->entries) ? 0 : st->current_entry + 1;
1132 if(!st->first_entry_t) st->first_entry_t = next_ut;
1136 for( rd = st->dimensions; rd ; rd = rd->next ) {
1137 if(!rd->updated) continue;
1138 rd->last_collected_value = rd->collected_value;
1139 rd->last_calculated_value = rd->calculated_value;
1140 rd->collected_value = 0;
1143 // if this is the first entry of incremental dimensions
1144 // we have to set the first calculated_value to zero
1145 // to eliminate the first spike
1146 if(st->counter_done == 1) switch(rd->algorithm) {
1147 case RRDDIM_PCENT_OVER_DIFF_TOTAL:
1148 case RRDDIM_INCREMENTAL:
1149 rd->calculated_value = 0;
1150 // the next time, a new incremental total will be calculated
1154 st->last_collected_total = st->collected_total;
1156 // ALL DONE ABOUT THE DATA UPDATE
1157 // --------------------------------------------------------------------
1159 // find if there are any obsolete dimensions (not updated recently)
1160 for( rd = st->dimensions; rd ; rd = rd->next )
1161 if((rd->last_collected_time.tv_sec + (10 * st->update_every)) < st->last_collected_time.tv_sec)
1165 // there is dimension to free
1166 // upgrade our read lock to a write lock
1167 pthread_rwlock_unlock(&st->rwlock);
1168 pthread_rwlock_wrlock(&st->rwlock);
1170 for( rd = st->dimensions, last = NULL ; rd ; ) {
1171 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
1172 debug(D_RRD_STATS, "Removing obsolete dimension '%s' (%s) of '%s' (%s).", rd->name, rd->id, st->name, st->id);
1175 st->dimensions = rd->next;
1177 rrddim_free(st, rd);
1178 rd = st->dimensions;
1182 last->next = rd->next;
1184 rrddim_free(st, rd);
1194 if(!st->dimensions) st->enabled = 0;
1197 pthread_rwlock_unlock(&st->rwlock);
1199 if(pthread_setcancelstate(oldstate, NULL) != 0)
1200 error("Cannot set pthread cancel state to RESTORE (%d).", oldstate);
1202 return(st->usec_since_last_update);
1206 // find the oldest entry in the data, skipping all empty slots
1207 time_t rrdset_first_entry_t(RRDSET *st)
1209 if(!st->first_entry_t) return st->last_updated.tv_sec;
1211 return st->first_entry_t / 1000000;