Provide error counters per worker for statsclient
[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,!novpp,!beyond18.10
16
17 package vppapiclient
18
19 /*
20 #cgo CFLAGS: -DPNG_DEBUG=1
21 #cgo LDFLAGS: -lvppapiclient
22
23 #include "stat_client_wrapper.h"
24 */
25 import "C"
26
27 import (
28         "fmt"
29         "os"
30         "unsafe"
31
32         "git.fd.io/govpp.git/adapter"
33 )
34
35 // global VPP stats API client, library vppapiclient only supports
36 // single connection at a time
37 var globalStatClient *statClient
38
39 // statClient is the default implementation of StatsAPI.
40 type statClient struct {
41         socketName string
42 }
43
44 // NewStatClient returns new VPP stats API client.
45 func NewStatClient(socketName string) adapter.StatsAPI {
46         return &statClient{
47                 socketName: socketName,
48         }
49 }
50
51 func (c *statClient) Connect() error {
52         if globalStatClient != nil {
53                 return fmt.Errorf("already connected to stats API, disconnect first")
54         }
55
56         var sockName string
57         if c.socketName == "" {
58                 sockName = adapter.DefaultStatsSocket
59         } else {
60                 sockName = c.socketName
61         }
62
63         if _, err := os.Stat(sockName); err != nil {
64                 if os.IsNotExist(err) {
65                         return fmt.Errorf("stats socket file %q does not exists, ensure that VPP is running with `statseg { ... }` section in config", sockName)
66                 }
67                 return fmt.Errorf("stats socket file error: %v", err)
68         }
69
70         rc := C.govpp_stat_connect(C.CString(sockName))
71         if rc != 0 {
72                 return fmt.Errorf("connecting to VPP stats API failed (rc=%v)", rc)
73         }
74
75         globalStatClient = c
76         return nil
77 }
78
79 func (c *statClient) Disconnect() error {
80         globalStatClient = nil
81
82         C.govpp_stat_disconnect()
83         return nil
84 }
85
86 func (c *statClient) ListStats(patterns ...string) (stats []string, err error) {
87         dir := C.govpp_stat_segment_ls(convertStringSlice(patterns))
88         if dir == nil {
89                 return nil, adapter.ErrStatsDataBusy
90         }
91         defer C.govpp_stat_segment_vec_free(unsafe.Pointer(dir))
92
93         l := C.govpp_stat_segment_vec_len(unsafe.Pointer(dir))
94         for i := 0; i < int(l); i++ {
95                 nameChar := C.govpp_stat_segment_dir_index_to_name(dir, C.uint32_t(i))
96                 stats = append(stats, C.GoString(nameChar))
97                 C.free(unsafe.Pointer(nameChar))
98         }
99
100         return stats, nil
101 }
102
103 func (c *statClient) DumpStats(patterns ...string) (stats []adapter.StatEntry, err error) {
104         dir := C.govpp_stat_segment_ls(convertStringSlice(patterns))
105         if dir == nil {
106                 return nil, adapter.ErrStatsDataBusy
107         }
108         defer C.govpp_stat_segment_vec_free(unsafe.Pointer(dir))
109
110         dump := C.govpp_stat_segment_dump(dir)
111         if dump == nil {
112                 return nil, adapter.ErrStatsDataBusy
113         }
114         defer C.govpp_stat_segment_data_free(dump)
115
116         l := C.govpp_stat_segment_vec_len(unsafe.Pointer(dump))
117         for i := 0; i < int(l); i++ {
118                 v := C.govpp_stat_segment_dump_index(dump, C.int(i))
119                 nameChar := v.name
120                 name := C.GoString(nameChar)
121                 typ := adapter.StatType(C.govpp_stat_segment_data_type(&v))
122
123                 stat := adapter.StatEntry{
124                         Name: []byte(name),
125                         Type: typ,
126                 }
127
128                 switch typ {
129                 case adapter.ScalarIndex:
130                         stat.Data = adapter.ScalarStat(C.govpp_stat_segment_data_get_scalar_value(&v))
131
132                 case adapter.ErrorIndex:
133                         stat.Data = adapter.ErrorStat([]adapter.Counter{adapter.Counter(C.govpp_stat_segment_data_get_error_value(&v))})
134
135                 case adapter.SimpleCounterVector:
136                         length := int(C.govpp_stat_segment_vec_len(unsafe.Pointer(C.govpp_stat_segment_data_get_simple_counter(&v))))
137                         vector := make([][]adapter.Counter, length)
138                         for k := 0; k < length; k++ {
139                                 for j := 0; j < int(C.govpp_stat_segment_vec_len(unsafe.Pointer(C.govpp_stat_segment_data_get_simple_counter_index(&v, C.int(k))))); j++ {
140                                         vector[k] = append(vector[k], adapter.Counter(C.govpp_stat_segment_data_get_simple_counter_index_value(&v, C.int(k), C.int(j))))
141                                 }
142                         }
143                         stat.Data = adapter.SimpleCounterStat(vector)
144
145                 case adapter.CombinedCounterVector:
146                         length := int(C.govpp_stat_segment_vec_len(unsafe.Pointer(C.govpp_stat_segment_data_get_combined_counter(&v))))
147                         vector := make([][]adapter.CombinedCounter, length)
148                         for k := 0; k < length; k++ {
149                                 for j := 0; j < int(C.govpp_stat_segment_vec_len(unsafe.Pointer(C.govpp_stat_segment_data_get_combined_counter_index(&v, C.int(k))))); j++ {
150                                         vector[k] = append(vector[k], adapter.CombinedCounter([2]uint64{
151                                                 uint64(C.govpp_stat_segment_data_get_combined_counter_index_packets(&v, C.int(k), C.int(j))),
152                                                 uint64(C.govpp_stat_segment_data_get_combined_counter_index_bytes(&v, C.int(k), C.int(j))),
153                                         }))
154                                 }
155                         }
156                         stat.Data = adapter.CombinedCounterStat(vector)
157
158                 case adapter.NameVector:
159                         length := int(C.govpp_stat_segment_vec_len(unsafe.Pointer(C.govpp_stat_segment_data_get_name_vector(&v))))
160                         var vector []adapter.Name
161                         for k := 0; k < length; k++ {
162                                 s := C.govpp_stat_segment_data_get_name_vector_index(&v, C.int(k))
163                                 var name adapter.Name
164                                 if s != nil {
165                                         name = adapter.Name(C.GoString(s))
166                                 }
167                                 vector = append(vector, name)
168                         }
169                         stat.Data = adapter.NameStat(vector)
170
171                 default:
172                         fmt.Fprintf(os.Stderr, "invalid stat type: %v (%v)\n", typ, name)
173                         continue
174
175                 }
176
177                 stats = append(stats, stat)
178         }
179
180         return stats, nil
181 }
182
183 func (c *statClient) PrepareDir(prefixes ...string) (*adapter.StatDir, error) {
184         return nil, adapter.ErrNotImplemented
185 }
186
187 func (c *statClient) UpdateDir(dir *adapter.StatDir) error {
188         return adapter.ErrNotImplemented
189 }
190
191 func convertStringSlice(strs []string) **C.uint8_t {
192         var arr **C.uint8_t
193         for _, str := range strs {
194                 arr = C.govpp_stat_segment_string_vector(arr, C.CString(str))
195         }
196         return arr
197 }