FD.io VPP  v21.06-3-gbb25fbf28
Vector Packet Processing
gmon.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012 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 <stdio.h>
17 #include <stdlib.h>
18 #include <sys/types.h>
19 #include <sys/mman.h>
20 #include <sys/stat.h>
21 #include <netinet/in.h>
22 #include <signal.h>
23 #include <pthread.h>
24 #include <unistd.h>
25 #include <time.h>
26 #include <fcntl.h>
27 #include <string.h>
28 #include <vppinfra/clib.h>
29 #include <vppinfra/vec.h>
30 #include <vppinfra/hash.h>
31 #include <vppinfra/bitmap.h>
32 #include <vppinfra/fifo.h>
33 #include <vppinfra/time.h>
34 #include <vppinfra/heap.h>
35 #include <vppinfra/pool.h>
36 #include <vppinfra/format.h>
37 #include <vlibapi/api.h>
38 #include <vlibmemory/api.h>
39 
40 #include <vlib/vlib.h>
41 #include <vlib/unix/unix.h>
42 #include <vnet/api_errno.h>
43 
44 #include <svm/svmdb.h>
45 
46 typedef struct
47 {
52  pid_t *vpef_pid_ptr;
58 
59 } gmon_main_t;
60 
61 #include <vlib/vlib.h>
62 #include <vnet/vnet.h>
63 #include <vnet/devices/devices.h>
64 
66 
67 static u64
69 {
70  vlib_main_t *this_vlib_main;
72  uword code;
73  int vm_index;
74  u64 significant_errors = 0;
75 
76  /* *INDENT-OFF* */
77  clib_bitmap_foreach (code, gm->sig_error_bitmap)
78  {
79  for (vm_index = 0; vm_index < vec_len (gm->my_vlib_mains); vm_index++)
80  {
81  this_vlib_main = gm->my_vlib_mains[vm_index];
82  em = &this_vlib_main->error_main;
83  significant_errors += em->counters[code] -
84  ((vec_len(em->counters_last_clear) > code) ?
85  em->counters_last_clear[code] : 0);
86  }
87  }
88  /* *INDENT-ON* */
89 
90  return (significant_errors);
91 }
92 
93 static clib_error_t *
95 {
97 
98  *gm->vpef_pid_ptr = getpid ();
99 
100  return 0;
101 }
102 
104 
105 
106 static uword
108 {
109  f64 vector_rate;
110  u64 input_packets, last_input_packets, new_sig_errors;
111  f64 last_runtime, dt, now;
113  int i;
114 
115  last_runtime = 0.0;
116  last_input_packets = 0;
117 
118  last_runtime = 0.0;
119  last_input_packets = 0;
120 
121  /* Initial wait for the world to settle down */
122  vlib_process_suspend (vm, 5.0);
123 
124  for (i = 0; i < vlib_get_n_threads (); i++)
125  vec_add1 (gm->my_vlib_mains, vlib_mains[i]);
126 
127  while (1)
128  {
129  vlib_process_suspend (vm, 5.0);
130  vector_rate = vlib_last_vector_length_per_node (vm);
131  *gm->vector_rate_ptr = vector_rate;
132  now = vlib_time_now (vm);
133  dt = now - last_runtime;
134  input_packets = vnet_get_aggregate_rx_packets ();
135  *gm->input_rate_ptr = (f64) (input_packets - last_input_packets) / dt;
136  last_runtime = now;
137  last_input_packets = input_packets;
138 
139  new_sig_errors = get_significant_errors (gm);
140  *gm->sig_error_rate_ptr =
141  ((f64) (new_sig_errors - gm->last_sig_errors)) / dt;
142  gm->last_sig_errors = new_sig_errors;
143  }
144 
145  return 0; /* not so much */
146 }
147 
148 /* *INDENT-OFF* */
150  .function = gmon_process,
151  .type = VLIB_NODE_TYPE_PROCESS,
152  .name = "gmon-process",
153 };
154 /* *INDENT-ON* */
155 
156 static clib_error_t *
158 {
161  pid_t *swp = 0;
162  f64 *v = 0;
164  svmdb_map_args_t _ma, *ma = &_ma;
165 
167  return (error);
168 
170  return (error);
171 
172  gm->vlib_main = vm;
173 
174  clib_memset (ma, 0, sizeof (*ma));
175  ma->root_path = am->root_path;
176  ma->uid = am->api_uid;
177  ma->gid = am->api_gid;
178 
179  gm->svmdb_client = svmdb_map (ma);
180 
181  /* Find or create, set to zero */
182  vec_add1 (v, 0.0);
183  svmdb_local_set_vec_variable (gm->svmdb_client,
184  "vpp_vector_rate", (char *) v, sizeof (*v));
185  vec_free (v);
186  vec_add1 (v, 0.0);
187  svmdb_local_set_vec_variable (gm->svmdb_client,
188  "vpp_input_rate", (char *) v, sizeof (*v));
189  vec_free (v);
190  vec_add1 (v, 0.0);
191  svmdb_local_set_vec_variable (gm->svmdb_client,
192  "vpp_sig_error_rate",
193  (char *) v, sizeof (*v));
194  vec_free (v);
195 
196  vec_add1 (swp, 0.0);
197  svmdb_local_set_vec_variable (gm->svmdb_client,
198  "vpp_pid", (char *) swp, sizeof (*swp));
199  vec_free (swp);
200 
201  /* the value cells will never move, so acquire references to them */
202  gm->vector_rate_ptr =
203  svmdb_local_get_variable_reference (gm->svmdb_client,
205  "vpp_vector_rate");
206  gm->input_rate_ptr =
207  svmdb_local_get_variable_reference (gm->svmdb_client,
209  "vpp_input_rate");
210  gm->sig_error_rate_ptr =
211  svmdb_local_get_variable_reference (gm->svmdb_client,
213  "vpp_sig_error_rate");
214  gm->vpef_pid_ptr =
215  svmdb_local_get_variable_reference (gm->svmdb_client,
216  SVMDB_NAMESPACE_VEC, "vpp_pid");
217  return 0;
218 }
219 
221 
222 static clib_error_t *
224 {
226 
227  if (gm->vector_rate_ptr)
228  {
229  *gm->vector_rate_ptr = 0.0;
230  *gm->vpef_pid_ptr = 0;
231  *gm->input_rate_ptr = 0.0;
232  *gm->sig_error_rate_ptr = 0.0;
233  svm_region_unmap ((void *) gm->svmdb_client->db_rp);
234  vec_free (gm->svmdb_client);
235  }
236  return 0;
237 }
238 
240 
241 static int
243 {
244  vlib_main_t *vm = gm->vlib_main;
246 
247  if (index >= vec_len (em->counters))
248  return VNET_API_ERROR_NO_SUCH_ENTRY;
249 
250  gm->sig_error_bitmap =
251  clib_bitmap_set (gm->sig_error_bitmap, index, enable);
252  return 0;
253 }
254 
255 static clib_error_t *
257  unformat_input_t * input,
258  vlib_cli_command_t * cmd)
259 {
260  u32 index;
261  int enable = 1;
262  int rv;
264 
266  {
267  if (unformat (input, "%d", &index))
268  ;
269  else if (unformat (input, "disable"))
270  enable = 0;
271  else
272  return clib_error_return (0, "unknown input `%U'",
273  format_unformat_error, input);
274  }
275 
277 
278  switch (rv)
279  {
280  case 0:
281  break;
282 
283  default:
284  return clib_error_return
285  (0, "significant_error_enable_disable returned %d", rv);
286  }
287 
288  return 0;
289 }
290 
291 /* *INDENT-OFF* */
293  .path = "set significant error",
294  .short_help = "set significant error <counter-index-nnn> [disable]",
296 };
297 /* *INDENT-ON* */
298 
299 /*
300  * fd.io coding-style-patch-verification: ON
301  *
302  * Local Variables:
303  * eval: (c-set-style "gnu")
304  * End:
305  */
vlib.h
vlib_error_main_t
Definition: error.h:61
svmdb_local_set_vec_variable
void svmdb_local_set_vec_variable(svmdb_client_t *client, char *var, void *val_arg, u32 elsize)
Definition: svmdb.c:560
api.h
gmon_main_t
Definition: gmon.c:46
svmdb_map_args_t::root_path
const char * root_path
Definition: svmdb.h:86
svmdb_client_t
Definition: svmdb.h:65
f
vlib_frame_t * f
Definition: interface_output.c:1080
bitmap.h
clib.h
set_significant_error_command_fn
static clib_error_t * set_significant_error_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: gmon.c:256
string.h
publish_pid
static clib_error_t * publish_pid(vlib_main_t *vm)
Definition: gmon.c:94
svm_region_unmap
void svm_region_unmap(void *rp_arg)
Definition: svm.c:1145
clib_error_return
#define clib_error_return(e, args...)
Definition: error.h:99
vlib_cli_command_t::path
char * path
Definition: cli.h:96
get_significant_errors
static u64 get_significant_errors(gmon_main_t *gm)
Definition: gmon.c:68
vlib_main_t::error_main
vlib_error_main_t error_main
Definition: main.h:177
am
app_main_t * am
Definition: application.c:489
vlib_call_init_function
#define vlib_call_init_function(vm, x)
Definition: init.h:259
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
api.h
fifo.h
gmon_main_t::vpef_pid_ptr
pid_t * vpef_pid_ptr
Definition: gmon.c:52
unformat_input_t
struct _unformat_input_t unformat_input_t
vlib_frame_t
Definition: node.h:372
error
Definition: cJSON.c:88
gmon_init
static clib_error_t * gmon_init(vlib_main_t *vm)
Definition: gmon.c:157
unformat
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
gmon_main_t::sig_error_bitmap
uword * sig_error_bitmap
Definition: gmon.c:55
svmdb_local_get_variable_reference
void * svmdb_local_get_variable_reference(svmdb_client_t *client, svmdb_namespace_t namespace, char *var)
Definition: svmdb.c:377
gmon_exit
static clib_error_t * gmon_exit(vlib_main_t *vm)
Definition: gmon.c:223
vec_len
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
Definition: vec_bootstrap.h:142
vlib_error_main_t::counters
u64 * counters
Definition: error.h:64
vec_add1
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:606
gmon_main_t::current_sig_errors
u64 current_sig_errors
Definition: gmon.c:54
gmon_main_t::svmdb_client
svmdb_client_t * svmdb_client
Definition: gmon.c:48
unformat_check_input
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:163
uword
u64 uword
Definition: types.h:112
pool.h
Fixed length block allocator. Pools are built from clib vectors and bitmaps. Use pools when repeatedl...
vlibapi_get_main
static api_main_t * vlibapi_get_main(void)
Definition: api_common.h:390
svmdb_map_args_t::gid
u32 gid
Definition: svmdb.h:89
gmon_main_t::my_vlib_mains
vlib_main_t ** my_vlib_mains
Definition: gmon.c:57
gmon_main_t::last_sig_errors
u64 last_sig_errors
Definition: gmon.c:53
time.h
i
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:261
f64
double f64
Definition: types.h:142
gmon_main_t::sig_error_rate_ptr
f64 * sig_error_rate_ptr
Definition: gmon.c:51
gmon_process
static uword gmon_process(vlib_main_t *vm, vlib_node_runtime_t *rt, vlib_frame_t *f)
Definition: gmon.c:107
format_unformat_error
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
format.h
VLIB_CLI_COMMAND
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:163
clib_bitmap_set
static uword * clib_bitmap_set(uword *ai, uword i, uword value)
Sets the ith bit of a bitmap to new_value Removes trailing zeros from the bitmap.
Definition: bitmap.h:167
VLIB_API_INIT_FUNCTION
VLIB_API_INIT_FUNCTION(publish_pid)
svmdb.h
vlib_node_registration_t
struct _vlib_node_registration vlib_node_registration_t
gm
#define gm
Definition: dlmalloc.c:1219
api_main_t
API main structure, used by both vpp and binary API clients.
Definition: api_common.h:228
vec_free
#define vec_free(V)
Free vector's memory (no header).
Definition: vec.h:395
index
u32 index
Definition: flow_types.api:221
svmdb_map_args_t::uid
u32 uid
Definition: svmdb.h:88
significant_error_enable_disable
static int significant_error_enable_disable(gmon_main_t *gm, u32 index, int enable)
Definition: gmon.c:242
gmon_main_t::vector_rate_ptr
f64 * vector_rate_ptr
Definition: gmon.c:49
vlib_error_main_t::counters_last_clear
u64 * counters_last_clear
Definition: error.h:67
u64
unsigned long u64
Definition: types.h:89
vlib_process_suspend
static uword vlib_process_suspend(vlib_main_t *vm, f64 dt)
Suspend a vlib cooperative multi-tasking thread for a period of time.
Definition: node_funcs.h:486
svmdb_map
svmdb_client_t * svmdb_map(svmdb_map_args_t *dba)
Definition: svmdb.c:70
u32
unsigned int u32
Definition: types.h:88
VLIB_INIT_FUNCTION
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:172
SVMDB_NAMESPACE_VEC
@ SVMDB_NAMESPACE_VEC
Definition: svmdb.h:51
vpe_api_init
clib_error_t * vpe_api_init(vlib_main_t *vm)
Definition: api.c:652
VLIB_NODE_TYPE_PROCESS
@ VLIB_NODE_TYPE_PROCESS
Definition: node.h:84
vlibmemory_init
clib_error_t * vlibmemory_init(vlib_main_t *vm)
Definition: memory_api.c:931
set_significant_error_command
static vlib_cli_command_t set_significant_error_command
(constructor) VLIB_CLI_COMMAND (set_significant_error_command)
Definition: gmon.c:292
vec.h
now
f64 now
Definition: nat44_ei_out2in.c:710
clib_memset
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
vlib_main_t
Definition: main.h:102
gmon_main_t::vlib_main
vlib_main_t * vlib_main
Definition: gmon.c:56
vlib_get_n_threads
static u32 vlib_get_n_threads()
Definition: global_funcs.h:23
hash.h
clib_error_t
Definition: clib_error.h:21
unix.h
rt
vnet_interface_output_runtime_t * rt
Definition: interface_output.c:399
vlib_init_function_t
clib_error_t *() vlib_init_function_t(struct vlib_main_t *vm)
Definition: init.h:51
gmon_main_t::input_rate_ptr
f64 * input_rate_ptr
Definition: gmon.c:50
devices.h
rv
int __clib_unused rv
Definition: application.c:491
vlib_time_now
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:325
VLIB_MAIN_LOOP_EXIT_FUNCTION
#define VLIB_MAIN_LOOP_EXIT_FUNCTION(x)
Definition: init.h:177
vnet.h
api_errno.h
clib_bitmap_foreach
#define clib_bitmap_foreach(i, ai)
Macro to iterate across set bits in a bitmap.
Definition: bitmap.h:361
vlib_node_runtime_t
Definition: node.h:454
vlib_cli_command_t
Definition: cli.h:92
gmon_process_node
static vlib_node_registration_t gmon_process_node
(constructor) VLIB_REGISTER_NODE (gmon_process_node)
Definition: gmon.c:149
vnet_get_aggregate_rx_packets
static u64 vnet_get_aggregate_rx_packets(void)
Definition: devices.h:74
heap.h
gmon_main
gmon_main_t gmon_main
Definition: gmon.c:65
svmdb_map_args_t
Definition: svmdb.h:84
UNFORMAT_END_OF_INPUT
#define UNFORMAT_END_OF_INPUT
Definition: format.h:137
VLIB_REGISTER_NODE
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169