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>
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;
62 static inline u64 vnet_get_aggregate_rx_packets (void)
65 #include <vlib/vlib.h>
66 #include <vnet/vnet.h>
67 #include <vnet/devices/dpdk/dpdk.h>
70 gmon_main_t gmon_main;
72 static u64 get_significant_errors(gmon_main_t * gm)
74 vlib_main_t * this_vlib_main;
75 vlib_error_main_t * em;
78 u64 significant_errors = 0;
80 clib_bitmap_foreach (code, gm->sig_error_bitmap,
82 for (vm_index = 0; vm_index < vec_len (gm->my_vlib_mains); vm_index++)
84 this_vlib_main = gm->my_vlib_mains[vm_index];
85 em = &this_vlib_main->error_main;
86 significant_errors += em->counters[code] -
87 ((vec_len(em->counters_last_clear) > code) ?
88 em->counters_last_clear[code] : 0);
92 return (significant_errors);
96 publish_pid (vlib_main_t *vm)
98 gmon_main_t *gm = &gmon_main;
100 *gm->vpef_pid_ptr = getpid();
104 VLIB_API_INIT_FUNCTION(publish_pid);
108 gmon_process (vlib_main_t * vm,
109 vlib_node_runtime_t * rt,
113 u64 input_packets, last_input_packets, new_sig_errors;
114 f64 last_runtime, dt, now;
115 gmon_main_t *gm = &gmon_main;
119 last_input_packets = 0;
122 last_input_packets = 0;
124 /* Initial wait for the world to settle down */
125 vlib_process_suspend (vm, 5.0);
127 if (vec_len(vlib_mains) == 0)
128 vec_add1(gm->my_vlib_mains, &vlib_global_main);
131 for (i = 0; i < vec_len(vlib_mains); i++)
132 vec_add1 (gm->my_vlib_mains, vlib_mains[i]);
136 vlib_process_suspend (vm, 5.0);
137 vector_rate = vlib_last_vector_length_per_node (vm);
138 *gm->vector_rate_ptr = vector_rate;
139 now = vlib_time_now(vm);
140 dt = now - last_runtime;
141 input_packets = vnet_get_aggregate_rx_packets();
142 *gm->input_rate_ptr = (f64)(input_packets - last_input_packets) / dt;
144 last_input_packets = input_packets;
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;
152 return 0; /* not so much */
155 VLIB_REGISTER_NODE (gmon_process_node,static) = {
156 .function = gmon_process,
157 .type = VLIB_NODE_TYPE_PROCESS,
158 .name = "gmon-process",
161 static clib_error_t *
162 gmon_init (vlib_main_t *vm)
164 gmon_main_t *gm = &gmon_main;
165 api_main_t * am = &api_main;
168 clib_error_t * error;
170 if ((error = vlib_call_init_function(vm, vpe_api_init)))
173 /* Make sure that /global-vm is owned as directed */
174 svm_region_init_chroot_uid_gid (am->root_path, am->api_uid, am->api_gid);
177 gm->svmdb_client = svmdb_map_chroot(am->root_path);
179 /* Find or create, set to zero */
181 svmdb_local_set_vec_variable(gm->svmdb_client,
183 (char *)v, sizeof (*v));
186 svmdb_local_set_vec_variable(gm->svmdb_client,
188 (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,
199 (char *)swp, sizeof (*swp));
202 /* the value cells will never move, so acquire references to them */
203 gm->vector_rate_ptr =
204 svmdb_local_get_variable_reference (gm->svmdb_client,
208 svmdb_local_get_variable_reference (gm->svmdb_client,
211 gm->sig_error_rate_ptr =
212 svmdb_local_get_variable_reference (gm->svmdb_client,
214 "vpp_sig_error_rate");
216 svmdb_local_get_variable_reference (gm->svmdb_client,
222 VLIB_INIT_FUNCTION (gmon_init);
224 static clib_error_t *gmon_exit (vlib_main_t *vm)
226 gmon_main_t *gm = &gmon_main;
228 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);
238 VLIB_MAIN_LOOP_EXIT_FUNCTION (gmon_exit);
241 significant_error_enable_disable (gmon_main_t * gm,
242 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 = clib_bitmap_set (gm->sig_error_bitmap, index, enable);
254 static clib_error_t *
255 set_significant_error_command_fn (vlib_main_t * vm,
256 unformat_input_t * input,
257 vlib_cli_command_t * cmd)
262 gmon_main_t *gm = &gmon_main;
264 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
265 if (unformat (input, "%d", &index))
267 else if (unformat (input, "disable"))
270 return clib_error_return (0, "unknown input `%U'",
271 format_unformat_error, input);
274 rv = significant_error_enable_disable (gm, index, enable);
282 return clib_error_return
283 (0, "significant_error_enable_disable returned %d", rv);
289 VLIB_CLI_COMMAND (set_significant_error_command, static) = {
290 .path = "set significant error",
291 .short_help = "set significant error <counter-index-nnn> [disable]",
292 .function = set_significant_error_command_fn,