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 format_sched_policy_and_priority (u8 * s, va_list * args)
26 long i = va_arg (*args, long);
27 struct sched_param sched_param;
30 switch (sched_getscheduler (i))
32 #define _(v,f,str) case SCHED_POLICY_##f: t = (u8 *) str; break;
36 if (sched_getparam (i, &sched_param) == 0)
37 return format (s, "%s (%d)", t, sched_param.sched_priority);
39 return format (s, "%s (n/a)", t);
43 show_threads_fn (vlib_main_t * vm,
44 unformat_input_t * input, vlib_cli_command_t * cmd)
46 vlib_worker_thread_t *w;
49 vlib_cli_output (vm, "%-7s%-20s%-12s%-8s%-25s%-7s%-7s%-7s%-10s",
50 "ID", "Name", "Type", "LWP", "Sched Policy (Priority)",
51 "lcore", "Core", "Socket", "State");
53 #if !defined(__powerpc64__)
54 for (i = 0; i < vec_len (vlib_worker_threads); i++)
56 w = vlib_worker_threads + i;
59 line = format (line, "%-7d%-20s%-12s%-8d",
61 w->name ? w->name : (u8 *) "",
62 w->registration ? w->registration->name : "", w->lwp);
64 line = format (line, "%-25U", format_sched_policy_and_priority, w->lwp);
72 pthread_getaffinity_np (w->thread_id, sizeof (cpu_set_t), &cpuset);
76 for (c = 0; c < CPU_SETSIZE; c++)
77 if (CPU_ISSET (c, &cpuset))
94 const char *sys_cpu_path = "/sys/devices/system/cpu/cpu";
99 p = format (p, "%s%u/topology/core_id%c", sys_cpu_path, lcore, 0);
100 vlib_sysfs_read ((char *) p, "%d", &core_id);
102 vec_reset_length (p);
105 "%s%u/topology/physical_package_id%c",
106 sys_cpu_path, lcore, 0);
107 vlib_sysfs_read ((char *) p, "%d", &socket_id);
110 line = format (line, "%-7u%-7u%-7u%", lcore, core_id, socket_id);
112 ASSERT (lcore <= RTE_MAX_LCORE);
113 switch (lcore_config[lcore].state)
116 line = format (line, "wait");
119 line = format (line, "running");
122 line = format (line, "finished");
125 line = format (line, "unknown");
132 format (line, "%-7s%-7s%-7s%", (lcore == -2) ? "M" : "n/a", "n/a",
136 vlib_cli_output (vm, "%v", line);
146 VLIB_CLI_COMMAND (show_threads_command, static) = {
147 .path = "show threads",
148 .short_help = "Show threads",
149 .function = show_threads_fn,
154 * Trigger threads to grab frame queue trace data
156 static clib_error_t *
157 trace_frame_queue (vlib_main_t * vm, unformat_input_t * input,
158 vlib_cli_command_t * cmd)
160 unformat_input_t _line_input, *line_input = &_line_input;
161 clib_error_t *error = NULL;
162 frame_queue_trace_t *fqt;
163 frame_queue_nelt_counter_t *fqh;
164 vlib_thread_main_t *tm = vlib_get_thread_main ();
165 vlib_frame_queue_main_t *fqm;
169 u32 index = ~(u32) 0;
171 if (!unformat_user (input, unformat_line_input, line_input))
174 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
176 if (unformat (line_input, "on"))
178 else if (unformat (line_input, "off"))
180 else if (unformat (line_input, "index %u"), &index)
183 return clib_error_return (0, "parse error: '%U'",
184 format_unformat_error, line_input);
187 unformat_free (line_input);
190 return clib_error_return (0, "expecting on or off");
192 if (vec_len (tm->frame_queue_mains) == 0)
193 return clib_error_return (0, "no worker handoffs exist");
195 if (index > vec_len (tm->frame_queue_mains) - 1)
196 return clib_error_return (0,
197 "expecting valid worker handoff queue index");
199 fqm = vec_elt_at_index (tm->frame_queue_mains, index);
201 num_fq = vec_len (fqm->vlib_frame_queues);
204 vlib_cli_output (vm, "No frame queues exist\n");
208 // Allocate storage for trace if necessary
209 vec_validate_aligned (fqm->frame_queue_traces, num_fq - 1,
210 CLIB_CACHE_LINE_BYTES);
211 vec_validate_aligned (fqm->frame_queue_histogram, num_fq - 1,
212 CLIB_CACHE_LINE_BYTES);
214 for (fqix = 0; fqix < num_fq; fqix++)
216 fqt = &fqm->frame_queue_traces[fqix];
217 fqh = &fqm->frame_queue_histogram[fqix];
219 memset (fqt->n_vectors, 0xff, sizeof (fqt->n_vectors));
221 memset (fqh, 0, sizeof (*fqh));
222 fqm->vlib_frame_queues[fqix]->trace = enable;
228 VLIB_CLI_COMMAND (cmd_trace_frame_queue,static) = {
229 .path = "trace frame-queue",
230 .short_help = "trace frame-queue (on|off)",
231 .function = trace_frame_queue,
238 * Adding two counters and compute percent of total
239 * Round up, e.g. 0.000001 => 1%
242 compute_percent (u64 * two_counters, u64 total)
250 return (((two_counters[0] + two_counters[1]) * 100) +
251 (total - 1)) / total;
256 * Display frame queue trace data gathered by threads.
258 static clib_error_t *
259 show_frame_queue_internal (vlib_main_t * vm,
260 vlib_frame_queue_main_t * fqm, u32 histogram)
262 clib_error_t *error = NULL;
263 frame_queue_trace_t *fqt;
264 frame_queue_nelt_counter_t *fqh;
268 num_fq = vec_len (fqm->frame_queue_traces);
271 vlib_cli_output (vm, "No trace data for frame queues\n");
277 vlib_cli_output (vm, "0-1 2-3 4-5 6-7 8-9 10-11 12-13 14-15 "
278 "16-17 18-19 20-21 22-23 24-25 26-27 28-29 30-31\n");
281 for (fqix = 0; fqix < num_fq; fqix++)
283 fqt = &(fqm->frame_queue_traces[fqix]);
285 vlib_cli_output (vm, "Thread %d %v\n", fqix,
286 vlib_worker_threads[fqix].name);
288 if (fqt->written == 0)
290 vlib_cli_output (vm, " no trace data\n");
296 fqh = &(fqm->frame_queue_histogram[fqix]);
300 for (nelt = 0; nelt < FRAME_QUEUE_MAX_NELTS; nelt++)
302 total += fqh->count[nelt];
306 * Print in pairs to condense the output.
307 * Allow entries with 0 counts to be clearly identified, by rounding up.
308 * Any non-zero value will be displayed as at least one percent. This
309 * also means the sum of percentages can be > 100, but that is fine. The
310 * histogram is counted from the last time "trace frame on" was issued.
313 "%3d%% %3d%% %3d%% %3d%% %3d%% %3d%% %3d%% %3d%% "
314 "%3d%% %3d%% %3d%% %3d%% %3d%% %3d%% %3d%% %3d%%\n",
315 compute_percent (&fqh->count[0], total),
316 compute_percent (&fqh->count[2], total),
317 compute_percent (&fqh->count[4], total),
318 compute_percent (&fqh->count[6], total),
319 compute_percent (&fqh->count[8], total),
320 compute_percent (&fqh->count[10], total),
321 compute_percent (&fqh->count[12], total),
322 compute_percent (&fqh->count[14], total),
323 compute_percent (&fqh->count[16], total),
324 compute_percent (&fqh->count[18], total),
325 compute_percent (&fqh->count[20], total),
326 compute_percent (&fqh->count[22], total),
327 compute_percent (&fqh->count[24], total),
328 compute_percent (&fqh->count[26], total),
329 compute_percent (&fqh->count[28], total),
330 compute_percent (&fqh->count[30], total));
335 " vector-threshold %d ring size %d in use %d\n",
336 fqt->threshold, fqt->nelts, fqt->n_in_use);
337 vlib_cli_output (vm, " head %12d head_hint %12d tail %12d\n",
338 fqt->head, fqt->head_hint, fqt->tail);
340 " %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d\n",
341 fqt->n_vectors[0], fqt->n_vectors[1],
342 fqt->n_vectors[2], fqt->n_vectors[3],
343 fqt->n_vectors[4], fqt->n_vectors[5],
344 fqt->n_vectors[6], fqt->n_vectors[7],
345 fqt->n_vectors[8], fqt->n_vectors[9],
346 fqt->n_vectors[10], fqt->n_vectors[11],
347 fqt->n_vectors[12], fqt->n_vectors[13],
348 fqt->n_vectors[14], fqt->n_vectors[15]);
353 " %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d\n",
354 fqt->n_vectors[16], fqt->n_vectors[17],
355 fqt->n_vectors[18], fqt->n_vectors[19],
356 fqt->n_vectors[20], fqt->n_vectors[21],
357 fqt->n_vectors[22], fqt->n_vectors[23],
358 fqt->n_vectors[24], fqt->n_vectors[25],
359 fqt->n_vectors[26], fqt->n_vectors[27],
360 fqt->n_vectors[28], fqt->n_vectors[29],
361 fqt->n_vectors[30], fqt->n_vectors[31]);
369 static clib_error_t *
370 show_frame_queue_trace (vlib_main_t * vm, unformat_input_t * input,
371 vlib_cli_command_t * cmd)
373 vlib_thread_main_t *tm = vlib_get_thread_main ();
374 vlib_frame_queue_main_t *fqm;
377 vec_foreach (fqm, tm->frame_queue_mains)
379 vlib_cli_output (vm, "Worker handoff queue index %u (next node '%U'):",
380 fqm - tm->frame_queue_mains,
381 format_vlib_node_name, vm, fqm->node_index);
382 error = show_frame_queue_internal (vm, fqm, 0);
389 static clib_error_t *
390 show_frame_queue_histogram (vlib_main_t * vm, unformat_input_t * input,
391 vlib_cli_command_t * cmd)
393 vlib_thread_main_t *tm = vlib_get_thread_main ();
394 vlib_frame_queue_main_t *fqm;
397 vec_foreach (fqm, tm->frame_queue_mains)
399 vlib_cli_output (vm, "Worker handoff queue index %u (next node '%U'):",
400 fqm - tm->frame_queue_mains,
401 format_vlib_node_name, vm, fqm->node_index);
402 error = show_frame_queue_internal (vm, fqm, 1);
410 VLIB_CLI_COMMAND (cmd_show_frame_queue_trace,static) = {
411 .path = "show frame-queue",
412 .short_help = "show frame-queue trace",
413 .function = show_frame_queue_trace,
418 VLIB_CLI_COMMAND (cmd_show_frame_queue_histogram,static) = {
419 .path = "show frame-queue histogram",
420 .short_help = "show frame-queue histogram",
421 .function = show_frame_queue_histogram,
427 * Modify the number of elements on the frame_queues
429 static clib_error_t *
430 test_frame_queue_nelts (vlib_main_t * vm, unformat_input_t * input,
431 vlib_cli_command_t * cmd)
433 unformat_input_t _line_input, *line_input = &_line_input;
434 vlib_thread_main_t *tm = vlib_get_thread_main ();
435 vlib_frame_queue_main_t *fqm;
436 clib_error_t *error = NULL;
440 u32 index = ~(u32) 0;
442 if (!unformat_user (input, unformat_line_input, line_input))
445 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
447 if (unformat (line_input, "nelts %u"), &nelts)
449 else if (unformat (line_input, "index %u"), &index)
452 return clib_error_return (0, "parse error: '%U'",
453 format_unformat_error, line_input);
456 unformat_free (line_input);
458 if (index > vec_len (tm->frame_queue_mains) - 1)
459 return clib_error_return (0,
460 "expecting valid worker handoff queue index");
462 fqm = vec_elt_at_index (tm->frame_queue_mains, index);
464 if ((nelts != 4) && (nelts != 8) && (nelts != 16) && (nelts != 32))
466 return clib_error_return (0, "expecting 4,8,16,32");
469 num_fq = vec_len (fqm->vlib_frame_queues);
472 vlib_cli_output (vm, "No frame queues exist\n");
476 for (fqix = 0; fqix < num_fq; fqix++)
478 fqm->vlib_frame_queues[fqix]->nelts = nelts;
485 VLIB_CLI_COMMAND (cmd_test_frame_queue_nelts,static) = {
486 .path = "test frame-queue nelts",
487 .short_help = "test frame-queue nelts (4,8,16,32)",
488 .function = test_frame_queue_nelts,
494 * Modify the max number of packets pulled off the frame queues
496 static clib_error_t *
497 test_frame_queue_threshold (vlib_main_t * vm, unformat_input_t * input,
498 vlib_cli_command_t * cmd)
500 unformat_input_t _line_input, *line_input = &_line_input;
501 vlib_thread_main_t *tm = vlib_get_thread_main ();
502 vlib_frame_queue_main_t *fqm;
503 clib_error_t *error = NULL;
506 u32 threshold = ~(u32) 0;
507 u32 index = ~(u32) 0;
509 if (!unformat_user (input, unformat_line_input, line_input))
512 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
514 if (unformat (line_input, "threshold %u"), &threshold)
516 else if (unformat (line_input, "index %u"), &index)
519 return clib_error_return (0, "parse error: '%U'",
520 format_unformat_error, line_input);
523 unformat_free (line_input);
525 if (index > vec_len (tm->frame_queue_mains) - 1)
526 return clib_error_return (0,
527 "expecting valid worker handoff queue index");
529 fqm = vec_elt_at_index (tm->frame_queue_mains, index);
532 if (threshold == ~(u32) 0)
534 vlib_cli_output (vm, "expecting threshold value\n");
541 num_fq = vec_len (fqm->vlib_frame_queues);
544 vlib_cli_output (vm, "No frame queues exist\n");
548 for (fqix = 0; fqix < num_fq; fqix++)
550 fqm->vlib_frame_queues[fqix]->vector_threshold = threshold;
557 VLIB_CLI_COMMAND (cmd_test_frame_queue_threshold,static) = {
558 .path = "test frame-queue threshold",
559 .short_help = "test frame-queue threshold N (0=no limit)",
560 .function = test_frame_queue_threshold,
566 * fd.io coding-style-patch-verification: ON
569 * eval: (c-set-style "gnu")