2 * collectd - src/hugepages.c
5 * Copyright(c) 2016 Intel Corporation. All rights reserved.
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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
26 * Jaroslav Safka <jaroslavx.safka@intel.com>
27 * Kim-Marie Jones <kim-marie.jones@intel.com>
31 #include "common.h" /* auxiliary functions */
32 #include "plugin.h" /* plugin_register_*, plugin_dispatch_values */
34 static const char g_plugin_name[] = "hugepages";
35 static const char g_cfg_rpt_numa[] = "ReportPerNodeHP";
36 static const char g_cfg_rpt_mm[] = "ReportRootHP";
38 static const char *g_config_keys[] = {
39 g_cfg_rpt_numa, g_cfg_rpt_mm,
41 static size_t g_config_keys_num = STATIC_ARRAY_SIZE(g_config_keys);
42 static int g_flag_rpt_numa = 1;
43 static int g_flag_rpt_mm = 1;
50 static int huge_config_callback(const char *key, const char *val) {
51 DEBUG("%s: HugePages config key='%s', val='%s'", g_plugin_name, key, val);
53 if (strcasecmp(key, g_cfg_rpt_numa) == 0) {
54 g_flag_rpt_numa = IS_TRUE(val);
57 if (strcasecmp(key, g_cfg_rpt_mm) == 0) {
58 g_flag_rpt_mm = IS_TRUE(val);
65 static void submit_hp(const char *plug_inst, const char *type,
66 const char *type_instance, gauge_t free_value,
68 value_list_t vl = VALUE_LIST_INIT;
70 { .gauge = free_value },
71 { .gauge = used_value },
75 vl.values_len = STATIC_ARRAY_SIZE (values);
76 sstrncpy(vl.host, hostname_g, sizeof(vl.host));
77 sstrncpy(vl.plugin, g_plugin_name, sizeof(vl.plugin));
78 sstrncpy(vl.plugin_instance, plug_inst, sizeof(vl.plugin_instance));
79 sstrncpy(vl.type, type, sizeof(vl.type));
81 if (type_instance != NULL) {
82 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
85 DEBUG("submit_hp pl_inst:%s, inst_type %s, type %s, free=%lf, used=%lf",
86 plug_inst, type_instance, type, free_value, used_value);
88 plugin_dispatch_values(&vl);
91 static int read_hugepage_entry(const char *path, const char *entry,
94 static const char type[] = "hugepages";
95 static const char partial_type_inst[] = "free_used";
96 char type_instance[PATH_MAX];
98 struct entry_info *hpsize_plinst = e_info;
100 static double used_hp = 0;
101 static double free_hp = 0;
104 ssnprintf(path2, sizeof(path2), "%s/%s", path, entry);
106 FILE *fh = fopen(path2, "rt");
108 ERROR("%s: cannot open %s", g_plugin_name, path2);
112 if (fscanf(fh, "%lf", &value) != 1) {
113 ERROR("%s: cannot parse file %s", g_plugin_name, path2);
118 if (strcmp(entry, "nr_hugepages") == 0) {
121 } else if (strcmp(entry, "surplus_hugepages") == 0) {
124 } else if (strcmp(entry, "free_hugepages") == 0) {
131 /* Can now submit "used" and "free" values.
132 * 0x2D is the ASCII "-" character, after which the string
133 * contains "<size>kB"
134 * The string passed as param 3 to submit_hp is of the format:
135 * <type>-<partial_type_inst>-<size>kB
137 strin = strchr(hpsize_plinst->d_name, 0x2D);
139 ssnprintf(type_instance, sizeof(type_instance), "%s%s", partial_type_inst,
142 ssnprintf(type_instance, sizeof(type_instance), "%s%s", partial_type_inst,
143 hpsize_plinst->d_name);
145 submit_hp(hpsize_plinst->node, type, type_instance, free_hp, used_hp);
147 /* Reset for next time */
157 static int read_syshugepages(const char *path, const char *node) {
158 static const char hugepages_dir[] = "hugepages";
160 struct dirent *result;
161 char path2[PATH_MAX];
162 struct entry_info e_info;
167 ERROR("%s: cannot open directory %s", g_plugin_name, path);
172 if ((lim = pathconf(path, _PC_NAME_MAX)) == -1) {
173 /* Limit not defined if errno == 0, otherwise error */
175 ERROR("%s: pathconf failed", g_plugin_name);
183 /* read "hugepages-XXXXXkB" entries */
184 while ((result = readdir(dir)) != NULL) {
185 if (strncmp(result->d_name, hugepages_dir, sizeof(hugepages_dir) - 1)) {
191 /* /sys/devices/system/node/node?/hugepages/ */
192 ssnprintf(path2, (size_t)lim, "%s/%s", path, result->d_name);
194 e_info.d_name = result->d_name;
196 walk_directory(path2, read_hugepage_entry, &e_info, 0);
200 /* Check if NULL return from readdir() was an error */
202 ERROR("%s: readdir failed", g_plugin_name);
211 static int read_nodes(void) {
212 static const char sys_node[] = "/sys/devices/system/node";
213 static const char node_string[] = "node";
214 static const char sys_node_hugepages[] =
215 "/sys/devices/system/node/%s/hugepages";
217 struct dirent *result;
221 dir = opendir(sys_node);
223 ERROR("%s: cannot open directory %s", g_plugin_name, sys_node);
228 if ((lim = pathconf(sys_node, _PC_NAME_MAX)) == -1) {
229 /* Limit not defined if errno == 0, otherwise error */
231 ERROR("%s: pathconf failed", g_plugin_name);
239 while ((result = readdir(dir)) != NULL) {
240 if (strncmp(result->d_name, node_string, sizeof(node_string) - 1)) {
246 ssnprintf(path, (size_t)lim, sys_node_hugepages, result->d_name);
247 read_syshugepages(path, result->d_name);
251 /* Check if NULL return from readdir() was an error */
253 ERROR("%s: readdir failed", g_plugin_name);
262 static int huge_read(void) {
263 static const char sys_mm_hugepages[] = "/sys/kernel/mm/hugepages";
266 if (read_syshugepages(sys_mm_hugepages, "mm") != 0) {
270 if (g_flag_rpt_numa) {
271 if (read_nodes() != 0) {
279 void module_register(void) {
280 plugin_register_config(g_plugin_name, huge_config_callback, g_config_keys,
282 plugin_register_read(g_plugin_name, huge_read);