95cfbaf1ebed1d580787d73f3e3e90d640a70013
[collectd.git] / src / vmem.c
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  **/
26
27 #include "collectd.h"
28
29 #include "common.h"
30 #include "plugin.h"
31
32 #if KERNEL_LINUX
33 static const char *config_keys[] = {"Verbose"};
34 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
35
36 static int verbose_output = 0;
37 /* #endif KERNEL_LINUX */
38
39 #else
40 #error "No applicable input method."
41 #endif /* HAVE_LIBSTATGRAB */
42
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;
46
47   vl.values = values;
48   vl.values_len = values_len;
49
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));
56
57   plugin_dispatch_values(&vl);
58 } /* void vmem_submit */
59
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   };
65
66   submit(plugin_instance, type, type_instance, values,
67          STATIC_ARRAY_SIZE(values));
68 } /* void submit_one */
69
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 */
74
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   }
84
85   return 0;
86 } /* int vmem_config */
87
88 static int vmem_read(void) {
89 #if KERNEL_LINUX
90   derive_t pgpgin = 0;
91   derive_t pgpgout = 0;
92   int pgpgvalid = 0;
93
94   derive_t pswpin = 0;
95   derive_t pswpout = 0;
96   int pswpvalid = 0;
97
98   derive_t pgfault = 0;
99   derive_t pgmajfault = 0;
100   int pgfaultvalid = 0;
101
102   FILE *fh;
103   char buffer[1024];
104
105   fh = fopen("/proc/vmstat", "r");
106   if (fh == NULL) {
107     ERROR("vmem plugin: fopen (/proc/vmstat) failed: %s", STRERRNO);
108     return -1;
109   }
110
111   while (fgets(buffer, sizeof(buffer), fh) != NULL) {
112     char *fields[4];
113     int fields_num;
114     char *key;
115     char *endptr;
116     derive_t counter;
117     gauge_t gauge;
118
119     fields_num = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
120     if (fields_num != 2)
121       continue;
122
123     key = fields[0];
124
125     endptr = NULL;
126     counter = strtoll(fields[1], &endptr, 10);
127     if (fields[1] == endptr)
128       continue;
129
130     endptr = NULL;
131     gauge = strtod(fields[1], &endptr);
132     if (fields[1] == endptr)
133       continue;
134
135     /*
136      * Number of pages
137      *
138      * The total number of {inst} pages, e. g dirty pages.
139      */
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);
145       } else {
146         value_t value = {.gauge = gauge};
147         submit_one(NULL, "vmpage_number", inst, value);
148       }
149     }
150
151     /*
152      * Page in and page outs. For memory and swap.
153      */
154     else if (strcmp("pgpgin", key) == 0) {
155       pgpgin = counter;
156       pgpgvalid |= 0x01;
157     } else if (strcmp("pgpgout", key) == 0) {
158       pgpgout = counter;
159       pgpgvalid |= 0x02;
160     } else if (strcmp("pswpin", key) == 0) {
161       pswpin = counter;
162       pswpvalid |= 0x01;
163     } else if (strcmp("pswpout", key) == 0) {
164       pswpout = counter;
165       pswpvalid |= 0x02;
166     }
167
168     /*
169      * Pagefaults
170      */
171     else if (strcmp("pgfault", key) == 0) {
172       pgfault = counter;
173       pgfaultvalid |= 0x01;
174     } else if (strcmp("pgmajfault", key) == 0) {
175       pgmajfault = counter;
176       pgfaultvalid |= 0x02;
177     }
178
179     /*
180      * Skip the other statistics if verbose output is disabled.
181      */
182     else if (verbose_output == 0)
183       continue;
184
185     /*
186      * Number of page allocations, refills, steals and scans. This is collected
187      * ``per zone'', i. e. for DMA, DMA32, normal and possibly highmem.
188      */
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_")) ==
198                0) {
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_")) ==
203                0) {
204       char *inst = key + strlen("pgsteal_direct_");
205       value_t value = {.derive = counter};
206       submit_one(inst, "vmpage_action", "steal_direct", value);
207     }
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);
221     }
222
223     /*
224      * Page action
225      *
226      * number of pages moved to the active or inactive lists and freed, i. e.
227      * removed from either list.
228      */
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);
238     }
239   } /* while (fgets) */
240
241   fclose(fh);
242   fh = NULL;
243
244   if (pgfaultvalid == 0x03)
245     submit_two(NULL, "vmpage_faults", NULL, pgfault, pgmajfault);
246
247   if (pgpgvalid == 0x03)
248     submit_two(NULL, "vmpage_io", "memory", pgpgin, pgpgout);
249
250   if (pswpvalid == 0x03)
251     submit_two(NULL, "vmpage_io", "swap", pswpin, pswpout);
252 #endif /* KERNEL_LINUX */
253
254   return 0;
255 } /* int vmem_read */
256
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 */