using_cpp_profilers.rst 6.8 KB

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