Debian Codegraph @

src/mic.c

raw
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 **/
21
22 #include "collectd.h"
23
24 #include "plugin.h"
25 #include "utils/common/common.h"
26 #include "utils/ignorelist/ignorelist.h"
27
28 #include <MicAccessApi.h>
29 #include <MicAccessErrorTypes.h>
30 #include <MicAccessTypes.h>
31 #include <MicPowerManagerAPI.h>
32 #include <MicThermalAPI.h>
33
34 #define MAX_MICS 32
35 #define MAX_CORES 256
36
37 static MicDeviceOnSystem mics[MAX_MICS];
38 static U32 num_mics;
39 static HANDLE mic_handle;
40
41 static int const therm_ids[] = {
42 eMicThermalDie, eMicThermalDevMem, eMicThermalFin, eMicThermalFout,
43 eMicThermalVccp, eMicThermalVddg, eMicThermalVddq};
44 static char const *const therm_names[] = {"die", "devmem", "fin", "fout",
45 "vccp", "vddg", "vddq"};
46
47 static const char *config_keys[] = {
48 "ShowCPU", "ShowCPUCores", "ShowMemory",
49 "ShowTemperatures", "Temperature", "IgnoreSelectedTemperature",
50 "ShowPower", "Power", "IgnoreSelectedPower"};
51 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
52
53 static bool show_cpu = true;
54 static bool show_cpu_cores = true;
55 static bool show_memory = true;
56 static bool show_temps = true;
57 static ignorelist_t *temp_ignore;
58 static bool show_power = true;
59 static ignorelist_t *power_ignore;
60
61 static int mic_init(void) {
62 U32 ret;
63 U32 mic_count;
64
65 if (mic_handle)
66 return 0;
67
68 mic_count = (U32)STATIC_ARRAY_SIZE(mics);
69 ret = MicInitAPI(&mic_handle, eTARGET_SCIF_DRIVER, mics, &mic_count);
70 if (ret != MIC_ACCESS_API_SUCCESS) {
71 ERROR("mic plugin: Problem initializing MicAccessAPI: %s",
72 MicGetErrorString(ret));
73 }
74 DEBUG("mic plugin: found: %" PRIu32 " MIC(s)", mic_count);
75
76 if (mic_count < 0 || mic_count >= MAX_MICS) {
77 ERROR("mic plugin: No Intel MICs in system");
78 return 1;
79 } else {
80 num_mics = mic_count;
81 return 0;
82 }
83 }
84
85 static int mic_config(const char *key, const char *value) {
86 if (temp_ignore == NULL)
87 temp_ignore = ignorelist_create(1);
88 if (power_ignore == NULL)
89 power_ignore = ignorelist_create(1);
90 if (temp_ignore == NULL || power_ignore == NULL)
91 return 1;
92
93 if (strcasecmp("ShowCPU", key) == 0) {
94 show_cpu = IS_TRUE(value);
95 } else if (strcasecmp("ShowCPUCores", key) == 0) {
96 show_cpu_cores = IS_TRUE(value);
97 } else if (strcasecmp("ShowTemperatures", key) == 0) {
98 show_temps = IS_TRUE(value);
99 } else if (strcasecmp("ShowMemory", key) == 0) {
100 show_memory = IS_TRUE(value);
101 } else if (strcasecmp("ShowPower", key) == 0) {
102 show_power = IS_TRUE(value);
103 } else if (strcasecmp("Temperature", key) == 0) {
104 ignorelist_add(temp_ignore, value);
105 } else if (strcasecmp("IgnoreSelectedTemperature", key) == 0) {
106 int invert = 1;
107 if (IS_TRUE(value))
108 invert = 0;
109 ignorelist_set_invert(temp_ignore, invert);
110 } else if (strcasecmp("Power", key) == 0) {
111 ignorelist_add(power_ignore, value);
112 } else if (strcasecmp("IgnoreSelectedPower", key) == 0) {
113 int invert = 1;
114 if (IS_TRUE(value))
115 invert = 0;
116 ignorelist_set_invert(power_ignore, invert);
117 } else {
118 return -1;
119 }
120 return 0;
121 }
122
123 static void mic_submit_memory_use(int micnumber, const char *type_instance,
124 U32 value) {
125 value_list_t vl = VALUE_LIST_INIT;
126
127 /* MicAccessAPI reports KB's of memory, adjust for this */
128 DEBUG("mic plugin: Memory Value Report; %u %lf", value,
129 ((gauge_t)value) * 1024.0);
130
131 vl.values = &(value_t){.gauge = ((gauge_t)value) * 1024.0};
132 vl.values_len = 1;
133
134 strncpy(vl.plugin, "mic", sizeof(vl.plugin));
135 ssnprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%i", micnumber);
136 strncpy(vl.type, "memory", sizeof(vl.type));
137 strncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
138
139 plugin_dispatch_values(&vl);
140 }
141
142 /* Gather memory Utilization */
143 static int mic_read_memory(int mic) {
144 U32 ret;
145 U32 mem_total, mem_free, mem_bufs;
146
147 ret = MicGetMemoryUtilization(mic_handle, &mem_total, &mem_free, &mem_bufs);
148 if (ret != MIC_ACCESS_API_SUCCESS) {
149 ERROR("mic plugin: Problem getting Memory Utilization: %s",
150 MicGetErrorString(ret));
151 return 1;
152 }
153 mic_submit_memory_use(mic, "free", mem_free);
154 mic_submit_memory_use(mic, "used", mem_total - mem_free - mem_bufs);
155 mic_submit_memory_use(mic, "buffered", mem_bufs);
156 DEBUG("mic plugin: Memory Read: %u %u %u", mem_total, mem_free, mem_bufs);
157 return 0;
158 }
159
160 static void mic_submit_temp(int micnumber, const char *type, gauge_t value) {
161 value_list_t vl = VALUE_LIST_INIT;
162
163 vl.values = &(value_t){.gauge = value};
164 vl.values_len = 1;
165
166 strncpy(vl.plugin, "mic", sizeof(vl.plugin));
167 ssnprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%i", micnumber);
168 strncpy(vl.type, "temperature", sizeof(vl.type));
169 strncpy(vl.type_instance, type, sizeof(vl.type_instance));
170
171 plugin_dispatch_values(&vl);
172 }
173
174 /* Gather Temperature Information */
175 static int mic_read_temps(int mic) {
176 size_t num_therms = STATIC_ARRAY_SIZE(therm_ids);
177
178 for (size_t j = 0; j < num_therms; j++) {
179 U32 status;
180 U32 temp_buffer;
181 U32 buffer_size = (U32)sizeof(temp_buffer);
182 char const *name = therm_names[j];
183
184 if (ignorelist_match(temp_ignore, name) != 0)
185 continue;
186
187 status =
188 MicGetTemperature(mic_handle, therm_ids[j], &temp_buffer, &buffer_size);
189 if (status != MIC_ACCESS_API_SUCCESS) {
190 ERROR("mic plugin: Error reading temperature \"%s\": "
191 "%s",
192 name, MicGetErrorString(status));
193 return 1;
194 }
195 mic_submit_temp(mic, name, temp_buffer);
196 }
197 return 0;
198 }
199
200 static void mic_submit_cpu(int micnumber, const char *type_instance, int core,
201 derive_t value) {
202 value_list_t vl = VALUE_LIST_INIT;
203
204 vl.values = &(value_t){.derive = value};
205 vl.values_len = 1;
206
207 strncpy(vl.plugin, "mic", sizeof(vl.plugin));
208 if (core < 0) /* global aggregation */
209 ssnprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%i", micnumber);
210 else /* per-core statistics */
211 ssnprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%i-cpu-%i",
212 micnumber, core);
213 strncpy(vl.type, "cpu", sizeof(vl.type));
214 strncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
215
216 plugin_dispatch_values(&vl);
217 }
218
219 /*Gather CPU Utilization Information */
220 static int mic_read_cpu(int mic) {
221 MicCoreUtil core_util;
222 MicCoreJiff core_jiffs[MAX_CORES];
223 U32 core_jiffs_size;
224 U32 status;
225
226 core_jiffs_size = MAX_CORES * sizeof(MicCoreJiff);
227 status = MicGetCoreUtilization(mic_handle, &core_util, core_jiffs,
228 &core_jiffs_size);
229 if (status != MIC_ACCESS_API_SUCCESS) {
230 ERROR("mic plugin: Problem getting CPU utilization: %s",
231 MicGetErrorString(status));
232 return -1;
233 }
234
235 if (show_cpu) {
236 mic_submit_cpu(mic, "user", -1, core_util.sum.user);
237 mic_submit_cpu(mic, "sys", -1, core_util.sum.sys);
238 mic_submit_cpu(mic, "nice", -1, core_util.sum.nice);
239 mic_submit_cpu(mic, "idle", -1, core_util.sum.idle);
240 }
241
242 if (show_cpu_cores) {
243 for (int j = 0; j < core_util.core; j++) {
244 mic_submit_cpu(mic, "user", j, core_jiffs[j].user);
245 mic_submit_cpu(mic, "sys", j, core_jiffs[j].sys);
246 mic_submit_cpu(mic, "nice", j, core_jiffs[j].nice);
247 mic_submit_cpu(mic, "idle", j, core_jiffs[j].idle);
248 }
249 }
250 return 0;
251 }
252
253 static void mic_submit_power(int micnumber, const char *type,
254 const char *type_instance, gauge_t value) {
255 value_list_t vl = VALUE_LIST_INIT;
256
257 vl.values = &(value_t){.gauge = value};
258 vl.values_len = 1;
259
260 strncpy(vl.plugin, "mic", sizeof(vl.plugin));
261 ssnprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%i", micnumber);
262 strncpy(vl.type, type, sizeof(vl.type));
263 strncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
264
265 plugin_dispatch_values(&vl);
266 }
267
268 /* Gather Power Information */
269 static int mic_read_power(int mic) {
270 U32 ret;
271 MicPwrUsage power_use;
272
273 ret = MicGetPowerUsage(mic_handle, &power_use);
274 if (ret != MIC_ACCESS_API_SUCCESS) {
275 ERROR("mic plugin: Problem getting Power Usage: %s",
276 MicGetErrorString(ret));
277 return 1;
278 }
279
280 /* power is in uWatts, current in mA, voltage in uVolts.. convert to
281 * base unit */
282 #define SUB_POWER(name) \
283 do { \
284 if (ignorelist_match(power_ignore, #name) == 0) \
285 mic_submit_power(mic, "power", #name, \
286 (gauge_t)power_use.name.prr * 0.000001); \
287 } while (0)
288 #define SUB_VOLTS(name) \
289 do { \
290 if (ignorelist_match(power_ignore, #name) == 0) { \
291 mic_submit_power(mic, "power", #name, \
292 (gauge_t)(power_use.name.pwr * 0.000001)); \
293 mic_submit_power(mic, "current", #name, \
294 (gauge_t)(power_use.name.cur * 0.001)); \
295 mic_submit_power(mic, "voltage", #name, \
296 (gauge_t)(power_use.name.volt * 0.000001)); \
297 } \
298 } while (0)
299
300 SUB_POWER(total0);
301 SUB_POWER(total1);
302 SUB_POWER(inst);
303 SUB_POWER(imax);
304 SUB_POWER(pcie);
305 SUB_POWER(c2x3);
306 SUB_POWER(c2x4);
307 SUB_VOLTS(vccp);
308 SUB_VOLTS(vddg);
309 SUB_VOLTS(vddq);
310
311 return 0;
312 }
313
314 static int mic_read(void) {
315 U32 ret;
316 int error;
317
318 error = 0;
319 for (int i = 0; i < num_mics; i++) {
320 ret = MicInitAdapter(&mic_handle, &mics[i]);
321 if (ret != MIC_ACCESS_API_SUCCESS) {
322 ERROR("mic plugin: Problem initializing MicAdapter: %s",
323 MicGetErrorString(ret));
324 error = 1;
325 }
326
327 if (error == 0 && show_memory)
328 error = mic_read_memory(i);
329
330 if (error == 0 && show_temps)
331 error = mic_read_temps(i);
332
333 if (error == 0 && (show_cpu || show_cpu_cores))
334 error = mic_read_cpu(i);
335
336 if (error == 0 && (show_power))
337 error = mic_read_power(i);
338
339 ret = MicCloseAdapter(mic_handle);
340 if (ret != MIC_ACCESS_API_SUCCESS) {
341 ERROR("mic plugin: Problem closing MicAdapter: %s",
342 MicGetErrorString(ret));
343 error = 2;
344 break;
345 }
346 }
347 if (num_mics == 0)
348 error = 3;
349 return error;
350 }
351
352 static int mic_shutdown(void) {
353 if (mic_handle)
354 MicCloseAPI(&mic_handle);
355 mic_handle = NULL;
356
357 return 0;
358 }
359
360 void module_register(void) {
361 plugin_register_init("mic", mic_init);
362 plugin_register_shutdown("mic", mic_shutdown);
363 plugin_register_read("mic", mic_read);
364 plugin_register_config("mic", mic_config, config_keys, config_keys_num);
365 } /* void module_register */