2 * collectd - src/vmem.c
3 * Copyright (C) 2008 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Florian octo Forster <octo at verplant.org>
27 /* No global variables */
28 /* #endif KERNEL_LINUX */
31 # error "No applicable input method."
32 #endif /* HAVE_LIBSTATGRAB */
34 static void submit (const char *plugin_instance, const char *type,
35 const char *type_instance, value_t *values, int values_len)
37 value_list_t vl = VALUE_LIST_INIT;
40 vl.values_len = values_len;
42 vl.time = time (NULL);
43 strcpy (vl.host, hostname_g);
44 strcpy (vl.plugin, "vmem");
45 if (plugin_instance != NULL)
46 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
47 if (type_instance != NULL)
48 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
50 plugin_dispatch_values (type, &vl);
51 } /* void vmem_submit */
53 static void submit_two (const char *plugin_instance, const char *type,
54 const char *type_instance, counter_t c0, counter_t c1)
58 values[0].counter = c0;
59 values[1].counter = c1;
61 submit (plugin_instance, type, type_instance, values, 2);
62 } /* void submit_one */
64 static void submit_one (const char *plugin_instance, const char *type,
65 const char *type_instance, value_t value)
67 submit (plugin_instance, type, type_instance, &value, 1);
68 } /* void submit_one */
70 static int vmem_read (void)
74 counter_t pgpgout = 0;
78 counter_t pswpout = 0;
81 counter_t pgfault = 0;
82 counter_t pgmajfault = 0;
88 fh = fopen ("/proc/vmstat", "r");
92 ERROR ("vmem plugin: fopen (/proc/vmstat) failed: %s",
93 sstrerror (errno, errbuf, sizeof (errbuf)));
97 while (fgets (buffer, sizeof (buffer), fh) != NULL)
106 fields_num = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
113 counter = strtoll (fields[1], &endptr, 10);
114 if (fields[1] == endptr)
118 gauge = strtod (fields[1], &endptr);
119 if (fields[1] == endptr)
125 * The total number of {inst} pages, e. g dirty pages.
127 if (strncmp ("nr_", key, strlen ("nr_")) == 0)
129 char *inst = key + strlen ("nr_");
130 value_t value = { .gauge = gauge };
131 submit_one (NULL, "vmpage_number", inst, value);
135 * Number of page allocations, refills, steals and scans. This is collected
136 * ``per zone'', i. e. for DMA, DMA32, normal and possibly highmem.
138 else if (strncmp ("pgalloc_", key, strlen ("pgalloc_")) == 0)
140 char *inst = key + strlen ("pgalloc_");
141 value_t value = { .counter = counter };
142 submit_one (inst, "vmpage_action", "alloc", value);
144 else if (strncmp ("pgrefill_", key, strlen ("pgrefill_")) == 0)
146 char *inst = key + strlen ("pgrefill_");
147 value_t value = { .counter = counter };
148 submit_one (inst, "vmpage_action", "refill", value);
150 else if (strncmp ("pgsteal_", key, strlen ("pgsteal_")) == 0)
152 char *inst = key + strlen ("pgsteal_");
153 value_t value = { .counter = counter };
154 submit_one (inst, "vmpage_action", "steal", value);
156 else if (strncmp ("pgscan_kswapd_", key, strlen ("pgscan_kswapd_")) == 0)
158 char *inst = key + strlen ("pgscan_kswapd_");
159 value_t value = { .counter = counter };
160 submit_one (inst, "vmpage_action", "scan_kswapd", value);
162 else if (strncmp ("pgscan_direct_", key, strlen ("pgscan_direct_")) == 0)
164 char *inst = key + strlen ("pgscan_direct_");
165 value_t value = { .counter = counter };
166 submit_one (inst, "vmpage_action", "scan_direct", value);
170 * Page in and page outs. For memory and swap.
172 else if (strcmp ("pgpgin", key) == 0)
177 else if (strcmp ("pgpgout", key) == 0)
182 else if (strcmp ("pswpin", key) == 0)
187 else if (strcmp ("pswpout", key) == 0)
196 else if (strcmp ("pgfault", key) == 0)
199 pgfaultvalid |= 0x01;
201 else if (strcmp ("pgmajfault", key) == 0)
203 pgmajfault = counter;
204 pgfaultvalid |= 0x02;
210 * number of pages moved to the active or inactive lists and freed, i. e.
211 * removed from either list.
213 else if (strcmp ("pgfree", key) == 0)
215 value_t value = { .counter = counter };
216 submit_one (NULL, "vmpage_action", "free", value);
218 else if (strcmp ("pgactivate", key) == 0)
220 value_t value = { .counter = counter };
221 submit_one (NULL, "vmpage_action", "activate", value);
223 else if (strcmp ("pgdeactivate", key) == 0)
225 value_t value = { .counter = counter };
226 submit_one (NULL, "vmpage_action", "deactivate", value);
228 } /* while (fgets) */
233 if (pgfaultvalid == 0x03)
234 submit_two (NULL, "vmpage_faults", NULL, pgfault, pgmajfault);
236 if (pgpgvalid == 0x03)
237 submit_two (NULL, "vmpage_io", "memory", pgpgin, pgpgout);
239 if (pswpvalid == 0x03)
240 submit_two (NULL, "vmpage_io", "swap", pswpin, pswpout);
241 #endif /* KERNEL_LINUX */
244 } /* int vmem_read */
246 void module_register (void)
248 plugin_register_read ("vmem", vmem_read);
249 } /* void module_register */
251 /* vim: set sw=2 sts=2 ts=8 : */