2 * collectd - src/vmem.c
3 * Copyright (C) 2008-2010 Florian octo Forster
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Florian octo Forster <octo at collectd.org>
33 static const char *config_keys[] = {"Verbose"};
34 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
36 static int verbose_output = 0;
37 /* #endif KERNEL_LINUX */
40 #error "No applicable input method."
41 #endif /* HAVE_LIBSTATGRAB */
43 static void submit(const char *plugin_instance, const char *type,
44 const char *type_instance, value_t *values, int values_len) {
45 value_list_t vl = VALUE_LIST_INIT;
48 vl.values_len = values_len;
50 sstrncpy(vl.plugin, "vmem", sizeof(vl.plugin));
51 if (plugin_instance != NULL)
52 sstrncpy(vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance));
53 sstrncpy(vl.type, type, sizeof(vl.type));
54 if (type_instance != NULL)
55 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
57 plugin_dispatch_values(&vl);
58 } /* void vmem_submit */
60 static void submit_two(const char *plugin_instance, const char *type,
61 const char *type_instance, derive_t c0, derive_t c1) {
63 {.derive = c0}, {.derive = c1},
66 submit(plugin_instance, type, type_instance, values,
67 STATIC_ARRAY_SIZE(values));
68 } /* void submit_one */
70 static void submit_one(const char *plugin_instance, const char *type,
71 const char *type_instance, value_t value) {
72 submit(plugin_instance, type, type_instance, &value, 1);
73 } /* void submit_one */
75 static int vmem_config(const char *key, const char *value) {
76 if (strcasecmp("Verbose", key) == 0) {
86 } /* int vmem_config */
88 static int vmem_read(void) {
99 derive_t pgmajfault = 0;
100 int pgfaultvalid = 0;
105 fh = fopen("/proc/vmstat", "r");
108 ERROR("vmem plugin: fopen (/proc/vmstat) failed: %s",
109 sstrerror(errno, errbuf, sizeof(errbuf)));
113 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
121 fields_num = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
128 counter = strtoll(fields[1], &endptr, 10);
129 if (fields[1] == endptr)
133 gauge = strtod(fields[1], &endptr);
134 if (fields[1] == endptr)
140 * The total number of {inst} pages, e. g dirty pages.
142 if (strncmp("nr_", key, strlen("nr_")) == 0) {
143 char *inst = key + strlen("nr_");
144 if (strcmp(inst, "dirtied") == 0 || strcmp(inst, "written") == 0) {
145 value_t value = {.derive = counter};
146 submit_one(NULL, "vmpage_action", inst, value);
148 value_t value = {.gauge = gauge};
149 submit_one(NULL, "vmpage_number", inst, value);
154 * Page in and page outs. For memory and swap.
156 else if (strcmp("pgpgin", key) == 0) {
159 } else if (strcmp("pgpgout", key) == 0) {
162 } else if (strcmp("pswpin", key) == 0) {
165 } else if (strcmp("pswpout", key) == 0) {
173 else if (strcmp("pgfault", key) == 0) {
175 pgfaultvalid |= 0x01;
176 } else if (strcmp("pgmajfault", key) == 0) {
177 pgmajfault = counter;
178 pgfaultvalid |= 0x02;
182 * Skip the other statistics if verbose output is disabled.
184 else if (verbose_output == 0)
188 * Number of page allocations, refills, steals and scans. This is collected
189 * ``per zone'', i. e. for DMA, DMA32, normal and possibly highmem.
191 else if (strncmp("pgalloc_", key, strlen("pgalloc_")) == 0) {
192 char *inst = key + strlen("pgalloc_");
193 value_t value = {.derive = counter};
194 submit_one(inst, "vmpage_action", "alloc", value);
195 } else if (strncmp("pgrefill_", key, strlen("pgrefill_")) == 0) {
196 char *inst = key + strlen("pgrefill_");
197 value_t value = {.derive = counter};
198 submit_one(inst, "vmpage_action", "refill", value);
199 } else if (strncmp("pgsteal_kswapd_", key, strlen("pgsteal_kswapd_")) ==
201 char *inst = key + strlen("pgsteal_kswapd_");
202 value_t value = {.derive = counter};
203 submit_one(inst, "vmpage_action", "steal_kswapd", value);
204 } else if (strncmp("pgsteal_direct_", key, strlen("pgsteal_direct_")) ==
206 char *inst = key + strlen("pgsteal_direct_");
207 value_t value = {.derive = counter};
208 submit_one(inst, "vmpage_action", "steal_direct", value);
210 /* For backwards compatibility (somewhen before 4.2.3) */
211 else if (strncmp("pgsteal_", key, strlen("pgsteal_")) == 0) {
212 char *inst = key + strlen("pgsteal_");
213 value_t value = {.derive = counter};
214 submit_one(inst, "vmpage_action", "steal", value);
215 } else if (strncmp("pgscan_kswapd_", key, strlen("pgscan_kswapd_")) == 0) {
216 char *inst = key + strlen("pgscan_kswapd_");
217 value_t value = {.derive = counter};
218 submit_one(inst, "vmpage_action", "scan_kswapd", value);
219 } else if (strncmp("pgscan_direct_", key, strlen("pgscan_direct_")) == 0) {
220 char *inst = key + strlen("pgscan_direct_");
221 value_t value = {.derive = counter};
222 submit_one(inst, "vmpage_action", "scan_direct", value);
228 * number of pages moved to the active or inactive lists and freed, i. e.
229 * removed from either list.
231 else if (strcmp("pgfree", key) == 0) {
232 value_t value = {.derive = counter};
233 submit_one(NULL, "vmpage_action", "free", value);
234 } else if (strcmp("pgactivate", key) == 0) {
235 value_t value = {.derive = counter};
236 submit_one(NULL, "vmpage_action", "activate", value);
237 } else if (strcmp("pgdeactivate", key) == 0) {
238 value_t value = {.derive = counter};
239 submit_one(NULL, "vmpage_action", "deactivate", value);
241 } /* while (fgets) */
246 if (pgfaultvalid == 0x03)
247 submit_two(NULL, "vmpage_faults", NULL, pgfault, pgmajfault);
249 if (pgpgvalid == 0x03)
250 submit_two(NULL, "vmpage_io", "memory", pgpgin, pgpgout);
252 if (pswpvalid == 0x03)
253 submit_two(NULL, "vmpage_io", "swap", pswpin, pswpout);
254 #endif /* KERNEL_LINUX */
257 } /* int vmem_read */
259 void module_register(void) {
260 plugin_register_config("vmem", vmem_config, config_keys, config_keys_num);
261 plugin_register_read("vmem", vmem_read);
262 } /* void module_register */