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 if (vec_len (vlib_mains) == 0)
77 vec_add1 (stat_vms, vm);
80 for (i = 0; i < vec_len (vlib_mains); i++)
82 stat_vm = vlib_mains[i];
84 vec_add1 (stat_vms, stat_vm);
89 threads_to_serialize = clib_min (max_threads, vec_len (stat_vms));
92 * Barrier sync across stats scraping.
93 * Otherwise, the counts will be grossly inaccurate.
95 vl_msg_api_barrier_sync ();
97 for (j = 0; j < threads_to_serialize; j++)
99 stat_vm = stat_vms[j];
100 nm = &stat_vm->node_main;
104 for (i = 0; i < vec_len (nm->nodes); i++)
107 vlib_node_sync_stats (stat_vm, n);
111 nodes = vec_dup (nm->nodes);
113 vec_add1 (node_dups, nodes);
115 vl_msg_api_barrier_release ();
117 serialize_open_vector (sm, vector);
119 serialize_likely_small_unsigned_integer (sm, vec_len (stat_vms));
121 for (j = 0; j < vec_len (stat_vms); j++)
123 stat_vm = stat_vms[j];
124 nodes = node_dups[j];
126 serialize_likely_small_unsigned_integer (sm, vec_len (nodes));
128 for (i = 0; i < vec_len (nodes); i++)
132 l = n->stats_total.clocks - n->stats_last_clear.clocks;
133 v = n->stats_total.vectors - n->stats_last_clear.vectors;
134 c = n->stats_total.calls - n->stats_last_clear.calls;
135 d = n->stats_total.suspends - n->stats_last_clear.suspends;
137 state_code = STATE_INTERNAL;
139 if (n->type == VLIB_NODE_TYPE_PROCESS)
141 vlib_process_t *p = vlib_get_process_from_node (vm, n);
144 & (VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK
145 | VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT))
148 if (!(p->flags & VLIB_PROCESS_IS_RUNNING))
149 state_code = STATE_DONE;
152 case VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK:
153 state_code = STATE_TIME_WAIT;
156 case VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT:
157 state_code = STATE_EVENT_WAIT;
160 case (VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT | VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK):
166 else if (n->type != VLIB_NODE_TYPE_INTERNAL)
168 state_code = STATE_POLLING;
169 if (n->state == VLIB_NODE_STATE_DISABLED)
170 state_code = STATE_DISABLED;
171 else if (n->state == VLIB_NODE_STATE_INTERRUPT)
172 state_code = STATE_INTERRUPT_WAIT;
175 /* See unserialize_cstring */
176 name_bytes = vec_len (n->name);
177 serialize_likely_small_unsigned_integer (sm, name_bytes);
178 namep = serialize_get (sm, name_bytes);
179 memcpy (namep, n->name, name_bytes);
181 serialize_likely_small_unsigned_integer (sm, (u64) state_code);
182 serialize_likely_small_unsigned_integer (sm, n->type);
186 serialize_likely_small_unsigned_integer
187 (sm, vec_len (n->next_nodes));
188 for (k = 0; k < vec_len (n->next_nodes); k++)
189 serialize_likely_small_unsigned_integer (sm,
193 serialize_likely_small_unsigned_integer (sm, 0);
198 serialize_likely_small_unsigned_integer (sm, 1);
200 serialize_integer (sm, l, 8);
202 serialize_integer (sm, c, 8);
204 serialize_integer (sm, v, 8);
206 serialize_integer (sm, d, 8);
209 serialize_likely_small_unsigned_integer (sm, 0);
213 return (serialize_close_vector (sm));
217 vlib_node_unserialize (u8 * vector)
219 serialize_main_t _sm, *sm = &_sm;
224 vlib_node_t ***nodes_by_thread = 0;
227 state_string_enum_t state_code;
230 serialize_open_vector (sm, vector);
232 nstat_vms = unserialize_likely_small_unsigned_integer (sm);
234 vec_validate (nodes_by_thread, nstat_vms - 1);
235 _vec_len (nodes_by_thread) = 0;
237 for (i = 0; i < nstat_vms; i++)
239 nnodes = unserialize_likely_small_unsigned_integer (sm);
242 vec_validate (nodes, nnodes - 1);
243 vec_add1 (nodes_by_thread, nodes);
245 for (j = 0; j < nnodes; j++)
248 vec_validate (node, 0);
251 unserialize_cstring (sm, (char **) &(node->name));
252 state_code = unserialize_likely_small_unsigned_integer (sm);
253 node->state_string = (u8 *) state_strings[state_code];
255 node->type = unserialize_likely_small_unsigned_integer (sm);
256 nnexts = unserialize_likely_small_unsigned_integer (sm);
258 vec_validate (node->next_nodes, nnexts - 1);
259 for (k = 0; k < nnexts; k++)
260 node->next_nodes[k] =
261 unserialize_likely_small_unsigned_integer (sm);
263 stats_present = unserialize_likely_small_unsigned_integer (sm);
268 unserialize_integer (sm, &l, 8);
269 node->stats_total.clocks = l;
270 node->stats_last_clear.clocks = 0;
273 unserialize_integer (sm, &c, 8);
274 node->stats_total.calls = c;
277 unserialize_integer (sm, &v, 8);
278 node->stats_total.vectors = v;
281 unserialize_integer (sm, &d, 8);
282 node->stats_total.suspends = d;
286 return nodes_by_thread;
291 static clib_error_t *
292 test_node_serialize_command_fn (vlib_main_t * vm,
293 unformat_input_t * input,
294 vlib_cli_command_t * cmd)
296 vlib_node_main_t *nm = &vm->node_main;
298 vlib_node_t ***nodes_by_thread;
301 vlib_node_t *next_node;
303 u32 max_threads = (u32) ~ 0;
304 int include_nexts = 0;
305 int include_stats = 0;
307 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
309 if (unformat (input, "max-threads %d", &max_threads))
311 else if (unformat (input, "stats"))
313 else if (unformat (input, "nexts"))
320 * Keep the number of memcpy ops to a minimum (e.g. 1).
321 * The current size of the serialized vector is
324 vec_validate (vector, 16383);
325 vec_reset_length (vector);
327 vector = vlib_node_serialize (nm, vector, max_threads,
328 include_nexts, include_stats);
330 vlib_cli_output (vm, "result vector %d bytes", vec_len (vector));
332 nodes_by_thread = vlib_node_unserialize (vector);
336 for (i = 0; i < vec_len (nodes_by_thread); i++)
338 nodes = nodes_by_thread[i];
340 vlib_cli_output (vm, "thread %d", i);
342 for (j = 0; j < vec_len (nodes); j++)
346 vlib_cli_output (vm, "[%d] %s state %s", j, node->name,
350 (vm, " clocks %lld calls %lld suspends"
351 " %lld vectors %lld",
352 node->stats_total.clocks,
353 node->stats_total.calls,
354 node->stats_total.suspends, node->stats_total.vectors);
356 for (k = 0; k < vec_len (node->next_nodes); k++)
358 if (node->next_nodes[k] != ~0)
360 next_node = nodes[node->next_nodes[k]];
361 vlib_cli_output (vm, " [%d] %s", k, next_node->name);
367 for (j = 0; j < vec_len (nodes_by_thread); j++)
369 nodes = nodes_by_thread[j];
371 for (i = 0; i < vec_len (nodes); i++)
373 vec_free (nodes[i]->name);
374 vec_free (nodes[i]->next_nodes);
379 vec_free (nodes_by_thread);
385 VLIB_CLI_COMMAND (test_node_serialize_node, static) = {
386 .path = "test node serialize",
387 .short_help = "test node serialize [max-threads NN] nexts stats",
388 .function = test_node_serialize_command_fn,
394 * fd.io coding-style-patch-verification: ON
397 * eval: (c-set-style "gnu")