using_cpp_profilers.rst 7.7 KB

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