Initial implementation of build addition, and memory gathering for the mic card
[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
30 #define MAX_MICS 32
31
32 static MicDeviceOnSystem mics[MAX_MICS];
33 static U32 numMics = MAX_MICS;
34 static HANDLE micHandle=NULL;
35
36 static int mic_init (void)
37 {
38   U32 ret;
39
40   ret = MicInitAPI(&micHandle,  eTARGET_SCIF_DRIVER, mics, &numMics);
41   if (ret != MIC_ACCESS_API_SUCCESS) {
42         ERROR("Problem initializing MicAccessAPI: %s",MicGetErrorString(ret));
43   }
44   INFO("MICs found: %d",numMics);
45   if (numMics<0 || numMics>=MAX_MICS)
46         return (1);
47   else
48         return (0);
49 }
50
51 static void mic_submit_memory_use(int micnumber, char *type, gauge_t val)
52 {
53   value_t values[1];
54   value_list_t vl = VALUE_LIST_INIT;
55
56   values[0].gauge = val;
57
58   vl.values=values;
59   vl.values_len=1;
60
61   strncpy (vl.host, hostname_g, sizeof (vl.host));
62   strncpy (vl.plugin, "mic", sizeof (vl.plugin));
63   ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance), "%i", micnumber);
64   strncpy (vl.type, "memory", sizeof (vl.type));
65   strncpy (vl.type_instance, type, sizeof (vl.type_instance));
66
67   plugin_dispatch_values (&vl);
68
69
70 static int mic_read (void)
71 {
72   int i;
73   U32 ret;
74   int error;
75   U32 mem_total,mem_used,mem_bufs;
76
77   error=0;
78   for (i=0;i<numMics;i++) {
79         ret = MicInitAdapter(&micHandle,&mics[i]);
80         if (ret != MIC_ACCESS_API_SUCCESS) {
81           ERROR("Problem initializing MicAdapter: %s",MicGetErrorString(ret));
82           error=1;
83           break;
84         }
85
86         /* Gather memory Utilization */
87         ret = MicGetMemoryUtilization(micHandle,&mem_total,&mem_used,&mem_bufs);
88         if (ret != MIC_ACCESS_API_SUCCESS) {
89           ERROR("Problem initializing MicAdapter: %s",MicGetErrorString(ret));
90           error=3;
91           break;
92         }
93         mic_submit_memory_use(i,"total",mem_total);
94         mic_submit_memory_use(i,"used",mem_used);
95         mic_submit_memory_use(i,"bufs",mem_bufs);
96         /*INFO("Memory Read: %u %u %u",mem_total,mem_used,mem_bufs);*/
97
98         ret = MicCloseAdapter(micHandle);
99         if (ret != MIC_ACCESS_API_SUCCESS) {
100           ERROR("Problem initializing MicAdapter: %s",MicGetErrorString(ret));
101           error=2;
102           break;
103         }
104   }
105   return error;
106 }
107
108
109 static int mic_shutdown (void)
110 {
111   if (micHandle)
112         MicCloseAPI(micHandle);
113   return (0);
114 }
115
116 void module_register (void)
117 {
118   plugin_register_init ("mic", mic_init);
119   plugin_register_shutdown ("mic", mic_shutdown);
120   plugin_register_read ("mic", mic_read);
121 } /* void module_register */
122
123 /*
124  * vim: shiftwidth=2:softtabstop=2:textwidth=78
125  */