3 * Copyright (C) 2013 Battelle Memorial Institute
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.
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.
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
19 * Evan Felix <evan.felix at pnnl.gov>
26 #include "utils_ignorelist.h"
28 #include <MicAccessApi.h>
29 #include <MicAccessErrorTypes.h>
30 #include <MicAccessTypes.h>
31 #include <MicPowerManagerAPI.h>
32 #include <MicThermalAPI.h>
37 static MicDeviceOnSystem mics[MAX_MICS];
38 static U32 num_mics = 0;
39 static HANDLE mic_handle = NULL;
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"};
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);
53 static _Bool show_cpu = 1;
54 static _Bool show_cpu_cores = 1;
55 static _Bool show_memory = 1;
56 static _Bool show_temps = 1;
57 static ignorelist_t *temp_ignore = NULL;
58 static _Bool show_power = 1;
59 static ignorelist_t *power_ignore = NULL;
61 static int mic_init(void) {
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));
74 DEBUG("mic plugin: found: %" PRIu32 " MIC(s)", mic_count);
76 if (mic_count < 0 || mic_count >= MAX_MICS) {
77 ERROR("mic plugin: No Intel MICs in system");
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)
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) {
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) {
116 ignorelist_set_invert(power_ignore, invert);
123 static void mic_submit_memory_use(int micnumber, const char *type_instance,
125 value_list_t vl = VALUE_LIST_INIT;
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);
131 vl.values = &(value_t){.gauge = ((gauge_t)value) * 1024.0};
134 strncpy(vl.plugin, "mic", sizeof(vl.plugin));
135 snprintf(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));
139 plugin_dispatch_values(&vl);
142 /* Gather memory Utilization */
143 static int mic_read_memory(int mic) {
145 U32 mem_total, mem_free, mem_bufs;
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));
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);
160 static void mic_submit_temp(int micnumber, const char *type, gauge_t value) {
161 value_list_t vl = VALUE_LIST_INIT;
163 vl.values = &(value_t){.gauge = value};
166 strncpy(vl.plugin, "mic", sizeof(vl.plugin));
167 snprintf(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));
171 plugin_dispatch_values(&vl);
174 /* Gather Temperature Information */
175 static int mic_read_temps(int mic) {
176 size_t num_therms = STATIC_ARRAY_SIZE(therm_ids);
178 for (size_t j = 0; j < num_therms; j++) {
181 U32 buffer_size = (U32)sizeof(temp_buffer);
182 char const *name = therm_names[j];
184 if (ignorelist_match(temp_ignore, name) != 0)
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\": "
192 name, MicGetErrorString(status));
195 mic_submit_temp(mic, name, temp_buffer);
200 static void mic_submit_cpu(int micnumber, const char *type_instance, int core,
202 value_list_t vl = VALUE_LIST_INIT;
204 vl.values = &(value_t){.derive = value};
207 strncpy(vl.plugin, "mic", sizeof(vl.plugin));
208 if (core < 0) /* global aggregation */
209 snprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%i", micnumber);
210 else /* per-core statistics */
211 snprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%i-cpu-%i",
213 strncpy(vl.type, "cpu", sizeof(vl.type));
214 strncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
216 plugin_dispatch_values(&vl);
219 /*Gather CPU Utilization Information */
220 static int mic_read_cpu(int mic) {
221 MicCoreUtil core_util;
222 MicCoreJiff core_jiffs[MAX_CORES];
226 core_jiffs_size = MAX_CORES * sizeof(MicCoreJiff);
227 status = MicGetCoreUtilization(mic_handle, &core_util, core_jiffs,
229 if (status != MIC_ACCESS_API_SUCCESS) {
230 ERROR("mic plugin: Problem getting CPU utilization: %s",
231 MicGetErrorString(status));
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);
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);
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;
257 vl.values = &(value_t){.gauge = value};
260 strncpy(vl.plugin, "mic", sizeof(vl.plugin));
261 snprintf(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));
265 plugin_dispatch_values(&vl);
268 /* Gather Power Information */
269 static int mic_read_power(int mic) {
271 MicPwrUsage power_use;
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));
280 /* power is in uWatts, current in mA, voltage in uVolts.. convert to
282 #define SUB_POWER(name) \
284 if (ignorelist_match(power_ignore, #name) == 0) \
285 mic_submit_power(mic, "power", #name, \
286 (gauge_t)power_use.name.prr * 0.000001); \
288 #define SUB_VOLTS(name) \
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)); \
314 static int mic_read(void) {
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));
327 if (error == 0 && show_memory)
328 error = mic_read_memory(i);
330 if (error == 0 && show_temps)
331 error = mic_read_temps(i);
333 if (error == 0 && (show_cpu || show_cpu_cores))
334 error = mic_read_cpu(i);
336 if (error == 0 && (show_power))
337 error = mic_read_power(i);
339 ret = MicCloseAdapter(mic_handle);
340 if (ret != MIC_ACCESS_API_SUCCESS) {
341 ERROR("mic plugin: Problem closing MicAdapter: %s",
342 MicGetErrorString(ret));
352 static int mic_shutdown(void) {
354 MicCloseAPI(&mic_handle);
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 */