Initial commit of vpp code.
[vpp.git] / vlib / vlib / trace.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  * trace.c: VLIB trace buffer.
17  *
18  * Copyright (c) 2008 Eliot Dresselhaus
19  *
20  * Permission is hereby granted, free of charge, to any person obtaining
21  * a copy of this software and associated documentation files (the
22  * "Software"), to deal in the Software without restriction, including
23  * without limitation the rights to use, copy, modify, merge, publish,
24  * distribute, sublicense, and/or sell copies of the Software, and to
25  * permit persons to whom the Software is furnished to do so, subject to
26  * the following conditions:
27  *
28  * The above copyright notice and this permission notice shall be
29  * included in all copies or substantial portions of the Software.
30  *
31  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38  */
39
40 #include <vlib/vlib.h>
41 #include <vlib/threads.h>
42
43 /* Helper function for nodes which only trace buffer data. */
44 void
45 vlib_trace_frame_buffers_only (vlib_main_t * vm,
46                                vlib_node_runtime_t * node,
47                                u32 * buffers,
48                                uword n_buffers,
49                                uword next_buffer_stride,
50                                uword n_buffer_data_bytes_in_trace)
51 {
52   u32 n_left, * from;
53
54   n_left = n_buffers;
55   from = buffers;
56   
57   while (n_left >= 4)
58     {
59       u32 bi0, bi1;
60       vlib_buffer_t * b0, * b1;
61       u8 * t0, * t1;
62
63       /* Prefetch next iteration. */
64       vlib_prefetch_buffer_with_index (vm, from[2], LOAD);
65       vlib_prefetch_buffer_with_index (vm, from[3], LOAD);
66
67       bi0 = from[0];
68       bi1 = from[1];
69
70       b0 = vlib_get_buffer (vm, bi0);
71       b1 = vlib_get_buffer (vm, bi1);
72
73       if (b0->flags & VLIB_BUFFER_IS_TRACED)
74         {
75           t0 = vlib_add_trace (vm, node, b0, n_buffer_data_bytes_in_trace);
76           memcpy (t0, b0->data + b0->current_data,
77                   n_buffer_data_bytes_in_trace);
78         }
79       if (b1->flags & VLIB_BUFFER_IS_TRACED)
80         {
81           t1 = vlib_add_trace (vm, node, b1, n_buffer_data_bytes_in_trace);
82           memcpy (t1, b1->data + b1->current_data,
83                   n_buffer_data_bytes_in_trace);
84         }
85       from += 2;
86       n_left -= 2;
87     }
88
89   while (n_left >= 1)
90     {
91       u32 bi0;
92       vlib_buffer_t * b0;
93       u8 * t0;
94
95       bi0 = from[0];
96
97       b0 = vlib_get_buffer (vm, bi0);
98
99       if (b0->flags & VLIB_BUFFER_IS_TRACED)
100         {
101           t0 = vlib_add_trace (vm, node, b0, n_buffer_data_bytes_in_trace);
102           memcpy (t0, b0->data + b0->current_data,
103                   n_buffer_data_bytes_in_trace);
104         }
105       from += 1;
106       n_left -= 1;
107     }
108 }
109
110 /* Free up all trace buffer memory. */
111 always_inline void
112 clear_trace_buffer (vlib_trace_main_t * tm)
113 {
114   int i;
115
116   foreach_vlib_main (
117   ({
118     void *mainheap;
119
120     tm = &this_vlib_main->trace_main;
121     mainheap = clib_mem_set_heap (this_vlib_main->heap_base);
122
123     for (i = 0; i < vec_len (tm->trace_buffer_pool); i++)
124       if (! pool_is_free_index (tm->trace_buffer_pool, i))
125         vec_free (tm->trace_buffer_pool[i]);
126     pool_free (tm->trace_buffer_pool);
127     clib_mem_set_heap (mainheap);
128   }));
129 }
130
131 static u8 * format_vlib_trace (u8 * s, va_list * va)
132 {
133   vlib_main_t * vm = va_arg (*va, vlib_main_t *);
134   vlib_trace_header_t * h = va_arg (*va, vlib_trace_header_t *);
135   vlib_trace_header_t * e = vec_end (h);
136   vlib_node_t * node, * prev_node;
137   clib_time_t * ct = &vm->clib_time;
138   f64 t;
139   
140   prev_node = 0;
141   while (h < e)
142     {
143       node = vlib_get_node (vm, h->node_index);
144
145       if (node != prev_node)
146         {
147           t = (h->time - vm->cpu_time_main_loop_start) * ct->seconds_per_clock;
148           s = format (s, "\n%U: %v",
149                       format_time_interval, "h:m:s:u", t,
150                       node->name);
151         }
152       prev_node = node;
153
154       if (node->format_trace)
155         s = format (s, "\n  %U",
156                     node->format_trace, vm, node, h->data);
157       else
158         s = format (s, "\n  %U",
159                     node->format_buffer, h->data);
160
161       h = vlib_trace_header_next (h);
162     }
163
164   return s;
165 }
166
167 /* Root of all trace cli commands. */
168 VLIB_CLI_COMMAND (trace_cli_command,static) = {
169   .path = "trace",
170   .short_help = "Packet tracer commands",
171 };
172
173 static clib_error_t *
174 cli_show_trace_buffer (vlib_main_t * vm,
175                        unformat_input_t * input,
176                        vlib_cli_command_t * cmd)
177 {
178   vlib_trace_main_t * tm = &vm->trace_main;
179   vlib_trace_header_t ** h, ** traces;
180   u32 i, index = 0;
181   char * fmt;
182
183   /* Get active traces from pool. */
184
185   foreach_vlib_main (
186   ({
187     void *mainheap;
188
189     fmt = "------------------- Start of thread %d %v -------------------";
190     vlib_cli_output (vm, fmt, index, vlib_worker_threads[index].name);
191
192     tm = &this_vlib_main->trace_main;
193
194     mainheap = clib_mem_set_heap (this_vlib_main->heap_base);
195     traces = 0;
196     pool_foreach (h, tm->trace_buffer_pool, 
197     ({
198       vec_add1 (traces, h[0]);
199     }));
200     
201     if (vec_len (traces) == 0)
202       {
203         clib_mem_set_heap (mainheap);
204         vlib_cli_output (vm, "No packets in trace buffer");
205         goto done;
206       }
207     
208     /* Sort them by increasing time. */
209     vec_sort (traces, t0, t1, ({
210           i64 dt = t0[0]->time - t1[0]->time;
211           dt < 0 ? -1 : (dt > 0 ? +1 : 0);
212         }));
213     
214     for (i = 0; i < vec_len (traces); i++)
215       {
216         clib_mem_set_heap (mainheap);
217         
218         vlib_cli_output (vm, "Packet %d\n%U\n\n", i + 1,
219                          format_vlib_trace, vm, traces[i]);
220
221         mainheap = clib_mem_set_heap (this_vlib_main->heap_base);
222       }
223     
224   done:
225     vec_free (traces);
226     clib_mem_set_heap (mainheap);
227
228     index++;
229   }));
230
231   return 0;
232 }
233
234 VLIB_CLI_COMMAND (show_trace_cli,static) = {
235   .path = "show trace",
236   .short_help = "Show trace buffer",
237   .function = cli_show_trace_buffer,
238 };
239
240 static clib_error_t *
241 cli_add_trace_buffer (vlib_main_t * vm,
242                       unformat_input_t * input,
243                       vlib_cli_command_t * cmd)
244 {
245   vlib_trace_main_t * tm;
246   vlib_trace_node_t * tn;
247   u32 node_index, add;
248
249   if (unformat (input, "%U %d", unformat_vlib_node, vm, &node_index, &add))
250     ;
251   else
252     return clib_error_create ("expected NODE COUNT, got `%U'",
253                               format_unformat_error, input);
254
255   foreach_vlib_main (
256   ({
257     void *oldheap;
258     tm = &this_vlib_main->trace_main;
259
260     oldheap = clib_mem_set_heap (this_vlib_main->heap_base);
261
262     vec_validate (tm->nodes, node_index);
263     tn = tm->nodes + node_index;
264     tn->limit += add;
265     clib_mem_set_heap (oldheap);
266   }));
267
268   return 0;
269 }
270
271 VLIB_CLI_COMMAND (add_trace_cli,static) = {
272   .path = "trace add",
273   .short_help = "Trace given number of packets",
274   .function = cli_add_trace_buffer,
275 };
276
277 static clib_error_t *
278 cli_clear_trace_buffer (vlib_main_t * vm,
279                         unformat_input_t * input,
280                         vlib_cli_command_t * cmd)
281 {
282   vlib_trace_main_t * tm = &vm->trace_main;
283   clear_trace_buffer (tm);
284   return 0;
285 }
286
287 VLIB_CLI_COMMAND (clear_trace_cli,static) = {
288   .path = "clear trace",
289   .short_help = "Clear trace buffer and free memory",
290   .function = cli_clear_trace_buffer,
291 };
292
293 /* Dummy function to get us linked in. */
294 void vlib_trace_cli_reference (void) {}