]> arthur.barton.de Git - netdata.git/blob - src/proc_vmstat.c
748fa3486da5fb7751cc800ea1ff7ec549929798
[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_ONDEMAND_ONDEMAND);
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_ONDEMAND_ONDEMAND);
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_ONDEMAND_YES || (do_numa == CONFIG_ONDEMAND_ONDEMAND && 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_ONDEMAND_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_ONDEMAND_YES) {
91         do_swapio = CONFIG_ONDEMAND_YES;
92
93         static RRDSET *st_swapio = NULL;
94         if(unlikely(!st_swapio)) {
95             st_swapio = rrdset_create("system", "swapio", NULL, "swap", NULL, "Swap I/O", "kilobytes/s", 250, update_every, RRDSET_TYPE_AREA);
96
97             rrddim_add(st_swapio, "in",  NULL, sysconf(_SC_PAGESIZE), 1024, RRDDIM_INCREMENTAL);
98             rrddim_add(st_swapio, "out", NULL, -sysconf(_SC_PAGESIZE), 1024, RRDDIM_INCREMENTAL);
99         }
100         else rrdset_next(st_swapio);
101
102         rrddim_set(st_swapio, "in", pswpin);
103         rrddim_set(st_swapio, "out", pswpout);
104         rrdset_done(st_swapio);
105     }
106
107     // --------------------------------------------------------------------
108
109     if(do_io) {
110         static RRDSET *st_io = NULL;
111         if(unlikely(!st_io)) {
112             st_io = rrdset_create("system", "io", NULL, "disk", NULL, "Disk I/O", "kilobytes/s", 150, update_every, RRDSET_TYPE_AREA);
113
114             rrddim_add(st_io, "in",  NULL,  1, 1, RRDDIM_INCREMENTAL);
115             rrddim_add(st_io, "out", NULL, -1, 1, RRDDIM_INCREMENTAL);
116         }
117         else rrdset_next(st_io);
118
119         rrddim_set(st_io, "in", pgpgin);
120         rrddim_set(st_io, "out", pgpgout);
121         rrdset_done(st_io);
122     }
123
124     // --------------------------------------------------------------------
125
126     if(do_pgfaults) {
127         static RRDSET *st_pgfaults = NULL;
128         if(unlikely(!st_pgfaults)) {
129             st_pgfaults = rrdset_create("mem", "pgfaults", NULL, "system", NULL, "Memory Page Faults", "page faults/s", 500, update_every, RRDSET_TYPE_LINE);
130             st_pgfaults->isdetail = 1;
131
132             rrddim_add(st_pgfaults, "minor",  NULL,  1, 1, RRDDIM_INCREMENTAL);
133             rrddim_add(st_pgfaults, "major", NULL, -1, 1, RRDDIM_INCREMENTAL);
134         }
135         else rrdset_next(st_pgfaults);
136
137         rrddim_set(st_pgfaults, "minor", pgfault);
138         rrddim_set(st_pgfaults, "major", pgmajfault);
139         rrdset_done(st_pgfaults);
140     }
141
142     // --------------------------------------------------------------------
143
144     // Ondemand criteria for NUMA. Since this won't change at run time, we
145     // check it only once. We check whether the node count is >= 2 because
146     // single-node systems have uninteresting statistics (since all accesses
147     // are local).
148     if(unlikely(has_numa == -1))
149         has_numa = (numa_local || numa_foreign || numa_interleave || numa_other || numa_pte_updates ||
150                      numa_huge_pte_updates || numa_hint_faults || numa_hint_faults_local || numa_pages_migrated) ? 1 : 0;
151
152     if(do_numa == CONFIG_ONDEMAND_YES || (do_numa == CONFIG_ONDEMAND_ONDEMAND && has_numa)) {
153         do_numa = CONFIG_ONDEMAND_YES;
154
155         static RRDSET *st_numa = NULL;
156         if(unlikely(!st_numa)) {
157             st_numa = rrdset_create("mem", "numa", NULL, "numa", NULL, "NUMA events", "events/s", 800, update_every, RRDSET_TYPE_LINE);
158             st_numa->isdetail = 1;
159
160             // These depend on CONFIG_NUMA in the kernel.
161             rrddim_add(st_numa, "local", NULL, 1, 1, RRDDIM_INCREMENTAL);
162             rrddim_add(st_numa, "foreign", NULL, 1, 1, RRDDIM_INCREMENTAL);
163             rrddim_add(st_numa, "interleave", NULL, 1, 1, RRDDIM_INCREMENTAL);
164             rrddim_add(st_numa, "other", NULL, 1, 1, RRDDIM_INCREMENTAL);
165
166             // The following stats depend on CONFIG_NUMA_BALANCING in the
167             // kernel.
168             rrddim_add(st_numa, "pte updates", NULL, 1, 1, RRDDIM_INCREMENTAL);
169             rrddim_add(st_numa, "huge pte updates", NULL, 1, 1, RRDDIM_INCREMENTAL);
170             rrddim_add(st_numa, "hint faults", NULL, 1, 1, RRDDIM_INCREMENTAL);
171             rrddim_add(st_numa, "hint faults local", NULL, 1, 1, RRDDIM_INCREMENTAL);
172             rrddim_add(st_numa, "pages migrated", NULL, 1, 1, RRDDIM_INCREMENTAL);
173         }
174         else rrdset_next(st_numa);
175
176         rrddim_set(st_numa, "local", numa_local);
177         rrddim_set(st_numa, "foreign", numa_foreign);
178         rrddim_set(st_numa, "interleave", numa_interleave);
179         rrddim_set(st_numa, "other", numa_other);
180
181         rrddim_set(st_numa, "pte updates", numa_pte_updates);
182         rrddim_set(st_numa, "huge pte updates", numa_huge_pte_updates);
183         rrddim_set(st_numa, "hint faults", numa_hint_faults);
184         rrddim_set(st_numa, "hint faults local", numa_hint_faults_local);
185         rrddim_set(st_numa, "pages migrated", numa_pages_migrated);
186
187         rrdset_done(st_numa);
188     }
189
190     return 0;
191 }
192