trace.rst 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. .. SPDX-License-Identifier: GPL-2.0+
  2. .. Copyright (c) 2013 The Chromium OS Authors.
  3. Tracing in U-Boot
  4. =================
  5. U-Boot supports a simple tracing feature which allows a record of execution
  6. to be collected and sent to a host machine for analysis. At present the
  7. main use for this is to profile boot time.
  8. Overview
  9. --------
  10. The trace feature uses GCC's instrument-functions feature to trace all
  11. function entry/exit points. These are then recorded in a memory buffer.
  12. The memory buffer can be saved to the host over a network link using
  13. tftpput or by writing to an attached storage device such as MMC.
  14. On the host, the file is first converted with a tool called 'proftool',
  15. which extracts useful information from it. The resulting trace output
  16. resembles that emitted by Linux's ftrace feature, so can be visually
  17. displayed by kernelshark (see kernelshark_) and used with
  18. 'trace-cmd report' (see trace_cmd_).
  19. It is also possible to produce a flame graph for use with flamegraph.pl
  20. (see flamegraph_pl_).
  21. Quick-start using Sandbox
  22. -------------------------
  23. Sandbox is a build of U-Boot that can run under Linux so it is a convenient
  24. way of trying out tracing before you use it on your actual board. To do
  25. this, follow these steps:
  26. Add the following to `config/sandbox_defconfig`:
  27. .. code-block:: c
  28. CONFIG_TRACE=y
  29. Build sandbox U-Boot with tracing enabled:
  30. .. code-block:: console
  31. $ make FTRACE=1 O=sandbox sandbox_config
  32. $ make FTRACE=1 O=sandbox
  33. Run sandbox, wait for a bit of trace information to appear, and then capture
  34. a trace:
  35. .. code-block:: console
  36. $ ./sandbox/u-boot
  37. U-Boot 2013.04-rc2-00100-ga72fcef (Apr 17 2013 - 19:25:24)
  38. DRAM: 128 MiB
  39. trace: enabled
  40. Using default environment
  41. In: serial
  42. Out: serial
  43. Err: serial
  44. =>trace stats
  45. 671,406 function sites
  46. 69,712 function calls
  47. 0 untracked function calls
  48. 73,373 traced function calls
  49. 16 maximum observed call depth
  50. 15 call depth limit
  51. 66,491 calls not traced due to depth
  52. =>trace stats
  53. 671,406 function sites
  54. 1,279,450 function calls
  55. 0 untracked function calls
  56. 950,490 traced function calls (333217 dropped due to overflow)
  57. 16 maximum observed call depth
  58. 15 call depth limit
  59. 1,275,767 calls not traced due to depth
  60. =>trace calls 1000000 e00000
  61. Call list dumped to 00000000, size 0xae0a40
  62. =>print
  63. baudrate=115200
  64. profbase=0
  65. profoffset=ae0a40
  66. profsize=e00000
  67. stderr=serial
  68. stdin=serial
  69. stdout=serial
  70. Environment size: 117/8188 bytes
  71. =>host save hostfs - 1000000 trace ${profoffset}
  72. 11405888 bytes written in 10 ms (1.1 GiB/s)
  73. =>reset
  74. Then run proftool to convert the trace information to ftrace format
  75. .. code-block:: console
  76. $ ./sandbox/tools/proftool -m sandbox/System.map -t trace dump-ftrace -o trace.dat
  77. Finally run kernelshark to display it (note it only works with `.dat` files!):
  78. .. code-block:: console
  79. $ kernelshark trace.dat
  80. Using this tool you can view the trace records and see the timestamp for each
  81. function.
  82. .. image:: pics/kernelshark.png
  83. :width: 800
  84. :alt: Kernelshark showing function-trace records
  85. To see the records on the console, use trace-cmd:
  86. .. code-block:: console
  87. $ trace-cmd report trace.dat | less
  88. cpus=1
  89. u-boot-1 [000] 3.116364: function: initf_malloc
  90. u-boot-1 [000] 3.116375: function: initf_malloc
  91. u-boot-1 [000] 3.116386: function: initf_bootstage
  92. u-boot-1 [000] 3.116396: function: bootstage_init
  93. u-boot-1 [000] 3.116408: function: malloc
  94. u-boot-1 [000] 3.116418: function: malloc_simple
  95. u-boot-1 [000] 3.116429: function: alloc_simple
  96. u-boot-1 [000] 3.116441: function: alloc_simple
  97. u-boot-1 [000] 3.116449: function: malloc_simple
  98. u-boot-1 [000] 3.116457: function: malloc
  99. Note that `pytimechart` is obsolete so cannot be used anymore.
  100. There is a -f option available to select a function graph:
  101. .. code-block:: console
  102. $ ./sandbox/tools/proftool -m sandbox/System.map -t trace -f funcgraph dump-ftrace -o trace.dat
  103. Again, you can use kernelshark or trace-cmd to look at the output. In this case
  104. you will see the time taken by each function shown against its exit record.
  105. .. image:: pics/kernelshark_fg.png
  106. :width: 800
  107. :alt: Kernelshark showing function-graph records
  108. .. code-block:: console
  109. $ trace-cmd report trace.dat | less
  110. cpus=1
  111. u-boot-1 [000] 3.116364: funcgraph_entry: 0.011 us | initf_malloc();
  112. u-boot-1 [000] 3.116386: funcgraph_entry: | initf_bootstage() {
  113. u-boot-1 [000] 3.116396: funcgraph_entry: | bootstage_init() {
  114. u-boot-1 [000] 3.116408: funcgraph_entry: | malloc() {
  115. u-boot-1 [000] 3.116418: funcgraph_entry: | malloc_simple() {
  116. u-boot-1 [000] 3.116429: funcgraph_entry: 0.012 us | alloc_simple();
  117. u-boot-1 [000] 3.116449: funcgraph_exit: 0.031 us | }
  118. u-boot-1 [000] 3.116457: funcgraph_exit: 0.049 us | }
  119. u-boot-1 [000] 3.116466: funcgraph_entry: 0.063 us | memset();
  120. u-boot-1 [000] 3.116539: funcgraph_exit: 0.143 us | }
  121. Flame graph
  122. -----------
  123. Some simple flame graph options are available as well, using the dump-flamegraph
  124. command:
  125. .. code-block:: console
  126. $ ./sandbox/tools/proftool -m sandbox/System.map -t trace dump-flamegraph -o trace.fg
  127. $ flamegraph.pl trace.fg >trace.svg
  128. You can load the .svg file into a viewer. If you use Chrome (and some other
  129. programs) you can click around and zoom in and out.
  130. .. image:: pics/flamegraph.png
  131. :width: 800
  132. :alt: Chrome showing the flamegraph.pl output
  133. .. image:: pics/flamegraph_zoom.png
  134. :width: 800
  135. :alt: Chrome showing zooming into the flamegraph.pl output
  136. A timing variant is also available, which gives an idea of how much time is
  137. spend in each call stack:
  138. .. code-block:: console
  139. $ ./sandbox/tools/proftool -m sandbox/System.map -t trace dump-flamegraph -f timing -o trace.fg
  140. $ flamegraph.pl trace.fg >trace.svg
  141. Note that trace collection does slow down execution so the timings will be
  142. inflated. They should be used to guide optimisation. For accurate boot timings,
  143. use bootstage.
  144. .. image:: pics/flamegraph_timing.png
  145. :width: 800
  146. :alt: Chrome showing flamegraph.pl output with timing
  147. CONFIG Options
  148. --------------
  149. CONFIG_TRACE
  150. Enables the trace feature in U-Boot.
  151. CONFIG_CMD_TRACE
  152. Enables the trace command.
  153. CONFIG_TRACE_BUFFER_SIZE
  154. Size of trace buffer to allocate for U-Boot. This buffer is
  155. used after relocation, as a place to put function tracing
  156. information. The address of the buffer is determined by
  157. the relocation code.
  158. CONFIG_TRACE_EARLY
  159. Define this to start tracing early, before relocation.
  160. CONFIG_TRACE_EARLY_SIZE
  161. Size of 'early' trace buffer. Before U-Boot has relocated
  162. it doesn't have a proper trace buffer. On many boards
  163. you can define an area of memory to use for the trace
  164. buffer until the 'real' trace buffer is available after
  165. relocation. The contents of this buffer are then copied to
  166. the real buffer.
  167. CONFIG_TRACE_EARLY_ADDR
  168. Address of early trace buffer
  169. CONFIG_TRACE_CALL_DEPTH_LIMIT
  170. Sets the limit on trace call-depth. For a broad view, 10 is typically
  171. sufficient. Setting this too large creates enormous traces and distorts
  172. the overall timing considerable.
  173. Building U-Boot with Tracing Enabled
  174. ------------------------------------
  175. Pass 'FTRACE=1' to the U-Boot Makefile to actually instrument the code.
  176. This is kept as a separate option so that it is easy to enable/disable
  177. instrumenting from the command line instead of having to change board
  178. config files.
  179. Board requirements
  180. ------------------
  181. Trace data collection relies on a microsecond timer, accessed through
  182. `timer_get_us()`. So the first thing you should do is make sure that
  183. this produces sensible results for your board. Suitable sources for
  184. this timer include high resolution timers, PWMs or profile timers if
  185. available. Most modern SOCs have a suitable timer for this.
  186. See `add_ftrace()` for where `timer_get_us()` is called. The `notrace`
  187. attribute must be used on each function called by `timer_get_us()` since
  188. recursive calls to `add_ftrace()` will cause a fault::
  189. trace: recursion detected, disabling
  190. You cannot use driver model to obtain the microsecond timer, since tracing
  191. may be enabled before driver model is set up. Instead, provide a low-level
  192. function which accesses the timer, setting it up if needed.
  193. Collecting Trace Data
  194. ---------------------
  195. When you run U-Boot on your board it will collect trace data up to the
  196. limit of the trace buffer size you have specified. Once that is exhausted
  197. no more data will be collected.
  198. Collecting trace data affects execution time and performance. You
  199. will notice this particularly with trivial functions - the overhead of
  200. recording their execution may even exceed their normal execution time.
  201. In practice this doesn't matter much so long as you are aware of the
  202. effect. Once you have done your optimizations, turn off tracing before
  203. doing end-to-end timing using bootstage.
  204. The best time to start tracing is right at the beginning of U-Boot. The
  205. best time to stop tracing is right at the end. In practice it is hard
  206. to achieve these ideals.
  207. This implementation enables tracing early in `board_init_r()`, or
  208. `board_init_f()` when `TRACE_EARLY` is enabled. This means
  209. that it captures most of the board init process, missing only the
  210. early architecture-specific init. However, it also misses the entire
  211. SPL stage if there is one. At present tracing is not supported in SPL.
  212. U-Boot typically ends with a 'bootm' command which loads and runs an
  213. OS. There is useful trace data in the execution of that bootm
  214. command. Therefore this implementation provides a way to collect trace
  215. data after bootm has finished processing, but just before it jumps to
  216. the OS. In practical terms, U-Boot runs the 'fakegocmd' environment
  217. variable at this point. This variable should have a short script which
  218. collects the trace data and writes it somewhere.
  219. Controlling the trace
  220. ---------------------
  221. U-Boot provides a command-line interface to the trace system for controlling
  222. tracing and accessing the trace data. See :doc:`../usage/cmd/trace`.
  223. Environment Variables
  224. ---------------------
  225. The following are used:
  226. profbase
  227. Base address of trace output buffer
  228. profoffset
  229. Offset of first unwritten byte in trace output buffer
  230. profsize
  231. Size of trace output buffer
  232. All of these are set by the 'trace calls' command.
  233. These variables keep track of the amount of data written to the trace
  234. output buffer by the 'trace' command. The trace commands which write data
  235. to the output buffer can use these to specify the buffer to write to, and
  236. update profoffset each time. This allows successive commands to append data
  237. to the same buffer, for example::
  238. => trace funclist 10000 e00000
  239. => trace calls
  240. (the latter command appends more data to the buffer).
  241. fakegocmd
  242. Specifies commands to run just before booting the OS. This
  243. is a useful time to write the trace data to the host for
  244. processing.
  245. Writing Out Trace Data
  246. ----------------------
  247. Once the trace data is in an output buffer in memory there are various ways
  248. to transmit it to the host. Notably you can use tftput to send the data
  249. over a network link::
  250. fakegocmd=trace pause; usb start; set autoload n; bootp;
  251. trace calls 10000000 1000000;
  252. tftpput ${profbase} ${profoffset} 192.168.1.4:/tftpboot/calls
  253. This starts up USB (to talk to an attached USB Ethernet dongle), writes
  254. a trace log to address 10000000 and sends it to a host machine using
  255. TFTP. After this, U-Boot will boot the OS normally, albeit a little
  256. later.
  257. For a filesystem you may do something like::
  258. trace calls 10000000 1000000;
  259. save mmc 1:1 10000000 /trace ${profoffset}
  260. The trace buffer format is internal to the trace system. It consists of a
  261. header, a call count for each function site, followed by a list of trace
  262. records, once for each function call.
  263. Converting Trace Output Data (proftool)
  264. ---------------------------------------
  265. The trace output data is kept in a binary format which is not documented
  266. here. See the `trace.h` header file if you are interested. To convert it into
  267. something useful, you can use proftool.
  268. This tool must be given the U-Boot map file and the trace data received
  269. from running that U-Boot. It produces a binary output file.
  270. It is also possible to provide a configuration file to indicate which functions
  271. should be included or dropped during conversion. This file consists of lines
  272. like::
  273. include-func <regex>
  274. exclude-func <regex>
  275. where <regex> is a regular expression matched against function names. It
  276. allows some functions to be dropped from the trace when producing ftrace
  277. records.
  278. Options:
  279. -c <config_file>
  280. Specify the optional configuration file, to control which functions are
  281. included in the output.
  282. -f <format>
  283. Specifies the format to use (see below)
  284. -m <map_file>
  285. Specify U-Boot map file (`System.map`)
  286. -o <output file>
  287. Specify the output filename
  288. -t <trace_file>
  289. Specify trace file, the data saved from U-Boot
  290. -v <0-4>
  291. Specify the verbosity, where 0 is the minimum and 4 is for debugging.
  292. Commands:
  293. dump-ftrace:
  294. Write a binary dump of the file in Linux ftrace format. Two options are
  295. available:
  296. function
  297. write function-call records (caller/callee)
  298. funcgraph
  299. write function entry/exit records (graph)
  300. This format can be used with kernelshark_ and trace_cmd_.
  301. dump-flamegraph
  302. Write a list of stack records useful for producing a flame graph. Two
  303. options are available:
  304. calls
  305. create a flamegraph of stack frames
  306. timing
  307. create a flamegraph of microseconds for each stack frame
  308. This format can be used with flamegraph_pl_.
  309. Viewing the Trace Data
  310. ----------------------
  311. You can use kernelshark_ for a GUI, but note that version 2.0.x was broken. If
  312. you have that version you could try building it from source.
  313. The file must have a .dat extension or it is ignored. The program has terse
  314. user interface but is very convenient for viewing U-Boot profile information.
  315. Also available is trace_cmd_ which provides a command-line interface.
  316. Workflow Suggestions
  317. --------------------
  318. The following suggestions may be helpful if you are trying to reduce boot
  319. time:
  320. 1. Enable CONFIG_BOOTSTAGE and CONFIG_BOOTSTAGE_REPORT. This should get
  321. you are helpful overall snapshot of the boot time.
  322. 2. Build U-Boot with tracing and run it. Note the difference in boot time
  323. (it is common for tracing to add 10% to the time)
  324. 3. Collect the trace information as described above. Use this to find where
  325. all the time is being spent.
  326. 4. Take a look at that code and see if you can optimize it. Perhaps it is
  327. possible to speed up the initialization of a device, or remove an unused
  328. feature.
  329. 5. Rebuild, run and collect again. Compare your results.
  330. 6. Keep going until you run out of steam, or your boot is fast enough.
  331. Configuring Trace
  332. -----------------
  333. There are a few parameters in the code that you may want to consider.
  334. There is a function call depth limit (set to 15 by default). When the
  335. stack depth goes above this then no tracing information is recorded.
  336. The maximum depth reached is recorded and displayed by the 'trace stats'
  337. command. While it might be tempting to set the depth limit quite high, this
  338. can dramatically increase the size of the trace output as well as the execution
  339. time.
  340. Future Work
  341. -----------
  342. Tracing could be a little tidier in some areas, for example providing
  343. run-time configuration options for trace.
  344. Some other features that might be useful:
  345. - Trace filter to select which functions are recorded
  346. - Sample-based profiling using a timer interrupt
  347. - Better control over trace depth
  348. - Compression of trace information
  349. .. sectionauthor:: Simon Glass <sjg@chromium.org>
  350. .. April 2013
  351. .. Updated January 2023
  352. .. _kernelshark: https://kernelshark.org/
  353. .. _trace_cmd: https://www.trace-cmd.org/
  354. .. _flamegraph_pl: https://github.com/brendangregg/FlameGraph/blob/master/flamegraph.pl