2 * Copyright (c) 2016 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.
15 #include <vlib/vlib.h>
17 #include <vppinfra/serialize.h>
19 extern void vl_msg_api_barrier_sync (void);
20 extern void vl_msg_api_barrier_release (void);
22 /* serialized representation of state strings */
24 #define foreach_state_string_code \
25 _(STATE_DONE, "done") \
26 _(STATE_DISABLED, "disabled") \
27 _(STATE_TIME_WAIT, "time wait") \
28 _(STATE_EVENT_WAIT, "event wait") \
29 _(STATE_ANY_WAIT, "any wait") \
30 _(STATE_POLLING, "polling") \
31 _(STATE_INTERRUPT_WAIT, "interrupt wait") \
32 _(STATE_INTERNAL, "internal")
37 foreach_state_string_code
39 } state_string_enum_t;
41 static char *state_strings[] = {
43 foreach_state_string_code
48 * Serialize a vlib_node_main_t. Appends the result to vector.
49 * Pass 0 to create a new vector, use vec_reset_length(vector)
50 * to recycle a vector / avoid memory allocation, etc.
51 * Switch heaps before/after to serialize into API client shared memory.
55 vlib_node_serialize (vlib_node_main_t * nm, u8 * vector,
56 u32 max_threads, int include_nexts, int include_stats)
58 serialize_main_t _sm, *sm = &_sm;
59 vlib_main_t *vm = vlib_get_main ();
61 static vlib_node_t ***node_dups;
63 static vlib_main_t **stat_vms;
69 state_string_enum_t state_code;
70 u32 threads_to_serialize;
72 vec_reset_length (node_dups);
74 if (vec_len (stat_vms) == 0)
76 for (i = 0; i < vec_len (vlib_mains); i++)
78 stat_vm = vlib_mains[i];
80 vec_add1 (stat_vms, stat_vm);
84 threads_to_serialize = clib_min (max_threads, vec_len (stat_vms));
87 * Barrier sync across stats scraping.
88 * Otherwise, the counts will be grossly inaccurate.
90 vl_msg_api_barrier_sync ();
92 for (j = 0; j < threads_to_serialize; j++)
94 stat_vm = stat_vms[j];
95 nm = &stat_vm->node_main;
99 for (i = 0; i < vec_len (nm->nodes); i++)
102 vlib_node_sync_stats (stat_vm, n);
106 nodes = vec_dup (nm->nodes);
108 vec_add1 (node_dups, nodes);
110 vl_msg_api_barrier_release ();
112 serialize_open_vector (sm, vector);
114 serialize_likely_small_unsigned_integer (sm, vec_len (stat_vms));
116 for (j = 0; j < vec_len (stat_vms); j++)
118 stat_vm = stat_vms[j];
119 nodes = node_dups[j];
121 serialize_likely_small_unsigned_integer (sm, vec_len (nodes));
123 for (i = 0; i < vec_len (nodes); i++)
127 l = n->stats_total.clocks - n->stats_last_clear.clocks;
128 v = n->stats_total.vectors - n->stats_last_clear.vectors;
129 c = n->stats_total.calls - n->stats_last_clear.calls;
130 d = n->stats_total.suspends - n->stats_last_clear.suspends;
132 state_code = STATE_INTERNAL;
134 if (n->type == VLIB_NODE_TYPE_PROCESS)
136 vlib_process_t *p = vlib_get_process_from_node (vm, n);
139 & (VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK
140 | VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT))
143 if (!(p->flags & VLIB_PROCESS_IS_RUNNING))
144 state_code = STATE_DONE;
147 case VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK:
148 state_code = STATE_TIME_WAIT;
151 case VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT:
152 state_code = STATE_EVENT_WAIT;
155 case (VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT | VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK):
161 else if (n->type != VLIB_NODE_TYPE_INTERNAL)
163 state_code = STATE_POLLING;
164 if (n->state == VLIB_NODE_STATE_DISABLED)
165 state_code = STATE_DISABLED;
166 else if (n->state == VLIB_NODE_STATE_INTERRUPT)
167 state_code = STATE_INTERRUPT_WAIT;
170 /* See unserialize_cstring */
171 name_bytes = vec_len (n->name);
172 serialize_likely_small_unsigned_integer (sm, name_bytes);
173 namep = serialize_get (sm, name_bytes);
174 memcpy (namep, n->name, name_bytes);
176 serialize_likely_small_unsigned_integer (sm, (u64) state_code);
177 serialize_likely_small_unsigned_integer (sm, n->type);
181 serialize_likely_small_unsigned_integer
182 (sm, vec_len (n->next_nodes));
183 for (k = 0; k < vec_len (n->next_nodes); k++)
184 serialize_likely_small_unsigned_integer (sm,
188 serialize_likely_small_unsigned_integer (sm, 0);
193 serialize_likely_small_unsigned_integer (sm, 1);
195 serialize_integer (sm, l, 8);
197 serialize_integer (sm, c, 8);
199 serialize_integer (sm, v, 8);
201 serialize_integer (sm, d, 8);
204 serialize_likely_small_unsigned_integer (sm, 0);
208 return (serialize_close_vector (sm));
212 vlib_node_unserialize (u8 * vector)
214 serialize_main_t _sm, *sm = &_sm;
219 vlib_node_t ***nodes_by_thread = 0;
222 state_string_enum_t state_code;
225 serialize_open_vector (sm, vector);
227 nstat_vms = unserialize_likely_small_unsigned_integer (sm);
229 vec_validate (nodes_by_thread, nstat_vms - 1);
230 _vec_len (nodes_by_thread) = 0;
232 for (i = 0; i < nstat_vms; i++)
234 nnodes = unserialize_likely_small_unsigned_integer (sm);
237 vec_validate (nodes, nnodes - 1);
238 vec_add1 (nodes_by_thread, nodes);
240 for (j = 0; j < nnodes; j++)
243 vec_validate (node, 0);
246 unserialize_cstring (sm, (char **) &(node->name));
247 state_code = unserialize_likely_small_unsigned_integer (sm);
248 node->state_string = (u8 *) state_strings[state_code];
250 node->type = unserialize_likely_small_unsigned_integer (sm);
251 nnexts = unserialize_likely_small_unsigned_integer (sm);
253 vec_validate (node->next_nodes, nnexts - 1);
254 for (k = 0; k < nnexts; k++)
255 node->next_nodes[k] =
256 unserialize_likely_small_unsigned_integer (sm);
258 stats_present = unserialize_likely_small_unsigned_integer (sm);
263 unserialize_integer (sm, &l, 8);
264 node->stats_total.clocks = l;
265 node->stats_last_clear.clocks = 0;
268 unserialize_integer (sm, &c, 8);
269 node->stats_total.calls = c;
272 unserialize_integer (sm, &v, 8);
273 node->stats_total.vectors = v;
276 unserialize_integer (sm, &d, 8);
277 node->stats_total.suspends = d;
281 return nodes_by_thread;
286 static clib_error_t *
287 test_node_serialize_command_fn (vlib_main_t * vm,
288 unformat_input_t * input,
289 vlib_cli_command_t * cmd)
291 vlib_node_main_t *nm = &vm->node_main;
293 vlib_node_t ***nodes_by_thread;
296 vlib_node_t *next_node;
298 u32 max_threads = (u32) ~ 0;
299 int include_nexts = 0;
300 int include_stats = 0;
302 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
304 if (unformat (input, "max-threads %d", &max_threads))
306 else if (unformat (input, "stats"))
308 else if (unformat (input, "nexts"))
315 * Keep the number of memcpy ops to a minimum (e.g. 1).
316 * The current size of the serialized vector is
319 vec_validate (vector, 16383);
320 vec_reset_length (vector);
322 vector = vlib_node_serialize (nm, vector, max_threads,
323 include_nexts, include_stats);
325 vlib_cli_output (vm, "result vector %d bytes", vec_len (vector));
327 nodes_by_thread = vlib_node_unserialize (vector);
331 for (i = 0; i < vec_len (nodes_by_thread); i++)
333 nodes = nodes_by_thread[i];
335 vlib_cli_output (vm, "thread %d", i);
337 for (j = 0; j < vec_len (nodes); j++)
341 vlib_cli_output (vm, "[%d] %s state %s", j, node->name,
345 (vm, " clocks %lld calls %lld suspends"
346 " %lld vectors %lld",
347 node->stats_total.clocks,
348 node->stats_total.calls,
349 node->stats_total.suspends, node->stats_total.vectors);
351 for (k = 0; k < vec_len (node->next_nodes); k++)
353 if (node->next_nodes[k] != ~0)
355 next_node = nodes[node->next_nodes[k]];
356 vlib_cli_output (vm, " [%d] %s", k, next_node->name);
362 for (j = 0; j < vec_len (nodes_by_thread); j++)
364 nodes = nodes_by_thread[j];
366 for (i = 0; i < vec_len (nodes); i++)
368 vec_free (nodes[i]->name);
369 vec_free (nodes[i]->next_nodes);
374 vec_free (nodes_by_thread);
380 VLIB_CLI_COMMAND (test_node_serialize_node, static) = {
381 .path = "test node serialize",
382 .short_help = "test node serialize [max-threads NN] nexts stats",
383 .function = test_node_serialize_command_fn,
389 * fd.io coding-style-patch-verification: ON
392 * eval: (c-set-style "gnu")