]> arthur.barton.de Git - netdata.git/blob - src/plugin_idlejitter.c
23fb4c9d984460515cda5e2d8b786941ea15d499
[netdata.git] / src / plugin_idlejitter.c
1 #include "common.h"
2
3 #define CPU_IDLEJITTER_SLEEP_TIME_MS 20
4
5 void *cpuidlejitter_main(void *ptr) {
6     struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
7
8     info("IDLEJITTER thread created with task id %d", gettid());
9
10     if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
11         error("Cannot set pthread cancel type to DEFERRED.");
12
13     if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
14         error("Cannot set pthread cancel state to ENABLE.");
15
16     int sleep_ms = (int) config_get_number("plugin:idlejitter", "loop time in ms", CPU_IDLEJITTER_SLEEP_TIME_MS);
17     if(sleep_ms <= 0) {
18         config_set_number("plugin:idlejitter", "loop time in ms", CPU_IDLEJITTER_SLEEP_TIME_MS);
19         sleep_ms = CPU_IDLEJITTER_SLEEP_TIME_MS;
20     }
21
22     RRDSET *st = rrdset_find("system.idlejitter");
23     if(!st) {
24         st = rrdset_create("system", "idlejitter", NULL, "processes", NULL, "CPU Idle Jitter", "microseconds lost/s", 9999, rrd_update_every, RRDSET_TYPE_LINE);
25         rrddim_add(st, "jitter", NULL, 1, 1, RRDDIM_ALGORITHM_ABSOLUTE);
26     }
27
28     struct timeval before, after;
29     unsigned long long counter;
30     for(counter = 0; 1 ;counter++) {
31         usec_t usec = 0, susec = 0;
32
33         while(susec < (rrd_update_every * USEC_PER_SEC)) {
34
35             now_monotonic_timeval(&before);
36             sleep_usec(sleep_ms * 1000);
37             now_monotonic_timeval(&after);
38
39             // calculate the time it took for a full loop
40             usec = dt_usec(&after, &before);
41             susec += usec;
42         }
43         usec -= (sleep_ms * 1000);
44
45         if(counter) rrdset_next(st);
46         rrddim_set(st, "jitter", usec);
47         rrdset_done(st);
48     }
49
50     info("IDLEJITTER thread exiting");
51
52     static_thread->enabled = 0;
53     pthread_exit(NULL);
54     return NULL;
55 }
56