Merge branch 'collectd-5.5' into collectd-5.6
[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 {
45   value_list_t vl = VALUE_LIST_INIT;
46
47   vl.values = &v;
48   vl.values_len = 1;
49
50   sstrncpy (vl.host, hostname_g, sizeof (vl.host));
51   sstrncpy (vl.plugin, "numa", sizeof (vl.plugin));
52   ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance), "node%i", node);
53   sstrncpy (vl.type, "vmpage_action", sizeof (vl.type));
54   sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
55
56   plugin_dispatch_values (&vl);
57 } /* }}} void numa_dispatch_value */
58
59 static int numa_read_node (int node) /* {{{ */
60 {
61   char path[PATH_MAX];
62   FILE *fh;
63   char buffer[128];
64   int status;
65   int success;
66
67   ssnprintf (path, sizeof (path), NUMA_ROOT_DIR "/node%i/numastat", node);
68
69   fh = fopen (path, "r");
70   if (fh == NULL)
71   {
72     char errbuf[1024];
73     ERROR ("numa plugin: Reading node %i failed: open(%s): %s",
74         node, path, sstrerror (errno, errbuf, sizeof (errbuf)));
75     return (-1);
76   }
77
78   success = 0;
79   while (fgets (buffer, sizeof (buffer), fh) != NULL)
80   {
81     char *fields[4];
82     value_t v;
83
84     status = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
85     if (status != 2)
86     {
87       WARNING ("numa plugin: Ignoring line with unexpected "
88           "number of fields (node %i).", node);
89       continue;
90     }
91
92     v.derive = 0;
93     status = parse_value (fields[1], &v, DS_TYPE_DERIVE);
94     if (status != 0)
95       continue;
96
97     numa_dispatch_value (node, fields[0], v);
98     success++;
99   }
100
101   fclose (fh);
102   return (success ? 0 : -1);
103 } /* }}} int numa_read_node */
104
105 static int numa_read (void) /* {{{ */
106 {
107   int i;
108   int status;
109   int success;
110
111   if (max_node < 0)
112   {
113     WARNING ("numa plugin: No NUMA nodes were detected.");
114     return (-1);
115   }
116
117   success = 0;
118   for (i = 0; i <= max_node; i++)
119   {
120     status = numa_read_node (i);
121     if (status == 0)
122       success++;
123   }
124
125   return (success ? 0 : -1);
126 } /* }}} int numa_read */
127
128 static int numa_init (void) /* {{{ */
129 {
130   /* Determine the number of nodes on this machine. */
131   while (42)
132   {
133     char path[PATH_MAX];
134     struct stat statbuf = { 0 };
135     int status;
136
137     ssnprintf (path, sizeof (path), NUMA_ROOT_DIR "/node%i", max_node + 1);
138
139     status = stat (path, &statbuf);
140     if (status == 0)
141     {
142       max_node++;
143       continue;
144     }
145     else if (errno == ENOENT)
146     {
147       break;
148     }
149     else /* ((status != 0) && (errno != ENOENT)) */
150     {
151       char errbuf[1024];
152       ERROR ("numa plugin: stat(%s) failed: %s", path,
153           sstrerror (errno, errbuf, sizeof (errbuf)));
154       return (-1);
155     }
156   }
157
158   DEBUG ("numa plugin: Found %i nodes.", max_node + 1);
159   return (0);
160 } /* }}} int numa_init */
161
162 void module_register (void)
163 {
164   plugin_register_init ("numa", numa_init);
165   plugin_register_read ("numa", numa_read);
166 } /* void module_register */
167
168 /* vim: set sw=2 sts=2 et : */