Merge "Introduce StatsAPI and it's initial implementation"
[govpp.git] / adapter / vppapiclient / stat_client.go
1 // Copyright (c) 2018 Cisco and/or its affiliates.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at:
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 // +build !windows,!darwin
16
17 package vppapiclient
18
19 /*
20 #cgo CFLAGS: -DPNG_DEBUG=1
21 #cgo LDFLAGS: -lvppapiclient
22
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <stdint.h>
26 #include <arpa/inet.h>
27 #include <vpp-api/client/vppapiclient.h>
28 #include <vpp-api/client/stat_client.h>
29
30 static int
31 govpp_stat_connect(char *socket_name)
32 {
33         return stat_segment_connect(socket_name);
34 }
35
36 static void
37 govpp_stat_disconnect()
38 {
39     stat_segment_disconnect();
40 }
41
42 static uint32_t*
43 govpp_stat_segment_ls(uint8_t ** pattern)
44 {
45         return stat_segment_ls(pattern);
46 }
47
48 static int
49 govpp_stat_segment_vec_len(void *vec)
50 {
51         return stat_segment_vec_len(vec);
52 }
53
54 static void
55 govpp_stat_segment_vec_free(void *vec)
56 {
57         stat_segment_vec_free(vec);
58 }
59
60 static char*
61 govpp_stat_segment_dir_index_to_name(uint32_t *dir, uint32_t index)
62 {
63         return stat_segment_index_to_name(dir[index]);
64 }
65
66 static stat_segment_data_t*
67 govpp_stat_segment_dump(uint32_t *counter_vec)
68 {
69         return stat_segment_dump(counter_vec);
70 }
71
72 static stat_segment_data_t
73 govpp_stat_segment_dump_index(stat_segment_data_t *data, int index)
74 {
75         return data[index];
76 }
77
78 static int
79 govpp_stat_segment_data_type(stat_segment_data_t *data)
80 {
81         return data->type;
82 }
83
84 static double
85 govpp_stat_segment_data_get_scalar_value(stat_segment_data_t *data)
86 {
87         return data->scalar_value;
88 }
89
90 static double
91 govpp_stat_segment_data_get_error_value(stat_segment_data_t *data)
92 {
93         return data->error_value;
94 }
95
96 static uint64_t**
97 govpp_stat_segment_data_get_simple_counter(stat_segment_data_t *data)
98 {
99         return data->simple_counter_vec;
100 }
101
102 static uint64_t*
103 govpp_stat_segment_data_get_simple_counter_index(stat_segment_data_t *data, int index)
104 {
105         return data->simple_counter_vec[index];
106 }
107
108 static uint64_t
109 govpp_stat_segment_data_get_simple_counter_index_value(stat_segment_data_t *data, int index, int index2)
110 {
111         return data->simple_counter_vec[index][index2];
112 }
113
114 static vlib_counter_t**
115 govpp_stat_segment_data_get_combined_counter(stat_segment_data_t *data)
116 {
117         return data->combined_counter_vec;
118 }
119
120 static vlib_counter_t*
121 govpp_stat_segment_data_get_combined_counter_index(stat_segment_data_t *data, int index)
122 {
123         return data->combined_counter_vec[index];
124 }
125
126 static uint64_t
127 govpp_stat_segment_data_get_combined_counter_index_packets(stat_segment_data_t *data, int index, int index2)
128 {
129         return data->combined_counter_vec[index][index2].packets;
130 }
131
132 static uint64_t
133 govpp_stat_segment_data_get_combined_counter_index_bytes(stat_segment_data_t *data, int index, int index2)
134 {
135         return data->combined_counter_vec[index][index2].bytes;
136 }
137
138 static void
139 govpp_stat_segment_data_free(stat_segment_data_t *data)
140 {
141         stat_segment_data_free(data);
142 }
143
144 static uint8_t**
145 govpp_stat_segment_string_vector(uint8_t ** string_vector, char *string)
146 {
147         return stat_segment_string_vector(string_vector, string);
148 }
149 */
150 import "C"
151 import (
152         "fmt"
153         "os"
154         "unsafe"
155
156         "git.fd.io/govpp.git/adapter"
157 )
158
159 var (
160         // DefaultStatSocket is the default path for the VPP stat socket file.
161         DefaultStatSocket = "/run/vpp/stats.sock"
162 )
163
164 // StatClient is the default implementation of StatsAPI.
165 type StatClient struct {
166         socketName string
167 }
168
169 // NewStatClient returns new VPP stats API client.
170 func NewStatClient(socketName string) *StatClient {
171         return &StatClient{
172                 socketName: socketName,
173         }
174 }
175
176 func (c *StatClient) Connect() error {
177         var sockName string
178
179         if c.socketName == "" {
180                 sockName = DefaultStatSocket
181         } else {
182                 sockName = c.socketName
183         }
184
185         rc := C.govpp_stat_connect(C.CString(sockName))
186         if rc != 0 {
187                 return fmt.Errorf("connecting to VPP stats API failed (rc=%v)", rc)
188         }
189
190         return nil
191 }
192
193 func (c *StatClient) Disconnect() error {
194         C.govpp_stat_disconnect()
195         return nil
196 }
197
198 func (c *StatClient) ListStats(patterns ...string) (stats []string, err error) {
199         dir := C.govpp_stat_segment_ls(convertStringSlice(patterns))
200         defer C.govpp_stat_segment_vec_free(unsafe.Pointer(dir))
201
202         l := C.govpp_stat_segment_vec_len(unsafe.Pointer(dir))
203         for i := 0; i < int(l); i++ {
204                 nameChar := C.govpp_stat_segment_dir_index_to_name(dir, C.uint32_t(i))
205                 stats = append(stats, C.GoString(nameChar))
206                 C.free(unsafe.Pointer(nameChar))
207         }
208
209         return stats, nil
210 }
211
212 func (c *StatClient) DumpStats(patterns ...string) (stats []*adapter.StatEntry, err error) {
213         dir := C.govpp_stat_segment_ls(convertStringSlice(patterns))
214         defer C.govpp_stat_segment_vec_free(unsafe.Pointer(dir))
215
216         dump := C.govpp_stat_segment_dump(dir)
217         defer C.govpp_stat_segment_data_free(dump)
218
219         l := C.govpp_stat_segment_vec_len(unsafe.Pointer(dump))
220         for i := 0; i < int(l); i++ {
221                 v := C.govpp_stat_segment_dump_index(dump, C.int(i))
222                 nameChar := v.name
223                 name := C.GoString(nameChar)
224                 typ := adapter.StatType(C.govpp_stat_segment_data_type(&v))
225
226                 stat := &adapter.StatEntry{
227                         Name: name,
228                         Type: typ,
229                 }
230
231                 switch typ {
232                 case adapter.ScalarIndex:
233                         stat.Data = adapter.ScalarStat(C.govpp_stat_segment_data_get_scalar_value(&v))
234
235                 case adapter.ErrorIndex:
236                         stat.Data = adapter.ErrorStat(C.govpp_stat_segment_data_get_error_value(&v))
237
238                 case adapter.SimpleCounterVector:
239                         length := int(C.govpp_stat_segment_vec_len(unsafe.Pointer(C.govpp_stat_segment_data_get_simple_counter(&v))))
240                         vector := make([][]adapter.Counter, length)
241                         for k := 0; k < length; k++ {
242                                 for j := 0; j < int(C.govpp_stat_segment_vec_len(unsafe.Pointer(C.govpp_stat_segment_data_get_simple_counter_index(&v, _Ctype_int(k))))); j++ {
243                                         vector[k] = append(vector[k], adapter.Counter(C.govpp_stat_segment_data_get_simple_counter_index_value(&v, _Ctype_int(k), _Ctype_int(j))))
244                                 }
245                         }
246                         stat.Data = adapter.SimpleCounterStat(vector)
247
248                 case adapter.CombinedCounterVector:
249                         length := int(C.govpp_stat_segment_vec_len(unsafe.Pointer(C.govpp_stat_segment_data_get_combined_counter(&v))))
250                         vector := make([][]adapter.CombinedCounter, length)
251                         for k := 0; k < length; k++ {
252                                 for j := 0; j < int(C.govpp_stat_segment_vec_len(unsafe.Pointer(C.govpp_stat_segment_data_get_combined_counter_index(&v, _Ctype_int(k))))); j++ {
253                                         vector[k] = append(vector[k], adapter.CombinedCounter{
254                                                 Packets: adapter.Counter(C.govpp_stat_segment_data_get_combined_counter_index_packets(&v, _Ctype_int(k), _Ctype_int(j))),
255                                                 Bytes:   adapter.Counter(C.govpp_stat_segment_data_get_combined_counter_index_bytes(&v, _Ctype_int(k), _Ctype_int(j))),
256                                         })
257                                 }
258                         }
259                         stat.Data = adapter.CombinedCounterStat(vector)
260
261                 default:
262                         fmt.Fprintf(os.Stderr, "invalid stat type: %v (%d)", typ, typ)
263                         continue
264
265                 }
266
267                 stats = append(stats, stat)
268         }
269
270         return stats, nil
271 }
272
273 func convertStringSlice(strs []string) **C.uint8_t {
274         var arr **C.uint8_t
275         for _, str := range strs {
276                 arr = C.govpp_stat_segment_string_vector(arr, C.CString(str))
277         }
278         return arr
279 }