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@pnnl.gov>
25 #include "utils_ignorelist.h"
27 #include <MicAccessTypes.h>
28 #include <MicAccessErrorTypes.h>
29 #include <MicAccessApi.h>
30 #include <MicThermalAPI.h>
35 static MicDeviceOnSystem mics[MAX_MICS];
36 static U32 num_mics = MAX_MICS;
37 static HANDLE mic_handle = NULL;
39 static const int therms[NUM_THERMS] = {eMicThermalDie,eMicThermalDevMem,eMicThermalFin,eMicThermalFout,eMicThermalVccp,eMicThermalVddg,eMicThermalVddq};
40 static const char *therm_names[NUM_THERMS] = {"die","devmem","fin","fout","vccp","vddg","vddq"};
42 static const char *config_keys[] =
51 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
53 static _Bool show_total_cpu = 1;
54 static _Bool show_per_cpu = 1;
55 static _Bool show_temps = 1;
56 static _Bool show_memory = 1;
57 static ignorelist_t *temp_ignore = NULL;
60 static int mic_init (void)
67 ret = MicInitAPI(&mic_handle, eTARGET_SCIF_DRIVER, mics, &num_mics);
68 if (ret != MIC_ACCESS_API_SUCCESS) {
69 ERROR("Problem initializing MicAccessAPI: %s",MicGetErrorString(ret));
71 DEBUG("MICs found: %d",num_mics);
73 if (num_mics<0 || num_mics>=MAX_MICS) {
74 ERROR("No Intel MICs in system");
81 static int mic_config (const char *key, const char *value) {
82 if (temp_ignore == NULL)
83 temp_ignore = ignorelist_create(1);
84 if (temp_ignore == NULL)
87 if (strcasecmp("ShowTotalCPU",key) == 0)
89 show_total_cpu = IS_TRUE(value);
91 else if (strcasecmp("ShowPerCPU",key) == 0)
93 show_per_cpu = IS_TRUE(value);
95 else if (strcasecmp("ShowTemps",key) == 0)
97 show_temps = IS_TRUE(value);
99 else if (strcasecmp("ShowMemory",key) == 0)
101 show_memory = IS_TRUE(value);
103 else if (strcasecmp("TempSensor",key) == 0)
105 ignorelist_add(temp_ignore,value);
107 else if (strcasecmp("IgnoreTempSelected",key) == 0)
112 ignorelist_set_invert(temp_ignore,invert);
121 static void mic_submit_memory_use(int micnumber, const char *type_instance, U32 val)
124 value_list_t vl = VALUE_LIST_INIT;
126 /* MicAccessAPI reports KB's of memory, adjust for this */
127 DEBUG("Memory Value Report; %u %lf",val,((gauge_t)val)*1024.0);
128 values[0].gauge = ((gauge_t)val)*1024.0;
133 strncpy (vl.host, hostname_g, sizeof (vl.host));
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));
139 plugin_dispatch_values (&vl);
142 /* Gather memory Utilization */
143 static int mic_read_memory(int mic)
146 U32 mem_total,mem_free,mem_bufs;
148 ret = MicGetMemoryUtilization(mic_handle,&mem_total,&mem_free,&mem_bufs);
149 if (ret != MIC_ACCESS_API_SUCCESS) {
150 ERROR("Problem getting Memory Utilization: %s",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("Memory Read: %u %u %u",mem_total,mem_free,mem_bufs);
160 static void mic_submit_temp(int micnumber, const char *type, gauge_t val)
163 value_list_t vl = VALUE_LIST_INIT;
165 values[0].gauge = val;
170 strncpy (vl.host, hostname_g, sizeof (vl.host));
171 strncpy (vl.plugin, "mic", sizeof (vl.plugin));
172 ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance), "%i", micnumber);
173 strncpy (vl.type, "temperature", sizeof (vl.type));
174 strncpy (vl.type_instance, type, sizeof (vl.type_instance));
176 plugin_dispatch_values (&vl);
179 /* Gather Temperature Information */
180 static int mic_read_temps(int mic)
185 U32 buffer_size = (U32)sizeof(temp_buffer);
187 for (j=0;j<NUM_THERMS;j++) {
188 if (ignorelist_match(temp_ignore,therm_names[j])!=0)
190 ret = MicGetTemperature(mic_handle,therms[j],&temp_buffer,&buffer_size);
191 if (ret != MIC_ACCESS_API_SUCCESS) {
192 ERROR("Problem getting Temperature(%d) %s",j,MicGetErrorString(ret));
195 mic_submit_temp(mic,therm_names[j],temp_buffer);
200 static void mic_submit_cpu(int micnumber, const char *type_instance, int core, derive_t val)
203 value_list_t vl = VALUE_LIST_INIT;
205 values[0].derive = val;
210 strncpy (vl.host, hostname_g, sizeof (vl.host));
211 strncpy (vl.plugin, "mic", sizeof (vl.plugin));
212 ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance), "%i", micnumber);
213 strncpy (vl.type, "cpu", sizeof (vl.type));
215 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
217 ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%i-%s", core, type_instance);
219 plugin_dispatch_values (&vl);
222 /*Gather CPU Utilization Information */
223 static int mic_read_cpu(int mic)
228 MicCoreUtil core_util;
229 MicCoreJiff core_jiffs[MAX_CORES];
231 buffer_size=MAX_CORES*sizeof(MicCoreJiff);
232 ret = MicGetCoreUtilization(mic_handle,&core_util,core_jiffs,&buffer_size);
233 if (ret != MIC_ACCESS_API_SUCCESS) {
234 ERROR("Problem getting CPU utilization: %s",MicGetErrorString(ret));
237 if (show_total_cpu) {
238 mic_submit_cpu(mic,"user",-1,core_util.sum.user);
239 mic_submit_cpu(mic,"sys",-1,core_util.sum.sys);
240 mic_submit_cpu(mic,"nice",-1,core_util.sum.nice);
241 mic_submit_cpu(mic,"idle",-1,core_util.sum.idle);
244 for (j=0;j<core_util.core;j++) {
245 mic_submit_cpu(mic,"user",j,core_jiffs[j].user);
246 mic_submit_cpu(mic,"sys",j,core_jiffs[j].sys);
247 mic_submit_cpu(mic,"nice",j,core_jiffs[j].nice);
248 mic_submit_cpu(mic,"idle",j,core_jiffs[j].idle);
254 static int mic_read (void)
261 for (i=0;i<num_mics;i++) {
262 ret = MicInitAdapter(&mic_handle,&mics[i]);
263 if (ret != MIC_ACCESS_API_SUCCESS) {
264 ERROR("Problem initializing MicAdapter: %s",MicGetErrorString(ret));
268 if (error == 0 && show_memory)
269 error = mic_read_memory(i);
271 if (error == 0 && show_temps)
272 error = mic_read_temps(i);
274 if (error == 0 && (show_total_cpu || show_per_cpu))
275 error = mic_read_cpu(i);
277 ret = MicCloseAdapter(mic_handle);
278 if (ret != MIC_ACCESS_API_SUCCESS) {
279 ERROR("Problem closing MicAdapter: %s",MicGetErrorString(ret));
290 static int mic_shutdown (void)
293 MicCloseAPI(&mic_handle);
297 void module_register (void)
299 plugin_register_init ("mic", mic_init);
300 plugin_register_shutdown ("mic", mic_shutdown);
301 plugin_register_read ("mic", mic_read);
302 plugin_register_config ("mic",mic_config, config_keys, config_keys_num);
303 } /* void module_register */
306 * vim: shiftwidth=2:softtabstop=2:textwidth=78