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 <MicAccessTypes.h>
29 #include <MicAccessErrorTypes.h>
30 #include <MicAccessApi.h>
31 #include <MicThermalAPI.h>
32 #include <MicPowerManagerAPI.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[] = {
45 "die", "devmem", "fin", "fout",
46 "vccp", "vddg", "vddq" };
48 static const char *config_keys[] =
55 "IgnoreSelectedTemperature",
60 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
62 static _Bool show_cpu = 1;
63 static _Bool show_cpu_cores = 1;
64 static _Bool show_memory = 1;
65 static _Bool show_temps = 1;
66 static ignorelist_t *temp_ignore = NULL;
67 static _Bool show_power = 1;
68 static ignorelist_t *power_ignore = NULL;
70 static int mic_init (void)
78 mic_count = (U32) STATIC_ARRAY_SIZE(mics);
79 ret = MicInitAPI(&mic_handle, eTARGET_SCIF_DRIVER, mics, &mic_count);
80 if (ret != MIC_ACCESS_API_SUCCESS) {
81 ERROR("mic plugin: Problem initializing MicAccessAPI: %s",
82 MicGetErrorString(ret));
84 DEBUG("mic plugin: found: %"PRIu32" MIC(s)",mic_count);
86 if (mic_count<0 || mic_count>=MAX_MICS) {
87 ERROR("mic plugin: No Intel MICs in system");
96 static int mic_config (const char *key, const char *value) {
97 if (temp_ignore == NULL)
98 temp_ignore = ignorelist_create(1);
99 if (power_ignore == NULL)
100 power_ignore = ignorelist_create(1);
101 if (temp_ignore == NULL || power_ignore == NULL)
104 if (strcasecmp("ShowCPU",key) == 0)
106 show_cpu = IS_TRUE(value);
108 else if (strcasecmp("ShowCPUCores",key) == 0)
110 show_cpu_cores = IS_TRUE(value);
112 else if (strcasecmp("ShowTemperatures",key) == 0)
114 show_temps = IS_TRUE(value);
116 else if (strcasecmp("ShowMemory",key) == 0)
118 show_memory = IS_TRUE(value);
120 else if (strcasecmp("ShowPower",key) == 0)
122 show_power = IS_TRUE(value);
124 else if (strcasecmp("Temperature",key) == 0)
126 ignorelist_add(temp_ignore,value);
128 else if (strcasecmp("IgnoreSelectedTemperature",key) == 0)
133 ignorelist_set_invert(temp_ignore,invert);
135 else if (strcasecmp("Power",key) == 0)
137 ignorelist_add(power_ignore,value);
139 else if (strcasecmp("IgnoreSelectedPower",key) == 0)
144 ignorelist_set_invert(power_ignore,invert);
153 static void mic_submit_memory_use(int micnumber, const char *type_instance, U32 val)
156 value_list_t vl = VALUE_LIST_INIT;
158 /* MicAccessAPI reports KB's of memory, adjust for this */
159 DEBUG("mic plugin: Memory Value Report; %u %lf",val,((gauge_t)val)*1024.0);
160 values[0].gauge = ((gauge_t)val)*1024.0;
165 strncpy (vl.host, hostname_g, sizeof (vl.host));
166 strncpy (vl.plugin, "mic", sizeof (vl.plugin));
167 ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance), "%i", micnumber);
168 strncpy (vl.type, "memory", sizeof (vl.type));
169 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
171 plugin_dispatch_values (&vl);
174 /* Gather memory Utilization */
175 static int mic_read_memory(int mic)
178 U32 mem_total,mem_free,mem_bufs;
180 ret = MicGetMemoryUtilization(mic_handle,&mem_total,&mem_free,&mem_bufs);
181 if (ret != MIC_ACCESS_API_SUCCESS) {
182 ERROR("mic plugin: Problem getting Memory Utilization: %s",
183 MicGetErrorString(ret));
186 mic_submit_memory_use(mic,"free",mem_free);
187 mic_submit_memory_use(mic,"used",mem_total-mem_free-mem_bufs);
188 mic_submit_memory_use(mic,"buffered",mem_bufs);
189 DEBUG("mic plugin: Memory Read: %u %u %u",mem_total,mem_free,mem_bufs);
193 static void mic_submit_temp(int micnumber, const char *type, gauge_t val)
196 value_list_t vl = VALUE_LIST_INIT;
198 values[0].gauge = val;
203 strncpy (vl.host, hostname_g, sizeof (vl.host));
204 strncpy (vl.plugin, "mic", sizeof (vl.plugin));
205 ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
207 strncpy (vl.type, "temperature", sizeof (vl.type));
208 strncpy (vl.type_instance, type, sizeof (vl.type_instance));
210 plugin_dispatch_values (&vl);
213 /* Gather Temperature Information */
214 static int mic_read_temps(int mic)
216 size_t num_therms = STATIC_ARRAY_SIZE(therm_ids);
218 for (size_t j = 0; j < num_therms; j++) {
221 U32 buffer_size = (U32)sizeof(temp_buffer);
222 char const *name = therm_names[j];
224 if (ignorelist_match(temp_ignore, name) != 0)
227 status = MicGetTemperature(mic_handle, therm_ids[j],
228 &temp_buffer, &buffer_size);
229 if (status != MIC_ACCESS_API_SUCCESS) {
230 ERROR("mic plugin: Error reading temperature \"%s\": "
231 "%s", name, MicGetErrorString(status));
234 mic_submit_temp(mic, name, temp_buffer);
239 static void mic_submit_cpu(int micnumber, const char *type_instance,
240 int core, derive_t val)
243 value_list_t vl = VALUE_LIST_INIT;
245 values[0].derive = val;
250 strncpy (vl.host, hostname_g, sizeof (vl.host));
251 strncpy (vl.plugin, "mic", sizeof (vl.plugin));
252 if (core < 0) /* global aggregation */
253 ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
255 else /* per-core statistics */
256 ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
257 "%i-cpu-%i", micnumber, core);
258 strncpy (vl.type, "cpu", sizeof (vl.type));
259 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
261 plugin_dispatch_values (&vl);
264 /*Gather CPU Utilization Information */
265 static int mic_read_cpu(int mic)
267 MicCoreUtil core_util;
268 MicCoreJiff core_jiffs[MAX_CORES];
272 core_jiffs_size = MAX_CORES * sizeof(MicCoreJiff);
273 status = MicGetCoreUtilization(mic_handle, &core_util,
274 core_jiffs, &core_jiffs_size);
275 if (status != MIC_ACCESS_API_SUCCESS) {
276 ERROR("mic plugin: Problem getting CPU utilization: %s",
277 MicGetErrorString(status));
282 mic_submit_cpu(mic, "user", -1, core_util.sum.user);
283 mic_submit_cpu(mic, "sys", -1, core_util.sum.sys);
284 mic_submit_cpu(mic, "nice", -1, core_util.sum.nice);
285 mic_submit_cpu(mic, "idle", -1, core_util.sum.idle);
288 if (show_cpu_cores) {
289 for (int j = 0; j < core_util.core; j++) {
290 mic_submit_cpu(mic, "user", j, core_jiffs[j].user);
291 mic_submit_cpu(mic, "sys", j, core_jiffs[j].sys);
292 mic_submit_cpu(mic, "nice", j, core_jiffs[j].nice);
293 mic_submit_cpu(mic, "idle", j, core_jiffs[j].idle);
299 static void mic_submit_power(int micnumber, const char *type, const char *type_instance, gauge_t val)
302 value_list_t vl = VALUE_LIST_INIT;
304 values[0].gauge = val;
309 strncpy (vl.host, hostname_g, sizeof (vl.host));
310 strncpy (vl.plugin, "mic", sizeof (vl.plugin));
311 ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance), "%i", micnumber);
312 strncpy (vl.type, type, sizeof (vl.type));
313 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
315 plugin_dispatch_values (&vl);
318 /* Gather Power Information */
319 static int mic_read_power(int mic)
322 MicPwrUsage power_use;
324 ret = MicGetPowerUsage(mic_handle,&power_use);
325 if (ret != MIC_ACCESS_API_SUCCESS) {
326 ERROR("mic plugin: Problem getting Power Usage: %s",
327 MicGetErrorString(ret));
331 /* power is in uWatts, current in mA, voltage in uVolts.. convert to
333 #define SUB_POWER(name) do { if (ignorelist_match(power_ignore,#name)==0) \
334 mic_submit_power(mic,"power",#name,(gauge_t)power_use.name.prr*0.000001); \
336 #define SUB_VOLTS(name) do { if (ignorelist_match(power_ignore,#name)==0) {\
337 mic_submit_power(mic,"power",#name,(gauge_t)(power_use.name.pwr*0.000001)); \
338 mic_submit_power(mic,"current",#name,(gauge_t)(power_use.name.cur*0.001)); \
339 mic_submit_power(mic,"voltage",#name,(gauge_t)(power_use.name.volt*0.000001)); \
356 static int mic_read (void)
362 for (int i=0;i<num_mics;i++) {
363 ret = MicInitAdapter(&mic_handle,&mics[i]);
364 if (ret != MIC_ACCESS_API_SUCCESS) {
365 ERROR("mic plugin: Problem initializing MicAdapter: %s",
366 MicGetErrorString(ret));
370 if (error == 0 && show_memory)
371 error = mic_read_memory(i);
373 if (error == 0 && show_temps)
374 error = mic_read_temps(i);
376 if (error == 0 && (show_cpu || show_cpu_cores))
377 error = mic_read_cpu(i);
379 if (error == 0 && (show_power))
380 error = mic_read_power(i);
382 ret = MicCloseAdapter(mic_handle);
383 if (ret != MIC_ACCESS_API_SUCCESS) {
384 ERROR("mic plugin: Problem closing MicAdapter: %s",
385 MicGetErrorString(ret));
396 static int mic_shutdown (void)
399 MicCloseAPI(&mic_handle);
405 void module_register (void)
407 plugin_register_init ("mic", mic_init);
408 plugin_register_shutdown ("mic", mic_shutdown);
409 plugin_register_read ("mic", mic_read);
410 plugin_register_config ("mic",mic_config, config_keys, config_keys_num);
411 } /* void module_register */
414 * vim: set shiftwidth=8 softtabstop=8 noet textwidth=78 :