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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <sys/types.h>
21 #include <netinet/in.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>
41 #include <vlib/vlib.h>
42 #include <vlib/unix/unix.h>
43 #include <vnet/api_errno.h>
49 svmdb_client_t *svmdb_client;
52 f64 *sig_error_rate_ptr;
55 u64 current_sig_errors;
56 uword *sig_error_bitmap;
57 vlib_main_t *vlib_main;
58 vlib_main_t **my_vlib_mains;
64 vnet_get_aggregate_rx_packets (void)
69 #include <vlib/vlib.h>
70 #include <vnet/vnet.h>
71 #include <vnet/devices/dpdk/dpdk.h>
74 gmon_main_t gmon_main;
77 get_significant_errors (gmon_main_t * gm)
79 vlib_main_t *this_vlib_main;
80 vlib_error_main_t *em;
83 u64 significant_errors = 0;
86 clib_bitmap_foreach (code, gm->sig_error_bitmap,
88 for (vm_index = 0; vm_index < vec_len (gm->my_vlib_mains); vm_index++)
90 this_vlib_main = gm->my_vlib_mains[vm_index];
91 em = &this_vlib_main->error_main;
92 significant_errors += em->counters[code] -
93 ((vec_len(em->counters_last_clear) > code) ?
94 em->counters_last_clear[code] : 0);
99 return (significant_errors);
102 static clib_error_t *
103 publish_pid (vlib_main_t * vm)
105 gmon_main_t *gm = &gmon_main;
107 *gm->vpef_pid_ptr = getpid ();
112 VLIB_API_INIT_FUNCTION (publish_pid);
116 gmon_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
119 u64 input_packets, last_input_packets, new_sig_errors;
120 f64 last_runtime, dt, now;
121 gmon_main_t *gm = &gmon_main;
125 last_input_packets = 0;
128 last_input_packets = 0;
130 /* Initial wait for the world to settle down */
131 vlib_process_suspend (vm, 5.0);
133 if (vec_len (vlib_mains) == 0)
134 vec_add1 (gm->my_vlib_mains, &vlib_global_main);
137 for (i = 0; i < vec_len (vlib_mains); i++)
138 vec_add1 (gm->my_vlib_mains, vlib_mains[i]);
143 vlib_process_suspend (vm, 5.0);
144 vector_rate = vlib_last_vector_length_per_node (vm);
145 *gm->vector_rate_ptr = vector_rate;
146 now = vlib_time_now (vm);
147 dt = now - last_runtime;
148 input_packets = vnet_get_aggregate_rx_packets ();
149 *gm->input_rate_ptr = (f64) (input_packets - last_input_packets) / dt;
151 last_input_packets = input_packets;
153 new_sig_errors = get_significant_errors (gm);
154 *gm->sig_error_rate_ptr =
155 ((f64) (new_sig_errors - gm->last_sig_errors)) / dt;
156 gm->last_sig_errors = new_sig_errors;
159 return 0; /* not so much */
163 VLIB_REGISTER_NODE (gmon_process_node,static) = {
164 .function = gmon_process,
165 .type = VLIB_NODE_TYPE_PROCESS,
166 .name = "gmon-process",
170 static clib_error_t *
171 gmon_init (vlib_main_t * vm)
173 gmon_main_t *gm = &gmon_main;
174 api_main_t *am = &api_main;
178 svmdb_map_args_t _ma, *ma = &_ma;
180 if ((error = vlib_call_init_function (vm, vpe_api_init)))
183 if ((error = vlib_call_init_function (vm, vlibmemory_init)))
188 memset (ma, 0, sizeof (*ma));
189 ma->root_path = am->root_path;
190 ma->uid = am->api_uid;
191 ma->gid = am->api_gid;
193 gm->svmdb_client = svmdb_map (ma);
195 /* Find or create, set to zero */
197 svmdb_local_set_vec_variable (gm->svmdb_client,
198 "vpp_vector_rate", (char *) v, sizeof (*v));
201 svmdb_local_set_vec_variable (gm->svmdb_client,
202 "vpp_input_rate", (char *) v, sizeof (*v));
205 svmdb_local_set_vec_variable (gm->svmdb_client,
206 "vpp_sig_error_rate",
207 (char *) v, sizeof (*v));
211 svmdb_local_set_vec_variable (gm->svmdb_client,
212 "vpp_pid", (char *) swp, sizeof (*swp));
215 /* the value cells will never move, so acquire references to them */
216 gm->vector_rate_ptr =
217 svmdb_local_get_variable_reference (gm->svmdb_client,
221 svmdb_local_get_variable_reference (gm->svmdb_client,
224 gm->sig_error_rate_ptr =
225 svmdb_local_get_variable_reference (gm->svmdb_client,
227 "vpp_sig_error_rate");
229 svmdb_local_get_variable_reference (gm->svmdb_client,
230 SVMDB_NAMESPACE_VEC, "vpp_pid");
234 VLIB_INIT_FUNCTION (gmon_init);
236 static clib_error_t *
237 gmon_exit (vlib_main_t * vm)
239 gmon_main_t *gm = &gmon_main;
241 if (gm->vector_rate_ptr)
243 *gm->vector_rate_ptr = 0.0;
244 *gm->vpef_pid_ptr = 0;
245 *gm->input_rate_ptr = 0.0;
246 *gm->sig_error_rate_ptr = 0.0;
247 svm_region_unmap ((void *) gm->svmdb_client->db_rp);
248 vec_free (gm->svmdb_client);
253 VLIB_MAIN_LOOP_EXIT_FUNCTION (gmon_exit);
256 significant_error_enable_disable (gmon_main_t * gm, u32 index, int enable)
258 vlib_main_t *vm = gm->vlib_main;
259 vlib_error_main_t *em = &vm->error_main;
261 if (index >= vec_len (em->counters))
262 return VNET_API_ERROR_NO_SUCH_ENTRY;
264 gm->sig_error_bitmap =
265 clib_bitmap_set (gm->sig_error_bitmap, index, enable);
269 static clib_error_t *
270 set_significant_error_command_fn (vlib_main_t * vm,
271 unformat_input_t * input,
272 vlib_cli_command_t * cmd)
277 gmon_main_t *gm = &gmon_main;
279 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
281 if (unformat (input, "%d", &index))
283 else if (unformat (input, "disable"))
286 return clib_error_return (0, "unknown input `%U'",
287 format_unformat_error, input);
290 rv = significant_error_enable_disable (gm, index, enable);
298 return clib_error_return
299 (0, "significant_error_enable_disable returned %d", rv);
306 VLIB_CLI_COMMAND (set_significant_error_command, static) = {
307 .path = "set significant error",
308 .short_help = "set significant error <counter-index-nnn> [disable]",
309 .function = set_significant_error_command_fn,
314 * fd.io coding-style-patch-verification: ON
317 * eval: (c-set-style "gnu")