86e511e56f2118f20cc38b3e0aa3ab0040236d40
[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/stats/stats.h>
23
24 static int
25 stat_poll_loop (stat_segment_cached_pointer_t * cp)
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
32   printf ("\033[2J");           /*  clear the screen  */
33   while (1)
34     {
35       heartbeat = stat_segment_heartbeat ();
36       if (heartbeat > prev_heartbeat)
37         {
38           prev_heartbeat = heartbeat;
39           lost_connection = 0;
40         }
41       else
42         {
43           lost_connection++;
44         }
45       if (lost_connection > 10)
46         {
47           fformat (stderr, "Lost connection to VPP...\n");
48           return -1;
49         }
50
51       printf ("\033[H");        /* Cursor top left corner */
52       res = stat_segment_collect (cp);
53       for (i = 0; i < vec_len (res); i++)
54         {
55           switch (res[i].type)
56             {
57             case STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE:
58               for (k = 0; k < vec_len (res[i].simple_counter_vec); k++)
59                 for (j = 0; j < vec_len (res[i].simple_counter_vec[k]); j++)
60                   fformat (stdout, "[%d]: %lld packets %s\n",
61                            j, res[i].simple_counter_vec[k][j], res[i].name);
62               break;
63
64             case STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED:
65               for (k = 0; k < vec_len (res[i].simple_counter_vec); k++)
66                 for (j = 0; j < vec_len (res[i].combined_counter_vec[k]); j++)
67                   fformat (stdout, "[%d]: %lld packets, %lld bytes %s\n",
68                            j, res[i].combined_counter_vec[k][j].packets,
69                            res[i].combined_counter_vec[k][j].bytes,
70                            res[i].name);
71               break;
72
73             case STAT_DIR_TYPE_ERROR_INDEX:
74               fformat (stdout, "%lld %s\n", res[i].error_value, res[i].name);
75               break;
76
77             case STAT_DIR_TYPE_SCALAR_POINTER:
78               fformat (stdout, "%.2f %s\n", res[i].scalar_value, res[i].name);
79               break;
80
81             default:
82               printf ("Unknown value\n");
83               ;
84             }
85         }
86       stat_segment_data_free (res);
87       /* Scrape stats every 5 seconds */
88       ts.tv_sec = 1;
89       ts.tv_nsec = 0;
90       while (nanosleep (&ts, &tsrem) < 0)
91         ts = tsrem;
92
93     }
94 }
95
96 enum stat_client_cmd_e
97 {
98   STAT_CLIENT_CMD_UNKNOWN,
99   STAT_CLIENT_CMD_LS,
100   STAT_CLIENT_CMD_POLL,
101   STAT_CLIENT_CMD_DUMP,
102 };
103
104 int
105 main (int argc, char **argv)
106 {
107   unformat_input_t _argv, *a = &_argv;
108   u8 *stat_segment_name, *pattern = 0, **patterns = 0;
109   int rv;
110   enum stat_client_cmd_e cmd = STAT_CLIENT_CMD_UNKNOWN;
111
112   clib_mem_init (0, 128 << 20);
113
114   unformat_init_command_line (a, argv);
115
116   stat_segment_name = (u8 *) STAT_SEGMENT_SOCKET_FILE;
117
118   while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)
119     {
120       if (unformat (a, "socket-name %s", &stat_segment_name))
121         ;
122       else if (unformat (a, "ls"))
123         {
124           cmd = STAT_CLIENT_CMD_LS;
125         }
126       else if (unformat (a, "dump"))
127         {
128           cmd = STAT_CLIENT_CMD_DUMP;
129         }
130       else if (unformat (a, "poll"))
131         {
132           cmd = STAT_CLIENT_CMD_POLL;
133         }
134       else if (unformat (a, "%s", &pattern))
135         {
136           vec_add1 (patterns, pattern);
137         }
138       else
139         {
140           fformat (stderr,
141                    "%s: usage [socket-name <name>] [ls|dump|poll] <patterns> ...\n",
142                    argv[0]);
143           exit (1);
144         }
145     }
146 reconnect:
147   rv = stat_segment_connect ((char *) stat_segment_name);
148   if (rv)
149     {
150       fformat (stderr, "Couldn't connect to vpp, does %s exist?\n",
151                stat_segment_name);
152       exit (1);
153     }
154
155   u8 **dir;
156   int i, j, k;
157   stat_segment_data_t *res;
158   stat_segment_cached_pointer_t *cp;
159
160   dir = stat_segment_ls (patterns);
161
162   switch (cmd)
163     {
164     case STAT_CLIENT_CMD_LS:
165       /* List all counters */
166       for (i = 0; i < vec_len (dir); i++)
167         {
168           printf ("%s\n", (char *) dir[i]);
169         }
170       break;
171
172     case STAT_CLIENT_CMD_DUMP:
173       res = stat_segment_dump (dir);
174       for (i = 0; i < vec_len (res); i++)
175         {
176           switch (res[i].type)
177             {
178             case STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE:
179               for (k = 0; k < vec_len (res[i].simple_counter_vec) - 1; k++)
180                 for (j = 0; j < vec_len (res[i].simple_counter_vec[k]); j++)
181                   fformat (stdout, "[%d @ %d]: %lld packets %s\n",
182                            j, k, res[i].simple_counter_vec[k][j],
183                            res[i].name);
184               break;
185
186             case STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED:
187               for (k = 0; k < vec_len (res[i].simple_counter_vec); k++)
188                 for (j = 0; j < vec_len (res[i].combined_counter_vec[k]); j++)
189                   fformat (stdout, "[%d @ %d]: %lld packets, %lld bytes %s\n",
190                            j, k, res[i].combined_counter_vec[k][j].packets,
191                            res[i].combined_counter_vec[k][j].bytes,
192                            res[i].name);
193               break;
194
195             case STAT_DIR_TYPE_ERROR_INDEX:
196               fformat (stdout, "%lld %s\n", res[i].error_value, dir[i]);
197               break;
198
199             case STAT_DIR_TYPE_SCALAR_POINTER:
200               fformat (stdout, "%.2f %s\n", dir[i], res[i].scalar_value,
201                        res[i].name);
202               break;
203
204             default:
205               ;
206             }
207         }
208       stat_segment_data_free (res);
209       break;
210
211     case STAT_CLIENT_CMD_POLL:
212       cp = stat_segment_register (dir);
213       if (!cp)
214         {
215           fformat (stderr,
216                    "Couldn't register required counters with stat segment\n");
217           exit (1);
218         }
219       stat_poll_loop (cp);
220       /* We can only exist the pool loop if we lost connection to VPP */
221       stat_segment_disconnect ();
222       goto reconnect;
223       break;
224
225     default:
226       fformat (stderr,
227                "%s: usage [socket-name <name>] [ls|dump|poll] <patterns> ...\n",
228                argv[0]);
229     }
230
231   stat_segment_disconnect ();
232
233   exit (0);
234 }
235
236 /*
237  * fd.io coding-style-patch-verification: ON
238  *
239  * Local Variables:
240  * eval: (c-set-style "gnu")
241  * End:
242  */