2 * collectd - src/numa.c
3 * Copyright (C) 2012 Florian Forster
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
24 * Florian Forster <octo at collectd.org>
33 #error "No applicable input method."
37 #define NUMA_ROOT_DIR "/sys/devices/system/node"
40 static int max_node = -1;
42 static void numa_dispatch_value(int node, /* {{{ */
43 const char *type_instance, value_t v) {
44 value_list_t vl = VALUE_LIST_INIT;
49 sstrncpy(vl.host, hostname_g, sizeof(vl.host));
50 sstrncpy(vl.plugin, "numa", sizeof(vl.plugin));
51 ssnprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "node%i", node);
52 sstrncpy(vl.type, "vmpage_action", sizeof(vl.type));
53 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
55 plugin_dispatch_values(&vl);
56 } /* }}} void numa_dispatch_value */
58 static int numa_read_node(int node) /* {{{ */
66 ssnprintf(path, sizeof(path), NUMA_ROOT_DIR "/node%i/numastat", node);
68 fh = fopen(path, "r");
71 ERROR("numa plugin: Reading node %i failed: open(%s): %s", node, path,
72 sstrerror(errno, errbuf, sizeof(errbuf)));
77 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
81 status = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
83 WARNING("numa plugin: Ignoring line with unexpected "
84 "number of fields (node %i).",
90 status = parse_value(fields[1], &v, DS_TYPE_DERIVE);
94 numa_dispatch_value(node, fields[0], v);
99 return (success ? 0 : -1);
100 } /* }}} int numa_read_node */
102 static int numa_read(void) /* {{{ */
109 WARNING("numa plugin: No NUMA nodes were detected.");
114 for (i = 0; i <= max_node; i++) {
115 status = numa_read_node(i);
120 return (success ? 0 : -1);
121 } /* }}} int numa_read */
123 static int numa_init(void) /* {{{ */
125 /* Determine the number of nodes on this machine. */
128 struct stat statbuf = {0};
131 ssnprintf(path, sizeof(path), NUMA_ROOT_DIR "/node%i", max_node + 1);
133 status = stat(path, &statbuf);
137 } else if (errno == ENOENT) {
139 } else /* ((status != 0) && (errno != ENOENT)) */
142 ERROR("numa plugin: stat(%s) failed: %s", path,
143 sstrerror(errno, errbuf, sizeof(errbuf)));
148 DEBUG("numa plugin: Found %i nodes.", max_node + 1);
150 } /* }}} int numa_init */
152 void module_register(void) {
153 plugin_register_init("numa", numa_init);
154 plugin_register_read("numa", numa_read);
155 } /* void module_register */
157 /* vim: set sw=2 sts=2 et : */