2 * Copyright (c) 2015 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.
17 #include <vppinfra/format.h>
18 #include <vlib/vlib.h>
20 #include <vlib/threads.h>
21 #include <vlib/unix/unix.h>
24 #include <rte_config.h>
25 #include <rte_common.h>
27 #include <rte_launch.h>
28 #include <rte_lcore.h>
32 format_sched_policy_and_priority (u8 * s, va_list * args)
34 long i = va_arg (*args, long);
35 struct sched_param sched_param;
38 switch (sched_getscheduler (i))
40 #define _(v,f,str) case SCHED_POLICY_##f: t = (u8 *) str; break;
44 if (sched_getparam (i, &sched_param) == 0)
45 return format (s, "%s (%d)", t, sched_param.sched_priority);
47 return format (s, "%s (n/a)", t);
51 show_threads_fn (vlib_main_t * vm,
52 unformat_input_t * input, vlib_cli_command_t * cmd)
54 vlib_worker_thread_t *w;
57 vlib_cli_output (vm, "%-7s%-20s%-12s%-8s%-25s%-7s%-7s%-7s%-10s",
58 "ID", "Name", "Type", "LWP", "Sched Policy (Priority)",
59 "lcore", "Core", "Socket", "State");
61 #if !defined(__powerpc64__)
62 for (i = 0; i < vec_len (vlib_worker_threads); i++)
64 w = vlib_worker_threads + i;
67 line = format (line, "%-7d%-20s%-12s%-8d",
69 w->name ? w->name : (u8 *) "",
70 w->registration ? w->registration->name : "", w->lwp);
72 line = format (line, "%-25U", format_sched_policy_and_priority, w->lwp);
80 pthread_getaffinity_np (w->thread_id, sizeof (cpu_set_t), &cpuset);
84 for (c = 0; c < CPU_SETSIZE; c++)
85 if (CPU_ISSET (c, &cpuset))
102 const char *sys_cpu_path = "/sys/devices/system/cpu/cpu";
107 p = format (p, "%s%u/topology/core_id%c", sys_cpu_path, lcore, 0);
108 vlib_sysfs_read ((char *) p, "%d", &core_id);
110 vec_reset_length (p);
113 "%s%u/topology/physical_package_id%c",
114 sys_cpu_path, lcore, 0);
115 vlib_sysfs_read ((char *) p, "%d", &socket_id);
118 line = format (line, "%-7u%-7u%-7u%", lcore, core_id, socket_id);
120 ASSERT (lcore <= RTE_MAX_LCORE);
121 switch (lcore_config[lcore].state)
124 line = format (line, "wait");
127 line = format (line, "running");
130 line = format (line, "finished");
133 line = format (line, "unknown");
140 format (line, "%-7s%-7s%-7s%", (lcore == -2) ? "M" : "n/a", "n/a",
144 vlib_cli_output (vm, "%v", line);
154 VLIB_CLI_COMMAND (show_threads_command, static) = {
155 .path = "show threads",
156 .short_help = "Show threads",
157 .function = show_threads_fn,
162 * Trigger threads to grab frame queue trace data
164 static clib_error_t *
165 trace_frame_queue (vlib_main_t * vm, unformat_input_t * input,
166 vlib_cli_command_t * cmd)
168 unformat_input_t _line_input, *line_input = &_line_input;
169 clib_error_t *error = NULL;
170 frame_queue_trace_t *fqt;
171 frame_queue_nelt_counter_t *fqh;
172 vlib_thread_main_t *tm = vlib_get_thread_main ();
173 vlib_frame_queue_main_t *fqm;
177 u32 index = ~(u32) 0;
179 if (!unformat_user (input, unformat_line_input, line_input))
182 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
184 if (unformat (line_input, "on"))
186 else if (unformat (line_input, "off"))
188 else if (unformat (line_input, "index %u"), &index)
191 return clib_error_return (0, "parse error: '%U'",
192 format_unformat_error, line_input);
195 unformat_free (line_input);
198 return clib_error_return (0, "expecting on or off");
200 if (vec_len (tm->frame_queue_mains) == 0)
201 return clib_error_return (0, "no worker handoffs exist");
203 if (index > vec_len (tm->frame_queue_mains) - 1)
204 return clib_error_return (0,
205 "expecting valid worker handoff queue index");
207 fqm = vec_elt_at_index (tm->frame_queue_mains, index);
209 num_fq = vec_len (fqm->vlib_frame_queues);
212 vlib_cli_output (vm, "No frame queues exist\n");
216 // Allocate storage for trace if necessary
217 vec_validate_aligned (fqm->frame_queue_traces, num_fq - 1,
218 CLIB_CACHE_LINE_BYTES);
219 vec_validate_aligned (fqm->frame_queue_histogram, num_fq - 1,
220 CLIB_CACHE_LINE_BYTES);
222 for (fqix = 0; fqix < num_fq; fqix++)
224 fqt = &fqm->frame_queue_traces[fqix];
225 fqh = &fqm->frame_queue_histogram[fqix];
227 memset (fqt->n_vectors, 0xff, sizeof (fqt->n_vectors));
229 memset (fqh, 0, sizeof (*fqh));
230 fqm->vlib_frame_queues[fqix]->trace = enable;
236 VLIB_CLI_COMMAND (cmd_trace_frame_queue,static) = {
237 .path = "trace frame-queue",
238 .short_help = "trace frame-queue (on|off)",
239 .function = trace_frame_queue,
246 * Adding two counters and compute percent of total
247 * Round up, e.g. 0.000001 => 1%
250 compute_percent (u64 * two_counters, u64 total)
258 return (((two_counters[0] + two_counters[1]) * 100) +
259 (total - 1)) / total;
264 * Display frame queue trace data gathered by threads.
266 static clib_error_t *
267 show_frame_queue_internal (vlib_main_t * vm,
268 vlib_frame_queue_main_t * fqm, u32 histogram)
270 clib_error_t *error = NULL;
271 frame_queue_trace_t *fqt;
272 frame_queue_nelt_counter_t *fqh;
276 num_fq = vec_len (fqm->frame_queue_traces);
279 vlib_cli_output (vm, "No trace data for frame queues\n");
285 vlib_cli_output (vm, "0-1 2-3 4-5 6-7 8-9 10-11 12-13 14-15 "
286 "16-17 18-19 20-21 22-23 24-25 26-27 28-29 30-31\n");
289 for (fqix = 0; fqix < num_fq; fqix++)
291 fqt = &(fqm->frame_queue_traces[fqix]);
293 vlib_cli_output (vm, "Thread %d %v\n", fqix,
294 vlib_worker_threads[fqix].name);
296 if (fqt->written == 0)
298 vlib_cli_output (vm, " no trace data\n");
304 fqh = &(fqm->frame_queue_histogram[fqix]);
308 for (nelt = 0; nelt < FRAME_QUEUE_MAX_NELTS; nelt++)
310 total += fqh->count[nelt];
314 * Print in pairs to condense the output.
315 * Allow entries with 0 counts to be clearly identified, by rounding up.
316 * Any non-zero value will be displayed as at least one percent. This
317 * also means the sum of percentages can be > 100, but that is fine. The
318 * histogram is counted from the last time "trace frame on" was issued.
321 "%3d%% %3d%% %3d%% %3d%% %3d%% %3d%% %3d%% %3d%% "
322 "%3d%% %3d%% %3d%% %3d%% %3d%% %3d%% %3d%% %3d%%\n",
323 compute_percent (&fqh->count[0], total),
324 compute_percent (&fqh->count[2], total),
325 compute_percent (&fqh->count[4], total),
326 compute_percent (&fqh->count[6], total),
327 compute_percent (&fqh->count[8], total),
328 compute_percent (&fqh->count[10], total),
329 compute_percent (&fqh->count[12], total),
330 compute_percent (&fqh->count[14], total),
331 compute_percent (&fqh->count[16], total),
332 compute_percent (&fqh->count[18], total),
333 compute_percent (&fqh->count[20], total),
334 compute_percent (&fqh->count[22], total),
335 compute_percent (&fqh->count[24], total),
336 compute_percent (&fqh->count[26], total),
337 compute_percent (&fqh->count[28], total),
338 compute_percent (&fqh->count[30], total));
343 " vector-threshold %d ring size %d in use %d\n",
344 fqt->threshold, fqt->nelts, fqt->n_in_use);
345 vlib_cli_output (vm, " head %12d head_hint %12d tail %12d\n",
346 fqt->head, fqt->head_hint, fqt->tail);
348 " %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d\n",
349 fqt->n_vectors[0], fqt->n_vectors[1],
350 fqt->n_vectors[2], fqt->n_vectors[3],
351 fqt->n_vectors[4], fqt->n_vectors[5],
352 fqt->n_vectors[6], fqt->n_vectors[7],
353 fqt->n_vectors[8], fqt->n_vectors[9],
354 fqt->n_vectors[10], fqt->n_vectors[11],
355 fqt->n_vectors[12], fqt->n_vectors[13],
356 fqt->n_vectors[14], fqt->n_vectors[15]);
361 " %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d\n",
362 fqt->n_vectors[16], fqt->n_vectors[17],
363 fqt->n_vectors[18], fqt->n_vectors[19],
364 fqt->n_vectors[20], fqt->n_vectors[21],
365 fqt->n_vectors[22], fqt->n_vectors[23],
366 fqt->n_vectors[24], fqt->n_vectors[25],
367 fqt->n_vectors[26], fqt->n_vectors[27],
368 fqt->n_vectors[28], fqt->n_vectors[29],
369 fqt->n_vectors[30], fqt->n_vectors[31]);
377 static clib_error_t *
378 show_frame_queue_trace (vlib_main_t * vm, unformat_input_t * input,
379 vlib_cli_command_t * cmd)
381 vlib_thread_main_t *tm = vlib_get_thread_main ();
382 vlib_frame_queue_main_t *fqm;
385 vec_foreach (fqm, tm->frame_queue_mains)
387 vlib_cli_output (vm, "Worker handoff queue index %u (next node '%U'):",
388 fqm - tm->frame_queue_mains,
389 format_vlib_node_name, vm, fqm->node_index);
390 error = show_frame_queue_internal (vm, fqm, 0);
397 static clib_error_t *
398 show_frame_queue_histogram (vlib_main_t * vm, unformat_input_t * input,
399 vlib_cli_command_t * cmd)
401 vlib_thread_main_t *tm = vlib_get_thread_main ();
402 vlib_frame_queue_main_t *fqm;
405 vec_foreach (fqm, tm->frame_queue_mains)
407 vlib_cli_output (vm, "Worker handoff queue index %u (next node '%U'):",
408 fqm - tm->frame_queue_mains,
409 format_vlib_node_name, vm, fqm->node_index);
410 error = show_frame_queue_internal (vm, fqm, 1);
418 VLIB_CLI_COMMAND (cmd_show_frame_queue_trace,static) = {
419 .path = "show frame-queue",
420 .short_help = "show frame-queue trace",
421 .function = show_frame_queue_trace,
426 VLIB_CLI_COMMAND (cmd_show_frame_queue_histogram,static) = {
427 .path = "show frame-queue histogram",
428 .short_help = "show frame-queue histogram",
429 .function = show_frame_queue_histogram,
435 * Modify the number of elements on the frame_queues
437 static clib_error_t *
438 test_frame_queue_nelts (vlib_main_t * vm, unformat_input_t * input,
439 vlib_cli_command_t * cmd)
441 unformat_input_t _line_input, *line_input = &_line_input;
442 vlib_thread_main_t *tm = vlib_get_thread_main ();
443 vlib_frame_queue_main_t *fqm;
444 clib_error_t *error = NULL;
448 u32 index = ~(u32) 0;
450 if (!unformat_user (input, unformat_line_input, line_input))
453 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
455 if (unformat (line_input, "nelts %u", &nelts))
457 else if (unformat (line_input, "index %u", &index))
460 return clib_error_return (0, "parse error: '%U'",
461 format_unformat_error, line_input);
464 unformat_free (line_input);
466 if (index > vec_len (tm->frame_queue_mains) - 1)
467 return clib_error_return (0,
468 "expecting valid worker handoff queue index");
470 fqm = vec_elt_at_index (tm->frame_queue_mains, index);
472 if ((nelts != 4) && (nelts != 8) && (nelts != 16) && (nelts != 32))
474 return clib_error_return (0, "expecting 4,8,16,32");
477 num_fq = vec_len (fqm->vlib_frame_queues);
480 vlib_cli_output (vm, "No frame queues exist\n");
484 for (fqix = 0; fqix < num_fq; fqix++)
486 fqm->vlib_frame_queues[fqix]->nelts = nelts;
493 VLIB_CLI_COMMAND (cmd_test_frame_queue_nelts,static) = {
494 .path = "test frame-queue nelts",
495 .short_help = "test frame-queue nelts (4,8,16,32)",
496 .function = test_frame_queue_nelts,
502 * Modify the max number of packets pulled off the frame queues
504 static clib_error_t *
505 test_frame_queue_threshold (vlib_main_t * vm, unformat_input_t * input,
506 vlib_cli_command_t * cmd)
508 unformat_input_t _line_input, *line_input = &_line_input;
509 vlib_thread_main_t *tm = vlib_get_thread_main ();
510 vlib_frame_queue_main_t *fqm;
511 clib_error_t *error = NULL;
514 u32 threshold = ~(u32) 0;
515 u32 index = ~(u32) 0;
517 if (!unformat_user (input, unformat_line_input, line_input))
520 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
522 if (unformat (line_input, "threshold %u", &threshold))
524 else if (unformat (line_input, "index %u", &index))
527 return clib_error_return (0, "parse error: '%U'",
528 format_unformat_error, line_input);
531 unformat_free (line_input);
533 if (index > vec_len (tm->frame_queue_mains) - 1)
534 return clib_error_return (0,
535 "expecting valid worker handoff queue index");
537 fqm = vec_elt_at_index (tm->frame_queue_mains, index);
540 if (threshold == ~(u32) 0)
542 vlib_cli_output (vm, "expecting threshold value\n");
549 num_fq = vec_len (fqm->vlib_frame_queues);
552 vlib_cli_output (vm, "No frame queues exist\n");
556 for (fqix = 0; fqix < num_fq; fqix++)
558 fqm->vlib_frame_queues[fqix]->vector_threshold = threshold;
565 VLIB_CLI_COMMAND (cmd_test_frame_queue_threshold,static) = {
566 .path = "test frame-queue threshold",
567 .short_help = "test frame-queue threshold N (0=no limit)",
568 .function = test_frame_queue_threshold,
574 * fd.io coding-style-patch-verification: ON
577 * eval: (c-set-style "gnu")