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