stats: check if stats vector entry is empty
[vpp.git] / src / vpp / app / vpp_get_stats.c
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
20 #include <vpp-api/client/stat_client.h>
21 #include <vlib/vlib.h>
22
23 static int
24 stat_poll_loop (u8 ** patterns)
25 {
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);
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             {
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);
71               break;
72
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,
79                            res[i].name);
80               break;
81
82             case STAT_DIR_TYPE_SCALAR_INDEX:
83               fformat (stdout, "%.2f %s\n", res[i].scalar_value, res[i].name);
84               break;
85
86             case STAT_DIR_TYPE_EMPTY:
87               break;
88
89             default:
90               printf ("Unknown value\n");
91               ;
92             }
93         }
94       stat_segment_data_free (res);
95       /* Scrape stats every 5 seconds */
96       ts.tv_sec = 1;
97       ts.tv_nsec = 0;
98       while (nanosleep (&ts, &tsrem) < 0)
99         ts = tsrem;
100
101     }
102 }
103
104 enum stat_client_cmd_e
105 {
106   STAT_CLIENT_CMD_UNKNOWN,
107   STAT_CLIENT_CMD_LS,
108   STAT_CLIENT_CMD_POLL,
109   STAT_CLIENT_CMD_DUMP,
110   STAT_CLIENT_CMD_TIGHTPOLL,
111 };
112
113 int
114 main (int argc, char **argv)
115 {
116   unformat_input_t _argv, *a = &_argv;
117   u8 *stat_segment_name, *pattern = 0, **patterns = 0;
118   int rv;
119   enum stat_client_cmd_e cmd = STAT_CLIENT_CMD_UNKNOWN;
120
121   /* Create a heap of 64MB */
122   clib_mem_init (0, 64 << 20);
123
124   unformat_init_command_line (a, argv);
125
126   stat_segment_name = (u8 *) STAT_SEGMENT_SOCKET_FILE;
127
128   while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)
129     {
130       if (unformat (a, "socket-name %s", &stat_segment_name))
131         ;
132       else if (unformat (a, "ls"))
133         {
134           cmd = STAT_CLIENT_CMD_LS;
135         }
136       else if (unformat (a, "dump"))
137         {
138           cmd = STAT_CLIENT_CMD_DUMP;
139         }
140       else if (unformat (a, "poll"))
141         {
142           cmd = STAT_CLIENT_CMD_POLL;
143         }
144       else if (unformat (a, "tightpoll"))
145         {
146           cmd = STAT_CLIENT_CMD_TIGHTPOLL;
147         }
148       else if (unformat (a, "%s", &pattern))
149         {
150           vec_add1 (patterns, pattern);
151         }
152       else
153         {
154           fformat (stderr,
155                    "%s: usage [socket-name <name>] [ls|dump|poll] <patterns> ...\n",
156                    argv[0]);
157           exit (1);
158         }
159     }
160 reconnect:
161   rv = stat_segment_connect ((char *) stat_segment_name);
162   if (rv)
163     {
164       fformat (stderr, "Couldn't connect to vpp, does %s exist?\n",
165                stat_segment_name);
166       exit (1);
167     }
168
169   u32 *dir;
170   int i, j, k;
171   stat_segment_data_t *res;
172
173   dir = stat_segment_ls (patterns);
174
175   switch (cmd)
176     {
177     case STAT_CLIENT_CMD_LS:
178       /* List all counters */
179       for (i = 0; i < vec_len (dir); i++)
180         {
181           char *n = stat_segment_index_to_name (dir[i]);
182           if (!n)
183             continue;
184           printf ("%s\n", n);
185           free (n);
186         }
187       break;
188
189     case STAT_CLIENT_CMD_DUMP:
190       res = stat_segment_dump (dir);
191       for (i = 0; i < vec_len (res); i++)
192         {
193           switch (res[i].type)
194             {
195             case STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE:
196               if (res[i].simple_counter_vec == 0)
197                 continue;
198               for (k = 0; k < vec_len (res[i].simple_counter_vec); k++)
199                 for (j = 0; j < vec_len (res[i].simple_counter_vec[k]); j++)
200                   fformat (stdout, "[%d @ %d]: %llu packets %s\n",
201                            j, k, res[i].simple_counter_vec[k][j],
202                            res[i].name);
203               break;
204
205             case STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED:
206               if (res[i].combined_counter_vec == 0)
207                 continue;
208               for (k = 0; k < vec_len (res[i].combined_counter_vec); k++)
209                 for (j = 0; j < vec_len (res[i].combined_counter_vec[k]); j++)
210                   fformat (stdout, "[%d @ %d]: %llu packets, %llu bytes %s\n",
211                            j, k, res[i].combined_counter_vec[k][j].packets,
212                            res[i].combined_counter_vec[k][j].bytes,
213                            res[i].name);
214               break;
215
216             case STAT_DIR_TYPE_SCALAR_INDEX:
217               fformat (stdout, "%.2f %s\n", res[i].scalar_value, res[i].name);
218               break;
219
220             case STAT_DIR_TYPE_NAME_VECTOR:
221               if (res[i].name_vector == 0)
222                 continue;
223               for (k = 0; k < vec_len (res[i].name_vector); k++)
224                 if (res[i].name_vector[k])
225                   fformat (stdout, "[%d]: %s %s\n", k, res[i].name_vector[k],
226                            res[i].name);
227               break;
228
229             case STAT_DIR_TYPE_EMPTY:
230               break;
231
232             default:
233               ;
234             }
235         }
236       stat_segment_data_free (res);
237       break;
238
239     case STAT_CLIENT_CMD_POLL:
240       stat_poll_loop (patterns);
241       /* We can only exist the pool loop if we lost connection to VPP */
242       stat_segment_disconnect ();
243       goto reconnect;
244       break;
245
246     case STAT_CLIENT_CMD_TIGHTPOLL:
247       while (1)
248         {
249           res = stat_segment_dump (dir);
250           if (res == 0)
251             {
252               /* Refresh */
253               vec_free (dir);
254               dir = stat_segment_ls (patterns);
255               continue;
256             }
257           stat_segment_data_free (res);
258         }
259       break;
260
261     default:
262       fformat (stderr,
263                "%s: usage [socket-name <name>] [ls|dump|poll] <patterns> ...\n",
264                argv[0]);
265     }
266
267   stat_segment_disconnect ();
268
269   exit (0);
270 }
271
272 /*
273  * fd.io coding-style-patch-verification: ON
274  *
275  * Local Variables:
276  * eval: (c-set-style "gnu")
277  * End:
278  */