Tree wide: Reformat with clang-format.
[collectd.git] / src / numa.c
1 /**
2  * collectd - src/numa.c
3  * Copyright (C) 2012       Florian 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 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 #error "No applicable input method."
34 #endif
35
36 #ifndef NUMA_ROOT_DIR
37 #define NUMA_ROOT_DIR "/sys/devices/system/node"
38 #endif
39
40 static int max_node = -1;
41
42 static void numa_dispatch_value(int node, /* {{{ */
43                                 const char *type_instance, value_t v) {
44   value_list_t vl = VALUE_LIST_INIT;
45
46   vl.values = &v;
47   vl.values_len = 1;
48
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));
54
55   plugin_dispatch_values(&vl);
56 } /* }}} void numa_dispatch_value */
57
58 static int numa_read_node(int node) /* {{{ */
59 {
60   char path[PATH_MAX];
61   FILE *fh;
62   char buffer[128];
63   int status;
64   int success;
65
66   ssnprintf(path, sizeof(path), NUMA_ROOT_DIR "/node%i/numastat", node);
67
68   fh = fopen(path, "r");
69   if (fh == NULL) {
70     char errbuf[1024];
71     ERROR("numa plugin: Reading node %i failed: open(%s): %s", node, path,
72           sstrerror(errno, errbuf, sizeof(errbuf)));
73     return (-1);
74   }
75
76   success = 0;
77   while (fgets(buffer, sizeof(buffer), fh) != NULL) {
78     char *fields[4];
79     value_t v;
80
81     status = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
82     if (status != 2) {
83       WARNING("numa plugin: Ignoring line with unexpected "
84               "number of fields (node %i).",
85               node);
86       continue;
87     }
88
89     v.derive = 0;
90     status = parse_value(fields[1], &v, DS_TYPE_DERIVE);
91     if (status != 0)
92       continue;
93
94     numa_dispatch_value(node, fields[0], v);
95     success++;
96   }
97
98   fclose(fh);
99   return (success ? 0 : -1);
100 } /* }}} int numa_read_node */
101
102 static int numa_read(void) /* {{{ */
103 {
104   int i;
105   int status;
106   int success;
107
108   if (max_node < 0) {
109     WARNING("numa plugin: No NUMA nodes were detected.");
110     return (-1);
111   }
112
113   success = 0;
114   for (i = 0; i <= max_node; i++) {
115     status = numa_read_node(i);
116     if (status == 0)
117       success++;
118   }
119
120   return (success ? 0 : -1);
121 } /* }}} int numa_read */
122
123 static int numa_init(void) /* {{{ */
124 {
125   /* Determine the number of nodes on this machine. */
126   while (42) {
127     char path[PATH_MAX];
128     struct stat statbuf = {0};
129     int status;
130
131     ssnprintf(path, sizeof(path), NUMA_ROOT_DIR "/node%i", max_node + 1);
132
133     status = stat(path, &statbuf);
134     if (status == 0) {
135       max_node++;
136       continue;
137     } else if (errno == ENOENT) {
138       break;
139     } else /* ((status != 0) && (errno != ENOENT)) */
140     {
141       char errbuf[1024];
142       ERROR("numa plugin: stat(%s) failed: %s", path,
143             sstrerror(errno, errbuf, sizeof(errbuf)));
144       return (-1);
145     }
146   }
147
148   DEBUG("numa plugin: Found %i nodes.", max_node + 1);
149   return (0);
150 } /* }}} int numa_init */
151
152 void module_register(void) {
153   plugin_register_init("numa", numa_init);
154   plugin_register_read("numa", numa_read);
155 } /* void module_register */
156
157 /* vim: set sw=2 sts=2 et : */