14 #include <sys/types.h>
19 #include "appconfig.h"
24 #define RRD_DEFAULT_GAP_INTERPOLATIONS 1
26 // ----------------------------------------------------------------------------
29 // if not zero it gives the time (in seconds) to remove un-updated dimensions
31 // if dimensions are removed, the chart generation will have to run again
32 int rrd_delete_unupdated_dimensions = 0;
34 int rrd_update_every = UPDATE_EVERY;
35 int rrd_default_history_entries = RRD_DEFAULT_HISTORY_ENTRIES;
36 int rrd_memory_mode = RRD_MEMORY_MODE_SAVE;
38 static int rrdset_compare(void* a, void* b);
39 static int rrdset_compare_name(void* a, void* b);
40 static int rrdcontext_compare(void* a, void* b);
43 .hostname = "localhost",
45 .rrdset_root_rwlock = PTHREAD_RWLOCK_INITIALIZER,
46 .rrdset_root_index = {
47 { NULL, rrdset_compare },
50 .rrdset_root_index_name = {
51 { NULL, rrdset_compare_name },
54 .rrdcontext_root_index = {
55 { NULL, rrdcontext_compare },
60 // ----------------------------------------------------------------------------
63 static int rrdcontext_compare(void* a, void* b) {
64 if(((RRDCONTEXT *)a)->hash < ((RRDCONTEXT *)b)->hash) return -1;
65 else if(((RRDCONTEXT *)a)->hash > ((RRDCONTEXT *)b)->hash) return 1;
66 else return strcmp(((RRDCONTEXT *)a)->id, ((RRDCONTEXT *)b)->id);
69 #define rrdcontext_index_add(host, rc) (RRDCONTEXT *)avl_insert_lock(&((host)->rrdcontext_root_index), (avl *)(rc))
70 #define rrdcontext_index_del(host, rc) (RRDCONTEXT *)avl_remove_lock(&((host)->rrdcontext_root_index), (avl *)(rc))
72 static RRDCONTEXT *rrdcontext_index_find(RRDHOST *host, const char *id, uint32_t hash) {
75 tmp.hash = (hash)?hash:simple_hash(tmp.id);
77 return (RRDCONTEXT *)avl_search_lock(&(host->rrdcontext_root_index), (avl *) &tmp);
80 RRDCONTEXT *rrdcontext_create(const char *id) {
81 RRDCONTEXT *rc = rrdcontext_index_find(&localhost, id, 0);
83 rc = calloc(1, sizeof(RRDCONTEXT));
84 if(!rc) fatal("Cannot allocate RRDCONTEXT memory");
86 if(!rc->id) fatal("Cannot allocate RRDCONTEXT.id memory");
87 rc->hash = simple_hash(rc->id);
88 // avl_init_lock(&rc->variables_root_index, compar);
89 RRDCONTEXT *ret = rrdcontext_index_add(&localhost, rc);
91 fatal("INTERNAL ERROR: Expected to INSERT RRDCONTEXT '%s' into index, but inserted '%s'.", rc->id, (ret)?ret->id:"NONE");
98 void rrdcontext_free(RRDCONTEXT *rc) {
101 RRDCONTEXT *ret = rrdcontext_index_del(&localhost, rc);
103 fatal("INTERNAL ERROR: Expected to DELETE RRDCONTEXT '%s' from index, but deleted '%s'.", rc->id, (ret)?ret->id:"NONE");
105 free((void *)rc->id);
110 // ----------------------------------------------------------------------------
113 static int rrdset_compare(void* a, void* b) {
114 if(((RRDSET *)a)->hash < ((RRDSET *)b)->hash) return -1;
115 else if(((RRDSET *)a)->hash > ((RRDSET *)b)->hash) return 1;
116 else return strcmp(((RRDSET *)a)->id, ((RRDSET *)b)->id);
119 #define rrdset_index_add(host, st) (RRDSET *)avl_insert_lock(&((host)->rrdset_root_index), (avl *)(st))
120 #define rrdset_index_del(host, st) (RRDSET *)avl_remove_lock(&((host)->rrdset_root_index), (avl *)(st))
122 static RRDSET *rrdset_index_find(RRDHOST *host, const char *id, uint32_t hash) {
124 strncpyz(tmp.id, id, RRD_ID_LENGTH_MAX);
125 tmp.hash = (hash)?hash:simple_hash(tmp.id);
127 return (RRDSET *)avl_search_lock(&(host->rrdset_root_index), (avl *) &tmp);
130 // ----------------------------------------------------------------------------
133 #define rrdset_from_avlname(avlname_ptr) ((RRDSET *)((avlname_ptr) - offsetof(RRDSET, avlname)))
135 static int rrdset_compare_name(void* a, void* b) {
136 RRDSET *A = rrdset_from_avlname(a);
137 RRDSET *B = rrdset_from_avlname(b);
139 // fprintf(stderr, "COMPARING: %s with %s\n", A->name, B->name);
141 if(A->hash_name < B->hash_name) return -1;
142 else if(A->hash_name > B->hash_name) return 1;
143 else return strcmp(A->name, B->name);
146 RRDSET *rrdset_index_add_name(RRDHOST *host, RRDSET *st) {
148 // fprintf(stderr, "ADDING: %s (name: %s)\n", st->id, st->name);
149 result = avl_insert_lock(&host->rrdset_root_index_name, (avl *) (&st->avlname));
150 if(result) return rrdset_from_avlname(result);
154 RRDSET *rrdset_index_del_name(RRDHOST *host, RRDSET *st) {
156 // fprintf(stderr, "DELETING: %s (name: %s)\n", st->id, st->name);
157 return (RRDSET *)avl_remove_lock(&((host)->rrdset_root_index_name), (avl *)(&st->avlname));
158 if(result) return rrdset_from_avlname(result);
162 static RRDSET *rrdset_index_find_name(RRDHOST *host, const char *name, uint32_t hash) {
166 tmp.hash_name = (hash)?hash:simple_hash(tmp.name);
168 // fprintf(stderr, "SEARCHING: %s\n", name);
169 result = avl_search_lock(&host->rrdset_root_index_name, (avl *) (&(tmp.avlname)));
171 RRDSET *st = rrdset_from_avlname(result);
172 if(strcmp(st->magic, RRDSET_MAGIC))
173 error("Search for RRDSET %s returned an invalid RRDSET %s (name %s)", name, st->id, st->name);
175 // fprintf(stderr, "FOUND: %s\n", name);
176 return rrdset_from_avlname(result);
178 // fprintf(stderr, "NOT FOUND: %s\n", name);
183 // ----------------------------------------------------------------------------
186 static int rrddim_compare(void* a, void* b) {
187 if(((RRDDIM *)a)->hash < ((RRDDIM *)b)->hash) return -1;
188 else if(((RRDDIM *)a)->hash > ((RRDDIM *)b)->hash) return 1;
189 else return strcmp(((RRDDIM *)a)->id, ((RRDDIM *)b)->id);
192 #define rrddim_index_add(st, rd) avl_insert_lock(&((st)->dimensions_index), (avl *)(rd))
193 #define rrddim_index_del(st,rd ) avl_remove_lock(&((st)->dimensions_index), (avl *)(rd))
195 static RRDDIM *rrddim_index_find(RRDSET *st, const char *id, uint32_t hash) {
197 strncpyz(tmp.id, id, RRD_ID_LENGTH_MAX);
198 tmp.hash = (hash)?hash:simple_hash(tmp.id);
200 return (RRDDIM *)avl_search_lock(&(st->dimensions_index), (avl *) &tmp);
203 // ----------------------------------------------------------------------------
206 int rrdset_type_id(const char *name)
208 if(unlikely(strcmp(name, RRDSET_TYPE_AREA_NAME) == 0)) return RRDSET_TYPE_AREA;
209 else if(unlikely(strcmp(name, RRDSET_TYPE_STACKED_NAME) == 0)) return RRDSET_TYPE_STACKED;
210 else if(unlikely(strcmp(name, RRDSET_TYPE_LINE_NAME) == 0)) return RRDSET_TYPE_LINE;
211 return RRDSET_TYPE_LINE;
214 const char *rrdset_type_name(int chart_type)
216 static char line[] = RRDSET_TYPE_LINE_NAME;
217 static char area[] = RRDSET_TYPE_AREA_NAME;
218 static char stacked[] = RRDSET_TYPE_STACKED_NAME;
221 case RRDSET_TYPE_LINE:
224 case RRDSET_TYPE_AREA:
227 case RRDSET_TYPE_STACKED:
233 // ----------------------------------------------------------------------------
236 const char *rrd_memory_mode_name(int id)
238 static const char ram[] = RRD_MEMORY_MODE_RAM_NAME;
239 static const char map[] = RRD_MEMORY_MODE_MAP_NAME;
240 static const char save[] = RRD_MEMORY_MODE_SAVE_NAME;
243 case RRD_MEMORY_MODE_RAM:
246 case RRD_MEMORY_MODE_MAP:
249 case RRD_MEMORY_MODE_SAVE:
257 int rrd_memory_mode_id(const char *name)
259 if(unlikely(!strcmp(name, RRD_MEMORY_MODE_RAM_NAME)))
260 return RRD_MEMORY_MODE_RAM;
261 else if(unlikely(!strcmp(name, RRD_MEMORY_MODE_MAP_NAME)))
262 return RRD_MEMORY_MODE_MAP;
264 return RRD_MEMORY_MODE_SAVE;
267 // ----------------------------------------------------------------------------
270 int rrddim_algorithm_id(const char *name)
272 if(strcmp(name, RRDDIM_INCREMENTAL_NAME) == 0) return RRDDIM_INCREMENTAL;
273 if(strcmp(name, RRDDIM_ABSOLUTE_NAME) == 0) return RRDDIM_ABSOLUTE;
274 if(strcmp(name, RRDDIM_PCENT_OVER_ROW_TOTAL_NAME) == 0) return RRDDIM_PCENT_OVER_ROW_TOTAL;
275 if(strcmp(name, RRDDIM_PCENT_OVER_DIFF_TOTAL_NAME) == 0) return RRDDIM_PCENT_OVER_DIFF_TOTAL;
276 return RRDDIM_ABSOLUTE;
279 const char *rrddim_algorithm_name(int chart_type)
281 static char absolute[] = RRDDIM_ABSOLUTE_NAME;
282 static char incremental[] = RRDDIM_INCREMENTAL_NAME;
283 static char percentage_of_absolute_row[] = RRDDIM_PCENT_OVER_ROW_TOTAL_NAME;
284 static char percentage_of_incremental_row[] = RRDDIM_PCENT_OVER_DIFF_TOTAL_NAME;
287 case RRDDIM_ABSOLUTE:
290 case RRDDIM_INCREMENTAL:
293 case RRDDIM_PCENT_OVER_ROW_TOTAL:
294 return percentage_of_absolute_row;
296 case RRDDIM_PCENT_OVER_DIFF_TOTAL:
297 return percentage_of_incremental_row;
302 // ----------------------------------------------------------------------------
305 char *rrdset_strncpyz_name(char *to, const char *from, size_t length)
309 while (length-- && (c = *from++)) {
310 if(c != '.' && !isalnum(c))
321 void rrdset_set_name(RRDSET *st, const char *name)
323 debug(D_RRD_CALLS, "rrdset_set_name() old: %s, new: %s", st->name, name);
325 if(st->name) rrdset_index_del_name(&localhost, st);
327 char b[CONFIG_MAX_VALUE + 1];
328 char n[RRD_ID_LENGTH_MAX + 1];
330 snprintfz(n, RRD_ID_LENGTH_MAX, "%s.%s", st->type, name);
331 rrdset_strncpyz_name(b, n, CONFIG_MAX_VALUE);
332 st->name = config_get(st->id, "name", b);
333 st->hash_name = simple_hash(st->name);
335 rrdset_index_add_name(&localhost, st);
338 // ----------------------------------------------------------------------------
341 char *rrdset_cache_dir(const char *id)
345 static char *cache_dir = NULL;
347 cache_dir = config_get("global", "cache directory", CACHE_DIR);
348 int r = mkdir(cache_dir, 0755);
349 if(r != 0 && errno != EEXIST)
350 error("Cannot create directory '%s'", cache_dir);
353 char b[FILENAME_MAX + 1];
354 char n[FILENAME_MAX + 1];
355 rrdset_strncpyz_name(b, id, FILENAME_MAX);
357 snprintfz(n, FILENAME_MAX, "%s/%s", cache_dir, b);
358 ret = config_get(id, "cache directory", n);
360 if(rrd_memory_mode == RRD_MEMORY_MODE_MAP || rrd_memory_mode == RRD_MEMORY_MODE_SAVE) {
361 int r = mkdir(ret, 0775);
362 if(r != 0 && errno != EEXIST)
363 error("Cannot create directory '%s'", ret);
369 // ----------------------------------------------------------------------------
372 void rrdset_reset(RRDSET *st)
374 debug(D_RRD_CALLS, "rrdset_reset() %s", st->name);
376 st->last_collected_time.tv_sec = 0;
377 st->last_collected_time.tv_usec = 0;
378 st->last_updated.tv_sec = 0;
379 st->last_updated.tv_usec = 0;
380 st->current_entry = 0;
382 st->counter_done = 0;
385 for(rd = st->dimensions; rd ; rd = rd->next) {
386 rd->last_collected_time.tv_sec = 0;
387 rd->last_collected_time.tv_usec = 0;
389 bzero(rd->values, rd->entries * sizeof(storage_number));
393 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)
395 if(!type || !type[0]) {
396 fatal("Cannot create rrd stats without a type.");
401 fatal("Cannot create rrd stats without an id.");
405 char fullid[RRD_ID_LENGTH_MAX + 1];
406 char fullfilename[FILENAME_MAX + 1];
409 snprintfz(fullid, RRD_ID_LENGTH_MAX, "%s.%s", type, id);
411 st = rrdset_find(fullid);
413 error("Cannot create rrd stats for '%s', it already exists.", fullid);
417 long entries = config_get_number(fullid, "history", rrd_default_history_entries);
418 if(entries < 5) entries = config_set_number(fullid, "history", 5);
419 if(entries > RRD_HISTORY_ENTRIES_MAX) entries = config_set_number(fullid, "history", RRD_HISTORY_ENTRIES_MAX);
421 int enabled = config_get_boolean(fullid, "enabled", 1);
422 if(!enabled) entries = 5;
424 unsigned long size = sizeof(RRDSET);
425 char *cache_dir = rrdset_cache_dir(fullid);
427 debug(D_RRD_CALLS, "Creating RRD_STATS for '%s.%s'.", type, id);
429 snprintfz(fullfilename, FILENAME_MAX, "%s/main.db", cache_dir);
430 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);
432 if(strcmp(st->magic, RRDSET_MAGIC) != 0) {
434 info("Initializing file %s.", fullfilename);
437 else if(strcmp(st->id, fullid) != 0) {
439 error("File %s contents are not for chart %s. Clearing it.", fullfilename, fullid);
444 else if(st->memsize != size || st->entries != entries) {
446 error("File %s does not have the desired size. Clearing it.", fullfilename);
449 else if(st->update_every != update_every) {
451 error("File %s does not have the desired update frequency. Clearing it.", fullfilename);
454 else if((time(NULL) - st->last_updated.tv_sec) > update_every * entries) {
456 error("File %s is too old. Clearing it.", fullfilename);
468 st->dimensions = NULL;
470 st->mapped = rrd_memory_mode;
473 st = calloc(1, size);
475 fatal("Cannot allocate memory for RRD_STATS %s.%s", type, id);
478 st->mapped = RRD_MEMORY_MODE_RAM;
481 st->entries = entries;
482 st->update_every = update_every;
484 if(st->current_entry >= st->entries) st->current_entry = 0;
486 strcpy(st->cache_filename, fullfilename);
487 strcpy(st->magic, RRDSET_MAGIC);
489 strcpy(st->id, fullid);
490 st->hash = simple_hash(st->id);
492 st->cache_dir = cache_dir;
494 st->chart_type = rrdset_type_id(config_get(st->id, "chart type", rrdset_type_name(chart_type)));
495 st->type = config_get(st->id, "type", type);
496 st->family = config_get(st->id, "family", family?family:st->type);
497 st->context = config_get(st->id, "context", context?context:st->id);
498 st->units = config_get(st->id, "units", units?units:"");
500 st->priority = config_get_number(st->id, "priority", priority);
501 st->enabled = enabled;
506 st->last_collected_time.tv_sec = 0;
507 st->last_collected_time.tv_usec = 0;
508 st->counter_done = 0;
510 st->gap_when_lost_iterations_above = (int) (
511 config_get_number(st->id, "gap when lost iterations above", RRD_DEFAULT_GAP_INTERPOLATIONS) + 2);
513 avl_init_lock(&st->dimensions_index, rrddim_compare);
515 pthread_rwlock_init(&st->rwlock, NULL);
516 pthread_rwlock_wrlock(&localhost.rrdset_root_rwlock);
518 if(name && *name) rrdset_set_name(st, name);
519 else rrdset_set_name(st, id);
522 char varvalue[CONFIG_MAX_VALUE + 1];
523 snprintfz(varvalue, CONFIG_MAX_VALUE, "%s (%s)", title?title:"", st->name);
524 st->title = config_get(st->id, "title", varvalue);
527 st->next = localhost.rrdset_root;
528 localhost.rrdset_root = st;
530 rrdset_index_add(&localhost, st);
532 st->rrdcontext = rrdcontext_create(st->context);
534 pthread_rwlock_unlock(&localhost.rrdset_root_rwlock);
539 RRDDIM *rrddim_add(RRDSET *st, const char *id, const char *name, long multiplier, long divisor, int algorithm)
541 char filename[FILENAME_MAX + 1];
542 char fullfilename[FILENAME_MAX + 1];
544 char varname[CONFIG_MAX_NAME + 1];
546 unsigned long size = sizeof(RRDDIM) + (st->entries * sizeof(storage_number));
548 debug(D_RRD_CALLS, "Adding dimension '%s/%s'.", st->id, id);
550 rrdset_strncpyz_name(filename, id, FILENAME_MAX);
551 snprintfz(fullfilename, FILENAME_MAX, "%s/%s.db", st->cache_dir, filename);
552 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);
555 gettimeofday(&now, NULL);
557 if(strcmp(rd->magic, RRDDIMENSION_MAGIC) != 0) {
559 info("Initializing file %s.", fullfilename);
562 else if(rd->memsize != size) {
564 error("File %s does not have the desired size. Clearing it.", fullfilename);
567 else if(rd->multiplier != multiplier) {
569 error("File %s does not have the same multiplier. Clearing it.", fullfilename);
572 else if(rd->divisor != divisor) {
574 error("File %s does not have the same divisor. Clearing it.", fullfilename);
577 else if(rd->algorithm != algorithm) {
579 error("File %s does not have the same algorithm. Clearing it.", fullfilename);
582 else if(rd->update_every != st->update_every) {
584 error("File %s does not have the same refresh frequency. Clearing it.", fullfilename);
587 else if(usecdiff(&now, &rd->last_collected_time) > (rd->entries * rd->update_every * 1000000ULL)) {
589 error("File %s is too old. Clearing it.", fullfilename);
592 else if(strcmp(rd->id, id) != 0) {
594 error("File %s contents are not for dimension %s. Clearing it.", fullfilename, id);
602 // we have a file mapped for rd
603 rd->mapped = rrd_memory_mode;
604 rd->flags = 0x00000000;
609 // if we didn't manage to get a mmap'd dimension, just create one
611 rd = calloc(1, size);
613 fatal("Cannot allocate RRD_DIMENSION %s/%s.", st->id, id);
617 rd->mapped = RRD_MEMORY_MODE_RAM;
621 strcpy(rd->magic, RRDDIMENSION_MAGIC);
622 strcpy(rd->cache_filename, fullfilename);
623 strncpyz(rd->id, id, RRD_ID_LENGTH_MAX);
624 rd->hash = simple_hash(rd->id);
626 snprintfz(varname, CONFIG_MAX_NAME, "dim %s name", rd->id);
627 rd->name = config_get(st->id, varname, (name && *name)?name:rd->id);
629 snprintfz(varname, CONFIG_MAX_NAME, "dim %s algorithm", rd->id);
630 rd->algorithm = rrddim_algorithm_id(config_get(st->id, varname, rrddim_algorithm_name(algorithm)));
632 snprintfz(varname, CONFIG_MAX_NAME, "dim %s multiplier", rd->id);
633 rd->multiplier = config_get_number(st->id, varname, multiplier);
635 snprintfz(varname, CONFIG_MAX_NAME, "dim %s divisor", rd->id);
636 rd->divisor = config_get_number(st->id, varname, divisor);
637 if(!rd->divisor) rd->divisor = 1;
639 rd->entries = st->entries;
640 rd->update_every = st->update_every;
642 // prevent incremental calculation spikes
645 rd->calculated_value = 0;
646 rd->last_calculated_value = 0;
647 rd->collected_value = 0;
648 rd->last_collected_value = 0;
649 rd->collected_volume = 0;
650 rd->stored_volume = 0;
651 rd->values[st->current_entry] = pack_storage_number(0, SN_NOT_EXISTS);
652 rd->last_collected_time.tv_sec = 0;
653 rd->last_collected_time.tv_usec = 0;
655 // append this dimension
656 pthread_rwlock_wrlock(&st->rwlock);
660 RRDDIM *td = st->dimensions;
661 for(; td->next; td = td->next) ;
664 pthread_rwlock_unlock(&st->rwlock);
666 rrddim_index_add(st, rd);
671 void rrddim_set_name(RRDSET *st, RRDDIM *rd, const char *name)
673 debug(D_RRD_CALLS, "rrddim_set_name() %s.%s", st->name, rd->name);
675 char varname[CONFIG_MAX_NAME + 1];
676 snprintfz(varname, CONFIG_MAX_NAME, "dim %s name", rd->id);
677 config_set_default(st->id, varname, name);
680 void rrddim_free(RRDSET *st, RRDDIM *rd)
682 debug(D_RRD_CALLS, "rrddim_free() %s.%s", st->name, rd->name);
684 RRDDIM *i, *last = NULL;
685 for(i = st->dimensions; i && i != rd ; i = i->next) last = i;
688 error("Request to free dimension %s.%s but it is not linked.", st->id, rd->name);
692 if(last) last->next = rd->next;
693 else st->dimensions = rd->next;
696 rrddim_index_del(st, rd);
698 // free(rd->annotations);
699 if(rd->mapped == RRD_MEMORY_MODE_SAVE) {
700 debug(D_RRD_CALLS, "Saving dimension '%s' to '%s'.", rd->name, rd->cache_filename);
701 savememory(rd->cache_filename, rd, rd->memsize);
703 debug(D_RRD_CALLS, "Unmapping dimension '%s'.", rd->name);
704 munmap(rd, rd->memsize);
706 else if(rd->mapped == RRD_MEMORY_MODE_MAP) {
707 debug(D_RRD_CALLS, "Unmapping dimension '%s'.", rd->name);
708 munmap(rd, rd->memsize);
711 debug(D_RRD_CALLS, "Removing dimension '%s'.", rd->name);
716 void rrdset_free_all(void)
718 info("Freeing all memory...");
721 for(st = localhost.rrdset_root; st ;) {
722 RRDSET *next = st->next;
724 while(st->dimensions)
725 rrddim_free(st, st->dimensions);
727 rrdset_index_del(&localhost, st);
729 st->rrdcontext->use_count--;
730 if(!st->rrdcontext->use_count)
731 rrdcontext_free(st->rrdcontext);
733 if(st->mapped == RRD_MEMORY_MODE_SAVE) {
734 debug(D_RRD_CALLS, "Saving stats '%s' to '%s'.", st->name, st->cache_filename);
735 savememory(st->cache_filename, st, st->memsize);
737 debug(D_RRD_CALLS, "Unmapping stats '%s'.", st->name);
738 munmap(st, st->memsize);
740 else if(st->mapped == RRD_MEMORY_MODE_MAP) {
741 debug(D_RRD_CALLS, "Unmapping stats '%s'.", st->name);
742 munmap(st, st->memsize);
749 localhost.rrdset_root = NULL;
751 info("Memory cleanup completed...");
754 void rrdset_save_all(void) {
755 info("Saving database...");
760 pthread_rwlock_wrlock(&localhost.rrdset_root_rwlock);
761 for(st = localhost.rrdset_root; st ; st = st->next) {
762 pthread_rwlock_wrlock(&st->rwlock);
764 if(st->mapped == RRD_MEMORY_MODE_SAVE) {
765 debug(D_RRD_CALLS, "Saving stats '%s' to '%s'.", st->name, st->cache_filename);
766 savememory(st->cache_filename, st, st->memsize);
769 for(rd = st->dimensions; rd ; rd = rd->next) {
770 if(likely(rd->mapped == RRD_MEMORY_MODE_SAVE)) {
771 debug(D_RRD_CALLS, "Saving dimension '%s' to '%s'.", rd->name, rd->cache_filename);
772 savememory(rd->cache_filename, rd, rd->memsize);
776 pthread_rwlock_unlock(&st->rwlock);
778 pthread_rwlock_unlock(&localhost.rrdset_root_rwlock);
782 RRDSET *rrdset_find(const char *id)
784 debug(D_RRD_CALLS, "rrdset_find() for chart %s", id);
786 RRDSET *st = rrdset_index_find(&localhost, id, 0);
790 RRDSET *rrdset_find_bytype(const char *type, const char *id)
792 debug(D_RRD_CALLS, "rrdset_find_bytype() for chart %s.%s", type, id);
794 char buf[RRD_ID_LENGTH_MAX + 1];
796 strncpyz(buf, type, RRD_ID_LENGTH_MAX - 1);
798 int len = (int) strlen(buf);
799 strncpyz(&buf[len], id, (size_t) (RRD_ID_LENGTH_MAX - len));
801 return(rrdset_find(buf));
804 RRDSET *rrdset_find_byname(const char *name)
806 debug(D_RRD_CALLS, "rrdset_find_byname() for chart %s", name);
808 RRDSET *st = rrdset_index_find_name(&localhost, name, 0);
812 RRDDIM *rrddim_find(RRDSET *st, const char *id)
814 debug(D_RRD_CALLS, "rrddim_find() for chart %s, dimension %s", st->name, id);
816 return rrddim_index_find(st, id, 0);
819 int rrddim_hide(RRDSET *st, const char *id)
821 debug(D_RRD_CALLS, "rrddim_hide() for chart %s, dimension %s", st->name, id);
823 RRDDIM *rd = rrddim_find(st, id);
825 error("Cannot find dimension with id '%s' on stats '%s' (%s).", id, st->name, st->id);
829 rd->flags |= RRDDIM_FLAG_HIDDEN;
833 int rrddim_unhide(RRDSET *st, const char *id)
835 debug(D_RRD_CALLS, "rrddim_unhide() for chart %s, dimension %s", st->name, id);
837 RRDDIM *rd = rrddim_find(st, id);
839 error("Cannot find dimension with id '%s' on stats '%s' (%s).", id, st->name, st->id);
843 if(rd->flags & RRDDIM_FLAG_HIDDEN) rd->flags ^= RRDDIM_FLAG_HIDDEN;
847 collected_number rrddim_set_by_pointer(RRDSET *st, RRDDIM *rd, collected_number value)
849 debug(D_RRD_CALLS, "rrddim_set_by_pointer() for chart %s, dimension %s, value " COLLECTED_NUMBER_FORMAT, st->name, rd->name, value);
851 gettimeofday(&rd->last_collected_time, NULL);
852 rd->collected_value = value;
856 return rd->last_collected_value;
859 collected_number rrddim_set(RRDSET *st, const char *id, collected_number value)
861 RRDDIM *rd = rrddim_find(st, id);
863 error("Cannot find dimension with id '%s' on stats '%s' (%s).", id, st->name, st->id);
867 return rrddim_set_by_pointer(st, rd, value);
870 void rrdset_next_usec(RRDSET *st, unsigned long long microseconds)
872 if(!microseconds) rrdset_next(st);
874 debug(D_RRD_CALLS, "rrdset_next_usec() for chart %s with microseconds %llu", st->name, microseconds);
876 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s: NEXT: %llu microseconds", st->name, microseconds);
877 st->usec_since_last_update = microseconds;
881 void rrdset_next(RRDSET *st)
883 unsigned long long microseconds = 0;
885 if(likely(st->last_collected_time.tv_sec)) {
887 gettimeofday(&now, NULL);
888 microseconds = usecdiff(&now, &st->last_collected_time);
890 // prevent infinite loop
891 else microseconds = st->update_every * 1000000ULL;
893 rrdset_next_usec(st, microseconds);
896 void rrdset_next_plugins(RRDSET *st)
901 unsigned long long rrdset_done(RRDSET *st)
903 if(unlikely(netdata_exit)) return 0;
905 debug(D_RRD_CALLS, "rrdset_done() for chart %s", st->name);
908 int oldstate, store_this_entry = 1, first_entry = 0;
909 unsigned long long last_ut, now_ut, next_ut, stored_entries = 0;
911 if(unlikely(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate) != 0))
912 error("Cannot set pthread cancel state to DISABLE.");
914 // a read lock is OK here
915 pthread_rwlock_rdlock(&st->rwlock);
917 // enable the chart, if it was disabled
918 if(unlikely(rrd_delete_unupdated_dimensions) && !st->enabled)
921 // check if the chart has a long time to be updated
922 if(unlikely(st->usec_since_last_update > st->entries * st->update_every * 1000000ULL)) {
923 info("%s: took too long to be updated (%0.3Lf secs). Reseting it.", st->name, (long double)(st->usec_since_last_update / 1000000.0));
925 st->usec_since_last_update = st->update_every * 1000000ULL;
928 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s: microseconds since last update: %llu", st->name, st->usec_since_last_update);
930 // set last_collected_time
931 if(unlikely(!st->last_collected_time.tv_sec)) {
932 // it is the first entry
933 // set the last_collected_time to now
934 gettimeofday(&st->last_collected_time, NULL);
936 // the first entry should not be stored
937 store_this_entry = 0;
940 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);
943 // it is not the first entry
944 // calculate the proper last_collected_time, using usec_since_last_update
945 unsigned long long ut = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec + st->usec_since_last_update;
946 st->last_collected_time.tv_sec = (time_t) (ut / 1000000ULL);
947 st->last_collected_time.tv_usec = (suseconds_t) (ut % 1000000ULL);
950 // if this set has not been updated in the past
951 // we fake the last_update time to be = now - usec_since_last_update
952 if(unlikely(!st->last_updated.tv_sec)) {
953 // it has never been updated before
954 // set a fake last_updated, in the past using usec_since_last_update
955 unsigned long long ut = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec - st->usec_since_last_update;
956 st->last_updated.tv_sec = (time_t) (ut / 1000000ULL);
957 st->last_updated.tv_usec = (suseconds_t) (ut % 1000000ULL);
959 // the first entry should not be stored
960 store_this_entry = 0;
963 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);
966 // check if we will re-write the entire data set
967 if(unlikely(usecdiff(&st->last_collected_time, &st->last_updated) > st->update_every * st->entries * 1000000ULL)) {
968 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);
971 st->usec_since_last_update = st->update_every * 1000000ULL;
973 gettimeofday(&st->last_collected_time, NULL);
975 unsigned long long ut = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec - st->usec_since_last_update;
976 st->last_updated.tv_sec = (time_t) (ut / 1000000ULL);
977 st->last_updated.tv_usec = (suseconds_t) (ut % 1000000ULL);
979 // the first entry should not be stored
980 store_this_entry = 0;
984 // these are the 3 variables that will help us in interpolation
985 // last_ut = the last time we added a value to the storage
986 // now_ut = the time the current value is taken at
987 // next_ut = the time of the next interpolation point
988 last_ut = st->last_updated.tv_sec * 1000000ULL + st->last_updated.tv_usec;
989 now_ut = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec;
990 next_ut = (st->last_updated.tv_sec + st->update_every) * 1000000ULL;
992 if(unlikely(!first_entry && now_ut < next_ut)) {
993 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s: THIS IS IN THE SAME INTERPOLATION POINT", st->name);
996 if(unlikely(st->debug)) {
997 debug(D_RRD_STATS, "%s: last ut = %0.3Lf (last updated time)", st->name, (long double)last_ut/1000000.0);
998 debug(D_RRD_STATS, "%s: now ut = %0.3Lf (current update time)", st->name, (long double)now_ut/1000000.0);
999 debug(D_RRD_STATS, "%s: next ut = %0.3Lf (next interpolation point)", st->name, (long double)next_ut/1000000.0);
1002 if(unlikely(!st->counter_done)) {
1003 store_this_entry = 0;
1004 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s: Will not store the next entry.", st->name);
1008 // calculate totals and count the dimensions
1010 st->collected_total = 0;
1011 for( rd = st->dimensions, dimensions = 0 ; likely(rd) ; rd = rd->next, dimensions++ )
1012 if(likely(rd->updated)) st->collected_total += rd->collected_value;
1014 uint32_t storage_flags = SN_EXISTS;
1016 // process all dimensions to calculate their values
1017 // based on the collected figures only
1018 // at this stage we do not interpolate anything
1019 for( rd = st->dimensions ; likely(rd) ; rd = rd->next ) {
1021 if(unlikely(!rd->updated)) {
1022 rd->calculated_value = 0;
1026 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s/%s: START "
1027 " last_collected_value = " COLLECTED_NUMBER_FORMAT
1028 " collected_value = " COLLECTED_NUMBER_FORMAT
1029 " last_calculated_value = " CALCULATED_NUMBER_FORMAT
1030 " calculated_value = " CALCULATED_NUMBER_FORMAT
1032 , rd->last_collected_value
1033 , rd->collected_value
1034 , rd->last_calculated_value
1035 , rd->calculated_value
1038 switch(rd->algorithm) {
1039 case RRDDIM_ABSOLUTE:
1040 rd->calculated_value = (calculated_number)rd->collected_value
1041 * (calculated_number)rd->multiplier
1042 / (calculated_number)rd->divisor;
1044 if(unlikely(st->debug))
1045 debug(D_RRD_STATS, "%s/%s: CALC ABS/ABS-NO-IN "
1046 CALCULATED_NUMBER_FORMAT " = "
1047 COLLECTED_NUMBER_FORMAT
1048 " * " CALCULATED_NUMBER_FORMAT
1049 " / " CALCULATED_NUMBER_FORMAT
1051 , rd->calculated_value
1052 , rd->collected_value
1053 , (calculated_number)rd->multiplier
1054 , (calculated_number)rd->divisor
1058 case RRDDIM_PCENT_OVER_ROW_TOTAL:
1059 if(unlikely(!st->collected_total))
1060 rd->calculated_value = 0;
1062 // the percentage of the current value
1063 // over the total of all dimensions
1064 rd->calculated_value =
1065 (calculated_number)100
1066 * (calculated_number)rd->collected_value
1067 / (calculated_number)st->collected_total;
1069 if(unlikely(st->debug))
1070 debug(D_RRD_STATS, "%s/%s: CALC PCENT-ROW "
1071 CALCULATED_NUMBER_FORMAT " = 100"
1072 " * " COLLECTED_NUMBER_FORMAT
1073 " / " COLLECTED_NUMBER_FORMAT
1075 , rd->calculated_value
1076 , rd->collected_value
1077 , st->collected_total
1081 case RRDDIM_INCREMENTAL:
1082 if(unlikely(rd->counter <= 1)) {
1083 rd->calculated_value = 0;
1087 // if the new is smaller than the old (an overflow, or reset), set the old equal to the new
1088 // to reset the calculation (it will give zero as the calculation for this second)
1089 if(unlikely(rd->last_collected_value > rd->collected_value)) {
1090 debug(D_RRD_STATS, "%s.%s: RESET or OVERFLOW. Last collected value = " COLLECTED_NUMBER_FORMAT ", current = " COLLECTED_NUMBER_FORMAT
1091 , st->name, rd->name
1092 , rd->last_collected_value
1093 , rd->collected_value);
1094 if(!(rd->flags & RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS)) storage_flags = SN_EXISTS_RESET;
1095 rd->last_collected_value = rd->collected_value;
1098 rd->calculated_value =
1099 (calculated_number)(rd->collected_value - rd->last_collected_value)
1100 * (calculated_number)rd->multiplier
1101 / (calculated_number)rd->divisor;
1103 if(unlikely(st->debug))
1104 debug(D_RRD_STATS, "%s/%s: CALC INC PRE "
1105 CALCULATED_NUMBER_FORMAT " = ("
1106 COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT
1108 " * " CALCULATED_NUMBER_FORMAT
1109 " / " CALCULATED_NUMBER_FORMAT
1111 , rd->calculated_value
1112 , rd->collected_value, rd->last_collected_value
1113 , (calculated_number)rd->multiplier
1114 , (calculated_number)rd->divisor
1118 case RRDDIM_PCENT_OVER_DIFF_TOTAL:
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 // the percentage of the current increment
1136 // over the increment of all dimensions together
1137 if(unlikely(st->collected_total == st->last_collected_total))
1138 rd->calculated_value = 0;
1140 rd->calculated_value =
1141 (calculated_number)100
1142 * (calculated_number)(rd->collected_value - rd->last_collected_value)
1143 / (calculated_number)(st->collected_total - st->last_collected_total);
1145 if(unlikely(st->debug))
1146 debug(D_RRD_STATS, "%s/%s: CALC PCENT-DIFF "
1147 CALCULATED_NUMBER_FORMAT " = 100"
1148 " * (" COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT ")"
1149 " / (" COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT ")"
1151 , rd->calculated_value
1152 , rd->collected_value, rd->last_collected_value
1153 , st->collected_total, st->last_collected_total
1158 // make the default zero, to make sure
1159 // it gets noticed when we add new types
1160 rd->calculated_value = 0;
1162 if(unlikely(st->debug))
1163 debug(D_RRD_STATS, "%s/%s: CALC "
1164 CALCULATED_NUMBER_FORMAT " = 0"
1166 , rd->calculated_value
1171 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s/%s: PHASE2 "
1172 " last_collected_value = " COLLECTED_NUMBER_FORMAT
1173 " collected_value = " COLLECTED_NUMBER_FORMAT
1174 " last_calculated_value = " CALCULATED_NUMBER_FORMAT
1175 " calculated_value = " CALCULATED_NUMBER_FORMAT
1177 , rd->last_collected_value
1178 , rd->collected_value
1179 , rd->last_calculated_value
1180 , rd->calculated_value
1185 // at this point we have all the calculated values ready
1186 // it is now time to interpolate values on a second boundary
1188 unsigned long long first_ut = last_ut;
1189 long long iterations = (now_ut - last_ut) / (st->update_every * 1000000ULL);
1190 if((now_ut % (st->update_every * 1000000ULL)) == 0) iterations++;
1192 for( ; likely(next_ut <= now_ut) ; next_ut += st->update_every * 1000000ULL, iterations-- ) {
1193 #ifdef NETDATA_INTERNAL_CHECKS
1194 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); }
1197 if(unlikely(st->debug)) {
1198 debug(D_RRD_STATS, "%s: last ut = %0.3Lf (last updated time)", st->name, (long double)last_ut/1000000.0);
1199 debug(D_RRD_STATS, "%s: next ut = %0.3Lf (next interpolation point)", st->name, (long double)next_ut/1000000.0);
1202 st->last_updated.tv_sec = (time_t) (next_ut / 1000000ULL);
1203 st->last_updated.tv_usec = 0;
1205 for( rd = st->dimensions ; likely(rd) ; rd = rd->next ) {
1206 calculated_number new_value;
1208 switch(rd->algorithm) {
1209 case RRDDIM_INCREMENTAL:
1210 new_value = (calculated_number)
1211 ( rd->calculated_value
1212 * (calculated_number)(next_ut - last_ut)
1213 / (calculated_number)(now_ut - last_ut)
1216 if(unlikely(st->debug))
1217 debug(D_RRD_STATS, "%s/%s: CALC2 INC "
1218 CALCULATED_NUMBER_FORMAT " = "
1219 CALCULATED_NUMBER_FORMAT
1224 , rd->calculated_value
1225 , (next_ut - last_ut)
1226 , (now_ut - last_ut)
1229 rd->calculated_value -= new_value;
1230 new_value += rd->last_calculated_value;
1231 rd->last_calculated_value = 0;
1232 new_value /= (calculated_number)st->update_every;
1235 case RRDDIM_ABSOLUTE:
1236 case RRDDIM_PCENT_OVER_ROW_TOTAL:
1237 case RRDDIM_PCENT_OVER_DIFF_TOTAL:
1239 if(iterations == 1) {
1240 // this is the last iteration
1241 // do not interpolate
1242 // just show the calculated value
1244 new_value = rd->calculated_value;
1247 // we have missed an update
1248 // interpolate in the middle values
1250 new_value = (calculated_number)
1251 ( ( (rd->calculated_value - rd->last_calculated_value)
1252 * (calculated_number)(next_ut - first_ut)
1253 / (calculated_number)(now_ut - first_ut)
1255 + rd->last_calculated_value
1258 if(unlikely(st->debug))
1259 debug(D_RRD_STATS, "%s/%s: CALC2 DEF "
1260 CALCULATED_NUMBER_FORMAT " = ((("
1261 "(" CALCULATED_NUMBER_FORMAT " - " CALCULATED_NUMBER_FORMAT ")"
1263 " / %llu) + " CALCULATED_NUMBER_FORMAT
1266 , rd->calculated_value, rd->last_calculated_value
1267 , (next_ut - first_ut)
1268 , (now_ut - first_ut), rd->last_calculated_value
1272 // it fades the value towards the target
1273 // while we know the calculated value is different
1274 // if(likely(next_ut + st->update_every * 1000000ULL > now_ut)) rd->calculated_value = new_value;
1279 if(unlikely(!store_this_entry)) {
1280 store_this_entry = 1;
1284 if(likely(rd->updated && rd->counter > 1 && iterations < st->gap_when_lost_iterations_above)) {
1285 rd->values[st->current_entry] = pack_storage_number(new_value, storage_flags );
1287 if(unlikely(st->debug))
1288 debug(D_RRD_STATS, "%s/%s: STORE[%ld] "
1289 CALCULATED_NUMBER_FORMAT " = " CALCULATED_NUMBER_FORMAT
1292 , unpack_storage_number(rd->values[st->current_entry]), new_value
1296 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s/%s: STORE[%ld] = NON EXISTING "
1300 rd->values[st->current_entry] = pack_storage_number(0, SN_NOT_EXISTS);
1305 if(unlikely(st->debug)) {
1306 calculated_number t1 = new_value * (calculated_number)rd->multiplier / (calculated_number)rd->divisor;
1307 calculated_number t2 = unpack_storage_number(rd->values[st->current_entry]);
1308 calculated_number accuracy = accuracy_loss(t1, t2);
1309 debug(D_RRD_STATS, "%s/%s: UNPACK[%ld] = " CALCULATED_NUMBER_FORMAT " FLAGS=0x%08x (original = " CALCULATED_NUMBER_FORMAT ", accuracy loss = " CALCULATED_NUMBER_FORMAT "%%%s)"
1313 , get_storage_number_flags(rd->values[st->current_entry])
1316 , (accuracy > ACCURACY_LOSS) ? " **TOO BIG** " : ""
1319 rd->collected_volume += t1;
1320 rd->stored_volume += t2;
1321 accuracy = accuracy_loss(rd->collected_volume, rd->stored_volume);
1322 debug(D_RRD_STATS, "%s/%s: VOLUME[%ld] = " CALCULATED_NUMBER_FORMAT ", calculated = " CALCULATED_NUMBER_FORMAT ", accuracy loss = " CALCULATED_NUMBER_FORMAT "%%%s"
1326 , rd->collected_volume
1328 , (accuracy > ACCURACY_LOSS) ? " **TOO BIG** " : ""
1333 // reset the storage flags for the next point, if any;
1334 storage_flags = SN_EXISTS;
1337 st->current_entry = ((st->current_entry + 1) >= st->entries) ? 0 : st->current_entry + 1;
1341 // align next interpolation to last collection point
1342 if(likely(stored_entries || !store_this_entry)) {
1343 st->last_updated.tv_sec = st->last_collected_time.tv_sec;
1344 st->last_updated.tv_usec = st->last_collected_time.tv_usec;
1345 st->last_collected_total = st->collected_total;
1348 for( rd = st->dimensions; likely(rd) ; rd = rd->next ) {
1349 if(unlikely(!rd->updated)) continue;
1351 if(likely(stored_entries || !store_this_entry)) {
1352 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);
1353 rd->last_collected_value = rd->collected_value;
1355 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);
1356 rd->last_calculated_value = rd->calculated_value;
1359 rd->calculated_value = 0;
1360 rd->collected_value = 0;
1363 if(unlikely(st->debug)) debug(D_RRD_STATS, "%s/%s: END "
1364 " last_collected_value = " COLLECTED_NUMBER_FORMAT
1365 " collected_value = " COLLECTED_NUMBER_FORMAT
1366 " last_calculated_value = " CALCULATED_NUMBER_FORMAT
1367 " calculated_value = " CALCULATED_NUMBER_FORMAT
1369 , rd->last_collected_value
1370 , rd->collected_value
1371 , rd->last_calculated_value
1372 , rd->calculated_value
1376 // ALL DONE ABOUT THE DATA UPDATE
1377 // --------------------------------------------------------------------
1379 // find if there are any obsolete dimensions (not updated recently)
1380 if(unlikely(rrd_delete_unupdated_dimensions)) {
1382 for( rd = st->dimensions; likely(rd) ; rd = rd->next )
1383 if((rd->last_collected_time.tv_sec + (rrd_delete_unupdated_dimensions * st->update_every)) < st->last_collected_time.tv_sec)
1387 // there is dimension to free
1388 // upgrade our read lock to a write lock
1389 pthread_rwlock_unlock(&st->rwlock);
1390 pthread_rwlock_wrlock(&st->rwlock);
1392 for( rd = st->dimensions, last = NULL ; likely(rd) ; ) {
1393 // remove it only it is not updated in rrd_delete_unupdated_dimensions seconds
1395 if(unlikely((rd->last_collected_time.tv_sec + (rrd_delete_unupdated_dimensions * st->update_every)) < st->last_collected_time.tv_sec)) {
1396 info("Removing obsolete dimension '%s' (%s) of '%s' (%s).", rd->name, rd->id, st->name, st->id);
1398 if(unlikely(!last)) {
1399 st->dimensions = rd->next;
1401 rrddim_free(st, rd);
1402 rd = st->dimensions;
1406 last->next = rd->next;
1408 rrddim_free(st, rd);
1418 if(unlikely(!st->dimensions)) {
1419 info("Disabling chart %s (%s) since it does not have any dimensions", st->name, st->id);
1425 pthread_rwlock_unlock(&st->rwlock);
1427 if(unlikely(pthread_setcancelstate(oldstate, NULL) != 0))
1428 error("Cannot set pthread cancel state to RESTORE (%d).", oldstate);
1430 return(st->usec_since_last_update);