FD.io VPP  v21.10.1-2-g0a485f517
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 {
79  return m_combined_counter_vec;
80 }
81 
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 
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 
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 
235 {
237 }
238 
239 } // namespace VOM
240 
241 /*
242  * fd.io coding-style-patch-verification: OFF
243  *
244  * Local Variables:
245  * eval: (c-set-style "mozilla")
246  * End:
247  */
STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED
@ STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED
Definition: stat_segment_shared.h:24
stat_segment_vec_free
void stat_segment_vec_free(void *vec)
Definition: stat_client.c:456
VOM::stat_client::connect
int connect()
Connect to stat segment.
Definition: stat_client.cpp:137
stat_segment_data_t::simple_counter_vec
counter_t ** simple_counter_vec
Definition: stat_client.h:43
VOM::stat_client::stat_data_t::m_error_vec
counter_t * m_error_vec
Definition: stat_client.hpp:80
VOM
The VPP Object Model (VOM) library.
Definition: acl_binding.cpp:19
VOM::stat_client::stat_data_t::name
const std::string & name() const
get name of stat
Definition: stat_client.cpp:47
stat_segment_string_vector
uint8_t ** stat_segment_string_vector(uint8_t **string_vector, const char *string)
Definition: stat_client.c:463
stat_segment_data_t::error_vector
counter_t * error_vector
Definition: stat_client.h:42
VOM::stat_client::stat_data_t
stat data representation
Definition: stat_client.hpp:38
name
string name[64]
Definition: fib.api:25
VOM::stat_client::stat_data_t::type
const stat_directory_type_t & type() const
get type of stat
Definition: stat_client.cpp:53
VOM::stat_client::stat_data_t::m_simple_counter_vec
counter_t ** m_simple_counter_vec
Definition: stat_client.hpp:81
STAT_DIR_TYPE_NAME_VECTOR
@ STAT_DIR_TYPE_NAME_VECTOR
Definition: stat_segment_shared.h:26
VOM::stat_client::stat_data_t::get_stat_segment_error_data
uint64_t * get_stat_segment_error_data() const
Definition: stat_client.cpp:65
VOM::stat_client::stat_data_t::get_stat_segment_combined_counter_data
vlib_counter_t ** get_stat_segment_combined_counter_data() const
Definition: stat_client.cpp:77
STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE
@ STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE
Definition: stat_segment_shared.h:23
stat_directory_type_t
stat_directory_type_t
Definition: stat_segment_shared.h:19
stat_segment_index_to_name
char * stat_segment_index_to_name(uint32_t index)
Definition: stat_client.c:523
stat_segment_data_t::scalar_value
double scalar_value
Definition: stat_client.h:41
STAT_DIR_TYPE_EMPTY
@ STAT_DIR_TYPE_EMPTY
Definition: stat_segment_shared.h:27
stat_segment_ls
uint32_t * stat_segment_ls(uint8_t **patterns)
Definition: stat_client.c:405
stat_segment_disconnect
void stat_segment_disconnect(void)
Definition: stat_client.c:165
VOM::stat_client::disconnect
void disconnect()
Disconnect to stat segment.
Definition: stat_client.cpp:147
stat_segment_data_free
void stat_segment_data_free(stat_segment_data_t *res)
Definition: stat_client.c:315
stat_segment_connect
int stat_segment_connect(const char *socket_name)
Definition: stat_client.c:151
stat_segment_vec_len
int stat_segment_vec_len(void *vec)
Definition: stat_client.c:450
vlib_counter_t
Combined counter to hold both packets and byte differences.
Definition: counter_types.h:26
VOM::stat_client::vec_len
int vec_len(void *vec)
Get vector length of VPP style vector.
Definition: stat_client.cpp:155
VOM::stat_client::stat_data_t::m_combined_counter_vec
vlib_counter_t ** m_combined_counter_vec
Definition: stat_client.hpp:82
VOM::stat_client::stat_data_t::get_stat_segment_scalar_data
double get_stat_segment_scalar_data() const
Get pointer to actual data.
Definition: stat_client.cpp:59
VOM::stat_client::stat_data_t::stat_data_t
stat_data_t(const stat_segment_data_t &stat_seg_data)
stat data custom constructor
Definition: stat_client.cpp:20
false
#define false
Definition: cJSON.c:70
index
u32 index
Definition: flow_types.api:221
VOM::stat_client::stat_data_vec_t
std::vector< stat_data_t > stat_data_vec_t
vector of stat_data_t
Definition: stat_client.hpp:89
stat_segment_data_t
Definition: stat_client.h:35
stat_segment_data_t::combined_counter_vec
vlib_counter_t ** combined_counter_vec
Definition: stat_client.h:44
VOM::stat_client::stat_client
stat_client()
Stat Client constructor.
Definition: stat_client.cpp:112
VOM::stat_client::stat_data_t::get_stat_segment_simple_counter_data
uint64_t ** get_stat_segment_simple_counter_data() const
Definition: stat_client.cpp:71
STAT_DIR_TYPE_SCALAR_INDEX
@ STAT_DIR_TYPE_SCALAR_INDEX
Definition: stat_segment_shared.h:22
VOM::stat_client
A representation of a stat client in VPP.
Definition: stat_client.hpp:32
VOM::stat_client::dump
const stat_data_vec_t & dump()
dump all the stats for given pattern
Definition: stat_client.cpp:178
VOM::stat_client::stat_data_t::m_scalar_value
double m_scalar_value
Definition: stat_client.hpp:79
VOM::stat_client::index_to_name
std::string index_to_name(uint32_t index)
get index to name of stat
Definition: stat_client.cpp:234
stat_segment_heartbeat
double stat_segment_heartbeat(void)
Definition: stat_client.c:189
STAT_DIR_TYPE_ILLEGAL
@ STAT_DIR_TYPE_ILLEGAL
Definition: stat_segment_shared.h:21
VOM::stat_client::dump_entry
const stat_data_vec_t & dump_entry(uint32_t index)
dump stats for given index in stat directory
Definition: stat_client.cpp:200
i
int i
Definition: flowhash_template.h:376
VOM::stat_client::~stat_client
~stat_client()
Stat Client destructor.
Definition: stat_client.cpp:123
VOM::stat_client::heartbeat
double heartbeat()
Definition: stat_client.cpp:228
stat_segment_dump
stat_segment_data_t * stat_segment_dump(uint32_t *stats)
Definition: stat_client.c:442
stat_segment_dump_entry
stat_segment_data_t * stat_segment_dump_entry(uint32_t index)
Definition: stat_client.c:497
stat_client.hpp
STAT_DIR_TYPE_ERROR_INDEX
@ STAT_DIR_TYPE_ERROR_INDEX
Definition: stat_segment_shared.h:25
type
vl_api_fib_path_type_t type
Definition: fib_types.api:123
string
const char *const string
Definition: cJSON.h:172