FD.io VPP  v20.05.1-6-gf53edbc3b
Vector Packet Processing
vpp_get_metrics.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 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 <pwd.h>
22 #include <grp.h>
23 #include <netinet/in.h>
24 #include <signal.h>
25 #include <pthread.h>
26 #include <unistd.h>
27 #include <time.h>
28 #include <fcntl.h>
29 #include <string.h>
30 #include <vppinfra/clib.h>
31 #include <vppinfra/vec.h>
32 #include <vppinfra/hash.h>
33 #include <vppinfra/bitmap.h>
34 #include <vppinfra/fifo.h>
35 #include <vppinfra/time.h>
36 #include <vppinfra/mheap.h>
37 #include <vppinfra/heap.h>
38 #include <vppinfra/pool.h>
39 #include <vppinfra/format.h>
40 #include <vlibapi/api.h>
41 #include <vlibmemory/api.h>
42 
43 #include <vlib/vlib.h>
44 #include <vlib/unix/unix.h>
45 #include <vnet/api_errno.h>
46 
47 #include <svm/svmdb.h>
48 
50 volatile int signal_received;
51 
52 static void
53 unix_signal_handler (int signum, siginfo_t * si, ucontext_t * uc)
54 {
55  static int once;
56 
57  if (once)
58  exit (1);
59 
60  once = 1;
61  signal_received = 1;
62 }
63 
64 static void
66 {
67  uword i;
68  struct sigaction sa;
69 
70  for (i = 1; i < 32; i++)
71  {
72  clib_memset (&sa, 0, sizeof (sa));
73  sa.sa_sigaction = (void *) unix_signal_handler;
74  sa.sa_flags = SA_SIGINFO;
75 
76  switch (i)
77  {
78  /* these signals take the default action */
79  case SIGABRT:
80  case SIGKILL:
81  case SIGSTOP:
82  case SIGUSR1:
83  case SIGUSR2:
84  case SIGPROF:
85  continue;
86 
87  /* ignore SIGPIPE, SIGCHLD */
88  case SIGPIPE:
89  case SIGCHLD:
90  sa.sa_sigaction = (void *) SIG_IGN;
91  break;
92 
93  /* catch and handle all other signals */
94  default:
95  break;
96  }
97 
98  if (sigaction (i, &sa, 0) < 0)
99  return clib_unix_warning (0, "sigaction %U", format_signal, i);
100  }
101 }
102 
103 int
104 main (int argc, char **argv)
105 {
106  unformat_input_t input;
107  char *chroot_path = 0;
108  u8 *chroot_path_u8;
109  int interval = 0;
110  f64 *vector_ratep, *rx_ratep, *sig_error_ratep;
111  pid_t *vpp_pidp;
112  svmdb_map_args_t _ma, *ma = &_ma;
113  int uid, gid, rv;
114  struct passwd _pw, *pw;
115  struct group _grp, *grp;
116  char *s, buf[128];
117 
118  unformat_init_command_line (&input, argv);
119 
120  uid = geteuid ();
121  gid = getegid ();
122 
123  while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
124  {
125  if (unformat (&input, "chroot %s", &chroot_path_u8))
126  {
127  chroot_path = (char *) chroot_path_u8;
128  }
129  else if (unformat (&input, "interval %d", &interval))
130  ;
131  else if (unformat (&input, "uid %d", &uid))
132  ;
133  else if (unformat (&input, "gid %d", &gid))
134  ;
135  else if (unformat (&input, "uid %s", &s))
136  {
137  /* lookup the username */
138  pw = NULL;
139  rv = getpwnam_r (s, &_pw, buf, sizeof (buf), &pw);
140  if (rv < 0)
141  {
142  fformat (stderr, "cannot fetch username %s", s);
143  exit (1);
144  }
145  if (pw == NULL)
146  {
147  fformat (stderr, "username %s does not exist", s);
148  exit (1);
149  }
150  vec_free (s);
151  uid = pw->pw_uid;
152  }
153  else if (unformat (&input, "gid %s", &s))
154  {
155  /* lookup the group name */
156  grp = NULL;
157  rv = getgrnam_r (s, &_grp, buf, sizeof (buf), &grp);
158  if (rv != 0)
159  {
160  fformat (stderr, "cannot fetch group %s", s);
161  exit (1);
162  }
163  if (grp == NULL)
164  {
165  fformat (stderr, "group %s does not exist", s);
166  exit (1);
167  }
168  vec_free (s);
169  gid = grp->gr_gid;
170  }
171  else
172  {
173  fformat (stderr,
174  "usage: vpp_get_metrics [chroot <path>] [interval <nn>]\n");
175  exit (1);
176  }
177  }
178 
180 
181  clib_memset (ma, 0, sizeof (*ma));
182  ma->root_path = chroot_path;
183  ma->uid = uid;
184  ma->gid = gid;
185 
186  c = svmdb_map (ma);
187 
188  vpp_pidp =
190  vector_ratep =
192  "vpp_vector_rate");
193  rx_ratep =
195  "vpp_input_rate");
196  sig_error_ratep =
198  "vpp_sig_error_rate");
199 
200  /*
201  * Make sure vpp is actually running. Otherwise, there's every
202  * chance that the database region will be wiped out by the
203  * process monitor script
204  */
205 
206  if (vpp_pidp == 0 || vector_ratep == 0 || rx_ratep == 0
207  || sig_error_ratep == 0)
208  {
209  fformat (stdout, "vpp not running\n");
210  exit (1);
211  }
212 
213  do
214  {
215  /*
216  * Once vpp exits, the svm db region will be recreated...
217  * Can't use kill (*vpp_pidp, 0) if running as non-root /
218  * accessing the shared-VM database via group perms.
219  */
220  if (*vpp_pidp == 0)
221  {
222  fformat (stdout, "vpp not running\n");
223  exit (1);
224  }
225  fformat (stdout,
226  "%d: vpp_vector_rate=%.2f, vpp_input_rate=%f, vpp_sig_error_rate=%f\n",
227  *vpp_pidp, *vector_ratep, *rx_ratep, *sig_error_ratep);
228 
229  if (interval)
230  sleep (interval);
231  if (signal_received)
232  break;
233  }
234  while (interval);
235 
236  svmdb_unmap (c);
237  exit (0);
238 }
239 
240 /*
241  * fd.io coding-style-patch-verification: ON
242  *
243  * Local Variables:
244  * eval: (c-set-style "gnu")
245  * End:
246  */
247 
248 /*
249  * fd.io coding-style-patch-verification: ON
250  *
251  * Local Variables:
252  * eval: (c-set-style "gnu")
253  * End:
254  */
void * svmdb_local_get_variable_reference(svmdb_client_t *client, svmdb_namespace_t namespace, char *var)
Definition: svmdb.c:378
Optimized string handling code, including c11-compliant "safe C library" variants.
static void unix_signal_handler(int signum, siginfo_t *si, ucontext_t *uc)
Fixed length block allocator.
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
const char * root_path
Definition: svmdb.h:86
unsigned char u8
Definition: types.h:56
double f64
Definition: types.h:142
void svmdb_unmap(svmdb_client_t *client)
Definition: svmdb.c:139
struct _unformat_input_t unformat_input_t
void unformat_init_command_line(unformat_input_t *input, char *argv[])
Definition: unformat.c:1013
word fformat(FILE *f, char *fmt,...)
Definition: format.c:462
static void setup_signal_handlers(void)
int main(int argc, char **argv)
#define UNFORMAT_END_OF_INPUT
Definition: format.h:145
svmdb_client_t * c
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
Bitmaps built as vectors of machine words.
u16 interval
Definition: vrrp.api:34
volatile int signal_received
u64 uword
Definition: types.h:112
u8 * format_signal(u8 *s, va_list *args)
Definition: unix-formats.c:858
#define clib_unix_warning(format, args...)
Definition: error.h:68
svmdb_client_t * svmdb_map(svmdb_map_args_t *dba)
Definition: svmdb.c:71
u8 si
Definition: lisp_types.api:47
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:171