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/heap.h>
35 #include <vppinfra/pool.h>
36 #include <vppinfra/format.h>
37 #include <vlibapi/api.h>
38 #include <vlibmemory/api.h>
40 #include <vlib/vlib.h>
41 #include <vlib/unix/unix.h>
42 #include <vnet/api_errno.h>
44 #include <svm/svmdb.h>
48 svmdb_client_t *svmdb_client;
51 f64 *sig_error_rate_ptr;
54 u64 current_sig_errors;
55 uword *sig_error_bitmap;
56 vlib_main_t *vlib_main;
57 vlib_main_t **my_vlib_mains;
61 #include <vlib/vlib.h>
62 #include <vnet/vnet.h>
63 #include <vnet/devices/devices.h>
65 gmon_main_t gmon_main;
68 get_significant_errors (gmon_main_t * gm)
70 vlib_main_t *this_vlib_main;
71 vlib_error_main_t *em;
74 u64 significant_errors = 0;
77 clib_bitmap_foreach (code, gm->sig_error_bitmap)
79 for (vm_index = 0; vm_index < vec_len (gm->my_vlib_mains); vm_index++)
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);
90 return (significant_errors);
94 publish_pid (vlib_main_t * vm)
96 gmon_main_t *gm = &gmon_main;
98 *gm->vpef_pid_ptr = getpid ();
103 VLIB_API_INIT_FUNCTION (publish_pid);
107 gmon_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
110 u64 input_packets, last_input_packets, new_sig_errors;
111 f64 last_runtime, dt, now;
112 gmon_main_t *gm = &gmon_main;
116 last_input_packets = 0;
119 last_input_packets = 0;
121 /* Initial wait for the world to settle down */
122 vlib_process_suspend (vm, 5.0);
124 for (i = 0; i < vlib_get_n_threads (); i++)
125 vec_add1 (gm->my_vlib_mains, vlib_mains[i]);
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;
137 last_input_packets = input_packets;
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;
145 return 0; /* not so much */
149 VLIB_REGISTER_NODE (gmon_process_node,static) = {
150 .function = gmon_process,
151 .type = VLIB_NODE_TYPE_PROCESS,
152 .name = "gmon-process",
156 static clib_error_t *
157 gmon_init (vlib_main_t * vm)
159 gmon_main_t *gm = &gmon_main;
160 api_main_t *am = vlibapi_get_main ();
164 svmdb_map_args_t _ma, *ma = &_ma;
166 if ((error = vlib_call_init_function (vm, vpe_api_init)))
169 if ((error = vlib_call_init_function (vm, vlibmemory_init)))
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;
179 gm->svmdb_client = svmdb_map (ma);
181 /* Find or create, set to zero */
183 svmdb_local_set_vec_variable (gm->svmdb_client,
184 "vpp_vector_rate", (char *) v, sizeof (*v));
187 svmdb_local_set_vec_variable (gm->svmdb_client,
188 "vpp_input_rate", (char *) v, sizeof (*v));
191 svmdb_local_set_vec_variable (gm->svmdb_client,
192 "vpp_sig_error_rate",
193 (char *) v, sizeof (*v));
197 svmdb_local_set_vec_variable (gm->svmdb_client,
198 "vpp_pid", (char *) swp, sizeof (*swp));
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,
207 svmdb_local_get_variable_reference (gm->svmdb_client,
210 gm->sig_error_rate_ptr =
211 svmdb_local_get_variable_reference (gm->svmdb_client,
213 "vpp_sig_error_rate");
215 svmdb_local_get_variable_reference (gm->svmdb_client,
216 SVMDB_NAMESPACE_VEC, "vpp_pid");
220 VLIB_INIT_FUNCTION (gmon_init);
222 static clib_error_t *
223 gmon_exit (vlib_main_t * vm)
225 gmon_main_t *gm = &gmon_main;
227 if (gm->vector_rate_ptr)
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);
239 VLIB_MAIN_LOOP_EXIT_FUNCTION (gmon_exit);
242 significant_error_enable_disable (gmon_main_t * gm, u32 index, int enable)
244 vlib_main_t *vm = gm->vlib_main;
245 vlib_error_main_t *em = &vm->error_main;
247 if (index >= vec_len (em->counters))
248 return VNET_API_ERROR_NO_SUCH_ENTRY;
250 gm->sig_error_bitmap =
251 clib_bitmap_set (gm->sig_error_bitmap, index, enable);
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)
263 gmon_main_t *gm = &gmon_main;
265 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
267 if (unformat (input, "%d", &index))
269 else if (unformat (input, "disable"))
272 return clib_error_return (0, "unknown input `%U'",
273 format_unformat_error, input);
276 rv = significant_error_enable_disable (gm, index, enable);
284 return clib_error_return
285 (0, "significant_error_enable_disable returned %d", rv);
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,
300 * fd.io coding-style-patch-verification: ON
303 * eval: (c-set-style "gnu")