FD.io VPP  v20.05-21-gb1500e9ff
Vector Packet Processing
stat_client.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 Cisco and/or its affiliates.
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 
16 #include "vom/stat_client.hpp"
17 
18 namespace VOM {
19 
21  : m_name(stat_seg_data.name)
22  , m_type(stat_seg_data.type)
23 {
24  switch (m_type) {
26  m_scalar_value = stat_seg_data.scalar_value;
27  break;
30  break;
33  break;
35  m_error_vec = stat_seg_data.error_vector;
36  break;
38  break;
40  break;
42  break;
43  }
44 }
45 
46 const std::string&
48 {
49  return m_name;
50 }
51 
54 {
55  return m_type;
56 }
57 
58 double
60 {
61  return m_scalar_value;
62 }
63 
64 uint64_t*
66 {
67  return m_error_vec;
68 }
69 
70 uint64_t**
72 {
73  return m_simple_counter_vec;
74 }
75 
78 {
80 }
81 
82 stat_client::stat_client(std::string& socket_name)
83  : m_socket_name(socket_name)
84  , m_patterns()
85  , m_stat_connect(false)
86  , m_counter_vec()
87  , m_stat_seg_data(nullptr)
88  , m_stat_data()
89 {
90  m_patterns.push_back("/if");
91 }
92 
93 stat_client::stat_client(std::vector<std::string>& pattern)
94  : m_socket_name("/run/vpp/stats.sock")
95  , m_patterns(pattern)
96  , m_stat_connect(false)
97  , m_counter_vec()
98  , m_stat_seg_data(nullptr)
99  , m_stat_data()
100 {}
101 
102 stat_client::stat_client(std::string socket_name,
103  std::vector<std::string> patterns)
104  : m_socket_name(socket_name)
105  , m_patterns(patterns)
106  , m_stat_connect(false)
107  , m_counter_vec()
108  , m_stat_seg_data(nullptr)
109  , m_stat_data()
110 {}
111 
113  : m_socket_name("/run/vpp/stats.sock")
114  , m_patterns()
115  , m_stat_connect(false)
116  , m_counter_vec()
117  , m_stat_seg_data(nullptr)
118  , m_stat_data()
119 {
120  m_patterns.push_back("/if");
121 }
122 
124 {
125  stat_segment_vec_free(m_counter_vec);
126  data_free();
127  if (m_stat_connect)
129 }
130 
132  : m_socket_name(o.m_socket_name)
133  , m_patterns(o.m_patterns)
134 {}
135 
136 int
138 {
139  if (stat_segment_connect(m_socket_name.c_str()) == 0) {
140  m_stat_connect = true;
141  ls();
142  }
143  return m_stat_connect;
144 }
145 
146 void
148 {
149  if (m_stat_connect)
151  m_stat_connect = false;
152 }
153 
154 int
156 {
157  return stat_segment_vec_len(vec);
158 }
159 
160 void
161 stat_client::vec_free(void* vec)
162 {
164 }
165 
166 void
167 stat_client::ls()
168 {
169  uint8_t** string_vec = { 0 };
170  for (auto& pattern : m_patterns) {
171  string_vec = stat_segment_string_vector(string_vec, pattern.c_str());
172  }
173  m_counter_vec = stat_segment_ls(string_vec);
174  stat_segment_vec_free(string_vec);
175 }
176 
179 {
180  stat_segment_data_free(m_stat_seg_data);
181  m_stat_seg_data = NULL;
182  if (m_stat_data.size()) {
183  m_stat_data.clear();
184  }
185  if (m_stat_connect) {
186  m_stat_seg_data = stat_segment_dump(m_counter_vec);
187  if (!m_stat_seg_data) {
188  ls();
189  return m_stat_data;
190  }
191  for (int i = 0; i < stat_segment_vec_len(m_stat_seg_data); i++) {
192  stat_data_t sd(m_stat_seg_data[i]);
193  m_stat_data.push_back(sd);
194  }
195  }
196  return m_stat_data;
197 }
198 
200 stat_client::dump_entry(uint32_t index)
201 {
202  stat_segment_data_free(m_stat_seg_data);
203  m_stat_seg_data = NULL;
204  if (m_stat_data.size()) {
205  m_stat_data.clear();
206  }
207  if (m_stat_connect) {
208  m_stat_seg_data = stat_segment_dump_entry(index);
209  if (!m_stat_seg_data) {
210  ls();
211  return m_stat_data;
212  }
213  for (int i = 0; i < stat_segment_vec_len(m_stat_seg_data); i++) {
214  stat_data_t sd(m_stat_seg_data[i]);
215  m_stat_data.push_back(sd);
216  }
217  }
218  return m_stat_data;
219 }
220 
221 void
222 stat_client::data_free()
223 {
224  stat_segment_data_free(m_stat_seg_data);
225 }
226 
227 double
229 {
230  return stat_segment_heartbeat();
231 }
232 
233 std::string
235 {
236  return stat_segment_index_to_name(index);
237 }
238 
239 } // namespace VOM
240 
241 /*
242  * fd.io coding-style-patch-verification: ON
243  *
244  * Local Variables:
245  * eval: (c-set-style "mozilla")
246  * End:
247  */
int stat_segment_connect(const char *socket_name)
Definition: stat_client.c:151
stat_segment_data_t * stat_segment_dump_entry(uint32_t index)
Definition: stat_client.c:451
const stat_directory_type_t & type() const
get type of stat
Definition: stat_client.cpp:53
void stat_segment_data_free(stat_segment_data_t *res)
Definition: stat_client.c:287
counter_t ** simple_counter_vec
Definition: stat_client.h:42
vlib_counter_t ** get_stat_segment_combined_counter_data() const
Definition: stat_client.cpp:77
Combined counter to hold both packets and byte differences.
Definition: counter_types.h:26
stat_client()
Stat Client constructor.
vlib_counter_t ** m_combined_counter_vec
Definition: stat_client.hpp:82
vl_api_fib_path_type_t type
Definition: fib_types.api:123
int vec_len(void *vec)
Get vector length of VPP style vector.
stat_directory_type_t
double get_stat_segment_scalar_data() const
Get pointer to actual data.
Definition: stat_client.cpp:59
stat data representation
Definition: stat_client.hpp:38
void stat_segment_disconnect(void)
Definition: stat_client.c:165
void disconnect()
Disconnect to stat segment.
uint8_t ** stat_segment_string_vector(uint8_t **string_vector, const char *string)
Definition: stat_client.c:422
A representation of a stat client in VPP.
Definition: stat_client.hpp:32
uint64_t * get_stat_segment_error_data() const
Definition: stat_client.cpp:65
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:317
const stat_data_vec_t & dump_entry(uint32_t index)
dump stats for given index in stat directory
std::vector< stat_data_t > stat_data_vec_t
vector of stat_data_t
Definition: stat_client.hpp:89
~stat_client()
Stat Client destructor.
char * stat_segment_index_to_name(uint32_t index)
Definition: stat_client.c:476
uint64_t ** get_stat_segment_simple_counter_data() const
Definition: stat_client.cpp:71
std::string index_to_name(uint32_t index)
get index to name of stat
string name[64]
Definition: ip.api:44
uint32_t * stat_segment_ls(uint8_t **patterns)
Definition: stat_client.c:366
int connect()
Connect to stat segment.
stat_segment_data_t * stat_segment_dump(uint32_t *stats)
Definition: stat_client.c:401
const stat_data_vec_t & dump()
dump all the stats for given pattern
vlib_counter_t ** combined_counter_vec
Definition: stat_client.h:43
The VPP Object Model (VOM) library.
Definition: acl_binding.cpp:19
void stat_segment_vec_free(void *vec)
Definition: stat_client.c:415
int stat_segment_vec_len(void *vec)
Definition: stat_client.c:409
const std::string & name() const
get name of stat
Definition: stat_client.cpp:47
stat_data_t(const stat_segment_data_t &stat_seg_data)
stat data custom constructor
Definition: stat_client.cpp:20
counter_t * error_vector
Definition: stat_client.h:41
double stat_segment_heartbeat(void)
Definition: stat_client.c:188