diff options
author | Federico Ceratto <federico.ceratto@gmail.com> | 2016-03-30 21:40:42 +0000 |
---|---|---|
committer | Federico Ceratto <federico.ceratto@gmail.com> | 2016-03-30 21:40:42 +0000 |
commit | 9ce153ce7167c11adba8ac225edc7a707e97c6eb (patch) | |
tree | 9f6e849cce2dcc7e5b4e9e6252c843dc2d0787a2 /src/plugin_idlejitter.c | |
download | netdata-9ce153ce7167c11adba8ac225edc7a707e97c6eb.tar.xz netdata-9ce153ce7167c11adba8ac225edc7a707e97c6eb.zip |
Imported Upstream version 1.0.0upstream/1.0.0
Diffstat (limited to 'src/plugin_idlejitter.c')
-rwxr-xr-x | src/plugin_idlejitter.c | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/src/plugin_idlejitter.c b/src/plugin_idlejitter.c new file mode 100755 index 00000000..56c22a16 --- /dev/null +++ b/src/plugin_idlejitter.c @@ -0,0 +1,68 @@ +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif +#include <pthread.h> +#include <sys/time.h> +#include <sys/resource.h> +#include <strings.h> +#include <unistd.h> + +#include "global_statistics.h" +#include "common.h" +#include "appconfig.h" +#include "log.h" +#include "rrd.h" +#include "plugin_idlejitter.h" + +#define CPU_IDLEJITTER_SLEEP_TIME_MS 20 + +void *cpuidlejitter_main(void *ptr) +{ + if(ptr) { ; } + + info("CPU Idle Jitter thread created with task id %d", gettid()); + + if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0) + error("Cannot set pthread cancel type to DEFERRED."); + + if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0) + error("Cannot set pthread cancel state to ENABLE."); + + int sleep_ms = (int) config_get_number("plugin:idlejitter", "loop time in ms", CPU_IDLEJITTER_SLEEP_TIME_MS); + if(sleep_ms <= 0) { + config_set_number("plugin:idlejitter", "loop time in ms", CPU_IDLEJITTER_SLEEP_TIME_MS); + sleep_ms = CPU_IDLEJITTER_SLEEP_TIME_MS; + } + + RRDSET *st = rrdset_find("system.idlejitter"); + if(!st) { + st = rrdset_create("system", "idlejitter", NULL, "processes", NULL, "CPU Idle Jitter", "microseconds lost/s", 9999, rrd_update_every, RRDSET_TYPE_LINE); + rrddim_add(st, "jitter", NULL, 1, 1, RRDDIM_ABSOLUTE); + } + + struct timeval before, after; + unsigned long long counter; + for(counter = 0; 1 ;counter++) { + unsigned long long usec = 0, susec = 0; + + while(susec < (rrd_update_every * 1000000ULL)) { + + gettimeofday(&before, NULL); + usleep(sleep_ms * 1000); + gettimeofday(&after, NULL); + + // calculate the time it took for a full loop + usec = usecdiff(&after, &before); + susec += usec; + } + usec -= (sleep_ms * 1000); + + if(counter) rrdset_next(st); + rrddim_set(st, "jitter", usec); + rrdset_done(st); + } + + pthread_exit(NULL); + return NULL; +} + |