FD.io VPP  v19.01.3-6-g70449b9b9
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_value = stat_seg_data.error_value;
36  break;
38  break;
39  }
40 }
41 
42 const std::string&
44 {
45  return m_name;
46 }
47 
50 {
51  return m_type;
52 }
53 
54 double
56 {
57  return m_scalar_value;
58 }
59 
60 uint64_t
62 {
63  return m_error_value;
64 }
65 
66 uint64_t**
68 {
69  return m_simple_counter_vec;
70 }
71 
74 {
76 }
77 
78 stat_client::stat_client(std::string& socket_name)
79  : m_socket_name(socket_name)
80  , m_patterns()
81  , m_stat_connect(false)
82  , m_counter_vec()
83  , m_stat_seg_data(nullptr)
84  , m_stat_data()
85 {
86  m_patterns.push_back("/if");
87 }
88 
89 stat_client::stat_client(std::vector<std::string>& pattern)
90  : m_socket_name("/run/vpp/stats.sock")
91  , m_patterns(pattern)
92  , m_stat_connect(false)
93  , m_counter_vec()
94  , m_stat_seg_data(nullptr)
95  , m_stat_data()
96 {
97 }
98 
99 stat_client::stat_client(std::string socket_name,
100  std::vector<std::string> patterns)
101  : m_socket_name(socket_name)
102  , m_patterns(patterns)
103  , m_stat_connect(false)
104  , m_counter_vec()
105  , m_stat_seg_data(nullptr)
106  , m_stat_data()
107 {
108 }
109 
111  : m_socket_name("/run/vpp/stats.sock")
112  , m_patterns()
113  , m_stat_connect(false)
114  , m_counter_vec()
115  , m_stat_seg_data(nullptr)
116  , m_stat_data()
117 {
118  m_patterns.push_back("/if");
119 }
120 
122 {
123  stat_segment_vec_free(m_counter_vec);
124  data_free();
125  if (m_stat_connect)
127 }
128 
130  : m_socket_name(o.m_socket_name)
131  , m_patterns(o.m_patterns)
132 {
133 }
134 
135 int
137 {
138  if (stat_segment_connect(m_socket_name.c_str()) == 0) {
139  m_stat_connect = true;
140  ls();
141  }
142  return m_stat_connect;
143 }
144 
145 void
147 {
148  if (m_stat_connect)
150  m_stat_connect = false;
151 }
152 
153 int
155 {
156  return stat_segment_vec_len(vec);
157 }
158 
159 void
160 stat_client::vec_free(void* vec)
161 {
163 }
164 
165 void
166 stat_client::ls()
167 {
168  uint8_t** string_vec = { 0 };
169  for (auto& pattern : m_patterns) {
170  string_vec = stat_segment_string_vector(string_vec, pattern.c_str());
171  }
172  m_counter_vec = stat_segment_ls(string_vec);
173  stat_segment_vec_free(string_vec);
174 }
175 
178 {
179  stat_segment_data_free(m_stat_seg_data);
180  m_stat_seg_data = NULL;
181  if (m_stat_data.size()) {
182  m_stat_data.clear();
183  }
184  if (m_stat_connect) {
185  m_stat_seg_data = stat_segment_dump(m_counter_vec);
186  if (!m_stat_seg_data) {
187  ls();
188  return m_stat_data;
189  }
190  for (int i = 0; i < stat_segment_vec_len(m_stat_seg_data); i++) {
191  stat_data_t sd(m_stat_seg_data[i]);
192  m_stat_data.push_back(sd);
193  }
194  }
195  return m_stat_data;
196 }
197 
199 stat_client::dump_entry(uint32_t index)
200 {
201  stat_segment_data_free(m_stat_seg_data);
202  m_stat_seg_data = NULL;
203  if (m_stat_data.size()) {
204  m_stat_data.clear();
205  }
206  if (m_stat_connect) {
207  m_stat_seg_data = stat_segment_dump_entry(index);
208  if (!m_stat_seg_data) {
209  ls();
210  return m_stat_data;
211  }
212  for (int i = 0; i < stat_segment_vec_len(m_stat_seg_data); i++) {
213  stat_data_t sd(m_stat_seg_data[i]);
214  m_stat_data.push_back(sd);
215  }
216  }
217  return m_stat_data;
218 }
219 
220 void
221 stat_client::data_free()
222 {
223  stat_segment_data_free(m_stat_seg_data);
224 }
225 
226 double
228 {
229  return stat_segment_heartbeat();
230 }
231 
232 std::string
234 {
235  return stat_segment_index_to_name(index);
236 }
237 
238 } // namespace VOM
239 
240 /*
241  * fd.io coding-style-patch-verification: ON
242  *
243  * Local Variables:
244  * eval: (c-set-style "mozilla")
245  * End:
246  */
int stat_segment_connect(const char *socket_name)
Definition: stat_client.c:163
stat_segment_data_t * stat_segment_dump_entry(uint32_t index)
Definition: stat_client.c:452
const stat_directory_type_t & type() const
get type of stat
Definition: stat_client.cpp:49
void stat_segment_data_free(stat_segment_data_t *res)
Definition: stat_client.c:262
counter_t ** simple_counter_vec
Definition: stat_client.h:46
#define NULL
Definition: clib.h:58
vlib_counter_t ** get_stat_segment_combined_counter_data() const
Definition: stat_client.cpp:73
Combined counter to hold both packets and byte differences.
Definition: counter_types.h:26
int i
stat_client()
Stat Client constructor.
vlib_counter_t ** m_combined_counter_vec
Definition: stat_client.hpp:82
int vec_len(void *vec)
Get vector length of VPP style vector.
double get_stat_segment_scalar_data() const
Get pointer to actual data.
Definition: stat_client.cpp:55
u8 name[64]
Definition: memclnt.api:152
stat data representation
Definition: stat_client.hpp:38
void stat_segment_disconnect(void)
Definition: stat_client.c:177
void disconnect()
Disconnect to stat segment.
uint8_t ** stat_segment_string_vector(uint8_t **string_vector, const char *string)
Definition: stat_client.c:423
A representation of a stat client in VPP.
Definition: stat_client.hpp:32
stat_directory_type_t
Definition: stat_client.h:24
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:459
uint64_t get_stat_segment_error_data() const
Definition: stat_client.cpp:61
uint64_t ** get_stat_segment_simple_counter_data() const
Definition: stat_client.cpp:67
std::string index_to_name(uint32_t index)
get index to name of stat
uint32_t * stat_segment_ls(uint8_t **patterns)
Definition: stat_client.c:367
int connect()
Connect to stat segment.
stat_segment_data_t * stat_segment_dump(uint32_t *stats)
Definition: stat_client.c:402
const stat_data_vec_t & dump()
dump all the stats for given pattern
vlib_counter_t ** combined_counter_vec
Definition: stat_client.h:47
The VPP Object Model (VOM) library.
Definition: acl_binding.cpp:19
void stat_segment_vec_free(void *vec)
Definition: stat_client.c:416
uint64_t error_value
Definition: stat_client.h:45
int stat_segment_vec_len(void *vec)
Definition: stat_client.c:410
const std::string & name() const
get name of stat
Definition: stat_client.cpp:43
stat_data_t(const stat_segment_data_t &stat_seg_data)
stat data custom constructor
Definition: stat_client.cpp:20
double stat_segment_heartbeat(void)
Definition: stat_client.c:192