FD.io VPP  v16.09
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/mheap.h>
35 #include <vppinfra/heap.h>
36 #include <vppinfra/pool.h>
37 #include <vppinfra/format.h>
38 #include <vlibapi/api.h>
39 #include <vlibmemory/api.h>
40 
41 #include <vlib/vlib.h>
42 #include <vlib/unix/unix.h>
43 #include <vnet/api_errno.h>
44 
45 #include <svmdb.h>
46 
47 typedef struct
48 {
53  pid_t *vpef_pid_ptr;
59 
60 } gmon_main_t;
61 
62 #if DPDK == 0
63 static inline u64
65 {
66  return 0;
67 }
68 #else
69 #include <vlib/vlib.h>
70 #include <vnet/vnet.h>
71 #include <vnet/devices/dpdk/dpdk.h>
72 #endif
73 
75 
76 static u64
78 {
79  vlib_main_t *this_vlib_main;
81  uword code;
82  int vm_index;
83  u64 significant_errors = 0;
84 
85  /* *INDENT-OFF* */
87  ({
88  for (vm_index = 0; vm_index < vec_len (gm->my_vlib_mains); vm_index++)
89  {
90  this_vlib_main = gm->my_vlib_mains[vm_index];
91  em = &this_vlib_main->error_main;
92  significant_errors += em->counters[code] -
93  ((vec_len(em->counters_last_clear) > code) ?
94  em->counters_last_clear[code] : 0);
95  }
96  }));
97  /* *INDENT-ON* */
98 
99  return (significant_errors);
100 }
101 
102 static clib_error_t *
104 {
105  gmon_main_t *gm = &gmon_main;
106 
107  *gm->vpef_pid_ptr = getpid ();
108 
109  return 0;
110 }
111 
113 
114 
115 static uword
117 {
118  f64 vector_rate;
119  u64 input_packets, last_input_packets, new_sig_errors;
120  f64 last_runtime, dt, now;
121  gmon_main_t *gm = &gmon_main;
122  int i;
123 
124  last_runtime = 0.0;
125  last_input_packets = 0;
126 
127  last_runtime = 0.0;
128  last_input_packets = 0;
129 
130  /* Initial wait for the world to settle down */
131  vlib_process_suspend (vm, 5.0);
132 
133  if (vec_len (vlib_mains) == 0)
135  else
136  {
137  for (i = 0; i < vec_len (vlib_mains); i++)
139  }
140 
141  while (1)
142  {
143  vlib_process_suspend (vm, 5.0);
144  vector_rate = vlib_last_vector_length_per_node (vm);
145  *gm->vector_rate_ptr = vector_rate;
146  now = vlib_time_now (vm);
147  dt = now - last_runtime;
148  input_packets = vnet_get_aggregate_rx_packets ();
149  *gm->input_rate_ptr = (f64) (input_packets - last_input_packets) / dt;
150  last_runtime = now;
151  last_input_packets = input_packets;
152 
153  new_sig_errors = get_significant_errors (gm);
154  *gm->sig_error_rate_ptr =
155  ((f64) (new_sig_errors - gm->last_sig_errors)) / dt;
156  gm->last_sig_errors = new_sig_errors;
157  }
158 
159  return 0; /* not so much */
160 }
161 
162 /* *INDENT-OFF* */
164  .function = gmon_process,
165  .type = VLIB_NODE_TYPE_PROCESS,
166  .name = "gmon-process",
167 };
168 /* *INDENT-ON* */
169 
170 static clib_error_t *
172 {
173  gmon_main_t *gm = &gmon_main;
174  api_main_t *am = &api_main;
175  pid_t *swp = 0;
176  f64 *v = 0;
177  clib_error_t *error;
178  svmdb_map_args_t _ma, *ma = &_ma;
179 
180  if ((error = vlib_call_init_function (vm, vpe_api_init)))
181  return (error);
182 
183  if ((error = vlib_call_init_function (vm, vlibmemory_init)))
184  return (error);
185 
186  gm->vlib_main = vm;
187 
188  memset (ma, 0, sizeof (*ma));
189  ma->root_path = am->root_path;
190  ma->uid = am->api_uid;
191  ma->gid = am->api_gid;
192 
193  gm->svmdb_client = svmdb_map (ma);
194 
195  /* Find or create, set to zero */
196  vec_add1 (v, 0.0);
198  "vpp_vector_rate", (char *) v, sizeof (*v));
199  vec_free (v);
200  vec_add1 (v, 0.0);
202  "vpp_input_rate", (char *) v, sizeof (*v));
203  vec_free (v);
204  vec_add1 (v, 0.0);
206  "vpp_sig_error_rate",
207  (char *) v, sizeof (*v));
208  vec_free (v);
209 
210  vec_add1 (swp, 0.0);
212  "vpp_pid", (char *) swp, sizeof (*swp));
213  vec_free (swp);
214 
215  /* the value cells will never move, so acquire references to them */
216  gm->vector_rate_ptr =
219  "vpp_vector_rate");
220  gm->input_rate_ptr =
223  "vpp_input_rate");
224  gm->sig_error_rate_ptr =
227  "vpp_sig_error_rate");
228  gm->vpef_pid_ptr =
230  SVMDB_NAMESPACE_VEC, "vpp_pid");
231  return 0;
232 }
233 
235 
236 static clib_error_t *
238 {
239  gmon_main_t *gm = &gmon_main;
240 
241  if (gm->vector_rate_ptr)
242  {
243  *gm->vector_rate_ptr = 0.0;
244  *gm->vpef_pid_ptr = 0;
245  *gm->input_rate_ptr = 0.0;
246  *gm->sig_error_rate_ptr = 0.0;
247  svm_region_unmap ((void *) gm->svmdb_client->db_rp);
248  vec_free (gm->svmdb_client);
249  }
250  return 0;
251 }
252 
254 
255 static int
257 {
258  vlib_main_t *vm = gm->vlib_main;
259  vlib_error_main_t *em = &vm->error_main;
260 
261  if (index >= vec_len (em->counters))
262  return VNET_API_ERROR_NO_SUCH_ENTRY;
263 
264  gm->sig_error_bitmap =
265  clib_bitmap_set (gm->sig_error_bitmap, index, enable);
266  return 0;
267 }
268 
269 static clib_error_t *
271  unformat_input_t * input,
272  vlib_cli_command_t * cmd)
273 {
274  u32 index;
275  int enable = 1;
276  int rv;
277  gmon_main_t *gm = &gmon_main;
278 
280  {
281  if (unformat (input, "%d", &index))
282  ;
283  else if (unformat (input, "disable"))
284  enable = 0;
285  else
286  return clib_error_return (0, "unknown input `%U'",
287  format_unformat_error, input);
288  }
289 
290  rv = significant_error_enable_disable (gm, index, enable);
291 
292  switch (rv)
293  {
294  case 0:
295  break;
296 
297  default:
298  return clib_error_return
299  (0, "significant_error_enable_disable returned %d", rv);
300  }
301 
302  return 0;
303 }
304 
305 /* *INDENT-OFF* */
306 VLIB_CLI_COMMAND (set_significant_error_command, static) = {
307  .path = "set significant error",
308  .short_help = "set significant error <counter-index-nnn> [disable]",
310 };
311 /* *INDENT-ON* */
312 
313 /*
314  * fd.io coding-style-patch-verification: ON
315  *
316  * Local Variables:
317  * eval: (c-set-style "gnu")
318  * End:
319  */
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:343
uword unformat(unformat_input_t *i, char *fmt,...)
Definition: unformat.c:966
void * svmdb_local_get_variable_reference(svmdb_client_t *client, svmdb_namespace_t namespace, char *var)
Definition: svmdb.c:372
Fixed length block allocator.
#define UNFORMAT_END_OF_INPUT
Definition: format.h:143
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:182
static uword gmon_process(vlib_main_t *vm, vlib_node_runtime_t *rt, vlib_frame_t *f)
Definition: gmon.c:116
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:482
int api_uid
Definition: api.h:145
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
f64 * vector_rate_ptr
Definition: gmon.c:50
char * root_path
Definition: svmdb.h:86
vlib_main_t ** my_vlib_mains
Definition: gmon.c:58
int api_gid
Definition: api.h:147
vlib_main_t * vlib_main
Definition: gmon.c:57
static f64 vlib_last_vector_length_per_node(vlib_main_t *vm)
Definition: main.h:273
api_main_t api_main
Definition: api.h:185
static vlib_node_registration_t gmon_process_node
(constructor) VLIB_REGISTER_NODE (gmon_process_node)
Definition: gmon.c:163
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:430
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:111
unsigned long u64
Definition: types.h:89
#define vlib_call_init_function(vm, x)
Definition: init.h:161
vlib_main_t ** vlib_mains
Definition: dpdk_buffer.c:157
u64 current_sig_errors
Definition: gmon.c:55
#define clib_bitmap_foreach(i, ai, body)
Macro to iterate across set bits in a bitmap.
Definition: bitmap.h:361
static clib_error_t * gmon_exit(vlib_main_t *vm)
Definition: gmon.c:237
vlib_error_main_t error_main
Definition: main.h:124
static clib_error_t * vpe_api_init(vlib_main_t *vm)
Definition: api.c:8178
static u64 vnet_get_aggregate_rx_packets(void)
Definition: dpdk.h:551
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:270
svm_region_t * db_rp
Definition: svmdb.h:69
u64 * counters
Definition: error.h:78
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:300
#define VLIB_MAIN_LOOP_EXIT_FUNCTION(x)
Definition: init.h:115
vlib_main_t vlib_global_main
Definition: main.c:1535
uword * sig_error_bitmap
Definition: gmon.c:56
void svmdb_local_set_vec_variable(svmdb_client_t *client, char *var, void *val_arg, u32 elsize)
Definition: svmdb.c:437
unsigned int u32
Definition: types.h:88
clib_error_t * vlibmemory_init(vlib_main_t *vm)
Definition: memory_vlib.c:1176
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
static u64 get_significant_errors(gmon_main_t *gm)
Definition: gmon.c:77
Bitmaps built as vectors of machine words.
u64 uword
Definition: types.h:112
char * root_path
Definition: api.h:182
VLIB_CLI_COMMAND(set_interface_ip_source_and_port_range_check_command, static)
f64 * input_rate_ptr
Definition: gmon.c:51
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
double f64
Definition: types.h:142
gmon_main_t gmon_main
Definition: gmon.c:74
pid_t * vpef_pid_ptr
Definition: gmon.c:53
void svm_region_unmap(void *rp_arg)
Definition: svm.c:958
svmdb_client_t * svmdb_map(svmdb_map_args_t *dba)
Definition: svmdb.c:70
svmdb_client_t * svmdb_client
Definition: gmon.c:49
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:169
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:143
#define clib_error_return(e, args...)
Definition: error.h:111
struct _unformat_input_t unformat_input_t
static int significant_error_enable_disable(gmon_main_t *gm, u32 index, int enable)
Definition: gmon.c:256
u64 last_sig_errors
Definition: gmon.c:54
VLIB_API_INIT_FUNCTION(publish_pid)
static clib_error_t * gmon_init(vlib_main_t *vm)
Definition: gmon.c:171
static clib_error_t * publish_pid(vlib_main_t *vm)
Definition: gmon.c:103
CLIB vectors are ubiquitous dynamically resized arrays with by user defined "headers".
f64 * sig_error_rate_ptr
Definition: gmon.c:52