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