tracing_profilers.rst 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. .. _doc_tracing_profilers:
  2. Tracing Profilers
  3. =================
  4. Godot currently supports two tracing profilers:
  5. `Tracy <https://github.com/wolfpld/tracy>`__ and `Perfetto <https://perfetto.dev>`__.
  6. In order to use either of them, you'll need to build the engine from source.
  7. If you've never done this before, please read
  8. :ref:`these docs <doc_compiling_index>` for the platform you want to profile on.
  9. You'll need to perform the same steps here, but with some additional arguments
  10. for ``scons``.
  11. .. _doc_tracy_profiler:
  12. Tracy for Windows, Linux, and macOS
  13. -----------------------------------
  14. Tracy is an Open Source profiler that runs on a wide variety of platforms,
  15. including Windows, Linux, and macOS. While it is primarily a tracing profiler,
  16. it can also periodically sample data like a
  17. :ref:`sampling profiler <doc_sampling_profilers>`, giving some of the benefits
  18. of both approaches.
  19. Build Godot with Tracy support
  20. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  21. First, clone the latest version of the Tracy source code ("0.13.0" at the
  22. time of writing) using Git:
  23. .. code-block:: shell
  24. git clone -b v0.13.0 --single-branch https://github.com/wolfpld/tracy.git
  25. This will create a ``tracy`` directory - you can place this anywhere.
  26. Next, build the release templates for your platform using ``scons``, but adding
  27. the ``profiler=tracy profiler_path=path/to/tracy`` arguments with the real path
  28. to the ``tracy`` directory, as well as ``debug_symbols=yes`` to allow Tracy's
  29. sampling features to work.
  30. .. note::
  31. You don't have to build release templates, you could also build debug
  32. templates, or even the editor. However, it's generally recommended to
  33. profile release templates, because that is the version your players will
  34. use, and it will perform differently than other types of builds.
  35. For example, to build release templates for Windows:
  36. .. code-block:: shell
  37. scons platform=windows target=template_release debug_symbols=yes profiler=tracy profiler_path=path/to/tracy
  38. Get the Tracy "server"
  39. ~~~~~~~~~~~~~~~~~~~~~~
  40. In Tracy terminology, the application you are profiling is the "client", and
  41. the one receiving the data is the "server".
  42. If you are on Windows, you can download a pre-built ``tracy-profiler.exe``
  43. from the Tracy `releases page <https://github.com/wolfpld/tracy/releases>`_.
  44. However, if you're on Linux or macOS, you'll either need to find a pre-built
  45. binary from a package manager (like ``brew`` or ``nix``), or build it from
  46. source yourself.
  47. .. note::
  48. If you do use a pre-built binary, be sure to use the same version that
  49. you used when building Godot.
  50. Build the Tracy server from source
  51. ++++++++++++++++++++++++++++++++++
  52. In order to build Tracy, you'll need to install ``cmake``, which can be
  53. downloaded from the `CMake website <https://cmake.org/download/>`_, or
  54. possibly installed via a package manager (like ``brew`` or ``nix``).
  55. The full instructions for building Tracy from source can be found in the
  56. `Tracy manual <https://github.com/wolfpld/tracy/releases/latest/download/tracy.pdf>`_,
  57. but here is the TL;DR:
  58. .. code-block:: shell
  59. # On Linux, Tracy uses Wayland by default, so if you use X11 add -DLEGACY=1
  60. cmake -B profiler/build -S profiler -DCMAKE_BUILD_TYPE=Release
  61. cmake --build profiler/build --config Release --parallel
  62. This will place the binary at ``tracy/profiler/build/tracy-profiler`` or
  63. ``tracy/profiler/build/tracy-profiler.exe`` (on Windows).
  64. Record a trace
  65. ~~~~~~~~~~~~~~
  66. Launch the Tracy server - you'll see something like this:
  67. .. image:: img/cpp_profiler_tracy_start.webp
  68. Press "connect". This will ensure tracy makes a connection immediately when
  69. the game launches. If you forget to press "connect", Tracy will store system
  70. events in RAM, which can quickly blow up your memory usage (see the
  71. ``TRACY_ON_DEMAND`` documentation).
  72. Now, export your game using the release templates you built above, and run it.
  73. As soon as both are running, and you have pressed the "Connect" button in
  74. Tracy, you'll see data coming in:
  75. .. image:: img/cpp_profiler_tracy_recording.webp
  76. When you think you've gathered enough data, press the "Stop" button. If you
  77. clicked somewhere and the box with the "Stop" button disappeared, you can
  78. click the top-left most icon to bring it back.
  79. Examining the trace
  80. ~~~~~~~~~~~~~~~~~~~
  81. Here are some of the basic controls:
  82. - Zoom in/out with the mouse wheel
  83. - Right click and drag to move forward/backward on the timeline
  84. - In the top bar, click the left and right arrow buttons by "Frames" to move a single frame on the timeline
  85. To learn more, see the
  86. `Tracy manual <https://github.com/wolfpld/tracy/releases/latest/download/tracy.pdf>`_.
  87. Perfetto for Android
  88. --------------------
  89. Perfetto is the default tracing system for Android. In fact, its system tracing
  90. service has been built into the platform since Android 9.
  91. Build Godot with Perfetto support
  92. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  93. First, clone the latest version of the Perfetto source code ("53.0" at the
  94. time of writing) using Git:
  95. .. code-block:: shell
  96. git clone -b v53.0 --single-branch https://github.com/google/perfetto.git
  97. This will create a ``perfetto`` directory - you can place this anywhere.
  98. Next, build the Android debug or release templates for your architecture using
  99. ``scons`` (per :ref:`Compiling for Android <doc_compiling_for_android>`), but
  100. adding the ``profiler=perfetto profiler_path=path/to/perfetto`` arguments with
  101. the real path to the ``perfetto`` directory.
  102. .. note::
  103. It's generally recommended to profile release templates, because that is
  104. the version your players will use, and it will perform differently than
  105. other types of builds. However, in the case of Android, it can sometimes
  106. be useful to use debug templates, because Godot can only do remote
  107. debugging of games exported from debug templates.
  108. For example, to build the release templates for arm64:
  109. .. code-block:: shell
  110. scons platform=android target=template_release arch=arm64 generate_android_binaries=yes profiler=perfetto profiler_path=path/to/perfetto
  111. Configuration
  112. ~~~~~~~~~~~~~
  113. Perfetto requires a configuration file to tell it which events to track.
  114. Create a file called ``godot.config`` inside of the ``perfetto`` directory
  115. with this content:
  116. .. code-block:: text
  117. # Trace for 10 seconds.
  118. duration_ms: 10000
  119. buffers {
  120. size_kb: 32768
  121. fill_policy: RING_BUFFER
  122. }
  123. # Write to file once every second to prevent overflowing the buffer.
  124. write_into_file: true
  125. file_write_period_ms: 1000
  126. # Track events in the "godot" category.
  127. data_sources {
  128. config {
  129. name: "track_event"
  130. track_event_config {
  131. enabled_categories: "godot"
  132. }
  133. }
  134. }
  135. Record a trace
  136. ~~~~~~~~~~~~~~
  137. Finally, launch your game on an Android device using the export templates you
  138. built earlier.
  139. When you're ready to record a trace (for example, when you've hit the part of
  140. your game that is exhibiting performance issues), you can use this script that
  141. comes with the Perfetto source code:
  142. .. code-block:: shell
  143. cd perfetto
  144. ./tools/record_android_trace -c godot.config
  145. This will record for 10 seconds (per the configuration), or until you press
  146. :kbd:`Ctrl + C`.
  147. Examining the trace
  148. ~~~~~~~~~~~~~~~~~~~
  149. As soon as that script exits, it will launch the Perfetto UI in a web browser.
  150. To see the Godot events, expand the row for your application by clicking on its
  151. Android "Unique Name" (Perfetto will also include some events from system
  152. services in the trace).
  153. .. image:: img/cpp_profiler_perfetto.webp
  154. Then you can use the ``WASD`` keys to navigate the graph:
  155. - Press :kbd:`A` or :kbd:`D` to navigate forward or backward along the timeline
  156. - Press :kbd:`W` or :kbd:`S` to zoom in or out
  157. You'll probably need to zoom a bit before you're able to see the individual
  158. events from Godot.
  159. To learn more, see the
  160. `Perfetto UI documentation <https://perfetto.dev/docs/visualization/perfetto-ui>`_.