2 #include "adaptive_resortable_list.h"
4 int do_proc_vmstat(int update_every, usec_t dt) {
7 static procfile *ff = NULL;
8 static int do_swapio = -1, do_io = -1, do_pgfaults = -1, do_numa = -1;
9 static int has_numa = -1;
11 static ARL_BASE *arl_base = NULL;
12 static unsigned long long numa_foreign = 0ULL;
13 static unsigned long long numa_hint_faults = 0ULL;
14 static unsigned long long numa_hint_faults_local = 0ULL;
15 static unsigned long long numa_huge_pte_updates = 0ULL;
16 static unsigned long long numa_interleave = 0ULL;
17 static unsigned long long numa_local = 0ULL;
18 static unsigned long long numa_other = 0ULL;
19 static unsigned long long numa_pages_migrated = 0ULL;
20 static unsigned long long numa_pte_updates = 0ULL;
21 static unsigned long long pgfault = 0ULL;
22 static unsigned long long pgmajfault = 0ULL;
23 static unsigned long long pgpgin = 0ULL;
24 static unsigned long long pgpgout = 0ULL;
25 static unsigned long long pswpin = 0ULL;
26 static unsigned long long pswpout = 0ULL;
28 if(unlikely(!arl_base)) {
29 do_swapio = config_get_boolean_ondemand("plugin:proc:/proc/vmstat", "swap i/o", CONFIG_ONDEMAND_ONDEMAND);
30 do_io = config_get_boolean("plugin:proc:/proc/vmstat", "disk i/o", 1);
31 do_pgfaults = config_get_boolean("plugin:proc:/proc/vmstat", "memory page faults", 1);
32 do_numa = config_get_boolean_ondemand("plugin:proc:/proc/vmstat", "system-wide numa metric summary", CONFIG_ONDEMAND_ONDEMAND);
35 arl_base = arl_create(NULL, 60);
36 arl_expect(arl_base, "pgfault", &pgfault);
37 arl_expect(arl_base, "pgmajfault", &pgmajfault);
38 arl_expect(arl_base, "pgpgin", &pgpgin);
39 arl_expect(arl_base, "pgpgout", &pgpgout);
40 arl_expect(arl_base, "pswpin", &pswpin);
41 arl_expect(arl_base, "pswpout", &pswpout);
43 if(do_numa == CONFIG_ONDEMAND_YES || (do_numa == CONFIG_ONDEMAND_ONDEMAND && get_numa_node_count() >= 2)) {
44 arl_expect(arl_base, "numa_foreign", &numa_foreign);
45 arl_expect(arl_base, "numa_hint_faults_local", &numa_hint_faults_local);
46 arl_expect(arl_base, "numa_hint_faults", &numa_hint_faults);
47 arl_expect(arl_base, "numa_huge_pte_updates", &numa_huge_pte_updates);
48 arl_expect(arl_base, "numa_interleave", &numa_interleave);
49 arl_expect(arl_base, "numa_local", &numa_local);
50 arl_expect(arl_base, "numa_other", &numa_other);
51 arl_expect(arl_base, "numa_pages_migrated", &numa_pages_migrated);
52 arl_expect(arl_base, "numa_pte_updates", &numa_pte_updates);
55 // Do not expect numa metrics when they are not needed.
56 // By not adding them, the ARL will stop processing the file
57 // when all the expected metrics are collected.
58 // Also ARL will not parse their values.
60 do_numa = CONFIG_ONDEMAND_NO;
65 char filename[FILENAME_MAX + 1];
66 snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, "/proc/vmstat");
67 ff = procfile_open(config_get("plugin:proc:/proc/vmstat", "filename to monitor", filename), " \t:", PROCFILE_FLAG_DEFAULT);
68 if(unlikely(!ff)) return 1;
71 ff = procfile_readall(ff);
72 if(unlikely(!ff)) return 0; // we return 0, so that we will retry to open it next time
74 uint32_t lines = procfile_lines(ff), l;
77 for(l = 0; l < lines ;l++) {
78 uint32_t words = procfile_linewords(ff, l);
79 if(unlikely(words < 2)) {
80 if(unlikely(words)) error("Cannot read /proc/vmstat line %u. Expected 2 params, read %u.", l, words);
84 if(unlikely(arl_check(arl_base,
85 procfile_lineword(ff, l, 0),
86 procfile_lineword(ff, l, 1)))) break;
89 // --------------------------------------------------------------------
91 if(pswpin || pswpout || do_swapio == CONFIG_ONDEMAND_YES) {
92 do_swapio = CONFIG_ONDEMAND_YES;
94 static RRDSET *st_swapio = NULL;
95 if(unlikely(!st_swapio)) {
96 st_swapio = rrdset_create("system", "swapio", NULL, "swap", NULL, "Swap I/O", "kilobytes/s", 250, update_every, RRDSET_TYPE_AREA);
98 rrddim_add(st_swapio, "in", NULL, sysconf(_SC_PAGESIZE), 1024, RRDDIM_INCREMENTAL);
99 rrddim_add(st_swapio, "out", NULL, -sysconf(_SC_PAGESIZE), 1024, RRDDIM_INCREMENTAL);
101 else rrdset_next(st_swapio);
103 rrddim_set(st_swapio, "in", pswpin);
104 rrddim_set(st_swapio, "out", pswpout);
105 rrdset_done(st_swapio);
108 // --------------------------------------------------------------------
111 static RRDSET *st_io = NULL;
112 if(unlikely(!st_io)) {
113 st_io = rrdset_create("system", "io", NULL, "disk", NULL, "Disk I/O", "kilobytes/s", 150, update_every, RRDSET_TYPE_AREA);
115 rrddim_add(st_io, "in", NULL, 1, 1, RRDDIM_INCREMENTAL);
116 rrddim_add(st_io, "out", NULL, -1, 1, RRDDIM_INCREMENTAL);
118 else rrdset_next(st_io);
120 rrddim_set(st_io, "in", pgpgin);
121 rrddim_set(st_io, "out", pgpgout);
125 // --------------------------------------------------------------------
128 static RRDSET *st_pgfaults = NULL;
129 if(unlikely(!st_pgfaults)) {
130 st_pgfaults = rrdset_create("mem", "pgfaults", NULL, "system", NULL, "Memory Page Faults", "page faults/s", 500, update_every, RRDSET_TYPE_LINE);
131 st_pgfaults->isdetail = 1;
133 rrddim_add(st_pgfaults, "minor", NULL, 1, 1, RRDDIM_INCREMENTAL);
134 rrddim_add(st_pgfaults, "major", NULL, -1, 1, RRDDIM_INCREMENTAL);
136 else rrdset_next(st_pgfaults);
138 rrddim_set(st_pgfaults, "minor", pgfault);
139 rrddim_set(st_pgfaults, "major", pgmajfault);
140 rrdset_done(st_pgfaults);
143 // --------------------------------------------------------------------
145 // Ondemand criteria for NUMA. Since this won't change at run time, we
146 // check it only once. We check whether the node count is >= 2 because
147 // single-node systems have uninteresting statistics (since all accesses
149 if(unlikely(has_numa == -1))
150 has_numa = (numa_local || numa_foreign || numa_interleave || numa_other || numa_pte_updates ||
151 numa_huge_pte_updates || numa_hint_faults || numa_hint_faults_local || numa_pages_migrated) ? 1 : 0;
153 if(do_numa == CONFIG_ONDEMAND_YES || (do_numa == CONFIG_ONDEMAND_ONDEMAND && has_numa)) {
154 do_numa = CONFIG_ONDEMAND_YES;
156 static RRDSET *st_numa = NULL;
157 if(unlikely(!st_numa)) {
158 st_numa = rrdset_create("mem", "numa", NULL, "numa", NULL, "NUMA events", "events/s", 800, update_every, RRDSET_TYPE_LINE);
159 st_numa->isdetail = 1;
161 // These depend on CONFIG_NUMA in the kernel.
162 rrddim_add(st_numa, "local", NULL, 1, 1, RRDDIM_INCREMENTAL);
163 rrddim_add(st_numa, "foreign", NULL, 1, 1, RRDDIM_INCREMENTAL);
164 rrddim_add(st_numa, "interleave", NULL, 1, 1, RRDDIM_INCREMENTAL);
165 rrddim_add(st_numa, "other", NULL, 1, 1, RRDDIM_INCREMENTAL);
167 // The following stats depend on CONFIG_NUMA_BALANCING in the
169 rrddim_add(st_numa, "pte updates", NULL, 1, 1, RRDDIM_INCREMENTAL);
170 rrddim_add(st_numa, "huge pte updates", NULL, 1, 1, RRDDIM_INCREMENTAL);
171 rrddim_add(st_numa, "hint faults", NULL, 1, 1, RRDDIM_INCREMENTAL);
172 rrddim_add(st_numa, "hint faults local", NULL, 1, 1, RRDDIM_INCREMENTAL);
173 rrddim_add(st_numa, "pages migrated", NULL, 1, 1, RRDDIM_INCREMENTAL);
175 else rrdset_next(st_numa);
177 rrddim_set(st_numa, "local", numa_local);
178 rrddim_set(st_numa, "foreign", numa_foreign);
179 rrddim_set(st_numa, "interleave", numa_interleave);
180 rrddim_set(st_numa, "other", numa_other);
182 rrddim_set(st_numa, "pte updates", numa_pte_updates);
183 rrddim_set(st_numa, "huge pte updates", numa_huge_pte_updates);
184 rrddim_set(st_numa, "hint faults", numa_hint_faults);
185 rrddim_set(st_numa, "hint faults local", numa_hint_faults_local);
186 rrddim_set(st_numa, "pages migrated", numa_pages_migrated);
188 rrdset_done(st_numa);