]> arthur.barton.de Git - netdata.git/blob - src/proc_stat.c
Merge pull request #1998 from ktsaou/master
[netdata.git] / src / proc_stat.c
1 #include "common.h"
2
3 int do_proc_stat(int update_every, usec_t dt) {
4     (void)dt;
5
6     static procfile *ff = NULL;
7     static int do_cpu = -1, do_cpu_cores = -1, do_interrupts = -1, do_context = -1, do_forks = -1, do_processes = -1;
8     static uint32_t hash_intr, hash_ctxt, hash_processes, hash_procs_running, hash_procs_blocked;
9
10     if(unlikely(do_cpu == -1)) {
11         do_cpu          = config_get_boolean("plugin:proc:/proc/stat", "cpu utilization", 1);
12         do_cpu_cores    = config_get_boolean("plugin:proc:/proc/stat", "per cpu core utilization", 1);
13         do_interrupts   = config_get_boolean("plugin:proc:/proc/stat", "cpu interrupts", 1);
14         do_context      = config_get_boolean("plugin:proc:/proc/stat", "context switches", 1);
15         do_forks        = config_get_boolean("plugin:proc:/proc/stat", "processes started", 1);
16         do_processes    = config_get_boolean("plugin:proc:/proc/stat", "processes running", 1);
17
18         hash_intr = simple_hash("intr");
19         hash_ctxt = simple_hash("ctxt");
20         hash_processes = simple_hash("processes");
21         hash_procs_running = simple_hash("procs_running");
22         hash_procs_blocked = simple_hash("procs_blocked");
23     }
24
25     if(unlikely(!ff)) {
26         char filename[FILENAME_MAX + 1];
27         snprintfz(filename, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/proc/stat");
28         ff = procfile_open(config_get("plugin:proc:/proc/stat", "filename to monitor", filename), " \t:", PROCFILE_FLAG_DEFAULT);
29         if(unlikely(!ff)) return 1;
30     }
31
32     ff = procfile_readall(ff);
33     if(unlikely(!ff)) return 0; // we return 0, so that we will retry to open it next time
34
35     size_t lines = procfile_lines(ff), l;
36     size_t words;
37
38     unsigned long long processes = 0, running = 0 , blocked = 0;
39     RRDSET *st;
40
41     for(l = 0; l < lines ;l++) {
42         char *row_key = procfile_lineword(ff, l, 0);
43         uint32_t hash = simple_hash(row_key);
44
45         // faster strncmp(row_key, "cpu", 3) == 0
46         if(likely(row_key[0] == 'c' && row_key[1] == 'p' && row_key[2] == 'u')) {
47             words = procfile_linewords(ff, l);
48             if(unlikely(words < 9)) {
49                 error("Cannot read /proc/stat cpu line. Expected 9 params, read %zu.", words);
50                 continue;
51             }
52
53             char *id;
54             unsigned long long user = 0, nice = 0, system = 0, idle = 0, iowait = 0, irq = 0, softirq = 0, steal = 0, guest = 0, guest_nice = 0;
55
56             id          = row_key;
57             user        = str2ull(procfile_lineword(ff, l, 1));
58             nice        = str2ull(procfile_lineword(ff, l, 2));
59             system      = str2ull(procfile_lineword(ff, l, 3));
60             idle        = str2ull(procfile_lineword(ff, l, 4));
61             iowait      = str2ull(procfile_lineword(ff, l, 5));
62             irq         = str2ull(procfile_lineword(ff, l, 6));
63             softirq     = str2ull(procfile_lineword(ff, l, 7));
64             steal       = str2ull(procfile_lineword(ff, l, 8));
65
66             guest       = str2ull(procfile_lineword(ff, l, 9));
67             user -= guest;
68
69             guest_nice  = str2ull(procfile_lineword(ff, l, 10));
70             nice -= guest_nice;
71
72             char *title, *type, *context, *family;
73             long priority;
74             int isthistotal;
75
76             if(unlikely(strcmp(id, "cpu")) == 0) {
77                 title = "Total CPU utilization";
78                 type = "system";
79                 context = "system.cpu";
80                 family = id;
81                 priority = 100;
82                 isthistotal = 1;
83             }
84             else {
85                 title = "Core utilization";
86                 type = "cpu";
87                 context = "cpu.cpu";
88                 family = "utilization";
89                 priority = 1000;
90                 isthistotal = 0;
91             }
92
93             if(likely((isthistotal && do_cpu) || (!isthistotal && do_cpu_cores))) {
94                 st = rrdset_find_bytype_localhost(type, id);
95                 if(unlikely(!st)) {
96                     st = rrdset_create_localhost(type, id, NULL, family, context, title, "percentage", priority
97                                                  , update_every, RRDSET_TYPE_STACKED);
98
99                     long multiplier = 1;
100                     long divisor = 1; // sysconf(_SC_CLK_TCK);
101
102                     rrddim_add(st, "guest_nice", NULL, multiplier, divisor, RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL);
103                     rrddim_add(st, "guest", NULL, multiplier, divisor, RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL);
104                     rrddim_add(st, "steal", NULL, multiplier, divisor, RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL);
105                     rrddim_add(st, "softirq", NULL, multiplier, divisor, RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL);
106                     rrddim_add(st, "irq", NULL, multiplier, divisor, RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL);
107                     rrddim_add(st, "user", NULL, multiplier, divisor, RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL);
108                     rrddim_add(st, "system", NULL, multiplier, divisor, RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL);
109                     rrddim_add(st, "nice", NULL, multiplier, divisor, RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL);
110                     rrddim_add(st, "iowait", NULL, multiplier, divisor, RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL);
111
112                     rrddim_add(st, "idle", NULL, multiplier, divisor, RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL);
113                     rrddim_hide(st, "idle");
114                 }
115                 else rrdset_next(st);
116
117                 rrddim_set(st, "user", user);
118                 rrddim_set(st, "nice", nice);
119                 rrddim_set(st, "system", system);
120                 rrddim_set(st, "idle", idle);
121                 rrddim_set(st, "iowait", iowait);
122                 rrddim_set(st, "irq", irq);
123                 rrddim_set(st, "softirq", softirq);
124                 rrddim_set(st, "steal", steal);
125                 rrddim_set(st, "guest", guest);
126                 rrddim_set(st, "guest_nice", guest_nice);
127                 rrdset_done(st);
128             }
129         }
130         else if(unlikely(hash == hash_intr && strcmp(row_key, "intr") == 0)) {
131             unsigned long long value = str2ull(procfile_lineword(ff, l, 1));
132
133             // --------------------------------------------------------------------
134
135             if(likely(do_interrupts)) {
136                 st = rrdset_find_bytype_localhost("system", "intr");
137                 if(unlikely(!st)) {
138                     st = rrdset_create_localhost("system", "intr", NULL, "interrupts", NULL, "CPU Interrupts"
139                                                  , "interrupts/s", 900, update_every, RRDSET_TYPE_LINE);
140                     rrdset_flag_set(st, RRDSET_FLAG_DETAIL);
141
142                     rrddim_add(st, "interrupts", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
143                 }
144                 else rrdset_next(st);
145
146                 rrddim_set(st, "interrupts", value);
147                 rrdset_done(st);
148             }
149         }
150         else if(unlikely(hash == hash_ctxt && strcmp(row_key, "ctxt") == 0)) {
151             unsigned long long value = str2ull(procfile_lineword(ff, l, 1));
152
153             // --------------------------------------------------------------------
154
155             if(likely(do_context)) {
156                 st = rrdset_find_bytype_localhost("system", "ctxt");
157                 if(unlikely(!st)) {
158                     st = rrdset_create_localhost("system", "ctxt", NULL, "processes", NULL, "CPU Context Switches"
159                                                  , "context switches/s", 800, update_every, RRDSET_TYPE_LINE);
160
161                     rrddim_add(st, "switches", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
162                 }
163                 else rrdset_next(st);
164
165                 rrddim_set(st, "switches", value);
166                 rrdset_done(st);
167             }
168         }
169         else if(unlikely(hash == hash_processes && !processes && strcmp(row_key, "processes") == 0)) {
170             processes = str2ull(procfile_lineword(ff, l, 1));
171         }
172         else if(unlikely(hash == hash_procs_running && !running && strcmp(row_key, "procs_running") == 0)) {
173             running = str2ull(procfile_lineword(ff, l, 1));
174         }
175         else if(unlikely(hash == hash_procs_blocked && !blocked && strcmp(row_key, "procs_blocked") == 0)) {
176             blocked = str2ull(procfile_lineword(ff, l, 1));
177         }
178     }
179
180     // --------------------------------------------------------------------
181
182     if(likely(do_forks)) {
183         st = rrdset_find_bytype_localhost("system", "forks");
184         if(unlikely(!st)) {
185             st = rrdset_create_localhost("system", "forks", NULL, "processes", NULL, "Started Processes", "processes/s"
186                                          , 700, update_every, RRDSET_TYPE_LINE);
187             rrdset_flag_set(st, RRDSET_FLAG_DETAIL);
188
189             rrddim_add(st, "started", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
190         }
191         else rrdset_next(st);
192
193         rrddim_set(st, "started", processes);
194         rrdset_done(st);
195     }
196
197     // --------------------------------------------------------------------
198
199     if(likely(do_processes)) {
200         st = rrdset_find_bytype_localhost("system", "processes");
201         if(unlikely(!st)) {
202             st = rrdset_create_localhost("system", "processes", NULL, "processes", NULL, "System Processes", "processes"
203                                          , 600, update_every, RRDSET_TYPE_LINE);
204
205             rrddim_add(st, "running", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
206             rrddim_add(st, "blocked", NULL, -1, 1, RRD_ALGORITHM_ABSOLUTE);
207         }
208         else rrdset_next(st);
209
210         rrddim_set(st, "running", running);
211         rrddim_set(st, "blocked", blocked);
212         rrdset_done(st);
213     }
214
215     return 0;
216 }