profiling.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /**************************************************************************/
  2. /* profiling.cpp */
  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. #include "profiling.h"
  31. #if defined(GODOT_USE_TRACY)
  32. // Use the tracy profiler.
  33. #include "core/os/mutex.h"
  34. #include "core/templates/paged_allocator.h"
  35. namespace tracy {
  36. static bool configured = false;
  37. static const char dummy_string[] = "dummy";
  38. static tracy::SourceLocationData dummy_source_location = tracy::SourceLocationData{ dummy_string, dummy_string, dummy_string, 0, 0 };
  39. // Implementation similar to StringName.
  40. struct StringInternData {
  41. StringName name;
  42. CharString name_utf8;
  43. uint32_t hash = 0;
  44. StringInternData *prev = nullptr;
  45. StringInternData *next = nullptr;
  46. StringInternData() {}
  47. };
  48. struct SourceLocationInternData {
  49. const StringInternData *file;
  50. const StringInternData *function;
  51. const StringInternData *name;
  52. tracy::SourceLocationData source_location_data;
  53. uint32_t function_ptr_hash = 0;
  54. SourceLocationInternData *prev = nullptr;
  55. SourceLocationInternData *next = nullptr;
  56. SourceLocationInternData() {}
  57. };
  58. struct TracyInternTable {
  59. constexpr static uint32_t TABLE_BITS = 16;
  60. constexpr static uint32_t TABLE_LEN = 1 << TABLE_BITS;
  61. constexpr static uint32_t TABLE_MASK = TABLE_LEN - 1;
  62. static inline BinaryMutex mutex;
  63. static inline SourceLocationInternData *source_location_table[TABLE_LEN];
  64. static inline PagedAllocator<SourceLocationInternData> source_location_allocator;
  65. static inline StringInternData *string_table[TABLE_LEN];
  66. static inline PagedAllocator<StringInternData> string_allocator;
  67. };
  68. const StringInternData *_intern_name(const StringName &p_name) {
  69. CRASH_COND(!configured);
  70. const uint32_t hash = p_name.hash();
  71. const uint32_t idx = hash & TracyInternTable::TABLE_MASK;
  72. StringInternData *_data = TracyInternTable::string_table[idx];
  73. while (_data) {
  74. if (_data->hash == hash) {
  75. return _data;
  76. }
  77. _data = _data->next;
  78. }
  79. _data = TracyInternTable::string_allocator.alloc();
  80. _data->name = p_name;
  81. _data->name_utf8 = p_name.operator String().utf8();
  82. _data->next = TracyInternTable::string_table[idx];
  83. _data->prev = nullptr;
  84. if (TracyInternTable::string_table[idx]) {
  85. TracyInternTable::string_table[idx]->prev = _data;
  86. }
  87. TracyInternTable::string_table[idx] = _data;
  88. return _data;
  89. }
  90. const tracy::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) {
  91. ERR_FAIL_COND_V(!configured, &dummy_source_location);
  92. const uint32_t hash = HashMapHasherDefault::hash(p_function_ptr);
  93. const uint32_t idx = hash & TracyInternTable::TABLE_MASK;
  94. MutexLock lock(TracyInternTable::mutex);
  95. SourceLocationInternData *_data = TracyInternTable::source_location_table[idx];
  96. while (_data) {
  97. if (_data->function_ptr_hash == hash && _data->source_location_data.line == p_line && _data->file->name == p_file && _data->function->name == p_function && _data->name->name == p_name) {
  98. return &_data->source_location_data;
  99. }
  100. _data = _data->next;
  101. }
  102. _data = TracyInternTable::source_location_allocator.alloc();
  103. _data->function_ptr_hash = hash;
  104. _data->file = _intern_name(p_file);
  105. _data->function = _intern_name(p_function);
  106. _data->name = _intern_name(p_name);
  107. _data->source_location_data.file = _data->file->name_utf8.get_data();
  108. _data->source_location_data.function = _data->function->name_utf8.get_data();
  109. _data->source_location_data.name = _data->name->name_utf8.get_data();
  110. _data->source_location_data.line = p_line;
  111. _data->source_location_data.color = p_is_script ? 0x478cbf : 0; // godot_logo_blue
  112. _data->next = TracyInternTable::source_location_table[idx];
  113. _data->prev = nullptr;
  114. if (TracyInternTable::source_location_table[idx]) {
  115. TracyInternTable::source_location_table[idx]->prev = _data;
  116. }
  117. TracyInternTable::source_location_table[idx] = _data;
  118. return &_data->source_location_data;
  119. }
  120. } // namespace tracy
  121. void godot_init_profiler() {
  122. MutexLock lock(tracy::TracyInternTable::mutex);
  123. ERR_FAIL_COND(tracy::configured);
  124. for (uint32_t i = 0; i < tracy::TracyInternTable::TABLE_LEN; i++) {
  125. tracy::TracyInternTable::source_location_table[i] = nullptr;
  126. }
  127. for (uint32_t i = 0; i < tracy::TracyInternTable::TABLE_LEN; i++) {
  128. tracy::TracyInternTable::string_table[i] = nullptr;
  129. }
  130. tracy::configured = true;
  131. // Send our first event to tracy; otherwise it doesn't start collecting data.
  132. // FrameMark is kind of fitting because it communicates "this is where we started tracing".
  133. FrameMark;
  134. }
  135. void godot_cleanup_profiler() {
  136. MutexLock lock(tracy::TracyInternTable::mutex);
  137. ERR_FAIL_COND(!tracy::configured);
  138. for (uint32_t i = 0; i < tracy::TracyInternTable::TABLE_LEN; i++) {
  139. while (tracy::TracyInternTable::source_location_table[i]) {
  140. tracy::SourceLocationInternData *d = tracy::TracyInternTable::source_location_table[i];
  141. tracy::TracyInternTable::source_location_table[i] = tracy::TracyInternTable::source_location_table[i]->next;
  142. tracy::TracyInternTable::source_location_allocator.free(d);
  143. }
  144. }
  145. for (uint32_t i = 0; i < tracy::TracyInternTable::TABLE_LEN; i++) {
  146. while (tracy::TracyInternTable::string_table[i]) {
  147. tracy::StringInternData *d = tracy::TracyInternTable::string_table[i];
  148. tracy::TracyInternTable::string_table[i] = tracy::TracyInternTable::string_table[i]->next;
  149. tracy::TracyInternTable::string_allocator.free(d);
  150. }
  151. }
  152. tracy::configured = false;
  153. }
  154. #elif defined(GODOT_USE_PERFETTO)
  155. PERFETTO_TRACK_EVENT_STATIC_STORAGE();
  156. void godot_init_profiler() {
  157. perfetto::TracingInitArgs args;
  158. args.backends |= perfetto::kSystemBackend;
  159. perfetto::Tracing::Initialize(args);
  160. perfetto::TrackEvent::Register();
  161. }
  162. void godot_cleanup_profiler() {
  163. // Stub
  164. }
  165. #elif defined(GODOT_USE_INSTRUMENTS)
  166. namespace apple::instruments {
  167. os_log_t LOG;
  168. os_log_t LOG_TRACING;
  169. } // namespace apple::instruments
  170. void godot_init_profiler() {
  171. static bool initialized = false;
  172. if (initialized) {
  173. return;
  174. }
  175. initialized = true;
  176. apple::instruments::LOG = os_log_create("org.godotengine.godot", OS_LOG_CATEGORY_POINTS_OF_INTEREST);
  177. #ifdef INSTRUMENTS_SAMPLE_CALLSTACKS
  178. apple::instruments::LOG_TRACING = os_log_create("org.godotengine.godot", OS_LOG_CATEGORY_DYNAMIC_STACK_TRACING);
  179. #else
  180. apple::instruments::LOG_TRACING = os_log_create("org.godotengine.godot", "tracing");
  181. #endif
  182. }
  183. void godot_cleanup_profiler() {
  184. }
  185. #else
  186. void godot_init_profiler() {
  187. // Stub
  188. }
  189. void godot_cleanup_profiler() {
  190. // Stub
  191. }
  192. #endif