blob: b1e4ae186978b7b41522a78c8c21a6c6607aa1e4 [file] [log] [blame]
Dave Barachb44e9bc2016-02-19 09:06:23 -05001/*
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:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
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.
14 */
15#include <vlib/vlib.h>
16
17#include <vppinfra/serialize.h>
18
Dave Barach371e4e12016-07-08 09:38:52 -040019extern void vl_msg_api_barrier_sync (void);
20extern void vl_msg_api_barrier_release (void);
Dave Barach6931f592016-05-13 12:55:01 -040021
22/* serialized representation of state strings */
23
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")
33
Dave Barach371e4e12016-07-08 09:38:52 -040034typedef enum
35{
Dave Barach6931f592016-05-13 12:55:01 -040036#define _(a,b) a,
37 foreach_state_string_code
38#undef _
39} state_string_enum_t;
40
Dave Barach371e4e12016-07-08 09:38:52 -040041static char *state_strings[] = {
Dave Barach6931f592016-05-13 12:55:01 -040042#define _(a,b) b,
Dave Barach371e4e12016-07-08 09:38:52 -040043 foreach_state_string_code
Dave Barach6931f592016-05-13 12:55:01 -040044#undef _
Dave Barach371e4e12016-07-08 09:38:52 -040045};
Dave Barach6931f592016-05-13 12:55:01 -040046
Dave Barach371e4e12016-07-08 09:38:52 -040047/*
Dave Barachb44e9bc2016-02-19 09:06:23 -050048 * 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.
52 */
53
Dave Barach371e4e12016-07-08 09:38:52 -040054u8 *
55vlib_node_serialize (vlib_node_main_t * nm, u8 * vector,
56 u32 max_threads, int include_nexts, int include_stats)
Dave Barachb44e9bc2016-02-19 09:06:23 -050057{
Dave Barach371e4e12016-07-08 09:38:52 -040058 serialize_main_t _sm, *sm = &_sm;
59 vlib_main_t *vm = vlib_get_main ();
60 vlib_node_t *n;
61 static vlib_node_t ***node_dups;
62 vlib_node_t **nodes;
63 static vlib_main_t **stat_vms;
Dave Barach6931f592016-05-13 12:55:01 -040064 vlib_main_t *stat_vm;
Dave Barach371e4e12016-07-08 09:38:52 -040065 u8 *namep;
Dave Barach6931f592016-05-13 12:55:01 -040066 u32 name_bytes;
67 uword i, j, k;
68 u64 l, v, c, d;
69 state_string_enum_t state_code;
70 u32 threads_to_serialize;
Dave Barach6931f592016-05-13 12:55:01 -040071
Dave Barach371e4e12016-07-08 09:38:52 -040072 vec_reset_length (node_dups);
73
74 if (vec_len (stat_vms) == 0)
Dave Barach6931f592016-05-13 12:55:01 -040075 {
Dave Barach371e4e12016-07-08 09:38:52 -040076 if (vec_len (vlib_mains) == 0)
77 vec_add1 (stat_vms, vm);
Dave Barach6931f592016-05-13 12:55:01 -040078 else
Dave Barach371e4e12016-07-08 09:38:52 -040079 {
80 for (i = 0; i < vec_len (vlib_mains); i++)
81 {
82 stat_vm = vlib_mains[i];
83 if (stat_vm)
84 vec_add1 (stat_vms, stat_vm);
85 }
86 }
Dave Barach6931f592016-05-13 12:55:01 -040087 }
88
89 threads_to_serialize = clib_min (max_threads, vec_len (stat_vms));
90
Dave Barach371e4e12016-07-08 09:38:52 -040091 /*
Dave Barach6931f592016-05-13 12:55:01 -040092 * Barrier sync across stats scraping.
93 * Otherwise, the counts will be grossly inaccurate.
94 */
Dave Barach371e4e12016-07-08 09:38:52 -040095 vl_msg_api_barrier_sync ();
Dave Barach6931f592016-05-13 12:55:01 -040096
97 for (j = 0; j < threads_to_serialize; j++)
98 {
99 stat_vm = stat_vms[j];
100 nm = &stat_vm->node_main;
101
102 if (include_stats)
Dave Barach371e4e12016-07-08 09:38:52 -0400103 {
104 for (i = 0; i < vec_len (nm->nodes); i++)
105 {
106 n = nm->nodes[i];
107 vlib_node_sync_stats (stat_vm, n);
108 }
109 }
Dave Barach6931f592016-05-13 12:55:01 -0400110
111 nodes = vec_dup (nm->nodes);
Dave Barach371e4e12016-07-08 09:38:52 -0400112
113 vec_add1 (node_dups, nodes);
Dave Barach6931f592016-05-13 12:55:01 -0400114 }
Dave Barach371e4e12016-07-08 09:38:52 -0400115 vl_msg_api_barrier_release ();
116
Dave Barachb44e9bc2016-02-19 09:06:23 -0500117 serialize_open_vector (sm, vector);
Dave Barach6931f592016-05-13 12:55:01 -0400118
Dave Barach371e4e12016-07-08 09:38:52 -0400119 serialize_likely_small_unsigned_integer (sm, vec_len (stat_vms));
120
Dave Barach6931f592016-05-13 12:55:01 -0400121 for (j = 0; j < vec_len (stat_vms); j++)
Dave Barachb44e9bc2016-02-19 09:06:23 -0500122 {
Dave Barach6931f592016-05-13 12:55:01 -0400123 stat_vm = stat_vms[j];
124 nodes = node_dups[j];
125
Dave Barach371e4e12016-07-08 09:38:52 -0400126 serialize_likely_small_unsigned_integer (sm, vec_len (nodes));
Dave Barach6931f592016-05-13 12:55:01 -0400127
128 for (i = 0; i < vec_len (nodes); i++)
Dave Barach371e4e12016-07-08 09:38:52 -0400129 {
130 n = nodes[i];
Dave Barach6931f592016-05-13 12:55:01 -0400131
Dave Barach371e4e12016-07-08 09:38:52 -0400132 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;
Dave Barach6931f592016-05-13 12:55:01 -0400136
Dave Barach371e4e12016-07-08 09:38:52 -0400137 state_code = STATE_INTERNAL;
Dave Barach6931f592016-05-13 12:55:01 -0400138
Dave Barach371e4e12016-07-08 09:38:52 -0400139 if (n->type == VLIB_NODE_TYPE_PROCESS)
140 {
141 vlib_process_t *p = vlib_get_process_from_node (vm, n);
Dave Barach6931f592016-05-13 12:55:01 -0400142
Dave Barach371e4e12016-07-08 09:38:52 -0400143 switch (p->flags
144 & (VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK
145 | VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT))
146 {
147 default:
148 if (!(p->flags & VLIB_PROCESS_IS_RUNNING))
149 state_code = STATE_DONE;
150 break;
Dave Barach6931f592016-05-13 12:55:01 -0400151
Dave Barach371e4e12016-07-08 09:38:52 -0400152 case VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK:
153 state_code = STATE_TIME_WAIT;
154 break;
Dave Barach6931f592016-05-13 12:55:01 -0400155
Dave Barach371e4e12016-07-08 09:38:52 -0400156 case VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT:
157 state_code = STATE_EVENT_WAIT;
158 break;
Dave Barach6931f592016-05-13 12:55:01 -0400159
Dave Barach371e4e12016-07-08 09:38:52 -0400160 case (VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT | VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK):
161 state_code =
162 STATE_ANY_WAIT;
163 break;
164 }
165 }
166 else if (n->type != VLIB_NODE_TYPE_INTERNAL)
167 {
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;
173 }
Dave Barach6931f592016-05-13 12:55:01 -0400174
Dave Barach371e4e12016-07-08 09:38:52 -0400175 /* 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);
180
181 serialize_likely_small_unsigned_integer (sm, (u64) state_code);
182 serialize_likely_small_unsigned_integer (sm, n->type);
183
184 if (include_nexts)
185 {
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,
190 n->next_nodes[k]);
191 }
192 else
193 serialize_likely_small_unsigned_integer (sm, 0);
194
195 if (include_stats)
196 {
197 /* stats present */
198 serialize_likely_small_unsigned_integer (sm, 1);
199 /* total clocks */
200 serialize_integer (sm, l, 8);
201 /* Total calls */
202 serialize_integer (sm, c, 8);
203 /* Total vectors */
204 serialize_integer (sm, v, 8);
205 /* Total suspends */
206 serialize_integer (sm, d, 8);
207 }
208 else /* no stats */
209 serialize_likely_small_unsigned_integer (sm, 0);
210 }
Dave Barach6931f592016-05-13 12:55:01 -0400211 vec_free (nodes);
Dave Barachb44e9bc2016-02-19 09:06:23 -0500212 }
Dave Barachb44e9bc2016-02-19 09:06:23 -0500213 return (serialize_close_vector (sm));
214}
215
Dave Barach371e4e12016-07-08 09:38:52 -0400216vlib_node_t ***
217vlib_node_unserialize (u8 * vector)
Dave Barachb44e9bc2016-02-19 09:06:23 -0500218{
Dave Barach371e4e12016-07-08 09:38:52 -0400219 serialize_main_t _sm, *sm = &_sm;
Dave Barachb44e9bc2016-02-19 09:06:23 -0500220 u32 nnodes, nnexts;
Dave Barach6931f592016-05-13 12:55:01 -0400221 u32 nstat_vms;
Dave Barach371e4e12016-07-08 09:38:52 -0400222 vlib_node_t *node;
223 vlib_node_t **nodes;
224 vlib_node_t ***nodes_by_thread = 0;
Dave Barach6931f592016-05-13 12:55:01 -0400225 int i, j, k;
226 u64 l, v, c, d;
227 state_string_enum_t state_code;
228 int stats_present;
Dave Barachb44e9bc2016-02-19 09:06:23 -0500229
230 serialize_open_vector (sm, vector);
Dave Barach371e4e12016-07-08 09:38:52 -0400231
Dave Barach6931f592016-05-13 12:55:01 -0400232 nstat_vms = unserialize_likely_small_unsigned_integer (sm);
Dave Barachb44e9bc2016-02-19 09:06:23 -0500233
Dave Barach6931f592016-05-13 12:55:01 -0400234 vec_validate (nodes_by_thread, nstat_vms - 1);
235 _vec_len (nodes_by_thread) = 0;
Dave Barachb44e9bc2016-02-19 09:06:23 -0500236
Dave Barach6931f592016-05-13 12:55:01 -0400237 for (i = 0; i < nstat_vms; i++)
Dave Barachb44e9bc2016-02-19 09:06:23 -0500238 {
Dave Barach6931f592016-05-13 12:55:01 -0400239 nnodes = unserialize_likely_small_unsigned_integer (sm);
Dave Barachb44e9bc2016-02-19 09:06:23 -0500240
Dave Barach6931f592016-05-13 12:55:01 -0400241 nodes = 0;
Dave Barach371e4e12016-07-08 09:38:52 -0400242 vec_validate (nodes, nnodes - 1);
Dave Barach6931f592016-05-13 12:55:01 -0400243 vec_add1 (nodes_by_thread, nodes);
244
245 for (j = 0; j < nnodes; j++)
Dave Barach371e4e12016-07-08 09:38:52 -0400246 {
247 node = 0;
248 vec_validate (node, 0);
249 nodes[j] = node;
Dave Barach6931f592016-05-13 12:55:01 -0400250
Dave Barach371e4e12016-07-08 09:38:52 -0400251 unserialize_cstring (sm, (char **) &(node->name));
252 state_code = unserialize_likely_small_unsigned_integer (sm);
253 node->state_string = (u8 *) state_strings[state_code];
Dave Barach6931f592016-05-13 12:55:01 -0400254
Dave Barach371e4e12016-07-08 09:38:52 -0400255 node->type = unserialize_likely_small_unsigned_integer (sm);
256 nnexts = unserialize_likely_small_unsigned_integer (sm);
257 if (nnexts > 0)
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);
Dave Barach6931f592016-05-13 12:55:01 -0400262
Dave Barach371e4e12016-07-08 09:38:52 -0400263 stats_present = unserialize_likely_small_unsigned_integer (sm);
Dave Barach6931f592016-05-13 12:55:01 -0400264
Dave Barach371e4e12016-07-08 09:38:52 -0400265 if (stats_present)
266 {
267 /* total clocks */
268 unserialize_integer (sm, &l, 8);
269 node->stats_total.clocks = l;
270 node->stats_last_clear.clocks = 0;
271
272 /* Total calls */
273 unserialize_integer (sm, &c, 8);
274 node->stats_total.calls = c;
275
276 /* Total vectors */
277 unserialize_integer (sm, &v, 8);
278 node->stats_total.vectors = v;
279
280 /* Total suspends */
281 unserialize_integer (sm, &d, 8);
282 node->stats_total.suspends = d;
283 }
284 }
Dave Barachb44e9bc2016-02-19 09:06:23 -0500285 }
Dave Barach371e4e12016-07-08 09:38:52 -0400286 return nodes_by_thread;
Dave Barachb44e9bc2016-02-19 09:06:23 -0500287}
288
Dave Barachb44e9bc2016-02-19 09:06:23 -0500289#if CLIB_DEBUG > 0
290
291static clib_error_t *
292test_node_serialize_command_fn (vlib_main_t * vm,
Dave Barach371e4e12016-07-08 09:38:52 -0400293 unformat_input_t * input,
294 vlib_cli_command_t * cmd)
Dave Barachb44e9bc2016-02-19 09:06:23 -0500295{
Dave Barach371e4e12016-07-08 09:38:52 -0400296 vlib_node_main_t *nm = &vm->node_main;
297 u8 *vector = 0;
298 vlib_node_t ***nodes_by_thread;
299 vlib_node_t **nodes;
300 vlib_node_t *node;
301 vlib_node_t *next_node;
Dave Barach6931f592016-05-13 12:55:01 -0400302 int i, j, k;
Dave Barach371e4e12016-07-08 09:38:52 -0400303 u32 max_threads = (u32) ~ 0;
Dave Barach6931f592016-05-13 12:55:01 -0400304 int include_nexts = 0;
Dave Barach371e4e12016-07-08 09:38:52 -0400305 int include_stats = 0;
Dave Barach6931f592016-05-13 12:55:01 -0400306
Dave Barach371e4e12016-07-08 09:38:52 -0400307 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
Dave Barach6931f592016-05-13 12:55:01 -0400308 {
309 if (unformat (input, "max-threads %d", &max_threads))
Dave Barach371e4e12016-07-08 09:38:52 -0400310 ;
Dave Barach6931f592016-05-13 12:55:01 -0400311 else if (unformat (input, "stats"))
Dave Barach371e4e12016-07-08 09:38:52 -0400312 include_stats = 1;
Dave Barach6931f592016-05-13 12:55:01 -0400313 else if (unformat (input, "nexts"))
Dave Barach371e4e12016-07-08 09:38:52 -0400314 include_nexts = 1;
Dave Barach6931f592016-05-13 12:55:01 -0400315 else
Dave Barach371e4e12016-07-08 09:38:52 -0400316 break;
Dave Barach6931f592016-05-13 12:55:01 -0400317 }
Dave Barachb44e9bc2016-02-19 09:06:23 -0500318
Dave Barach371e4e12016-07-08 09:38:52 -0400319 /*
Dave Barachb44e9bc2016-02-19 09:06:23 -0500320 * Keep the number of memcpy ops to a minimum (e.g. 1).
321 * The current size of the serialized vector is
322 * slightly under 4K.
323 */
Dave Barach6931f592016-05-13 12:55:01 -0400324 vec_validate (vector, 16383);
Dave Barachb44e9bc2016-02-19 09:06:23 -0500325 vec_reset_length (vector);
326
Dave Barach371e4e12016-07-08 09:38:52 -0400327 vector = vlib_node_serialize (nm, vector, max_threads,
328 include_nexts, include_stats);
Dave Barachb44e9bc2016-02-19 09:06:23 -0500329
Dave Barach371e4e12016-07-08 09:38:52 -0400330 vlib_cli_output (vm, "result vector %d bytes", vec_len (vector));
Dave Barach6931f592016-05-13 12:55:01 -0400331
332 nodes_by_thread = vlib_node_unserialize (vector);
Dave Barachb44e9bc2016-02-19 09:06:23 -0500333
334 vec_free (vector);
Dave Barach371e4e12016-07-08 09:38:52 -0400335
336 for (i = 0; i < vec_len (nodes_by_thread); i++)
Dave Barachb44e9bc2016-02-19 09:06:23 -0500337 {
Dave Barach6931f592016-05-13 12:55:01 -0400338 nodes = nodes_by_thread[i];
339
340 vlib_cli_output (vm, "thread %d", i);
341
Dave Barach371e4e12016-07-08 09:38:52 -0400342 for (j = 0; j < vec_len (nodes); j++)
343 {
344 node = nodes[j];
Dave Barach6931f592016-05-13 12:55:01 -0400345
Dave Barach371e4e12016-07-08 09:38:52 -0400346 vlib_cli_output (vm, "[%d] %s state %s", j, node->name,
347 node->state_string);
Dave Barach6931f592016-05-13 12:55:01 -0400348
Dave Barach371e4e12016-07-08 09:38:52 -0400349 vlib_cli_output
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);
Dave Barach6931f592016-05-13 12:55:01 -0400355
Dave Barach371e4e12016-07-08 09:38:52 -0400356 for (k = 0; k < vec_len (node->next_nodes); k++)
357 {
358 if (node->next_nodes[k] != ~0)
Dave Barach5c55e6b2016-07-25 16:13:00 -0400359 {
360 next_node = nodes[node->next_nodes[k]];
361 vlib_cli_output (vm, " [%d] %s", k, next_node->name);
362 }
Dave Barach371e4e12016-07-08 09:38:52 -0400363 }
364 }
365 }
Dave Barachb44e9bc2016-02-19 09:06:23 -0500366
Dave Barach371e4e12016-07-08 09:38:52 -0400367 for (j = 0; j < vec_len (nodes_by_thread); j++)
Dave Barachb44e9bc2016-02-19 09:06:23 -0500368 {
Dave Barach6931f592016-05-13 12:55:01 -0400369 nodes = nodes_by_thread[j];
370
Dave Barach371e4e12016-07-08 09:38:52 -0400371 for (i = 0; i < vec_len (nodes); i++)
372 {
373 vec_free (nodes[i]->name);
374 vec_free (nodes[i]->next_nodes);
375 vec_free (nodes[i]);
376 }
377 vec_free (nodes);
Dave Barachb44e9bc2016-02-19 09:06:23 -0500378 }
Dave Barach6931f592016-05-13 12:55:01 -0400379 vec_free (nodes_by_thread);
Dave Barachb44e9bc2016-02-19 09:06:23 -0500380
381 return 0;
382}
383
Dave Barach371e4e12016-07-08 09:38:52 -0400384/* *INDENT-OFF* */
Dave Barachb44e9bc2016-02-19 09:06:23 -0500385VLIB_CLI_COMMAND (test_node_serialize_node, static) = {
386 .path = "test node serialize",
Dave Barach6931f592016-05-13 12:55:01 -0400387 .short_help = "test node serialize [max-threads NN] nexts stats",
Dave Barachb44e9bc2016-02-19 09:06:23 -0500388 .function = test_node_serialize_command_fn,
389};
Dave Barach371e4e12016-07-08 09:38:52 -0400390/* *INDENT-ON* */
Dave Barachb44e9bc2016-02-19 09:06:23 -0500391#endif
Dave Barach371e4e12016-07-08 09:38:52 -0400392
393/*
394 * fd.io coding-style-patch-verification: ON
395 *
396 * Local Variables:
397 * eval: (c-set-style "gnu")
398 * End:
399 */