610f40eddbf0fcfa2c7b4e35f5cad1edc44cb35b
[vpp.git] / src / vpp / api / gmon.c
1 /*
2  * Copyright (c) 2012 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <sys/types.h>
19 #include <sys/mman.h>
20 #include <sys/stat.h>
21 #include <netinet/in.h>
22 #include <signal.h>
23 #include <pthread.h>
24 #include <unistd.h>
25 #include <time.h>
26 #include <fcntl.h>
27 #include <string.h>
28 #include <vppinfra/clib.h>
29 #include <vppinfra/vec.h>
30 #include <vppinfra/hash.h>
31 #include <vppinfra/bitmap.h>
32 #include <vppinfra/fifo.h>
33 #include <vppinfra/time.h>
34 #include <vppinfra/mheap.h>
35 #include <vppinfra/heap.h>
36 #include <vppinfra/pool.h>
37 #include <vppinfra/format.h>
38 #include <vlibapi/api.h>
39 #include <vlibmemory/api.h>
40
41 #include <vlib/vlib.h>
42 #include <vlib/unix/unix.h>
43 #include <vnet/api_errno.h>
44
45 #include <svm/svmdb.h>
46
47 typedef struct
48 {
49   svmdb_client_t *svmdb_client;
50   f64 *vector_rate_ptr;
51   f64 *input_rate_ptr;
52   f64 *sig_error_rate_ptr;
53   pid_t *vpef_pid_ptr;
54   u64 last_sig_errors;
55   u64 current_sig_errors;
56   uword *sig_error_bitmap;
57   vlib_main_t *vlib_main;
58   vlib_main_t **my_vlib_mains;
59
60 } gmon_main_t;
61
62 #include <vlib/vlib.h>
63 #include <vnet/vnet.h>
64 #include <vnet/devices/devices.h>
65
66 gmon_main_t gmon_main;
67
68 static u64
69 get_significant_errors (gmon_main_t * gm)
70 {
71   vlib_main_t *this_vlib_main;
72   vlib_error_main_t *em;
73   uword code;
74   int vm_index;
75   u64 significant_errors = 0;
76
77   /* *INDENT-OFF* */
78   clib_bitmap_foreach (code, gm->sig_error_bitmap,
79   ({
80     for (vm_index = 0; vm_index < vec_len (gm->my_vlib_mains); vm_index++)
81       {
82         this_vlib_main = gm->my_vlib_mains[vm_index];
83         em = &this_vlib_main->error_main;
84         significant_errors += em->counters[code] -
85           ((vec_len(em->counters_last_clear) > code) ?
86            em->counters_last_clear[code] : 0);
87       }
88   }));
89   /* *INDENT-ON* */
90
91   return (significant_errors);
92 }
93
94 static clib_error_t *
95 publish_pid (vlib_main_t * vm)
96 {
97   gmon_main_t *gm = &gmon_main;
98
99   *gm->vpef_pid_ptr = getpid ();
100
101   return 0;
102 }
103
104 VLIB_API_INIT_FUNCTION (publish_pid);
105
106
107 static uword
108 gmon_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
109 {
110   f64 vector_rate;
111   u64 input_packets, last_input_packets, new_sig_errors;
112   f64 last_runtime, dt, now;
113   gmon_main_t *gm = &gmon_main;
114   int i;
115
116   last_runtime = 0.0;
117   last_input_packets = 0;
118
119   last_runtime = 0.0;
120   last_input_packets = 0;
121
122   /* Initial wait for the world to settle down */
123   vlib_process_suspend (vm, 5.0);
124
125   if (vec_len (vlib_mains) == 0)
126     vec_add1 (gm->my_vlib_mains, &vlib_global_main);
127   else
128     {
129       for (i = 0; i < vec_len (vlib_mains); i++)
130         vec_add1 (gm->my_vlib_mains, vlib_mains[i]);
131     }
132
133   while (1)
134     {
135       vlib_process_suspend (vm, 5.0);
136       vector_rate = vlib_last_vector_length_per_node (vm);
137       *gm->vector_rate_ptr = vector_rate;
138       now = vlib_time_now (vm);
139       dt = now - last_runtime;
140       // TODO
141       //input_packets = vnet_get_aggregate_rx_packets ();
142       *gm->input_rate_ptr = (f64) (input_packets - last_input_packets) / dt;
143       last_runtime = now;
144       last_input_packets = input_packets;
145
146       new_sig_errors = get_significant_errors (gm);
147       *gm->sig_error_rate_ptr =
148         ((f64) (new_sig_errors - gm->last_sig_errors)) / dt;
149       gm->last_sig_errors = new_sig_errors;
150     }
151
152   return 0;                     /* not so much */
153 }
154
155 /* *INDENT-OFF* */
156 VLIB_REGISTER_NODE (gmon_process_node,static) = {
157   .function = gmon_process,
158   .type = VLIB_NODE_TYPE_PROCESS,
159   .name = "gmon-process",
160 };
161 /* *INDENT-ON* */
162
163 static clib_error_t *
164 gmon_init (vlib_main_t * vm)
165 {
166   gmon_main_t *gm = &gmon_main;
167   api_main_t *am = &api_main;
168   pid_t *swp = 0;
169   f64 *v = 0;
170   clib_error_t *error;
171   svmdb_map_args_t _ma, *ma = &_ma;
172
173   if ((error = vlib_call_init_function (vm, vpe_api_init)))
174     return (error);
175
176   if ((error = vlib_call_init_function (vm, vlibmemory_init)))
177     return (error);
178
179   gm->vlib_main = vm;
180
181   memset (ma, 0, sizeof (*ma));
182   ma->root_path = am->root_path;
183   ma->uid = am->api_uid;
184   ma->gid = am->api_gid;
185
186   gm->svmdb_client = svmdb_map (ma);
187
188   /* Find or create, set to zero */
189   vec_add1 (v, 0.0);
190   svmdb_local_set_vec_variable (gm->svmdb_client,
191                                 "vpp_vector_rate", (char *) v, sizeof (*v));
192   vec_free (v);
193   vec_add1 (v, 0.0);
194   svmdb_local_set_vec_variable (gm->svmdb_client,
195                                 "vpp_input_rate", (char *) v, sizeof (*v));
196   vec_free (v);
197   vec_add1 (v, 0.0);
198   svmdb_local_set_vec_variable (gm->svmdb_client,
199                                 "vpp_sig_error_rate",
200                                 (char *) v, sizeof (*v));
201   vec_free (v);
202
203   vec_add1 (swp, 0.0);
204   svmdb_local_set_vec_variable (gm->svmdb_client,
205                                 "vpp_pid", (char *) swp, sizeof (*swp));
206   vec_free (swp);
207
208   /* the value cells will never move, so acquire references to them */
209   gm->vector_rate_ptr =
210     svmdb_local_get_variable_reference (gm->svmdb_client,
211                                         SVMDB_NAMESPACE_VEC,
212                                         "vpp_vector_rate");
213   gm->input_rate_ptr =
214     svmdb_local_get_variable_reference (gm->svmdb_client,
215                                         SVMDB_NAMESPACE_VEC,
216                                         "vpp_input_rate");
217   gm->sig_error_rate_ptr =
218     svmdb_local_get_variable_reference (gm->svmdb_client,
219                                         SVMDB_NAMESPACE_VEC,
220                                         "vpp_sig_error_rate");
221   gm->vpef_pid_ptr =
222     svmdb_local_get_variable_reference (gm->svmdb_client,
223                                         SVMDB_NAMESPACE_VEC, "vpp_pid");
224   return 0;
225 }
226
227 VLIB_INIT_FUNCTION (gmon_init);
228
229 static clib_error_t *
230 gmon_exit (vlib_main_t * vm)
231 {
232   gmon_main_t *gm = &gmon_main;
233
234   if (gm->vector_rate_ptr)
235     {
236       *gm->vector_rate_ptr = 0.0;
237       *gm->vpef_pid_ptr = 0;
238       *gm->input_rate_ptr = 0.0;
239       *gm->sig_error_rate_ptr = 0.0;
240       svm_region_unmap ((void *) gm->svmdb_client->db_rp);
241       vec_free (gm->svmdb_client);
242     }
243   return 0;
244 }
245
246 VLIB_MAIN_LOOP_EXIT_FUNCTION (gmon_exit);
247
248 static int
249 significant_error_enable_disable (gmon_main_t * gm, u32 index, int enable)
250 {
251   vlib_main_t *vm = gm->vlib_main;
252   vlib_error_main_t *em = &vm->error_main;
253
254   if (index >= vec_len (em->counters))
255     return VNET_API_ERROR_NO_SUCH_ENTRY;
256
257   gm->sig_error_bitmap =
258     clib_bitmap_set (gm->sig_error_bitmap, index, enable);
259   return 0;
260 }
261
262 static clib_error_t *
263 set_significant_error_command_fn (vlib_main_t * vm,
264                                   unformat_input_t * input,
265                                   vlib_cli_command_t * cmd)
266 {
267   u32 index;
268   int enable = 1;
269   int rv;
270   gmon_main_t *gm = &gmon_main;
271
272   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
273     {
274       if (unformat (input, "%d", &index))
275         ;
276       else if (unformat (input, "disable"))
277         enable = 0;
278       else
279         return clib_error_return (0, "unknown input `%U'",
280                                   format_unformat_error, input);
281     }
282
283   rv = significant_error_enable_disable (gm, index, enable);
284
285   switch (rv)
286     {
287     case 0:
288       break;
289
290     default:
291       return clib_error_return
292         (0, "significant_error_enable_disable returned %d", rv);
293     }
294
295   return 0;
296 }
297
298 /* *INDENT-OFF* */
299 VLIB_CLI_COMMAND (set_significant_error_command, static) = {
300   .path = "set significant error",
301   .short_help = "set significant error <counter-index-nnn> [disable]",
302   .function = set_significant_error_command_fn,
303 };
304 /* *INDENT-ON* */
305
306 /*
307  * fd.io coding-style-patch-verification: ON
308  *
309  * Local Variables:
310  * eval: (c-set-style "gnu")
311  * End:
312  */