FD.io VPP  v21.01.1
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* */
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 < vec_len (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 {
160  api_main_t *am = vlibapi_get_main ();
161  pid_t *swp = 0;
162  f64 *v = 0;
164  svmdb_map_args_t _ma, *ma = &_ma;
165 
166  if ((error = vlib_call_init_function (vm, vpe_api_init)))
167  return (error);
168 
169  if ((error = vlib_call_init_function (vm, vlibmemory_init)))
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);
184  "vpp_vector_rate", (char *) v, sizeof (*v));
185  vec_free (v);
186  vec_add1 (v, 0.0);
188  "vpp_input_rate", (char *) v, sizeof (*v));
189  vec_free (v);
190  vec_add1 (v, 0.0);
192  "vpp_sig_error_rate",
193  (char *) v, sizeof (*v));
194  vec_free (v);
195 
196  vec_add1 (swp, 0.0);
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 =
205  "vpp_vector_rate");
206  gm->input_rate_ptr =
209  "vpp_input_rate");
210  gm->sig_error_rate_ptr =
213  "vpp_sig_error_rate");
214  gm->vpef_pid_ptr =
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;
245  vlib_error_main_t *em = &vm->error_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 
276  rv = significant_error_enable_disable (gm, index, enable);
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* */
292 VLIB_CLI_COMMAND (set_significant_error_command, static) = {
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  */
void * svmdb_local_get_variable_reference(svmdb_client_t *client, svmdb_namespace_t namespace, char *var)
Definition: svmdb.c:377
static u64 vnet_get_aggregate_rx_packets(void)
Definition: devices.h:96
Optimized string handling code, including c11-compliant "safe C library" variants.
unsigned long u64
Definition: types.h:89
Fixed length block allocator.
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:334
#define clib_bitmap_foreach(i, ai)
Macro to iterate across set bits in a bitmap.
Definition: bitmap.h:361
static uword gmon_process(vlib_main_t *vm, vlib_node_runtime_t *rt, vlib_frame_t *f)
Definition: gmon.c:107
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:592
int api_uid
uid for the api shared memory region
Definition: api_common.h:311
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_main_t * vm
Definition: in2out_ed.c:1580
f64 * vector_rate_ptr
Definition: gmon.c:49
vlib_main_t ** my_vlib_mains
Definition: gmon.c:57
int api_gid
gid for the api shared memory region
Definition: api_common.h:314
vlib_main_t ** vlib_mains
Definition: buffer.c:332
const char * root_path
Definition: svmdb.h:86
double f64
Definition: types.h:142
vlib_main_t * vlib_main
Definition: gmon.c:56
u64 * counters_last_clear
Definition: error.h:65
static vlib_node_registration_t gmon_process_node
(constructor) VLIB_REGISTER_NODE (gmon_process_node)
Definition: gmon.c:149
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:482
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:173
const char * root_path
Chroot path to the shared memory API files.
Definition: api_common.h:359
#define clib_error_return(e, args...)
Definition: error.h:99
unsigned int u32
Definition: types.h:88
#define vlib_call_init_function(vm, x)
Definition: init.h:270
u64 current_sig_errors
Definition: gmon.c:54
Definition: cJSON.c:84
static clib_error_t * gmon_exit(vlib_main_t *vm)
Definition: gmon.c:223
vlib_error_main_t error_main
Definition: main.h:210
#define gm
Definition: dlmalloc.c:1219
struct _unformat_input_t unformat_input_t
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
clib_error_t * vlibmemory_init(vlib_main_t *vm)
Definition: memory_api.c:931
API main structure, used by both vpp and binary API clients.
Definition: api_common.h:227
svm_region_t * db_rp
Definition: svmdb.h:69
u64 * counters
Definition: error.h:62
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:170
#define UNFORMAT_END_OF_INPUT
Definition: format.h:144
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:317
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:380
clib_error_t * vpe_api_init(vlib_main_t *vm)
Definition: api.c:655
#define VLIB_MAIN_LOOP_EXIT_FUNCTION(x)
Definition: init.h:178
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:158
uword * sig_error_bitmap
Definition: gmon.c:55
void svmdb_local_set_vec_variable(svmdb_client_t *client, char *var, void *val_arg, u32 elsize)
Definition: svmdb.c:560
static u64 get_significant_errors(gmon_main_t *gm)
Definition: gmon.c:68
Bitmaps built as vectors of machine words.
f64 * input_rate_ptr
Definition: gmon.c:50
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
gmon_main_t gmon_main
Definition: gmon.c:65
u64 uword
Definition: types.h:112
pid_t * vpef_pid_ptr
Definition: gmon.c:52
void svm_region_unmap(void *rp_arg)
Definition: svm.c:1140
u32 index
Definition: flow_types.api:221
svmdb_client_t * svmdb_map(svmdb_map_args_t *dba)
Definition: svmdb.c:70
svmdb_client_t * svmdb_client
Definition: gmon.c:48
static api_main_t * vlibapi_get_main(void)
Definition: api_common.h:389
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
static int significant_error_enable_disable(gmon_main_t *gm, u32 index, int enable)
Definition: gmon.c:242
u64 last_sig_errors
Definition: gmon.c:53
VLIB_API_INIT_FUNCTION(publish_pid)
static clib_error_t * gmon_init(vlib_main_t *vm)
Definition: gmon.c:157
static clib_error_t * publish_pid(vlib_main_t *vm)
Definition: gmon.c:94
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
CLIB vectors are ubiquitous dynamically resized arrays with by user defined "headers".
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:170
f64 * sig_error_rate_ptr
Definition: gmon.c:51