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