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>
22 #include <vpp/stats/stats.h>
25 stat_poll_loop (u8 ** patterns)
27 struct timespec ts, tsrem;
28 stat_segment_data_t *res;
29 int i, j, k, lost_connection = 0;
30 f64 heartbeat, prev_heartbeat = 0;
31 u32 *stats = stat_segment_ls (patterns);
37 printf ("\033[2J"); /* clear the screen */
40 heartbeat = stat_segment_heartbeat ();
41 if (heartbeat > prev_heartbeat)
43 prev_heartbeat = heartbeat;
50 if (lost_connection > 10)
52 fformat (stderr, "Lost connection to VPP...\n");
56 printf ("\033[H"); /* Cursor top left corner */
57 res = stat_segment_dump (stats);
60 stats = stat_segment_ls (patterns);
63 for (i = 0; i < vec_len (res); i++)
67 case STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE:
68 for (k = 0; k < vec_len (res[i].simple_counter_vec); k++)
69 for (j = 0; j < vec_len (res[i].simple_counter_vec[k]); j++)
70 fformat (stdout, "[%d]: %llu packets %s\n",
71 j, res[i].simple_counter_vec[k][j], res[i].name);
74 case STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED:
75 for (k = 0; k < vec_len (res[i].simple_counter_vec); k++)
76 for (j = 0; j < vec_len (res[i].combined_counter_vec[k]); j++)
77 fformat (stdout, "[%d]: %llu packets, %llu bytes %s\n",
78 j, res[i].combined_counter_vec[k][j].packets,
79 res[i].combined_counter_vec[k][j].bytes,
83 case STAT_DIR_TYPE_ERROR_INDEX:
84 fformat (stdout, "%llu %s\n", res[i].error_value, res[i].name);
87 case STAT_DIR_TYPE_SCALAR_INDEX:
88 fformat (stdout, "%.2f %s\n", res[i].scalar_value, res[i].name);
92 printf ("Unknown value\n");
96 stat_segment_data_free (res);
97 /* Scrape stats every 5 seconds */
100 while (nanosleep (&ts, &tsrem) < 0)
106 enum stat_client_cmd_e
108 STAT_CLIENT_CMD_UNKNOWN,
110 STAT_CLIENT_CMD_POLL,
111 STAT_CLIENT_CMD_DUMP,
112 STAT_CLIENT_CMD_TIGHTPOLL,
116 main (int argc, char **argv)
118 unformat_input_t _argv, *a = &_argv;
119 u8 *stat_segment_name, *pattern = 0, **patterns = 0;
121 enum stat_client_cmd_e cmd = STAT_CLIENT_CMD_UNKNOWN;
123 /* Create a heap of 64MB */
124 clib_mem_init (0, 64 << 20);
126 unformat_init_command_line (a, argv);
128 stat_segment_name = (u8 *) STAT_SEGMENT_SOCKET_FILE;
130 while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)
132 if (unformat (a, "socket-name %s", &stat_segment_name))
134 else if (unformat (a, "ls"))
136 cmd = STAT_CLIENT_CMD_LS;
138 else if (unformat (a, "dump"))
140 cmd = STAT_CLIENT_CMD_DUMP;
142 else if (unformat (a, "poll"))
144 cmd = STAT_CLIENT_CMD_POLL;
146 else if (unformat (a, "tightpoll"))
148 cmd = STAT_CLIENT_CMD_TIGHTPOLL;
150 else if (unformat (a, "%s", &pattern))
152 vec_add1 (patterns, pattern);
157 "%s: usage [socket-name <name>] [ls|dump|poll] <patterns> ...\n",
163 rv = stat_segment_connect ((char *) stat_segment_name);
166 fformat (stderr, "Couldn't connect to vpp, does %s exist?\n",
173 stat_segment_data_t *res;
175 dir = stat_segment_ls (patterns);
179 case STAT_CLIENT_CMD_LS:
180 /* List all counters */
181 for (i = 0; i < vec_len (dir); i++)
183 char *n = stat_segment_index_to_name (dir[i]);
189 case STAT_CLIENT_CMD_DUMP:
190 res = stat_segment_dump (dir);
191 for (i = 0; i < vec_len (res); i++)
195 case STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE:
196 for (k = 0; k < vec_len (res[i].simple_counter_vec) - 1; k++)
197 for (j = 0; j < vec_len (res[i].simple_counter_vec[k]); j++)
198 fformat (stdout, "[%d @ %d]: %llu packets %s\n",
199 j, k, res[i].simple_counter_vec[k][j],
203 case STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED:
204 for (k = 0; k < vec_len (res[i].combined_counter_vec); k++)
205 for (j = 0; j < vec_len (res[i].combined_counter_vec[k]); j++)
206 fformat (stdout, "[%d @ %d]: %llu packets, %llu bytes %s\n",
207 j, k, res[i].combined_counter_vec[k][j].packets,
208 res[i].combined_counter_vec[k][j].bytes,
212 case STAT_DIR_TYPE_ERROR_INDEX:
213 fformat (stdout, "%llu %s\n", res[i].error_value, res[i].name);
216 case STAT_DIR_TYPE_SCALAR_INDEX:
217 fformat (stdout, "%.2f %s\n", res[i].scalar_value, res[i].name);
224 stat_segment_data_free (res);
227 case STAT_CLIENT_CMD_POLL:
228 stat_poll_loop (patterns);
229 /* We can only exist the pool loop if we lost connection to VPP */
230 stat_segment_disconnect ();
234 case STAT_CLIENT_CMD_TIGHTPOLL:
237 res = stat_segment_dump (dir);
242 dir = stat_segment_ls (patterns);
245 stat_segment_data_free (res);
251 "%s: usage [socket-name <name>] [ls|dump|poll] <patterns> ...\n",
255 stat_segment_disconnect ();
261 * fd.io coding-style-patch-verification: ON
264 * eval: (c-set-style "gnu")