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",MicGetErrorString(ret));
76 }
77 DEBUG("mic plugin: found: %"PRIu32" MIC(s)",mic_count);
79 if (mic_count<0 || mic_count>=MAX_MICS) {
80 ERROR("mic plugin: No Intel MICs in system");
81 return (1);
82 }
83 else {
84 num_mics = mic_count;
85 return (0);
86 }
87 }
89 static int mic_config (const char *key, const char *value) {
90 if (temp_ignore == NULL)
91 temp_ignore = ignorelist_create(1);
92 if (temp_ignore == NULL)
93 return (1);
95 if (strcasecmp("ShowTotalCPU",key) == 0)
96 {
97 show_total_cpu = IS_TRUE(value);
98 }
99 else if (strcasecmp("ShowPerCPU",key) == 0)
100 {
101 show_per_cpu = IS_TRUE(value);
102 }
103 else if (strcasecmp("ShowTemps",key) == 0)
104 {
105 show_temps = IS_TRUE(value);
106 }
107 else if (strcasecmp("ShowMemory",key) == 0)
108 {
109 show_memory = IS_TRUE(value);
110 }
111 else if (strcasecmp("TempSensor",key) == 0)
112 {
113 ignorelist_add(temp_ignore,value);
114 }
115 else if (strcasecmp("IgnoreTempSelected",key) == 0)
116 {
117 int invert = 1;
118 if (IS_TRUE(value))
119 invert = 0;
120 ignorelist_set_invert(temp_ignore,invert);
121 }
122 else
123 {
124 return (-1);
125 }
126 return (0);
127 }
129 static void mic_submit_memory_use(int micnumber, const char *type_instance, U32 val)
130 {
131 value_t values[1];
132 value_list_t vl = VALUE_LIST_INIT;
134 /* MicAccessAPI reports KB's of memory, adjust for this */
135 DEBUG("mic plugin: Memory Value Report; %u %lf",val,((gauge_t)val)*1024.0);
136 values[0].gauge = ((gauge_t)val)*1024.0;
138 vl.values=values;
139 vl.values_len=1;
141 strncpy (vl.host, hostname_g, sizeof (vl.host));
142 strncpy (vl.plugin, "mic", sizeof (vl.plugin));
143 ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance), "%i", micnumber);
144 strncpy (vl.type, "memory", sizeof (vl.type));
145 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
147 plugin_dispatch_values (&vl);
148 }
150 /* Gather memory Utilization */
151 static int mic_read_memory(int mic)
152 {
153 U32 ret;
154 U32 mem_total,mem_free,mem_bufs;
156 ret = MicGetMemoryUtilization(mic_handle,&mem_total,&mem_free,&mem_bufs);
157 if (ret != MIC_ACCESS_API_SUCCESS) {
158 ERROR("mic plugin: Problem getting Memory Utilization: %s",MicGetErrorString(ret));
159 return (1);
160 }
161 mic_submit_memory_use(mic,"free",mem_free);
162 mic_submit_memory_use(mic,"used",mem_total-mem_free-mem_bufs);
163 mic_submit_memory_use(mic,"buffered",mem_bufs);
164 DEBUG("mic plugin: Memory Read: %u %u %u",mem_total,mem_free,mem_bufs);
165 return (0);
166 }
168 static void mic_submit_temp(int micnumber, const char *type, gauge_t val)
169 {
170 value_t values[1];
171 value_list_t vl = VALUE_LIST_INIT;
173 values[0].gauge = val;
175 vl.values=values;
176 vl.values_len=1;
178 strncpy (vl.host, hostname_g, sizeof (vl.host));
179 strncpy (vl.plugin, "mic", sizeof (vl.plugin));
180 ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance), "%i", micnumber);
181 strncpy (vl.type, "temperature", sizeof (vl.type));
182 strncpy (vl.type_instance, type, sizeof (vl.type_instance));
184 plugin_dispatch_values (&vl);
185 }
187 /* Gather Temperature Information */
188 static int mic_read_temps(int mic)
189 {
190 size_t num_therms = STATIC_ARRAY_SIZE(therm_ids);
191 size_t j;
193 for (j = 0; j < num_therms; j++) {
194 U32 status;
195 U32 temp_buffer;
196 U32 buffer_size = (U32)sizeof(temp_buffer);
197 char const *name = therm_names[j];
199 if (ignorelist_match(temp_ignore, name) != 0)
200 continue;
202 status = MicGetTemperature(mic_handle, therm_ids[j],
203 &temp_buffer, &buffer_size);
204 if (status != MIC_ACCESS_API_SUCCESS) {
205 ERROR("mic plugin: Error reading temperature \"%s\": "
206 "%s", name, MicGetErrorString(status));
207 return (1);
208 }
209 mic_submit_temp(mic, name, temp_buffer);
210 }
211 return (0);
212 }
214 static void mic_submit_cpu(int micnumber, const char *type_instance, int core, derive_t val)
215 {
216 value_t values[1];
217 value_list_t vl = VALUE_LIST_INIT;
219 values[0].derive = val;
221 vl.values=values;
222 vl.values_len=1;
224 strncpy (vl.host, hostname_g, sizeof (vl.host));
225 strncpy (vl.plugin, "mic", sizeof (vl.plugin));
226 ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance), "%i", micnumber);
227 strncpy (vl.type, "cpu", sizeof (vl.type));
228 if (core < 0)
229 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
230 else
231 ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%i-%s", core, type_instance);
233 plugin_dispatch_values (&vl);
234 }
236 /*Gather CPU Utilization Information */
237 static int mic_read_cpu(int mic)
238 {
239 U32 ret;
240 U32 buffer_size;
241 int j;
242 MicCoreUtil core_util;
243 MicCoreJiff core_jiffs[MAX_CORES];
245 buffer_size=MAX_CORES*sizeof(MicCoreJiff);
246 ret = MicGetCoreUtilization(mic_handle,&core_util,core_jiffs,&buffer_size);
247 if (ret != MIC_ACCESS_API_SUCCESS) {
248 ERROR("mic plugin: Problem getting CPU utilization: %s",MicGetErrorString(ret));
249 return(0);
250 }
251 if (show_total_cpu) {
252 mic_submit_cpu(mic,"user",-1,core_util.sum.user);
253 mic_submit_cpu(mic,"sys",-1,core_util.sum.sys);
254 mic_submit_cpu(mic,"nice",-1,core_util.sum.nice);
255 mic_submit_cpu(mic,"idle",-1,core_util.sum.idle);
256 }
257 if (show_per_cpu) {
258 for (j=0;j<core_util.core;j++) {
259 mic_submit_cpu(mic,"user",j,core_jiffs[j].user);
260 mic_submit_cpu(mic,"sys",j,core_jiffs[j].sys);
261 mic_submit_cpu(mic,"nice",j,core_jiffs[j].nice);
262 mic_submit_cpu(mic,"idle",j,core_jiffs[j].idle);
263 }
264 }
265 return (0);
266 }
268 static int mic_read (void)
269 {
270 int i;
271 U32 ret;
272 int error;
274 error=0;
275 for (i=0;i<num_mics;i++) {
276 ret = MicInitAdapter(&mic_handle,&mics[i]);
277 if (ret != MIC_ACCESS_API_SUCCESS) {
278 ERROR("mic plugin: Problem initializing MicAdapter: %s",MicGetErrorString(ret));
279 error=1;
280 }
282 if (error == 0 && show_memory)
283 error = mic_read_memory(i);
285 if (error == 0 && show_temps)
286 error = mic_read_temps(i);
288 if (error == 0 && (show_total_cpu || show_per_cpu))
289 error = mic_read_cpu(i);
291 ret = MicCloseAdapter(mic_handle);
292 if (ret != MIC_ACCESS_API_SUCCESS) {
293 ERROR("mic plugin: Problem closing MicAdapter: %s",MicGetErrorString(ret));
294 error=2;
295 break;
296 }
297 }
298 if (num_mics==0)
299 error=3;
300 return error;
301 }
304 static int mic_shutdown (void)
305 {
306 if (mic_handle)
307 MicCloseAPI(&mic_handle);
308 return (0);
309 }
311 void module_register (void)
312 {
313 plugin_register_init ("mic", mic_init);
314 plugin_register_shutdown ("mic", mic_shutdown);
315 plugin_register_read ("mic", mic_read);
316 plugin_register_config ("mic",mic_config, config_keys, config_keys_num);
317 } /* void module_register */
319 /*
320 * vim: set shiftwidth=8 softtabstop=8 noet textwidth=78 :
321 */