FD.io VPP  v19.04.2-12-g66b1689
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;
40  break;
41  }
42 }
43 
44 const std::string&
46 {
47  return m_name;
48 }
49 
52 {
53  return m_type;
54 }
55 
56 double
58 {
59  return m_scalar_value;
60 }
61 
62 uint64_t
64 {
65  return m_error_value;
66 }
67 
68 uint64_t**
70 {
71  return m_simple_counter_vec;
72 }
73 
76 {
78 }
79 
80 stat_client::stat_client(std::string& socket_name)
81  : m_socket_name(socket_name)
82  , m_patterns()
83  , m_stat_connect(false)
84  , m_counter_vec()
85  , m_stat_seg_data(nullptr)
86  , m_stat_data()
87 {
88  m_patterns.push_back("/if");
89 }
90 
91 stat_client::stat_client(std::vector<std::string>& pattern)
92  : m_socket_name("/run/vpp/stats.sock")
93  , m_patterns(pattern)
94  , m_stat_connect(false)
95  , m_counter_vec()
96  , m_stat_seg_data(nullptr)
97  , m_stat_data()
98 {
99 }
100 
101 stat_client::stat_client(std::string socket_name,
102  std::vector<std::string> patterns)
103  : m_socket_name(socket_name)
104  , m_patterns(patterns)
105  , m_stat_connect(false)
106  , m_counter_vec()
107  , m_stat_seg_data(nullptr)
108  , m_stat_data()
109 {
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 
137 int
139 {
140  if (stat_segment_connect(m_socket_name.c_str()) == 0) {
141  m_stat_connect = true;
142  ls();
143  }
144  return m_stat_connect;
145 }
146 
147 void
149 {
150  if (m_stat_connect)
152  m_stat_connect = false;
153 }
154 
155 int
157 {
158  return stat_segment_vec_len(vec);
159 }
160 
161 void
162 stat_client::vec_free(void* vec)
163 {
165 }
166 
167 void
168 stat_client::ls()
169 {
170  uint8_t** string_vec = { 0 };
171  for (auto& pattern : m_patterns) {
172  string_vec = stat_segment_string_vector(string_vec, pattern.c_str());
173  }
174  m_counter_vec = stat_segment_ls(string_vec);
175  stat_segment_vec_free(string_vec);
176 }
177 
180 {
181  stat_segment_data_free(m_stat_seg_data);
182  m_stat_seg_data = NULL;
183  if (m_stat_data.size()) {
184  m_stat_data.clear();
185  }
186  if (m_stat_connect) {
187  m_stat_seg_data = stat_segment_dump(m_counter_vec);
188  if (!m_stat_seg_data) {
189  ls();
190  return m_stat_data;
191  }
192  for (int i = 0; i < stat_segment_vec_len(m_stat_seg_data); i++) {
193  stat_data_t sd(m_stat_seg_data[i]);
194  m_stat_data.push_back(sd);
195  }
196  }
197  return m_stat_data;
198 }
199 
201 stat_client::dump_entry(uint32_t index)
202 {
203  stat_segment_data_free(m_stat_seg_data);
204  m_stat_seg_data = NULL;
205  if (m_stat_data.size()) {
206  m_stat_data.clear();
207  }
208  if (m_stat_connect) {
209  m_stat_seg_data = stat_segment_dump_entry(index);
210  if (!m_stat_seg_data) {
211  ls();
212  return m_stat_data;
213  }
214  for (int i = 0; i < stat_segment_vec_len(m_stat_seg_data); i++) {
215  stat_data_t sd(m_stat_seg_data[i]);
216  m_stat_data.push_back(sd);
217  }
218  }
219  return m_stat_data;
220 }
221 
222 void
223 stat_client::data_free()
224 {
225  stat_segment_data_free(m_stat_seg_data);
226 }
227 
228 double
230 {
231  return stat_segment_heartbeat();
232 }
233 
234 std::string
236 {
237  return stat_segment_index_to_name(index);
238 }
239 
240 } // namespace VOM
241 
242 /*
243  * fd.io coding-style-patch-verification: ON
244  *
245  * Local Variables:
246  * eval: (c-set-style "mozilla")
247  * End:
248  */
int stat_segment_connect(const char *socket_name)
Definition: stat_client.c:166
stat_segment_data_t * stat_segment_dump_entry(uint32_t index)
Definition: stat_client.c:478
void stat_segment_data_free(stat_segment_data_t *res)
Definition: stat_client.c:286
counter_t ** simple_counter_vec
Definition: stat_client.h:50
#define NULL
Definition: clib.h:58
Combined counter to hold both packets and byte differences.
Definition: counter_types.h:26
int i
const std::string & name() const
get name of stat
Definition: stat_client.cpp:45
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:57
u8 name[64]
Definition: memclnt.api:152
stat data representation
Definition: stat_client.hpp:38
void stat_segment_disconnect(void)
Definition: stat_client.c:180
vlib_counter_t ** get_stat_segment_combined_counter_data() const
Definition: stat_client.cpp:75
void disconnect()
Disconnect to stat segment.
uint8_t ** stat_segment_string_vector(uint8_t **string_vector, const char *string)
Definition: stat_client.c:449
A representation of a stat client in VPP.
Definition: stat_client.hpp:32
stat_directory_type_t
Definition: stat_client.h:27
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:485
std::string index_to_name(uint32_t index)
get index to name of stat
uint64_t get_stat_segment_error_data() const
Definition: stat_client.cpp:63
uint32_t * stat_segment_ls(uint8_t **patterns)
Definition: stat_client.c:393
int connect()
Connect to stat segment.
stat_segment_data_t * stat_segment_dump(uint32_t *stats)
Definition: stat_client.c:428
const stat_data_vec_t & dump()
dump all the stats for given pattern
vlib_counter_t ** combined_counter_vec
Definition: stat_client.h:51
The VPP Object Model (VOM) library.
Definition: acl_binding.cpp:19
void stat_segment_vec_free(void *vec)
Definition: stat_client.c:442
uint64_t error_value
Definition: stat_client.h:49
uint64_t ** get_stat_segment_simple_counter_data() const
Definition: stat_client.cpp:69
const stat_directory_type_t & type() const
get type of stat
Definition: stat_client.cpp:51
int stat_segment_vec_len(void *vec)
Definition: stat_client.c:436
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:195