10 #include <sys/types.h>
19 int update_every = UPDATE_EVERY;
20 int save_history = HISTORY;
22 // ----------------------------------------------------------------------------
25 int chart_type_id(const char *name)
27 if(strcmp(name, CHART_TYPE_AREA_NAME) == 0) return CHART_TYPE_AREA;
28 if(strcmp(name, CHART_TYPE_STACKED_NAME) == 0) return CHART_TYPE_STACKED;
29 if(strcmp(name, CHART_TYPE_LINE_NAME) == 0) return CHART_TYPE_LINE;
30 return CHART_TYPE_LINE;
33 const char *chart_type_name(int chart_type)
35 static char line[] = CHART_TYPE_LINE_NAME;
36 static char area[] = CHART_TYPE_AREA_NAME;
37 static char stacked[] = CHART_TYPE_STACKED_NAME;
46 case CHART_TYPE_STACKED:
52 // ----------------------------------------------------------------------------
55 int memory_mode = NETDATA_MEMORY_MODE_SAVE;
57 const char *memory_mode_name(int id)
59 static const char ram[] = NETDATA_MEMORY_MODE_RAM_NAME;
60 static const char map[] = NETDATA_MEMORY_MODE_MAP_NAME;
61 static const char save[] = NETDATA_MEMORY_MODE_SAVE_NAME;
64 case NETDATA_MEMORY_MODE_RAM:
67 case NETDATA_MEMORY_MODE_MAP:
70 case NETDATA_MEMORY_MODE_SAVE:
78 int memory_mode_id(const char *name)
80 if(!strcmp(name, NETDATA_MEMORY_MODE_RAM_NAME))
81 return NETDATA_MEMORY_MODE_RAM;
82 else if(!strcmp(name, NETDATA_MEMORY_MODE_MAP_NAME))
83 return NETDATA_MEMORY_MODE_MAP;
85 return NETDATA_MEMORY_MODE_SAVE;
88 // ----------------------------------------------------------------------------
91 int algorithm_id(const char *name)
93 if(strcmp(name, RRD_DIMENSION_ABSOLUTE_NAME) == 0) return RRD_DIMENSION_ABSOLUTE;
94 if(strcmp(name, RRD_DIMENSION_INCREMENTAL_NAME) == 0) return RRD_DIMENSION_INCREMENTAL;
95 if(strcmp(name, RRD_DIMENSION_PCENT_OVER_ROW_TOTAL_NAME) == 0) return RRD_DIMENSION_PCENT_OVER_ROW_TOTAL;
96 if(strcmp(name, RRD_DIMENSION_PCENT_OVER_DIFF_TOTAL_NAME) == 0) return RRD_DIMENSION_PCENT_OVER_DIFF_TOTAL;
97 return RRD_DIMENSION_ABSOLUTE;
100 const char *algorithm_name(int chart_type)
102 static char absolute[] = RRD_DIMENSION_ABSOLUTE_NAME;
103 static char incremental[] = RRD_DIMENSION_INCREMENTAL_NAME;
104 static char percentage_of_absolute_row[] = RRD_DIMENSION_PCENT_OVER_ROW_TOTAL_NAME;
105 static char percentage_of_incremental_row[] = RRD_DIMENSION_PCENT_OVER_DIFF_TOTAL_NAME;
108 case RRD_DIMENSION_ABSOLUTE:
111 case RRD_DIMENSION_INCREMENTAL:
114 case RRD_DIMENSION_PCENT_OVER_ROW_TOTAL:
115 return percentage_of_absolute_row;
117 case RRD_DIMENSION_PCENT_OVER_DIFF_TOTAL:
118 return percentage_of_incremental_row;
123 RRD_STATS *root = NULL;
124 pthread_rwlock_t root_rwlock = PTHREAD_RWLOCK_INITIALIZER;
126 char *rrd_stats_strncpy_name(char *to, const char *from, int length)
129 for(i = 0; i < length && from[i] ;i++) {
130 if(from[i] == '.' || isalpha(from[i]) || isdigit(from[i])) to[i] = from[i];
133 if(i < length) to[i] = '\0';
134 to[length - 1] = '\0';
139 void rrd_stats_set_name(RRD_STATS *st, const char *name)
141 char b[CONFIG_MAX_VALUE + 1];
142 char n[RRD_STATS_NAME_MAX + 1];
144 snprintf(n, RRD_STATS_NAME_MAX, "%s.%s", st->type, name);
145 rrd_stats_strncpy_name(b, n, CONFIG_MAX_VALUE);
146 st->name = config_get(st->id, "name", b);
147 st->hash_name = simple_hash(st->name);
150 char *rrd_stats_cache_dir(const char *id)
154 static char *cache_dir = NULL;
155 if(!cache_dir) cache_dir = config_get("global", "database directory", "cache");
157 char b[FILENAME_MAX + 1];
158 char n[FILENAME_MAX + 1];
159 rrd_stats_strncpy_name(b, id, FILENAME_MAX);
161 snprintf(n, FILENAME_MAX, "%s/%s", cache_dir, b);
162 ret = config_get(id, "database directory", n);
164 if(memory_mode == NETDATA_MEMORY_MODE_MAP || memory_mode == NETDATA_MEMORY_MODE_SAVE) {
165 int r = mkdir(ret, 0775);
166 if(r != 0 && errno != EEXIST)
167 error("Cannot create directory '%s'", ret);
173 void rrd_stats_reset(RRD_STATS *st)
175 st->last_collected_time.tv_sec = 0;
176 st->last_collected_time.tv_usec = 0;
177 st->last_updated.tv_sec = 0;
178 st->last_updated.tv_usec = 0;
179 st->current_entry = 0;
180 st->counter_done = 0;
183 for(rd = st->dimensions; rd ; rd = rd->next) {
184 rd->last_collected_time.tv_sec = 0;
185 rd->last_collected_time.tv_usec = 0;
186 rd->current_entry = 0;
190 RRD_STATS *rrd_stats_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)
193 fatal("Cannot create rrd stats without an id.");
197 char fullid[RRD_STATS_NAME_MAX + 1];
198 char fullfilename[FILENAME_MAX + 1];
199 RRD_STATS *st = NULL;
201 snprintf(fullid, RRD_STATS_NAME_MAX, "%s.%s", type, id);
203 long entries = config_get_number(fullid, "history", save_history);
204 if(entries < 5) entries = config_set_number(fullid, "history", 5);
205 if(entries > HISTORY_MAX) entries = config_set_number(fullid, "history", HISTORY_MAX);
207 int enabled = config_get_boolean(fullid, "enabled", 1);
208 if(!enabled) entries = 5;
210 unsigned long size = sizeof(RRD_STATS);
211 char *cache_dir = rrd_stats_cache_dir(fullid);
213 debug(D_RRD_CALLS, "Creating RRD_STATS for '%s.%s'.", type, id);
215 snprintf(fullfilename, FILENAME_MAX, "%s/main.db", cache_dir);
216 if(memory_mode != NETDATA_MEMORY_MODE_RAM) st = (RRD_STATS *)mymmap(fullfilename, size, ((memory_mode == NETDATA_MEMORY_MODE_MAP)?MAP_SHARED:MAP_PRIVATE));
218 if(strcmp(st->magic, RRD_STATS_MAGIC) != 0) {
220 error("File %s does not have our version. Clearing it.", fullfilename);
223 else if(strcmp(st->id, fullid) != 0) {
225 error("File %s does not have our id. Unmapping it.", fullfilename);
229 else if(st->memsize != size || st->entries != entries) {
231 error("File %s does not have the desired size. Clearing it.", fullfilename);
234 else if(st->update_every != update_every) {
236 error("File %s does not have the desired update frequency. Clearing it.", fullfilename);
239 else if((time(NULL) - st->last_updated.tv_sec) > update_every * entries) {
241 error("File %s is too old. Clearing it.", fullfilename);
252 st->dimensions = NULL;
254 st->mapped = memory_mode;
257 st = calloc(1, size);
259 fatal("Cannot allocate memory for RRD_STATS %s.%s", type, id);
262 st->mapped = NETDATA_MEMORY_MODE_RAM;
265 st->entries = entries;
266 st->update_every = update_every;
268 strcpy(st->cache_file, fullfilename);
269 strcpy(st->magic, RRD_STATS_MAGIC);
271 strcpy(st->id, fullid);
272 st->hash = simple_hash(st->id);
274 st->cache_dir = cache_dir;
276 st->family = config_get(st->id, "family", family?family:st->id);
277 st->units = config_get(st->id, "units", units?units:"");
278 st->type = config_get(st->id, "type", type);
279 st->chart_type = chart_type_id(config_get(st->id, "chart type", chart_type_name(chart_type)));
281 if(name && *name) rrd_stats_set_name(st, name);
282 else rrd_stats_set_name(st, id);
285 char varvalue[CONFIG_MAX_VALUE + 1];
286 snprintf(varvalue, CONFIG_MAX_VALUE, "%s (%s)", title?title:"", st->name);
287 st->title = config_get(st->id, "title", varvalue);
290 st->priority = config_get_number(st->id, "priority", priority);
291 st->enabled = enabled;
296 st->last_collected_time.tv_sec = 0;
297 st->last_collected_time.tv_usec = 0;
298 st->counter_done = 0;
300 pthread_rwlock_init(&st->rwlock, NULL);
301 pthread_rwlock_wrlock(&root_rwlock);
306 pthread_rwlock_unlock(&root_rwlock);
311 RRD_DIMENSION *rrd_stats_dimension_add(RRD_STATS *st, const char *id, const char *name, long multiplier, long divisor, int algorithm)
313 char filename[FILENAME_MAX + 1];
314 char fullfilename[FILENAME_MAX + 1];
316 char varname[CONFIG_MAX_NAME + 1];
317 RRD_DIMENSION *rd = NULL;
318 unsigned long size = sizeof(RRD_DIMENSION) + (st->entries * sizeof(storage_number));
320 debug(D_RRD_CALLS, "Adding dimension '%s/%s'.", st->id, id);
322 rrd_stats_strncpy_name(filename, id, FILENAME_MAX);
323 snprintf(fullfilename, FILENAME_MAX, "%s/%s.db", st->cache_dir, filename);
324 if(memory_mode != NETDATA_MEMORY_MODE_RAM) rd = (RRD_DIMENSION *)mymmap(fullfilename, size, ((memory_mode == NETDATA_MEMORY_MODE_MAP)?MAP_SHARED:MAP_PRIVATE));
327 gettimeofday(&now, NULL);
329 if(strcmp(rd->magic, RRD_DIMENSION_MAGIC) != 0) {
331 error("File %s does not have our version. Clearing it.", fullfilename);
334 else if(rd->memsize != size) {
336 error("File %s does not have the desired size. Clearing it.", fullfilename);
339 else if(rd->multiplier != multiplier) {
341 error("File %s does not have the same multiplier. Clearing it.", fullfilename);
344 else if(rd->divisor != divisor) {
346 error("File %s does not have the same divisor. Clearing it.", fullfilename);
349 else if(rd->algorithm != algorithm) {
351 error("File %s does not have the same algorithm. Clearing it.", fullfilename);
354 else if(rd->update_every != st->update_every) {
356 error("File %s does not have the same refresh frequency. Clearing it.", fullfilename);
359 else if(usecdiff(&now, &rd->last_collected_time) > (rd->entries * rd->update_every * 1000000ULL)) {
361 error("File %s is too old. Clearing it.", fullfilename);
364 else if(strcmp(rd->id, id) != 0) {
366 error("File %s does not have our dimension id. Unmapping it.", fullfilename);
373 // we have a file mapped for rd
374 rd->mapped = memory_mode;
380 // if we didn't manage to get a mmap'd dimension, just create one
382 rd = calloc(1, size);
384 fatal("Cannot allocate RRD_DIMENSION %s/%s.", st->id, id);
388 rd->mapped = NETDATA_MEMORY_MODE_RAM;
392 strcpy(rd->magic, RRD_DIMENSION_MAGIC);
393 strcpy(rd->cache_file, fullfilename);
394 strncpy(rd->id, id, RRD_STATS_NAME_MAX);
395 rd->hash = simple_hash(rd->id);
397 snprintf(varname, CONFIG_MAX_NAME, "dim %s name", rd->id);
398 rd->name = config_get(st->id, varname, (name && *name)?name:rd->id);
400 snprintf(varname, CONFIG_MAX_NAME, "dim %s algorithm", rd->id);
401 rd->algorithm = algorithm_id(config_get(st->id, varname, algorithm_name(algorithm)));
403 snprintf(varname, CONFIG_MAX_NAME, "dim %s multiplier", rd->id);
404 rd->multiplier = config_get_number(st->id, varname, multiplier);
406 snprintf(varname, CONFIG_MAX_NAME, "dim %s divisor", rd->id);
407 rd->divisor = config_get_number(st->id, varname, divisor);
408 if(!rd->divisor) rd->divisor = 1;
410 rd->entries = st->entries;
411 rd->update_every = st->update_every;
413 // append this dimension
417 RRD_DIMENSION *td = st->dimensions;
418 for(; td->next; td = td->next) ;
425 void rrd_stats_dimension_set_name(RRD_STATS *st, RRD_DIMENSION *rd, const char *name)
427 char varname[CONFIG_MAX_NAME + 1];
428 snprintf(varname, CONFIG_MAX_NAME, "dim %s name", rd->id);
429 config_get(st->id, varname, name);
432 void rrd_stats_dimension_free(RRD_DIMENSION *rd)
434 if(rd->next) rrd_stats_dimension_free(rd->next);
435 // free(rd->annotations);
436 if(rd->mapped == NETDATA_MEMORY_MODE_SAVE) {
437 debug(D_RRD_CALLS, "Saving dimension '%s' to '%s'.", rd->name, rd->cache_file);
438 savememory(rd->cache_file, rd, rd->memsize);
440 debug(D_RRD_CALLS, "Unmapping dimension '%s'.", rd->name);
441 munmap(rd, rd->memsize);
443 else if(rd->mapped == NETDATA_MEMORY_MODE_MAP) {
444 debug(D_RRD_CALLS, "Unmapping dimension '%s'.", rd->name);
445 munmap(rd, rd->memsize);
448 debug(D_RRD_CALLS, "Removing dimension '%s'.", rd->name);
453 void rrd_stats_free_all(void)
455 info("Freeing all memory...");
458 for(st = root; st ;) {
459 RRD_STATS *next = st->next;
461 if(st->dimensions) rrd_stats_dimension_free(st->dimensions);
462 st->dimensions = NULL;
464 if(st->mapped == NETDATA_MEMORY_MODE_SAVE) {
465 debug(D_RRD_CALLS, "Saving stats '%s' to '%s'.", st->name, st->cache_file);
466 savememory(st->cache_file, st, st->memsize);
468 debug(D_RRD_CALLS, "Unmapping stats '%s'.", st->name);
469 munmap(st, st->memsize);
471 else if(st->mapped == NETDATA_MEMORY_MODE_MAP) {
472 debug(D_RRD_CALLS, "Unmapping stats '%s'.", st->name);
473 munmap(st, st->memsize);
482 info("Memory cleanup completed...");
485 void rrd_stats_save_all(void)
490 pthread_rwlock_wrlock(&root_rwlock);
491 for(st = root; st ; st = st->next) {
492 pthread_rwlock_wrlock(&st->rwlock);
494 if(st->mapped == NETDATA_MEMORY_MODE_SAVE) {
495 debug(D_RRD_CALLS, "Saving stats '%s' to '%s'.", st->name, st->cache_file);
496 savememory(st->cache_file, st, st->memsize);
499 for(rd = st->dimensions; rd ; rd = rd->next) {
500 if(rd->mapped == NETDATA_MEMORY_MODE_SAVE) {
501 debug(D_RRD_CALLS, "Saving dimension '%s' to '%s'.", rd->name, rd->cache_file);
502 savememory(rd->cache_file, rd, rd->memsize);
506 pthread_rwlock_unlock(&st->rwlock);
508 pthread_rwlock_unlock(&root_rwlock);
512 RRD_STATS *rrd_stats_find(const char *id)
514 debug(D_RRD_CALLS, "rrd_stats_find() for chart %s", id);
516 unsigned long hash = simple_hash(id);
518 pthread_rwlock_rdlock(&root_rwlock);
519 RRD_STATS *st = root;
520 for ( ; st ; st = st->next )
522 if(strcmp(st->id, id) == 0)
524 pthread_rwlock_unlock(&root_rwlock);
529 RRD_STATS *rrd_stats_find_bytype(const char *type, const char *id)
531 debug(D_RRD_CALLS, "rrd_stats_find_bytype() for chart %s.%s", type, id);
533 char buf[RRD_STATS_NAME_MAX + 1];
535 strncpy(buf, type, RRD_STATS_NAME_MAX - 1);
536 buf[RRD_STATS_NAME_MAX - 1] = '\0';
538 int len = strlen(buf);
539 strncpy(&buf[len], id, RRD_STATS_NAME_MAX - len);
540 buf[RRD_STATS_NAME_MAX] = '\0';
542 return(rrd_stats_find(buf));
545 RRD_STATS *rrd_stats_find_byname(const char *name)
547 debug(D_RRD_CALLS, "rrd_stats_find_byname() for chart %s", name);
549 char b[CONFIG_MAX_VALUE + 1];
551 rrd_stats_strncpy_name(b, name, CONFIG_MAX_VALUE);
552 unsigned long hash = simple_hash(b);
554 pthread_rwlock_rdlock(&root_rwlock);
555 RRD_STATS *st = root;
556 for ( ; st ; st = st->next ) {
557 if(hash == st->hash_name && strcmp(st->name, b) == 0) break;
559 pthread_rwlock_unlock(&root_rwlock);
564 RRD_DIMENSION *rrd_stats_dimension_find(RRD_STATS *st, const char *id)
566 debug(D_RRD_CALLS, "rrd_stats_dimension_find() for chart %s, dimension %s", st->name, id);
568 unsigned long hash = simple_hash(id);
570 RRD_DIMENSION *rd = st->dimensions;
572 for ( ; rd ; rd = rd->next )
574 if(strcmp(rd->id, id) == 0)
580 int rrd_stats_dimension_hide(RRD_STATS *st, const char *id)
582 debug(D_RRD_CALLS, "rrd_stats_dimension_hide() for chart %s, dimension %s", st->name, id);
584 RRD_DIMENSION *rd = rrd_stats_dimension_find(st, id);
586 error("Cannot find dimension with id '%s' on stats '%s' (%s).", id, st->name, st->id);
594 void rrd_stats_dimension_set_by_pointer(RRD_STATS *st, RRD_DIMENSION *rd, collected_number value)
596 debug(D_RRD_CALLS, "rrd_stats_dimension_set() for chart %s, dimension %s, value " COLLECTED_NUMBER_FORMAT, st->name, rd->name, value);
598 gettimeofday(&rd->last_collected_time, NULL);
599 rd->collected_value = value;
602 int rrd_stats_dimension_set(RRD_STATS *st, const char *id, collected_number value)
604 RRD_DIMENSION *rd = rrd_stats_dimension_find(st, id);
606 error("Cannot find dimension with id '%s' on stats '%s' (%s).", id, st->name, st->id);
610 rrd_stats_dimension_set_by_pointer(st, rd, value);
614 void rrd_stats_next_usec(RRD_STATS *st, unsigned long long microseconds)
616 debug(D_RRD_CALLS, "rrd_stats_next() for chart %s with microseconds %llu", st->name, microseconds);
618 if(st->debug) debug(D_RRD_STATS, "%s: NEXT: %llu microseconds", st->name, microseconds);
619 st->usec_since_last_update = microseconds;
622 void rrd_stats_next(RRD_STATS *st)
624 unsigned long long microseconds = 0;
626 if(st->last_collected_time.tv_sec) {
628 gettimeofday(&now, NULL);
629 microseconds = usecdiff(&now, &st->last_collected_time);
632 rrd_stats_next_usec(st, microseconds);
635 void rrd_stats_next_plugins(RRD_STATS *st)
640 unsigned long long rrd_stats_done(RRD_STATS *st)
642 debug(D_RRD_CALLS, "rrd_stats_done() for chart %s", st->name);
644 RRD_DIMENSION *rd, *last;
647 if(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate) != 0)
648 error("Cannot set pthread cancel state to DISABLE.");
650 // a read lock is OK here
651 pthread_rwlock_rdlock(&st->rwlock);
653 if(st->usec_since_last_update > st->entries * st->update_every * 1000000ULL) {
654 info("Chart chart %s took too long to be updated (%0.3Lf secs). Reseting chart history.", st->name, (long double)(st->usec_since_last_update / 1000000.0));
656 st->usec_since_last_update = st->update_every * 1000000ULL;
659 if(st->debug) debug(D_RRD_STATS, "%s: microseconds since last update: %llu", st->name, st->usec_since_last_update);
661 if(!st->last_collected_time.tv_sec) gettimeofday(&st->last_collected_time, NULL);
663 unsigned long long ut = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec + st->usec_since_last_update;
664 st->last_collected_time.tv_sec = ut / 1000000ULL;
665 st->last_collected_time.tv_usec = ut % 1000000ULL;
668 if(!st->last_updated.tv_sec) {
669 unsigned long long ut = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec - st->usec_since_last_update;
670 st->last_updated.tv_sec = ut / 1000000ULL;
671 st->last_updated.tv_usec = ut % 1000000ULL;
673 if(st->debug) debug(D_RRD_STATS, "%s: initializing last_updated to now - %llu microseconds (%0.3Lf)", st->name, st->usec_since_last_update, (long double)ut/1000000.0);
676 if(usecdiff(&st->last_collected_time, &st->last_updated) > st->entries * st->update_every * 1000000ULL) {
677 info("History of chart %s too old (last updated at %u, last collected at %u). Reseting chart.", st->name, st->last_updated.tv_sec, st->last_collected_time.tv_sec);
679 st->usec_since_last_update = st->update_every * 1000000ULL;
680 pthread_rwlock_unlock(&st->rwlock);
681 return(st->usec_since_last_update);
684 unsigned long long last_ut = st->last_updated.tv_sec * 1000000ULL + st->last_updated.tv_usec;
685 unsigned long long now_ut = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec;
686 unsigned long long next_ut = (st->last_updated.tv_sec + st->update_every) * 1000000ULL;
688 if(st->debug) debug(D_RRD_STATS, "%s: last ut = %0.3Lf (last updated time)", st->name, (long double)last_ut/1000000.0);
689 if(st->debug) debug(D_RRD_STATS, "%s: now ut = %0.3Lf (current update time)", st->name, (long double)now_ut/1000000.0);
690 if(st->debug) debug(D_RRD_STATS, "%s: next ut = %0.3Lf (next interpolation point)", st->name, (long double)next_ut/1000000.0);
694 // calculate totals and count the dimensions
696 st->collected_total = 0;
697 for( rd = st->dimensions, dimensions = 0 ; rd ; rd = rd->next, dimensions++ )
698 st->collected_total += rd->collected_value;
700 // process all dimensions to calculate their values
701 // based on the collected figures only
702 // at this stage we do not interpolate anything
703 for( rd = st->dimensions ; rd ; rd = rd->next ) {
704 if(st->debug) debug(D_RRD_STATS, "%s/%s: "
705 " last_collected_value = " COLLECTED_NUMBER_FORMAT
706 " collected_value = " COLLECTED_NUMBER_FORMAT
707 " last_calculated_value = " CALCULATED_NUMBER_FORMAT
708 " calculated_value = " CALCULATED_NUMBER_FORMAT
710 , rd->last_collected_value
711 , rd->collected_value
712 , rd->last_calculated_value
713 , rd->calculated_value
716 switch(rd->algorithm) {
717 case RRD_DIMENSION_PCENT_OVER_DIFF_TOTAL:
718 // the percentage of the current increment
719 // over the increment of all dimensions together
720 if(st->collected_total == st->last_collected_total) rd->calculated_value = rd->last_calculated_value;
721 else rd->calculated_value =
722 (calculated_number)100
723 * (calculated_number)(rd->collected_value - rd->last_collected_value)
724 / (calculated_number)(st->collected_total - st->last_collected_total);
727 debug(D_RRD_STATS, "%s/%s: CALC PCENT-DIFF "
728 CALCULATED_NUMBER_FORMAT " = 100"
729 " * (" COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT ")"
730 " / (" COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT ")"
732 , rd->calculated_value
733 , rd->collected_value, rd->last_collected_value
734 , st->collected_total, st->last_collected_total
738 case RRD_DIMENSION_PCENT_OVER_ROW_TOTAL:
739 if(!st->collected_total) rd->calculated_value = 0;
741 // the percentage of the current value
742 // over the total of all dimensions
743 rd->calculated_value =
744 (calculated_number)100
745 * (calculated_number)rd->collected_value
746 / (calculated_number)st->collected_total;
749 debug(D_RRD_STATS, "%s/%s: CALC PCENT-ROW "
750 CALCULATED_NUMBER_FORMAT " = 100"
751 " * " COLLECTED_NUMBER_FORMAT
752 " / " COLLECTED_NUMBER_FORMAT
754 , rd->calculated_value
755 , rd->collected_value
756 , st->collected_total
760 case RRD_DIMENSION_INCREMENTAL:
761 // if the new is smaller than the old (an overflow, or reset), set the old equal to the new
762 // to reset the calculation (it will give zero as the calculation for this second)
763 if(rd->last_collected_value > rd->collected_value) rd->last_collected_value = rd->collected_value;
765 rd->calculated_value += (calculated_number)(rd->collected_value - rd->last_collected_value);
768 debug(D_RRD_STATS, "%s/%s: CALC INC "
769 CALCULATED_NUMBER_FORMAT " += "
770 COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT
772 , rd->calculated_value
773 , rd->collected_value, rd->last_collected_value
777 case RRD_DIMENSION_ABSOLUTE:
778 rd->calculated_value = (calculated_number)rd->collected_value;
781 debug(D_RRD_STATS, "%s/%s: CALC ABS/ABS-NO-IN "
782 CALCULATED_NUMBER_FORMAT " = "
783 COLLECTED_NUMBER_FORMAT
785 , rd->calculated_value
786 , rd->collected_value
791 // make the default zero, to make sure
792 // it gets noticed when we add new types
793 rd->calculated_value = 0;
796 debug(D_RRD_STATS, "%s/%s: CALC "
797 CALCULATED_NUMBER_FORMAT " = 0"
799 , rd->calculated_value
804 // at this point we have all the calculated values ready
806 if(st->counter_done == 1 || next_ut > now_ut) {
807 // we don't have any usable data yet
808 if(st->debug) debug(D_RRD_STATS, "%s: Skipping collected values (usec since last update = %llu, counter_done = %lu)", st->name, st->usec_since_last_update, st->counter_done);
810 for( rd = st->dimensions; rd ; rd = rd->next ) {
811 rd->last_calculated_value = rd->calculated_value;
812 rd->last_collected_value = rd->collected_value;
814 switch(rd->algorithm) {
815 case RRD_DIMENSION_PCENT_OVER_DIFF_TOTAL:
816 case RRD_DIMENSION_INCREMENTAL:
817 if(!st->usec_since_last_update) rd->calculated_value = 0;
818 // keep the previous values
819 // the next time, a new incremental total will be calculated
823 rd->collected_value = 0;
825 st->last_collected_total = st->collected_total;
827 pthread_rwlock_unlock(&st->rwlock);
828 if(pthread_setcancelstate(oldstate, NULL) != 0)
829 error("Cannot set pthread cancel state to RESTORE (%d).", oldstate);
831 return(st->usec_since_last_update);
834 // it is now time to interpolate values on a second boundary
835 unsigned long long first_ut = last_ut;
836 for( ; next_ut <= now_ut ; next_ut += st->update_every * 1000000ULL ) {
837 if(st->debug) debug(D_RRD_STATS, "%s: last ut = %0.3Lf (last updated time)", st->name, (long double)last_ut/1000000.0);
838 if(st->debug) debug(D_RRD_STATS, "%s: next ut = %0.3Lf (next interpolation point)", st->name, (long double)next_ut/1000000.0);
840 st->last_updated.tv_sec = next_ut / 1000000ULL;
841 st->last_updated.tv_usec = 0;
843 for( rd = st->dimensions ; rd ; rd = rd->next ) {
844 calculated_number new_value;
846 switch(rd->algorithm) {
847 case RRD_DIMENSION_INCREMENTAL:
848 new_value = (calculated_number)
849 ( rd->calculated_value
850 * (calculated_number)(next_ut - last_ut)
851 / (calculated_number)(now_ut - last_ut)
855 debug(D_RRD_STATS, "%s/%s: CALC2 INC "
856 CALCULATED_NUMBER_FORMAT " = "
857 CALCULATED_NUMBER_FORMAT
862 , rd->calculated_value
863 , (unsigned long long)(next_ut - last_ut)
864 , (unsigned long long)(now_ut - last_ut)
867 rd->calculated_value -= new_value;
870 case RRD_DIMENSION_ABSOLUTE:
871 case RRD_DIMENSION_PCENT_OVER_ROW_TOTAL:
872 case RRD_DIMENSION_PCENT_OVER_DIFF_TOTAL:
874 new_value = (calculated_number)
875 ( ( (rd->calculated_value - rd->last_calculated_value)
876 * (calculated_number)(next_ut - first_ut)
877 / (calculated_number)(now_ut - first_ut)
879 + rd->last_calculated_value
883 debug(D_RRD_STATS, "%s/%s: CALC2 DEF "
884 CALCULATED_NUMBER_FORMAT " = ((("
885 "(" CALCULATED_NUMBER_FORMAT " - " CALCULATED_NUMBER_FORMAT ")"
887 " / %llu) + " CALCULATED_NUMBER_FORMAT
890 , rd->calculated_value, rd->last_calculated_value
891 , (next_ut - first_ut)
892 , (now_ut - first_ut), rd->last_calculated_value
895 if(next_ut + st->update_every * 1000000ULL > now_ut) rd->calculated_value = new_value;
901 rd->values[st->current_entry] = pack_storage_number(
903 * (calculated_number)rd->multiplier
904 / (calculated_number)rd->divisor
908 debug(D_RRD_STATS, "%s/%s: STORE[%ld] "
909 CALCULATED_NUMBER_FORMAT " = " CALCULATED_NUMBER_FORMAT
914 , unpack_storage_number(rd->values[st->current_entry]), new_value
920 if(st->first_entry_t && st->counter >= (unsigned long long)st->entries) {
921 // the db is overwriting values
922 // add the value we will overwrite
923 st->first_entry_t += st->update_every * 1000000ULL;
927 st->current_entry = ((st->current_entry + 1) >= st->entries) ? 0 : st->current_entry + 1;
928 if(!st->first_entry_t) st->first_entry_t = next_ut;
932 for( rd = st->dimensions; rd ; rd = rd->next ) {
933 rd->last_collected_value = rd->collected_value;
934 rd->last_calculated_value = rd->calculated_value;
935 rd->collected_value = 0;
937 st->last_collected_total = st->collected_total;
939 // ALL DONE ABOUT THE DATA UPDATE
940 // --------------------------------------------------------------------
943 // find if there are any obsolete dimensions (not updated recently)
944 for( rd = st->dimensions; rd ; rd = rd->next )
945 if((rd->last_collected_time.tv_sec + (10 * st->update_every)) < st->last_collected_time.tv_sec)
949 // there is dimension to free
950 // upgrade our read lock to a write lock
951 pthread_rwlock_unlock(&st->rwlock);
952 pthread_rwlock_wrlock(&st->rwlock);
954 for( rd = st->dimensions, last = NULL ; rd ; ) {
955 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
956 debug(D_RRD_STATS, "Removing obsolete dimension '%s' (%s) of '%s' (%s).", rd->name, rd->id, st->name, st->id);
959 st->dimensions = rd->next;
961 rrd_stats_dimension_free(rd);
966 last->next = rd->next;
968 rrd_stats_dimension_free(rd);
978 if(!st->dimensions) st->enabled = 0;
981 pthread_rwlock_unlock(&st->rwlock);
983 if(pthread_setcancelstate(oldstate, NULL) != 0)
984 error("Cannot set pthread cancel state to RESTORE (%d).", oldstate);
986 return(st->usec_since_last_update);
990 // find the oldest entry in the data, skipping all empty slots
991 time_t rrd_stats_first_entry_t(RRD_STATS *st)
993 if(!st->first_entry_t) return st->last_updated.tv_sec;
995 return st->first_entry_t / 1000000;