]> arthur.barton.de Git - netdata.git/blobdiff - src/rrd.c
40% faster AVL searching and DICTIONARY operations
[netdata.git] / src / rrd.c
old mode 100755 (executable)
new mode 100644 (file)
index 3881d51..ee23da0
--- a/src/rrd.c
+++ b/src/rrd.c
@@ -42,35 +42,26 @@ int rrd_memory_mode = RRD_MEMORY_MODE_SAVE;
 // ----------------------------------------------------------------------------
 // RRDSET index
 
-static int rrdset_iterator(avl *a) { if(a) {}; return 0; }
-
 static int rrdset_compare(void* a, void* b) {
        if(((RRDSET *)a)->hash < ((RRDSET *)b)->hash) return -1;
        else if(((RRDSET *)a)->hash > ((RRDSET *)b)->hash) return 1;
        else return strcmp(((RRDSET *)a)->id, ((RRDSET *)b)->id);
 }
 
-avl_tree rrdset_root_index = {
-               NULL,
-               rrdset_compare,
-#ifdef AVL_LOCK_WITH_MUTEX
-               PTHREAD_MUTEX_INITIALIZER
-#else
-               PTHREAD_RWLOCK_INITIALIZER
-#endif
+avl_tree_lock rrdset_root_index = {
+               { NULL, rrdset_compare },
+               AVL_LOCK_INITIALIZER
 };
 
-#define rrdset_index_add(st) avl_insert(&rrdset_root_index, (avl *)(st))
-#define rrdset_index_del(st) avl_remove(&rrdset_root_index, (avl *)(st))
+#define rrdset_index_add(st) avl_insert_lock(&rrdset_root_index, (avl *)(st))
+#define rrdset_index_del(st) avl_remove_lock(&rrdset_root_index, (avl *)(st))
 
 static RRDSET *rrdset_index_find(const char *id, uint32_t hash) {
-       RRDSET *result = NULL, tmp;
-       strncpy(tmp.id, id, RRD_ID_LENGTH_MAX);
-       tmp.id[RRD_ID_LENGTH_MAX] = '\0';
+       RRDSET tmp;
+       strncpyz(tmp.id, id, RRD_ID_LENGTH_MAX);
        tmp.hash = (hash)?hash:simple_hash(tmp.id);
 
-       avl_search(&(rrdset_root_index), (avl *)&tmp, rrdset_iterator, (avl **)&result);
-       return result;
+       return (RRDSET *)avl_search_lock(&(rrdset_root_index), (avl *) &tmp);
 }
 
 // ----------------------------------------------------------------------------
@@ -78,8 +69,6 @@ static RRDSET *rrdset_index_find(const char *id, uint32_t hash) {
 
 #define rrdset_from_avlname(avlname_ptr) ((RRDSET *)((avlname_ptr) - offsetof(RRDSET, avlname)))
 
-static int rrdset_iterator_name(avl *a) { if(a) {}; return 0; }
-
 static int rrdset_compare_name(void* a, void* b) {
        RRDSET *A = rrdset_from_avlname(a);
        RRDSET *B = rrdset_from_avlname(b);
@@ -91,22 +80,17 @@ static int rrdset_compare_name(void* a, void* b) {
        else return strcmp(A->name, B->name);
 }
 
-avl_tree rrdset_root_index_name = {
-               NULL,
-               rrdset_compare_name,
-#ifdef AVL_LOCK_WITH_MUTEX
-               PTHREAD_MUTEX_INITIALIZER
-#else
-               PTHREAD_RWLOCK_INITIALIZER
-#endif
+avl_tree_lock rrdset_root_index_name = {
+               { NULL, rrdset_compare_name },
+               AVL_LOCK_INITIALIZER
 };
 
 int rrdset_index_add_name(RRDSET *st) {
        // fprintf(stderr, "ADDING: %s (name: %s)\n", st->id, st->name);
-       return avl_insert(&rrdset_root_index_name, (avl *)(&st->avlname));
+       return avl_insert_lock(&rrdset_root_index_name, (avl *) (&st->avlname));
 }
 
-#define rrdset_index_del_name(st) avl_remove(&rrdset_root_index_name, (avl *)(&st->avlname))
+#define rrdset_index_del_name(st) avl_remove_lock(&rrdset_root_index_name, (avl *)(&st->avlname))
 
 static RRDSET *rrdset_index_find_name(const char *name, uint32_t hash) {
        void *result = NULL;
@@ -115,7 +99,7 @@ static RRDSET *rrdset_index_find_name(const char *name, uint32_t hash) {
        tmp.hash_name = (hash)?hash:simple_hash(tmp.name);
 
        // fprintf(stderr, "SEARCHING: %s\n", name);
-       avl_search(&(rrdset_root_index_name), (avl *)(&(tmp.avlname)), rrdset_iterator_name, (avl **)&result);
+       result = avl_search_lock(&(rrdset_root_index_name), (avl *) (&(tmp.avlname)));
        if(result) {
                RRDSET *st = rrdset_from_avlname(result);
                if(strcmp(st->magic, RRDSET_MAGIC))
@@ -132,25 +116,21 @@ static RRDSET *rrdset_index_find_name(const char *name, uint32_t hash) {
 // ----------------------------------------------------------------------------
 // RRDDIM index
 
-static int rrddim_iterator(avl *a) { if(a) {}; return 0; }
-
 static int rrddim_compare(void* a, void* b) {
        if(((RRDDIM *)a)->hash < ((RRDDIM *)b)->hash) return -1;
        else if(((RRDDIM *)a)->hash > ((RRDDIM *)b)->hash) return 1;
        else return strcmp(((RRDDIM *)a)->id, ((RRDDIM *)b)->id);
 }
 
-#define rrddim_index_add(st, rd) avl_insert(&((st)->dimensions_index), (avl *)(rd))
-#define rrddim_index_del(st,rd ) avl_remove(&((st)->dimensions_index), (avl *)(rd))
+#define rrddim_index_add(st, rd) avl_insert_lock(&((st)->dimensions_index), (avl *)(rd))
+#define rrddim_index_del(st,rd ) avl_remove_lock(&((st)->dimensions_index), (avl *)(rd))
 
 static RRDDIM *rrddim_index_find(RRDSET *st, const char *id, uint32_t hash) {
-       RRDDIM *result = NULL, tmp;
-       strncpy(tmp.id, id, RRD_ID_LENGTH_MAX);
-       tmp.id[RRD_ID_LENGTH_MAX] = '\0';
+       RRDDIM tmp;
+       strncpyz(tmp.id, id, RRD_ID_LENGTH_MAX);
        tmp.hash = (hash)?hash:simple_hash(tmp.id);
 
-       avl_search(&(st->dimensions_index), (avl *)&tmp, rrddim_iterator, (avl **)&result);
-       return result;
+       return (RRDDIM *)avl_search_lock(&(st->dimensions_index), (avl *) &tmp);
 }
 
 // ----------------------------------------------------------------------------
@@ -222,8 +202,8 @@ int rrd_memory_mode_id(const char *name)
 
 int rrddim_algorithm_id(const char *name)
 {
-       if(strcmp(name, RRDDIM_ABSOLUTE_NAME) == 0)                     return RRDDIM_ABSOLUTE;
        if(strcmp(name, RRDDIM_INCREMENTAL_NAME) == 0)                  return RRDDIM_INCREMENTAL;
+       if(strcmp(name, RRDDIM_ABSOLUTE_NAME) == 0)                     return RRDDIM_ABSOLUTE;
        if(strcmp(name, RRDDIM_PCENT_OVER_ROW_TOTAL_NAME) == 0)                 return RRDDIM_PCENT_OVER_ROW_TOTAL;
        if(strcmp(name, RRDDIM_PCENT_OVER_DIFF_TOTAL_NAME) == 0)        return RRDDIM_PCENT_OVER_DIFF_TOTAL;
        return RRDDIM_ABSOLUTE;
@@ -277,7 +257,7 @@ void rrdset_set_name(RRDSET *st, const char *name)
        char b[CONFIG_MAX_VALUE + 1];
        char n[RRD_ID_LENGTH_MAX + 1];
 
-       snprintf(n, RRD_ID_LENGTH_MAX, "%s.%s", st->type, name);
+       snprintfz(n, RRD_ID_LENGTH_MAX, "%s.%s", st->type, name);
        rrdset_strncpy_name(b, n, CONFIG_MAX_VALUE);
        st->name = config_get(st->id, "name", b);
        st->hash_name = simple_hash(st->name);
@@ -299,7 +279,7 @@ char *rrdset_cache_dir(const char *id)
        char n[FILENAME_MAX + 1];
        rrdset_strncpy_name(b, id, FILENAME_MAX);
 
-       snprintf(n, FILENAME_MAX, "%s/%s", cache_dir, b);
+       snprintfz(n, FILENAME_MAX, "%s/%s", cache_dir, b);
        ret = config_get(id, "cache directory", n);
 
        if(rrd_memory_mode == RRD_MEMORY_MODE_MAP || rrd_memory_mode == RRD_MEMORY_MODE_SAVE) {
@@ -335,8 +315,13 @@ void rrdset_reset(RRDSET *st)
        }
 }
 
-RRDSET *rrdset_create(const char *type, const char *id, const char *name, const char *family, const char *title, const char *units, long priority, int update_every, int chart_type)
+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)
 {
+       if(!type || !type[0]) {
+               fatal("Cannot create rrd stats without a type.");
+               return NULL;
+       }
+
        if(!id || !id[0]) {
                fatal("Cannot create rrd stats without an id.");
                return NULL;
@@ -346,7 +331,13 @@ RRDSET *rrdset_create(const char *type, const char *id, const char *name, const
        char fullfilename[FILENAME_MAX + 1];
        RRDSET *st = NULL;
 
-       snprintf(fullid, RRD_ID_LENGTH_MAX, "%s.%s", type, id);
+       snprintfz(fullid, RRD_ID_LENGTH_MAX, "%s.%s", type, id);
+
+       st = rrdset_find(fullid);
+       if(st) {
+               error("Cannot create rrd stats for '%s', it already exists.", fullid);
+               return st;
+       }
 
        long entries = config_get_number(fullid, "history", rrd_default_history_entries);
        if(entries < 5) entries = config_set_number(fullid, "history", 5);
@@ -360,7 +351,7 @@ RRDSET *rrdset_create(const char *type, const char *id, const char *name, const
 
        debug(D_RRD_CALLS, "Creating RRD_STATS for '%s.%s'.", type, id);
 
-       snprintf(fullfilename, FILENAME_MAX, "%s/main.db", cache_dir);
+       snprintfz(fullfilename, FILENAME_MAX, "%s/main.db", cache_dir);
        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);
        if(st) {
                if(strcmp(st->magic, RRDSET_MAGIC) != 0) {
@@ -396,6 +387,7 @@ RRDSET *rrdset_create(const char *type, const char *id, const char *name, const
                st->name = NULL;
                st->type = NULL;
                st->family = NULL;
+               st->context = NULL;
                st->title = NULL;
                st->units = NULL;
                st->dimensions = NULL;
@@ -422,10 +414,11 @@ RRDSET *rrdset_create(const char *type, const char *id, const char *name, const
 
        st->cache_dir = cache_dir;
 
-       st->family     = config_get(st->id, "family", family?family:st->id);
-       st->units      = config_get(st->id, "units", units?units:"");
-       st->type       = config_get(st->id, "type", type);
        st->chart_type = rrdset_type_id(config_get(st->id, "chart type", rrdset_type_name(chart_type)));
+       st->type       = config_get(st->id, "type", type);
+       st->family     = config_get(st->id, "family", family?family:st->type);
+       st->context    = config_get(st->id, "context", context?context:st->id);
+       st->units      = config_get(st->id, "units", units?units:"");
 
        st->priority = config_get_number(st->id, "priority", priority);
        st->enabled = enabled;
@@ -437,9 +430,10 @@ RRDSET *rrdset_create(const char *type, const char *id, const char *name, const
        st->last_collected_time.tv_usec = 0;
        st->counter_done = 0;
 
-       st->gap_when_lost_iterations_above = config_get_number(st->id, "gap when lost iterations above", RRD_DEFAULT_GAP_INTERPOLATIONS) + 2;
+       st->gap_when_lost_iterations_above = (int) (
+                       config_get_number(st->id, "gap when lost iterations above", RRD_DEFAULT_GAP_INTERPOLATIONS) + 2);
 
-       avl_init(&st->dimensions_index, rrddim_compare);
+       avl_init_lock(&st->dimensions_index, rrddim_compare);
 
        pthread_rwlock_init(&st->rwlock, NULL);
        pthread_rwlock_wrlock(&rrdset_root_rwlock);
@@ -449,7 +443,7 @@ RRDSET *rrdset_create(const char *type, const char *id, const char *name, const
 
        {
                char varvalue[CONFIG_MAX_VALUE + 1];
-               snprintf(varvalue, CONFIG_MAX_VALUE, "%s (%s)", title?title:"", st->name);
+               snprintfz(varvalue, CONFIG_MAX_VALUE, "%s (%s)", title?title:"", st->name);
                st->title = config_get(st->id, "title", varvalue);
        }
 
@@ -475,7 +469,7 @@ RRDDIM *rrddim_add(RRDSET *st, const char *id, const char *name, long multiplier
        debug(D_RRD_CALLS, "Adding dimension '%s/%s'.", st->id, id);
 
        rrdset_strncpy_name(filename, id, FILENAME_MAX);
-       snprintf(fullfilename, FILENAME_MAX, "%s/%s.db", st->cache_dir, filename);
+       snprintfz(fullfilename, FILENAME_MAX, "%s/%s.db", st->cache_dir, filename);
        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);
        if(rd) {
                struct timeval now;
@@ -547,19 +541,19 @@ RRDDIM *rrddim_add(RRDSET *st, const char *id, const char *name, long multiplier
 
        strcpy(rd->magic, RRDDIMENSION_MAGIC);
        strcpy(rd->cache_filename, fullfilename);
-       strncpy(rd->id, id, RRD_ID_LENGTH_MAX);
+       strncpyz(rd->id, id, RRD_ID_LENGTH_MAX);
        rd->hash = simple_hash(rd->id);
 
-       snprintf(varname, CONFIG_MAX_NAME, "dim %s name", rd->id);
+       snprintfz(varname, CONFIG_MAX_NAME, "dim %s name", rd->id);
        rd->name = config_get(st->id, varname, (name && *name)?name:rd->id);
 
-       snprintf(varname, CONFIG_MAX_NAME, "dim %s algorithm", rd->id);
+       snprintfz(varname, CONFIG_MAX_NAME, "dim %s algorithm", rd->id);
        rd->algorithm = rrddim_algorithm_id(config_get(st->id, varname, rrddim_algorithm_name(algorithm)));
 
-       snprintf(varname, CONFIG_MAX_NAME, "dim %s multiplier", rd->id);
+       snprintfz(varname, CONFIG_MAX_NAME, "dim %s multiplier", rd->id);
        rd->multiplier = config_get_number(st->id, varname, multiplier);
 
-       snprintf(varname, CONFIG_MAX_NAME, "dim %s divisor", rd->id);
+       snprintfz(varname, CONFIG_MAX_NAME, "dim %s divisor", rd->id);
        rd->divisor = config_get_number(st->id, varname, divisor);
        if(!rd->divisor) rd->divisor = 1;
 
@@ -590,15 +584,15 @@ void rrddim_set_name(RRDSET *st, RRDDIM *rd, const char *name)
        debug(D_RRD_CALLS, "rrddim_set_name() %s.%s", st->name, rd->name);
 
        char varname[CONFIG_MAX_NAME + 1];
-       snprintf(varname, CONFIG_MAX_NAME, "dim %s name", rd->id);
-       config_get(st->id, varname, name);
+       snprintfz(varname, CONFIG_MAX_NAME, "dim %s name", rd->id);
+       config_set_default(st->id, varname, name);
 }
 
 void rrddim_free(RRDSET *st, RRDDIM *rd)
 {
        debug(D_RRD_CALLS, "rrddim_free() %s.%s", st->name, rd->name);
 
-       RRDDIM *i = st->dimensions, *last = NULL;
+       RRDDIM *i, *last = NULL;
        for(i = st->dimensions; i && i != rd ; i = i->next) last = i;
 
        if(!i) {
@@ -606,8 +600,9 @@ void rrddim_free(RRDSET *st, RRDDIM *rd)
                return;
        }
 
-       if(last) last = i->next;
-       else st->dimensions = i->next;
+       if(last) last->next = rd->next;
+       else st->dimensions = rd->next;
+       rd->next = NULL;
 
        rrddim_index_del(st, rd);
 
@@ -667,6 +662,9 @@ void rrdset_save_all(void)
 {
        debug(D_RRD_CALLS, "rrdset_save_all()");
 
+       // let it log a few error messages
+       error_log_limit_reset();
+
        RRDSET *st;
        RRDDIM *rd;
 
@@ -706,12 +704,10 @@ RRDSET *rrdset_find_bytype(const char *type, const char *id)
 
        char buf[RRD_ID_LENGTH_MAX + 1];
 
-       strncpy(buf, type, RRD_ID_LENGTH_MAX - 1);
-       buf[RRD_ID_LENGTH_MAX - 1] = '\0';
+       strncpyz(buf, type, RRD_ID_LENGTH_MAX - 1);
        strcat(buf, ".");
-       int len = strlen(buf);
-       strncpy(&buf[len], id, RRD_ID_LENGTH_MAX - len);
-       buf[RRD_ID_LENGTH_MAX] = '\0';
+       int len = (int) strlen(buf);
+       strncpyz(&buf[len], id, (size_t) (RRD_ID_LENGTH_MAX - len));
 
        return(rrdset_find(buf));
 }
@@ -802,6 +798,8 @@ void rrdset_next(RRDSET *st)
                gettimeofday(&now, NULL);
                microseconds = usecdiff(&now, &st->last_collected_time);
        }
+       // prevent infinite loop
+       else microseconds = st->update_every * 1000000ULL;
 
        rrdset_next_usec(st, microseconds);
 }
@@ -816,7 +814,8 @@ unsigned long long rrdset_done(RRDSET *st)
        debug(D_RRD_CALLS, "rrdset_done() for chart %s", st->name);
 
        RRDDIM *rd, *last;
-       int oldstate, store_this_entry = 1;
+       int oldstate, store_this_entry = 1, first_entry = 0;
+       unsigned long long last_ut, now_ut, next_ut, stored_entries = 0;
 
        if(unlikely(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate) != 0))
                error("Cannot set pthread cancel state to DISABLE.");
@@ -825,13 +824,15 @@ unsigned long long rrdset_done(RRDSET *st)
        pthread_rwlock_rdlock(&st->rwlock);
 
        // enable the chart, if it was disabled
-       st->enabled = 1;
+       if(unlikely(rrd_delete_unupdated_dimensions) && !st->enabled)
+               st->enabled = 1;
 
-       // check if the chart has a long time to be refreshed
+       // check if the chart has a long time to be updated
        if(unlikely(st->usec_since_last_update > st->entries * st->update_every * 1000000ULL)) {
                info("%s: took too long to be updated (%0.3Lf secs). Reseting it.", st->name, (long double)(st->usec_since_last_update / 1000000.0));
                rrdset_reset(st);
                st->usec_since_last_update = st->update_every * 1000000ULL;
+               first_entry = 1;
        }
        if(unlikely(st->debug)) debug(D_RRD_STATS, "%s: microseconds since last update: %llu", st->name, st->usec_since_last_update);
 
@@ -843,15 +844,16 @@ unsigned long long rrdset_done(RRDSET *st)
 
                // the first entry should not be stored
                store_this_entry = 0;
+               first_entry = 1;
 
-               if(unlikely(st->debug)) debug(D_RRD_STATS, "%s: initializing last_collected to now. Will not store the next entry.", st->name);
+               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);
        }
        else {
                // it is not the first entry
                // calculate the proper last_collected_time, using usec_since_last_update
                unsigned long long ut = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec + st->usec_since_last_update;
-               st->last_collected_time.tv_sec = ut / 1000000ULL;
-               st->last_collected_time.tv_usec = ut % 1000000ULL;
+               st->last_collected_time.tv_sec = (time_t) (ut / 1000000ULL);
+               st->last_collected_time.tv_usec = (useconds_t) (ut % 1000000ULL);
        }
 
        // if this set has not been updated in the past
@@ -860,11 +862,12 @@ unsigned long long rrdset_done(RRDSET *st)
                // it has never been updated before
                // set a fake last_updated, in the past using usec_since_last_update
                unsigned long long ut = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec - st->usec_since_last_update;
-               st->last_updated.tv_sec = ut / 1000000ULL;
-               st->last_updated.tv_usec = ut % 1000000ULL;
+               st->last_updated.tv_sec = (time_t) (ut / 1000000ULL);
+               st->last_updated.tv_usec = (useconds_t) (ut % 1000000ULL);
 
                // the first entry should not be stored
                store_this_entry = 0;
+               first_entry = 1;
 
                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);
        }
@@ -879,20 +882,25 @@ unsigned long long rrdset_done(RRDSET *st)
                gettimeofday(&st->last_collected_time, NULL);
 
                unsigned long long ut = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec - st->usec_since_last_update;
-               st->last_updated.tv_sec = ut / 1000000ULL;
-               st->last_updated.tv_usec = ut % 1000000ULL;
+               st->last_updated.tv_sec = (time_t) (ut / 1000000ULL);
+               st->last_updated.tv_usec = (useconds_t) (ut % 1000000ULL);
 
                // the first entry should not be stored
                store_this_entry = 0;
+               first_entry = 1;
        }
 
        // these are the 3 variables that will help us in interpolation
        // last_ut = the last time we added a value to the storage
        //  now_ut = the time the current value is taken at
        // next_ut = the time of the next interpolation point
-       unsigned long long last_ut = st->last_updated.tv_sec * 1000000ULL + st->last_updated.tv_usec;
-       unsigned long long now_ut  = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec;
-       unsigned long long next_ut = (st->last_updated.tv_sec + st->update_every) * 1000000ULL;
+       last_ut = st->last_updated.tv_sec * 1000000ULL + st->last_updated.tv_usec;
+       now_ut  = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec;
+       next_ut = (st->last_updated.tv_sec + st->update_every) * 1000000ULL;
+
+       if(unlikely(!first_entry && now_ut < next_ut)) {
+               if(unlikely(st->debug)) debug(D_RRD_STATS, "%s: THIS IS IN THE SAME INTERPOLATION POINT", st->name);
+       }
 
        if(unlikely(st->debug)) {
                debug(D_RRD_STATS, "%s: last ut = %0.3Lf (last updated time)", st->name, (long double)last_ut/1000000.0);
@@ -919,12 +927,7 @@ unsigned long long rrdset_done(RRDSET *st)
        // at this stage we do not interpolate anything
        for( rd = st->dimensions ; likely(rd) ; rd = rd->next ) {
 
-               if(unlikely(!rd->updated || rd->counter <= 1)) {
-                       rd->calculated_value = 0;
-                       continue;
-               }
-
-               if(unlikely(st->debug)) debug(D_RRD_STATS, "%s/%s: "
+               if(unlikely(st->debug)) debug(D_RRD_STATS, "%s/%s: START "
                        " last_collected_value = " COLLECTED_NUMBER_FORMAT
                        " collected_value = " COLLECTED_NUMBER_FORMAT
                        " last_calculated_value = " CALCULATED_NUMBER_FORMAT
@@ -937,26 +940,24 @@ unsigned long long rrdset_done(RRDSET *st)
                        );
 
                switch(rd->algorithm) {
-                       case RRDDIM_PCENT_OVER_DIFF_TOTAL:
-                               // the percentage of the current increment
-                               // over the increment of all dimensions together
-                               if(unlikely(st->collected_total == st->last_collected_total)) rd->calculated_value = rd->last_calculated_value;
-                               else rd->calculated_value =
-                                         (calculated_number)100
-                                       * (calculated_number)(rd->collected_value - rd->last_collected_value)
-                                       / (calculated_number)(st->collected_total  - st->last_collected_total);
-
-                               if(unlikely(st->debug))
-                                       debug(D_RRD_STATS, "%s/%s: CALC PCENT-DIFF "
-                                               CALCULATED_NUMBER_FORMAT " = 100"
-                                               " * (" COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT ")"
-                                               " / (" COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT ")"
-                                               , st->id, rd->name
-                                               , rd->calculated_value
-                                               , rd->collected_value, rd->last_collected_value
-                                               , st->collected_total, st->last_collected_total
-                                               );
-                               break;
+               case RRDDIM_ABSOLUTE:
+                       rd->calculated_value = (calculated_number)rd->collected_value
+                               * (calculated_number)rd->multiplier
+                               / (calculated_number)rd->divisor;
+
+                       if(unlikely(st->debug))
+                               debug(D_RRD_STATS, "%s/%s: CALC ABS/ABS-NO-IN "
+                                       CALCULATED_NUMBER_FORMAT " = "
+                                       COLLECTED_NUMBER_FORMAT
+                                       " * " CALCULATED_NUMBER_FORMAT
+                                       " / " CALCULATED_NUMBER_FORMAT
+                                       , st->id, rd->name
+                                       , rd->calculated_value
+                                       , rd->collected_value
+                                       , (calculated_number)rd->multiplier
+                                       , (calculated_number)rd->divisor
+                                       );
+                       break;
 
                        case RRDDIM_PCENT_OVER_ROW_TOTAL:
                                if(unlikely(!st->collected_total)) rd->calculated_value = 0;
@@ -981,6 +982,11 @@ unsigned long long rrdset_done(RRDSET *st)
                                break;
 
                        case RRDDIM_INCREMENTAL:
+                               if(unlikely(!rd->updated || rd->counter <= 1)) {
+                                       rd->calculated_value = 0;
+                                       continue;
+                               }
+
                                // if the new is smaller than the old (an overflow, or reset), set the old equal to the new
                                // to reset the calculation (it will give zero as the calculation for this second)
                                if(unlikely(rd->last_collected_value > rd->collected_value)) {
@@ -992,17 +998,17 @@ unsigned long long rrdset_done(RRDSET *st)
                                        rd->last_collected_value = rd->collected_value;
                                }
 
-                               rd->calculated_value += (calculated_number)(rd->collected_value - rd->last_collected_value)
+                               rd->calculated_value = (calculated_number)(rd->collected_value - rd->last_collected_value)
                                        * (calculated_number)rd->multiplier
                                        / (calculated_number)rd->divisor;
 
                                if(unlikely(st->debug))
-                                       debug(D_RRD_STATS, "%s/%s: CALC INC "
-                                               CALCULATED_NUMBER_FORMAT " += ("
+                                       debug(D_RRD_STATS, "%s/%s: CALC INC PRE "
+                                               CALCULATED_NUMBER_FORMAT " = ("
                                                COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT
-                                               " * %ld"
-                                               " / %ld"
                                                ")"
+                                               " * " CALCULATED_NUMBER_FORMAT
+                                               " / " CALCULATED_NUMBER_FORMAT
                                                , st->id, rd->name
                                                , rd->calculated_value
                                                , rd->collected_value, rd->last_collected_value
@@ -1011,22 +1017,29 @@ unsigned long long rrdset_done(RRDSET *st)
                                                );
                                break;
 
-                       case RRDDIM_ABSOLUTE:
-                               rd->calculated_value = (calculated_number)rd->collected_value
-                                       * (calculated_number)rd->multiplier
-                                       / (calculated_number)rd->divisor;
+                       case RRDDIM_PCENT_OVER_DIFF_TOTAL:
+                               if(unlikely(!rd->updated || rd->counter <= 1)) {
+                                       rd->calculated_value = 0;
+                                       continue;
+                               }
+
+                               // the percentage of the current increment
+                               // over the increment of all dimensions together
+                               if(unlikely(st->collected_total == st->last_collected_total)) rd->calculated_value = rd->last_calculated_value;
+                               else rd->calculated_value =
+                                         (calculated_number)100
+                                       * (calculated_number)(rd->collected_value - rd->last_collected_value)
+                                       / (calculated_number)(st->collected_total  - st->last_collected_total);
 
                                if(unlikely(st->debug))
-                                       debug(D_RRD_STATS, "%s/%s: CALC ABS/ABS-NO-IN "
-                                               CALCULATED_NUMBER_FORMAT " = "
-                                               COLLECTED_NUMBER_FORMAT
-                                               " * %ld"
-                                               " / %ld"
+                                       debug(D_RRD_STATS, "%s/%s: CALC PCENT-DIFF "
+                                               CALCULATED_NUMBER_FORMAT " = 100"
+                                               " * (" COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT ")"
+                                               " / (" COLLECTED_NUMBER_FORMAT " - " COLLECTED_NUMBER_FORMAT ")"
                                                , st->id, rd->name
                                                , rd->calculated_value
-                                               , rd->collected_value
-                                               , (calculated_number)rd->multiplier
-                                               , (calculated_number)rd->divisor
+                                               , rd->collected_value, rd->last_collected_value
+                                               , st->collected_total, st->last_collected_total
                                                );
                                break;
 
@@ -1043,17 +1056,31 @@ unsigned long long rrdset_done(RRDSET *st)
                                                );
                                break;
                }
+
+               if(unlikely(st->debug)) debug(D_RRD_STATS, "%s/%s: PHASE2 "
+                       " last_collected_value = " COLLECTED_NUMBER_FORMAT
+                       " collected_value = " COLLECTED_NUMBER_FORMAT
+                       " last_calculated_value = " CALCULATED_NUMBER_FORMAT
+                       " calculated_value = " CALCULATED_NUMBER_FORMAT
+                       , st->id, rd->name
+                       , rd->last_collected_value
+                       , rd->collected_value
+                       , rd->last_calculated_value
+                       , rd->calculated_value
+                       );
+
        }
 
        // at this point we have all the calculated values ready
        // it is now time to interpolate values on a second boundary
 
        unsigned long long first_ut = last_ut;
-       int iterations = (now_ut - last_ut) / (st->update_every * 1000000ULL);
+       long long iterations = (now_ut - last_ut) / (st->update_every * 1000000ULL);
+       if((now_ut % (st->update_every * 1000000ULL)) == 0) iterations++;
 
        for( ; likely(next_ut <= now_ut) ; next_ut += st->update_every * 1000000ULL, iterations-- ) {
 #ifdef NETDATA_INTERNAL_CHECKS
-               if(iterations <= 0) error("iterations calculation wrapped!");
+               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); }
 #endif
 
                if(unlikely(st->debug)) {
@@ -1061,7 +1088,7 @@ unsigned long long rrdset_done(RRDSET *st)
                        debug(D_RRD_STATS, "%s: next ut = %0.3Lf (next interpolation point)", st->name, (long double)next_ut/1000000.0);
                }
 
-               st->last_updated.tv_sec = next_ut / 1000000ULL;
+               st->last_updated.tv_sec = (time_t) (next_ut / 1000000ULL);
                st->last_updated.tv_usec = 0;
 
                for( rd = st->dimensions ; likely(rd) ; rd = rd->next ) {
@@ -1084,11 +1111,13 @@ unsigned long long rrdset_done(RRDSET *st)
                                                        , st->id, rd->name
                                                        , new_value
                                                        , rd->calculated_value
-                                                       , (unsigned long long)(next_ut - last_ut)
-                                                       , (unsigned long long)(now_ut - last_ut)
+                                                       , (next_ut - last_ut)
+                                                       , (now_ut - last_ut)
                                                        );
 
                                        rd->calculated_value -= new_value;
+                                       new_value += rd->last_calculated_value;
+                                       rd->last_calculated_value = 0;
                                        new_value /= (calculated_number)st->update_every;
                                        break;
 
@@ -1160,6 +1189,8 @@ unsigned long long rrdset_done(RRDSET *st)
                                rd->values[st->current_entry] = pack_storage_number(0, SN_NOT_EXISTS);
                        }
 
+                       stored_entries++;
+
                        if(unlikely(st->debug)) {
                                calculated_number t1 = new_value * (calculated_number)rd->multiplier / (calculated_number)rd->divisor;
                                calculated_number t2 = unpack_storage_number(rd->values[st->current_entry]);
@@ -1196,23 +1227,38 @@ unsigned long long rrdset_done(RRDSET *st)
                last_ut = next_ut;
        }
 
+       // align next interpolation to last collection point
+       if(likely(stored_entries || !store_this_entry)) {
+               st->last_updated.tv_sec = st->last_collected_time.tv_sec;
+               st->last_updated.tv_usec = st->last_collected_time.tv_usec;
+       }
+
        for( rd = st->dimensions; likely(rd) ; rd = rd->next ) {
                if(unlikely(!rd->updated)) continue;
-               rd->last_collected_value = rd->collected_value;
-               rd->last_calculated_value = rd->calculated_value;
+
+               if(likely(stored_entries || !store_this_entry)) {
+                       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);
+                       rd->last_collected_value = rd->collected_value;
+
+                       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);
+                       rd->last_calculated_value = rd->calculated_value;
+               }
+
+               rd->calculated_value = 0;
                rd->collected_value = 0;
                rd->updated = 0;
 
-               // if this is the first entry of incremental dimensions
-               // we have to set the first calculated_value to zero
-               // to eliminate the first spike
-               if(unlikely(st->counter_done == 1)) switch(rd->algorithm) {
-                       case RRDDIM_PCENT_OVER_DIFF_TOTAL:
-                       case RRDDIM_INCREMENTAL:
-                               rd->calculated_value = 0;
-                               // the next time, a new incremental total will be calculated
-                               break;
-               }
+               if(unlikely(st->debug)) debug(D_RRD_STATS, "%s/%s: END "
+                       " last_collected_value = " COLLECTED_NUMBER_FORMAT
+                       " collected_value = " COLLECTED_NUMBER_FORMAT
+                       " last_calculated_value = " CALCULATED_NUMBER_FORMAT
+                       " calculated_value = " CALCULATED_NUMBER_FORMAT
+                       , st->id, rd->name
+                       , rd->last_collected_value
+                       , rd->collected_value
+                       , rd->last_calculated_value
+                       , rd->calculated_value
+                       );
        }
        st->last_collected_total  = st->collected_total;