1 /**
2 * collectd - src/mic.c
3 * Copyright (C) 2013 Battelle Memorial Institute
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 *
18 * Authors:
19 * Evan Felix <evan.felix at pnnl.gov>
20 **/
22 #include "collectd.h"
23 #include "plugin.h"
24 #include "common.h"
25 #include "utils_ignorelist.h"
27 #include <MicAccessTypes.h>
28 #include <MicAccessErrorTypes.h>
29 #include <MicAccessApi.h>
30 #include <MicThermalAPI.h>
32 #define MAX_MICS 32
33 #define MAX_CORES 256
35 static MicDeviceOnSystem mics[MAX_MICS];
36 static U32 num_mics = 0;
37 static HANDLE mic_handle = NULL;
39 static int const therm_ids[] = {
40 eMicThermalDie, eMicThermalDevMem, eMicThermalFin, eMicThermalFout,
41 eMicThermalVccp, eMicThermalVddg, eMicThermalVddq };
42 static char const * const therm_names[] = {
43 "die", "devmem", "fin", "fout",
44 "vccp", "vddg", "vddq" };
46 static const char *config_keys[] =
47 {
48 "ShowTotalCPU",
49 "ShowPerCPU",
50 "ShowTemps",
51 "ShowMemory",
52 "TempSensor",
53 "IgnoreTempSelected",
54 };
55 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
57 static _Bool show_total_cpu = 1;
58 static _Bool show_per_cpu = 1;
59 static _Bool show_temps = 1;
60 static _Bool show_memory = 1;
61 static ignorelist_t *temp_ignore = NULL;
64 static int mic_init (void)
65 {
66 U32 ret;
67 U32 mic_count;
69 if (mic_handle)
70 return (0);
72 mic_count = (U32) STATIC_ARRAY_SIZE(mics);
73 ret = MicInitAPI(&mic_handle, eTARGET_SCIF_DRIVER, mics, &mic_count);
74 if (ret != MIC_ACCESS_API_SUCCESS) {
75 ERROR("mic plugin: Problem initializing MicAccessAPI: %s",
76 MicGetErrorString(ret));
77 }
78 DEBUG("mic plugin: found: %"PRIu32" MIC(s)",mic_count);
80 if (mic_count<0 || mic_count>=MAX_MICS) {
81 ERROR("mic plugin: No Intel MICs in system");
82 return (1);
83 }
84 else {
85 num_mics = mic_count;
86 return (0);
87 }
88 }
90 static int mic_config (const char *key, const char *value) {
91 if (temp_ignore == NULL)
92 temp_ignore = ignorelist_create(1);
93 if (temp_ignore == NULL)
94 return (1);
96 if (strcasecmp("ShowTotalCPU",key) == 0)
97 {
98 show_total_cpu = IS_TRUE(value);
99 }
100 else if (strcasecmp("ShowPerCPU",key) == 0)
101 {
102 show_per_cpu = IS_TRUE(value);
103 }
104 else if (strcasecmp("ShowTemps",key) == 0)
105 {
106 show_temps = IS_TRUE(value);
107 }
108 else if (strcasecmp("ShowMemory",key) == 0)
109 {
110 show_memory = IS_TRUE(value);
111 }
112 else if (strcasecmp("TempSensor",key) == 0)
113 {
114 ignorelist_add(temp_ignore,value);
115 }
116 else if (strcasecmp("IgnoreTempSelected",key) == 0)
117 {
118 int invert = 1;
119 if (IS_TRUE(value))
120 invert = 0;
121 ignorelist_set_invert(temp_ignore,invert);
122 }
123 else
124 {
125 return (-1);
126 }
127 return (0);
128 }
130 static void mic_submit_memory_use(int micnumber, const char *type_instance, U32 val)
131 {
132 value_t values[1];
133 value_list_t vl = VALUE_LIST_INIT;
135 /* MicAccessAPI reports KB's of memory, adjust for this */
136 DEBUG("mic plugin: Memory Value Report; %u %lf",val,((gauge_t)val)*1024.0);
137 values[0].gauge = ((gauge_t)val)*1024.0;
139 vl.values=values;
140 vl.values_len=1;
142 strncpy (vl.host, hostname_g, sizeof (vl.host));
143 strncpy (vl.plugin, "mic", sizeof (vl.plugin));
144 ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance), "%i", micnumber);
145 strncpy (vl.type, "memory", sizeof (vl.type));
146 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
148 plugin_dispatch_values (&vl);
149 }
151 /* Gather memory Utilization */
152 static int mic_read_memory(int mic)
153 {
154 U32 ret;
155 U32 mem_total,mem_free,mem_bufs;
157 ret = MicGetMemoryUtilization(mic_handle,&mem_total,&mem_free,&mem_bufs);
158 if (ret != MIC_ACCESS_API_SUCCESS) {
159 ERROR("mic plugin: Problem getting Memory Utilization: %s",
160 MicGetErrorString(ret));
161 return (1);
162 }
163 mic_submit_memory_use(mic,"free",mem_free);
164 mic_submit_memory_use(mic,"used",mem_total-mem_free-mem_bufs);
165 mic_submit_memory_use(mic,"buffered",mem_bufs);
166 DEBUG("mic plugin: Memory Read: %u %u %u",mem_total,mem_free,mem_bufs);
167 return (0);
168 }
170 static void mic_submit_temp(int micnumber, const char *type, gauge_t val)
171 {
172 value_t values[1];
173 value_list_t vl = VALUE_LIST_INIT;
175 values[0].gauge = val;
177 vl.values=values;
178 vl.values_len=1;
180 strncpy (vl.host, hostname_g, sizeof (vl.host));
181 strncpy (vl.plugin, "mic", sizeof (vl.plugin));
182 ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
183 "%i", micnumber);
184 strncpy (vl.type, "temperature", sizeof (vl.type));
185 strncpy (vl.type_instance, type, sizeof (vl.type_instance));
187 plugin_dispatch_values (&vl);
188 }
190 /* Gather Temperature Information */
191 static int mic_read_temps(int mic)
192 {
193 size_t num_therms = STATIC_ARRAY_SIZE(therm_ids);
194 size_t j;
196 for (j = 0; j < num_therms; j++) {
197 U32 status;
198 U32 temp_buffer;
199 U32 buffer_size = (U32)sizeof(temp_buffer);
200 char const *name = therm_names[j];
202 if (ignorelist_match(temp_ignore, name) != 0)
203 continue;
205 status = MicGetTemperature(mic_handle, therm_ids[j],
206 &temp_buffer, &buffer_size);
207 if (status != MIC_ACCESS_API_SUCCESS) {
208 ERROR("mic plugin: Error reading temperature \"%s\": "
209 "%s", name, MicGetErrorString(status));
210 return (1);
211 }
212 mic_submit_temp(mic, name, temp_buffer);
213 }
214 return (0);
215 }
217 static void mic_submit_cpu(int micnumber, const char *type_instance,
218 int core, derive_t val)
219 {
220 value_t values[1];
221 value_list_t vl = VALUE_LIST_INIT;
223 values[0].derive = val;
225 vl.values=values;
226 vl.values_len=1;
228 strncpy (vl.host, hostname_g, sizeof (vl.host));
229 strncpy (vl.plugin, "mic", sizeof (vl.plugin));
230 if (core < 0) /* global aggregation */
231 ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
232 "%i", micnumber);
233 else /* per-core statistics */
234 ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
235 "%i-cpu-%i", micnumber, core);
236 strncpy (vl.type, "cpu", sizeof (vl.type));
237 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
239 plugin_dispatch_values (&vl);
240 }
242 /*Gather CPU Utilization Information */
243 static int mic_read_cpu(int mic)
244 {
245 MicCoreUtil core_util;
246 MicCoreJiff core_jiffs[MAX_CORES];
247 U32 core_jiffs_size;
248 U32 status;
250 core_jiffs_size = MAX_CORES * sizeof(MicCoreJiff);
251 status = MicGetCoreUtilization(mic_handle, &core_util,
252 core_jiffs, &core_jiffs_size);
253 if (status != MIC_ACCESS_API_SUCCESS) {
254 ERROR("mic plugin: Problem getting CPU utilization: %s",
255 MicGetErrorString(status));
256 return(-1);
257 }
259 if (show_total_cpu) {
260 mic_submit_cpu(mic, "user", -1, core_util.sum.user);
261 mic_submit_cpu(mic, "sys", -1, core_util.sum.sys);
262 mic_submit_cpu(mic, "nice", -1, core_util.sum.nice);
263 mic_submit_cpu(mic, "idle", -1, core_util.sum.idle);
264 }
266 if (show_per_cpu) {
267 int j;
268 for (j = 0; j < core_util.core; j++) {
269 mic_submit_cpu(mic, "user", j, core_jiffs[j].user);
270 mic_submit_cpu(mic, "sys", j, core_jiffs[j].sys);
271 mic_submit_cpu(mic, "nice", j, core_jiffs[j].nice);
272 mic_submit_cpu(mic, "idle", j, core_jiffs[j].idle);
273 }
274 }
275 return (0);
276 }
278 static int mic_read (void)
279 {
280 int i;
281 U32 ret;
282 int error;
284 error=0;
285 for (i=0;i<num_mics;i++) {
286 ret = MicInitAdapter(&mic_handle,&mics[i]);
287 if (ret != MIC_ACCESS_API_SUCCESS) {
288 ERROR("mic plugin: Problem initializing MicAdapter: %s",
289 MicGetErrorString(ret));
290 error=1;
291 }
293 if (error == 0 && show_memory)
294 error = mic_read_memory(i);
296 if (error == 0 && show_temps)
297 error = mic_read_temps(i);
299 if (error == 0 && (show_total_cpu || show_per_cpu))
300 error = mic_read_cpu(i);
302 ret = MicCloseAdapter(mic_handle);
303 if (ret != MIC_ACCESS_API_SUCCESS) {
304 ERROR("mic plugin: Problem closing MicAdapter: %s",
305 MicGetErrorString(ret));
306 error=2;
307 break;
308 }
309 }
310 if (num_mics==0)
311 error=3;
312 return error;
313 }
316 static int mic_shutdown (void)
317 {
318 if (mic_handle)
319 MicCloseAPI(&mic_handle);
320 return (0);
321 }
323 void module_register (void)
324 {
325 plugin_register_init ("mic", mic_init);
326 plugin_register_shutdown ("mic", mic_shutdown);
327 plugin_register_read ("mic", mic_read);
328 plugin_register_config ("mic",mic_config, config_keys, config_keys_num);
329 } /* void module_register */
331 /*
332 * vim: set shiftwidth=8 softtabstop=8 noet textwidth=78 :
333 */