1 /*-
2 * collectd - src/hugepages.c
3 * MIT License
4 *
5 * Copyright(c) 2016 Intel Corporation. All rights reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 * Authors:
26 * Jaroslav Safka <jaroslavx.safka@intel.com>
27 * Kim-Marie Jones <kim-marie.jones@intel.com>
28 * Florian Forster <octo at collectd.org>
29 */
31 #include "collectd.h"
33 #include "common.h" /* auxiliary functions */
34 #include "plugin.h" /* plugin_register_*, plugin_dispatch_values */
36 static const char g_plugin_name[] = "hugepages";
38 static _Bool g_flag_rpt_numa = 1;
39 static _Bool g_flag_rpt_mm = 1;
41 static _Bool g_values_pages = 1;
42 static _Bool g_values_bytes = 0;
43 static _Bool g_values_percent = 0;
45 #define HP_HAVE_NR 0x01
46 #define HP_HAVE_SURPLUS 0x02
47 #define HP_HAVE_FREE 0x04
48 #define HP_HAVE_ALL 0x07
50 struct entry_info {
51 char *d_name;
52 const char *node;
53 size_t page_size_kb;
55 gauge_t nr;
56 gauge_t surplus;
57 gauge_t free;
58 uint8_t flags;
59 };
61 static int hp_config(oconfig_item_t *ci) {
62 for (int i = 0; i < ci->children_num; i++) {
63 oconfig_item_t *child = ci->children + i;
64 if (strcasecmp("ReportPerNodeHP", child->key) == 0)
65 cf_util_get_boolean(child, &g_flag_rpt_numa);
66 else if (strcasecmp("ReportRootHP", child->key) == 0)
67 cf_util_get_boolean(child, &g_flag_rpt_mm);
68 else if (strcasecmp("ValuesPages", child->key) == 0)
69 cf_util_get_boolean(child, &g_values_pages);
70 else if (strcasecmp("ValuesBytes", child->key) == 0)
71 cf_util_get_boolean(child, &g_values_bytes);
72 else if (strcasecmp("ValuesPercentage", child->key) == 0)
73 cf_util_get_boolean(child, &g_values_percent);
74 else
75 ERROR("%s: Invalid configuration option: \"%s\".", g_plugin_name,
76 child->key);
77 }
79 return (0);
80 }
82 static void submit_hp(const struct entry_info *info) {
83 value_list_t vl = VALUE_LIST_INIT;
85 vl.values = &(value_t) { .gauge = NAN };
86 vl.values_len = 1;
88 sstrncpy(vl.host, hostname_g, sizeof(vl.host));
89 sstrncpy(vl.plugin, g_plugin_name, sizeof(vl.plugin));
90 if (info->node) {
91 ssnprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%s-%zuKb",
92 info->node, info->page_size_kb);
93 } else {
94 ssnprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%zuKb",
95 info->page_size_kb);
96 }
98 /* ensure all metrics have the same timestamp */
99 vl.time = cdtime();
101 gauge_t free = info->free;
102 gauge_t used = (info->nr + info->surplus) - info->free;
104 if (g_values_pages) {
105 sstrncpy(vl.type, "vmpage_number", sizeof(vl.type));
106 plugin_dispatch_multivalue(&vl, /* store_percentage = */ 0, DS_TYPE_GAUGE,
107 "free", free, "used", used, NULL);
108 }
109 if (g_values_bytes) {
110 gauge_t page_size = (gauge_t)(1024 * info->page_size_kb);
111 sstrncpy(vl.type, "memory", sizeof(vl.type));
112 plugin_dispatch_multivalue(&vl, /* store_percentage = */ 0, DS_TYPE_GAUGE,
113 "free", free * page_size, "used",
114 used * page_size, NULL);
115 }
116 if (g_values_percent) {
117 sstrncpy(vl.type, "percent", sizeof(vl.type));
118 plugin_dispatch_multivalue(&vl, /* store_percentage = */ 1, DS_TYPE_GAUGE,
119 "free", free, "used", used, NULL);
120 }
121 }
123 static int read_hugepage_entry(const char *path, const char *entry,
124 void *e_info) {
125 char path2[PATH_MAX];
126 struct entry_info *info = e_info;
127 double value;
129 ssnprintf(path2, sizeof(path2), "%s/%s", path, entry);
131 FILE *fh = fopen(path2, "rt");
132 if (fh == NULL) {
133 ERROR("%s: cannot open %s", g_plugin_name, path2);
134 return -1;
135 }
137 if (fscanf(fh, "%lf", &value) != 1) {
138 ERROR("%s: cannot parse file %s", g_plugin_name, path2);
139 fclose(fh);
140 return -1;
141 }
142 fclose(fh);
144 if (strcmp(entry, "nr_hugepages") == 0) {
145 info->nr = value;
146 info->flags |= HP_HAVE_NR;
147 } else if (strcmp(entry, "surplus_hugepages") == 0) {
148 info->surplus = value;
149 info->flags |= HP_HAVE_SURPLUS;
150 } else if (strcmp(entry, "free_hugepages") == 0) {
151 info->free = value;
152 info->flags |= HP_HAVE_FREE;
153 }
155 if (info->flags != HP_HAVE_ALL) {
156 return 0;
157 }
159 submit_hp(info);
161 /* Reset flags so subsequent calls don't submit again. */
162 info->flags = 0;
163 return 0;
164 }
166 static int read_syshugepages(const char *path, const char *node) {
167 static const char hugepages_dir[] = "hugepages-";
168 DIR *dir;
169 struct dirent *result;
170 char path2[PATH_MAX];
172 dir = opendir(path);
173 if (dir == NULL) {
174 ERROR("%s: cannot open directory %s", g_plugin_name, path);
175 return -1;
176 }
178 /* read "hugepages-XXXXXkB" entries */
179 while ((result = readdir(dir)) != NULL) {
180 if (strncmp(result->d_name, hugepages_dir, sizeof(hugepages_dir) - 1)) {
181 /* not node dir */
182 errno = 0;
183 continue;
184 }
186 long page_size = strtol(result->d_name + strlen(hugepages_dir),
187 /* endptr = */ NULL, /* base = */ 10);
188 if (errno != 0) {
189 char errbuf[1024];
190 ERROR("%s: failed to determine page size from directory name \"%s\": %s",
191 g_plugin_name, result->d_name,
192 sstrerror(errno, errbuf, sizeof(errbuf)));
193 continue;
194 }
196 /* /sys/devices/system/node/node?/hugepages/ */
197 ssnprintf(path2, sizeof(path2), "%s/%s", path, result->d_name);
199 walk_directory(path2, read_hugepage_entry,
200 &(struct entry_info){
201 .d_name = result->d_name,
202 .node = node,
203 .page_size_kb = (size_t)page_size,
204 },
205 /* hidden = */ 0);
206 errno = 0;
207 }
209 /* Check if NULL return from readdir() was an error */
210 if (errno != 0) {
211 ERROR("%s: readdir failed", g_plugin_name);
212 closedir(dir);
213 return -1;
214 }
216 closedir(dir);
217 return 0;
218 }
220 static int read_nodes(void) {
221 static const char sys_node[] = "/sys/devices/system/node";
222 static const char node_string[] = "node";
223 static const char sys_node_hugepages[] =
224 "/sys/devices/system/node/%s/hugepages";
225 DIR *dir;
226 struct dirent *result;
227 char path[PATH_MAX];
229 dir = opendir(sys_node);
230 if (dir == NULL) {
231 ERROR("%s: cannot open directory %s", g_plugin_name, sys_node);
232 return -1;
233 }
235 while ((result = readdir(dir)) != NULL) {
236 if (strncmp(result->d_name, node_string, sizeof(node_string) - 1)) {
237 /* not node dir */
238 errno = 0;
239 continue;
240 }
242 ssnprintf(path, sizeof(path), sys_node_hugepages, result->d_name);
243 read_syshugepages(path, result->d_name);
244 errno = 0;
245 }
247 /* Check if NULL return from readdir() was an error */
248 if (errno != 0) {
249 ERROR("%s: readdir failed", g_plugin_name);
250 closedir(dir);
251 return -1;
252 }
254 closedir(dir);
255 return 0;
256 }
258 static int huge_read(void) {
259 static const char sys_mm_hugepages[] = "/sys/kernel/mm/hugepages";
261 if (g_flag_rpt_mm) {
262 if (read_syshugepages(sys_mm_hugepages, "mm") != 0) {
263 return -1;
264 }
265 }
266 if (g_flag_rpt_numa) {
267 if (read_nodes() != 0) {
268 return -1;
269 }
270 }
272 return 0;
273 }
275 void module_register(void) {
276 plugin_register_complex_config(g_plugin_name, hp_config);
277 plugin_register_read(g_plugin_name, huge_read);
278 }