profiling.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /**************************************************************************/
  2. /* profiling.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #pragma once
  31. #include "profiling.gen.h"
  32. // This header provides profiling primitives (implemented as macros) for various backends.
  33. // See the "No profiling" branch at the bottom for a short description of the functions.
  34. // To configure / use the profiler, use the --profiler_path and other --profiler_* arguments
  35. // when compiling Godot. You can also find details in the SCSub file (in this folder).
  36. // Note: It is highly recommended to avoid including this header in other header files.
  37. // Prefer including it in .cpp files only. The reason is that we want to keep
  38. // the recompile cost of changing the profiler as low as possible.
  39. #if defined(GODOT_USE_TRACY)
  40. // Use the tracy profiler.
  41. #include "core/string/string_name.h"
  42. #define TRACY_ENABLE
  43. #include <tracy/Tracy.hpp>
  44. // Hijacking the tracy namespace so we can use their macros.
  45. namespace tracy {
  46. const SourceLocationData *intern_source_location(const void *p_function_ptr, const StringName &p_file, const StringName &p_function, const StringName &p_name, uint32_t p_line, bool p_is_script);
  47. } //namespace tracy
  48. // Define tracing macros.
  49. #define GodotProfileFrameMark FrameMark
  50. #define GodotProfileZone(m_zone_name) ZoneNamedN(GD_UNIQUE_NAME(__godot_tracy_szone_), m_zone_name, true)
  51. #define GodotProfileZoneGroupedFirst(m_group_name, m_zone_name) ZoneNamedN(__godot_tracy_zone_##m_group_name, m_zone_name, true)
  52. #define GodotProfileZoneGroupedEndEarly(m_group_name, m_zone_name) __godot_tracy_zone_##m_group_name.~ScopedZone();
  53. #ifndef TRACY_CALLSTACK
  54. #define GodotProfileZoneGrouped(m_group_name, m_zone_name) \
  55. GodotProfileZoneGroupedEndEarly(m_group_name, m_zone_name); \
  56. static constexpr tracy::SourceLocationData TracyConcat(__tracy_source_location, TracyLine){ m_zone_name, TracyFunction, TracyFile, (uint32_t)TracyLine, 0 }; \
  57. new (&__godot_tracy_zone_##m_group_name) tracy::ScopedZone(&TracyConcat(__tracy_source_location, TracyLine), true)
  58. #else
  59. #define GodotProfileZoneGrouped(m_group_name, m_zone_name) \
  60. GodotProfileZoneGroupedEndEarly(m_group_name, m_zone_name); \
  61. static constexpr tracy::SourceLocationData TracyConcat(__tracy_source_location, TracyLine){ m_zone_name, TracyFunction, TracyFile, (uint32_t)TracyLine, 0 }; \
  62. new (&__godot_tracy_zone_##m_group_name) tracy::ScopedZone(&TracyConcat(__tracy_source_location, TracyLine), TRACY_CALLSTACK, true)
  63. #endif
  64. #define GodotProfileZoneScript(m_ptr, m_file, m_function, m_name, m_line) \
  65. tracy::ScopedZone __godot_tracy_script(tracy::intern_source_location(m_ptr, m_file, m_function, m_name, m_line, true))
  66. #define GodotProfileZoneScriptSystemCall(m_ptr, m_file, m_function, m_name, m_line) \
  67. tracy::ScopedZone __godot_tracy_zone_system_call(tracy::intern_source_location(m_ptr, m_file, m_function, m_name, m_line, false))
  68. // Memory allocation
  69. #ifdef GODOT_PROFILER_TRACK_MEMORY
  70. #define GodotProfileAlloc(m_ptr, m_size) \
  71. GODOT_GCC_WARNING_PUSH_AND_IGNORE("-Wmaybe-uninitialized") \
  72. TracyAlloc(m_ptr, m_size); \
  73. GODOT_GCC_WARNING_POP
  74. #define GodotProfileFree(m_ptr) TracyFree(m_ptr)
  75. #else
  76. #define GodotProfileAlloc(m_ptr, m_size)
  77. #define GodotProfileFree(m_ptr)
  78. #endif
  79. void godot_init_profiler();
  80. void godot_cleanup_profiler();
  81. #elif defined(GODOT_USE_PERFETTO)
  82. // Use the perfetto profiler.
  83. #include <perfetto.h>
  84. #include "core/typedefs.h"
  85. PERFETTO_DEFINE_CATEGORIES(
  86. perfetto::Category("godot")
  87. .SetDescription("All Godot Events"), );
  88. // See PERFETTO_INTERNAL_SCOPED_EVENT_FINALIZER
  89. struct PerfettoGroupedEventEnder {
  90. _FORCE_INLINE_ void _end_now() {
  91. TRACE_EVENT_END("godot");
  92. }
  93. _FORCE_INLINE_ ~PerfettoGroupedEventEnder() {
  94. _end_now();
  95. }
  96. };
  97. #define GodotProfileFrameMark // TODO
  98. #define GodotProfileZone(m_zone_name) TRACE_EVENT("godot", m_zone_name);
  99. #define GodotProfileZoneGroupedFirst(m_group_name, m_zone_name) \
  100. TRACE_EVENT_BEGIN("godot", m_zone_name); \
  101. PerfettoGroupedEventEnder __godot_perfetto_zone_##m_group_name
  102. #define GodotProfileZoneGroupedEndEarly(m_group_name, m_zone_name) __godot_perfetto_zone_##m_group_name.~PerfettoGroupedEventEnder()
  103. #define GodotProfileZoneGrouped(m_group_name, m_zone_name) \
  104. __godot_perfetto_zone_##m_group_name._end_now(); \
  105. TRACE_EVENT_BEGIN("godot", m_zone_name);
  106. #define GodotProfileZoneScript(m_ptr, m_file, m_function, m_name, m_line)
  107. #define GodotProfileZoneScriptSystemCall(m_ptr, m_file, m_function, m_name, m_line)
  108. #define GodotProfileAlloc(m_ptr, m_size)
  109. #define GodotProfileFree(m_ptr)
  110. void godot_init_profiler();
  111. void godot_cleanup_profiler();
  112. #elif defined(GODOT_USE_INSTRUMENTS)
  113. #include <os/log.h>
  114. #include <os/signpost.h>
  115. namespace apple::instruments {
  116. extern os_log_t LOG;
  117. extern os_log_t LOG_TRACING;
  118. typedef void (*DeferFunc)();
  119. class Defer {
  120. public:
  121. explicit Defer(DeferFunc p_fn) :
  122. _fn(p_fn) {}
  123. ~Defer() {
  124. _fn();
  125. }
  126. private:
  127. DeferFunc _fn;
  128. };
  129. } // namespace apple::instruments
  130. #define GodotProfileFrameMark \
  131. os_signpost_event_emit(apple::instruments::LOG, OS_SIGNPOST_ID_EXCLUSIVE, "Frame");
  132. #define GodotProfileZoneGroupedFirst(m_group_name, m_zone_name) \
  133. os_signpost_interval_begin(apple::instruments::LOG_TRACING, OS_SIGNPOST_ID_EXCLUSIVE, m_zone_name); \
  134. apple::instruments::DeferFunc _GD_VARNAME_CONCAT_(defer__fn, _, m_group_name) = []() { \
  135. os_signpost_interval_end(apple::instruments::LOG_TRACING, OS_SIGNPOST_ID_EXCLUSIVE, m_zone_name); \
  136. }; \
  137. apple::instruments::Defer _GD_VARNAME_CONCAT_(__instruments_defer_zone_end__, _, m_group_name)(_GD_VARNAME_CONCAT_(defer__fn, _, m_group_name));
  138. #define GodotProfileZoneGroupedEndEarly(m_group_name, m_zone_name) \
  139. _GD_VARNAME_CONCAT_(__instruments_defer_zone_end__, _, m_group_name).~Defer();
  140. #define GodotProfileZoneGrouped(m_group_name, m_zone_name) \
  141. GodotProfileZoneGroupedEndEarly(m_group_name, m_zone_name); \
  142. os_signpost_interval_begin(apple::instruments::LOG_TRACING, OS_SIGNPOST_ID_EXCLUSIVE, m_zone_name); \
  143. _GD_VARNAME_CONCAT_(defer__fn, _, m_group_name) = []() { \
  144. os_signpost_interval_end(apple::instruments::LOG_TRACING, OS_SIGNPOST_ID_EXCLUSIVE, m_zone_name); \
  145. }; \
  146. new (&_GD_VARNAME_CONCAT_(__instruments_defer_zone_end__, _, m_group_name)) apple::instruments::Defer(_GD_VARNAME_CONCAT_(defer__fn, _, m_group_name));
  147. #define GodotProfileZone(m_zone_name) \
  148. GodotProfileZoneGroupedFirst(__COUNTER__, m_zone_name)
  149. #define GodotProfileZoneScript(m_ptr, m_file, m_function, m_name, m_line)
  150. #define GodotProfileZoneScriptSystemCall(m_ptr, m_file, m_function, m_name, m_line)
  151. // Instruments has its own memory profiling, so these are no-ops.
  152. #define GodotProfileAlloc(m_ptr, m_size)
  153. #define GodotProfileFree(m_ptr)
  154. void godot_init_profiler();
  155. void godot_cleanup_profiler();
  156. #else
  157. // No profiling; all macros are stubs.
  158. void godot_init_profiler();
  159. void godot_cleanup_profiler();
  160. // Tell the profiling backend that a new frame has started.
  161. #define GodotProfileFrameMark
  162. // Defines a profile zone from here to the end of the scope.
  163. #define GodotProfileZone(m_zone_name)
  164. // Defines a profile zone group. The first profile zone starts immediately,
  165. // and ends either when the next zone starts, or when the scope ends.
  166. #define GodotProfileZoneGroupedFirst(m_group_name, m_zone_name)
  167. // End the profile zone group's current profile zone now.
  168. #define GodotProfileZoneGroupedEndEarly(m_group_name, m_zone_name)
  169. // Replace the profile zone group's current profile zone.
  170. // The new zone ends either when the next zone starts, or when the scope ends.
  171. #define GodotProfileZoneGrouped(m_group_name, m_zone_name)
  172. // Tell the profiling backend that an allocation happened, with its location and size.
  173. #define GodotProfileAlloc(m_ptr, m_size)
  174. // Tell the profiling backend that an allocation was freed.
  175. // There must be a one to one correspondence of GodotProfileAlloc and GodotProfileFree calls.
  176. #define GodotProfileFree(m_ptr)
  177. // Define a zone for a script call (dynamic source location).
  178. // m_ptr is a pointer to the function instance, which will be used for the lookup.
  179. // m_file, m_function, m_name are StringNames, and m_line is uint32_t
  180. #define GodotProfileZoneScript(m_ptr, m_file, m_function, m_name, m_line)
  181. // Define a zone for a system call from a script (dynamic source location).
  182. #define GodotProfileZoneScriptSystemCall(m_ptr, m_file, m_function, m_name, m_line)
  183. #endif