FD.io VPP  v19.08.2-294-g37e99c22d
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  continue;
85 
86  /* ignore SIGPIPE, SIGCHLD */
87  case SIGPIPE:
88  case SIGCHLD:
89  sa.sa_sigaction = (void *) SIG_IGN;
90  break;
91 
92  /* catch and handle all other signals */
93  default:
94  break;
95  }
96 
97  if (sigaction (i, &sa, 0) < 0)
98  return clib_unix_warning (0, "sigaction %U", format_signal, i);
99  }
100 }
101 
102 int
103 main (int argc, char **argv)
104 {
105  unformat_input_t input;
106  char *chroot_path = 0;
107  u8 *chroot_path_u8;
108  int interval = 0;
109  f64 *vector_ratep, *rx_ratep, *sig_error_ratep;
110  pid_t *vpp_pidp;
111  svmdb_map_args_t _ma, *ma = &_ma;
112  int uid, gid, rv;
113  struct passwd _pw, *pw;
114  struct group _grp, *grp;
115  char *s, buf[128];
116 
117  unformat_init_command_line (&input, argv);
118 
119  uid = geteuid ();
120  gid = getegid ();
121 
122  while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
123  {
124  if (unformat (&input, "chroot %s", &chroot_path_u8))
125  {
126  chroot_path = (char *) chroot_path_u8;
127  }
128  else if (unformat (&input, "interval %d", &interval))
129  ;
130  else if (unformat (&input, "uid %d", &uid))
131  ;
132  else if (unformat (&input, "gid %d", &gid))
133  ;
134  else if (unformat (&input, "uid %s", &s))
135  {
136  /* lookup the username */
137  pw = NULL;
138  rv = getpwnam_r (s, &_pw, buf, sizeof (buf), &pw);
139  if (rv < 0)
140  {
141  fformat (stderr, "cannot fetch username %s", s);
142  exit (1);
143  }
144  if (pw == NULL)
145  {
146  fformat (stderr, "username %s does not exist", s);
147  exit (1);
148  }
149  vec_free (s);
150  uid = pw->pw_uid;
151  }
152  else if (unformat (&input, "gid %s", &s))
153  {
154  /* lookup the group name */
155  grp = NULL;
156  rv = getgrnam_r (s, &_grp, buf, sizeof (buf), &grp);
157  if (rv != 0)
158  {
159  fformat (stderr, "cannot fetch group %s", s);
160  exit (1);
161  }
162  if (grp == NULL)
163  {
164  fformat (stderr, "group %s does not exist", s);
165  exit (1);
166  }
167  vec_free (s);
168  gid = grp->gr_gid;
169  }
170  else
171  {
172  fformat (stderr,
173  "usage: vpp_get_metrics [chroot <path>] [interval <nn>]\n");
174  exit (1);
175  }
176  }
177 
179 
180  clib_memset (ma, 0, sizeof (*ma));
181  ma->root_path = chroot_path;
182  ma->uid = uid;
183  ma->gid = gid;
184 
185  c = svmdb_map (ma);
186 
187  vpp_pidp =
189  vector_ratep =
191  "vpp_vector_rate");
192  rx_ratep =
194  "vpp_input_rate");
195  sig_error_ratep =
197  "vpp_sig_error_rate");
198 
199  /*
200  * Make sure vpp is actually running. Otherwise, there's every
201  * chance that the database region will be wiped out by the
202  * process monitor script
203  */
204 
205  if (vpp_pidp == 0 || vector_ratep == 0 || rx_ratep == 0
206  || sig_error_ratep == 0)
207  {
208  fformat (stdout, "vpp not running\n");
209  exit (1);
210  }
211 
212  do
213  {
214  /*
215  * Once vpp exits, the svm db region will be recreated...
216  * Can't use kill (*vpp_pidp, 0) if running as non-root /
217  * accessing the shared-VM database via group perms.
218  */
219  if (*vpp_pidp == 0)
220  {
221  fformat (stdout, "vpp not running\n");
222  exit (1);
223  }
224  fformat (stdout,
225  "%d: vpp_vector_rate=%.2f, vpp_input_rate=%f, vpp_sig_error_rate=%f\n",
226  *vpp_pidp, *vector_ratep, *rx_ratep, *sig_error_ratep);
227 
228  if (interval)
229  sleep (interval);
230  if (signal_received)
231  break;
232  }
233  while (interval);
234 
235  svmdb_unmap (c);
236  exit (0);
237 }
238 
239 /*
240  * fd.io coding-style-patch-verification: ON
241  *
242  * Local Variables:
243  * eval: (c-set-style "gnu")
244  * End:
245  */
246 
247 /*
248  * fd.io coding-style-patch-verification: ON
249  *
250  * Local Variables:
251  * eval: (c-set-style "gnu")
252  * End:
253  */
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)
int i
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
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:341
Bitmaps built as vectors of machine words.
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
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