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 switch (lcore_config[lcore].state)
115 line = format (line, "wait");
118 line = format (line, "running");
121 line = format (line, "finished");
124 line = format (line, "unknown");
131 format (line, "%-7s%-7s%-7s%", (lcore == -2) ? "M" : "n/a", "n/a",
135 vlib_cli_output (vm, "%v", line);
145 VLIB_CLI_COMMAND (show_threads_command, static) = {
146 .path = "show threads",
147 .short_help = "Show threads",
148 .function = show_threads_fn,
153 * Trigger threads to grab frame queue trace data
155 static clib_error_t *
156 trace_frame_queue (vlib_main_t * vm, unformat_input_t * input,
157 vlib_cli_command_t * cmd)
159 clib_error_t *error = NULL;
160 frame_queue_trace_t *fqt;
161 frame_queue_nelt_counter_t *fqh;
162 vlib_thread_main_t *tm = vlib_get_thread_main ();
167 if (unformat (input, "on"))
171 else if (unformat (input, "off"))
177 return clib_error_return (0, "expecting on or off");
180 num_fq = vec_len (vlib_frame_queues);
183 vlib_cli_output (vm, "No frame queues exist\n");
187 // Allocate storage for trace if necessary
188 vec_validate_aligned (tm->frame_queue_traces, num_fq - 1,
189 CLIB_CACHE_LINE_BYTES);
190 vec_validate_aligned (tm->frame_queue_histogram, num_fq - 1,
191 CLIB_CACHE_LINE_BYTES);
193 for (fqix = 0; fqix < num_fq; fqix++)
195 fqt = &tm->frame_queue_traces[fqix];
196 fqh = &tm->frame_queue_histogram[fqix];
198 memset (fqt->n_vectors, 0xff, sizeof (fqt->n_vectors));
200 memset (fqh, 0, sizeof (*fqh));
201 vlib_frame_queues[fqix]->trace = enable;
207 VLIB_CLI_COMMAND (cmd_trace_frame_queue,static) = {
208 .path = "trace frame-queue",
209 .short_help = "trace frame-queue (on|off)",
210 .function = trace_frame_queue,
217 * Adding two counters and compute percent of total
218 * Round up, e.g. 0.000001 => 1%
221 compute_percent (u64 * two_counters, u64 total)
229 return (((two_counters[0] + two_counters[1]) * 100) +
230 (total - 1)) / total;
235 * Display frame queue trace data gathered by threads.
237 static clib_error_t *
238 show_frame_queue_internal (vlib_main_t * vm, u32 histogram)
240 vlib_thread_main_t *tm = vlib_get_thread_main ();
241 clib_error_t *error = NULL;
242 frame_queue_trace_t *fqt;
243 frame_queue_nelt_counter_t *fqh;
247 num_fq = vec_len (tm->frame_queue_traces);
250 vlib_cli_output (vm, "No trace data for frame queues\n");
256 vlib_cli_output (vm, "0-1 2-3 4-5 6-7 8-9 10-11 12-13 14-15 "
257 "16-17 18-19 20-21 22-23 24-25 26-27 28-29 30-31\n");
260 for (fqix = 0; fqix < num_fq; fqix++)
262 fqt = &(tm->frame_queue_traces[fqix]);
264 vlib_cli_output (vm, "Thread %d %v\n", fqix,
265 vlib_worker_threads[fqix].name);
267 if (fqt->written == 0)
269 vlib_cli_output (vm, " no trace data\n");
275 fqh = &(tm->frame_queue_histogram[fqix]);
279 for (nelt = 0; nelt < FRAME_QUEUE_MAX_NELTS; nelt++)
281 total += fqh->count[nelt];
285 * Print in pairs to condense the output.
286 * Allow entries with 0 counts to be clearly identified, by rounding up.
287 * Any non-zero value will be displayed as at least one percent. This
288 * also means the sum of percentages can be > 100, but that is fine. The
289 * histogram is counted from the last time "trace frame on" was issued.
292 "%3d%% %3d%% %3d%% %3d%% %3d%% %3d%% %3d%% %3d%% "
293 "%3d%% %3d%% %3d%% %3d%% %3d%% %3d%% %3d%% %3d%%\n",
294 compute_percent (&fqh->count[0], total),
295 compute_percent (&fqh->count[2], total),
296 compute_percent (&fqh->count[4], total),
297 compute_percent (&fqh->count[6], total),
298 compute_percent (&fqh->count[8], total),
299 compute_percent (&fqh->count[10], total),
300 compute_percent (&fqh->count[12], total),
301 compute_percent (&fqh->count[14], total),
302 compute_percent (&fqh->count[16], total),
303 compute_percent (&fqh->count[18], total),
304 compute_percent (&fqh->count[20], total),
305 compute_percent (&fqh->count[22], total),
306 compute_percent (&fqh->count[24], total),
307 compute_percent (&fqh->count[26], total),
308 compute_percent (&fqh->count[28], total),
309 compute_percent (&fqh->count[30], total));
314 " vector-threshold %d ring size %d in use %d\n",
315 fqt->threshold, fqt->nelts, fqt->n_in_use);
316 vlib_cli_output (vm, " head %12d head_hint %12d tail %12d\n",
317 fqt->head, fqt->head_hint, fqt->tail);
319 " %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d\n",
320 fqt->n_vectors[0], fqt->n_vectors[1],
321 fqt->n_vectors[2], fqt->n_vectors[3],
322 fqt->n_vectors[4], fqt->n_vectors[5],
323 fqt->n_vectors[6], fqt->n_vectors[7],
324 fqt->n_vectors[8], fqt->n_vectors[9],
325 fqt->n_vectors[10], fqt->n_vectors[11],
326 fqt->n_vectors[12], fqt->n_vectors[13],
327 fqt->n_vectors[14], fqt->n_vectors[15]);
332 " %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d\n",
333 fqt->n_vectors[16], fqt->n_vectors[17],
334 fqt->n_vectors[18], fqt->n_vectors[19],
335 fqt->n_vectors[20], fqt->n_vectors[21],
336 fqt->n_vectors[22], fqt->n_vectors[23],
337 fqt->n_vectors[24], fqt->n_vectors[25],
338 fqt->n_vectors[26], fqt->n_vectors[27],
339 fqt->n_vectors[28], fqt->n_vectors[29],
340 fqt->n_vectors[30], fqt->n_vectors[31]);
348 static clib_error_t *
349 show_frame_queue_trace (vlib_main_t * vm, unformat_input_t * input,
350 vlib_cli_command_t * cmd)
352 return show_frame_queue_internal (vm, 0);
355 static clib_error_t *
356 show_frame_queue_histogram (vlib_main_t * vm, unformat_input_t * input,
357 vlib_cli_command_t * cmd)
359 return show_frame_queue_internal (vm, 1);
363 VLIB_CLI_COMMAND (cmd_show_frame_queue_trace,static) = {
364 .path = "show frame-queue",
365 .short_help = "show frame-queue trace",
366 .function = show_frame_queue_trace,
371 VLIB_CLI_COMMAND (cmd_show_frame_queue_histogram,static) = {
372 .path = "show frame-queue histogram",
373 .short_help = "show frame-queue histogram",
374 .function = show_frame_queue_histogram,
380 * Modify the number of elements on the frame_queues
382 static clib_error_t *
383 test_frame_queue_nelts (vlib_main_t * vm, unformat_input_t * input,
384 vlib_cli_command_t * cmd)
386 clib_error_t *error = NULL;
391 if ((unformat (input, "%d", &nelts) != 1) ||
392 ((nelts != 4) && (nelts != 8) && (nelts != 16) && (nelts != 32)))
394 return clib_error_return (0, "expecting 4,8,16,32");
397 num_fq = vec_len (vlib_frame_queues);
400 vlib_cli_output (vm, "No frame queues exist\n");
404 for (fqix = 0; fqix < num_fq; fqix++)
406 vlib_frame_queues[fqix]->nelts = nelts;
413 VLIB_CLI_COMMAND (cmd_test_frame_queue_nelts,static) = {
414 .path = "test frame-queue nelts",
415 .short_help = "test frame-queue nelts (4,8,16,32)",
416 .function = test_frame_queue_nelts,
422 * Modify the max number of packets pulled off the frame queues
424 static clib_error_t *
425 test_frame_queue_threshold (vlib_main_t * vm, unformat_input_t * input,
426 vlib_cli_command_t * cmd)
428 clib_error_t *error = NULL;
433 if (unformat (input, "%d", &threshold))
438 vlib_cli_output (vm, "expecting threshold value\n");
445 num_fq = vec_len (vlib_frame_queues);
448 vlib_cli_output (vm, "No frame queues exist\n");
452 for (fqix = 0; fqix < num_fq; fqix++)
454 vlib_frame_queues[fqix]->vector_threshold = threshold;
461 VLIB_CLI_COMMAND (cmd_test_frame_queue_threshold,static) = {
462 .path = "test frame-queue threshold",
463 .short_help = "test frame-queue threshold N (0=no limit)",
464 .function = test_frame_queue_threshold,
470 * fd.io coding-style-patch-verification: ON
473 * eval: (c-set-style "gnu")