using_cpp_profilers.rst 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. .. _doc_using_cpp_profilers:
  2. Using C++ profilers
  3. ===================
  4. To optimize Godot's performance, you need to know what to optimize first.
  5. To this end, profilers are useful tools.
  6. .. note::
  7. There is a built-in GDScript profiler in the editor, but using C++ profiler
  8. may be useful in cases where the GDScript profiler is not accurate enough
  9. or is missing information due to bugs in the profiler.
  10. Recommended profilers
  11. ---------------------
  12. - `VerySleepy <http://www.codersnotes.com/sleepy/>`__ (Windows only)
  13. - `HotSpot <https://github.com/KDAB/hotspot>`__ (Linux only)
  14. - `Xcode Instruments <https://developer.apple.com/xcode/>`__ (macOS only)
  15. These profilers may not be the most powerful or flexible options, but their
  16. standalone operation and limited feature set tends to make them easier to use.
  17. .. note::
  18. In its latest stable release (0.90 as of writing), VerySleepy will freeze
  19. when loading debug symbols. To solve this, exit VerySleepy then save
  20. `this dbghelpms.dll file <https://github.com/AlanIWBFT/verysleepy/raw/0523fde86f24139fd4a041319f8b59ace12f602b/dbghelp_x64/dbghelpms.dll>`__
  21. at the root of the VerySleepy installation folder.
  22. This folder is generally located at ``C:\Program Files\Very Sleepy CS``.
  23. If you are debugging a 32-bit application instead of 64-bit, save
  24. `this dbghelpms.dll file <https://github.com/AlanIWBFT/verysleepy/raw/0523fde86f24139fd4a041319f8b59ace12f602b/dbghelp_x86/dbghelpms.dll>`__
  25. in the ``32`` folder of the VerySleepy installation folder.
  26. Setting up Godot
  27. ----------------
  28. To get useful profiling information, it is **absolutely required** to use a Godot
  29. build that includes debugging symbols. Official binaries do not include debugging
  30. symbols, since these would make the download size significantly larger.
  31. To get profiling data that best matches the production environment, you should
  32. compile binaries with the following SCons options:
  33. - For editor binaries: ``target=release_debug use_lto=yes``
  34. - For debug export templates: ``target=release_debug use_lto=yes``
  35. - For release export templates: ``tools=no target=release debug_symbols=yes``
  36. - ``debug_symbols=yes`` is required as export templates are stripped from debugging symbols by default.
  37. It is possible to run a profiler on less optimized builds (e.g. ``target=debug`` without LTO),
  38. but results will naturally be less representative of real world conditions.
  39. .. warning::
  40. Do *not* strip debugging symbols on the binaries using the ``strip`` command
  41. after compiling the binaries. Otherwise, you will no longer get useful
  42. profiling information when running a profiler.
  43. Benchmarking startup/shutdown times
  44. -----------------------------------
  45. If you're looking into optimizing Godot's startup/shutdown performance,
  46. you can tell the profiler to use the ``--quit`` command line option on the Godot binary.
  47. This will exit Godot just after it finished starting.
  48. The ``--quit`` option works with ``--editor``, ``--project-manager`` or
  49. ``--path <path to project directory>`` (which runs a project directly).
  50. .. seealso::
  51. See :ref:`doc_command_line_tutorial` for more command line arguments
  52. supported by Godot.
  53. Profiler-specific instructions
  54. ------------------------------
  55. VerySleepy
  56. ^^^^^^^^^^
  57. - Start the Godot editor or your project first.
  58. If you start the project manager, make sure to edit or run a project first.
  59. Otherwise, the profiler will not track the child process since the project manager
  60. will spawn a child process for every project edited or run.
  61. - Open VerySleepy and select the Godot executable in the list of processes on the left:
  62. .. image:: img/cpp_profiler_verysleepy_select_process.png
  63. - Click the **Profile All** button on the right to start profiling.
  64. - Perform the actions you wish to profile in the editor or project. When you're done, click **Stop** (*not* Abort).
  65. - Wait for the results window to appear.
  66. - Once the results window appears, filter the view to remove external modules (such as the graphics driver).
  67. You can filter by module by finding a line whose **Module** matches the Godot
  68. executable name, right-clicking that line then choosing
  69. **Filter Module to <Godot executable name>** in the dropdown that appears.
  70. - Your results window should now look something like this:
  71. .. image:: img/cpp_profiler_verysleepy_results_filtered.png
  72. HotSpot
  73. ^^^^^^^
  74. - Open HotSpot. Click **Record Data**:
  75. .. image:: img/cpp_profiler_hotspot_welcome.png
  76. - In the next window, specify the path to the Godot binary that includes debug symbols.
  77. - Specify command line arguments to run a specific project, with or without the editor.
  78. - The path to the working directory can be anything if an absolute path is used
  79. for the ``--path`` command line argument. Otherwise, it must be set to that
  80. the relative path to the project is valid.
  81. - Make sure **Elevate Privileges** is checked if you have administrative privileges.
  82. While not essential for profiling Godot, this will ensure all events can be captured.
  83. Otherwise, some events may be missing in the capture.
  84. Your settings should now look something like this:
  85. .. image:: img/cpp_profiler_hotspot_record.png
  86. - Click **Start Recording** and perform the actions you wish to profile in the editor/project.
  87. - Quit the editor/project normally or use the **Stop Profiling** button in HotSpot
  88. to stop profiling early. Stopping profiling early can result in cleaner profiles
  89. if you're not interested in the engine's quit procedure.
  90. - Click **View Results** and wait for the profiling visualization to be generated:
  91. .. image:: img/cpp_profiler_hotspot_view_results.png
  92. - Use the tabs at the top to navigate between the different views. These views
  93. show the same data, but in different ways. The **Flame Graph** tab is a good
  94. way to see which functions take up the most time at a glance. These functions
  95. are therefore the most important ones to optimize, since optimizing them will
  96. improve performance the most.
  97. - At the bottom of all tabs except **Summary**, you will also see a list of CPU threads
  98. started by the engine among with the CPU utilization for each thread.
  99. This lets you see threads that can be a bottleneck at a given point in time.
  100. .. image:: img/cpp_profiler_hotspot_flame_graph.png
  101. .. note::
  102. If you don't want the startup procedure to be included in the profile, you
  103. can also attach HotSpot to a running process by clicking **Record Data**
  104. then setting the **Launch Application** dropdown option to **Attach To
  105. Process(es)**.
  106. This process attachment-based workflow is similar to the one used by VerySleepy.
  107. Xcode Instruments
  108. ^^^^^^^^^^^^^^^^^
  109. - Open Xcode. Select **Open Developer Tool** - **Instruments** from the **Xcode** app menu:
  110. - Double-click on **Time Profiler** in the **Instruments** window:
  111. .. image:: img/cpp_profiler_xcode_menu.png
  112. - In the Time Profiler window, click on the **Target** menu, select **Choose target...**
  113. and specify the path to the Godot binary, command line arguments and environment variables
  114. in the next window.
  115. .. image:: img/cpp_profiler_time_profiler.png
  116. - You can also attach the Time Profiler to a running process by selecting it from the **Target**
  117. menu.
  118. - Click the **Start an immediate mode recording** button to start profiling.
  119. .. image:: img/cpp_profiler_time_profiler_record.png
  120. - Perform the actions you wish to profile in the editor or project. When you're done,
  121. click the **Stop** button.
  122. - Wait for the results to appear.
  123. - At the bottom of the window you will see a call tree for all CPU threads started, and
  124. the **Heaviest Stack Trace** overview.
  125. - Select **Hide system libraries** in the **Call Tree** menu (at the bottom of window) to
  126. remove external modules.
  127. - You can use the timeline at the top of the window to display details for the specific time period.
  128. .. image:: img/cpp_profiler_time_profiler_result.png