README.trace 11 KB

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