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);
66 int cpu_id = w->cpu_id;
69 int core_id = w->core_id;
70 int socket_id = w->socket_id;
71 line = format (line, "%-7u%-7u%-7u%", cpu_id, core_id, socket_id);
75 line = format (line, "%-7s%-7s%-7s%", "n/a", "n/a", "n/a");
78 vlib_cli_output (vm, "%v", line);
88 VLIB_CLI_COMMAND (show_threads_command, static) = {
89 .path = "show threads",
90 .short_help = "Show threads",
91 .function = show_threads_fn,
96 * Trigger threads to grab frame queue trace data
99 trace_frame_queue (vlib_main_t * vm, unformat_input_t * input,
100 vlib_cli_command_t * cmd)
102 unformat_input_t _line_input, *line_input = &_line_input;
103 clib_error_t *error = NULL;
104 frame_queue_trace_t *fqt;
105 frame_queue_nelt_counter_t *fqh;
106 vlib_thread_main_t *tm = vlib_get_thread_main ();
107 vlib_frame_queue_main_t *fqm;
111 u32 index = ~(u32) 0;
113 if (!unformat_user (input, unformat_line_input, line_input))
116 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
118 if (unformat (line_input, "on"))
120 else if (unformat (line_input, "off"))
122 else if (unformat (line_input, "index %u", &index))
126 error = clib_error_return (0, "parse error: '%U'",
127 format_unformat_error, line_input);
134 error = clib_error_return (0, "expecting on or off");
138 if (vec_len (tm->frame_queue_mains) == 0)
140 error = clib_error_return (0, "no worker handoffs exist");
144 if (index > vec_len (tm->frame_queue_mains) - 1)
146 error = clib_error_return (0,
147 "expecting valid worker handoff queue index");
151 fqm = vec_elt_at_index (tm->frame_queue_mains, index);
153 num_fq = vec_len (fqm->vlib_frame_queues);
156 vlib_cli_output (vm, "No frame queues exist\n");
160 // Allocate storage for trace if necessary
161 vec_validate_aligned (fqm->frame_queue_traces, num_fq - 1,
162 CLIB_CACHE_LINE_BYTES);
163 vec_validate_aligned (fqm->frame_queue_histogram, num_fq - 1,
164 CLIB_CACHE_LINE_BYTES);
166 for (fqix = 0; fqix < num_fq; fqix++)
168 fqt = &fqm->frame_queue_traces[fqix];
169 fqh = &fqm->frame_queue_histogram[fqix];
171 memset (fqt->n_vectors, 0xff, sizeof (fqt->n_vectors));
173 memset (fqh, 0, sizeof (*fqh));
174 fqm->vlib_frame_queues[fqix]->trace = enable;
178 unformat_free (line_input);
184 VLIB_CLI_COMMAND (cmd_trace_frame_queue,static) = {
185 .path = "trace frame-queue",
186 .short_help = "trace frame-queue (on|off)",
187 .function = trace_frame_queue,
194 * Adding two counters and compute percent of total
195 * Round up, e.g. 0.000001 => 1%
198 compute_percent (u64 * two_counters, u64 total)
206 return (((two_counters[0] + two_counters[1]) * 100) +
207 (total - 1)) / total;
212 * Display frame queue trace data gathered by threads.
214 static clib_error_t *
215 show_frame_queue_internal (vlib_main_t * vm,
216 vlib_frame_queue_main_t * fqm, u32 histogram)
218 clib_error_t *error = NULL;
219 frame_queue_trace_t *fqt;
220 frame_queue_nelt_counter_t *fqh;
224 num_fq = vec_len (fqm->frame_queue_traces);
227 vlib_cli_output (vm, "No trace data for frame queues\n");
233 vlib_cli_output (vm, "0-1 2-3 4-5 6-7 8-9 10-11 12-13 14-15 "
234 "16-17 18-19 20-21 22-23 24-25 26-27 28-29 30-31\n");
237 for (fqix = 0; fqix < num_fq; fqix++)
239 fqt = &(fqm->frame_queue_traces[fqix]);
241 vlib_cli_output (vm, "Thread %d %v\n", fqix,
242 vlib_worker_threads[fqix].name);
244 if (fqt->written == 0)
246 vlib_cli_output (vm, " no trace data\n");
252 fqh = &(fqm->frame_queue_histogram[fqix]);
256 for (nelt = 0; nelt < FRAME_QUEUE_MAX_NELTS; nelt++)
258 total += fqh->count[nelt];
262 * Print in pairs to condense the output.
263 * Allow entries with 0 counts to be clearly identified, by rounding up.
264 * Any non-zero value will be displayed as at least one percent. This
265 * also means the sum of percentages can be > 100, but that is fine. The
266 * histogram is counted from the last time "trace frame on" was issued.
269 "%3d%% %3d%% %3d%% %3d%% %3d%% %3d%% %3d%% %3d%% "
270 "%3d%% %3d%% %3d%% %3d%% %3d%% %3d%% %3d%% %3d%%\n",
271 compute_percent (&fqh->count[0], total),
272 compute_percent (&fqh->count[2], total),
273 compute_percent (&fqh->count[4], total),
274 compute_percent (&fqh->count[6], total),
275 compute_percent (&fqh->count[8], total),
276 compute_percent (&fqh->count[10], total),
277 compute_percent (&fqh->count[12], total),
278 compute_percent (&fqh->count[14], total),
279 compute_percent (&fqh->count[16], total),
280 compute_percent (&fqh->count[18], total),
281 compute_percent (&fqh->count[20], total),
282 compute_percent (&fqh->count[22], total),
283 compute_percent (&fqh->count[24], total),
284 compute_percent (&fqh->count[26], total),
285 compute_percent (&fqh->count[28], total),
286 compute_percent (&fqh->count[30], total));
291 " vector-threshold %d ring size %d in use %d\n",
292 fqt->threshold, fqt->nelts, fqt->n_in_use);
293 vlib_cli_output (vm, " head %12d head_hint %12d tail %12d\n",
294 fqt->head, fqt->head_hint, fqt->tail);
296 " %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d\n",
297 fqt->n_vectors[0], fqt->n_vectors[1],
298 fqt->n_vectors[2], fqt->n_vectors[3],
299 fqt->n_vectors[4], fqt->n_vectors[5],
300 fqt->n_vectors[6], fqt->n_vectors[7],
301 fqt->n_vectors[8], fqt->n_vectors[9],
302 fqt->n_vectors[10], fqt->n_vectors[11],
303 fqt->n_vectors[12], fqt->n_vectors[13],
304 fqt->n_vectors[14], fqt->n_vectors[15]);
309 " %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d\n",
310 fqt->n_vectors[16], fqt->n_vectors[17],
311 fqt->n_vectors[18], fqt->n_vectors[19],
312 fqt->n_vectors[20], fqt->n_vectors[21],
313 fqt->n_vectors[22], fqt->n_vectors[23],
314 fqt->n_vectors[24], fqt->n_vectors[25],
315 fqt->n_vectors[26], fqt->n_vectors[27],
316 fqt->n_vectors[28], fqt->n_vectors[29],
317 fqt->n_vectors[30], fqt->n_vectors[31]);
325 static clib_error_t *
326 show_frame_queue_trace (vlib_main_t * vm, unformat_input_t * input,
327 vlib_cli_command_t * cmd)
329 vlib_thread_main_t *tm = vlib_get_thread_main ();
330 vlib_frame_queue_main_t *fqm;
333 vec_foreach (fqm, tm->frame_queue_mains)
335 vlib_cli_output (vm, "Worker handoff queue index %u (next node '%U'):",
336 fqm - tm->frame_queue_mains,
337 format_vlib_node_name, vm, fqm->node_index);
338 error = show_frame_queue_internal (vm, fqm, 0);
345 static clib_error_t *
346 show_frame_queue_histogram (vlib_main_t * vm, unformat_input_t * input,
347 vlib_cli_command_t * cmd)
349 vlib_thread_main_t *tm = vlib_get_thread_main ();
350 vlib_frame_queue_main_t *fqm;
353 vec_foreach (fqm, tm->frame_queue_mains)
355 vlib_cli_output (vm, "Worker handoff queue index %u (next node '%U'):",
356 fqm - tm->frame_queue_mains,
357 format_vlib_node_name, vm, fqm->node_index);
358 error = show_frame_queue_internal (vm, fqm, 1);
366 VLIB_CLI_COMMAND (cmd_show_frame_queue_trace,static) = {
367 .path = "show frame-queue",
368 .short_help = "show frame-queue trace",
369 .function = show_frame_queue_trace,
374 VLIB_CLI_COMMAND (cmd_show_frame_queue_histogram,static) = {
375 .path = "show frame-queue histogram",
376 .short_help = "show frame-queue histogram",
377 .function = show_frame_queue_histogram,
383 * Modify the number of elements on the frame_queues
385 static clib_error_t *
386 test_frame_queue_nelts (vlib_main_t * vm, unformat_input_t * input,
387 vlib_cli_command_t * cmd)
389 unformat_input_t _line_input, *line_input = &_line_input;
390 vlib_thread_main_t *tm = vlib_get_thread_main ();
391 vlib_frame_queue_main_t *fqm;
392 clib_error_t *error = NULL;
396 u32 index = ~(u32) 0;
398 if (!unformat_user (input, unformat_line_input, line_input))
401 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
403 if (unformat (line_input, "nelts %u", &nelts))
405 else if (unformat (line_input, "index %u", &index))
409 error = clib_error_return (0, "parse error: '%U'",
410 format_unformat_error, line_input);
415 if (index > vec_len (tm->frame_queue_mains) - 1)
417 error = clib_error_return (0,
418 "expecting valid worker handoff queue index");
422 fqm = vec_elt_at_index (tm->frame_queue_mains, index);
424 if ((nelts != 4) && (nelts != 8) && (nelts != 16) && (nelts != 32))
426 error = clib_error_return (0, "expecting 4,8,16,32");
430 num_fq = vec_len (fqm->vlib_frame_queues);
433 vlib_cli_output (vm, "No frame queues exist\n");
437 for (fqix = 0; fqix < num_fq; fqix++)
439 fqm->vlib_frame_queues[fqix]->nelts = nelts;
443 unformat_free (line_input);
449 VLIB_CLI_COMMAND (cmd_test_frame_queue_nelts,static) = {
450 .path = "test frame-queue nelts",
451 .short_help = "test frame-queue nelts (4,8,16,32)",
452 .function = test_frame_queue_nelts,
458 * Modify the max number of packets pulled off the frame queues
460 static clib_error_t *
461 test_frame_queue_threshold (vlib_main_t * vm, unformat_input_t * input,
462 vlib_cli_command_t * cmd)
464 unformat_input_t _line_input, *line_input = &_line_input;
465 vlib_thread_main_t *tm = vlib_get_thread_main ();
466 vlib_frame_queue_main_t *fqm;
467 clib_error_t *error = NULL;
470 u32 threshold = ~(u32) 0;
471 u32 index = ~(u32) 0;
473 if (!unformat_user (input, unformat_line_input, line_input))
476 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
478 if (unformat (line_input, "threshold %u", &threshold))
480 else if (unformat (line_input, "index %u", &index))
484 error = clib_error_return (0, "parse error: '%U'",
485 format_unformat_error, line_input);
490 if (index > vec_len (tm->frame_queue_mains) - 1)
492 error = clib_error_return (0,
493 "expecting valid worker handoff queue index");
497 fqm = vec_elt_at_index (tm->frame_queue_mains, index);
500 if (threshold == ~(u32) 0)
502 vlib_cli_output (vm, "expecting threshold value\n");
509 num_fq = vec_len (fqm->vlib_frame_queues);
512 vlib_cli_output (vm, "No frame queues exist\n");
516 for (fqix = 0; fqix < num_fq; fqix++)
518 fqm->vlib_frame_queues[fqix]->vector_threshold = threshold;
522 unformat_free (line_input);
528 VLIB_CLI_COMMAND (cmd_test_frame_queue_threshold,static) = {
529 .path = "test frame-queue threshold",
530 .short_help = "test frame-queue threshold N (0=no limit)",
531 .function = test_frame_queue_threshold,
536 * fd.io coding-style-patch-verification: ON
539 * eval: (c-set-style "gnu")