blob: 5c7802da51abae6bd3ff6f30b7d5d304f5f80b2d [file] [log] [blame]
Heinrich Schuchardtcc26f742020-12-12 10:14:22 +01001.. SPDX-License-Identifier: GPL-2.0+
2.. Copyright (c) 2013 The Chromium OS Authors.
Simon Glassc44b8002013-06-11 11:14:39 -07003
4Tracing in U-Boot
5=================
6
Heinrich Schuchardt8963f192021-11-16 18:38:47 +01007U-Boot supports a simple tracing feature which allows a record of execution
Simon Glassc44b8002013-06-11 11:14:39 -07008to be collected and sent to a host machine for analysis. At present the
9main use for this is to profile boot time.
10
11
12Overview
13--------
14
15The trace feature uses GCC's instrument-functions feature to trace all
16function entry/exit points. These are then recorded in a memory buffer.
17The memory buffer can be saved to the host over a network link using
18tftpput or by writing to an attached memory device such as MMC.
19
20On the host, the file is first converted with a tool called 'proftool',
21which extracts useful information from it. The resulting trace output
22resembles that emitted by Linux's ftrace feature, so can be visually
23displayed by pytimechart.
24
25
26Quick-start using Sandbox
27-------------------------
28
29Sandbox is a build of U-Boot that can run under Linux so it is a convenient
30way of trying out tracing before you use it on your actual board. To do
31this, follow these steps:
32
Simon Glass84b77112021-11-24 09:26:39 -070033Add the following to config/sandbox_defconfig
Simon Glassc44b8002013-06-11 11:14:39 -070034
Heinrich Schuchardtcc26f742020-12-12 10:14:22 +010035.. code-block:: c
36
Simon Glass84b77112021-11-24 09:26:39 -070037 CONFIG_TRACE=y
Simon Glassc44b8002013-06-11 11:14:39 -070038
39Build sandbox U-Boot with tracing enabled:
40
Heinrich Schuchardtcc26f742020-12-12 10:14:22 +010041.. code-block:: console
42
43 $ make FTRACE=1 O=sandbox sandbox_config
44 $ make FTRACE=1 O=sandbox
Simon Glassc44b8002013-06-11 11:14:39 -070045
46Run sandbox, wait for a bit of trace information to appear, and then capture
47a trace:
48
Heinrich Schuchardtcc26f742020-12-12 10:14:22 +010049.. code-block:: console
Simon Glassc44b8002013-06-11 11:14:39 -070050
Heinrich Schuchardtcc26f742020-12-12 10:14:22 +010051 $ ./sandbox/u-boot
Simon Glassc44b8002013-06-11 11:14:39 -070052
Heinrich Schuchardtcc26f742020-12-12 10:14:22 +010053 U-Boot 2013.04-rc2-00100-ga72fcef (Apr 17 2013 - 19:25:24)
Simon Glassc44b8002013-06-11 11:14:39 -070054
Heinrich Schuchardtcc26f742020-12-12 10:14:22 +010055 DRAM: 128 MiB
56 trace: enabled
57 Using default environment
Simon Glassc44b8002013-06-11 11:14:39 -070058
Heinrich Schuchardtcc26f742020-12-12 10:14:22 +010059 In: serial
60 Out: serial
61 Err: serial
62 =>trace stats
63 671,406 function sites
64 69,712 function calls
65 0 untracked function calls
66 73,373 traced function calls
67 16 maximum observed call depth
68 15 call depth limit
69 66,491 calls not traced due to depth
70 =>trace stats
71 671,406 function sites
Simon Glassc44b8002013-06-11 11:14:39 -070072 1,279,450 function calls
Heinrich Schuchardtcc26f742020-12-12 10:14:22 +010073 0 untracked function calls
74 950,490 traced function calls (333217 dropped due to overflow)
75 16 maximum observed call depth
76 15 call depth limit
77 1,275,767 calls not traced due to depth
78 =>trace calls 0 e00000
79 Call list dumped to 00000000, size 0xae0a40
80 =>print
81 baudrate=115200
82 profbase=0
83 profoffset=ae0a40
84 profsize=e00000
85 stderr=serial
86 stdin=serial
87 stdout=serial
Simon Glassc44b8002013-06-11 11:14:39 -070088
Heinrich Schuchardtcc26f742020-12-12 10:14:22 +010089 Environment size: 117/8188 bytes
90 =>host save host 0 trace 0 ${profoffset}
91 11405888 bytes written in 10 ms (1.1 GiB/s)
92 =>reset
Simon Glassc44b8002013-06-11 11:14:39 -070093
94
Heinrich Schuchardtcc26f742020-12-12 10:14:22 +010095Then run proftool to convert the trace information to ftrace format
Simon Glassc44b8002013-06-11 11:14:39 -070096
Heinrich Schuchardtcc26f742020-12-12 10:14:22 +010097.. code-block:: console
Simon Glassc44b8002013-06-11 11:14:39 -070098
Heinrich Schuchardtcc26f742020-12-12 10:14:22 +010099 $ ./sandbox/tools/proftool -m sandbox/System.map -p trace dump-ftrace >trace.txt
Simon Glassc44b8002013-06-11 11:14:39 -0700100
Heinrich Schuchardtcc26f742020-12-12 10:14:22 +0100101Finally run pytimechart to display it
102
103.. code-block:: console
104
105 $ pytimechart trace.txt
Simon Glassc44b8002013-06-11 11:14:39 -0700106
107Using this tool you can zoom and pan across the trace, with the function
108calls on the left and little marks representing the start and end of each
109function.
110
111
112CONFIG Options
113--------------
114
Heinrich Schuchardtcc26f742020-12-12 10:14:22 +0100115CONFIG_TRACE
116 Enables the trace feature in U-Boot.
Simon Glassc44b8002013-06-11 11:14:39 -0700117
Heinrich Schuchardtcc26f742020-12-12 10:14:22 +0100118CONFIG_CMD_TRACE
119 Enables the trace command.
Simon Glassc44b8002013-06-11 11:14:39 -0700120
Heinrich Schuchardtcc26f742020-12-12 10:14:22 +0100121CONFIG_TRACE_BUFFER_SIZE
122 Size of trace buffer to allocate for U-Boot. This buffer is
123 used after relocation, as a place to put function tracing
124 information. The address of the buffer is determined by
125 the relocation code.
Simon Glassc44b8002013-06-11 11:14:39 -0700126
Heinrich Schuchardtcc26f742020-12-12 10:14:22 +0100127CONFIG_TRACE_EARLY
128 Define this to start tracing early, before relocation.
Simon Glassc44b8002013-06-11 11:14:39 -0700129
Heinrich Schuchardtcc26f742020-12-12 10:14:22 +0100130CONFIG_TRACE_EARLY_SIZE
131 Size of 'early' trace buffer. Before U-Boot has relocated
132 it doesn't have a proper trace buffer. On many boards
133 you can define an area of memory to use for the trace
134 buffer until the 'real' trace buffer is available after
135 relocation. The contents of this buffer are then copied to
136 the real buffer.
Simon Glassc44b8002013-06-11 11:14:39 -0700137
Heinrich Schuchardtcc26f742020-12-12 10:14:22 +0100138CONFIG_TRACE_EARLY_ADDR
139 Address of early trace buffer
Simon Glassc44b8002013-06-11 11:14:39 -0700140
141
142Building U-Boot with Tracing Enabled
143------------------------------------
144
145Pass 'FTRACE=1' to the U-Boot Makefile to actually instrument the code.
146This is kept as a separate option so that it is easy to enable/disable
147instrumenting from the command line instead of having to change board
148config files.
149
150
151Collecting Trace Data
152---------------------
153
154When you run U-Boot on your board it will collect trace data up to the
155limit of the trace buffer size you have specified. Once that is exhausted
156no more data will be collected.
157
158Collecting trace data has an affect on execution time/performance. You
Heinrich Schuchardt8963f192021-11-16 18:38:47 +0100159will notice this particularly with trivial functions - the overhead of
Simon Glassc44b8002013-06-11 11:14:39 -0700160recording their execution may even exceed their normal execution time.
161In practice this doesn't matter much so long as you are aware of the
Heinrich Schuchardt8963f192021-11-16 18:38:47 +0100162effect. Once you have done your optimizations, turn off tracing before
Simon Glassc44b8002013-06-11 11:14:39 -0700163doing end-to-end timing.
164
165The best time to start tracing is right at the beginning of U-Boot. The
166best time to stop tracing is right at the end. In practice it is hard
167to achieve these ideals.
168
169This implementation enables tracing early in board_init_f(). This means
170that it captures most of the board init process, missing only the
171early architecture-specific init. However, it also misses the entire
172SPL stage if there is one.
173
174U-Boot typically ends with a 'bootm' command which loads and runs an
175OS. There is useful trace data in the execution of that bootm
176command. Therefore this implementation provides a way to collect trace
177data after bootm has finished processing, but just before it jumps to
178the OS. In practical terms, U-Boot runs the 'fakegocmd' environment
179variable at this point. This variable should have a short script which
180collects the trace data and writes it somewhere.
181
Heinrich Schuchardt8963f192021-11-16 18:38:47 +0100182Trace data collection relies on a microsecond timer, accessed through
Simon Glassc44b8002013-06-11 11:14:39 -0700183timer_get_us(). So the first think you should do is make sure that
184this produces sensible results for your board. Suitable sources for
185this timer include high resolution timers, PWMs or profile timers if
186available. Most modern SOCs have a suitable timer for this. Make sure
187that you mark this timer (and anything it calls) with
Simon Glass56da76d2022-12-21 16:08:15 -0700188notrace so that the trace library can
Simon Glassc44b8002013-06-11 11:14:39 -0700189use it without causing an infinite loop.
190
191
192Commands
193--------
194
195The trace command has variable sub-commands:
196
Heinrich Schuchardtcc26f742020-12-12 10:14:22 +0100197stats
198 Display tracing statistics
Simon Glassc44b8002013-06-11 11:14:39 -0700199
Heinrich Schuchardtcc26f742020-12-12 10:14:22 +0100200pause
201 Pause tracing
Simon Glassc44b8002013-06-11 11:14:39 -0700202
Heinrich Schuchardtcc26f742020-12-12 10:14:22 +0100203resume
204 Resume tracing
Simon Glassc44b8002013-06-11 11:14:39 -0700205
Heinrich Schuchardtcc26f742020-12-12 10:14:22 +0100206funclist [<addr> <size>]
207 Dump a list of functions into the buffer
Simon Glassc44b8002013-06-11 11:14:39 -0700208
Heinrich Schuchardtcc26f742020-12-12 10:14:22 +0100209calls [<addr> <size>]
210 Dump function call trace into buffer
Simon Glassc44b8002013-06-11 11:14:39 -0700211
212If the address and size are not given, these are obtained from environment
213variables (see below). In any case the environment variables are updated
214after the command runs.
215
216
217Environment Variables
218---------------------
219
220The following are used:
221
Heinrich Schuchardtcc26f742020-12-12 10:14:22 +0100222profbase
223 Base address of trace output buffer
Simon Glassc44b8002013-06-11 11:14:39 -0700224
Heinrich Schuchardtcc26f742020-12-12 10:14:22 +0100225profoffset
226 Offset of first unwritten byte in trace output buffer
Simon Glassc44b8002013-06-11 11:14:39 -0700227
Heinrich Schuchardtcc26f742020-12-12 10:14:22 +0100228profsize
229 Size of trace output buffer
Simon Glassc44b8002013-06-11 11:14:39 -0700230
231All of these are set by the 'trace calls' command.
232
233These variables keep track of the amount of data written to the trace
234output buffer by the 'trace' command. The trace commands which write data
235to the output buffer can use these to specify the buffer to write to, and
236update profoffset each time. This allows successive commands to append data
Heinrich Schuchardtcc26f742020-12-12 10:14:22 +0100237to the same buffer, for example::
Simon Glassc44b8002013-06-11 11:14:39 -0700238
Heinrich Schuchardtcc26f742020-12-12 10:14:22 +0100239 => trace funclist 10000 e00000
240 => trace calls
Simon Glassc44b8002013-06-11 11:14:39 -0700241
242(the latter command appends more data to the buffer).
243
244
Heinrich Schuchardtcc26f742020-12-12 10:14:22 +0100245fakegocmd
246 Specifies commands to run just before booting the OS. This
247 is a useful time to write the trace data to the host for
248 processing.
Simon Glassc44b8002013-06-11 11:14:39 -0700249
250
251Writing Out Trace Data
252----------------------
253
254Once the trace data is in an output buffer in memory there are various ways
255to transmit it to the host. Notably you can use tftput to send the data
Heinrich Schuchardtcc26f742020-12-12 10:14:22 +0100256over a network link::
Simon Glassc44b8002013-06-11 11:14:39 -0700257
Heinrich Schuchardtcc26f742020-12-12 10:14:22 +0100258 fakegocmd=trace pause; usb start; set autoload n; bootp;
259 trace calls 10000000 1000000;
260 tftpput ${profbase} ${profoffset} 192.168.1.4:/tftpboot/calls
Simon Glassc44b8002013-06-11 11:14:39 -0700261
262This starts up USB (to talk to an attached USB Ethernet dongle), writes
263a trace log to address 10000000 and sends it to a host machine using
264TFTP. After this, U-Boot will boot the OS normally, albeit a little
265later.
266
267
268Converting Trace Output Data
269----------------------------
270
271The trace output data is kept in a binary format which is not documented
272here. To convert it into something useful, you can use proftool.
273
274This tool must be given the U-Boot map file and the trace data received
275from running that U-Boot. It produces a text output file.
276
277Options
Simon Glassc44b8002013-06-11 11:14:39 -0700278
Heinrich Schuchardtcc26f742020-12-12 10:14:22 +0100279-m <map_file>
280 Specify U-Boot map file
281
282-p <trace_file>
Heinrich Schuchardt8963f192021-11-16 18:38:47 +0100283 Specify profile/trace file
Simon Glassc44b8002013-06-11 11:14:39 -0700284
285Commands:
286
Heinrich Schuchardtcc26f742020-12-12 10:14:22 +0100287dump-ftrace
288 Write a text dump of the file in Linux ftrace format to stdout
Simon Glassc44b8002013-06-11 11:14:39 -0700289
290
291Viewing the Trace Data
292----------------------
293
294You can use pytimechart for this (sudo apt-get pytimechart might work on
295your Debian-style machine, and use your favourite search engine to obtain
296documentation). It expects the file to have a .txt extension. The program
297has terse user interface but is very convenient for viewing U-Boot
298profile information.
299
300
301Workflow Suggestions
302--------------------
303
304The following suggestions may be helpful if you are trying to reduce boot
305time:
306
3071. Enable CONFIG_BOOTSTAGE and CONFIG_BOOTSTAGE_REPORT. This should get
Heinrich Schuchardtcc26f742020-12-12 10:14:22 +0100308 you are helpful overall snapshot of the boot time.
Simon Glassc44b8002013-06-11 11:14:39 -0700309
3102. Build U-Boot with tracing and run it. Note the difference in boot time
Heinrich Schuchardtcc26f742020-12-12 10:14:22 +0100311 (it is common for tracing to add 10% to the time)
Simon Glassc44b8002013-06-11 11:14:39 -0700312
Heinrich Schuchardt8963f192021-11-16 18:38:47 +01003133. Collect the trace information as described above. Use this to find where
Heinrich Schuchardtcc26f742020-12-12 10:14:22 +0100314 all the time is being spent.
Simon Glassc44b8002013-06-11 11:14:39 -0700315
Heinrich Schuchardt8963f192021-11-16 18:38:47 +01003164. Take a look at that code and see if you can optimize it. Perhaps it is
317 possible to speed up the initialization of a device, or remove an unused
Heinrich Schuchardtcc26f742020-12-12 10:14:22 +0100318 feature.
Simon Glassc44b8002013-06-11 11:14:39 -0700319
3205. Rebuild, run and collect again. Compare your results.
321
3226. Keep going until you run out of steam, or your boot is fast enough.
323
324
325Configuring Trace
326-----------------
327
328There are a few parameters in the code that you may want to consider.
329There is a function call depth limit (set to 15 by default). When the
330stack depth goes above this then no tracing information is recorded.
331The maximum depth reached is recorded and displayed by the 'trace stats'
332command.
333
334
335Future Work
336-----------
337
338Tracing could be a little tidier in some areas, for example providing
339run-time configuration options for trace.
340
341Some other features that might be useful:
342
343- Trace filter to select which functions are recorded
344- Sample-based profiling using a timer interrupt
345- Better control over trace depth
346- Compression of trace information
347
348
349Simon Glass <sjg@chromium.org>
350April 2013