fix copy framework error
[collectd.git] / src / mic.c
1 /**
2  * collectd - src/mic.c
3  * Copyright (C) 2013  
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@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 numMics = MAX_MICS;
37 static HANDLE micHandle=NULL;
38 #define NUM_THERMS 7
39 static const int therms[NUM_THERMS] = {eMicThermalDie,eMicThermalDevMem,eMicThermalFin,eMicThermalFout,eMicThermalVccp,eMicThermalVddg,eMicThermalVddq};
40 static const char *thermNames[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
64   ret = MicInitAPI(&micHandle,  eTARGET_SCIF_DRIVER, mics, &numMics);
65   if (ret != MIC_ACCESS_API_SUCCESS) {
66         ERROR("Problem initializing MicAccessAPI: %s",MicGetErrorString(ret));
67   }
68   DEBUG("MICs found: %d",numMics);
69   
70   if (numMics<0 || numMics>=MAX_MICS)
71         return (1);
72   else
73         return (0);
74 }
75
76 static int mic_config (const char *key, const char *value) {
77   if (temp_ignore == NULL)
78         temp_ignore = ignorelist_create(1);
79   if (temp_ignore == NULL)
80         return (1);
81
82   if (strcasecmp("ShowTotalCPU",key) == 0)
83   {
84         show_total_cpu = IS_TRUE(value);
85   }
86   else if (strcasecmp("ShowPerCPU",key) == 0)
87   {
88         show_per_cpu = IS_TRUE(value);
89   }
90   else if (strcasecmp("ShowTemps",key) == 0)
91   {
92         show_temps = IS_TRUE(value);
93   }
94   else if (strcasecmp("ShowMemory",key) == 0)
95   {
96         show_memory = IS_TRUE(value);
97   }
98   else if (strcasecmp("TempSensor",key) == 0)
99   {
100         ignorelist_add(temp_ignore,value);
101   }
102   else if (strcasecmp("IgnoreTempSelected",key) == 0)
103   {
104         int invert = 1;
105         if (IS_TRUE(value))
106           invert = 0;
107         ignorelist_set_invert(temp_ignore,invert);
108   }
109   else
110   {
111         return (-1);
112   }
113   return (0);
114 }
115
116 static void mic_submit_memory_use(int micnumber, const char *type, gauge_t val)
117 {
118   value_t values[1];
119   value_list_t vl = VALUE_LIST_INIT;
120
121   values[0].gauge = val;
122
123   vl.values=values;
124   vl.values_len=1;
125
126   strncpy (vl.host, hostname_g, sizeof (vl.host));
127   strncpy (vl.plugin, "mic", sizeof (vl.plugin));
128   ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance), "%i", micnumber);
129   strncpy (vl.type, "memory", sizeof (vl.type));
130   strncpy (vl.type_instance, type, sizeof (vl.type_instance));
131
132   plugin_dispatch_values (&vl);
133
134
135 /* Gather memory Utilization */
136 static int mic_read_memory(int mic)
137 {
138   U32 ret;
139   U32 mem_total,mem_used,mem_bufs;
140   
141   ret = MicGetMemoryUtilization(micHandle,&mem_total,&mem_used,&mem_bufs);
142   if (ret != MIC_ACCESS_API_SUCCESS) {
143         ERROR("Problem getting Memory Utilization: %s",MicGetErrorString(ret));
144         return (1);
145   }
146   /* API reprots KB's of memory, adjust for this */ 
147   mic_submit_memory_use(mic,"total",mem_total*1024);
148   mic_submit_memory_use(mic,"used",mem_used*1024);
149   mic_submit_memory_use(mic,"bufs",mem_bufs*1024);
150   /*INFO("Memory Read: %u %u %u",mem_total,mem_used,mem_bufs);*/
151   return (0);
152 }
153
154 static void mic_submit_temp(int micnumber, const char *type, gauge_t val)
155 {
156   value_t values[1];
157   value_list_t vl = VALUE_LIST_INIT;
158
159   values[0].gauge = val;
160
161   vl.values=values;
162   vl.values_len=1;
163
164   strncpy (vl.host, hostname_g, sizeof (vl.host));
165   strncpy (vl.plugin, "mic", sizeof (vl.plugin));
166   ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance), "%i", micnumber);
167   strncpy (vl.type, "temperature", sizeof (vl.type));
168   strncpy (vl.type_instance, type, sizeof (vl.type_instance));
169
170   plugin_dispatch_values (&vl);
171
172
173 /* Gather Temperature Information */
174 static int mic_read_temps(int mic)
175 {
176   int j;
177   U32 ret;
178   U32 bufferSize;
179   U32 *tempBuffer;
180  
181   bufferSize = sizeof(U32);
182   tempBuffer = malloc(bufferSize);
183   for (j=0;j<NUM_THERMS;j++) {
184         if (ignorelist_match(temp_ignore,thermNames[j])!=0)
185           continue;
186         ret = MicGetTemperature(micHandle,therms[j],tempBuffer,&bufferSize);
187         if (ret != MIC_ACCESS_API_SUCCESS) {
188           ERROR("Problem getting Temperature(%d) %s",j,MicGetErrorString(ret));
189           return (1);
190         }
191         /*INFO("Temp Read: %u: %u %s",j,tempBuffer[0],thermNames[j]);*/
192         mic_submit_temp(mic,thermNames[j],tempBuffer[0]);
193   }
194   return (0);
195 }
196
197 static void mic_submit_cpu(int micnumber, const char *type, int core, derive_t val)
198 {
199   value_t values[1];
200   value_list_t vl = VALUE_LIST_INIT;
201
202   values[0].derive = val;
203
204   vl.values=values;
205   vl.values_len=1;
206
207   strncpy (vl.host, hostname_g, sizeof (vl.host));
208   strncpy (vl.plugin, "mic", sizeof (vl.plugin));
209   ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance), "%i", micnumber);
210   strncpy (vl.type, "cpu", sizeof (vl.type));
211   if (core < 0)
212         strncpy (vl.type_instance, type, sizeof (vl.type_instance));
213   else
214         ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%i-%s", core, type);
215
216   plugin_dispatch_values (&vl);
217
218
219 /*Gather CPU Utilization Information */
220 static int mic_read_cpu(int mic)
221 {
222   U32 ret;
223   U32 bufferSize;
224   int j;
225   MicCoreUtil coreUtil;
226   MicCoreJiff coreJiffs[MAX_CORES];
227
228   bufferSize=MAX_CORES*sizeof(MicCoreJiff);
229   ret = MicGetCoreUtilization(micHandle,&coreUtil,coreJiffs,&bufferSize);
230   if (ret != MIC_ACCESS_API_SUCCESS) {
231         ERROR("Problem getting CPU utilization: %s",MicGetErrorString(ret));
232         return(0);
233   }
234   if (show_total_cpu) {
235         mic_submit_cpu(mic,"user",-1,coreUtil.sum.user);
236         mic_submit_cpu(mic,"sys",-1,coreUtil.sum.sys);
237         mic_submit_cpu(mic,"nice",-1,coreUtil.sum.nice);
238         mic_submit_cpu(mic,"idle",-1,coreUtil.sum.idle);
239   }
240   if (show_per_cpu) {
241         for (j=0;j<coreUtil.core;j++) {
242           mic_submit_cpu(mic,"user",j,coreJiffs[j].user);
243           mic_submit_cpu(mic,"sys",j,coreJiffs[j].sys);
244           mic_submit_cpu(mic,"nice",j,coreJiffs[j].nice);
245           mic_submit_cpu(mic,"idle",j,coreJiffs[j].idle);
246         }
247   }
248   return (0);
249 }
250
251 static int mic_read (void)
252 {
253   int i;
254   U32 ret;
255   int error;
256
257   error=0;
258   for (i=0;i<numMics;i++) {
259         ret = MicInitAdapter(&micHandle,&mics[i]);
260         if (ret != MIC_ACCESS_API_SUCCESS) {
261           ERROR("Problem initializing MicAdapter: %s",MicGetErrorString(ret));
262           error=1;
263         }
264
265         if (error == 0 && show_memory)
266           error = mic_read_memory(i);
267
268         if (error == 0 && show_temps)
269           error = mic_read_temps(i);
270
271         if (error == 0 && (show_total_cpu || show_per_cpu))
272           error = mic_read_cpu(i);
273
274         ret = MicCloseAdapter(micHandle);
275         if (ret != MIC_ACCESS_API_SUCCESS) {
276           ERROR("Problem initializing MicAdapter: %s",MicGetErrorString(ret));
277           error=2;
278           break;
279         }
280   }
281   return error;
282 }
283
284
285 static int mic_shutdown (void)
286 {
287   if (micHandle)
288         MicCloseAPI(&micHandle);
289   return (0);
290 }
291
292 void module_register (void)
293 {
294   plugin_register_init ("mic", mic_init);
295   plugin_register_shutdown ("mic", mic_shutdown);
296   plugin_register_read ("mic", mic_read);
297   plugin_register_config ("mic",mic_config, config_keys, config_keys_num);
298 } /* void module_register */
299
300 /*
301  * vim: shiftwidth=2:softtabstop=2:textwidth=78
302  */