]> arthur.barton.de Git - netdata.git/blob - src/proc_vmstat.c
Merge pull request #1998 from ktsaou/master
[netdata.git] / src / proc_vmstat.c
1 #include "common.h"
2
3 int do_proc_vmstat(int update_every, usec_t dt) {
4     (void)dt;
5
6     static procfile *ff = NULL;
7     static int do_swapio = -1, do_io = -1, do_pgfaults = -1, do_numa = -1;
8     static int has_numa = -1;
9
10     static ARL_BASE *arl_base = NULL;
11     static unsigned long long numa_foreign = 0ULL;
12     static unsigned long long numa_hint_faults = 0ULL;
13     static unsigned long long numa_hint_faults_local = 0ULL;
14     static unsigned long long numa_huge_pte_updates = 0ULL;
15     static unsigned long long numa_interleave = 0ULL;
16     static unsigned long long numa_local = 0ULL;
17     static unsigned long long numa_other = 0ULL;
18     static unsigned long long numa_pages_migrated = 0ULL;
19     static unsigned long long numa_pte_updates = 0ULL;
20     static unsigned long long pgfault = 0ULL;
21     static unsigned long long pgmajfault = 0ULL;
22     static unsigned long long pgpgin = 0ULL;
23     static unsigned long long pgpgout = 0ULL;
24     static unsigned long long pswpin = 0ULL;
25     static unsigned long long pswpout = 0ULL;
26
27     if(unlikely(!arl_base)) {
28         do_swapio = config_get_boolean_ondemand("plugin:proc:/proc/vmstat", "swap i/o", CONFIG_BOOLEAN_AUTO);
29         do_io = config_get_boolean("plugin:proc:/proc/vmstat", "disk i/o", 1);
30         do_pgfaults = config_get_boolean("plugin:proc:/proc/vmstat", "memory page faults", 1);
31         do_numa = config_get_boolean_ondemand("plugin:proc:/proc/vmstat", "system-wide numa metric summary", CONFIG_BOOLEAN_AUTO);
32
33
34         arl_base = arl_create("vmstat", NULL, 60);
35         arl_expect(arl_base, "pgfault", &pgfault);
36         arl_expect(arl_base, "pgmajfault", &pgmajfault);
37         arl_expect(arl_base, "pgpgin", &pgpgin);
38         arl_expect(arl_base, "pgpgout", &pgpgout);
39         arl_expect(arl_base, "pswpin", &pswpin);
40         arl_expect(arl_base, "pswpout", &pswpout);
41
42         if(do_numa == CONFIG_BOOLEAN_YES || (do_numa == CONFIG_BOOLEAN_AUTO && get_numa_node_count() >= 2)) {
43             arl_expect(arl_base, "numa_foreign", &numa_foreign);
44             arl_expect(arl_base, "numa_hint_faults_local", &numa_hint_faults_local);
45             arl_expect(arl_base, "numa_hint_faults", &numa_hint_faults);
46             arl_expect(arl_base, "numa_huge_pte_updates", &numa_huge_pte_updates);
47             arl_expect(arl_base, "numa_interleave", &numa_interleave);
48             arl_expect(arl_base, "numa_local", &numa_local);
49             arl_expect(arl_base, "numa_other", &numa_other);
50             arl_expect(arl_base, "numa_pages_migrated", &numa_pages_migrated);
51             arl_expect(arl_base, "numa_pte_updates", &numa_pte_updates);
52         }
53         else {
54             // Do not expect numa metrics when they are not needed.
55             // By not adding them, the ARL will stop processing the file
56             // when all the expected metrics are collected.
57             // Also ARL will not parse their values.
58             has_numa = 0;
59             do_numa = CONFIG_BOOLEAN_NO;
60         }
61     }
62
63     if(unlikely(!ff)) {
64         char filename[FILENAME_MAX + 1];
65         snprintfz(filename, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/proc/vmstat");
66         ff = procfile_open(config_get("plugin:proc:/proc/vmstat", "filename to monitor", filename), " \t:", PROCFILE_FLAG_DEFAULT);
67         if(unlikely(!ff)) return 1;
68     }
69
70     ff = procfile_readall(ff);
71     if(unlikely(!ff)) return 0; // we return 0, so that we will retry to open it next time
72
73     size_t lines = procfile_lines(ff), l;
74
75     arl_begin(arl_base);
76     for(l = 0; l < lines ;l++) {
77         size_t words = procfile_linewords(ff, l);
78         if(unlikely(words < 2)) {
79             if(unlikely(words)) error("Cannot read /proc/vmstat line %zu. Expected 2 params, read %zu.", l, words);
80             continue;
81         }
82
83         if(unlikely(arl_check(arl_base,
84                 procfile_lineword(ff, l, 0),
85                 procfile_lineword(ff, l, 1)))) break;
86     }
87
88     // --------------------------------------------------------------------
89
90     if(pswpin || pswpout || do_swapio == CONFIG_BOOLEAN_YES) {
91         do_swapio = CONFIG_BOOLEAN_YES;
92
93         static RRDSET *st_swapio = NULL;
94         if(unlikely(!st_swapio)) {
95             st_swapio = rrdset_create_localhost("system", "swapio", NULL, "swap", NULL, "Swap I/O", "kilobytes/s", 250
96                                                 , update_every, RRDSET_TYPE_AREA);
97
98             rrddim_add(st_swapio, "in",  NULL, sysconf(_SC_PAGESIZE), 1024, RRD_ALGORITHM_INCREMENTAL);
99             rrddim_add(st_swapio, "out", NULL, -sysconf(_SC_PAGESIZE), 1024, RRD_ALGORITHM_INCREMENTAL);
100         }
101         else rrdset_next(st_swapio);
102
103         rrddim_set(st_swapio, "in", pswpin);
104         rrddim_set(st_swapio, "out", pswpout);
105         rrdset_done(st_swapio);
106     }
107
108     // --------------------------------------------------------------------
109
110     if(do_io) {
111         static RRDSET *st_io = NULL;
112         if(unlikely(!st_io)) {
113             st_io = rrdset_create_localhost("system", "io", NULL, "disk", NULL, "Disk I/O", "kilobytes/s", 150
114                                             , update_every, RRDSET_TYPE_AREA);
115
116             rrddim_add(st_io, "in",  NULL,  1, 1, RRD_ALGORITHM_INCREMENTAL);
117             rrddim_add(st_io, "out", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
118         }
119         else rrdset_next(st_io);
120
121         rrddim_set(st_io, "in", pgpgin);
122         rrddim_set(st_io, "out", pgpgout);
123         rrdset_done(st_io);
124     }
125
126     // --------------------------------------------------------------------
127
128     if(do_pgfaults) {
129         static RRDSET *st_pgfaults = NULL;
130         if(unlikely(!st_pgfaults)) {
131             st_pgfaults = rrdset_create_localhost("mem", "pgfaults", NULL, "system", NULL, "Memory Page Faults"
132                                                   , "page faults/s", 500, update_every, RRDSET_TYPE_LINE);
133             rrdset_flag_set(st_pgfaults, RRDSET_FLAG_DETAIL);
134
135             rrddim_add(st_pgfaults, "minor",  NULL,  1, 1, RRD_ALGORITHM_INCREMENTAL);
136             rrddim_add(st_pgfaults, "major", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
137         }
138         else rrdset_next(st_pgfaults);
139
140         rrddim_set(st_pgfaults, "minor", pgfault);
141         rrddim_set(st_pgfaults, "major", pgmajfault);
142         rrdset_done(st_pgfaults);
143     }
144
145     // --------------------------------------------------------------------
146
147     // Ondemand criteria for NUMA. Since this won't change at run time, we
148     // check it only once. We check whether the node count is >= 2 because
149     // single-node systems have uninteresting statistics (since all accesses
150     // are local).
151     if(unlikely(has_numa == -1))
152         has_numa = (numa_local || numa_foreign || numa_interleave || numa_other || numa_pte_updates ||
153                      numa_huge_pte_updates || numa_hint_faults || numa_hint_faults_local || numa_pages_migrated) ? 1 : 0;
154
155     if(do_numa == CONFIG_BOOLEAN_YES || (do_numa == CONFIG_BOOLEAN_AUTO && has_numa)) {
156         do_numa = CONFIG_BOOLEAN_YES;
157
158         static RRDSET *st_numa = NULL;
159         if(unlikely(!st_numa)) {
160             st_numa = rrdset_create_localhost("mem", "numa", NULL, "numa", NULL, "NUMA events", "events/s", 800
161                                               , update_every, RRDSET_TYPE_LINE);
162             rrdset_flag_set(st_numa, RRDSET_FLAG_DETAIL);
163
164             // These depend on CONFIG_NUMA in the kernel.
165             rrddim_add(st_numa, "local", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
166             rrddim_add(st_numa, "foreign", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
167             rrddim_add(st_numa, "interleave", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
168             rrddim_add(st_numa, "other", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
169
170             // The following stats depend on CONFIG_NUMA_BALANCING in the
171             // kernel.
172             rrddim_add(st_numa, "pte updates", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
173             rrddim_add(st_numa, "huge pte updates", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
174             rrddim_add(st_numa, "hint faults", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
175             rrddim_add(st_numa, "hint faults local", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
176             rrddim_add(st_numa, "pages migrated", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
177         }
178         else rrdset_next(st_numa);
179
180         rrddim_set(st_numa, "local", numa_local);
181         rrddim_set(st_numa, "foreign", numa_foreign);
182         rrddim_set(st_numa, "interleave", numa_interleave);
183         rrddim_set(st_numa, "other", numa_other);
184
185         rrddim_set(st_numa, "pte updates", numa_pte_updates);
186         rrddim_set(st_numa, "huge pte updates", numa_huge_pte_updates);
187         rrddim_set(st_numa, "hint faults", numa_hint_faults);
188         rrddim_set(st_numa, "hint faults local", numa_hint_faults_local);
189         rrddim_set(st_numa, "pages migrated", numa_pages_migrated);
190
191         rrdset_done(st_numa);
192     }
193
194     return 0;
195 }
196