// ----------------------------------------------------------------------------
// globals
+/*
// if not zero it gives the time (in seconds) to remove un-updated dimensions
// DO NOT ENABLE
// if dimensions are removed, the chart generation will have to run again
int rrd_delete_unupdated_dimensions = 0;
+*/
int rrd_update_every = UPDATE_EVERY;
int rrd_default_history_entries = RRD_DEFAULT_HISTORY_ENTRIES;
static int rrdset_compare(void* a, void* b);
static int rrdset_compare_name(void* a, void* b);
-static int rrdcontext_compare(void* a, void* b);
+static int rrdfamily_compare(void *a, void *b);
// ----------------------------------------------------------------------------
// RRDHOST
{ NULL, rrdset_compare_name },
AVL_LOCK_INITIALIZER
},
- .rrdcontext_root_index = {
- { NULL, rrdcontext_compare },
+ .rrdfamily_root_index = {
+ { NULL, rrdfamily_compare },
AVL_LOCK_INITIALIZER
},
.variables_root_index = {
{ NULL, rrdvar_compare },
AVL_LOCK_INITIALIZER
+ },
+ .health_log = {
+ .next_log_id = 1,
+ .next_alarm_id = 1,
+ .count = 0,
+ .max = 1000,
+ .alarms = NULL,
+ .alarm_log_rwlock = PTHREAD_RWLOCK_INITIALIZER
}
};
+void rrdhost_init(char *hostname) {
+ localhost.hostname = hostname;
+ localhost.health_log.next_log_id =
+ localhost.health_log.next_alarm_id = time(NULL);
+}
+
void rrdhost_rwlock(RRDHOST *host) {
pthread_rwlock_wrlock(&host->rrdset_root_rwlock);
}
}
// ----------------------------------------------------------------------------
-// RRDCONTEXT index
+// RRDFAMILY index
-static int rrdcontext_compare(void* a, void* b) {
- if(((RRDCONTEXT *)a)->hash < ((RRDCONTEXT *)b)->hash) return -1;
- else if(((RRDCONTEXT *)a)->hash > ((RRDCONTEXT *)b)->hash) return 1;
- else return strcmp(((RRDCONTEXT *)a)->id, ((RRDCONTEXT *)b)->id);
+static int rrdfamily_compare(void *a, void *b) {
+ if(((RRDFAMILY *)a)->hash_family < ((RRDFAMILY *)b)->hash_family) return -1;
+ else if(((RRDFAMILY *)a)->hash_family > ((RRDFAMILY *)b)->hash_family) return 1;
+ else return strcmp(((RRDFAMILY *)a)->family, ((RRDFAMILY *)b)->family);
}
-#define rrdcontext_index_add(host, rc) (RRDCONTEXT *)avl_insert_lock(&((host)->rrdcontext_root_index), (avl *)(rc))
-#define rrdcontext_index_del(host, rc) (RRDCONTEXT *)avl_remove_lock(&((host)->rrdcontext_root_index), (avl *)(rc))
+#define rrdfamily_index_add(host, rc) (RRDFAMILY *)avl_insert_lock(&((host)->rrdfamily_root_index), (avl *)(rc))
+#define rrdfamily_index_del(host, rc) (RRDFAMILY *)avl_remove_lock(&((host)->rrdfamily_root_index), (avl *)(rc))
-static RRDCONTEXT *rrdcontext_index_find(RRDHOST *host, const char *id, uint32_t hash) {
- RRDCONTEXT tmp;
- tmp.id = id;
- tmp.hash = (hash)?hash:simple_hash(tmp.id);
+static RRDFAMILY *rrdfamily_index_find(RRDHOST *host, const char *id, uint32_t hash) {
+ RRDFAMILY tmp;
+ tmp.family = id;
+ tmp.hash_family = (hash)?hash:simple_hash(tmp.family);
- return (RRDCONTEXT *)avl_search_lock(&(host->rrdcontext_root_index), (avl *) &tmp);
+ return (RRDFAMILY *)avl_search_lock(&(host->rrdfamily_root_index), (avl *) &tmp);
}
-RRDCONTEXT *rrdcontext_create(const char *id) {
- RRDCONTEXT *rc = rrdcontext_index_find(&localhost, id, 0);
+RRDFAMILY *rrdfamily_create(const char *id) {
+ RRDFAMILY *rc = rrdfamily_index_find(&localhost, id, 0);
if(!rc) {
- rc = callocz(1, sizeof(RRDCONTEXT));
+ rc = callocz(1, sizeof(RRDFAMILY));
- rc->id = strdupz(id);
- rc->hash = simple_hash(rc->id);
+ rc->family = strdupz(id);
+ rc->hash_family = simple_hash(rc->family);
// initialize the variables index
avl_init_lock(&rc->variables_root_index, rrdvar_compare);
- RRDCONTEXT *ret = rrdcontext_index_add(&localhost, rc);
+ RRDFAMILY *ret = rrdfamily_index_add(&localhost, rc);
if(ret != rc)
- fatal("INTERNAL ERROR: Expected to INSERT RRDCONTEXT '%s' into index, but inserted '%s'.", rc->id, (ret)?ret->id:"NONE");
+ fatal("INTERNAL ERROR: Expected to INSERT RRDFAMILY '%s' into index, but inserted '%s'.", rc->family, (ret)?ret->family:"NONE");
}
rc->use_count++;
return rc;
}
-void rrdcontext_free(RRDCONTEXT *rc) {
+void rrdfamily_free(RRDFAMILY *rc) {
rc->use_count--;
if(!rc->use_count) {
- RRDCONTEXT *ret = rrdcontext_index_del(&localhost, rc);
+ RRDFAMILY *ret = rrdfamily_index_del(&localhost, rc);
if(ret != rc)
- fatal("INTERNAL ERROR: Expected to DELETE RRDCONTEXT '%s' from index, but deleted '%s'.", rc->id, (ret)?ret->id:"NONE");
+ fatal("INTERNAL ERROR: Expected to DELETE RRDFAMILY '%s' from index, but deleted '%s'.", rc->family, (ret)?ret->family:"NONE");
if(rc->variables_root_index.avl_tree.root != NULL)
- fatal("INTERNAL ERROR: Variables index of RRDCONTEXT '%s' that is freed, is not empty.", rc->id);
+ fatal("INTERNAL ERROR: Variables index of RRDFAMILY '%s' that is freed, is not empty.", rc->family);
- freez((void *)rc->id);
+ freez((void *)rc->family);
freez(rc);
}
}
RRDSET *rrdset_index_del_name(RRDHOST *host, RRDSET *st) {
void *result;
// fprintf(stderr, "DELETING: %s (name: %s)\n", st->id, st->name);
- return (RRDSET *)avl_remove_lock(&((host)->rrdset_root_index_name), (avl *)(&st->avlname));
+ result = (RRDSET *)avl_remove_lock(&((host)->rrdset_root_index_name), (avl *)(&st->avlname));
if(result) return rrdset_from_avlname(result);
return NULL;
}
void rrdset_set_name(RRDSET *st, const char *name)
{
- debug(D_RRD_CALLS, "rrdset_set_name() old: %s, new: %s", st->name, name);
+ if(unlikely(st->name && !strcmp(st->name, name)))
+ return;
- if(st->name) {
- rrdset_index_del_name(&localhost, st);
- rrdsetvar_rename_all(st);
- }
+ debug(D_RRD_CALLS, "rrdset_set_name() old: %s, new: %s", st->name, name);
char b[CONFIG_MAX_VALUE + 1];
char n[RRD_ID_LENGTH_MAX + 1];
snprintfz(n, RRD_ID_LENGTH_MAX, "%s.%s", st->type, name);
rrdset_strncpyz_name(b, n, CONFIG_MAX_VALUE);
- st->name = config_get(st->id, "name", b);
- st->hash_name = simple_hash(st->name);
+
+ if(st->name) {
+ rrdset_index_del_name(&localhost, st);
+ st->name = config_set_default(st->id, "name", b);
+ st->hash_name = simple_hash(st->name);
+ rrdsetvar_rename_all(st);
+ }
+ else {
+ st->name = config_get(st->id, "name", b);
+ st->hash_name = simple_hash(st->name);
+ }
+
+ pthread_rwlock_wrlock(&st->rwlock);
+ RRDDIM *rd;
+ for(rd = st->dimensions; rd ;rd = rd->next)
+ rrddimvar_rename_all(rd);
+ pthread_rwlock_unlock(&st->rwlock);
rrdset_index_add_name(&localhost, st);
}
rd->last_collected_time.tv_sec = 0;
rd->last_collected_time.tv_usec = 0;
rd->counter = 0;
- bzero(rd->values, rd->entries * sizeof(storage_number));
+ memset(rd->values, 0, rd->entries * sizeof(storage_number));
}
}
+static long align_entries_to_pagesize(long entries) {
+ if(entries < 5) entries = 5;
+ if(entries > RRD_HISTORY_ENTRIES_MAX) entries = RRD_HISTORY_ENTRIES_MAX;
+
+#ifdef NETDATA_LOG_ALLOCATIONS
+ long page = (size_t)sysconf(_SC_PAGESIZE);
+
+ long size = sizeof(RRDDIM) + entries * sizeof(storage_number);
+ if(size % page) {
+ size -= (size % page);
+ size += page;
+
+ long n = (size - sizeof(RRDDIM)) / sizeof(storage_number);
+ return n;
+ }
+
+ return entries;
+#else
+ return entries;
+#endif
+}
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)
{
char fullid[RRD_ID_LENGTH_MAX + 1];
char fullfilename[FILENAME_MAX + 1];
- RRDSET *st = NULL;
snprintfz(fullid, RRD_ID_LENGTH_MAX, "%s.%s", type, id);
- st = rrdset_find(fullid);
+ RRDSET *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);
- if(entries > RRD_HISTORY_ENTRIES_MAX) entries = config_set_number(fullid, "history", RRD_HISTORY_ENTRIES_MAX);
+ long rentries = config_get_number(fullid, "history", rrd_default_history_entries);
+ long entries = align_entries_to_pagesize(rentries);
+ if(entries != rentries) entries = config_set_number(fullid, "history", entries);
int enabled = config_get_boolean(fullid, "enabled", 1);
if(!enabled) entries = 5;
if(strcmp(st->magic, RRDSET_MAGIC) != 0) {
errno = 0;
info("Initializing file %s.", fullfilename);
- bzero(st, size);
+ memset(st, 0, size);
}
else if(strcmp(st->id, fullid) != 0) {
errno = 0;
error("File %s contents are not for chart %s. Clearing it.", fullfilename, fullid);
// munmap(st, size);
// st = NULL;
- bzero(st, size);
+ memset(st, 0, size);
}
else if(st->memsize != size || st->entries != entries) {
errno = 0;
error("File %s does not have the desired size. Clearing it.", fullfilename);
- bzero(st, size);
+ memset(st, 0, size);
}
else if(st->update_every != update_every) {
errno = 0;
error("File %s does not have the desired update frequency. Clearing it.", fullfilename);
- bzero(st, size);
+ memset(st, 0, size);
}
else if((time(NULL) - st->last_updated.tv_sec) > update_every * entries) {
errno = 0;
error("File %s is too old. Clearing it.", fullfilename);
- bzero(st, size);
+ memset(st, 0, size);
}
}
st->next = NULL;
st->mapped = rrd_memory_mode;
st->variables = NULL;
+ st->alarms = NULL;
}
else {
st = callocz(1, size);
st->mapped = RRD_MEMORY_MODE_RAM;
}
+
st->memsize = size;
st->entries = entries;
st->update_every = update_every;
st->isdetail = 0;
st->debug = 0;
- st->green = 0;
- st->red = 0;
+ // if(!strcmp(st->id, "disk_util.dm-0")) {
+ // st->debug = 1;
+ // error("enabled debugging for '%s'", st->id);
+ // }
+ // else error("not enabled debugging for '%s'", st->id);
+
+ st->green = NAN;
+ st->red = NAN;
st->last_collected_time.tv_sec = 0;
st->last_collected_time.tv_usec = 0;
st->title = config_get(st->id, "title", varvalue);
}
- st->rrdcontext = rrdcontext_create(st->context);
+ st->rrdfamily = rrdfamily_create(st->family);
st->rrdhost = &localhost;
st->next = localhost.rrdset_root;
rrdsetvar_create(st, "collected_total_raw", RRDVAR_TYPE_TOTAL, &st->last_collected_total, 0);
rrdsetvar_create(st, "green", RRDVAR_TYPE_CALCULATED, &st->green, 0);
rrdsetvar_create(st, "red", RRDVAR_TYPE_CALCULATED, &st->red, 0);
+ rrdsetvar_create(st, "update_every", RRDVAR_TYPE_INT, &st->update_every, 0);
}
rrdset_index_add(&localhost, st);
if(strcmp(rd->magic, RRDDIMENSION_MAGIC) != 0) {
errno = 0;
info("Initializing file %s.", fullfilename);
- bzero(rd, size);
+ memset(rd, 0, size);
}
else if(rd->memsize != size) {
errno = 0;
error("File %s does not have the desired size. Clearing it.", fullfilename);
- bzero(rd, size);
+ memset(rd, 0, size);
}
else if(rd->multiplier != multiplier) {
errno = 0;
error("File %s does not have the same multiplier. Clearing it.", fullfilename);
- bzero(rd, size);
+ memset(rd, 0, size);
}
else if(rd->divisor != divisor) {
errno = 0;
error("File %s does not have the same divisor. Clearing it.", fullfilename);
- bzero(rd, size);
+ memset(rd, 0, size);
}
else if(rd->algorithm != algorithm) {
errno = 0;
error("File %s does not have the same algorithm. Clearing it.", fullfilename);
- bzero(rd, size);
+ memset(rd, 0, size);
}
else if(rd->update_every != st->update_every) {
errno = 0;
error("File %s does not have the same refresh frequency. Clearing it.", fullfilename);
- bzero(rd, size);
+ memset(rd, 0, size);
}
else if(usec_dt(&now, &rd->last_collected_time) > (rd->entries * rd->update_every * 1000000ULL)) {
errno = 0;
error("File %s is too old. Clearing it.", fullfilename);
- bzero(rd, size);
+ memset(rd, 0, size);
}
else if(strcmp(rd->id, id) != 0) {
errno = 0;
error("File %s contents are not for dimension %s. Clearing it.", fullfilename, id);
// munmap(rd, size);
// rd = NULL;
- bzero(rd, size);
+ memset(rd, 0, size);
}
}
rd->last_collected_value = 0;
rd->collected_volume = 0;
rd->stored_volume = 0;
+ rd->last_stored_value = 0;
rd->values[st->current_entry] = pack_storage_number(0, SN_NOT_EXISTS);
rd->last_collected_time.tv_sec = 0;
rd->last_collected_time.tv_usec = 0;
void rrddim_set_name(RRDSET *st, RRDDIM *rd, const char *name)
{
- debug(D_RRD_CALLS, "rrddim_set_name() %s.%s", st->name, rd->name);
+ if(unlikely(rd->name && !strcmp(rd->name, name)))
+ return;
+
+ debug(D_RRD_CALLS, "rrddim_set_name() from %s.%s to %s.%s", st->name, rd->name, st->name, name);
char varname[CONFIG_MAX_NAME + 1];
snprintfz(varname, CONFIG_MAX_NAME, "dim %s name", rd->id);
- config_set_default(st->id, varname, name);
+ rd->name = config_set_default(st->id, varname, name);
rrddimvar_rename_all(rd);
}
{
debug(D_RRD_CALLS, "rrddim_free() %s.%s", st->name, rd->name);
- RRDDIM *i, *last = NULL;
- for(i = st->dimensions; i && i != rd ; i = i->next) last = i;
+ if(rd == st->dimensions)
+ st->dimensions = rd->next;
+ else {
+ RRDDIM *i;
+ for (i = st->dimensions; i && i->next != rd; i = i->next) ;
- if(!i) {
- error("Request to free dimension %s.%s but it is not linked.", st->id, rd->name);
- return;
+ if (i && i->next == rd)
+ i->next = rd->next;
+ else
+ error("Request to free dimension '%s.%s' but it is not linked.", st->id, rd->name);
}
-
- if(last) last->next = rd->next;
- else st->dimensions = rd->next;
rd->next = NULL;
while(rd->variables)
rrdset_index_del(&localhost, st);
- st->rrdcontext->use_count--;
- if(!st->rrdcontext->use_count)
- rrdcontext_free(st->rrdcontext);
+ st->rrdfamily->use_count--;
+ if(!st->rrdfamily->use_count)
+ rrdfamily_free(st->rrdfamily);
pthread_rwlock_unlock(&st->rwlock);
rd->updated = 1;
rd->counter++;
+ // fprintf(stderr, "%s.%s %llu " COLLECTED_NUMBER_FORMAT " dt %0.6f" " rate " CALCULATED_NUMBER_FORMAT "\n", st->name, rd->name, st->usec_since_last_update, value, (float)((double)st->usec_since_last_update / (double)1000000), (calculated_number)((value - rd->last_collected_value) * (calculated_number)rd->multiplier / (calculated_number)rd->divisor * 1000000.0 / (calculated_number)st->usec_since_last_update));
+
return rd->last_collected_value;
}
return rrddim_set_by_pointer(st, rd, value);
}
-void rrdset_next_usec(RRDSET *st, unsigned long long microseconds)
+void rrdset_next_usec_unfiltered(RRDSET *st, unsigned long long microseconds)
{
- if(!microseconds) rrdset_next(st);
- else {
- debug(D_RRD_CALLS, "rrdset_next_usec() for chart %s with microseconds %llu", st->name, microseconds);
-
- if(unlikely(st->debug)) debug(D_RRD_STATS, "%s: NEXT: %llu microseconds", st->name, microseconds);
- st->usec_since_last_update = microseconds;
+ if(unlikely(!st->last_collected_time.tv_sec || !microseconds)) {
+ // the first entry
+ microseconds = st->update_every * 1000000ULL;
}
+ st->usec_since_last_update = microseconds;
}
-void rrdset_next(RRDSET *st)
+void rrdset_next_usec(RRDSET *st, unsigned long long microseconds)
{
- unsigned long long microseconds = 0;
+ struct timeval now;
+ gettimeofday(&now, NULL);
- if(likely(st->last_collected_time.tv_sec)) {
- struct timeval now;
- gettimeofday(&now, NULL);
+ if(unlikely(!st->last_collected_time.tv_sec)) {
+ // the first entry
+ microseconds = st->update_every * 1000000ULL;
+ }
+ else if(unlikely(!microseconds)) {
+ // no dt given by the plugin
microseconds = usec_dt(&now, &st->last_collected_time);
}
- // prevent infinite loop
- else microseconds = st->update_every * 1000000ULL;
+ else {
+ // microseconds has the time since the last collection
+ unsigned long long now_usec = timeval_usec(&now);
+ unsigned long long last_usec = timeval_usec(&st->last_collected_time);
+ unsigned long long since_last_usec = usec_dt(&now, &st->last_collected_time);
- rrdset_next_usec(st, microseconds);
-}
+ // verify the microseconds given is good
+ if(unlikely(microseconds > since_last_usec)) {
+ debug(D_RRD_CALLS, "dt %llu usec given is too big - it leads %llu usec to the future, for chart '%s' (%s).", microseconds, microseconds - since_last_usec, st->name, st->id);
-void rrdset_next_plugins(RRDSET *st)
-{
- rrdset_next(st);
+#ifdef NETDATA_INTERNAL_CHECKS
+ if(unlikely(last_usec + microseconds > now_usec + 1000))
+ error("dt %llu usec given is too big - it leads %llu usec to the future, for chart '%s' (%s).", microseconds, microseconds - since_last_usec, st->name, st->id);
+#endif
+
+ microseconds = since_last_usec;
+ }
+ else if(unlikely(microseconds < since_last_usec * 0.8)) {
+ debug(D_RRD_CALLS, "dt %llu usec given is too small - expected %llu usec up to -20%%, for chart '%s' (%s).", microseconds, since_last_usec, st->name, st->id);
+
+#ifdef NETDATA_INTERNAL_CHECKS
+ error("dt %llu usec given is too small - expected %llu usec up to -20%%, for chart '%s' (%s).", microseconds, since_last_usec, st->name, st->id);
+#endif
+ microseconds = since_last_usec;
+ }
+ }
+ debug(D_RRD_CALLS, "rrdset_next_usec() for chart %s with microseconds %llu", st->name, microseconds);
+
+ if(unlikely(st->debug)) debug(D_RRD_STATS, "%s: NEXT: %llu microseconds", st->name, microseconds);
+ st->usec_since_last_update = microseconds;
}
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, first_entry = 0;
- unsigned long long last_ut, now_ut, next_ut, stored_entries = 0;
+ RRDDIM *rd;
- if(unlikely(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate) != 0))
+ int
+ pthreadoldcancelstate; // store the old cancelable pthread state, to restore it at the end
+
+ char
+ store_this_entry = 1, // boolean: 1 = store this entry, 0 = don't store this entry
+ first_entry = 0; // boolean: 1 = this is the first entry seen for this chart, 0 = all other entries
+
+ unsigned int
+ stored_entries = 0; // the number of entries we have stored in the db, during this call to rrdset_done()
+
+ unsigned long long
+ last_collect_ut, // the timestamp in microseconds, of the last collected value
+ now_collect_ut, // the timestamp in microseconds, of this collected value (this is NOW)
+ last_stored_ut, // the timestamp in microseconds, of the last stored entry in the db
+ next_store_ut, // the timestamp in microseconds, of the next entry to store in the db
+ update_every_ut = st->update_every * 1000000ULL; // st->update_every in microseconds
+
+ if(unlikely(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &pthreadoldcancelstate) != 0))
error("Cannot set pthread cancel state to DISABLE.");
// a read lock is OK here
pthread_rwlock_rdlock(&st->rwlock);
+/*
// enable the chart, if it was disabled
if(unlikely(rrd_delete_unupdated_dimensions) && !st->enabled)
st->enabled = 1;
+*/
// check if the chart has a long time to be updated
- if(unlikely(st->usec_since_last_update > st->entries * st->update_every * 1000000ULL)) {
+ if(unlikely(st->usec_since_last_update > st->entries * update_every_ut)) {
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;
+ st->usec_since_last_update = update_every_ut;
first_entry = 1;
}
if(unlikely(st->debug)) debug(D_RRD_STATS, "%s: microseconds since last update: %llu", st->name, st->usec_since_last_update);
// it is the first entry
// set the last_collected_time to now
gettimeofday(&st->last_collected_time, NULL);
+ last_collect_ut = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec - update_every_ut;
// the first entry should not be stored
store_this_entry = 0;
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;
+ last_collect_ut = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec;
+ unsigned long long ut = last_collect_ut + st->usec_since_last_update;
st->last_collected_time.tv_sec = (time_t) (ut / 1000000ULL);
st->last_collected_time.tv_usec = (suseconds_t) (ut % 1000000ULL);
}
}
// check if we will re-write the entire data set
- if(unlikely(usec_dt(&st->last_collected_time, &st->last_updated) > st->update_every * st->entries * 1000000ULL)) {
+ if(unlikely(usec_dt(&st->last_collected_time, &st->last_updated) > st->entries * update_every_ut)) {
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);
rrdset_reset(st);
- st->usec_since_last_update = st->update_every * 1000000ULL;
+ st->usec_since_last_update = update_every_ut;
gettimeofday(&st->last_collected_time, NULL);
}
// 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
- 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);
- }
+ // last_stored_ut = the last time we added a value to the storage
+ // now_collect_ut = the time the current value has been collected
+ // next_store_ut = the time of the next interpolation point
+ last_stored_ut = st->last_updated.tv_sec * 1000000ULL + st->last_updated.tv_usec;
+ now_collect_ut = st->last_collected_time.tv_sec * 1000000ULL + st->last_collected_time.tv_usec;
+ next_store_ut = (st->last_updated.tv_sec + st->update_every) * 1000000ULL;
if(unlikely(st->debug)) {
- debug(D_RRD_STATS, "%s: last ut = %0.3Lf (last updated time)", st->name, (long double)last_ut/1000000.0);
- debug(D_RRD_STATS, "%s: now ut = %0.3Lf (current update time)", st->name, (long double)now_ut/1000000.0);
- debug(D_RRD_STATS, "%s: next ut = %0.3Lf (next interpolation point)", st->name, (long double)next_ut/1000000.0);
+ debug(D_RRD_STATS, "%s: last_collect_ut = %0.3Lf (last collection time)", st->name, (long double)last_collect_ut/1000000.0);
+ debug(D_RRD_STATS, "%s: now_collect_ut = %0.3Lf (current collection time)", st->name, (long double)now_collect_ut/1000000.0);
+ debug(D_RRD_STATS, "%s: last_stored_ut = %0.3Lf (last updated time)", st->name, (long double)last_stored_ut/1000000.0);
+ debug(D_RRD_STATS, "%s: next_store_ut = %0.3Lf (next interpolation point)", st->name, (long double)next_store_ut/1000000.0);
}
if(unlikely(!st->counter_done)) {
// calculate totals and count the dimensions
int dimensions;
st->collected_total = 0;
- for( rd = st->dimensions, dimensions = 0 ; likely(rd) ; rd = rd->next, dimensions++ )
+ for( rd = st->dimensions, dimensions = 0 ; rd ; rd = rd->next, dimensions++ )
if(likely(rd->updated)) st->collected_total += rd->collected_value;
uint32_t storage_flags = SN_EXISTS;
// process all dimensions to calculate their values
// based on the collected figures only
// at this stage we do not interpolate anything
- for( rd = st->dimensions ; likely(rd) ; rd = rd->next ) {
+ for( rd = st->dimensions ; rd ; rd = rd->next ) {
if(unlikely(!rd->updated)) {
rd->calculated_value = 0;
rd->last_collected_value = rd->collected_value;
}
- rd->calculated_value =
+ rd->calculated_value +=
(calculated_number)(rd->collected_value - rd->last_collected_value)
* (calculated_number)rd->multiplier
/ (calculated_number)rd->divisor;
// 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;
- long long iterations = (now_ut - last_ut) / (st->update_every * 1000000ULL);
- if((now_ut % (st->update_every * 1000000ULL)) == 0) iterations++;
+ if(unlikely(now_collect_ut < next_store_ut)) {
+ // this is collected in the same interpolation point
+ if(unlikely(st->debug)) debug(D_RRD_STATS, "%s: THIS IS IN THE SAME INTERPOLATION POINT", st->name);
+ }
+
+ unsigned long long first_ut = last_stored_ut;
+ long long iterations = (now_collect_ut - last_stored_ut) / (update_every_ut);
+ if((now_collect_ut % (update_every_ut)) == 0) iterations++;
- for( ; likely(next_ut <= now_ut) ; next_ut += st->update_every * 1000000ULL, iterations-- ) {
+ for( ; next_store_ut <= now_collect_ut ; last_collect_ut = next_store_ut, next_store_ut += update_every_ut, iterations-- ) {
#ifdef NETDATA_INTERNAL_CHECKS
- 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); }
+ if(iterations < 0) { error("%s: iterations calculation wrapped! first_ut = %llu, last_stored_ut = %llu, next_store_ut = %llu, now_collect_ut = %llu", st->name, first_ut, last_stored_ut, next_store_ut, now_collect_ut); }
#endif
if(unlikely(st->debug)) {
- debug(D_RRD_STATS, "%s: last ut = %0.3Lf (last updated time)", st->name, (long double)last_ut/1000000.0);
- debug(D_RRD_STATS, "%s: next ut = %0.3Lf (next interpolation point)", st->name, (long double)next_ut/1000000.0);
+ debug(D_RRD_STATS, "%s: last_stored_ut = %0.3Lf (last updated time)", st->name, (long double)last_stored_ut/1000000.0);
+ debug(D_RRD_STATS, "%s: next_store_ut = %0.3Lf (next interpolation point)", st->name, (long double)next_store_ut/1000000.0);
}
- st->last_updated.tv_sec = (time_t) (next_ut / 1000000ULL);
+ st->last_updated.tv_sec = (time_t) (next_store_ut / 1000000ULL);
st->last_updated.tv_usec = 0;
for( rd = st->dimensions ; likely(rd) ; rd = rd->next ) {
case RRDDIM_INCREMENTAL:
new_value = (calculated_number)
( rd->calculated_value
- * (calculated_number)(next_ut - last_ut)
- / (calculated_number)(now_ut - last_ut)
+ * (calculated_number)(next_store_ut - last_collect_ut)
+ / (calculated_number)(now_collect_ut - last_collect_ut)
);
if(unlikely(st->debug))
, st->id, rd->name
, new_value
, rd->calculated_value
- , (next_ut - last_ut)
- , (now_ut - last_ut)
+ , (next_store_ut - last_stored_ut)
+ , (now_collect_ut - last_stored_ut)
);
rd->calculated_value -= new_value;
new_value += rd->last_calculated_value;
rd->last_calculated_value = 0;
new_value /= (calculated_number)st->update_every;
+
+ if(unlikely(next_store_ut - last_stored_ut < update_every_ut)) {
+ if(unlikely(st->debug))
+ debug(D_RRD_STATS, "%s/%s: COLLECTION POINT IS SHORT " CALCULATED_NUMBER_FORMAT " - EXTRAPOLATING",
+ st->id, rd->name
+ , (calculated_number)(next_store_ut - last_stored_ut)
+ );
+ new_value = new_value * (calculated_number)(st->update_every * 1000000) / (calculated_number)(next_store_ut - last_stored_ut);
+ }
break;
case RRDDIM_ABSOLUTE:
new_value = (calculated_number)
( ( (rd->calculated_value - rd->last_calculated_value)
- * (calculated_number)(next_ut - first_ut)
- / (calculated_number)(now_ut - first_ut)
+ * (calculated_number)(next_store_ut - last_collect_ut)
+ / (calculated_number)(now_collect_ut - last_collect_ut)
)
+ rd->last_calculated_value
);
, st->id, rd->name
, new_value
, rd->calculated_value, rd->last_calculated_value
- , (next_ut - first_ut)
- , (now_ut - first_ut), rd->last_calculated_value
+ , (next_store_ut - first_ut)
+ , (now_collect_ut - first_ut), rd->last_calculated_value
);
-
- // this is wrong
- // it fades the value towards the target
- // while we know the calculated value is different
- // if(likely(next_ut + st->update_every * 1000000ULL > now_ut)) rd->calculated_value = new_value;
}
break;
}
if(unlikely(!store_this_entry)) {
- store_this_entry = 1;
+ rd->values[st->current_entry] = pack_storage_number(0, SN_NOT_EXISTS);
continue;
}
, st->current_entry
);
rd->values[st->current_entry] = pack_storage_number(0, SN_NOT_EXISTS);
- rd->last_stored_value = 0;
+ rd->last_stored_value = NAN;
}
stored_entries++;
st->counter++;
st->current_entry = ((st->current_entry + 1) >= st->entries) ? 0 : st->current_entry + 1;
- last_ut = next_ut;
+ last_stored_ut = next_store_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;
- st->last_collected_total = st->collected_total;
- }
+ st->last_collected_total = st->collected_total;
- for( rd = st->dimensions; likely(rd) ; rd = rd->next ) {
+ for( rd = st->dimensions; rd ; rd = rd->next ) {
if(unlikely(!rd->updated)) continue;
- 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_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;
+ switch(rd->algorithm) {
+ case RRDDIM_INCREMENTAL:
+ if(unlikely(!first_entry)) {
+ 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->calculated_value);
+ rd->last_calculated_value += rd->calculated_value;
+ }
+ else {
+ if(unlikely(st->debug)) debug(D_RRD_STATS, "%s: THIS IS THE FIRST POINT", st->name);
+ }
+ break;
+
+ case RRDDIM_ABSOLUTE:
+ case RRDDIM_PCENT_OVER_ROW_TOTAL:
+ case RRDDIM_PCENT_OVER_DIFF_TOTAL:
+ 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;
+ break;
}
rd->calculated_value = 0;
// ALL DONE ABOUT THE DATA UPDATE
// --------------------------------------------------------------------
+/*
// find if there are any obsolete dimensions (not updated recently)
if(unlikely(rrd_delete_unupdated_dimensions)) {
break;
if(unlikely(rd)) {
+ RRDDIM *last;
// there is dimension to free
// upgrade our read lock to a write lock
pthread_rwlock_unlock(&st->rwlock);
}
}
}
+*/
pthread_rwlock_unlock(&st->rwlock);
- if(unlikely(pthread_setcancelstate(oldstate, NULL) != 0))
- error("Cannot set pthread cancel state to RESTORE (%d).", oldstate);
+ if(unlikely(pthread_setcancelstate(pthreadoldcancelstate, NULL) != 0))
+ error("Cannot set pthread cancel state to RESTORE (%d).", pthreadoldcancelstate);
return(st->usec_since_last_update);
}