AUTHORS, README: Add Evan and the mic plugin.
[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 #include "plugin.h"
24 #include "common.h"
25 #include "utils_ignorelist.h"
26
27 #include <MicAccessTypes.h>
28 #include <MicAccessErrorTypes.h>
29 #include <MicAccessApi.h>
30 #include <MicThermalAPI.h>
31
32 #define MAX_MICS 32
33 #define MAX_CORES 256
34
35 static MicDeviceOnSystem mics[MAX_MICS];
36 static U32 num_mics = 0;
37 static HANDLE mic_handle = NULL;
38 #define NUM_THERMS 7
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"};
41
42 static const char *config_keys[] =
43 {
44         "ShowTotalCPU",
45         "ShowPerCPU",
46         "ShowTemps",
47         "ShowMemory",
48         "TempSensor",
49         "IgnoreTempSelected",
50 };
51 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
52
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;
58
59
60 static int mic_init (void)
61 {
62         U32 ret;
63         U32 mic_count;
64
65         if (mic_handle)
66                 return (0);
67
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",MicGetErrorString(ret));
72         }
73         DEBUG("mic plugin: found: %"PRIu32" MIC(s)",mic_count);
74         
75         if (mic_count<0 || mic_count>=MAX_MICS) {
76                 ERROR("mic plugin: No Intel MICs in system");
77                 return (1);
78         }
79         else {
80                 num_mics = mic_count;
81                 return (0);
82         }
83 }
84
85 static int mic_config (const char *key, const char *value) {
86         if (temp_ignore == NULL)
87                 temp_ignore = ignorelist_create(1);
88         if (temp_ignore == NULL)
89                 return (1);
90
91         if (strcasecmp("ShowTotalCPU",key) == 0)
92         {
93                 show_total_cpu = IS_TRUE(value);
94         }
95         else if (strcasecmp("ShowPerCPU",key) == 0)
96         {
97                 show_per_cpu = IS_TRUE(value);
98         }
99         else if (strcasecmp("ShowTemps",key) == 0)
100         {
101                 show_temps = IS_TRUE(value);
102         }
103         else if (strcasecmp("ShowMemory",key) == 0)
104         {
105                 show_memory = IS_TRUE(value);
106         }
107         else if (strcasecmp("TempSensor",key) == 0)
108         {
109                 ignorelist_add(temp_ignore,value);
110         }
111         else if (strcasecmp("IgnoreTempSelected",key) == 0)
112         {
113                 int invert = 1;
114                 if (IS_TRUE(value))
115                         invert = 0;
116                 ignorelist_set_invert(temp_ignore,invert);
117         }
118         else
119         {
120                 return (-1);
121         }
122         return (0);
123 }
124
125 static void mic_submit_memory_use(int micnumber, const char *type_instance, U32 val)
126 {
127         value_t values[1];
128         value_list_t vl = VALUE_LIST_INIT;
129
130         /* MicAccessAPI reports KB's of memory, adjust for this */ 
131         DEBUG("mic plugin: Memory Value Report; %u %lf",val,((gauge_t)val)*1024.0);
132         values[0].gauge = ((gauge_t)val)*1024.0;
133
134         vl.values=values;
135         vl.values_len=1;
136
137         strncpy (vl.host, hostname_g, sizeof (vl.host));
138         strncpy (vl.plugin, "mic", sizeof (vl.plugin));
139         ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance), "%i", micnumber);
140         strncpy (vl.type, "memory", sizeof (vl.type));
141         strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
142
143         plugin_dispatch_values (&vl);
144
145
146 /* Gather memory Utilization */
147 static int mic_read_memory(int mic)
148 {
149         U32 ret;
150         U32 mem_total,mem_free,mem_bufs;
151         
152         ret = MicGetMemoryUtilization(mic_handle,&mem_total,&mem_free,&mem_bufs);
153         if (ret != MIC_ACCESS_API_SUCCESS) {
154                 ERROR("mic plugin: Problem getting Memory Utilization: %s",MicGetErrorString(ret));
155                 return (1);
156         }
157         mic_submit_memory_use(mic,"free",mem_free);
158         mic_submit_memory_use(mic,"used",mem_total-mem_free-mem_bufs);
159         mic_submit_memory_use(mic,"buffered",mem_bufs);
160         DEBUG("mic plugin: Memory Read: %u %u %u",mem_total,mem_free,mem_bufs);
161         return (0);
162 }
163
164 static void mic_submit_temp(int micnumber, const char *type, gauge_t val)
165 {
166         value_t values[1];
167         value_list_t vl = VALUE_LIST_INIT;
168
169         values[0].gauge = val;
170
171         vl.values=values;
172         vl.values_len=1;
173
174         strncpy (vl.host, hostname_g, sizeof (vl.host));
175         strncpy (vl.plugin, "mic", sizeof (vl.plugin));
176         ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance), "%i", micnumber);
177         strncpy (vl.type, "temperature", sizeof (vl.type));
178         strncpy (vl.type_instance, type, sizeof (vl.type_instance));
179
180         plugin_dispatch_values (&vl);
181
182
183 /* Gather Temperature Information */
184 static int mic_read_temps(int mic)
185 {
186         int j;
187         U32 ret;
188         U32 temp_buffer;
189         U32 buffer_size = (U32)sizeof(temp_buffer);
190         
191         for (j=0;j<NUM_THERMS;j++) {
192                 if (ignorelist_match(temp_ignore,therm_names[j])!=0)
193                         continue;
194                 ret = MicGetTemperature(mic_handle,therms[j],&temp_buffer,&buffer_size);
195                 if (ret != MIC_ACCESS_API_SUCCESS) {
196                         ERROR("mic plugin: Problem getting Temperature(%d) %s",j,MicGetErrorString(ret));
197                         return (1);
198                 }
199                 mic_submit_temp(mic,therm_names[j],temp_buffer);
200         }
201         return (0);
202 }
203
204 static void mic_submit_cpu(int micnumber, const char *type_instance, int core, derive_t val)
205 {
206         value_t values[1];
207         value_list_t vl = VALUE_LIST_INIT;
208
209         values[0].derive = val;
210
211         vl.values=values;
212         vl.values_len=1;
213
214         strncpy (vl.host, hostname_g, sizeof (vl.host));
215         strncpy (vl.plugin, "mic", sizeof (vl.plugin));
216         ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance), "%i", micnumber);
217         strncpy (vl.type, "cpu", sizeof (vl.type));
218         if (core < 0)
219                 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
220         else
221                 ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%i-%s", core, type_instance);
222
223         plugin_dispatch_values (&vl);
224
225
226 /*Gather CPU Utilization Information */
227 static int mic_read_cpu(int mic)
228 {
229         U32 ret;
230         U32 buffer_size;
231         int j;
232         MicCoreUtil core_util;
233         MicCoreJiff core_jiffs[MAX_CORES];
234
235         buffer_size=MAX_CORES*sizeof(MicCoreJiff);
236         ret = MicGetCoreUtilization(mic_handle,&core_util,core_jiffs,&buffer_size);
237         if (ret != MIC_ACCESS_API_SUCCESS) {
238                 ERROR("mic plugin: Problem getting CPU utilization: %s",MicGetErrorString(ret));
239                 return(0);
240         }
241         if (show_total_cpu) {
242                 mic_submit_cpu(mic,"user",-1,core_util.sum.user);
243                 mic_submit_cpu(mic,"sys",-1,core_util.sum.sys);
244                 mic_submit_cpu(mic,"nice",-1,core_util.sum.nice);
245                 mic_submit_cpu(mic,"idle",-1,core_util.sum.idle);
246         }
247         if (show_per_cpu) {
248                 for (j=0;j<core_util.core;j++) {
249                         mic_submit_cpu(mic,"user",j,core_jiffs[j].user);
250                         mic_submit_cpu(mic,"sys",j,core_jiffs[j].sys);
251                         mic_submit_cpu(mic,"nice",j,core_jiffs[j].nice);
252                         mic_submit_cpu(mic,"idle",j,core_jiffs[j].idle);
253                 }
254         }
255         return (0);
256 }
257
258 static int mic_read (void)
259 {
260         int i;
261         U32 ret;
262         int error;
263
264         error=0;
265         for (i=0;i<num_mics;i++) {
266                 ret = MicInitAdapter(&mic_handle,&mics[i]);
267                 if (ret != MIC_ACCESS_API_SUCCESS) {
268                         ERROR("mic plugin: Problem initializing MicAdapter: %s",MicGetErrorString(ret));
269                         error=1;
270                 }
271
272                 if (error == 0 && show_memory)
273                         error = mic_read_memory(i);
274
275                 if (error == 0 && show_temps)
276                         error = mic_read_temps(i);
277
278                 if (error == 0 && (show_total_cpu || show_per_cpu))
279                         error = mic_read_cpu(i);
280
281                 ret = MicCloseAdapter(mic_handle);
282                 if (ret != MIC_ACCESS_API_SUCCESS) {
283                         ERROR("mic plugin: Problem closing MicAdapter: %s",MicGetErrorString(ret));
284                         error=2;
285                         break;
286                 }
287         }
288         if (num_mics==0)
289                 error=3;
290         return error;
291 }
292
293
294 static int mic_shutdown (void)
295 {
296         if (mic_handle)
297         MicCloseAPI(&mic_handle);
298         return (0);
299 }
300
301 void module_register (void)
302 {
303         plugin_register_init ("mic", mic_init);
304         plugin_register_shutdown ("mic", mic_shutdown);
305         plugin_register_read ("mic", mic_read);
306         plugin_register_config ("mic",mic_config, config_keys, config_keys_num);
307 } /* void module_register */
308
309 /*
310  * vim: shiftwidth=2:softtabstop=2:textwidth=78
311  */