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