FD.io VPP  v18.04-17-g3a0d853
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 <svm/svmdb.h>
46 
47 typedef struct
48 {
53  pid_t *vpef_pid_ptr;
59 
60 } gmon_main_t;
61 
62 #include <vlib/vlib.h>
63 #include <vnet/vnet.h>
64 #include <vnet/devices/devices.h>
65 
67 
68 static u64
70 {
71  vlib_main_t *this_vlib_main;
73  uword code;
74  int vm_index;
75  u64 significant_errors = 0;
76 
77  /* *INDENT-OFF* */
79  ({
80  for (vm_index = 0; vm_index < vec_len (gm->my_vlib_mains); vm_index++)
81  {
82  this_vlib_main = gm->my_vlib_mains[vm_index];
83  em = &this_vlib_main->error_main;
84  significant_errors += em->counters[code] -
85  ((vec_len(em->counters_last_clear) > code) ?
86  em->counters_last_clear[code] : 0);
87  }
88  }));
89  /* *INDENT-ON* */
90 
91  return (significant_errors);
92 }
93 
94 static clib_error_t *
96 {
97  gmon_main_t *gm = &gmon_main;
98 
99  *gm->vpef_pid_ptr = getpid ();
100 
101  return 0;
102 }
103 
105 
106 
107 static uword
109 {
110  f64 vector_rate;
111  u64 input_packets, last_input_packets, new_sig_errors;
112  f64 last_runtime, dt, now;
113  gmon_main_t *gm = &gmon_main;
114  int i;
115 
116  last_runtime = 0.0;
117  last_input_packets = 0;
118 
119  last_runtime = 0.0;
120  last_input_packets = 0;
121 
122  /* Initial wait for the world to settle down */
123  vlib_process_suspend (vm, 5.0);
124 
125  for (i = 0; i < vec_len (vlib_mains); i++)
127 
128  while (1)
129  {
130  vlib_process_suspend (vm, 5.0);
131  vector_rate = vlib_last_vector_length_per_node (vm);
132  *gm->vector_rate_ptr = vector_rate;
133  now = vlib_time_now (vm);
134  dt = now - last_runtime;
135  input_packets = vnet_get_aggregate_rx_packets ();
136  *gm->input_rate_ptr = (f64) (input_packets - last_input_packets) / dt;
137  last_runtime = now;
138  last_input_packets = input_packets;
139 
140  new_sig_errors = get_significant_errors (gm);
141  *gm->sig_error_rate_ptr =
142  ((f64) (new_sig_errors - gm->last_sig_errors)) / dt;
143  gm->last_sig_errors = new_sig_errors;
144  }
145 
146  return 0; /* not so much */
147 }
148 
149 /* *INDENT-OFF* */
151  .function = gmon_process,
152  .type = VLIB_NODE_TYPE_PROCESS,
153  .name = "gmon-process",
154 };
155 /* *INDENT-ON* */
156 
157 static clib_error_t *
159 {
160  gmon_main_t *gm = &gmon_main;
161  api_main_t *am = &api_main;
162  pid_t *swp = 0;
163  f64 *v = 0;
164  clib_error_t *error;
165  svmdb_map_args_t _ma, *ma = &_ma;
166 
167  if ((error = vlib_call_init_function (vm, vpe_api_init)))
168  return (error);
169 
170  if ((error = vlib_call_init_function (vm, vlibmemory_init)))
171  return (error);
172 
173  gm->vlib_main = vm;
174 
175  memset (ma, 0, sizeof (*ma));
176  ma->root_path = am->root_path;
177  ma->uid = am->api_uid;
178  ma->gid = am->api_gid;
179 
180  gm->svmdb_client = svmdb_map (ma);
181 
182  /* Find or create, set to zero */
183  vec_add1 (v, 0.0);
185  "vpp_vector_rate", (char *) v, sizeof (*v));
186  vec_free (v);
187  vec_add1 (v, 0.0);
189  "vpp_input_rate", (char *) v, sizeof (*v));
190  vec_free (v);
191  vec_add1 (v, 0.0);
193  "vpp_sig_error_rate",
194  (char *) v, sizeof (*v));
195  vec_free (v);
196 
197  vec_add1 (swp, 0.0);
199  "vpp_pid", (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 =
217  SVMDB_NAMESPACE_VEC, "vpp_pid");
218  return 0;
219 }
220 
222 
223 static clib_error_t *
225 {
226  gmon_main_t *gm = &gmon_main;
227 
228  if (gm->vector_rate_ptr)
229  {
230  *gm->vector_rate_ptr = 0.0;
231  *gm->vpef_pid_ptr = 0;
232  *gm->input_rate_ptr = 0.0;
233  *gm->sig_error_rate_ptr = 0.0;
234  svm_region_unmap ((void *) gm->svmdb_client->db_rp);
235  vec_free (gm->svmdb_client);
236  }
237  return 0;
238 }
239 
241 
242 static int
244 {
245  vlib_main_t *vm = gm->vlib_main;
246  vlib_error_main_t *em = &vm->error_main;
247 
248  if (index >= vec_len (em->counters))
249  return VNET_API_ERROR_NO_SUCH_ENTRY;
250 
251  gm->sig_error_bitmap =
252  clib_bitmap_set (gm->sig_error_bitmap, index, enable);
253  return 0;
254 }
255 
256 static clib_error_t *
258  unformat_input_t * input,
259  vlib_cli_command_t * cmd)
260 {
261  u32 index;
262  int enable = 1;
263  int rv;
264  gmon_main_t *gm = &gmon_main;
265 
267  {
268  if (unformat (input, "%d", &index))
269  ;
270  else if (unformat (input, "disable"))
271  enable = 0;
272  else
273  return clib_error_return (0, "unknown input `%U'",
274  format_unformat_error, input);
275  }
276 
277  rv = significant_error_enable_disable (gm, index, enable);
278 
279  switch (rv)
280  {
281  case 0:
282  break;
283 
284  default:
285  return clib_error_return
286  (0, "significant_error_enable_disable returned %d", rv);
287  }
288 
289  return 0;
290 }
291 
292 /* *INDENT-OFF* */
293 VLIB_CLI_COMMAND (set_significant_error_command, static) = {
294  .path = "set significant error",
295  .short_help = "set significant error <counter-index-nnn> [disable]",
297 };
298 /* *INDENT-ON* */
299 
300 /*
301  * fd.io coding-style-patch-verification: ON
302  *
303  * Local Variables:
304  * eval: (c-set-style "gnu")
305  * End:
306  */
void * svmdb_local_get_variable_reference(svmdb_client_t *client, svmdb_namespace_t namespace, char *var)
Definition: svmdb.c:378
static u64 vnet_get_aggregate_rx_packets(void)
Definition: devices.h:98
Fixed length block allocator.
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:227
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:520
int i
int api_uid
uid for the api shared memory region
Definition: api_common.h:280
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
vlib_main_t ** my_vlib_mains
Definition: gmon.c:58
int api_gid
gid for the api shared memory region
Definition: api_common.h:283
vlib_main_t ** vlib_mains
Definition: buffer.c:303
const char * root_path
Definition: svmdb.h:86
vlib_main_t * vlib_main
Definition: gmon.c:57
static f64 vlib_last_vector_length_per_node(vlib_main_t *vm)
Definition: main.h:318
static vlib_node_registration_t gmon_process_node
(constructor) VLIB_REGISTER_NODE (gmon_process_node)
Definition: gmon.c:150
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:448
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:111
const char * root_path
Chroot path to the shared memory API files.
Definition: api_common.h:331
#define clib_error_return(e, args...)
Definition: error.h:99
unsigned long u64
Definition: types.h:89
static clib_error_t * vpe_api_init(vlib_main_t *vm)
Definition: api.c:476
#define vlib_call_init_function(vm, x)
Definition: init.h:162
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:224
vlib_error_main_t error_main
Definition: main.h:141
#define v
Definition: acl.c:495
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:257
clib_error_t * vlibmemory_init(vlib_main_t *vm)
Definition: memory_api.c:876
API main structure, used by both vpp and binary API clients.
Definition: api_common.h:199
svm_region_t * db_rp
Definition: svmdb.h:69
u64 * counters
Definition: error.h:78
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:143
#define UNFORMAT_END_OF_INPUT
Definition: format.h:143
vlib_main_t * vm
Definition: buffer.c:294
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:336
#define VLIB_MAIN_LOOP_EXIT_FUNCTION(x)
Definition: init.h:116
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:154
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:561
unsigned int u32
Definition: types.h:88
static u64 get_significant_errors(gmon_main_t *gm)
Definition: gmon.c:69
Bitmaps built as vectors of machine words.
u64 uword
Definition: types.h:112
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:66
pid_t * vpef_pid_ptr
Definition: gmon.c:53
void svm_region_unmap(void *rp_arg)
Definition: svm.c:1158
svmdb_client_t * svmdb_map(svmdb_map_args_t *dba)
Definition: svmdb.c:71
svmdb_client_t * svmdb_client
Definition: gmon.c:49
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:243
api_main_t api_main
Definition: api_shared.c:35
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:158
static clib_error_t * publish_pid(vlib_main_t *vm)
Definition: gmon.c:95
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:972
CLIB vectors are ubiquitous dynamically resized arrays with by user defined "headers".
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:169
f64 * sig_error_rate_ptr
Definition: gmon.c:52