@cryptotaxi247 / netdata-1 / commits / 3faea5fb8

fixed rotation bug of the round robin db

Costa Tsaousis (ktsaou) committed May 2, 2014 at 08:36 UTC 3faea5fb8d082e78574507acb77f6c9653313b94
1 file changed +21 -22
netdata.c
+21 -22
@@ -864,8 +864,6 @@ RRD_STATS *rrd_stats_create(const char *type, const char *id, const char *name,
864 return NULL;
865 }
866
867 - // no need to terminate the strings after strncpy(), because of calloc()
868 -
867 snprintf(st->id, RRD_STATS_NAME_MAX, "%s.%s", type, id);
868 st->hash = simple_hash(st->id);
869
@@ -905,14 +903,12 @@ RRD_STATS *rrd_stats_create(const char *type, const char *id, const char *name,
903 st->update_every = update_every;
904
905 pthread_mutex_init(&st->mutex, NULL);
908 - pthread_mutex_lock(&st->mutex);
906 pthread_mutex_lock(&root_mutex);
907
908 st->next = root;
909 root = st;
910
911 pthread_mutex_unlock(&root_mutex);
915 - // leave st->mutex locked
912
913 return(st);
914 }
@@ -1096,13 +1092,15 @@ unsigned long long rrd_stats_next(RRD_STATS *st)
1092 st->absolute_total = 0;
1093 st->current_entry = ((st->current_entry + 1) >= st->entries) ? 0 : st->current_entry + 1;
1094
1099 - // leave mutex locked
1095 + pthread_mutex_unlock(&st->mutex);
1096
1097 return st->usec_since_last_update;
1098 }
1099
1100 void rrd_stats_done(RRD_STATS *st)
1101 {
1102 + pthread_mutex_lock(&st->mutex);
1103 +
1104 RRD_DIMENSION *rd, *last;
1105
1106 // find if there are any obsolete dimensions (not updated recently)
@@ -1132,6 +1130,7 @@ void rrd_stats_done(RRD_STATS *st)
1130
1131 if(!st->dimensions) {
1132 st->enabled = 0;
1133 + pthread_mutex_unlock(&st->mutex);
1134 // rrd_stats_free(st);
1135 return;
1136 }
@@ -1283,17 +1282,16 @@ void rrd_stats_done(RRD_STATS *st)
1282 // add the value we will overwrite
1283 st->first_entry_t += st->timediff[st->current_entry];
1284 }
1286 -
1287 - // store the time difference to the last entry
1288 - st->timediff[st->current_entry] = st->usec_since_last_update;
1285 }
1286 + // store the time difference to the last entry
1287 + st->timediff[st->current_entry] = st->usec_since_last_update;
1288 }
1289
1290 st->last_updated.tv_sec = now.tv_sec;
1291 st->last_updated.tv_usec = now.tv_usec;
1292
1293 st->counter++;
1296 -
1294 +
1295 pthread_mutex_unlock(&st->mutex);
1296 }
1297
@@ -1764,11 +1762,12 @@ unsigned long rrd_stats_json(int type, RRD_STATS *st, struct web_buffer *wb, siz
1762 // make sure current_entry is within limits
1763 long current_entry = st->current_entry;
1764 if(current_entry < 0) current_entry = 0;
1767 - else if(current_entry >= st->entries) current_entry = st->entries;
1765 + else if(current_entry >= st->entries) current_entry = st->entries - 1;
1766
1767 // find the oldest entry of the round-robin
1768 long stop_entry = current_entry + 1;
1769 if(stop_entry >= st->entries) stop_entry = 0;
1770 + if(st->counter < st->entries) stop_entry = 0;
1771
1772 if(before == 0) before = st->last_updated.tv_sec;
1773 if(after == 0) after = rrd_stats_first_entry_t(st);
@@ -1873,9 +1872,12 @@ unsigned long rrd_stats_json(int type, RRD_STATS *st, struct web_buffer *wb, siz
1872 unsigned long long time_usec = st->last_updated.tv_sec * 1000000ULL + st->last_updated.tv_usec;
1873 long t, count, printed, group_count;
1874
1876 - for (count = printed = group_count = 0, t = current_entry; t != stop_entry && st->timediff[t] ; time_usec -= st->timediff[t], t--) {
1875 + for (count = printed = group_count = 0, t = current_entry; t != stop_entry ; time_usec -= st->timediff[t], t--) {
1876 if(t < 0) t = st->entries - 1;
1877
1878 + if(st->debug)
1879 + if(st->timediff[t] == 0) debug(D_RRD_STATS, "WARNING: %s time diff is zero at position %ld", st->id, t);
1880 +
1881 int print_this = 0;
1882 time_t now = time_usec / 1000000ULL;
1883
@@ -5605,9 +5607,10 @@ void *chartsd_worker_thread(void *arg)
5607 continue;
5608 }
5609
5608 - debug(D_CHARTSD, "CHARTSD: script %s is setting dimension %s/%s to %s", cd->fullfilename, st->id, dimension, value);
5609 -
5610 + if(st->debug) debug(D_CHARTSD, "CHARTSD: script %s is setting dimension %s/%s to %s", cd->fullfilename, st->id, dimension, value);
5611 rrd_stats_dimension_set(st, dimension, atoll(value));
5612 +
5613 + count++;
5614 }
5615 else if(!strcmp(s, "BEGIN")) {
5616 char *id = qstrsep(&p);
@@ -5629,8 +5632,7 @@ void *chartsd_worker_thread(void *arg)
5632 continue;
5633 }
5634
5632 - debug(D_CHARTSD, "CHARTSD: script %s is requesting a END on chart %s", cd->fullfilename, st->id);
5633 -
5635 + if(st->debug) debug(D_CHARTSD, "CHARTSD: script %s is requesting a END on chart %s", cd->fullfilename, st->id);
5636 rrd_stats_done(st);
5637 st = NULL;
5638 }
@@ -5698,7 +5700,7 @@ void *chartsd_worker_thread(void *arg)
5700 }
5701
5702 if(!st) {
5701 - error("CHARTSD: script %s is requesting an DIMENSION, without a CHART", cd->fullfilename);
5703 + error("CHARTSD: script %s is requesting a DIMENSION, without a CHART", cd->fullfilename);
5704 continue;
5705 }
5706
@@ -5712,7 +5714,7 @@ void *chartsd_worker_thread(void *arg)
5714
5715 if(!algorithm || !*algorithm) algorithm = "absolute";
5716
5715 - debug(D_CHARTSD, "CHARTSD: Creating dimension in chart %s, id='%s', name='%s', algorithm='%s', multiplier=%ld, divisor=%ld, hidden='%s'"
5717 + if(st->debug) debug(D_CHARTSD, "CHARTSD: Creating dimension in chart %s, id='%s', name='%s', algorithm='%s', multiplier=%ld, divisor=%ld, hidden='%s'"
5718 , st->id
5719 , id
5720 , name?name:""
@@ -5723,8 +5725,7 @@ void *chartsd_worker_thread(void *arg)
5725 );
5726
5727 RRD_DIMENSION *rd = rrd_stats_dimension_add(st, id, name, multiplier, divisor, algorithm_id(algorithm));
5726 - if(hidden && strcmp(hidden, "hidden") == 0)
5727 - rd->hidden = 1;
5728 + if(hidden && strcmp(hidden, "hidden") == 0) rd->hidden = 1;
5729 }
5730 else if(!strcmp(s, "MYPID")) {
5731 char *pid = qstrsep(&p);
@@ -5732,10 +5733,8 @@ void *chartsd_worker_thread(void *arg)
5733 debug(D_CHARTSD, "CHARTSD: %s is on pid %d", cd->id, cd->pid);
5734 }
5735 else error("CHARTSD: script %s is sending command '%s' which is not known by netdata", cd->fullfilename, s);
5735 -
5736 - count++;
5736 }
5738 - if(!count) error("Script '%s' does not generate any output.", cd->fullfilename);
5737 + if(!count) error("Script '%s' does not generate usefull output.", cd->fullfilename);
5738 pclose(fp);
5739
5740 sleep(cd->update_every);