3 #define RRD_DEFAULT_GAP_INTERPOLATIONS 1
5 // ----------------------------------------------------------------------------
8 // if not zero it gives the time (in seconds) to remove un-updated dimensions
10 // if dimensions are removed, the chart generation will have to run again
11 int rrd_delete_unupdated_dimensions = 0;
13 int rrd_update_every = UPDATE_EVERY;
14 int rrd_default_history_entries = RRD_DEFAULT_HISTORY_ENTRIES;
15 int rrd_memory_mode = RRD_MEMORY_MODE_SAVE;
17 static int rrdset_compare(void* a, void* b);
18 static int rrdset_compare_name(void* a, void* b);
19 static int rrdcontext_compare(void* a, void* b);
21 // ----------------------------------------------------------------------------
25 .hostname = "localhost",
27 .rrdset_root_rwlock = PTHREAD_RWLOCK_INITIALIZER,
28 .rrdset_root_index = {
29 { NULL, rrdset_compare },
32 .rrdset_root_index_name = {
33 { NULL, rrdset_compare_name },
36 .rrdcontext_root_index = {
37 { NULL, rrdcontext_compare },
40 .variables_root_index = {
41 { NULL, rrdvar_compare },
46 void rrdhost_rwlock(RRDHOST *host) {
47 pthread_rwlock_wrlock(&host->rrdset_root_rwlock);
50 void rrdhost_rdlock(RRDHOST *host) {
51 pthread_rwlock_rdlock(&host->rrdset_root_rwlock);
54 void rrdhost_unlock(RRDHOST *host) {
55 pthread_rwlock_unlock(&host->rrdset_root_rwlock);
58 void rrdhost_check_wrlock_int(RRDHOST *host, const char *file, const char *function, const unsigned long line) {
59 if(pthread_rwlock_tryrdlock(&host->rrdset_root_rwlock) == 0) {
60 fatal("RRDHOST '%s' should be write-locked, but it is not, at function %s() at line %lu of file '%s'", host->hostname, function, line, file);
64 // ----------------------------------------------------------------------------
67 static int rrdcontext_compare(void* a, void* b) {
68 if(((RRDCONTEXT *)a)->hash < ((RRDCONTEXT *)b)->hash) return -1;
69 else if(((RRDCONTEXT *)a)->hash > ((RRDCONTEXT *)b)->hash) return 1;
70 else return strcmp(((RRDCONTEXT *)a)->id, ((RRDCONTEXT *)b)->id);
73 #define rrdcontext_index_add(host, rc) (RRDCONTEXT *)avl_insert_lock(&((host)->rrdcontext_root_index), (avl *)(rc))
74 #define rrdcontext_index_del(host, rc) (RRDCONTEXT *)avl_remove_lock(&((host)->rrdcontext_root_index), (avl *)(rc))
76 static RRDCONTEXT *rrdcontext_index_find(RRDHOST *host, const char *id, uint32_t hash) {
79 tmp.hash = (hash)?hash:simple_hash(tmp.id);
81 return (RRDCONTEXT *)avl_search_lock(&(host->rrdcontext_root_index), (avl *) &tmp);
84 RRDCONTEXT *rrdcontext_create(const char *id) {
85 RRDCONTEXT *rc = rrdcontext_index_find(&localhost, id, 0);
87 rc = callocz(1, sizeof(RRDCONTEXT));
90 rc->hash = simple_hash(rc->id);
92 // initialize the variables index
93 avl_init_lock(&rc->variables_root_index, rrdvar_compare);
95 RRDCONTEXT *ret = rrdcontext_index_add(&localhost, rc);
97 fatal("INTERNAL ERROR: Expected to INSERT RRDCONTEXT '%s' into index, but inserted '%s'.", rc->id, (ret)?ret->id:"NONE");
104 void rrdcontext_free(RRDCONTEXT *rc) {
107 RRDCONTEXT *ret = rrdcontext_index_del(&localhost, rc);
109 fatal("INTERNAL ERROR: Expected to DELETE RRDCONTEXT '%s' from index, but deleted '%s'.", rc->id, (ret)?ret->id:"NONE");
111 if(rc->variables_root_index.avl_tree.root != NULL)
112 fatal("INTERNAL ERROR: Variables index of RRDCONTEXT '%s' that is freed, is not empty.", rc->id);
114 freez((void *)rc->id);
119 // ----------------------------------------------------------------------------
122 static int rrdset_compare(void* a, void* b) {
123 if(((RRDSET *)a)->hash < ((RRDSET *)b)->hash) return -1;
124 else if(((RRDSET *)a)->hash > ((RRDSET *)b)->hash) return 1;
125 else return strcmp(((RRDSET *)a)->id, ((RRDSET *)b)->id);
128 #define rrdset_index_add(host, st) (RRDSET *)avl_insert_lock(&((host)->rrdset_root_index), (avl *)(st))
129 #define rrdset_index_del(host, st) (RRDSET *)avl_remove_lock(&((host)->rrdset_root_index), (avl *)(st))
131 static RRDSET *rrdset_index_find(RRDHOST *host, const char *id, uint32_t hash) {
133 strncpyz(tmp.id, id, RRD_ID_LENGTH_MAX);
134 tmp.hash = (hash)?hash:simple_hash(tmp.id);
136 return (RRDSET *)avl_search_lock(&(host->rrdset_root_index), (avl *) &tmp);
139 // ----------------------------------------------------------------------------
142 #define rrdset_from_avlname(avlname_ptr) ((RRDSET *)((avlname_ptr) - offsetof(RRDSET, avlname)))
144 static int rrdset_compare_name(void* a, void* b) {
145 RRDSET *A = rrdset_from_avlname(a);
146 RRDSET *B = rrdset_from_avlname(b);
148 // fprintf(stderr, "COMPARING: %s with %s\n", A->name, B->name);
150 if(A->hash_name < B->hash_name) return -1;
151 else if(A->hash_name > B->hash_name) return 1;
152 else return strcmp(A->name, B->name);
155 RRDSET *rrdset_index_add_name(RRDHOST *host, RRDSET *st) {
157 // fprintf(stderr, "ADDING: %s (name: %s)\n", st->id, st->name);
158 result = avl_insert_lock(&host->rrdset_root_index_name, (avl *) (&st->avlname));
159 if(result) return rrdset_from_avlname(result);
163 RRDSET *rrdset_index_del_name(RRDHOST *host, RRDSET *st) {
165 // fprintf(stderr, "DELETING: %s (name: %s)\n", st->id, st->name);
166 return (RRDSET *)avl_remove_lock(&((host)->rrdset_root_index_name), (avl *)(&st->avlname));
167 if(result) return rrdset_from_avlname(result);
171 static RRDSET *rrdset_index_find_name(RRDHOST *host, const char *name, uint32_t hash) {
175 tmp.hash_name = (hash)?hash:simple_hash(tmp.name);
177 // fprintf(stderr, "SEARCHING: %s\n", name);
178 result = avl_search_lock(&host->rrdset_root_index_name, (avl *) (&(tmp.avlname)));
180 RRDSET *st = rrdset_from_avlname(result);
181 if(strcmp(st->magic, RRDSET_MAGIC))
182 error("Search for RRDSET %s returned an invalid RRDSET %s (name %s)", name, st->id, st->name);
184 // fprintf(stderr, "FOUND: %s\n", name);
185 return rrdset_from_avlname(result);
187 // fprintf(stderr, "NOT FOUND: %s\n", name);
192 // ----------------------------------------------------------------------------
195 static int rrddim_compare(void* a, void* b) {
196 if(((RRDDIM *)a)->hash < ((RRDDIM *)b)->hash) return -1;
197 else if(((RRDDIM *)a)->hash > ((RRDDIM *)b)->hash) return 1;
198 else return strcmp(((RRDDIM *)a)->id, ((RRDDIM *)b)->id);
201 #define rrddim_index_add(st, rd) avl_insert_lock(&((st)->dimensions_index), (avl *)(rd))
202 #define rrddim_index_del(st,rd ) avl_remove_lock(&((st)->dimensions_index), (avl *)(rd))
204 static RRDDIM *rrddim_index_find(RRDSET *st, const char *id, uint32_t hash) {
206 strncpyz(tmp.id, id, RRD_ID_LENGTH_MAX);
207 tmp.hash = (hash)?hash:simple_hash(tmp.id);
209 return (RRDDIM *)avl_search_lock(&(st->dimensions_index), (avl *) &tmp);
212 // ----------------------------------------------------------------------------
215 int rrdset_type_id(const char *name)
217 if(unlikely(strcmp(name, RRDSET_TYPE_AREA_NAME) == 0)) return RRDSET_TYPE_AREA;
218 else if(unlikely(strcmp(name, RRDSET_TYPE_STACKED_NAME) == 0)) return RRDSET_TYPE_STACKED;
219 else if(unlikely(strcmp(name, RRDSET_TYPE_LINE_NAME) == 0)) return RRDSET_TYPE_LINE;
220 return RRDSET_TYPE_LINE;
223 const char *rrdset_type_name(int chart_type)
225 static char line[] = RRDSET_TYPE_LINE_NAME;
226 static char area[] = RRDSET_TYPE_AREA_NAME;
227 static char stacked[] = RRDSET_TYPE_STACKED_NAME;
230 case RRDSET_TYPE_LINE:
233 case RRDSET_TYPE_AREA:
236 case RRDSET_TYPE_STACKED:
242 // ----------------------------------------------------------------------------
245 const char *rrd_memory_mode_name(int id)
247 static const char ram[] = RRD_MEMORY_MODE_RAM_NAME;
248 static const char map[] = RRD_MEMORY_MODE_MAP_NAME;
249 static const char save[] = RRD_MEMORY_MODE_SAVE_NAME;
252 case RRD_MEMORY_MODE_RAM:
255 case RRD_MEMORY_MODE_MAP:
258 case RRD_MEMORY_MODE_SAVE:
266 int rrd_memory_mode_id(const char *name)
268 if(unlikely(!strcmp(name, RRD_MEMORY_MODE_RAM_NAME)))
269 return RRD_MEMORY_MODE_RAM;
270 else if(unlikely(!strcmp(name, RRD_MEMORY_MODE_MAP_NAME)))
271 return RRD_MEMORY_MODE_MAP;
273 return RRD_MEMORY_MODE_SAVE;
276 // ----------------------------------------------------------------------------
279 int rrddim_algorithm_id(const char *name)
281 if(strcmp(name, RRDDIM_INCREMENTAL_NAME) == 0) return RRDDIM_INCREMENTAL;
282 if(strcmp(name, RRDDIM_ABSOLUTE_NAME) == 0) return RRDDIM_ABSOLUTE;
283 if(strcmp(name, RRDDIM_PCENT_OVER_ROW_TOTAL_NAME) == 0) return RRDDIM_PCENT_OVER_ROW_TOTAL;
284 if(strcmp(name, RRDDIM_PCENT_OVER_DIFF_TOTAL_NAME) == 0) return RRDDIM_PCENT_OVER_DIFF_TOTAL;
285 return RRDDIM_ABSOLUTE;
288 const char *rrddim_algorithm_name(int chart_type)
290 static char absolute[] = RRDDIM_ABSOLUTE_NAME;
291 static char incremental[] = RRDDIM_INCREMENTAL_NAME;
292 static char percentage_of_absolute_row[] = RRDDIM_PCENT_OVER_ROW_TOTAL_NAME;
293 static char percentage_of_incremental_row[] = RRDDIM_PCENT_OVER_DIFF_TOTAL_NAME;
296 case RRDDIM_ABSOLUTE:
299 case RRDDIM_INCREMENTAL:
302 case RRDDIM_PCENT_OVER_ROW_TOTAL:
303 return percentage_of_absolute_row;
305 case RRDDIM_PCENT_OVER_DIFF_TOTAL:
306 return percentage_of_incremental_row;
311 // ----------------------------------------------------------------------------
314 char *rrdset_strncpyz_name(char *to, const char *from, size_t length)
318 while (length-- && (c = *from++)) {
319 if(c != '.' && !isalnum(c))
330 void rrdset_set_name(RRDSET *st, const char *name)
332 debug(D_RRD_CALLS, "rrdset_set_name() old: %s, new: %s", st->name, name);
335 rrdset_index_del_name(&localhost, st);
336 rrdsetvar_rename_all(st);
339 char b[CONFIG_MAX_VALUE + 1];
340 char n[RRD_ID_LENGTH_MAX + 1];
342 snprintfz(n, RRD_ID_LENGTH_MAX, "%s.%s", st->type, name);
343 rrdset_strncpyz_name(b, n, CONFIG_MAX_VALUE);
344 st->name = config_get(st->id, "name", b);
345 st->hash_name = simple_hash(st->name);
347 rrdset_index_add_name(&localhost, st);
350 // ----------------------------------------------------------------------------
353 char *rrdset_cache_dir(const char *id)
357 static char *cache_dir = NULL;
359 cache_dir = config_get("global", "cache directory", CACHE_DIR);
360 int r = mkdir(cache_dir, 0755);
361 if(r != 0 && errno != EEXIST)
362 error("Cannot create directory '%s'", cache_dir);
365 char b[FILENAME_MAX + 1];
366 char n[FILENAME_MAX + 1];
367 rrdset_strncpyz_name(b, id, FILENAME_MAX);
369 snprintfz(n, FILENAME_MAX, "%s/%s", cache_dir, b);
370 ret = config_get(id, "cache directory", n);
372 if(rrd_memory_mode == RRD_MEMORY_MODE_MAP || rrd_memory_mode == RRD_MEMORY_MODE_SAVE) {
373 int r = mkdir(ret, 0775);
374 if(r != 0 && errno != EEXIST)
375 error("Cannot create directory '%s'", ret);
381 // ----------------------------------------------------------------------------
384 void rrdset_reset(RRDSET *st)
386 debug(D_RRD_CALLS, "rrdset_reset() %s", st->name);
388 st->last_collected_time.tv_sec = 0;
389 st->last_collected_time.tv_usec = 0;
390 st->last_updated.tv_sec = 0;
391 st->last_updated.tv_usec = 0;
392 st->current_entry = 0;
394 st->counter_done = 0;
397 for(rd = st->dimensions; rd ; rd = rd->next) {
398 rd->last_collected_time.tv_sec = 0;
399 rd->last_collected_time.tv_usec = 0;
401 bzero(rd->values, rd->entries * sizeof(storage_number));
405 RRDSET *rrdset_create(const char *type, const char *id, const char *name, const char *family, const char *context, const char *title, const char *units, long priority, int update_every, int chart_type)
407 if(!type || !type[0]) {
408 fatal("Cannot create rrd stats without a type.");
413 fatal("Cannot create rrd stats without an id.");
417 char fullid[RRD_ID_LENGTH_MAX + 1];
418 char fullfilename[FILENAME_MAX + 1];
421 snprintfz(fullid, RRD_ID_LENGTH_MAX, "%s.%s", type, id);
423 st = rrdset_find(fullid);
425 error("Cannot create rrd stats for '%s', it already exists.", fullid);
429 long entries = config_get_number(fullid, "history", rrd_default_history_entries);
430 if(entries < 5) entries = config_set_number(fullid, "history", 5);
431 if(entries > RRD_HISTORY_ENTRIES_MAX) entries = config_set_number(fullid, "history", RRD_HISTORY_ENTRIES_MAX);
433 int enabled = config_get_boolean(fullid, "enabled", 1);
434 if(!enabled) entries = 5;
436 unsigned long size = sizeof(RRDSET);
437 char *cache_dir = rrdset_cache_dir(fullid);
439 debug(D_RRD_CALLS, "Creating RRD_STATS for '%s.%s'.", type, id);
441 snprintfz(fullfilename, FILENAME_MAX, "%s/main.db", cache_dir);
442 if(rrd_memory_mode != RRD_MEMORY_MODE_RAM) st = (RRDSET *)mymmap(fullfilename, size, ((rrd_memory_mode == RRD_MEMORY_MODE_MAP)?MAP_SHARED:MAP_PRIVATE), 0);
444 if(strcmp(st->magic, RRDSET_MAGIC) != 0) {
446 info("Initializing file %s.", fullfilename);
449 else if(strcmp(st->id, fullid) != 0) {
451 error("File %s contents are not for chart %s. Clearing it.", fullfilename, fullid);
456 else if(st->memsize != size || st->entries != entries) {
458 error("File %s does not have the desired size. Clearing it.", fullfilename);
461 else if(st->update_every != update_every) {
463 error("File %s does not have the desired update frequency. Clearing it.", fullfilename);
466 else if((time(NULL) - st->last_updated.tv_sec) > update_every * entries) {
468 error("File %s is too old. Clearing it.", fullfilename);
480 st->dimensions = NULL;
482 st->mapped = rrd_memory_mode;
483 st->variables = NULL;
486 st = callocz(1, size);
487 st->mapped = RRD_MEMORY_MODE_RAM;
490 st->entries = entries;
491 st->update_every = update_every;
493 if(st->current_entry >= st->entries) st->current_entry = 0;
495 strcpy(st->cache_filename, fullfilename);
496 strcpy(st->magic, RRDSET_MAGIC);
498 strcpy(st->id, fullid);
499 st->hash = simple_hash(st->id);
501 st->cache_dir = cache_dir;
503 st->chart_type = rrdset_type_id(config_get(st->id, "chart type", rrdset_type_name(chart_type)));
504 st->type = config_get(st->id, "type", type);
505 st->family = config_get(st->id, "family", family?family:st->type);
506 st->context = config_get(st->id, "context", context?context:st->id);
507 st->units = config_get(st->id, "units", units?units:"");
509 st->priority = config_get_number(st->id, "priority", priority);
510 st->enabled = enabled;
515 st->last_collected_time.tv_sec = 0;
516 st->last_collected_time.tv_usec = 0;
517 st->counter_done = 0;
519 st->gap_when_lost_iterations_above = (int) (
520 config_get_number(st->id, "gap when lost iterations above", RRD_DEFAULT_GAP_INTERPOLATIONS) + 2);
522 avl_init_lock(&st->dimensions_index, rrddim_compare);
523 avl_init_lock(&st->variables_root_index, rrdvar_compare);
525 pthread_rwlock_init(&st->rwlock, NULL);
526 pthread_rwlock_wrlock(&localhost.rrdset_root_rwlock);
528 if(name && *name) rrdset_set_name(st, name);
529 else rrdset_set_name(st, id);
532 char varvalue[CONFIG_MAX_VALUE + 1];
533 snprintfz(varvalue, CONFIG_MAX_VALUE, "%s (%s)", title?title:"", st->name);
534 st->title = config_get(st->id, "title", varvalue);
537 st->rrdcontext = rrdcontext_create(st->context);
538 st->rrdhost = &localhost;
540 st->next = localhost.rrdset_root;
541 localhost.rrdset_root = st;
543 rrdsetvar_create(st, "last_collected", RRDVAR_TYPE_TIME_T, &st->last_collected_time.tv_sec, 0);
544 rrdsetvar_create(st, "raw_total", RRDVAR_TYPE_TOTAL, &st->collected_total, 0);
546 rrdset_index_add(&localhost, st);
548 rrdsetcalc_link_matching(st);
550 pthread_rwlock_unlock(&localhost.rrdset_root_rwlock);
555 RRDDIM *rrddim_add(RRDSET *st, const char *id, const char *name, long multiplier, long divisor, int algorithm)
557 char filename[FILENAME_MAX + 1];
558 char fullfilename[FILENAME_MAX + 1];
560 char varname[CONFIG_MAX_NAME + 1];
562 unsigned long size = sizeof(RRDDIM) + (st->entries * sizeof(storage_number));
564 debug(D_RRD_CALLS, "Adding dimension '%s/%s'.", st->id, id);
566 rrdset_strncpyz_name(filename, id, FILENAME_MAX);
567 snprintfz(fullfilename, FILENAME_MAX, "%s/%s.db", st->cache_dir, filename);
568 if(rrd_memory_mode != RRD_MEMORY_MODE_RAM) rd = (RRDDIM *)mymmap(fullfilename, size, ((rrd_memory_mode == RRD_MEMORY_MODE_MAP)?MAP_SHARED:MAP_PRIVATE), 1);
571 gettimeofday(&now, NULL);
573 if(strcmp(rd->magic, RRDDIMENSION_MAGIC) != 0) {
575 info("Initializing file %s.", fullfilename);
578 else if(rd->memsize != size) {
580 error("File %s does not have the desired size. Clearing it.", fullfilename);
583 else if(rd->multiplier != multiplier) {
585 error("File %s does not have the same multiplier. Clearing it.", fullfilename);
588 else if(rd->divisor != divisor) {
590 error("File %s does not have the same divisor. Clearing it.", fullfilename);
593 else if(rd->algorithm != algorithm) {
595 error("File %s does not have the same algorithm. Clearing it.", fullfilename);
598 else if(rd->update_every != st->update_every) {
600 error("File %s does not have the same refresh frequency. Clearing it.", fullfilename);
603 else if(usecdiff(&now, &rd->last_collected_time) > (rd->entries * rd->update_every * 1000000ULL)) {
605 error("File %s is too old. Clearing it.", fullfilename);
608 else if(strcmp(rd->id, id) != 0) {
610 error("File %s contents are not for dimension %s. Clearing it.", fullfilename, id);
618 // we have a file mapped for rd
619 rd->mapped = rrd_memory_mode;
620 rd->flags = 0x00000000;
621 rd->variables = NULL;
626 // if we didn't manage to get a mmap'd dimension, just create one
628 rd = callocz(1, size);
629 rd->mapped = RRD_MEMORY_MODE_RAM;
633 strcpy(rd->magic, RRDDIMENSION_MAGIC);
634 strcpy(rd->cache_filename, fullfilename);
635 strncpyz(rd->id, id, RRD_ID_LENGTH_MAX);
636 rd->hash = simple_hash(rd->id);
638 snprintfz(varname, CONFIG_MAX_NAME, "dim %s name", rd->id);
639 rd->name = config_get(st->id, varname, (name && *name)?name:rd->id);
641 snprintfz(varname, CONFIG_MAX_NAME, "dim %s algorithm", rd->id);
642 rd->algorithm = rrddim_algorithm_id(config_get(st->id, varname, rrddim_algorithm_name(algorithm)));
644 snprintfz(varname, CONFIG_MAX_NAME, "dim %s multiplier", rd->id);
645 rd->multiplier = config_get_number(st->id, varname, multiplier);
647 snprintfz(varname, CONFIG_MAX_NAME, "dim %s divisor", rd->id);
648 rd->divisor = config_get_number(st->id, varname, divisor);
649 if(!rd->divisor) rd->divisor = 1;
651 rd->entries = st->entries;
652 rd->update_every = st->update_every;
654 // prevent incremental calculation spikes
657 rd->calculated_value = 0;
658 rd->last_calculated_value = 0;
659 rd->collected_value = 0;
660 rd->last_collected_value = 0;
661 rd->collected_volume = 0;
662 rd->stored_volume = 0;
663 rd->values[st->current_entry] = pack_storage_number(0, SN_NOT_EXISTS);
664 rd->last_collected_time.tv_sec = 0;
665 rd->last_collected_time.tv_usec = 0;
668 // append this dimension
669 pthread_rwlock_wrlock(&st->rwlock);
673 RRDDIM *td = st->dimensions;
674 for(; td->next; td = td->next) ;
678 rrddimvar_create(rd, RRDVAR_TYPE_CALCULATED, NULL, NULL, &rd->calculated_value, 0);
679 rrddimvar_create(rd, RRDVAR_TYPE_COLLECTED, NULL, "_raw", &rd->collected_value, 0);
680 rrddimvar_create(rd, RRDVAR_TYPE_TIME_T, NULL, "_last_collected", &rd->last_collected_time.tv_sec, 0);
682 pthread_rwlock_unlock(&st->rwlock);
684 rrddim_index_add(st, rd);
689 void rrddim_set_name(RRDSET *st, RRDDIM *rd, const char *name)
691 debug(D_RRD_CALLS, "rrddim_set_name() %s.%s", st->name, rd->name);
693 char varname[CONFIG_MAX_NAME + 1];
694 snprintfz(varname, CONFIG_MAX_NAME, "dim %s name", rd->id);
695 config_set_default(st->id, varname, name);
697 rrddimvar_rename_all(rd);
700 void rrddim_free(RRDSET *st, RRDDIM *rd)
702 debug(D_RRD_CALLS, "rrddim_free() %s.%s", st->name, rd->name);
704 RRDDIM *i, *last = NULL;
705 for(i = st->dimensions; i && i != rd ; i = i->next) last = i;
708 error("Request to free dimension %s.%s but it is not linked.", st->id, rd->name);
712 if(last) last->next = rd->next;
713 else st->dimensions = rd->next;
717 rrddimvar_free(rd->variables);
719 rrddim_index_del(st, rd);
721 // free(rd->annotations);
722 if(rd->mapped == RRD_MEMORY_MODE_SAVE) {
723 debug(D_RRD_CALLS, "Saving dimension '%s' to '%s'.", rd->name, rd->cache_filename);
724 savememory(rd->cache_filename, rd, rd->memsize);
726 debug(D_RRD_CALLS, "Unmapping dimension '%s'.", rd->name);
727 munmap(rd, rd->memsize);
729 else if(rd->mapped == RRD_MEMORY_MODE_MAP) {
730 debug(D_RRD_CALLS, "Unmapping dimension '%s'.", rd->name);
731 munmap(rd, rd->memsize);
734 debug(D_RRD_CALLS, "Removing dimension '%s'.", rd->name);
739 void rrdset_free_all(void)
741 info("Freeing all memory...");
743 pthread_rwlock_wrlock(&localhost.rrdset_root_rwlock);
746 for(st = localhost.rrdset_root; st ;) {
747 RRDSET *next = st->next;
749 pthread_rwlock_wrlock(&st->rwlock);
752 rrdsetvar_free(st->variables);
754 while(st->calculations)
755 rrdsetcalc_unlink(st->calculations);
757 while(st->dimensions)
758 rrddim_free(st, st->dimensions);
760 rrdset_index_del(&localhost, st);
762 st->rrdcontext->use_count--;
763 if(!st->rrdcontext->use_count)
764 rrdcontext_free(st->rrdcontext);
766 pthread_rwlock_unlock(&st->rwlock);
768 if(st->mapped == RRD_MEMORY_MODE_SAVE) {
769 debug(D_RRD_CALLS, "Saving stats '%s' to '%s'.", st->name, st->cache_filename);
770 savememory(st->cache_filename, st, st->memsize);
772 debug(D_RRD_CALLS, "Unmapping stats '%s'.", st->name);
773 munmap(st, st->memsize);
775 else if(st->mapped == RRD_MEMORY_MODE_MAP) {
776 debug(D_RRD_CALLS, "Unmapping stats '%s'.", st->name);
777 munmap(st, st->memsize);
784 localhost.rrdset_root = NULL;
786 pthread_rwlock_unlock(&localhost.rrdset_root_rwlock);
788 info("Memory cleanup completed...");
791 void rrdset_save_all(void) {
792 info("Saving database...");
797 pthread_rwlock_wrlock(&localhost.rrdset_root_rwlock);
798 for(st = localhost.rrdset_root; st ; st = st->next) {
799 pthread_rwlock_wrlock(&st->rwlock);
801 if(st->mapped == RRD_MEMORY_MODE_SAVE) {
802 debug(D_RRD_CALLS, "Saving stats '%s' to '%s'.", st->name, st->cache_filename);
803 savememory(st->cache_filename, st, st->memsize);
806 for(rd = st->dimensions; rd ; rd = rd->next) {
807 if(likely(rd->mapped == RRD_MEMORY_MODE_SAVE)) {
808 debug(D_RRD_CALLS, "Saving dimension '%s' to '%s'.", rd->name, rd->cache_filename);
809 savememory(rd->cache_filename, rd, rd->memsize);
813 pthread_rwlock_unlock(&st->rwlock);
815 pthread_rwlock_unlock(&localhost.rrdset_root_rwlock);
819 RRDSET *rrdset_find(const char *id)
821 debug(D_RRD_CALLS, "rrdset_find() for chart %s", id);
823 RRDSET *st = rrdset_index_find(&localhost, id, 0);
827 RRDSET *rrdset_find_bytype(const char *type, const char *id)
829 debug(D_RRD_CALLS, "rrdset_find_bytype() for chart %s.%s", type, id);
831 char buf[RRD_ID_LENGTH_MAX + 1];
833 strncpyz(buf, type, RRD_ID_LENGTH_MAX - 1);
835 int len = (int) strlen(buf);
836 strncpyz(&buf[len], id, (size_t) (RRD_ID_LENGTH_MAX - len));
838 return(rrdset_find(buf));
841 RRDSET *rrdset_find_byname(const char *name)
843 debug(D_RRD_CALLS, "rrdset_find_byname() for chart %s", name);
845 RRDSET *st = rrdset_index_find_name(&localhost, name, 0);
849 RRDDIM *rrddim_find(RRDSET *st, const char *id)
851 debug(D_RRD_CALLS, "rrddim_find() for chart %s, dimension %s", st->name, id);
853 return rrddim_index_find(st, id, 0);
856 int rrddim_hide(RRDSET *st, const char *id)
858 debug(D_RRD_CALLS, "rrddim_hide() for chart %s, dimension %s", st->name, id);
860 RRDDIM *rd = rrddim_find(st, id);
862 error("Cannot find dimension with id '%s' on stats '%s' (%s).", id, st->name, st->id);
866 rd->flags |= RRDDIM_FLAG_HIDDEN;
870 int rrddim_unhide(RRDSET *st, const char *id)
872 debug(D_RRD_CALLS, "rrddim_unhide() for chart %s, dimension %s", st->name, id);
874 RRDDIM *rd = rrddim_find(st, id);
876 error("Cannot find dimension with id '%s' on stats '%s' (%s).", id, st->name, st->id);
880 if(rd->flags & RRDDIM_FLAG_HIDDEN) rd->flags ^= RRDDIM_FLAG_HIDDEN;
884 collected_number rrddim_set_by_pointer(RRDSET *st, RRDDIM *rd, collected_number value)
886 debug(D_RRD_CALLS, "rrddim_set_by_pointer() for chart %s, dimension %s, value " COLLECTED_NUMBER_FORMAT, st->name, rd->name, value);
888 gettimeofday(&rd->last_collected_time, NULL);
889 rd->collected_value = value;
893 return rd->last_collected_value;
896 collected_number rrddim_set(RRDSET *st, const char *id, collected_number value)
898 RRDDIM *rd = rrddim_find(st, id);
900 error("Cannot find dimension with id '%s' on stats '%s' (%s).", id, st->name, st->id);
904 return rrddim_set_by_pointer(st, rd, value);
907 void rrdset_next_usec(RRDSET *st, unsigned long long microseconds)
909 if(!microseconds) rrdset_next(st);
911 debug(D_RRD_CALLS, "rrdset_next_usec() for chart %s with microseconds %llu", st->name, microseconds);
913 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s: NEXT: %llu microseconds", st->name, microseconds);
914 st->usec_since_last_update = microseconds;
918 void rrdset_next(RRDSET *st)
920 unsigned long long microseconds = 0;
922 if(likely(st->last_collected_time.tv_sec)) {
924 gettimeofday(&now, NULL);
925 microseconds = usecdiff(&now, &st->last_collected_time);
927 // prevent infinite loop
928 else microseconds = st->update_every * 1000000ULL;
930 rrdset_next_usec(st, microseconds);
933 void rrdset_next_plugins(RRDSET *st)
938 unsigned long long rrdset_done(RRDSET *st)
940 if(unlikely(netdata_exit)) return 0;
942 debug(D_RRD_CALLS, "rrdset_done() for chart %s", st->name);
945 int oldstate, store_this_entry = 1, first_entry = 0;
946 unsigned long long last_ut, now_ut, next_ut, stored_entries = 0;
948 if(unlikely(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate) != 0))
949 error("Cannot set pthread cancel state to DISABLE.");
951 // a read lock is OK here
952 pthread_rwlock_rdlock(&st->rwlock);
954 // enable the chart, if it was disabled
955 if(unlikely(rrd_delete_unupdated_dimensions) && !st->enabled)
958 // check if the chart has a long time to be updated
959 if(unlikely(st->usec_since_last_update > st->entries * st->update_every * 1000000ULL)) {
960 info("%s: took too long to be updated (%0.3Lf secs). Reseting it.", st->name, (long double)(st->usec_since_last_update / 1000000.0));
962 st->usec_since_last_update = st->update_every * 1000000ULL;
965 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s: microseconds since last update: %llu", st->name, st->usec_since_last_update);
967 // set last_collected_time
968 if(unlikely(!st->last_collected_time.tv_sec)) {
969 // it is the first entry
970 // set the last_collected_time to now
971 gettimeofday(&st->last_collected_time, NULL);
973 // the first entry should not be stored
974 store_this_entry = 0;
977 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s: has not set last_collected_time. Setting it now. Will not store the next entry.", st->name);
980 // it is not the first entry
981 // calculate the proper last_collected_time, using usec_since_last_update
982 unsigned long long ut = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec + st->usec_since_last_update;
983 st->last_collected_time.tv_sec = (time_t) (ut / 1000000ULL);
984 st->last_collected_time.tv_usec = (suseconds_t) (ut % 1000000ULL);
987 // if this set has not been updated in the past
988 // we fake the last_update time to be = now - usec_since_last_update
989 if(unlikely(!st->last_updated.tv_sec)) {
990 // it has never been updated before
991 // set a fake last_updated, in the past using usec_since_last_update
992 unsigned long long ut = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec - st->usec_since_last_update;
993 st->last_updated.tv_sec = (time_t) (ut / 1000000ULL);
994 st->last_updated.tv_usec = (suseconds_t) (ut % 1000000ULL);
996 // the first entry should not be stored
997 store_this_entry = 0;
1000 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s: initializing last_updated to now - %llu microseconds (%0.3Lf). Will not store the next entry.", st->name, st->usec_since_last_update, (long double)ut/1000000.0);
1003 // check if we will re-write the entire data set
1004 if(unlikely(usecdiff(&st->last_collected_time, &st->last_updated) > st->update_every * st->entries * 1000000ULL)) {
1005 info("%s: too old data (last updated at %ld.%ld, last collected at %ld.%ld). 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);
1008 st->usec_since_last_update = st->update_every * 1000000ULL;
1010 gettimeofday(&st->last_collected_time, NULL);
1012 unsigned long long ut = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec - st->usec_since_last_update;
1013 st->last_updated.tv_sec = (time_t) (ut / 1000000ULL);
1014 st->last_updated.tv_usec = (suseconds_t) (ut % 1000000ULL);
1016 // the first entry should not be stored
1017 store_this_entry = 0;
1021 // these are the 3 variables that will help us in interpolation
1022 // last_ut = the last time we added a value to the storage
1023 // now_ut = the time the current value is taken at
1024 // next_ut = the time of the next interpolation point
1025 last_ut = st->last_updated.tv_sec * 1000000ULL + st->last_updated.tv_usec;
1026 now_ut = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec;
1027 next_ut = (st->last_updated.tv_sec + st->update_every) * 1000000ULL;
1029 if(unlikely(!first_entry && now_ut < next_ut)) {
1030 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s: THIS IS IN THE SAME INTERPOLATION POINT", st->name);
1033 if(unlikely(st->debug)) {
1034 debug(D_RRD_STATS, "%s: last ut = %0.3Lf (last updated time)", st->name, (long double)last_ut/1000000.0);
1035 debug(D_RRD_STATS, "%s: now ut = %0.3Lf (current update time)", st->name, (long double)now_ut/1000000.0);
1036 debug(D_RRD_STATS, "%s: next ut = %0.3Lf (next interpolation point)", st->name, (long double)next_ut/1000000.0);
1039 if(unlikely(!st->counter_done)) {
1040 store_this_entry = 0;
1041 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s: Will not store the next entry.", st->name);
1045 // calculate totals and count the dimensions
1047 st->collected_total = 0;
1048 for( rd = st->dimensions, dimensions = 0 ; likely(rd) ; rd = rd->next, dimensions++ )
1049 if(likely(rd->updated)) st->collected_total += rd->collected_value;
1051 uint32_t storage_flags = SN_EXISTS;
1053 // process all dimensions to calculate their values
1054 // based on the collected figures only
1055 // at this stage we do not interpolate anything
1056 for( rd = st->dimensions ; likely(rd) ; rd = rd->next ) {
1058 if(unlikely(!rd->updated)) {
1059 rd->calculated_value = 0;
1063 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s/%s: START "
1064 " last_collected_value = " COLLECTED_NUMBER_FORMAT
1065 " collected_value = " COLLECTED_NUMBER_FORMAT
1066 " last_calculated_value = " CALCULATED_NUMBER_FORMAT
1067 " calculated_value = " CALCULATED_NUMBER_FORMAT
1069 , rd->last_collected_value
1070 , rd->collected_value
1071 , rd->last_calculated_value
1072 , rd->calculated_value
1075 switch(rd->algorithm) {
1076 case RRDDIM_ABSOLUTE:
1077 rd->calculated_value = (calculated_number)rd->collected_value
1078 * (calculated_number)rd->multiplier
1079 / (calculated_number)rd->divisor;
1081 if(unlikely(st->debug))
1082 debug(D_RRD_STATS, "%s/%s: CALC ABS/ABS-NO-IN "
1083 CALCULATED_NUMBER_FORMAT " = "
1084 COLLECTED_NUMBER_FORMAT
1085 " * " CALCULATED_NUMBER_FORMAT
1086 " / " CALCULATED_NUMBER_FORMAT
1088 , rd->calculated_value
1089 , rd->collected_value
1090 , (calculated_number)rd->multiplier
1091 , (calculated_number)rd->divisor
1095 case RRDDIM_PCENT_OVER_ROW_TOTAL:
1096 if(unlikely(!st->collected_total))
1097 rd->calculated_value = 0;
1099 // the percentage of the current value
1100 // over the total of all dimensions
1101 rd->calculated_value =
1102 (calculated_number)100
1103 * (calculated_number)rd->collected_value
1104 / (calculated_number)st->collected_total;
1106 if(unlikely(st->debug))
1107 debug(D_RRD_STATS, "%s/%s: CALC PCENT-ROW "
1108 CALCULATED_NUMBER_FORMAT " = 100"
1109 " * " COLLECTED_NUMBER_FORMAT
1110 " / " COLLECTED_NUMBER_FORMAT
1112 , rd->calculated_value
1113 , rd->collected_value
1114 , st->collected_total
1118 case RRDDIM_INCREMENTAL:
1119 if(unlikely(rd->counter <= 1)) {
1120 rd->calculated_value = 0;
1124 // if the new is smaller than the old (an overflow, or reset), set the old equal to the new
1125 // to reset the calculation (it will give zero as the calculation for this second)
1126 if(unlikely(rd->last_collected_value > rd->collected_value)) {
1127 debug(D_RRD_STATS, "%s.%s: RESET or OVERFLOW. Last collected value = " COLLECTED_NUMBER_FORMAT ", current = " COLLECTED_NUMBER_FORMAT
1128 , st->name, rd->name
1129 , rd->last_collected_value
1130 , rd->collected_value);
1131 if(!(rd->flags & RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS)) storage_flags = SN_EXISTS_RESET;
1132 rd->last_collected_value = rd->collected_value;
1135 rd->calculated_value =
1136 (calculated_number)(rd->collected_value - rd->last_collected_value)
1137 * (calculated_number)rd->multiplier
1138 / (calculated_number)rd->divisor;
1140 if(unlikely(st->debug))
1141 debug(D_RRD_STATS, "%s/%s: CALC INC PRE "
1142 CALCULATED_NUMBER_FORMAT " = ("
1143 COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT
1145 " * " CALCULATED_NUMBER_FORMAT
1146 " / " CALCULATED_NUMBER_FORMAT
1148 , rd->calculated_value
1149 , rd->collected_value, rd->last_collected_value
1150 , (calculated_number)rd->multiplier
1151 , (calculated_number)rd->divisor
1155 case RRDDIM_PCENT_OVER_DIFF_TOTAL:
1156 if(unlikely(rd->counter <= 1)) {
1157 rd->calculated_value = 0;
1161 // if the new is smaller than the old (an overflow, or reset), set the old equal to the new
1162 // to reset the calculation (it will give zero as the calculation for this second)
1163 if(unlikely(rd->last_collected_value > rd->collected_value)) {
1164 debug(D_RRD_STATS, "%s.%s: RESET or OVERFLOW. Last collected value = " COLLECTED_NUMBER_FORMAT ", current = " COLLECTED_NUMBER_FORMAT
1165 , st->name, rd->name
1166 , rd->last_collected_value
1167 , rd->collected_value);
1168 if(!(rd->flags & RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS)) storage_flags = SN_EXISTS_RESET;
1169 rd->last_collected_value = rd->collected_value;
1172 // the percentage of the current increment
1173 // over the increment of all dimensions together
1174 if(unlikely(st->collected_total == st->last_collected_total))
1175 rd->calculated_value = 0;
1177 rd->calculated_value =
1178 (calculated_number)100
1179 * (calculated_number)(rd->collected_value - rd->last_collected_value)
1180 / (calculated_number)(st->collected_total - st->last_collected_total);
1182 if(unlikely(st->debug))
1183 debug(D_RRD_STATS, "%s/%s: CALC PCENT-DIFF "
1184 CALCULATED_NUMBER_FORMAT " = 100"
1185 " * (" COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT ")"
1186 " / (" COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT ")"
1188 , rd->calculated_value
1189 , rd->collected_value, rd->last_collected_value
1190 , st->collected_total, st->last_collected_total
1195 // make the default zero, to make sure
1196 // it gets noticed when we add new types
1197 rd->calculated_value = 0;
1199 if(unlikely(st->debug))
1200 debug(D_RRD_STATS, "%s/%s: CALC "
1201 CALCULATED_NUMBER_FORMAT " = 0"
1203 , rd->calculated_value
1208 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s/%s: PHASE2 "
1209 " last_collected_value = " COLLECTED_NUMBER_FORMAT
1210 " collected_value = " COLLECTED_NUMBER_FORMAT
1211 " last_calculated_value = " CALCULATED_NUMBER_FORMAT
1212 " calculated_value = " CALCULATED_NUMBER_FORMAT
1214 , rd->last_collected_value
1215 , rd->collected_value
1216 , rd->last_calculated_value
1217 , rd->calculated_value
1222 // at this point we have all the calculated values ready
1223 // it is now time to interpolate values on a second boundary
1225 unsigned long long first_ut = last_ut;
1226 long long iterations = (now_ut - last_ut) / (st->update_every * 1000000ULL);
1227 if((now_ut % (st->update_every * 1000000ULL)) == 0) iterations++;
1229 for( ; likely(next_ut <= now_ut) ; next_ut += st->update_every * 1000000ULL, iterations-- ) {
1230 #ifdef NETDATA_INTERNAL_CHECKS
1231 if(iterations < 0) { error("%s: iterations calculation wrapped! first_ut = %llu, last_ut = %llu, next_ut = %llu, now_ut = %llu", st->name, first_ut, last_ut, next_ut, now_ut); }
1234 if(unlikely(st->debug)) {
1235 debug(D_RRD_STATS, "%s: last ut = %0.3Lf (last updated time)", st->name, (long double)last_ut/1000000.0);
1236 debug(D_RRD_STATS, "%s: next ut = %0.3Lf (next interpolation point)", st->name, (long double)next_ut/1000000.0);
1239 st->last_updated.tv_sec = (time_t) (next_ut / 1000000ULL);
1240 st->last_updated.tv_usec = 0;
1242 for( rd = st->dimensions ; likely(rd) ; rd = rd->next ) {
1243 calculated_number new_value;
1245 switch(rd->algorithm) {
1246 case RRDDIM_INCREMENTAL:
1247 new_value = (calculated_number)
1248 ( rd->calculated_value
1249 * (calculated_number)(next_ut - last_ut)
1250 / (calculated_number)(now_ut - last_ut)
1253 if(unlikely(st->debug))
1254 debug(D_RRD_STATS, "%s/%s: CALC2 INC "
1255 CALCULATED_NUMBER_FORMAT " = "
1256 CALCULATED_NUMBER_FORMAT
1261 , rd->calculated_value
1262 , (next_ut - last_ut)
1263 , (now_ut - last_ut)
1266 rd->calculated_value -= new_value;
1267 new_value += rd->last_calculated_value;
1268 rd->last_calculated_value = 0;
1269 new_value /= (calculated_number)st->update_every;
1272 case RRDDIM_ABSOLUTE:
1273 case RRDDIM_PCENT_OVER_ROW_TOTAL:
1274 case RRDDIM_PCENT_OVER_DIFF_TOTAL:
1276 if(iterations == 1) {
1277 // this is the last iteration
1278 // do not interpolate
1279 // just show the calculated value
1281 new_value = rd->calculated_value;
1284 // we have missed an update
1285 // interpolate in the middle values
1287 new_value = (calculated_number)
1288 ( ( (rd->calculated_value - rd->last_calculated_value)
1289 * (calculated_number)(next_ut - first_ut)
1290 / (calculated_number)(now_ut - first_ut)
1292 + rd->last_calculated_value
1295 if(unlikely(st->debug))
1296 debug(D_RRD_STATS, "%s/%s: CALC2 DEF "
1297 CALCULATED_NUMBER_FORMAT " = ((("
1298 "(" CALCULATED_NUMBER_FORMAT " - " CALCULATED_NUMBER_FORMAT ")"
1300 " / %llu) + " CALCULATED_NUMBER_FORMAT
1303 , rd->calculated_value, rd->last_calculated_value
1304 , (next_ut - first_ut)
1305 , (now_ut - first_ut), rd->last_calculated_value
1309 // it fades the value towards the target
1310 // while we know the calculated value is different
1311 // if(likely(next_ut + st->update_every * 1000000ULL > now_ut)) rd->calculated_value = new_value;
1316 if(unlikely(!store_this_entry)) {
1317 store_this_entry = 1;
1321 if(likely(rd->updated && rd->counter > 1 && iterations < st->gap_when_lost_iterations_above)) {
1322 rd->values[st->current_entry] = pack_storage_number(new_value, storage_flags );
1324 if(unlikely(st->debug))
1325 debug(D_RRD_STATS, "%s/%s: STORE[%ld] "
1326 CALCULATED_NUMBER_FORMAT " = " CALCULATED_NUMBER_FORMAT
1329 , unpack_storage_number(rd->values[st->current_entry]), new_value
1333 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s/%s: STORE[%ld] = NON EXISTING "
1337 rd->values[st->current_entry] = pack_storage_number(0, SN_NOT_EXISTS);
1342 if(unlikely(st->debug)) {
1343 calculated_number t1 = new_value * (calculated_number)rd->multiplier / (calculated_number)rd->divisor;
1344 calculated_number t2 = unpack_storage_number(rd->values[st->current_entry]);
1345 calculated_number accuracy = accuracy_loss(t1, t2);
1346 debug(D_RRD_STATS, "%s/%s: UNPACK[%ld] = " CALCULATED_NUMBER_FORMAT " FLAGS=0x%08x (original = " CALCULATED_NUMBER_FORMAT ", accuracy loss = " CALCULATED_NUMBER_FORMAT "%%%s)"
1350 , get_storage_number_flags(rd->values[st->current_entry])
1353 , (accuracy > ACCURACY_LOSS) ? " **TOO BIG** " : ""
1356 rd->collected_volume += t1;
1357 rd->stored_volume += t2;
1358 accuracy = accuracy_loss(rd->collected_volume, rd->stored_volume);
1359 debug(D_RRD_STATS, "%s/%s: VOLUME[%ld] = " CALCULATED_NUMBER_FORMAT ", calculated = " CALCULATED_NUMBER_FORMAT ", accuracy loss = " CALCULATED_NUMBER_FORMAT "%%%s"
1363 , rd->collected_volume
1365 , (accuracy > ACCURACY_LOSS) ? " **TOO BIG** " : ""
1370 // reset the storage flags for the next point, if any;
1371 storage_flags = SN_EXISTS;
1374 st->current_entry = ((st->current_entry + 1) >= st->entries) ? 0 : st->current_entry + 1;
1378 // align next interpolation to last collection point
1379 if(likely(stored_entries || !store_this_entry)) {
1380 st->last_updated.tv_sec = st->last_collected_time.tv_sec;
1381 st->last_updated.tv_usec = st->last_collected_time.tv_usec;
1382 st->last_collected_total = st->collected_total;
1385 for( rd = st->dimensions; likely(rd) ; rd = rd->next ) {
1386 if(unlikely(!rd->updated)) continue;
1388 if(likely(stored_entries || !store_this_entry)) {
1389 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s/%s: setting last_collected_value (old: " COLLECTED_NUMBER_FORMAT ") to last_collected_value (new: " COLLECTED_NUMBER_FORMAT ")", st->id, rd->name, rd->last_collected_value, rd->collected_value);
1390 rd->last_collected_value = rd->collected_value;
1392 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s/%s: setting last_calculated_value (old: " CALCULATED_NUMBER_FORMAT ") to last_calculated_value (new: " CALCULATED_NUMBER_FORMAT ")", st->id, rd->name, rd->last_calculated_value, rd->calculated_value);
1393 rd->last_calculated_value = rd->calculated_value;
1396 rd->calculated_value = 0;
1397 rd->collected_value = 0;
1400 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s/%s: END "
1401 " last_collected_value = " COLLECTED_NUMBER_FORMAT
1402 " collected_value = " COLLECTED_NUMBER_FORMAT
1403 " last_calculated_value = " CALCULATED_NUMBER_FORMAT
1404 " calculated_value = " CALCULATED_NUMBER_FORMAT
1406 , rd->last_collected_value
1407 , rd->collected_value
1408 , rd->last_calculated_value
1409 , rd->calculated_value
1413 // ALL DONE ABOUT THE DATA UPDATE
1414 // --------------------------------------------------------------------
1416 // find if there are any obsolete dimensions (not updated recently)
1417 if(unlikely(rrd_delete_unupdated_dimensions)) {
1419 for( rd = st->dimensions; likely(rd) ; rd = rd->next )
1420 if((rd->last_collected_time.tv_sec + (rrd_delete_unupdated_dimensions * st->update_every)) < st->last_collected_time.tv_sec)
1424 // there is dimension to free
1425 // upgrade our read lock to a write lock
1426 pthread_rwlock_unlock(&st->rwlock);
1427 pthread_rwlock_wrlock(&st->rwlock);
1429 for( rd = st->dimensions, last = NULL ; likely(rd) ; ) {
1430 // remove it only it is not updated in rrd_delete_unupdated_dimensions seconds
1432 if(unlikely((rd->last_collected_time.tv_sec + (rrd_delete_unupdated_dimensions * st->update_every)) < st->last_collected_time.tv_sec)) {
1433 info("Removing obsolete dimension '%s' (%s) of '%s' (%s).", rd->name, rd->id, st->name, st->id);
1435 if(unlikely(!last)) {
1436 st->dimensions = rd->next;
1438 rrddim_free(st, rd);
1439 rd = st->dimensions;
1443 last->next = rd->next;
1445 rrddim_free(st, rd);
1455 if(unlikely(!st->dimensions)) {
1456 info("Disabling chart %s (%s) since it does not have any dimensions", st->name, st->id);
1462 pthread_rwlock_unlock(&st->rwlock);
1464 if(unlikely(pthread_setcancelstate(oldstate, NULL) != 0))
1465 error("Cannot set pthread cancel state to RESTORE (%d).", oldstate);
1467 return(st->usec_since_last_update);