26cb2213d95334af88b98b13c35ae0afe325797b
[vpp.git] / vlib / vlib / threads_cli.c
1 /*
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 #include <vppinfra/format.h>
17 #include <vlib/vlib.h>
18
19 #include <vlib/threads.h>
20
21 static clib_error_t *
22 show_threads_fn (vlib_main_t * vm,
23        unformat_input_t * input,
24        vlib_cli_command_t * cmd)
25 {
26   vlib_worker_thread_t * w;
27   int i;
28
29   vlib_cli_output (vm, "%-7s%-20s%-12s%-8s%-7s%-7s%-7s%-10s",
30                    "ID", "Name", "Type", "LWP",
31                    "lcore", "Core", "Socket", "State");
32
33 #if !defined(__powerpc64__)
34   for (i = 0; i < vec_len(vlib_worker_threads); i++)
35     {
36       w = vlib_worker_threads + i;
37       u8 * line = NULL;
38
39       line = format(line, "%-7d%-20s%-12s%-8d",
40                     i,
41                     w->name ? w->name : (u8 *) "",
42                     w->registration ? w->registration->name : "",
43                     w->lwp);
44
45 #if DPDK==1
46       int lcore = w->dpdk_lcore_id;
47       if (lcore > -1)
48         {
49           line = format(line, "%-7u%-7u%-7u",
50                         lcore,
51                         lcore_config[lcore].core_id,
52                         lcore_config[lcore].socket_id);
53
54           switch(lcore_config[lcore].state)
55             {
56               case WAIT:
57                 line = format(line, "wait");
58                 break;
59               case RUNNING:
60                 line = format(line, "running");
61                 break;
62               case FINISHED:
63                 line = format(line, "finished");
64                 break;
65               default:
66                 line = format(line, "unknown");
67             }
68         }
69 #endif
70       vlib_cli_output(vm, "%v", line);
71       vec_free(line);
72     }
73 #endif
74
75   return 0;
76 }
77
78
79 VLIB_CLI_COMMAND (show_threads_command, static) = {
80   .path = "show threads",
81   .short_help = "Show threads",
82   .function = show_threads_fn,
83 };
84
85 /*
86  * Trigger threads to grab frame queue trace data
87  */
88 static clib_error_t *
89 trace_frame_queue (vlib_main_t *vm, unformat_input_t *input,
90                   vlib_cli_command_t *cmd)
91 {
92   clib_error_t * error = NULL;
93   frame_queue_trace_t *fqt;
94   frame_queue_nelt_counter_t *fqh;
95   vlib_thread_main_t *tm = vlib_get_thread_main();
96   u32 num_fq;
97   u32 fqix;
98   u32 enable = 0;
99
100   if (unformat(input, "on")) {
101     enable = 1;
102   } else if (unformat(input, "off")) {
103     enable = 0;
104   } else {
105       return clib_error_return(0, "expecting on or off");
106   }
107
108   num_fq = vec_len(vlib_frame_queues);
109   if (num_fq == 0)
110     {
111       vlib_cli_output(vm, "No frame queues exist\n");
112       return error;
113     }
114
115   // Allocate storage for trace if necessary
116   vec_validate_aligned(tm->frame_queue_traces, num_fq-1, CLIB_CACHE_LINE_BYTES);
117   vec_validate_aligned(tm->frame_queue_histogram, num_fq-1, CLIB_CACHE_LINE_BYTES);
118
119   for (fqix=0; fqix<num_fq; fqix++) {
120     fqt = &tm->frame_queue_traces[fqix];
121     fqh = &tm->frame_queue_histogram[fqix];
122
123     memset(fqt->n_vectors, 0xff, sizeof(fqt->n_vectors));
124     fqt->written = 0;
125     memset(fqh, 0, sizeof(*fqh));
126     vlib_frame_queues[fqix]->trace = enable;
127   }
128   return error;
129 }
130
131 VLIB_CLI_COMMAND (cmd_trace_frame_queue,static) = {
132     .path = "trace frame-queue",
133     .short_help = "trace frame-queue (on|off)",
134     .function = trace_frame_queue,
135     .is_mp_safe = 1,
136 };
137
138
139 /*
140  * Adding two counters and compute percent of total
141  * Round up, e.g. 0.000001 => 1%
142  */
143 static u32
144 compute_percent (u64 *two_counters, u64 total)
145 {
146     if (total == 0)
147       {
148         return 0;
149       }
150     else
151       {
152         return (((two_counters[0] + two_counters[1]) * 100) + (total-1)) / total;
153       }
154 }
155
156 /*
157  * Display frame queue trace data gathered by threads.
158  */
159 static clib_error_t *
160 show_frame_queue_internal (vlib_main_t *vm,
161                            u32          histogram)
162 {
163   vlib_thread_main_t *tm = vlib_get_thread_main();
164   clib_error_t * error = NULL;
165   frame_queue_trace_t *fqt;
166   frame_queue_nelt_counter_t *fqh;
167   u32 num_fq;
168   u32 fqix;
169
170   num_fq = vec_len(tm->frame_queue_traces);
171   if (num_fq == 0)
172     {
173       vlib_cli_output(vm, "No trace data for frame queues\n");
174       return error;
175     }
176
177   if (histogram)
178     {
179       vlib_cli_output(vm, "0-1   2-3   4-5   6-7   8-9   10-11 12-13 14-15 "
180                           "16-17 18-19 20-21 22-23 24-25 26-27 28-29 30-31\n");
181     }
182
183   for (fqix=0; fqix<num_fq; fqix++) {
184     fqt = &(tm->frame_queue_traces[fqix]);
185
186     vlib_cli_output(vm, "Thread %d %v\n", fqix, vlib_worker_threads[fqix].name);
187
188     if (fqt->written == 0)
189       {
190         vlib_cli_output(vm, "  no trace data\n");
191         continue;
192       }
193
194     if (histogram)
195       {
196         fqh = &(tm->frame_queue_histogram[fqix]);
197         u32 nelt;
198         u64 total = 0;
199
200         for (nelt=0; nelt<FRAME_QUEUE_MAX_NELTS; nelt++) {
201             total += fqh->count[nelt];
202         }
203
204         /*
205          * Print in pairs to condense the output.
206          * Allow entries with 0 counts to be clearly identified, by rounding up.
207          * Any non-zero value will be displayed as at least one percent. This
208          * also means the sum of percentages can be > 100, but that is fine. The
209          * histogram is counted from the last time "trace frame on" was issued.
210          */
211         vlib_cli_output(vm,
212                         "%3d%%  %3d%%  %3d%%  %3d%%  %3d%%  %3d%%  %3d%%  %3d%%  "
213                         "%3d%%  %3d%%  %3d%%  %3d%%  %3d%%  %3d%%  %3d%%  %3d%%\n",
214                         compute_percent(&fqh->count[ 0], total),
215                         compute_percent(&fqh->count[ 2], total),
216                         compute_percent(&fqh->count[ 4], total),
217                         compute_percent(&fqh->count[ 6], total),
218                         compute_percent(&fqh->count[ 8], total),
219                         compute_percent(&fqh->count[10], total),
220                         compute_percent(&fqh->count[12], total),
221                         compute_percent(&fqh->count[14], total),
222                         compute_percent(&fqh->count[16], total),
223                         compute_percent(&fqh->count[18], total),
224                         compute_percent(&fqh->count[20], total),
225                         compute_percent(&fqh->count[22], total),
226                         compute_percent(&fqh->count[24], total),
227                         compute_percent(&fqh->count[26], total),
228                         compute_percent(&fqh->count[28], total),
229                         compute_percent(&fqh->count[30], total));
230       }
231     else
232       {
233         vlib_cli_output(vm, "  vector-threshold %d  ring size %d  in use %d\n",
234                         fqt->threshold, fqt->nelts, fqt->n_in_use);
235         vlib_cli_output(vm, "  head %12d  head_hint %12d  tail %12d\n",
236                         fqt->head, fqt->head_hint, fqt->tail);
237         vlib_cli_output(vm, "  %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d\n",
238                         fqt->n_vectors[0], fqt->n_vectors[1], fqt->n_vectors[2], fqt->n_vectors[3],
239                         fqt->n_vectors[4], fqt->n_vectors[5], fqt->n_vectors[6], fqt->n_vectors[7],
240                         fqt->n_vectors[8], fqt->n_vectors[9], fqt->n_vectors[10], fqt->n_vectors[11],
241                         fqt->n_vectors[12], fqt->n_vectors[13], fqt->n_vectors[14], fqt->n_vectors[15]);
242
243         if (fqt->nelts > 16)
244           {
245             vlib_cli_output(vm, "  %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d\n",
246                             fqt->n_vectors[16], fqt->n_vectors[17], fqt->n_vectors[18], fqt->n_vectors[19],
247                             fqt->n_vectors[20], fqt->n_vectors[21], fqt->n_vectors[22], fqt->n_vectors[23],
248                             fqt->n_vectors[24], fqt->n_vectors[25], fqt->n_vectors[26], fqt->n_vectors[27],
249                             fqt->n_vectors[28], fqt->n_vectors[29], fqt->n_vectors[30], fqt->n_vectors[31]);
250           }
251       }
252
253    }
254   return error;
255 }
256
257 static clib_error_t *
258 show_frame_queue_trace (vlib_main_t *vm, unformat_input_t *input,
259                         vlib_cli_command_t *cmd)
260 {
261   return show_frame_queue_internal (vm, 0);
262 }
263
264 static clib_error_t *
265 show_frame_queue_histogram (vlib_main_t *vm, unformat_input_t *input,
266                             vlib_cli_command_t *cmd)
267 {
268   return show_frame_queue_internal (vm, 1);
269 }
270
271 VLIB_CLI_COMMAND (cmd_show_frame_queue_trace,static) = {
272     .path = "show frame-queue",
273     .short_help = "show frame-queue trace",
274     .function = show_frame_queue_trace,
275 };
276
277 VLIB_CLI_COMMAND (cmd_show_frame_queue_histogram,static) = {
278     .path = "show frame-queue histogram",
279     .short_help = "show frame-queue histogram",
280     .function = show_frame_queue_histogram,
281 };
282
283
284 /*
285  * Modify the number of elements on the frame_queues
286  */
287 static clib_error_t *
288 test_frame_queue_nelts (vlib_main_t *vm, unformat_input_t *input,
289                         vlib_cli_command_t *cmd)
290 {
291   clib_error_t * error = NULL;
292   u32 num_fq;
293   u32 fqix;
294   u32 nelts = 0;
295
296   unformat(input, "%d", &nelts);
297   if ((nelts != 4) && (nelts != 8) && (nelts != 16) && (nelts != 32)) {
298       return clib_error_return(0, "expecting 4,8,16,32");
299   }
300
301   num_fq = vec_len(vlib_frame_queues);
302   if (num_fq == 0)
303     {
304       vlib_cli_output(vm, "No frame queues exist\n");
305       return error;
306     }
307
308   for (fqix=0; fqix<num_fq; fqix++) {
309     vlib_frame_queues[fqix]->nelts = nelts;
310   }
311
312   return error;
313 }
314
315 VLIB_CLI_COMMAND (cmd_test_frame_queue_nelts,static) = {
316     .path = "test frame-queue nelts",
317     .short_help = "test frame-queue nelts (4,8,16,32)",
318     .function = test_frame_queue_nelts,
319 };
320
321
322 /*
323  * Modify the max number of packets pulled off the frame queues
324  */
325 static clib_error_t *
326 test_frame_queue_threshold (vlib_main_t *vm, unformat_input_t *input,
327                             vlib_cli_command_t *cmd)
328 {
329   clib_error_t * error = NULL;
330   u32 num_fq;
331   u32 fqix;
332   u32 threshold = 0;
333
334   if (unformat(input, "%d", &threshold)) {
335   } else {
336       vlib_cli_output(vm, "expecting threshold value\n");
337       return error;
338   }
339
340   if (threshold == 0)
341     threshold = ~0;
342
343   num_fq = vec_len(vlib_frame_queues);
344   if (num_fq == 0)
345     {
346       vlib_cli_output(vm, "No frame queues exist\n");
347       return error;
348     }
349
350   for (fqix=0; fqix<num_fq; fqix++) {
351     vlib_frame_queues[fqix]->vector_threshold = threshold;
352   }
353
354   return error;
355 }
356
357 VLIB_CLI_COMMAND (cmd_test_frame_queue_threshold,static) = {
358     .path = "test frame-queue threshold",
359     .short_help = "test frame-queue threshold N (0=no limit)",
360     .function = test_frame_queue_threshold,
361 };
362