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;
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");
107 ERROR("vmem plugin: fopen (/proc/vmstat) failed: %s", STRERRNO);
111 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
119 fields_num = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
126 counter = strtoll(fields[1], &endptr, 10);
127 if (fields[1] == endptr)
131 gauge = strtod(fields[1], &endptr);
132 if (fields[1] == endptr)
138 * The total number of {inst} pages, e. g dirty pages.
140 if (strncmp("nr_", key, strlen("nr_")) == 0) {
141 char *inst = key + strlen("nr_");
142 if (strcmp(inst, "dirtied") == 0 || strcmp(inst, "written") == 0) {
143 value_t value = {.derive = counter};
144 submit_one(NULL, "vmpage_action", inst, value);
146 value_t value = {.gauge = gauge};
147 submit_one(NULL, "vmpage_number", inst, value);
152 * Page in and page outs. For memory and swap.
154 else if (strcmp("pgpgin", key) == 0) {
157 } else if (strcmp("pgpgout", key) == 0) {
160 } else if (strcmp("pswpin", key) == 0) {
163 } else if (strcmp("pswpout", key) == 0) {
171 else if (strcmp("pgfault", key) == 0) {
173 pgfaultvalid |= 0x01;
174 } else if (strcmp("pgmajfault", key) == 0) {
175 pgmajfault = counter;
176 pgfaultvalid |= 0x02;
180 * Skip the other statistics if verbose output is disabled.
182 else if (verbose_output == 0)
186 * Number of page allocations, refills, steals and scans. This is collected
187 * ``per zone'', i. e. for DMA, DMA32, normal and possibly highmem.
189 else if (strncmp("pgalloc_", key, strlen("pgalloc_")) == 0) {
190 char *inst = key + strlen("pgalloc_");
191 value_t value = {.derive = counter};
192 submit_one(inst, "vmpage_action", "alloc", value);
193 } else if (strncmp("pgrefill_", key, strlen("pgrefill_")) == 0) {
194 char *inst = key + strlen("pgrefill_");
195 value_t value = {.derive = counter};
196 submit_one(inst, "vmpage_action", "refill", value);
197 } else if (strncmp("pgsteal_kswapd_", key, strlen("pgsteal_kswapd_")) ==
199 char *inst = key + strlen("pgsteal_kswapd_");
200 value_t value = {.derive = counter};
201 submit_one(inst, "vmpage_action", "steal_kswapd", value);
202 } else if (strncmp("pgsteal_direct_", key, strlen("pgsteal_direct_")) ==
204 char *inst = key + strlen("pgsteal_direct_");
205 value_t value = {.derive = counter};
206 submit_one(inst, "vmpage_action", "steal_direct", value);
208 /* For backwards compatibility (somewhen before 4.2.3) */
209 else if (strncmp("pgsteal_", key, strlen("pgsteal_")) == 0) {
210 char *inst = key + strlen("pgsteal_");
211 value_t value = {.derive = counter};
212 submit_one(inst, "vmpage_action", "steal", value);
213 } else if (strncmp("pgscan_kswapd_", key, strlen("pgscan_kswapd_")) == 0) {
214 char *inst = key + strlen("pgscan_kswapd_");
215 value_t value = {.derive = counter};
216 submit_one(inst, "vmpage_action", "scan_kswapd", value);
217 } else if (strncmp("pgscan_direct_", key, strlen("pgscan_direct_")) == 0) {
218 char *inst = key + strlen("pgscan_direct_");
219 value_t value = {.derive = counter};
220 submit_one(inst, "vmpage_action", "scan_direct", value);
226 * number of pages moved to the active or inactive lists and freed, i. e.
227 * removed from either list.
229 else if (strcmp("pgfree", key) == 0) {
230 value_t value = {.derive = counter};
231 submit_one(NULL, "vmpage_action", "free", value);
232 } else if (strcmp("pgactivate", key) == 0) {
233 value_t value = {.derive = counter};
234 submit_one(NULL, "vmpage_action", "activate", value);
235 } else if (strcmp("pgdeactivate", key) == 0) {
236 value_t value = {.derive = counter};
237 submit_one(NULL, "vmpage_action", "deactivate", value);
239 } /* while (fgets) */
244 if (pgfaultvalid == 0x03)
245 submit_two(NULL, "vmpage_faults", NULL, pgfault, pgmajfault);
247 if (pgpgvalid == 0x03)
248 submit_two(NULL, "vmpage_io", "memory", pgpgin, pgpgout);
250 if (pswpvalid == 0x03)
251 submit_two(NULL, "vmpage_io", "swap", pswpin, pswpout);
252 #endif /* KERNEL_LINUX */
255 } /* int vmem_read */
257 void module_register(void) {
258 plugin_register_config("vmem", vmem_config, config_keys, config_keys_num);
259 plugin_register_read("vmem", vmem_read);
260 } /* void module_register */