servers_debugger.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. /**************************************************************************/
  2. /* servers_debugger.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 "servers_debugger.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/debugger/engine_debugger.h"
  33. #include "core/debugger/engine_profiler.h"
  34. #include "core/io/resource_loader.h"
  35. #include "core/object/script_language.h"
  36. #include "servers/display_server.h"
  37. #define CHECK_SIZE(arr, expected, what) ERR_FAIL_COND_V_MSG((uint32_t)arr.size() < (uint32_t)(expected), false, String("Malformed ") + what + " message from script debugger, message too short. Expected size: " + itos(expected) + ", actual size: " + itos(arr.size()))
  38. #define CHECK_END(arr, expected, what) ERR_FAIL_COND_V_MSG((uint32_t)arr.size() > (uint32_t)expected, false, String("Malformed ") + what + " message from script debugger, message too long. Expected size: " + itos(expected) + ", actual size: " + itos(arr.size()))
  39. Array ServersDebugger::ResourceUsage::serialize() {
  40. infos.sort();
  41. Array arr = { infos.size() * 4 };
  42. for (const ResourceInfo &E : infos) {
  43. arr.push_back(E.path);
  44. arr.push_back(E.format);
  45. arr.push_back(E.type);
  46. arr.push_back(E.vram);
  47. }
  48. return arr;
  49. }
  50. bool ServersDebugger::ResourceUsage::deserialize(const Array &p_arr) {
  51. CHECK_SIZE(p_arr, 1, "ResourceUsage");
  52. uint32_t size = p_arr[0];
  53. ERR_FAIL_COND_V(size % 4, false);
  54. CHECK_SIZE(p_arr, 1 + size, "ResourceUsage");
  55. uint32_t idx = 1;
  56. while (idx < 1 + size) {
  57. ResourceInfo info;
  58. info.path = p_arr[idx];
  59. info.format = p_arr[idx + 1];
  60. info.type = p_arr[idx + 2];
  61. info.vram = p_arr[idx + 3];
  62. infos.push_back(info);
  63. idx += 4;
  64. }
  65. CHECK_END(p_arr, idx, "ResourceUsage");
  66. return true;
  67. }
  68. Array ServersDebugger::ScriptFunctionSignature::serialize() {
  69. return Array{ name, id };
  70. }
  71. bool ServersDebugger::ScriptFunctionSignature::deserialize(const Array &p_arr) {
  72. CHECK_SIZE(p_arr, 2, "ScriptFunctionSignature");
  73. name = p_arr[0];
  74. id = p_arr[1];
  75. CHECK_END(p_arr, 2, "ScriptFunctionSignature");
  76. return true;
  77. }
  78. Array ServersDebugger::ServersProfilerFrame::serialize() {
  79. Array arr = { frame_number, frame_time, process_time, physics_time, physics_frame_time, script_time };
  80. arr.push_back(servers.size());
  81. for (const ServerInfo &s : servers) {
  82. arr.push_back(s.name);
  83. arr.push_back(s.functions.size() * 2);
  84. for (const ServerFunctionInfo &f : s.functions) {
  85. arr.push_back(f.name);
  86. arr.push_back(f.time);
  87. }
  88. }
  89. arr.push_back(script_functions.size() * 5);
  90. for (int i = 0; i < script_functions.size(); i++) {
  91. arr.push_back(script_functions[i].sig_id);
  92. arr.push_back(script_functions[i].call_count);
  93. arr.push_back(script_functions[i].self_time);
  94. arr.push_back(script_functions[i].total_time);
  95. arr.push_back(script_functions[i].internal_time);
  96. }
  97. return arr;
  98. }
  99. bool ServersDebugger::ServersProfilerFrame::deserialize(const Array &p_arr) {
  100. CHECK_SIZE(p_arr, 7, "ServersProfilerFrame");
  101. frame_number = p_arr[0];
  102. frame_time = p_arr[1];
  103. process_time = p_arr[2];
  104. physics_time = p_arr[3];
  105. physics_frame_time = p_arr[4];
  106. script_time = p_arr[5];
  107. int servers_size = p_arr[6];
  108. int idx = 7;
  109. while (servers_size) {
  110. CHECK_SIZE(p_arr, idx + 2, "ServersProfilerFrame");
  111. servers_size--;
  112. ServerInfo si;
  113. si.name = p_arr[idx];
  114. int sub_data_size = p_arr[idx + 1];
  115. idx += 2;
  116. CHECK_SIZE(p_arr, idx + sub_data_size, "ServersProfilerFrame");
  117. for (int j = 0; j < sub_data_size / 2; j++) {
  118. ServerFunctionInfo sf;
  119. sf.name = p_arr[idx];
  120. sf.time = p_arr[idx + 1];
  121. idx += 2;
  122. si.functions.push_back(sf);
  123. }
  124. servers.push_back(si);
  125. }
  126. CHECK_SIZE(p_arr, idx + 1, "ServersProfilerFrame");
  127. int func_size = p_arr[idx];
  128. idx += 1;
  129. CHECK_SIZE(p_arr, idx + func_size, "ServersProfilerFrame");
  130. for (int i = 0; i < func_size / 5; i++) {
  131. ScriptFunctionInfo fi;
  132. fi.sig_id = p_arr[idx];
  133. fi.call_count = p_arr[idx + 1];
  134. fi.self_time = p_arr[idx + 2];
  135. fi.total_time = p_arr[idx + 3];
  136. fi.internal_time = p_arr[idx + 4];
  137. script_functions.push_back(fi);
  138. idx += 5;
  139. }
  140. CHECK_END(p_arr, idx, "ServersProfilerFrame");
  141. return true;
  142. }
  143. Array ServersDebugger::VisualProfilerFrame::serialize() {
  144. Array arr = { frame_number, areas.size() * 3 };
  145. for (int i = 0; i < areas.size(); i++) {
  146. arr.push_back(areas[i].name);
  147. arr.push_back(areas[i].cpu_msec);
  148. arr.push_back(areas[i].gpu_msec);
  149. }
  150. return arr;
  151. }
  152. bool ServersDebugger::VisualProfilerFrame::deserialize(const Array &p_arr) {
  153. CHECK_SIZE(p_arr, 2, "VisualProfilerFrame");
  154. frame_number = p_arr[0];
  155. int size = p_arr[1];
  156. CHECK_SIZE(p_arr, size, "VisualProfilerFrame");
  157. int idx = 2;
  158. areas.resize(size / 3);
  159. RS::FrameProfileArea *w = areas.ptrw();
  160. for (int i = 0; i < size / 3; i++) {
  161. w[i].name = p_arr[idx];
  162. w[i].cpu_msec = p_arr[idx + 1];
  163. w[i].gpu_msec = p_arr[idx + 2];
  164. idx += 3;
  165. }
  166. CHECK_END(p_arr, idx, "VisualProfilerFrame");
  167. return true;
  168. }
  169. class ServersDebugger::ScriptsProfiler : public EngineProfiler {
  170. typedef ServersDebugger::ScriptFunctionSignature FunctionSignature;
  171. typedef ServersDebugger::ScriptFunctionInfo FunctionInfo;
  172. struct ProfileInfoSort {
  173. bool operator()(ScriptLanguage::ProfilingInfo *A, ScriptLanguage::ProfilingInfo *B) const {
  174. return A->total_time > B->total_time;
  175. }
  176. };
  177. Vector<ScriptLanguage::ProfilingInfo> info;
  178. Vector<ScriptLanguage::ProfilingInfo *> ptrs;
  179. HashMap<StringName, int> sig_map;
  180. int max_frame_functions = 16;
  181. public:
  182. void toggle(bool p_enable, const Array &p_opts) {
  183. if (p_enable) {
  184. sig_map.clear();
  185. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  186. ScriptServer::get_language(i)->profiling_start();
  187. if (p_opts.size() == 2 && p_opts[1].get_type() == Variant::BOOL) {
  188. ScriptServer::get_language(i)->profiling_set_save_native_calls(p_opts[1]);
  189. }
  190. }
  191. if (p_opts.size() > 0 && p_opts[0].get_type() == Variant::INT) {
  192. max_frame_functions = MAX(0, int(p_opts[0]));
  193. }
  194. } else {
  195. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  196. ScriptServer::get_language(i)->profiling_stop();
  197. }
  198. }
  199. }
  200. void write_frame_data(Vector<FunctionInfo> &r_funcs, uint64_t &r_total, bool p_accumulated) {
  201. int ofs = 0;
  202. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  203. if (p_accumulated) {
  204. ofs += ScriptServer::get_language(i)->profiling_get_accumulated_data(&info.write[ofs], info.size() - ofs);
  205. } else {
  206. ofs += ScriptServer::get_language(i)->profiling_get_frame_data(&info.write[ofs], info.size() - ofs);
  207. }
  208. }
  209. for (int i = 0; i < ofs; i++) {
  210. ptrs.write[i] = &info.write[i];
  211. }
  212. SortArray<ScriptLanguage::ProfilingInfo *, ProfileInfoSort> sa;
  213. sa.sort(ptrs.ptrw(), ofs);
  214. int to_send = MIN(ofs, max_frame_functions);
  215. // Check signatures first, and compute total time.
  216. r_total = 0;
  217. for (int i = 0; i < to_send; i++) {
  218. if (!sig_map.has(ptrs[i]->signature)) {
  219. int idx = sig_map.size();
  220. FunctionSignature sig;
  221. sig.name = ptrs[i]->signature;
  222. sig.id = idx;
  223. EngineDebugger::get_singleton()->send_message("servers:function_signature", sig.serialize());
  224. sig_map[ptrs[i]->signature] = idx;
  225. }
  226. r_total += ptrs[i]->self_time;
  227. }
  228. // Send frame, script time, functions information then
  229. r_funcs.resize(to_send);
  230. FunctionInfo *w = r_funcs.ptrw();
  231. for (int i = 0; i < to_send; i++) {
  232. if (sig_map.has(ptrs[i]->signature)) {
  233. w[i].sig_id = sig_map[ptrs[i]->signature];
  234. }
  235. w[i].call_count = ptrs[i]->call_count;
  236. w[i].total_time = ptrs[i]->total_time / 1000000.0;
  237. w[i].self_time = ptrs[i]->self_time / 1000000.0;
  238. w[i].internal_time = ptrs[i]->internal_time / 1000000.0;
  239. }
  240. }
  241. ScriptsProfiler() {
  242. info.resize(GLOBAL_GET("debug/settings/profiler/max_functions"));
  243. ptrs.resize(info.size());
  244. }
  245. };
  246. class ServersDebugger::ServersProfiler : public EngineProfiler {
  247. bool skip_profile_frame = false;
  248. typedef ServersDebugger::ServerInfo ServerInfo;
  249. typedef ServersDebugger::ServerFunctionInfo ServerFunctionInfo;
  250. HashMap<StringName, ServerInfo> server_data;
  251. ScriptsProfiler scripts_profiler;
  252. double frame_time = 0;
  253. double process_time = 0;
  254. double physics_time = 0;
  255. double physics_frame_time = 0;
  256. void _send_frame_data(bool p_final) {
  257. ServersDebugger::ServersProfilerFrame frame;
  258. frame.frame_number = Engine::get_singleton()->get_process_frames();
  259. frame.frame_time = frame_time;
  260. frame.process_time = process_time;
  261. frame.physics_time = physics_time;
  262. frame.physics_frame_time = physics_frame_time;
  263. HashMap<StringName, ServerInfo>::Iterator E = server_data.begin();
  264. while (E) {
  265. if (!p_final) {
  266. frame.servers.push_back(E->value);
  267. }
  268. E->value.functions.clear();
  269. ++E;
  270. }
  271. uint64_t time = 0;
  272. scripts_profiler.write_frame_data(frame.script_functions, time, p_final);
  273. frame.script_time = USEC_TO_SEC(time);
  274. if (skip_profile_frame) {
  275. skip_profile_frame = false;
  276. return;
  277. }
  278. if (p_final) {
  279. EngineDebugger::get_singleton()->send_message("servers:profile_total", frame.serialize());
  280. } else {
  281. EngineDebugger::get_singleton()->send_message("servers:profile_frame", frame.serialize());
  282. }
  283. }
  284. public:
  285. void toggle(bool p_enable, const Array &p_opts) {
  286. skip_profile_frame = false;
  287. if (p_enable) {
  288. server_data.clear(); // Clear old profiling data.
  289. } else {
  290. _send_frame_data(true); // Send final frame.
  291. }
  292. scripts_profiler.toggle(p_enable, p_opts);
  293. }
  294. void add(const Array &p_data) {
  295. String name = p_data[0];
  296. if (!server_data.has(name)) {
  297. ServerInfo info;
  298. info.name = name;
  299. server_data[name] = info;
  300. }
  301. ServerInfo &srv = server_data[name];
  302. for (int idx = 1; idx < p_data.size() - 1; idx += 2) {
  303. ServerFunctionInfo fi;
  304. fi.name = p_data[idx];
  305. fi.time = p_data[idx + 1];
  306. srv.functions.push_back(fi);
  307. }
  308. }
  309. void tick(double p_frame_time, double p_process_time, double p_physics_time, double p_physics_frame_time) {
  310. frame_time = p_frame_time;
  311. process_time = p_process_time;
  312. physics_time = p_physics_time;
  313. physics_frame_time = p_physics_frame_time;
  314. _send_frame_data(false);
  315. }
  316. void skip_frame() {
  317. skip_profile_frame = true;
  318. }
  319. };
  320. class ServersDebugger::VisualProfiler : public EngineProfiler {
  321. typedef ServersDebugger::ServerInfo ServerInfo;
  322. typedef ServersDebugger::ServerFunctionInfo ServerFunctionInfo;
  323. HashMap<StringName, ServerInfo> server_data;
  324. public:
  325. void toggle(bool p_enable, const Array &p_opts) {
  326. RS::get_singleton()->set_frame_profiling_enabled(p_enable);
  327. // Send hardware information from the remote device so that it's accurate for remote debugging.
  328. Array hardware_info = {
  329. OS::get_singleton()->get_processor_name(),
  330. RenderingServer::get_singleton()->get_video_adapter_name()
  331. };
  332. EngineDebugger::get_singleton()->send_message("visual:hardware_info", hardware_info);
  333. }
  334. void add(const Array &p_data) {}
  335. void tick(double p_frame_time, double p_process_time, double p_physics_time, double p_physics_frame_time) {
  336. Vector<RS::FrameProfileArea> profile_areas = RS::get_singleton()->get_frame_profile();
  337. ServersDebugger::VisualProfilerFrame frame;
  338. if (!profile_areas.size()) {
  339. return;
  340. }
  341. frame.frame_number = RS::get_singleton()->get_frame_profile_frame();
  342. frame.areas.append_array(profile_areas);
  343. EngineDebugger::get_singleton()->send_message("visual:profile_frame", frame.serialize());
  344. }
  345. };
  346. ServersDebugger *ServersDebugger::singleton = nullptr;
  347. void ServersDebugger::initialize() {
  348. if (EngineDebugger::is_active()) {
  349. memnew(ServersDebugger);
  350. }
  351. }
  352. void ServersDebugger::deinitialize() {
  353. if (singleton) {
  354. memdelete(singleton);
  355. }
  356. }
  357. Error ServersDebugger::_capture(void *p_user, const String &p_cmd, const Array &p_data, bool &r_captured) {
  358. ERR_FAIL_NULL_V(singleton, ERR_BUG);
  359. r_captured = true;
  360. if (p_cmd == "memory") {
  361. singleton->_send_resource_usage();
  362. } else if (p_cmd == "draw") { // Forced redraw.
  363. // For camera override to stay live when the game is paused from the editor.
  364. double delta = 0.0;
  365. if (singleton->last_draw_time) {
  366. delta = (OS::get_singleton()->get_ticks_usec() - singleton->last_draw_time) / 1000000.0;
  367. }
  368. singleton->last_draw_time = OS::get_singleton()->get_ticks_usec();
  369. RenderingServer::get_singleton()->sync();
  370. if (RenderingServer::get_singleton()->has_changed()) {
  371. RenderingServer::get_singleton()->draw(true, delta);
  372. }
  373. EngineDebugger::get_singleton()->send_message("servers:drawn", Array());
  374. } else if (p_cmd == "foreground") {
  375. singleton->last_draw_time = 0.0;
  376. DisplayServer::get_singleton()->window_move_to_foreground();
  377. singleton->servers_profiler->skip_frame();
  378. } else {
  379. r_captured = false;
  380. }
  381. return OK;
  382. }
  383. void ServersDebugger::_send_resource_usage() {
  384. ServersDebugger::ResourceUsage usage;
  385. List<RS::TextureInfo> tinfo;
  386. RS::get_singleton()->texture_debug_usage(&tinfo);
  387. for (const RS::TextureInfo &E : tinfo) {
  388. ServersDebugger::ResourceInfo info;
  389. info.path = E.path;
  390. info.vram = E.bytes;
  391. info.id = E.texture;
  392. switch (E.type) {
  393. case RS::TextureType::TEXTURE_TYPE_2D:
  394. info.type = "Texture2D";
  395. break;
  396. case RS::TextureType::TEXTURE_TYPE_3D:
  397. info.type = "Texture3D";
  398. break;
  399. case RS::TextureType::TEXTURE_TYPE_LAYERED:
  400. info.type = "TextureLayered";
  401. break;
  402. }
  403. String possible_type = _get_resource_type_from_path(E.path);
  404. if (!possible_type.is_empty()) {
  405. info.type = possible_type;
  406. }
  407. if (E.depth == 0) {
  408. info.format = itos(E.width) + "x" + itos(E.height) + " " + Image::get_format_name(E.format);
  409. } else {
  410. info.format = itos(E.width) + "x" + itos(E.height) + "x" + itos(E.depth) + " " + Image::get_format_name(E.format);
  411. }
  412. usage.infos.push_back(info);
  413. }
  414. List<RS::MeshInfo> mesh_info;
  415. RS::get_singleton()->mesh_debug_usage(&mesh_info);
  416. for (const RS::MeshInfo &E : mesh_info) {
  417. ServersDebugger::ResourceInfo info;
  418. info.path = E.path;
  419. // We use 64-bit integers to avoid overflow, if for whatever reason, the sum is bigger than 4GB.
  420. uint64_t vram = E.vertex_buffer_size + E.attribute_buffer_size + E.skin_buffer_size + E.index_buffer_size + E.blend_shape_buffer_size + E.lod_index_buffers_size;
  421. // But can info.vram even hold that, and why is it an int instead of an uint?
  422. info.vram = vram;
  423. // Even though these empty meshes can be indicative of issues somewhere else
  424. // for UX reasons, we don't want to show them.
  425. if (vram == 0 && E.path.is_empty()) {
  426. continue;
  427. }
  428. info.id = E.mesh;
  429. info.type = "Mesh";
  430. String possible_type = _get_resource_type_from_path(E.path);
  431. if (!possible_type.is_empty()) {
  432. info.type = possible_type;
  433. }
  434. info.format = itos(E.vertex_count) + " Vertices";
  435. usage.infos.push_back(info);
  436. }
  437. EngineDebugger::get_singleton()->send_message("servers:memory_usage", usage.serialize());
  438. }
  439. // Done on a best-effort basis.
  440. String ServersDebugger::_get_resource_type_from_path(const String &p_path) {
  441. if (p_path.is_empty()) {
  442. return "";
  443. }
  444. if (!ResourceLoader::exists(p_path)) {
  445. return "";
  446. }
  447. if (ResourceCache::has(p_path)) {
  448. Ref<Resource> resource = ResourceCache::get_ref(p_path);
  449. return resource->get_class();
  450. } else {
  451. // This doesn't work all the time for embedded resources.
  452. String resource_type = ResourceLoader::get_resource_type(p_path);
  453. if (resource_type != "") {
  454. return resource_type;
  455. }
  456. }
  457. return "";
  458. }
  459. ServersDebugger::ServersDebugger() {
  460. singleton = this;
  461. // Generic servers profiler (audio/physics/...)
  462. servers_profiler.instantiate();
  463. servers_profiler->bind("servers");
  464. // Visual Profiler (cpu/gpu times)
  465. visual_profiler.instantiate();
  466. visual_profiler->bind("visual");
  467. EngineDebugger::Capture servers_cap(nullptr, &_capture);
  468. EngineDebugger::register_message_capture("servers", servers_cap);
  469. }
  470. ServersDebugger::~ServersDebugger() {
  471. EngineDebugger::unregister_message_capture("servers");
  472. singleton = nullptr;
  473. }