1 /**
2 * collectd - src/vmem.c
3 * Copyright (C) 2008-2010 Florian octo Forster
4 *
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:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
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.
22 *
23 * Authors:
24 * Florian octo Forster <octo at collectd.org>
25 **/
27 #include "collectd.h"
29 #include "common.h"
30 #include "plugin.h"
32 #if KERNEL_LINUX
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 */
39 #else
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;
47 vl.values = values;
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) {
62 value_t values[] = {
63 {.derive = c0}, {.derive = c1},
64 };
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) {
77 if (IS_TRUE(value))
78 verbose_output = 1;
79 else
80 verbose_output = 0;
81 } else {
82 return (-1);
83 }
85 return (0);
86 } /* int vmem_config */
88 static int vmem_read(void) {
89 #if KERNEL_LINUX
90 derive_t pgpgin = 0;
91 derive_t pgpgout = 0;
92 int pgpgvalid = 0;
94 derive_t pswpin = 0;
95 derive_t pswpout = 0;
96 int pswpvalid = 0;
98 derive_t pgfault = 0;
99 derive_t pgmajfault = 0;
100 int pgfaultvalid = 0;
102 FILE *fh;
103 char buffer[1024];
105 fh = fopen("/proc/vmstat", "r");
106 if (fh == NULL) {
107 char errbuf[1024];
108 ERROR("vmem plugin: fopen (/proc/vmstat) failed: %s",
109 sstrerror(errno, errbuf, sizeof(errbuf)));
110 return (-1);
111 }
113 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
114 char *fields[4];
115 int fields_num;
116 char *key;
117 char *endptr;
118 derive_t counter;
119 gauge_t gauge;
121 fields_num = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
122 if (fields_num != 2)
123 continue;
125 key = fields[0];
127 endptr = NULL;
128 counter = strtoll(fields[1], &endptr, 10);
129 if (fields[1] == endptr)
130 continue;
132 endptr = NULL;
133 gauge = strtod(fields[1], &endptr);
134 if (fields[1] == endptr)
135 continue;
137 /*
138 * Number of pages
139 *
140 * The total number of {inst} pages, e. g dirty pages.
141 */
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);
147 } else {
148 value_t value = {.gauge = gauge};
149 submit_one(NULL, "vmpage_number", inst, value);
150 }
151 }
153 /*
154 * Page in and page outs. For memory and swap.
155 */
156 else if (strcmp("pgpgin", key) == 0) {
157 pgpgin = counter;
158 pgpgvalid |= 0x01;
159 } else if (strcmp("pgpgout", key) == 0) {
160 pgpgout = counter;
161 pgpgvalid |= 0x02;
162 } else if (strcmp("pswpin", key) == 0) {
163 pswpin = counter;
164 pswpvalid |= 0x01;
165 } else if (strcmp("pswpout", key) == 0) {
166 pswpout = counter;
167 pswpvalid |= 0x02;
168 }
170 /*
171 * Pagefaults
172 */
173 else if (strcmp("pgfault", key) == 0) {
174 pgfault = counter;
175 pgfaultvalid |= 0x01;
176 } else if (strcmp("pgmajfault", key) == 0) {
177 pgmajfault = counter;
178 pgfaultvalid |= 0x02;
179 }
181 /*
182 * Skip the other statistics if verbose output is disabled.
183 */
184 else if (verbose_output == 0)
185 continue;
187 /*
188 * Number of page allocations, refills, steals and scans. This is collected
189 * ``per zone'', i. e. for DMA, DMA32, normal and possibly highmem.
190 */
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_")) ==
200 0) {
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_")) ==
205 0) {
206 char *inst = key + strlen("pgsteal_direct_");
207 value_t value = {.derive = counter};
208 submit_one(inst, "vmpage_action", "steal_direct", value);
209 }
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);
223 }
225 /*
226 * Page action
227 *
228 * number of pages moved to the active or inactive lists and freed, i. e.
229 * removed from either list.
230 */
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);
240 }
241 } /* while (fgets) */
243 fclose(fh);
244 fh = NULL;
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 */
256 return (0);
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 */