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