using_cpp_profilers.rst 7.1 KB

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