2 *------------------------------------------------------------------
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:
10 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
20 #include <vpp-api/client/stat_client.h>
21 #include <vlib/vlib.h>
24 stat_poll_loop (u8 ** patterns)
26 struct timespec ts, tsrem;
27 stat_segment_data_t *res;
28 int i, j, k, lost_connection = 0;
29 f64 heartbeat, prev_heartbeat = 0;
30 u32 *stats = stat_segment_ls (patterns);
36 printf ("\033[2J"); /* clear the screen */
39 heartbeat = stat_segment_heartbeat ();
40 if (heartbeat > prev_heartbeat)
42 prev_heartbeat = heartbeat;
49 if (lost_connection > 10)
51 fformat (stderr, "Lost connection to VPP...\n");
55 printf ("\033[H"); /* Cursor top left corner */
56 res = stat_segment_dump (stats);
59 stats = stat_segment_ls (patterns);
62 for (i = 0; i < vec_len (res); i++)
66 case STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE:
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);
73 case STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED:
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,
82 case STAT_DIR_TYPE_ERROR_INDEX:
83 for (j = 0; j < vec_len (res[i].error_vector); j++)
84 fformat (stdout, "%llu %s\n", res[i].error_vector[j],
88 case STAT_DIR_TYPE_SCALAR_INDEX:
89 fformat (stdout, "%.2f %s\n", res[i].scalar_value, res[i].name);
93 printf ("Unknown value\n");
97 stat_segment_data_free (res);
98 /* Scrape stats every 5 seconds */
101 while (nanosleep (&ts, &tsrem) < 0)
107 enum stat_client_cmd_e
109 STAT_CLIENT_CMD_UNKNOWN,
111 STAT_CLIENT_CMD_POLL,
112 STAT_CLIENT_CMD_DUMP,
113 STAT_CLIENT_CMD_TIGHTPOLL,
117 main (int argc, char **argv)
119 unformat_input_t _argv, *a = &_argv;
120 u8 *stat_segment_name, *pattern = 0, **patterns = 0;
122 enum stat_client_cmd_e cmd = STAT_CLIENT_CMD_UNKNOWN;
124 /* Create a heap of 64MB */
125 clib_mem_init (0, 64 << 20);
127 unformat_init_command_line (a, argv);
129 stat_segment_name = (u8 *) STAT_SEGMENT_SOCKET_FILE;
131 while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)
133 if (unformat (a, "socket-name %s", &stat_segment_name))
135 else if (unformat (a, "ls"))
137 cmd = STAT_CLIENT_CMD_LS;
139 else if (unformat (a, "dump"))
141 cmd = STAT_CLIENT_CMD_DUMP;
143 else if (unformat (a, "poll"))
145 cmd = STAT_CLIENT_CMD_POLL;
147 else if (unformat (a, "tightpoll"))
149 cmd = STAT_CLIENT_CMD_TIGHTPOLL;
151 else if (unformat (a, "%s", &pattern))
153 vec_add1 (patterns, pattern);
158 "%s: usage [socket-name <name>] [ls|dump|poll] <patterns> ...\n",
164 rv = stat_segment_connect ((char *) stat_segment_name);
167 fformat (stderr, "Couldn't connect to vpp, does %s exist?\n",
174 stat_segment_data_t *res;
176 dir = stat_segment_ls (patterns);
180 case STAT_CLIENT_CMD_LS:
181 /* List all counters */
182 for (i = 0; i < vec_len (dir); i++)
184 char *n = stat_segment_index_to_name (dir[i]);
190 case STAT_CLIENT_CMD_DUMP:
191 res = stat_segment_dump (dir);
192 for (i = 0; i < vec_len (res); i++)
196 case STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE:
197 if (res[i].simple_counter_vec == 0)
199 for (k = 0; k < vec_len (res[i].simple_counter_vec); k++)
200 for (j = 0; j < vec_len (res[i].simple_counter_vec[k]); j++)
201 fformat (stdout, "[%d @ %d]: %llu packets %s\n",
202 j, k, res[i].simple_counter_vec[k][j],
206 case STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED:
207 if (res[i].combined_counter_vec == 0)
209 for (k = 0; k < vec_len (res[i].combined_counter_vec); k++)
210 for (j = 0; j < vec_len (res[i].combined_counter_vec[k]); j++)
211 fformat (stdout, "[%d @ %d]: %llu packets, %llu bytes %s\n",
212 j, k, res[i].combined_counter_vec[k][j].packets,
213 res[i].combined_counter_vec[k][j].bytes,
217 case STAT_DIR_TYPE_ERROR_INDEX:
218 for (j = 0; j < vec_len (res[i].error_vector); j++)
219 fformat (stdout, "[@%d] %llu %s\n", j, res[i].error_vector[j],
223 case STAT_DIR_TYPE_SCALAR_INDEX:
224 fformat (stdout, "%.2f %s\n", res[i].scalar_value, res[i].name);
227 case STAT_DIR_TYPE_NAME_VECTOR:
228 if (res[i].name_vector == 0)
230 for (k = 0; k < vec_len (res[i].name_vector); k++)
231 if (res[i].name_vector[k])
232 fformat (stdout, "[%d]: %s %s\n", k, res[i].name_vector[k],
240 stat_segment_data_free (res);
243 case STAT_CLIENT_CMD_POLL:
244 stat_poll_loop (patterns);
245 /* We can only exist the pool loop if we lost connection to VPP */
246 stat_segment_disconnect ();
250 case STAT_CLIENT_CMD_TIGHTPOLL:
253 res = stat_segment_dump (dir);
258 dir = stat_segment_ls (patterns);
261 stat_segment_data_free (res);
267 "%s: usage [socket-name <name>] [ls|dump|poll] <patterns> ...\n",
271 stat_segment_disconnect ();
277 * fd.io coding-style-patch-verification: ON
280 * eval: (c-set-style "gnu")