add cpu stat gathering
[collectd.git] / src / mic.c
1 /**
2  * collectd - src/xmms.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
26 #include <MicAccessTypes.h>
27 #include <MicAccessErrorTypes.h>
28 #include <MicAccessApi.h>
29 #include <MicThermalAPI.h>
30
31 #define MAX_MICS 32
32 #define MAX_CORES 256
33
34 static MicDeviceOnSystem mics[MAX_MICS];
35 static U32 numMics = MAX_MICS;
36 static HANDLE micHandle=NULL;
37 #define NUM_THERMS 7
38 static const int therms[NUM_THERMS] = {eMicThermalDie,eMicThermalDevMem,eMicThermalFin,eMicThermalFout,eMicThermalVccp,eMicThermalVddg,eMicThermalVddq};
39 static const char *thermNames[NUM_THERMS] = {"die","devmem","fin","fout","vccp","vddg","vddq"};
40
41
42 static int mic_init (void)
43 {
44   U32 ret;
45
46   ret = MicInitAPI(&micHandle,  eTARGET_SCIF_DRIVER, mics, &numMics);
47   if (ret != MIC_ACCESS_API_SUCCESS) {
48         ERROR("Problem initializing MicAccessAPI: %s",MicGetErrorString(ret));
49   }
50   INFO("MICs found: %d",numMics);
51   if (numMics<0 || numMics>=MAX_MICS)
52         return (1);
53   else
54         return (0);
55 }
56
57 static void mic_submit_memory_use(int micnumber, const char *type, gauge_t val)
58 {
59   value_t values[1];
60   value_list_t vl = VALUE_LIST_INIT;
61
62   values[0].gauge = val;
63
64   vl.values=values;
65   vl.values_len=1;
66
67   strncpy (vl.host, hostname_g, sizeof (vl.host));
68   strncpy (vl.plugin, "mic", sizeof (vl.plugin));
69   ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance), "%i", micnumber);
70   strncpy (vl.type, "memory", sizeof (vl.type));
71   strncpy (vl.type_instance, type, sizeof (vl.type_instance));
72
73   plugin_dispatch_values (&vl);
74
75
76 static void mic_submit_temp(int micnumber, const char *type, gauge_t val)
77 {
78   value_t values[1];
79   value_list_t vl = VALUE_LIST_INIT;
80
81   values[0].gauge = val;
82
83   vl.values=values;
84   vl.values_len=1;
85
86   strncpy (vl.host, hostname_g, sizeof (vl.host));
87   strncpy (vl.plugin, "mic", sizeof (vl.plugin));
88   ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance), "%i", micnumber);
89   strncpy (vl.type, "temperature", sizeof (vl.type));
90   strncpy (vl.type_instance, type, sizeof (vl.type_instance));
91
92   plugin_dispatch_values (&vl);
93
94
95 static void mic_submit_cpu(int micnumber, const char *type, int core, derive_t val)
96 {
97   value_t values[1];
98   value_list_t vl = VALUE_LIST_INIT;
99
100   values[0].derive = val;
101
102   vl.values=values;
103   vl.values_len=1;
104
105   strncpy (vl.host, hostname_g, sizeof (vl.host));
106   strncpy (vl.plugin, "mic", sizeof (vl.plugin));
107   ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance), "%i", micnumber);
108   strncpy (vl.type, "cpu", sizeof (vl.type));
109   if (core < 0)
110         strncpy (vl.type_instance, type, sizeof (vl.type_instance));
111   else
112         ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%i-%s", core, type);
113
114   plugin_dispatch_values (&vl);
115
116
117
118
119 static int mic_read (void)
120 {
121   int i,j;
122   U32 ret,bufferSize;
123   U32 *tempBuffer;
124   int error;
125   U32 mem_total,mem_used,mem_bufs;
126   MicCoreUtil coreUtil;
127   MicCoreJiff coreJiffs[MAX_CORES];
128
129   error=0;
130   for (i=0;i<numMics;i++) {
131         ret = MicInitAdapter(&micHandle,&mics[i]);
132         if (ret != MIC_ACCESS_API_SUCCESS) {
133           ERROR("Problem initializing MicAdapter: %s",MicGetErrorString(ret));
134           error=1;
135           break;
136         }
137
138         /* Gather memory Utilization */
139         ret = MicGetMemoryUtilization(micHandle,&mem_total,&mem_used,&mem_bufs);
140         if (ret != MIC_ACCESS_API_SUCCESS) {
141           ERROR("Problem getting Memory Utilization: %s",MicGetErrorString(ret));
142           error=3;
143           break;
144         }
145         /* API reprots KB's of memory, adjust for this */ 
146         mic_submit_memory_use(i,"total",mem_total*1024);
147         mic_submit_memory_use(i,"used",mem_used*1024);
148         mic_submit_memory_use(i,"bufs",mem_bufs*1024);
149         /*INFO("Memory Read: %u %u %u",mem_total,mem_used,mem_bufs);*/
150
151         /* Gather Temperature Information */
152         bufferSize = sizeof(U32);
153         tempBuffer = malloc(bufferSize);
154         for (j=0;j<NUM_THERMS;j++) {
155           ret = MicGetTemperature(micHandle,therms[j],tempBuffer,&bufferSize);
156           if (ret != MIC_ACCESS_API_SUCCESS) {
157                 ERROR("Problem getting Temperature(%d) %s",j,MicGetErrorString(ret));
158                 error=4;
159                 break;
160           }
161           /*INFO("Temp Read: %u: %u %s",j,tempBuffer[0],thermNames[j]);*/
162           mic_submit_temp(i,thermNames[j],tempBuffer[0]);
163         }
164         if (error)
165           break;
166
167         /*Gather CPU Utilization Information */
168         bufferSize=MAX_CORES*sizeof(MicCoreJiff);
169         ret = MicGetCoreUtilization(micHandle,&coreUtil,coreJiffs,&bufferSize);
170         if (ret != MIC_ACCESS_API_SUCCESS) {
171           ERROR("Problem getting CPU utilization: %s",MicGetErrorString(ret));
172           error=5;
173           break;
174         }
175         mic_submit_cpu(i,"user",-1,coreUtil.sum.user);
176         mic_submit_cpu(i,"sys",-1,coreUtil.sum.sys);
177         mic_submit_cpu(i,"nice",-1,coreUtil.sum.nice);
178         mic_submit_cpu(i,"idle",-1,coreUtil.sum.idle);
179         for (j=0;j<coreUtil.core;j++) {
180           mic_submit_cpu(i,"user",j,coreJiffs[j].user);
181           mic_submit_cpu(i,"sys",j,coreJiffs[j].sys);
182           mic_submit_cpu(i,"nice",j,coreJiffs[j].nice);
183           mic_submit_cpu(i,"idle",j,coreJiffs[j].idle);
184         }
185
186         ret = MicCloseAdapter(micHandle);
187         if (ret != MIC_ACCESS_API_SUCCESS) {
188           ERROR("Problem initializing MicAdapter: %s",MicGetErrorString(ret));
189           error=2;
190           break;
191         }
192   }
193   return error;
194 }
195
196
197 static int mic_shutdown (void)
198 {
199   if (micHandle)
200         MicCloseAPI(&micHandle);
201   return (0);
202 }
203
204 void module_register (void)
205 {
206   plugin_register_init ("mic", mic_init);
207   plugin_register_shutdown ("mic", mic_shutdown);
208   plugin_register_read ("mic", mic_read);
209 } /* void module_register */
210
211 /*
212  * vim: shiftwidth=2:softtabstop=2:textwidth=78
213  */