]> arthur.barton.de Git - netdata.git/blob - src/plugin_idlejitter.c
build: migrate to autotools
[netdata.git] / src / plugin_idlejitter.c
1 #ifdef HAVE_CONFIG_H
2 #include <config.h>
3 #endif
4 #include <pthread.h>
5 #include <sys/time.h>
6 #include <sys/resource.h>
7 #include <strings.h>
8 #include <unistd.h>
9
10 #include "global_statistics.h"
11 #include "common.h"
12 #include "appconfig.h"
13 #include "log.h"
14 #include "rrd.h"
15 #include "plugin_idlejitter.h"
16
17 #define CPU_IDLEJITTER_SLEEP_TIME_MS 20
18
19 void *cpuidlejitter_main(void *ptr)
20 {
21         if(ptr) { ; }
22
23         info("CPU Idle Jitter thread created with task id %d", gettid());
24
25         if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
26                 error("Cannot set pthread cancel type to DEFERRED.");
27
28         if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
29                 error("Cannot set pthread cancel state to ENABLE.");
30
31         int sleep_ms = config_get_number("plugin:idlejitter", "loop time in ms", CPU_IDLEJITTER_SLEEP_TIME_MS);
32         if(sleep_ms <= 0) {
33                 config_set_number("plugin:idlejitter", "loop time in ms", CPU_IDLEJITTER_SLEEP_TIME_MS);
34                 sleep_ms = CPU_IDLEJITTER_SLEEP_TIME_MS;
35         }
36
37         RRDSET *st = rrdset_find("system.idlejitter");
38         if(!st) {
39                 st = rrdset_create("system", "idlejitter", NULL, "cpu", "CPU Idle Jitter", "microseconds lost/s", 9999, rrd_update_every, RRDSET_TYPE_LINE);
40                 rrddim_add(st, "jitter", NULL, 1, 1, RRDDIM_ABSOLUTE);
41         }
42
43         struct timeval before, after;
44         unsigned long long counter;
45         for(counter = 0; 1 ;counter++) {
46                 unsigned long long usec = 0, susec = 0;
47
48                 while(susec < (rrd_update_every * 1000000ULL)) {
49
50                         gettimeofday(&before, NULL);
51                         usleep(sleep_ms * 1000);
52                         gettimeofday(&after, NULL);
53
54                         // calculate the time it took for a full loop
55                         usec = usecdiff(&after, &before);
56                         susec += usec;
57                 }
58                 usec -= (sleep_ms * 1000);
59
60                 if(counter) rrdset_next_usec(st, susec);
61                 rrddim_set(st, "jitter", usec);
62                 rrdset_done(st);
63         }
64
65         return NULL;
66 }
67