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.
16 #include <vppinfra/format.h>
17 #include <vlib/vlib.h>
19 #include <vlib/threads.h>
20 #include <linux/sched.h>
23 format_sched_policy_and_priority (u8 * s, va_list * args)
25 long i = va_arg (*args, long);
26 struct sched_param sched_param;
29 switch (sched_getscheduler (i))
31 #define _(v,f,str) case SCHED_POLICY_##f: t = (u8 *) str; break;
35 if (sched_getparam (i, &sched_param) == 0)
36 return format (s, "%s (%d)", t, sched_param.sched_priority);
38 return format (s, "%s (n/a)", t);
42 show_threads_fn (vlib_main_t * vm,
43 unformat_input_t * input, vlib_cli_command_t * cmd)
45 vlib_worker_thread_t *w;
48 vlib_cli_output (vm, "%-7s%-20s%-12s%-8s%-25s%-7s%-7s%-7s%-10s",
49 "ID", "Name", "Type", "LWP", "Sched Policy (Priority)",
50 "lcore", "Core", "Socket", "State");
52 #if !defined(__powerpc64__)
53 for (i = 0; i < vec_len (vlib_worker_threads); i++)
55 w = vlib_worker_threads + i;
58 line = format (line, "%-7d%-20s%-12s%-8d",
60 w->name ? w->name : (u8 *) "",
61 w->registration ? w->registration->name : "", w->lwp);
63 line = format (line, "%-25U", format_sched_policy_and_priority, w->lwp);
66 int lcore = w->dpdk_lcore_id;
69 line = format (line, "%-7u%-7u%-7u",
71 lcore_config[lcore].core_id,
72 lcore_config[lcore].socket_id);
74 switch (lcore_config[lcore].state)
77 line = format (line, "wait");
80 line = format (line, "running");
83 line = format (line, "finished");
86 line = format (line, "unknown");
90 vlib_cli_output (vm, "%v", line);
100 VLIB_CLI_COMMAND (show_threads_command, static) = {
101 .path = "show threads",
102 .short_help = "Show threads",
103 .function = show_threads_fn,
108 * Trigger threads to grab frame queue trace data
110 static clib_error_t *
111 trace_frame_queue (vlib_main_t * vm, unformat_input_t * input,
112 vlib_cli_command_t * cmd)
114 clib_error_t *error = NULL;
115 frame_queue_trace_t *fqt;
116 frame_queue_nelt_counter_t *fqh;
117 vlib_thread_main_t *tm = vlib_get_thread_main ();
122 if (unformat (input, "on"))
126 else if (unformat (input, "off"))
132 return clib_error_return (0, "expecting on or off");
135 num_fq = vec_len (vlib_frame_queues);
138 vlib_cli_output (vm, "No frame queues exist\n");
142 // Allocate storage for trace if necessary
143 vec_validate_aligned (tm->frame_queue_traces, num_fq - 1,
144 CLIB_CACHE_LINE_BYTES);
145 vec_validate_aligned (tm->frame_queue_histogram, num_fq - 1,
146 CLIB_CACHE_LINE_BYTES);
148 for (fqix = 0; fqix < num_fq; fqix++)
150 fqt = &tm->frame_queue_traces[fqix];
151 fqh = &tm->frame_queue_histogram[fqix];
153 memset (fqt->n_vectors, 0xff, sizeof (fqt->n_vectors));
155 memset (fqh, 0, sizeof (*fqh));
156 vlib_frame_queues[fqix]->trace = enable;
162 VLIB_CLI_COMMAND (cmd_trace_frame_queue,static) = {
163 .path = "trace frame-queue",
164 .short_help = "trace frame-queue (on|off)",
165 .function = trace_frame_queue,
172 * Adding two counters and compute percent of total
173 * Round up, e.g. 0.000001 => 1%
176 compute_percent (u64 * two_counters, u64 total)
184 return (((two_counters[0] + two_counters[1]) * 100) +
185 (total - 1)) / total;
190 * Display frame queue trace data gathered by threads.
192 static clib_error_t *
193 show_frame_queue_internal (vlib_main_t * vm, u32 histogram)
195 vlib_thread_main_t *tm = vlib_get_thread_main ();
196 clib_error_t *error = NULL;
197 frame_queue_trace_t *fqt;
198 frame_queue_nelt_counter_t *fqh;
202 num_fq = vec_len (tm->frame_queue_traces);
205 vlib_cli_output (vm, "No trace data for frame queues\n");
211 vlib_cli_output (vm, "0-1 2-3 4-5 6-7 8-9 10-11 12-13 14-15 "
212 "16-17 18-19 20-21 22-23 24-25 26-27 28-29 30-31\n");
215 for (fqix = 0; fqix < num_fq; fqix++)
217 fqt = &(tm->frame_queue_traces[fqix]);
219 vlib_cli_output (vm, "Thread %d %v\n", fqix,
220 vlib_worker_threads[fqix].name);
222 if (fqt->written == 0)
224 vlib_cli_output (vm, " no trace data\n");
230 fqh = &(tm->frame_queue_histogram[fqix]);
234 for (nelt = 0; nelt < FRAME_QUEUE_MAX_NELTS; nelt++)
236 total += fqh->count[nelt];
240 * Print in pairs to condense the output.
241 * Allow entries with 0 counts to be clearly identified, by rounding up.
242 * Any non-zero value will be displayed as at least one percent. This
243 * also means the sum of percentages can be > 100, but that is fine. The
244 * histogram is counted from the last time "trace frame on" was issued.
247 "%3d%% %3d%% %3d%% %3d%% %3d%% %3d%% %3d%% %3d%% "
248 "%3d%% %3d%% %3d%% %3d%% %3d%% %3d%% %3d%% %3d%%\n",
249 compute_percent (&fqh->count[0], total),
250 compute_percent (&fqh->count[2], total),
251 compute_percent (&fqh->count[4], total),
252 compute_percent (&fqh->count[6], total),
253 compute_percent (&fqh->count[8], total),
254 compute_percent (&fqh->count[10], total),
255 compute_percent (&fqh->count[12], total),
256 compute_percent (&fqh->count[14], total),
257 compute_percent (&fqh->count[16], total),
258 compute_percent (&fqh->count[18], total),
259 compute_percent (&fqh->count[20], total),
260 compute_percent (&fqh->count[22], total),
261 compute_percent (&fqh->count[24], total),
262 compute_percent (&fqh->count[26], total),
263 compute_percent (&fqh->count[28], total),
264 compute_percent (&fqh->count[30], total));
269 " vector-threshold %d ring size %d in use %d\n",
270 fqt->threshold, fqt->nelts, fqt->n_in_use);
271 vlib_cli_output (vm, " head %12d head_hint %12d tail %12d\n",
272 fqt->head, fqt->head_hint, fqt->tail);
274 " %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d\n",
275 fqt->n_vectors[0], fqt->n_vectors[1],
276 fqt->n_vectors[2], fqt->n_vectors[3],
277 fqt->n_vectors[4], fqt->n_vectors[5],
278 fqt->n_vectors[6], fqt->n_vectors[7],
279 fqt->n_vectors[8], fqt->n_vectors[9],
280 fqt->n_vectors[10], fqt->n_vectors[11],
281 fqt->n_vectors[12], fqt->n_vectors[13],
282 fqt->n_vectors[14], fqt->n_vectors[15]);
287 " %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d\n",
288 fqt->n_vectors[16], fqt->n_vectors[17],
289 fqt->n_vectors[18], fqt->n_vectors[19],
290 fqt->n_vectors[20], fqt->n_vectors[21],
291 fqt->n_vectors[22], fqt->n_vectors[23],
292 fqt->n_vectors[24], fqt->n_vectors[25],
293 fqt->n_vectors[26], fqt->n_vectors[27],
294 fqt->n_vectors[28], fqt->n_vectors[29],
295 fqt->n_vectors[30], fqt->n_vectors[31]);
303 static clib_error_t *
304 show_frame_queue_trace (vlib_main_t * vm, unformat_input_t * input,
305 vlib_cli_command_t * cmd)
307 return show_frame_queue_internal (vm, 0);
310 static clib_error_t *
311 show_frame_queue_histogram (vlib_main_t * vm, unformat_input_t * input,
312 vlib_cli_command_t * cmd)
314 return show_frame_queue_internal (vm, 1);
318 VLIB_CLI_COMMAND (cmd_show_frame_queue_trace,static) = {
319 .path = "show frame-queue",
320 .short_help = "show frame-queue trace",
321 .function = show_frame_queue_trace,
326 VLIB_CLI_COMMAND (cmd_show_frame_queue_histogram,static) = {
327 .path = "show frame-queue histogram",
328 .short_help = "show frame-queue histogram",
329 .function = show_frame_queue_histogram,
335 * Modify the number of elements on the frame_queues
337 static clib_error_t *
338 test_frame_queue_nelts (vlib_main_t * vm, unformat_input_t * input,
339 vlib_cli_command_t * cmd)
341 clib_error_t *error = NULL;
346 if ((unformat (input, "%d", &nelts) != 1) ||
347 ((nelts != 4) && (nelts != 8) && (nelts != 16) && (nelts != 32)))
349 return clib_error_return (0, "expecting 4,8,16,32");
352 num_fq = vec_len (vlib_frame_queues);
355 vlib_cli_output (vm, "No frame queues exist\n");
359 for (fqix = 0; fqix < num_fq; fqix++)
361 vlib_frame_queues[fqix]->nelts = nelts;
368 VLIB_CLI_COMMAND (cmd_test_frame_queue_nelts,static) = {
369 .path = "test frame-queue nelts",
370 .short_help = "test frame-queue nelts (4,8,16,32)",
371 .function = test_frame_queue_nelts,
377 * Modify the max number of packets pulled off the frame queues
379 static clib_error_t *
380 test_frame_queue_threshold (vlib_main_t * vm, unformat_input_t * input,
381 vlib_cli_command_t * cmd)
383 clib_error_t *error = NULL;
388 if (unformat (input, "%d", &threshold))
393 vlib_cli_output (vm, "expecting threshold value\n");
400 num_fq = vec_len (vlib_frame_queues);
403 vlib_cli_output (vm, "No frame queues exist\n");
407 for (fqix = 0; fqix < num_fq; fqix++)
409 vlib_frame_queues[fqix]->vector_threshold = threshold;
416 VLIB_CLI_COMMAND (cmd_test_frame_queue_threshold,static) = {
417 .path = "test frame-queue threshold",
418 .short_help = "test frame-queue threshold N (0=no limit)",
419 .function = test_frame_queue_threshold,
425 * fd.io coding-style-patch-verification: ON
428 * eval: (c-set-style "gnu")