vlib: improvement to automatic core pinning
[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 #include <vpp/vnet/config.h>
23
24 static int
25 stat_poll_loop (u8 ** patterns)
26 {
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);
32   if (!stats)
33     {
34       return -1;
35     }
36
37   printf ("\033[2J");           /*  clear the screen  */
38   while (1)
39     {
40       heartbeat = stat_segment_heartbeat ();
41       if (heartbeat > prev_heartbeat)
42         {
43           prev_heartbeat = heartbeat;
44           lost_connection = 0;
45         }
46       else
47         {
48           lost_connection++;
49         }
50       if (lost_connection > 10)
51         {
52           fformat (stderr, "Lost connection to VPP...\n");
53           return -1;
54         }
55
56       printf ("\033[H");        /* Cursor top left corner */
57       res = stat_segment_dump (stats);
58       if (!res)
59         {
60           stats = stat_segment_ls (patterns);
61           continue;
62         }
63       for (i = 0; i < vec_len (res); i++)
64         {
65           switch (res[i].type)
66             {
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);
72               break;
73
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,
80                            res[i].name);
81               break;
82
83             case STAT_DIR_TYPE_SCALAR_INDEX:
84               fformat (stdout, "%.2f %s\n", res[i].scalar_value, res[i].name);
85               break;
86
87             case STAT_DIR_TYPE_EMPTY:
88               break;
89
90             default:
91               printf ("Unknown value\n");
92               ;
93             }
94         }
95       stat_segment_data_free (res);
96       /* Scrape stats every 5 seconds */
97       ts.tv_sec = 1;
98       ts.tv_nsec = 0;
99       while (nanosleep (&ts, &tsrem) < 0)
100         ts = tsrem;
101
102     }
103 }
104
105 enum stat_client_cmd_e
106 {
107   STAT_CLIENT_CMD_UNKNOWN,
108   STAT_CLIENT_CMD_LS,
109   STAT_CLIENT_CMD_POLL,
110   STAT_CLIENT_CMD_DUMP,
111   STAT_CLIENT_CMD_TIGHTPOLL,
112 };
113
114 #ifdef CLIB_SANITIZE_ADDR
115 /* default options for Address Sanitizer */
116 const char *
117 __asan_default_options (void)
118 {
119   return VPP_SANITIZE_ADDR_OPTIONS;
120 }
121 #endif /* CLIB_SANITIZE_ADDR */
122
123 int
124 main (int argc, char **argv)
125 {
126   unformat_input_t _argv, *a = &_argv;
127   u8 *stat_segment_name, *pattern = 0, **patterns = 0;
128   int rv;
129   enum stat_client_cmd_e cmd = STAT_CLIENT_CMD_UNKNOWN;
130
131   /* Create a heap of 64MB */
132   clib_mem_init (0, 64 << 20);
133
134   unformat_init_command_line (a, argv);
135
136   stat_segment_name = (u8 *) STAT_SEGMENT_SOCKET_FILE;
137
138   while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)
139     {
140       if (unformat (a, "socket-name %s", &stat_segment_name))
141         ;
142       else if (unformat (a, "ls"))
143         {
144           cmd = STAT_CLIENT_CMD_LS;
145         }
146       else if (unformat (a, "dump"))
147         {
148           cmd = STAT_CLIENT_CMD_DUMP;
149         }
150       else if (unformat (a, "poll"))
151         {
152           cmd = STAT_CLIENT_CMD_POLL;
153         }
154       else if (unformat (a, "tightpoll"))
155         {
156           cmd = STAT_CLIENT_CMD_TIGHTPOLL;
157         }
158       else if (unformat (a, "%s", &pattern))
159         {
160           vec_add1 (patterns, pattern);
161         }
162       else
163         {
164           fformat (stderr,
165                    "%s: usage [socket-name <name>] [ls|dump|poll] <patterns> ...\n",
166                    argv[0]);
167           exit (1);
168         }
169     }
170 reconnect:
171   rv = stat_segment_connect ((char *) stat_segment_name);
172   if (rv)
173     {
174       fformat (stderr, "Couldn't connect to vpp, does %s exist?\n",
175                stat_segment_name);
176       exit (1);
177     }
178
179   u32 *dir;
180   int i, j, k;
181   stat_segment_data_t *res;
182
183   dir = stat_segment_ls (patterns);
184
185   switch (cmd)
186     {
187     case STAT_CLIENT_CMD_LS:
188       /* List all counters */
189       for (i = 0; i < vec_len (dir); i++)
190         {
191           char *n = stat_segment_index_to_name (dir[i]);
192           if (!n)
193             continue;
194           printf ("%s\n", n);
195           free (n);
196         }
197       break;
198
199     case STAT_CLIENT_CMD_DUMP:
200       res = stat_segment_dump (dir);
201       for (i = 0; i < vec_len (res); i++)
202         {
203           switch (res[i].type)
204             {
205             case STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE:
206               if (res[i].simple_counter_vec == 0)
207                 continue;
208               for (k = 0; k < vec_len (res[i].simple_counter_vec); k++)
209                 for (j = 0; j < vec_len (res[i].simple_counter_vec[k]); j++)
210                   fformat (stdout, "[%d @ %d]: %llu packets %s\n",
211                            j, k, res[i].simple_counter_vec[k][j],
212                            res[i].name);
213               break;
214
215             case STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED:
216               if (res[i].combined_counter_vec == 0)
217                 continue;
218               for (k = 0; k < vec_len (res[i].combined_counter_vec); k++)
219                 for (j = 0; j < vec_len (res[i].combined_counter_vec[k]); j++)
220                   fformat (stdout, "[%d @ %d]: %llu packets, %llu bytes %s\n",
221                            j, k, res[i].combined_counter_vec[k][j].packets,
222                            res[i].combined_counter_vec[k][j].bytes,
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  */