misc: refactor clib_bitmap_foreach macro
[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/heap.h>
35 #include <vppinfra/pool.h>
36 #include <vppinfra/format.h>
37 #include <vlibapi/api.h>
38 #include <vlibmemory/api.h>
39
40 #include <vlib/vlib.h>
41 #include <vlib/unix/unix.h>
42 #include <vnet/api_errno.h>
43
44 #include <svm/svmdb.h>
45
46 typedef struct
47 {
48   svmdb_client_t *svmdb_client;
49   f64 *vector_rate_ptr;
50   f64 *input_rate_ptr;
51   f64 *sig_error_rate_ptr;
52   pid_t *vpef_pid_ptr;
53   u64 last_sig_errors;
54   u64 current_sig_errors;
55   uword *sig_error_bitmap;
56   vlib_main_t *vlib_main;
57   vlib_main_t **my_vlib_mains;
58
59 } gmon_main_t;
60
61 #include <vlib/vlib.h>
62 #include <vnet/vnet.h>
63 #include <vnet/devices/devices.h>
64
65 gmon_main_t gmon_main;
66
67 static u64
68 get_significant_errors (gmon_main_t * gm)
69 {
70   vlib_main_t *this_vlib_main;
71   vlib_error_main_t *em;
72   uword code;
73   int vm_index;
74   u64 significant_errors = 0;
75
76   /* *INDENT-OFF* */
77   clib_bitmap_foreach (code, gm->sig_error_bitmap)
78    {
79     for (vm_index = 0; vm_index < vec_len (gm->my_vlib_mains); vm_index++)
80       {
81         this_vlib_main = gm->my_vlib_mains[vm_index];
82         em = &this_vlib_main->error_main;
83         significant_errors += em->counters[code] -
84           ((vec_len(em->counters_last_clear) > code) ?
85            em->counters_last_clear[code] : 0);
86       }
87   }
88   /* *INDENT-ON* */
89
90   return (significant_errors);
91 }
92
93 static clib_error_t *
94 publish_pid (vlib_main_t * vm)
95 {
96   gmon_main_t *gm = &gmon_main;
97
98   *gm->vpef_pid_ptr = getpid ();
99
100   return 0;
101 }
102
103 VLIB_API_INIT_FUNCTION (publish_pid);
104
105
106 static uword
107 gmon_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
108 {
109   f64 vector_rate;
110   u64 input_packets, last_input_packets, new_sig_errors;
111   f64 last_runtime, dt, now;
112   gmon_main_t *gm = &gmon_main;
113   int i;
114
115   last_runtime = 0.0;
116   last_input_packets = 0;
117
118   last_runtime = 0.0;
119   last_input_packets = 0;
120
121   /* Initial wait for the world to settle down */
122   vlib_process_suspend (vm, 5.0);
123
124   for (i = 0; i < vec_len (vlib_mains); i++)
125     vec_add1 (gm->my_vlib_mains, vlib_mains[i]);
126
127   while (1)
128     {
129       vlib_process_suspend (vm, 5.0);
130       vector_rate = vlib_last_vector_length_per_node (vm);
131       *gm->vector_rate_ptr = vector_rate;
132       now = vlib_time_now (vm);
133       dt = now - last_runtime;
134       input_packets = vnet_get_aggregate_rx_packets ();
135       *gm->input_rate_ptr = (f64) (input_packets - last_input_packets) / dt;
136       last_runtime = now;
137       last_input_packets = input_packets;
138
139       new_sig_errors = get_significant_errors (gm);
140       *gm->sig_error_rate_ptr =
141         ((f64) (new_sig_errors - gm->last_sig_errors)) / dt;
142       gm->last_sig_errors = new_sig_errors;
143     }
144
145   return 0;                     /* not so much */
146 }
147
148 /* *INDENT-OFF* */
149 VLIB_REGISTER_NODE (gmon_process_node,static) = {
150   .function = gmon_process,
151   .type = VLIB_NODE_TYPE_PROCESS,
152   .name = "gmon-process",
153 };
154 /* *INDENT-ON* */
155
156 static clib_error_t *
157 gmon_init (vlib_main_t * vm)
158 {
159   gmon_main_t *gm = &gmon_main;
160   api_main_t *am = vlibapi_get_main ();
161   pid_t *swp = 0;
162   f64 *v = 0;
163   clib_error_t *error;
164   svmdb_map_args_t _ma, *ma = &_ma;
165
166   if ((error = vlib_call_init_function (vm, vpe_api_init)))
167     return (error);
168
169   if ((error = vlib_call_init_function (vm, vlibmemory_init)))
170     return (error);
171
172   gm->vlib_main = vm;
173
174   clib_memset (ma, 0, sizeof (*ma));
175   ma->root_path = am->root_path;
176   ma->uid = am->api_uid;
177   ma->gid = am->api_gid;
178
179   gm->svmdb_client = svmdb_map (ma);
180
181   /* Find or create, set to zero */
182   vec_add1 (v, 0.0);
183   svmdb_local_set_vec_variable (gm->svmdb_client,
184                                 "vpp_vector_rate", (char *) v, sizeof (*v));
185   vec_free (v);
186   vec_add1 (v, 0.0);
187   svmdb_local_set_vec_variable (gm->svmdb_client,
188                                 "vpp_input_rate", (char *) v, sizeof (*v));
189   vec_free (v);
190   vec_add1 (v, 0.0);
191   svmdb_local_set_vec_variable (gm->svmdb_client,
192                                 "vpp_sig_error_rate",
193                                 (char *) v, sizeof (*v));
194   vec_free (v);
195
196   vec_add1 (swp, 0.0);
197   svmdb_local_set_vec_variable (gm->svmdb_client,
198                                 "vpp_pid", (char *) swp, sizeof (*swp));
199   vec_free (swp);
200
201   /* the value cells will never move, so acquire references to them */
202   gm->vector_rate_ptr =
203     svmdb_local_get_variable_reference (gm->svmdb_client,
204                                         SVMDB_NAMESPACE_VEC,
205                                         "vpp_vector_rate");
206   gm->input_rate_ptr =
207     svmdb_local_get_variable_reference (gm->svmdb_client,
208                                         SVMDB_NAMESPACE_VEC,
209                                         "vpp_input_rate");
210   gm->sig_error_rate_ptr =
211     svmdb_local_get_variable_reference (gm->svmdb_client,
212                                         SVMDB_NAMESPACE_VEC,
213                                         "vpp_sig_error_rate");
214   gm->vpef_pid_ptr =
215     svmdb_local_get_variable_reference (gm->svmdb_client,
216                                         SVMDB_NAMESPACE_VEC, "vpp_pid");
217   return 0;
218 }
219
220 VLIB_INIT_FUNCTION (gmon_init);
221
222 static clib_error_t *
223 gmon_exit (vlib_main_t * vm)
224 {
225   gmon_main_t *gm = &gmon_main;
226
227   if (gm->vector_rate_ptr)
228     {
229       *gm->vector_rate_ptr = 0.0;
230       *gm->vpef_pid_ptr = 0;
231       *gm->input_rate_ptr = 0.0;
232       *gm->sig_error_rate_ptr = 0.0;
233       svm_region_unmap ((void *) gm->svmdb_client->db_rp);
234       vec_free (gm->svmdb_client);
235     }
236   return 0;
237 }
238
239 VLIB_MAIN_LOOP_EXIT_FUNCTION (gmon_exit);
240
241 static int
242 significant_error_enable_disable (gmon_main_t * gm, u32 index, int enable)
243 {
244   vlib_main_t *vm = gm->vlib_main;
245   vlib_error_main_t *em = &vm->error_main;
246
247   if (index >= vec_len (em->counters))
248     return VNET_API_ERROR_NO_SUCH_ENTRY;
249
250   gm->sig_error_bitmap =
251     clib_bitmap_set (gm->sig_error_bitmap, index, enable);
252   return 0;
253 }
254
255 static clib_error_t *
256 set_significant_error_command_fn (vlib_main_t * vm,
257                                   unformat_input_t * input,
258                                   vlib_cli_command_t * cmd)
259 {
260   u32 index;
261   int enable = 1;
262   int rv;
263   gmon_main_t *gm = &gmon_main;
264
265   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
266     {
267       if (unformat (input, "%d", &index))
268         ;
269       else if (unformat (input, "disable"))
270         enable = 0;
271       else
272         return clib_error_return (0, "unknown input `%U'",
273                                   format_unformat_error, input);
274     }
275
276   rv = significant_error_enable_disable (gm, index, enable);
277
278   switch (rv)
279     {
280     case 0:
281       break;
282
283     default:
284       return clib_error_return
285         (0, "significant_error_enable_disable returned %d", rv);
286     }
287
288   return 0;
289 }
290
291 /* *INDENT-OFF* */
292 VLIB_CLI_COMMAND (set_significant_error_command, static) = {
293   .path = "set significant error",
294   .short_help = "set significant error <counter-index-nnn> [disable]",
295   .function = set_significant_error_command_fn,
296 };
297 /* *INDENT-ON* */
298
299 /*
300  * fd.io coding-style-patch-verification: ON
301  *
302  * Local Variables:
303  * eval: (c-set-style "gnu")
304  * End:
305  */