Dave Barach | b44e9bc | 2016-02-19 09:06:23 -0500 | [diff] [blame] | 1 | /* |
| 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 Barach | 6931f59 | 2016-05-13 12:55:01 -0400 | [diff] [blame] | 19 | extern void vl_msg_api_barrier_sync(void); |
| 20 | extern void vl_msg_api_barrier_release(void); |
| 21 | |
| 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 | |
| 34 | typedef enum { |
| 35 | #define _(a,b) a, |
| 36 | foreach_state_string_code |
| 37 | #undef _ |
| 38 | } state_string_enum_t; |
| 39 | |
| 40 | static char *state_strings[] = |
| 41 | { |
| 42 | #define _(a,b) b, |
| 43 | foreach_state_string_code |
| 44 | #undef _ |
| 45 | }; |
| 46 | |
Dave Barach | b44e9bc | 2016-02-19 09:06:23 -0500 | [diff] [blame] | 47 | /* |
| 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. |
| 52 | */ |
| 53 | |
Dave Barach | 6931f59 | 2016-05-13 12:55:01 -0400 | [diff] [blame] | 54 | u8 * vlib_node_serialize (vlib_node_main_t *nm, u8 * vector, |
| 55 | u32 max_threads, int include_nexts, |
| 56 | int include_stats) |
Dave Barach | b44e9bc | 2016-02-19 09:06:23 -0500 | [diff] [blame] | 57 | { |
| 58 | serialize_main_t _sm, *sm=&_sm; |
Dave Barach | 6931f59 | 2016-05-13 12:55:01 -0400 | [diff] [blame] | 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; |
| 64 | vlib_main_t *stat_vm; |
| 65 | u8 * namep; |
| 66 | 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; |
| 71 | |
| 72 | vec_reset_length(node_dups); |
| 73 | |
| 74 | if (vec_len(stat_vms) == 0) |
| 75 | { |
| 76 | if (vec_len(vlib_mains) == 0) |
| 77 | vec_add1 (stat_vms, vm); |
| 78 | else |
| 79 | { |
| 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 | } |
| 87 | } |
| 88 | |
| 89 | threads_to_serialize = clib_min (max_threads, vec_len (stat_vms)); |
| 90 | |
| 91 | /* |
| 92 | * Barrier sync across stats scraping. |
| 93 | * Otherwise, the counts will be grossly inaccurate. |
| 94 | */ |
| 95 | vl_msg_api_barrier_sync(); |
| 96 | |
| 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) |
| 103 | { |
| 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 | } |
| 110 | |
| 111 | nodes = vec_dup (nm->nodes); |
| 112 | |
| 113 | vec_add1(node_dups, nodes); |
| 114 | } |
| 115 | vl_msg_api_barrier_release(); |
Dave Barach | b44e9bc | 2016-02-19 09:06:23 -0500 | [diff] [blame] | 116 | |
| 117 | serialize_open_vector (sm, vector); |
Dave Barach | 6931f59 | 2016-05-13 12:55:01 -0400 | [diff] [blame] | 118 | |
| 119 | serialize_likely_small_unsigned_integer (sm, vec_len(stat_vms)); |
Dave Barach | b44e9bc | 2016-02-19 09:06:23 -0500 | [diff] [blame] | 120 | |
Dave Barach | 6931f59 | 2016-05-13 12:55:01 -0400 | [diff] [blame] | 121 | for (j = 0; j < vec_len (stat_vms); j++) |
Dave Barach | b44e9bc | 2016-02-19 09:06:23 -0500 | [diff] [blame] | 122 | { |
Dave Barach | 6931f59 | 2016-05-13 12:55:01 -0400 | [diff] [blame] | 123 | stat_vm = stat_vms[j]; |
| 124 | nodes = node_dups[j]; |
| 125 | |
| 126 | serialize_likely_small_unsigned_integer (sm, vec_len(nodes)); |
| 127 | |
| 128 | for (i = 0; i < vec_len (nodes); i++) |
| 129 | { |
| 130 | n = nodes[i]; |
| 131 | |
| 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 |
| 136 | - n->stats_last_clear.suspends; |
| 137 | |
| 138 | state_code = STATE_INTERNAL; |
| 139 | |
| 140 | if (n->type == VLIB_NODE_TYPE_PROCESS) |
| 141 | { |
| 142 | vlib_process_t * p = vlib_get_process_from_node (vm, n); |
| 143 | |
| 144 | switch (p->flags |
| 145 | & (VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK |
| 146 | | VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT)) |
| 147 | { |
| 148 | default: |
| 149 | if (! (p->flags & VLIB_PROCESS_IS_RUNNING)) |
| 150 | state_code = STATE_DONE; |
| 151 | break; |
| 152 | |
| 153 | case VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK: |
| 154 | state_code = STATE_TIME_WAIT; |
| 155 | break; |
| 156 | |
| 157 | case VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT: |
| 158 | state_code = STATE_EVENT_WAIT; |
| 159 | break; |
| 160 | |
| 161 | case (VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT |
| 162 | | VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK): |
| 163 | state_code = STATE_ANY_WAIT; |
| 164 | break; |
| 165 | } |
| 166 | } |
| 167 | else if (n->type != VLIB_NODE_TYPE_INTERNAL) |
| 168 | { |
| 169 | state_code = STATE_POLLING; |
| 170 | if (n->state == VLIB_NODE_STATE_DISABLED) |
| 171 | state_code = STATE_DISABLED; |
| 172 | else if (n->state == VLIB_NODE_STATE_INTERRUPT) |
| 173 | state_code = STATE_INTERRUPT_WAIT; |
| 174 | } |
| 175 | |
| 176 | /* See unserialize_cstring */ |
| 177 | name_bytes = vec_len (n->name); |
| 178 | serialize_likely_small_unsigned_integer(sm, name_bytes); |
| 179 | namep = serialize_get (sm, name_bytes); |
| 180 | memcpy (namep, n->name, name_bytes); |
| 181 | |
| 182 | serialize_likely_small_unsigned_integer (sm, (u64)state_code); |
| 183 | serialize_likely_small_unsigned_integer (sm, n->type); |
| 184 | |
| 185 | if (include_nexts) |
| 186 | { |
| 187 | serialize_likely_small_unsigned_integer |
| 188 | (sm, vec_len(n->next_nodes)); |
| 189 | for (k = 0; k < vec_len (n->next_nodes); k++) |
| 190 | serialize_likely_small_unsigned_integer (sm, 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 | } |
| 211 | vec_free (nodes); |
Dave Barach | b44e9bc | 2016-02-19 09:06:23 -0500 | [diff] [blame] | 212 | } |
Dave Barach | b44e9bc | 2016-02-19 09:06:23 -0500 | [diff] [blame] | 213 | return (serialize_close_vector (sm)); |
| 214 | } |
| 215 | |
Dave Barach | 6931f59 | 2016-05-13 12:55:01 -0400 | [diff] [blame] | 216 | vlib_node_t *** vlib_node_unserialize (u8 * vector) |
Dave Barach | b44e9bc | 2016-02-19 09:06:23 -0500 | [diff] [blame] | 217 | { |
| 218 | serialize_main_t _sm, *sm=&_sm; |
| 219 | u32 nnodes, nnexts; |
Dave Barach | 6931f59 | 2016-05-13 12:55:01 -0400 | [diff] [blame] | 220 | u32 nstat_vms; |
Dave Barach | b44e9bc | 2016-02-19 09:06:23 -0500 | [diff] [blame] | 221 | vlib_node_t * node; |
Dave Barach | 6931f59 | 2016-05-13 12:55:01 -0400 | [diff] [blame] | 222 | vlib_node_t ** nodes; |
| 223 | vlib_node_t *** nodes_by_thread = 0; |
| 224 | int i, j, k; |
| 225 | u64 l, v, c, d; |
| 226 | state_string_enum_t state_code; |
| 227 | int stats_present; |
Dave Barach | b44e9bc | 2016-02-19 09:06:23 -0500 | [diff] [blame] | 228 | |
| 229 | serialize_open_vector (sm, vector); |
| 230 | |
Dave Barach | 6931f59 | 2016-05-13 12:55:01 -0400 | [diff] [blame] | 231 | nstat_vms = unserialize_likely_small_unsigned_integer (sm); |
Dave Barach | b44e9bc | 2016-02-19 09:06:23 -0500 | [diff] [blame] | 232 | |
Dave Barach | 6931f59 | 2016-05-13 12:55:01 -0400 | [diff] [blame] | 233 | vec_validate (nodes_by_thread, nstat_vms - 1); |
| 234 | _vec_len (nodes_by_thread) = 0; |
Dave Barach | b44e9bc | 2016-02-19 09:06:23 -0500 | [diff] [blame] | 235 | |
Dave Barach | 6931f59 | 2016-05-13 12:55:01 -0400 | [diff] [blame] | 236 | for (i = 0; i < nstat_vms; i++) |
Dave Barach | b44e9bc | 2016-02-19 09:06:23 -0500 | [diff] [blame] | 237 | { |
Dave Barach | 6931f59 | 2016-05-13 12:55:01 -0400 | [diff] [blame] | 238 | nnodes = unserialize_likely_small_unsigned_integer (sm); |
Dave Barach | b44e9bc | 2016-02-19 09:06:23 -0500 | [diff] [blame] | 239 | |
Dave Barach | 6931f59 | 2016-05-13 12:55:01 -0400 | [diff] [blame] | 240 | nodes = 0; |
| 241 | vec_validate (nodes, nnodes-1); |
| 242 | vec_add1 (nodes_by_thread, nodes); |
| 243 | |
| 244 | for (j = 0; j < nnodes; j++) |
| 245 | { |
| 246 | node = 0; |
| 247 | vec_validate (node,0); |
| 248 | nodes[j] = node; |
| 249 | |
| 250 | unserialize_cstring (sm, (char **)&(node->name)); |
| 251 | state_code = unserialize_likely_small_unsigned_integer (sm); |
| 252 | node->state_string = (u8 *) state_strings[state_code]; |
| 253 | |
| 254 | node->type = |
| 255 | 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); |
| 262 | |
| 263 | stats_present = unserialize_likely_small_unsigned_integer (sm); |
| 264 | |
| 265 | 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 Barach | b44e9bc | 2016-02-19 09:06:23 -0500 | [diff] [blame] | 285 | } |
Dave Barach | 6931f59 | 2016-05-13 12:55:01 -0400 | [diff] [blame] | 286 | return nodes_by_thread; |
Dave Barach | b44e9bc | 2016-02-19 09:06:23 -0500 | [diff] [blame] | 287 | } |
| 288 | |
Dave Barach | b44e9bc | 2016-02-19 09:06:23 -0500 | [diff] [blame] | 289 | #if CLIB_DEBUG > 0 |
| 290 | |
| 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) |
| 295 | { |
| 296 | vlib_node_main_t * nm = &vm->node_main; |
| 297 | u8 * vector = 0; |
Dave Barach | 6931f59 | 2016-05-13 12:55:01 -0400 | [diff] [blame] | 298 | vlib_node_t *** nodes_by_thread; |
Dave Barach | b44e9bc | 2016-02-19 09:06:23 -0500 | [diff] [blame] | 299 | vlib_node_t ** nodes; |
| 300 | vlib_node_t * node; |
| 301 | vlib_node_t * next_node; |
Dave Barach | 6931f59 | 2016-05-13 12:55:01 -0400 | [diff] [blame] | 302 | int i, j, k; |
| 303 | u32 max_threads = (u32) ~0; |
| 304 | int include_nexts = 0; |
| 305 | int include_stats = 0; |
| 306 | |
| 307 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 308 | { |
| 309 | if (unformat (input, "max-threads %d", &max_threads)) |
| 310 | ; |
| 311 | else if (unformat (input, "stats")) |
| 312 | include_stats = 1; |
| 313 | else if (unformat (input, "nexts")) |
| 314 | include_nexts = 1; |
| 315 | else |
| 316 | break; |
| 317 | } |
Dave Barach | b44e9bc | 2016-02-19 09:06:23 -0500 | [diff] [blame] | 318 | |
| 319 | /* |
| 320 | * 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 Barach | 6931f59 | 2016-05-13 12:55:01 -0400 | [diff] [blame] | 324 | vec_validate (vector, 16383); |
Dave Barach | b44e9bc | 2016-02-19 09:06:23 -0500 | [diff] [blame] | 325 | vec_reset_length (vector); |
| 326 | |
Dave Barach | 6931f59 | 2016-05-13 12:55:01 -0400 | [diff] [blame] | 327 | vector = vlib_node_serialize (nm, vector, max_threads, |
| 328 | include_nexts, include_stats); |
Dave Barach | b44e9bc | 2016-02-19 09:06:23 -0500 | [diff] [blame] | 329 | |
Dave Barach | 6931f59 | 2016-05-13 12:55:01 -0400 | [diff] [blame] | 330 | vlib_cli_output (vm, "result vector %d bytes", vec_len(vector)); |
| 331 | |
| 332 | nodes_by_thread = vlib_node_unserialize (vector); |
Dave Barach | b44e9bc | 2016-02-19 09:06:23 -0500 | [diff] [blame] | 333 | |
| 334 | vec_free (vector); |
| 335 | |
Dave Barach | 6931f59 | 2016-05-13 12:55:01 -0400 | [diff] [blame] | 336 | for (i = 0; i < vec_len(nodes_by_thread); i++) |
Dave Barach | b44e9bc | 2016-02-19 09:06:23 -0500 | [diff] [blame] | 337 | { |
Dave Barach | 6931f59 | 2016-05-13 12:55:01 -0400 | [diff] [blame] | 338 | nodes = nodes_by_thread[i]; |
| 339 | |
| 340 | vlib_cli_output (vm, "thread %d", i); |
| 341 | |
| 342 | for (j = 0; j < vec_len(nodes); j++) |
Dave Barach | b44e9bc | 2016-02-19 09:06:23 -0500 | [diff] [blame] | 343 | { |
Dave Barach | 6931f59 | 2016-05-13 12:55:01 -0400 | [diff] [blame] | 344 | node = nodes[j]; |
| 345 | |
| 346 | vlib_cli_output (vm, "[%d] %s state %s", j, node->name, |
| 347 | node->state_string); |
| 348 | |
| 349 | 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, |
| 355 | node->stats_total.vectors); |
| 356 | |
| 357 | for (k = 0; k < vec_len (node->next_nodes); k++) |
| 358 | { |
| 359 | 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); |
| 362 | } |
Dave Barach | b44e9bc | 2016-02-19 09:06:23 -0500 | [diff] [blame] | 363 | } |
| 364 | } |
| 365 | |
Dave Barach | 6931f59 | 2016-05-13 12:55:01 -0400 | [diff] [blame] | 366 | for (j = 0; j < vec_len(nodes_by_thread); j++) |
Dave Barach | b44e9bc | 2016-02-19 09:06:23 -0500 | [diff] [blame] | 367 | { |
Dave Barach | 6931f59 | 2016-05-13 12:55:01 -0400 | [diff] [blame] | 368 | nodes = nodes_by_thread[j]; |
| 369 | |
| 370 | for (i = 0; i < vec_len(nodes); i++) |
| 371 | { |
| 372 | vec_free (nodes[i]->name); |
| 373 | vec_free (nodes[i]->next_nodes); |
| 374 | vec_free (nodes[i]); |
| 375 | } |
| 376 | vec_free(nodes); |
Dave Barach | b44e9bc | 2016-02-19 09:06:23 -0500 | [diff] [blame] | 377 | } |
Dave Barach | 6931f59 | 2016-05-13 12:55:01 -0400 | [diff] [blame] | 378 | vec_free (nodes_by_thread); |
Dave Barach | b44e9bc | 2016-02-19 09:06:23 -0500 | [diff] [blame] | 379 | |
| 380 | return 0; |
| 381 | } |
| 382 | |
| 383 | VLIB_CLI_COMMAND (test_node_serialize_node, static) = { |
| 384 | .path = "test node serialize", |
Dave Barach | 6931f59 | 2016-05-13 12:55:01 -0400 | [diff] [blame] | 385 | .short_help = "test node serialize [max-threads NN] nexts stats", |
Dave Barach | b44e9bc | 2016-02-19 09:06:23 -0500 | [diff] [blame] | 386 | .function = test_node_serialize_command_fn, |
| 387 | }; |
| 388 | #endif |