FD.io VPP  v16.06
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 {
52  pid_t *vpef_pid_ptr;
58 
59 } gmon_main_t;
60 
61 #if DPDK == 0
62 static inline u64 vnet_get_aggregate_rx_packets (void)
63 { return 0; }
64 #else
65 #include <vlib/vlib.h>
66 #include <vnet/vnet.h>
67 #include <vnet/devices/dpdk/dpdk.h>
68 #endif
69 
71 
73 {
74  vlib_main_t * this_vlib_main;
75  vlib_error_main_t * em;
76  uword code;
77  int vm_index;
78  u64 significant_errors = 0;
79 
81  ({
82  for (vm_index = 0; vm_index < vec_len (gm->my_vlib_mains); vm_index++)
83  {
84  this_vlib_main = gm->my_vlib_mains[vm_index];
85  em = &this_vlib_main->error_main;
86  significant_errors += em->counters[code] -
87  ((vec_len(em->counters_last_clear) > code) ?
88  em->counters_last_clear[code] : 0);
89  }
90  }));
91 
92  return (significant_errors);
93 }
94 
95 static clib_error_t *
97 {
98  gmon_main_t *gm = &gmon_main;
99 
100  *gm->vpef_pid_ptr = getpid();
101 
102  return 0;
103 }
105 
106 
107 static uword
109  vlib_node_runtime_t * rt,
110  vlib_frame_t * f)
111 {
112  f64 vector_rate;
113  u64 input_packets, last_input_packets, new_sig_errors;
114  f64 last_runtime, dt, now;
115  gmon_main_t *gm = &gmon_main;
116  int i;
117 
118  last_runtime = 0.0;
119  last_input_packets = 0;
120 
121  last_runtime = 0.0;
122  last_input_packets = 0;
123 
124  /* Initial wait for the world to settle down */
125  vlib_process_suspend (vm, 5.0);
126 
127  if (vec_len(vlib_mains) == 0)
129  else
130  {
131  for (i = 0; i < vec_len(vlib_mains); i++)
133  }
134 
135  while (1) {
136  vlib_process_suspend (vm, 5.0);
137  vector_rate = vlib_last_vector_length_per_node (vm);
138  *gm->vector_rate_ptr = vector_rate;
139  now = vlib_time_now(vm);
140  dt = now - last_runtime;
141  input_packets = vnet_get_aggregate_rx_packets();
142  *gm->input_rate_ptr = (f64)(input_packets - last_input_packets) / dt;
143  last_runtime = now;
144  last_input_packets = input_packets;
145 
146  new_sig_errors = get_significant_errors(gm);
147  *gm->sig_error_rate_ptr =
148  ((f64)(new_sig_errors - gm->last_sig_errors)) / dt;
149  gm->last_sig_errors = new_sig_errors;
150  }
151 
152  return 0; /* not so much */
153 }
154 
156  .function = gmon_process,
157  .type = VLIB_NODE_TYPE_PROCESS,
158  .name = "gmon-process",
159 };
160 
161 static clib_error_t *
163 {
164  gmon_main_t *gm = &gmon_main;
165  api_main_t * am = &api_main;
166  pid_t *swp = 0;
167  f64 *v = 0;
168  clib_error_t * error;
169 
170  if ((error = vlib_call_init_function(vm, vpe_api_init)))
171  return(error);
172 
173  /* Make sure that /global-vm is owned as directed */
175 
176  gm->vlib_main = vm;
178 
179  /* Find or create, set to zero */
180  vec_add1 (v, 0.0);
182  "vpp_vector_rate",
183  (char *)v, sizeof (*v));
184  vec_free(v);
185  vec_add1 (v, 0.0);
187  "vpp_input_rate",
188  (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",
199  (char *)swp, sizeof (*swp));
200  vec_free(swp);
201 
202  /* the value cells will never move, so acquire references to them */
203  gm->vector_rate_ptr =
206  "vpp_vector_rate");
207  gm->input_rate_ptr =
210  "vpp_input_rate");
211  gm->sig_error_rate_ptr =
214  "vpp_sig_error_rate");
215  gm->vpef_pid_ptr =
218  "vpp_pid");
219  return 0;
220 }
221 
223 
225 {
226  gmon_main_t *gm = &gmon_main;
227 
228  if (gm->vector_rate_ptr) {
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 }
239 
240 static int
242  u32 index, int enable)
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 = clib_bitmap_set (gm->sig_error_bitmap, index, enable);
251  return 0;
252 }
253 
254 static clib_error_t *
256  unformat_input_t * input,
257  vlib_cli_command_t * cmd)
258 {
259  u32 index;
260  int enable = 1;
261  int rv;
262  gmon_main_t *gm = &gmon_main;
263 
264  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
265  if (unformat (input, "%d", &index))
266  ;
267  else if (unformat (input, "disable"))
268  enable = 0;
269  else
270  return clib_error_return (0, "unknown input `%U'",
271  format_unformat_error, input);
272  }
273 
274  rv = significant_error_enable_disable (gm, index, enable);
275 
276  switch (rv)
277  {
278  case 0:
279  break;
280 
281  default:
282  return clib_error_return
283  (0, "significant_error_enable_disable returned %d", rv);
284  }
285 
286  return 0;
287 }
288 
289 VLIB_CLI_COMMAND (set_significant_error_command, static) = {
290  .path = "set significant error",
291  .short_help = "set significant error <counter-index-nnn>",
293 };
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:267
uword unformat(unformat_input_t *i, char *fmt,...)
Definition: unformat.c:942
void * svmdb_local_get_variable_reference(svmdb_client_t *client, svmdb_namespace_t namespace, char *var)
Definition: svmdb.c:362
#define UNFORMAT_END_OF_INPUT
Definition: format.h:142
static uword gmon_process(vlib_main_t *vm, vlib_node_runtime_t *rt, vlib_frame_t *f)
Definition: gmon.c:108
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:480
int api_uid
Definition: api.h:139
f64 * vector_rate_ptr
Definition: gmon.c:49
always_inline uword vlib_process_suspend(vlib_main_t *vm, f64 dt)
Definition: node_funcs.h:326
vlib_main_t ** my_vlib_mains
Definition: gmon.c:57
int api_gid
Definition: api.h:138
vlib_main_t * vlib_main
Definition: gmon.c:56
always_inline uword unformat_check_input(unformat_input_t *i)
Definition: format.h:168
api_main_t api_main
Definition: api.h:162
vlib_node_registration_t gmon_process_node
(constructor) VLIB_REGISTER_NODE (gmon_process_node)
Definition: gmon.c:155
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:109
unsigned long u64
Definition: types.h:89
#define vlib_call_init_function(vm, x)
Definition: init.h:159
u64 current_sig_errors
Definition: gmon.c:54
#define clib_bitmap_foreach(i, ai, body)
Definition: bitmap.h:308
static clib_error_t * gmon_exit(vlib_main_t *vm)
Definition: gmon.c:224
vlib_error_main_t error_main
Definition: main.h:124
always_inline f64 vlib_last_vector_length_per_node(vlib_main_t *vm)
Definition: main.h:267
always_inline uword * clib_bitmap_set(uword *ai, uword i, uword value)
Definition: bitmap.h:132
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:255
svm_region_t * db_rp
Definition: svmdb.h:63
u64 * counters
Definition: error.h:73
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:298
#define VLIB_MAIN_LOOP_EXIT_FUNCTION(x)
Definition: init.h:113
vlib_main_t vlib_global_main
Definition: main.c:1505
void svm_region_init_chroot_uid_gid(char *root_path, int uid, int gid)
Definition: svm.c:687
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:150
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:421
unsigned int u32
Definition: types.h:88
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:87
static u64 get_significant_errors(gmon_main_t *gm)
Definition: gmon.c:72
u64 uword
Definition: types.h:112
svmdb_client_t * svmdb_map_chroot(char *root_path)
Definition: svmdb.c:135
char * root_path
Definition: api.h:159
f64 * input_rate_ptr
Definition: gmon.c:50
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
double f64
Definition: types.h:140
gmon_main_t gmon_main
Definition: gmon.c:70
pid_t * vpef_pid_ptr
Definition: gmon.c:52
void svm_region_unmap(void *rp_arg)
Definition: svm.c:802
static u64 vnet_get_aggregate_rx_packets(void)
Definition: gmon.c:62
svmdb_client_t * svmdb_client
Definition: gmon.c:48
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:140
always_inline f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:182
#define clib_error_return(e, args...)
Definition: error.h:112
struct _unformat_input_t unformat_input_t
static clib_error_t * vpe_api_init(vlib_main_t *vm)
Definition: api.c:6070
static int significant_error_enable_disable(gmon_main_t *gm, u32 index, int enable)
Definition: gmon.c:241
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:162
static clib_error_t * publish_pid(vlib_main_t *vm)
Definition: gmon.c:96
vlib_main_t ** vlib_mains
Definition: buffer.c:244
CLIB vectors are ubiquitous dynamically resized arrays with by user defined "headers".
f64 * sig_error_rate_ptr
Definition: gmon.c:51