README.trace 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. #
  2. # Copyright (c) 2013 The Chromium OS Authors.
  3. #
  4. # SPDX-License-Identifier: GPL-2.0+
  5. #
  6. Tracing in U-Boot
  7. =================
  8. U-Boot supports a simple tracing feature which allows a record of excecution
  9. to be collected and sent to a host machine for analysis. At present the
  10. main use for this is to profile boot time.
  11. Overview
  12. --------
  13. The trace feature uses GCC's instrument-functions feature to trace all
  14. function entry/exit points. These are then recorded in a memory buffer.
  15. The memory buffer can be saved to the host over a network link using
  16. tftpput or by writing to an attached memory device such as MMC.
  17. On the host, the file is first converted with a tool called 'proftool',
  18. which extracts useful information from it. The resulting trace output
  19. resembles that emitted by Linux's ftrace feature, so can be visually
  20. displayed by pytimechart.
  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 include/configs/sandbox.h (if not already there)
  27. #define CONFIG_TRACE
  28. #define CONFIG_CMD_TRACE
  29. #define CONFIG_TRACE_BUFFER_SIZE (16 << 20)
  30. #define CONFIG_TRACE_EARLY_SIZE (8 << 20)
  31. #define CONFIG_TRACE_EARLY
  32. #define CONFIG_TRACE_EARLY_ADDR 0x00100000
  33. Build sandbox U-Boot with tracing enabled:
  34. $ make FTRACE=1 O=sandbox sandbox_config
  35. $ make FTRACE=1 O=sandbox
  36. Run sandbox, wait for a bit of trace information to appear, and then capture
  37. a trace:
  38. $ ./sandbox/u-boot
  39. U-Boot 2013.04-rc2-00100-ga72fcef (Apr 17 2013 - 19:25:24)
  40. DRAM: 128 MiB
  41. trace: enabled
  42. Using default environment
  43. In: serial
  44. Out: serial
  45. Err: serial
  46. =>trace stats
  47. 671,406 function sites
  48. 69,712 function calls
  49. 0 untracked function calls
  50. 73,373 traced function calls
  51. 16 maximum observed call depth
  52. 15 call depth limit
  53. 66,491 calls not traced due to depth
  54. =>trace stats
  55. 671,406 function sites
  56. 1,279,450 function calls
  57. 0 untracked function calls
  58. 950,490 traced function calls (333217 dropped due to overflow)
  59. 16 maximum observed call depth
  60. 15 call depth limit
  61. 1,275,767 calls not traced due to depth
  62. =>trace calls 0 e00000
  63. Call list dumped to 00000000, size 0xae0a40
  64. =>print
  65. baudrate=115200
  66. profbase=0
  67. profoffset=ae0a40
  68. profsize=e00000
  69. stderr=serial
  70. stdin=serial
  71. stdout=serial
  72. Environment size: 117/8188 bytes
  73. =>sb save host 0 trace 0 ${profoffset}
  74. 11405888 bytes written in 10 ms (1.1 GiB/s)
  75. =>reset
  76. Then run proftool to convert the trace information to ftrace format.
  77. $ ./sandbox/tools/proftool -m sandbox/System.map -p trace dump-ftrace >trace.txt
  78. Finally run pytimechart to display it:
  79. $ pytimechart trace.txt
  80. Using this tool you can zoom and pan across the trace, with the function
  81. calls on the left and little marks representing the start and end of each
  82. function.
  83. CONFIG Options
  84. --------------
  85. - CONFIG_TRACE
  86. Enables the trace feature in U-Boot.
  87. - CONFIG_CMD_TRACE
  88. Enables the trace command.
  89. - CONFIG_TRACE_BUFFER_SIZE
  90. Size of trace buffer to allocate for U-Boot. This buffer is
  91. used after relocation, as a place to put function tracing
  92. information. The address of the buffer is determined by
  93. the relocation code.
  94. - CONFIG_TRACE_EARLY
  95. Define this to start tracing early, before relocation.
  96. - CONFIG_TRACE_EARLY_SIZE
  97. Size of 'early' trace buffer. Before U-Boot has relocated
  98. it doesn't have a proper trace buffer. On many boards
  99. you can define an area of memory to use for the trace
  100. buffer until the 'real' trace buffer is available after
  101. relocation. The contents of this buffer are then copied to
  102. the real buffer.
  103. - CONFIG_TRACE_EARLY_ADDR
  104. Address of early trace buffer
  105. Building U-Boot with Tracing Enabled
  106. ------------------------------------
  107. Pass 'FTRACE=1' to the U-Boot Makefile to actually instrument the code.
  108. This is kept as a separate option so that it is easy to enable/disable
  109. instrumenting from the command line instead of having to change board
  110. config files.
  111. Collecting Trace Data
  112. ---------------------
  113. When you run U-Boot on your board it will collect trace data up to the
  114. limit of the trace buffer size you have specified. Once that is exhausted
  115. no more data will be collected.
  116. Collecting trace data has an affect on execution time/performance. You
  117. will notice this particularly with trvial functions - the overhead of
  118. recording their execution may even exceed their normal execution time.
  119. In practice this doesn't matter much so long as you are aware of the
  120. effect. Once you have done your optimisations, turn off tracing before
  121. doing end-to-end timing.
  122. The best time to start tracing is right at the beginning of U-Boot. The
  123. best time to stop tracing is right at the end. In practice it is hard
  124. to achieve these ideals.
  125. This implementation enables tracing early in board_init_f(). This means
  126. that it captures most of the board init process, missing only the
  127. early architecture-specific init. However, it also misses the entire
  128. SPL stage if there is one.
  129. U-Boot typically ends with a 'bootm' command which loads and runs an
  130. OS. There is useful trace data in the execution of that bootm
  131. command. Therefore this implementation provides a way to collect trace
  132. data after bootm has finished processing, but just before it jumps to
  133. the OS. In practical terms, U-Boot runs the 'fakegocmd' environment
  134. variable at this point. This variable should have a short script which
  135. collects the trace data and writes it somewhere.
  136. Trace data collection relies on a microsecond timer, accesed through
  137. timer_get_us(). So the first think you should do is make sure that
  138. this produces sensible results for your board. Suitable sources for
  139. this timer include high resolution timers, PWMs or profile timers if
  140. available. Most modern SOCs have a suitable timer for this. Make sure
  141. that you mark this timer (and anything it calls) with
  142. __attribute__((no_instrument_function)) so that the trace library can
  143. use it without causing an infinite loop.
  144. Commands
  145. --------
  146. The trace command has variable sub-commands:
  147. - stats
  148. Display tracing statistics
  149. - pause
  150. Pause tracing
  151. - resume
  152. Resume tracing
  153. - funclist [<addr> <size>]
  154. Dump a list of functions into the buffer
  155. - calls [<addr> <size>]
  156. Dump function call trace into buffer
  157. If the address and size are not given, these are obtained from environment
  158. variables (see below). In any case the environment variables are updated
  159. after the command runs.
  160. Environment Variables
  161. ---------------------
  162. The following are used:
  163. - profbase
  164. Base address of trace output buffer
  165. - profoffset
  166. Offset of first unwritten byte in trace output buffer
  167. - profsize
  168. Size of trace output buffer
  169. All of these are set by the 'trace calls' command.
  170. These variables keep track of the amount of data written to the trace
  171. output buffer by the 'trace' command. The trace commands which write data
  172. to the output buffer can use these to specify the buffer to write to, and
  173. update profoffset each time. This allows successive commands to append data
  174. to the same buffer, for example:
  175. trace funclist 10000 e00000
  176. trace calls
  177. (the latter command appends more data to the buffer).
  178. - fakegocmd
  179. Specifies commands to run just before booting the OS. This
  180. is a useful time to write the trace data to the host for
  181. processing.
  182. Writing Out Trace Data
  183. ----------------------
  184. Once the trace data is in an output buffer in memory there are various ways
  185. to transmit it to the host. Notably you can use tftput to send the data
  186. over a network link:
  187. fakegocmd=trace pause; usb start; set autoload n; bootp;
  188. trace calls 10000000 1000000;
  189. tftpput ${profbase} ${profoffset} 192.168.1.4:/tftpboot/calls
  190. This starts up USB (to talk to an attached USB Ethernet dongle), writes
  191. a trace log to address 10000000 and sends it to a host machine using
  192. TFTP. After this, U-Boot will boot the OS normally, albeit a little
  193. later.
  194. Converting Trace Output Data
  195. ----------------------------
  196. The trace output data is kept in a binary format which is not documented
  197. here. To convert it into something useful, you can use proftool.
  198. This tool must be given the U-Boot map file and the trace data received
  199. from running that U-Boot. It produces a text output file.
  200. Options
  201. -m <map_file>
  202. Specify U-Boot map file
  203. -p <trace_file>
  204. Specifiy profile/trace file
  205. Commands:
  206. - dump-ftrace
  207. Write a text dump of the file in Linux ftrace format to stdout
  208. Viewing the Trace Data
  209. ----------------------
  210. You can use pytimechart for this (sudo apt-get pytimechart might work on
  211. your Debian-style machine, and use your favourite search engine to obtain
  212. documentation). It expects the file to have a .txt extension. The program
  213. has terse user interface but is very convenient for viewing U-Boot
  214. profile information.
  215. Workflow Suggestions
  216. --------------------
  217. The following suggestions may be helpful if you are trying to reduce boot
  218. time:
  219. 1. Enable CONFIG_BOOTSTAGE and CONFIG_BOOTSTAGE_REPORT. This should get
  220. you are helpful overall snapshot of the boot time.
  221. 2. Build U-Boot with tracing and run it. Note the difference in boot time
  222. (it is common for tracing to add 10% to the time)
  223. 3. Collect the trace information as descibed above. Use this to find where
  224. all the time is being spent.
  225. 4. Take a look at that code and see if you can optimise it. Perhaps it is
  226. possible to speed up the initialisation of a device, or remove an unused
  227. feature.
  228. 5. Rebuild, run and collect again. Compare your results.
  229. 6. Keep going until you run out of steam, or your boot is fast enough.
  230. Configuring Trace
  231. -----------------
  232. There are a few parameters in the code that you may want to consider.
  233. There is a function call depth limit (set to 15 by default). When the
  234. stack depth goes above this then no tracing information is recorded.
  235. The maximum depth reached is recorded and displayed by the 'trace stats'
  236. command.
  237. Future Work
  238. -----------
  239. Tracing could be a little tidier in some areas, for example providing
  240. run-time configuration options for trace.
  241. Some other features that might be useful:
  242. - Trace filter to select which functions are recorded
  243. - Sample-based profiling using a timer interrupt
  244. - Better control over trace depth
  245. - Compression of trace information
  246. Simon Glass <sjg@chromium.org>
  247. April 2013