a37dfca57a8c854381d2660763eaf8606109e318
[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           printf ("%s\n", n);
183           free (n);
184         }
185       break;
186
187     case STAT_CLIENT_CMD_DUMP:
188       res = stat_segment_dump (dir);
189       for (i = 0; i < vec_len (res); i++)
190         {
191           switch (res[i].type)
192             {
193             case STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE:
194               if (res[i].simple_counter_vec == 0)
195                 continue;
196               for (k = 0; k < vec_len (res[i].simple_counter_vec); 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],
200                            res[i].name);
201               break;
202
203             case STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED:
204               if (res[i].combined_counter_vec == 0)
205                 continue;
206               for (k = 0; k < vec_len (res[i].combined_counter_vec); k++)
207                 for (j = 0; j < vec_len (res[i].combined_counter_vec[k]); j++)
208                   fformat (stdout, "[%d @ %d]: %llu packets, %llu bytes %s\n",
209                            j, k, res[i].combined_counter_vec[k][j].packets,
210                            res[i].combined_counter_vec[k][j].bytes,
211                            res[i].name);
212               break;
213
214             case STAT_DIR_TYPE_SCALAR_INDEX:
215               fformat (stdout, "%.2f %s\n", res[i].scalar_value, res[i].name);
216               break;
217
218             case STAT_DIR_TYPE_NAME_VECTOR:
219               if (res[i].name_vector == 0)
220                 continue;
221               for (k = 0; k < vec_len (res[i].name_vector); k++)
222                 if (res[i].name_vector[k])
223                   fformat (stdout, "[%d]: %s %s\n", k, res[i].name_vector[k],
224                            res[i].name);
225               break;
226
227             case STAT_DIR_TYPE_EMPTY:
228               break;
229
230             default:
231               ;
232             }
233         }
234       stat_segment_data_free (res);
235       break;
236
237     case STAT_CLIENT_CMD_POLL:
238       stat_poll_loop (patterns);
239       /* We can only exist the pool loop if we lost connection to VPP */
240       stat_segment_disconnect ();
241       goto reconnect;
242       break;
243
244     case STAT_CLIENT_CMD_TIGHTPOLL:
245       while (1)
246         {
247           res = stat_segment_dump (dir);
248           if (res == 0)
249             {
250               /* Refresh */
251               vec_free (dir);
252               dir = stat_segment_ls (patterns);
253               continue;
254             }
255           stat_segment_data_free (res);
256         }
257       break;
258
259     default:
260       fformat (stderr,
261                "%s: usage [socket-name <name>] [ls|dump|poll] <patterns> ...\n",
262                argv[0]);
263     }
264
265   stat_segment_disconnect ();
266
267   exit (0);
268 }
269
270 /*
271  * fd.io coding-style-patch-verification: ON
272  *
273  * Local Variables:
274  * eval: (c-set-style "gnu")
275  * End:
276  */