FD.io VPP  v18.10-34-gcce845e
Vector Packet Processing
vpp_get_stats.c
Go to the documentation of this file.
1 /*
2  *------------------------------------------------------------------
3  * vpp_get_stats.c
4  *
5  * Copyright (c) 2018 Cisco and/or its affiliates.
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at:
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *------------------------------------------------------------------
18  */
19 
21 #include <vlib/vlib.h>
22 
23 static int
24 stat_poll_loop (u8 ** patterns)
25 {
26  struct timespec ts, tsrem;
28  int i, j, k, lost_connection = 0;
29  f64 heartbeat, prev_heartbeat = 0;
30  u32 *stats = stat_segment_ls (patterns);
31  if (!stats)
32  {
33  return -1;
34  }
35 
36  printf ("\033[2J"); /* clear the screen */
37  while (1)
38  {
39  heartbeat = stat_segment_heartbeat ();
40  if (heartbeat > prev_heartbeat)
41  {
42  prev_heartbeat = heartbeat;
43  lost_connection = 0;
44  }
45  else
46  {
47  lost_connection++;
48  }
49  if (lost_connection > 10)
50  {
51  fformat (stderr, "Lost connection to VPP...\n");
52  return -1;
53  }
54 
55  printf ("\033[H"); /* Cursor top left corner */
56  res = stat_segment_dump (stats);
57  if (!res)
58  {
59  stats = stat_segment_ls (patterns);
60  continue;
61  }
62  for (i = 0; i < vec_len (res); i++)
63  {
64  switch (res[i].type)
65  {
67  for (k = 0; k < vec_len (res[i].simple_counter_vec); k++)
68  for (j = 0; j < vec_len (res[i].simple_counter_vec[k]); j++)
69  fformat (stdout, "[%d]: %llu packets %s\n",
70  j, res[i].simple_counter_vec[k][j], res[i].name);
71  break;
72 
74  for (k = 0; k < vec_len (res[i].simple_counter_vec); k++)
75  for (j = 0; j < vec_len (res[i].combined_counter_vec[k]); j++)
76  fformat (stdout, "[%d]: %llu packets, %llu bytes %s\n",
77  j, res[i].combined_counter_vec[k][j].packets,
78  res[i].combined_counter_vec[k][j].bytes,
79  res[i].name);
80  break;
81 
83  fformat (stdout, "%llu %s\n", res[i].error_value, res[i].name);
84  break;
85 
87  fformat (stdout, "%.2f %s\n", res[i].scalar_value, res[i].name);
88  break;
89 
90  default:
91  printf ("Unknown value\n");
92  ;
93  }
94  }
96  /* Scrape stats every 5 seconds */
97  ts.tv_sec = 1;
98  ts.tv_nsec = 0;
99  while (nanosleep (&ts, &tsrem) < 0)
100  ts = tsrem;
101 
102  }
103 }
104 
106 {
112 };
113 
114 int
115 main (int argc, char **argv)
116 {
117  unformat_input_t _argv, *a = &_argv;
118  u8 *stat_segment_name, *pattern = 0, **patterns = 0;
119  int rv;
121 
122  /* Create a heap of 64MB */
123  clib_mem_init (0, 64 << 20);
124 
125  unformat_init_command_line (a, argv);
126 
127  stat_segment_name = (u8 *) STAT_SEGMENT_SOCKET_FILE;
128 
130  {
131  if (unformat (a, "socket-name %s", &stat_segment_name))
132  ;
133  else if (unformat (a, "ls"))
134  {
135  cmd = STAT_CLIENT_CMD_LS;
136  }
137  else if (unformat (a, "dump"))
138  {
139  cmd = STAT_CLIENT_CMD_DUMP;
140  }
141  else if (unformat (a, "poll"))
142  {
143  cmd = STAT_CLIENT_CMD_POLL;
144  }
145  else if (unformat (a, "tightpoll"))
146  {
148  }
149  else if (unformat (a, "%s", &pattern))
150  {
151  vec_add1 (patterns, pattern);
152  }
153  else
154  {
155  fformat (stderr,
156  "%s: usage [socket-name <name>] [ls|dump|poll] <patterns> ...\n",
157  argv[0]);
158  exit (1);
159  }
160  }
161 reconnect:
162  rv = stat_segment_connect ((char *) stat_segment_name);
163  if (rv)
164  {
165  fformat (stderr, "Couldn't connect to vpp, does %s exist?\n",
166  stat_segment_name);
167  exit (1);
168  }
169 
170  u32 *dir;
171  int i, j, k;
172  stat_segment_data_t *res;
173 
174  dir = stat_segment_ls (patterns);
175 
176  switch (cmd)
177  {
178  case STAT_CLIENT_CMD_LS:
179  /* List all counters */
180  for (i = 0; i < vec_len (dir); i++)
181  {
182  char *n = stat_segment_index_to_name (dir[i]);
183  printf ("%s\n", n);
184  free (n);
185  }
186  break;
187 
189  res = stat_segment_dump (dir);
190  for (i = 0; i < vec_len (res); i++)
191  {
192  switch (res[i].type)
193  {
195  if (res[i].simple_counter_vec == 0)
196  continue;
197  for (k = 0; k < vec_len (res[i].simple_counter_vec) - 1; k++)
198  for (j = 0; j < vec_len (res[i].simple_counter_vec[k]); j++)
199  fformat (stdout, "[%d @ %d]: %llu packets %s\n",
200  j, k, res[i].simple_counter_vec[k][j],
201  res[i].name);
202  break;
203 
205  if (res[i].simple_counter_vec == 0)
206  continue;
207  for (k = 0; k < vec_len (res[i].combined_counter_vec); k++)
208  for (j = 0; j < vec_len (res[i].combined_counter_vec[k]); j++)
209  fformat (stdout, "[%d @ %d]: %llu packets, %llu bytes %s\n",
210  j, k, res[i].combined_counter_vec[k][j].packets,
211  res[i].combined_counter_vec[k][j].bytes,
212  res[i].name);
213  break;
214 
216  fformat (stdout, "%llu %s\n", res[i].error_value, res[i].name);
217  break;
218 
220  fformat (stdout, "%.2f %s\n", res[i].scalar_value, res[i].name);
221  break;
222 
223  default:
224  ;
225  }
226  }
228  break;
229 
231  stat_poll_loop (patterns);
232  /* We can only exist the pool loop if we lost connection to VPP */
234  goto reconnect;
235  break;
236 
238  while (1)
239  {
240  res = stat_segment_dump (dir);
241  if (res == 0)
242  {
243  /* Refresh */
244  vec_free (dir);
245  dir = stat_segment_ls (patterns);
246  continue;
247  }
249  }
250  break;
251 
252  default:
253  fformat (stderr,
254  "%s: usage [socket-name <name>] [ls|dump|poll] <patterns> ...\n",
255  argv[0]);
256  }
257 
259 
260  exit (0);
261 }
262 
263 /*
264  * fd.io coding-style-patch-verification: ON
265  *
266  * Local Variables:
267  * eval: (c-set-style "gnu")
268  * End:
269  */
int stat_segment_connect(char *socket_name)
Definition: stat_client.c:86
stat_client_cmd_e
a
Definition: bitmap.h:538
void stat_segment_data_free(stat_segment_data_t *res)
Definition: stat_client.c:225
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:523
int i
int main(int argc, char **argv)
unsigned char u8
Definition: types.h:56
double f64
Definition: types.h:142
unsigned int u32
Definition: types.h:88
static int stat_poll_loop(u8 **patterns)
Definition: vpp_get_stats.c:24
struct _unformat_input_t unformat_input_t
void unformat_init_command_line(unformat_input_t *input, char *argv[])
Definition: unformat.c:1007
u8 name[64]
Definition: memclnt.api:151
void stat_segment_disconnect(void)
Definition: stat_client.c:143
word fformat(FILE *f, char *fmt,...)
Definition: format.c:453
void * clib_mem_init(void *heap, uword size)
Definition: mem_dlmalloc.c:206
#define STAT_SEGMENT_SOCKET_FILE
Definition: stat_segment.h:24
#define UNFORMAT_END_OF_INPUT
Definition: format.h:144
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:339
char * stat_segment_index_to_name(uint32_t index)
Definition: stat_client.c:403
uint32_t * stat_segment_ls(uint8_t **patterns)
Definition: stat_client.c:278
stat_segment_data_t * stat_segment_dump(uint32_t *stats)
Definition: stat_client.c:332
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:972
double stat_segment_heartbeat(void)
Definition: stat_client.c:152
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:170