servers_debugger.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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/marshalls.h"
  35. #include "servers/display_server.h"
  36. #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()))
  37. #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()))
  38. Array ServersDebugger::ResourceUsage::serialize() {
  39. infos.sort();
  40. Array arr;
  41. arr.push_back(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. Array arr;
  70. arr.push_back(name);
  71. arr.push_back(id);
  72. return arr;
  73. }
  74. bool ServersDebugger::ScriptFunctionSignature::deserialize(const Array &p_arr) {
  75. CHECK_SIZE(p_arr, 2, "ScriptFunctionSignature");
  76. name = p_arr[0];
  77. id = p_arr[1];
  78. CHECK_END(p_arr, 2, "ScriptFunctionSignature");
  79. return true;
  80. }
  81. Array ServersDebugger::ServersProfilerFrame::serialize() {
  82. Array arr;
  83. arr.push_back(frame_number);
  84. arr.push_back(frame_time);
  85. arr.push_back(process_time);
  86. arr.push_back(physics_time);
  87. arr.push_back(physics_frame_time);
  88. arr.push_back(script_time);
  89. arr.push_back(servers.size());
  90. for (const ServerInfo &s : servers) {
  91. arr.push_back(s.name);
  92. arr.push_back(s.functions.size() * 2);
  93. for (const ServerFunctionInfo &f : s.functions) {
  94. arr.push_back(f.name);
  95. arr.push_back(f.time);
  96. }
  97. }
  98. arr.push_back(script_functions.size() * 5);
  99. for (int i = 0; i < script_functions.size(); i++) {
  100. arr.push_back(script_functions[i].sig_id);
  101. arr.push_back(script_functions[i].call_count);
  102. arr.push_back(script_functions[i].self_time);
  103. arr.push_back(script_functions[i].total_time);
  104. arr.push_back(script_functions[i].internal_time);
  105. }
  106. return arr;
  107. }
  108. bool ServersDebugger::ServersProfilerFrame::deserialize(const Array &p_arr) {
  109. CHECK_SIZE(p_arr, 7, "ServersProfilerFrame");
  110. frame_number = p_arr[0];
  111. frame_time = p_arr[1];
  112. process_time = p_arr[2];
  113. physics_time = p_arr[3];
  114. physics_frame_time = p_arr[4];
  115. script_time = p_arr[5];
  116. int servers_size = p_arr[6];
  117. int idx = 7;
  118. while (servers_size) {
  119. CHECK_SIZE(p_arr, idx + 2, "ServersProfilerFrame");
  120. servers_size--;
  121. ServerInfo si;
  122. si.name = p_arr[idx];
  123. int sub_data_size = p_arr[idx + 1];
  124. idx += 2;
  125. CHECK_SIZE(p_arr, idx + sub_data_size, "ServersProfilerFrame");
  126. for (int j = 0; j < sub_data_size / 2; j++) {
  127. ServerFunctionInfo sf;
  128. sf.name = p_arr[idx];
  129. sf.time = p_arr[idx + 1];
  130. idx += 2;
  131. si.functions.push_back(sf);
  132. }
  133. servers.push_back(si);
  134. }
  135. CHECK_SIZE(p_arr, idx + 1, "ServersProfilerFrame");
  136. int func_size = p_arr[idx];
  137. idx += 1;
  138. CHECK_SIZE(p_arr, idx + func_size, "ServersProfilerFrame");
  139. for (int i = 0; i < func_size / 5; i++) {
  140. ScriptFunctionInfo fi;
  141. fi.sig_id = p_arr[idx];
  142. fi.call_count = p_arr[idx + 1];
  143. fi.self_time = p_arr[idx + 2];
  144. fi.total_time = p_arr[idx + 3];
  145. fi.internal_time = p_arr[idx + 4];
  146. script_functions.push_back(fi);
  147. idx += 5;
  148. }
  149. CHECK_END(p_arr, idx, "ServersProfilerFrame");
  150. return true;
  151. }
  152. Array ServersDebugger::VisualProfilerFrame::serialize() {
  153. Array arr;
  154. arr.push_back(frame_number);
  155. arr.push_back(areas.size() * 3);
  156. for (int i = 0; i < areas.size(); i++) {
  157. arr.push_back(areas[i].name);
  158. arr.push_back(areas[i].cpu_msec);
  159. arr.push_back(areas[i].gpu_msec);
  160. }
  161. return arr;
  162. }
  163. bool ServersDebugger::VisualProfilerFrame::deserialize(const Array &p_arr) {
  164. CHECK_SIZE(p_arr, 2, "VisualProfilerFrame");
  165. frame_number = p_arr[0];
  166. int size = p_arr[1];
  167. CHECK_SIZE(p_arr, size, "VisualProfilerFrame");
  168. int idx = 2;
  169. areas.resize(size / 3);
  170. RS::FrameProfileArea *w = areas.ptrw();
  171. for (int i = 0; i < size / 3; i++) {
  172. w[i].name = p_arr[idx];
  173. w[i].cpu_msec = p_arr[idx + 1];
  174. w[i].gpu_msec = p_arr[idx + 2];
  175. idx += 3;
  176. }
  177. CHECK_END(p_arr, idx, "VisualProfilerFrame");
  178. return true;
  179. }
  180. class ServersDebugger::ScriptsProfiler : public EngineProfiler {
  181. typedef ServersDebugger::ScriptFunctionSignature FunctionSignature;
  182. typedef ServersDebugger::ScriptFunctionInfo FunctionInfo;
  183. struct ProfileInfoSort {
  184. bool operator()(ScriptLanguage::ProfilingInfo *A, ScriptLanguage::ProfilingInfo *B) const {
  185. return A->total_time > B->total_time;
  186. }
  187. };
  188. Vector<ScriptLanguage::ProfilingInfo> info;
  189. Vector<ScriptLanguage::ProfilingInfo *> ptrs;
  190. HashMap<StringName, int> sig_map;
  191. int max_frame_functions = 16;
  192. public:
  193. void toggle(bool p_enable, const Array &p_opts) {
  194. if (p_enable) {
  195. sig_map.clear();
  196. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  197. ScriptServer::get_language(i)->profiling_start();
  198. if (p_opts.size() == 2 && p_opts[1].get_type() == Variant::BOOL) {
  199. ScriptServer::get_language(i)->profiling_set_save_native_calls(p_opts[1]);
  200. }
  201. }
  202. if (p_opts.size() > 0 && p_opts[0].get_type() == Variant::INT) {
  203. max_frame_functions = MAX(0, int(p_opts[0]));
  204. }
  205. } else {
  206. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  207. ScriptServer::get_language(i)->profiling_stop();
  208. }
  209. }
  210. }
  211. void write_frame_data(Vector<FunctionInfo> &r_funcs, uint64_t &r_total, bool p_accumulated) {
  212. int ofs = 0;
  213. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  214. if (p_accumulated) {
  215. ofs += ScriptServer::get_language(i)->profiling_get_accumulated_data(&info.write[ofs], info.size() - ofs);
  216. } else {
  217. ofs += ScriptServer::get_language(i)->profiling_get_frame_data(&info.write[ofs], info.size() - ofs);
  218. }
  219. }
  220. for (int i = 0; i < ofs; i++) {
  221. ptrs.write[i] = &info.write[i];
  222. }
  223. SortArray<ScriptLanguage::ProfilingInfo *, ProfileInfoSort> sa;
  224. sa.sort(ptrs.ptrw(), ofs);
  225. int to_send = MIN(ofs, max_frame_functions);
  226. // Check signatures first, and compute total time.
  227. r_total = 0;
  228. for (int i = 0; i < to_send; i++) {
  229. if (!sig_map.has(ptrs[i]->signature)) {
  230. int idx = sig_map.size();
  231. FunctionSignature sig;
  232. sig.name = ptrs[i]->signature;
  233. sig.id = idx;
  234. EngineDebugger::get_singleton()->send_message("servers:function_signature", sig.serialize());
  235. sig_map[ptrs[i]->signature] = idx;
  236. }
  237. r_total += ptrs[i]->self_time;
  238. }
  239. // Send frame, script time, functions information then
  240. r_funcs.resize(to_send);
  241. FunctionInfo *w = r_funcs.ptrw();
  242. for (int i = 0; i < to_send; i++) {
  243. if (sig_map.has(ptrs[i]->signature)) {
  244. w[i].sig_id = sig_map[ptrs[i]->signature];
  245. }
  246. w[i].call_count = ptrs[i]->call_count;
  247. w[i].total_time = ptrs[i]->total_time / 1000000.0;
  248. w[i].self_time = ptrs[i]->self_time / 1000000.0;
  249. w[i].internal_time = ptrs[i]->internal_time / 1000000.0;
  250. }
  251. }
  252. ScriptsProfiler() {
  253. info.resize(GLOBAL_GET("debug/settings/profiler/max_functions"));
  254. ptrs.resize(info.size());
  255. }
  256. };
  257. class ServersDebugger::ServersProfiler : public EngineProfiler {
  258. bool skip_profile_frame = false;
  259. typedef ServersDebugger::ServerInfo ServerInfo;
  260. typedef ServersDebugger::ServerFunctionInfo ServerFunctionInfo;
  261. HashMap<StringName, ServerInfo> server_data;
  262. ScriptsProfiler scripts_profiler;
  263. double frame_time = 0;
  264. double process_time = 0;
  265. double physics_time = 0;
  266. double physics_frame_time = 0;
  267. void _send_frame_data(bool p_final) {
  268. ServersDebugger::ServersProfilerFrame frame;
  269. frame.frame_number = Engine::get_singleton()->get_process_frames();
  270. frame.frame_time = frame_time;
  271. frame.process_time = process_time;
  272. frame.physics_time = physics_time;
  273. frame.physics_frame_time = physics_frame_time;
  274. HashMap<StringName, ServerInfo>::Iterator E = server_data.begin();
  275. while (E) {
  276. if (!p_final) {
  277. frame.servers.push_back(E->value);
  278. }
  279. E->value.functions.clear();
  280. ++E;
  281. }
  282. uint64_t time = 0;
  283. scripts_profiler.write_frame_data(frame.script_functions, time, p_final);
  284. frame.script_time = USEC_TO_SEC(time);
  285. if (skip_profile_frame) {
  286. skip_profile_frame = false;
  287. return;
  288. }
  289. if (p_final) {
  290. EngineDebugger::get_singleton()->send_message("servers:profile_total", frame.serialize());
  291. } else {
  292. EngineDebugger::get_singleton()->send_message("servers:profile_frame", frame.serialize());
  293. }
  294. }
  295. public:
  296. void toggle(bool p_enable, const Array &p_opts) {
  297. skip_profile_frame = false;
  298. if (p_enable) {
  299. server_data.clear(); // Clear old profiling data.
  300. } else {
  301. _send_frame_data(true); // Send final frame.
  302. }
  303. scripts_profiler.toggle(p_enable, p_opts);
  304. }
  305. void add(const Array &p_data) {
  306. String name = p_data[0];
  307. if (!server_data.has(name)) {
  308. ServerInfo info;
  309. info.name = name;
  310. server_data[name] = info;
  311. }
  312. ServerInfo &srv = server_data[name];
  313. for (int idx = 1; idx < p_data.size() - 1; idx += 2) {
  314. ServerFunctionInfo fi;
  315. fi.name = p_data[idx];
  316. fi.time = p_data[idx + 1];
  317. srv.functions.push_back(fi);
  318. }
  319. }
  320. void tick(double p_frame_time, double p_process_time, double p_physics_time, double p_physics_frame_time) {
  321. frame_time = p_frame_time;
  322. process_time = p_process_time;
  323. physics_time = p_physics_time;
  324. physics_frame_time = p_physics_frame_time;
  325. _send_frame_data(false);
  326. }
  327. void skip_frame() {
  328. skip_profile_frame = true;
  329. }
  330. };
  331. class ServersDebugger::VisualProfiler : public EngineProfiler {
  332. typedef ServersDebugger::ServerInfo ServerInfo;
  333. typedef ServersDebugger::ServerFunctionInfo ServerFunctionInfo;
  334. HashMap<StringName, ServerInfo> server_data;
  335. public:
  336. void toggle(bool p_enable, const Array &p_opts) {
  337. RS::get_singleton()->set_frame_profiling_enabled(p_enable);
  338. }
  339. void add(const Array &p_data) {}
  340. void tick(double p_frame_time, double p_process_time, double p_physics_time, double p_physics_frame_time) {
  341. Vector<RS::FrameProfileArea> profile_areas = RS::get_singleton()->get_frame_profile();
  342. ServersDebugger::VisualProfilerFrame frame;
  343. if (!profile_areas.size()) {
  344. return;
  345. }
  346. frame.frame_number = RS::get_singleton()->get_frame_profile_frame();
  347. frame.areas.append_array(profile_areas);
  348. EngineDebugger::get_singleton()->send_message("visual:profile_frame", frame.serialize());
  349. }
  350. };
  351. ServersDebugger *ServersDebugger::singleton = nullptr;
  352. void ServersDebugger::initialize() {
  353. if (EngineDebugger::is_active()) {
  354. memnew(ServersDebugger);
  355. }
  356. }
  357. void ServersDebugger::deinitialize() {
  358. if (singleton) {
  359. memdelete(singleton);
  360. }
  361. }
  362. Error ServersDebugger::_capture(void *p_user, const String &p_cmd, const Array &p_data, bool &r_captured) {
  363. ERR_FAIL_NULL_V(singleton, ERR_BUG);
  364. r_captured = true;
  365. if (p_cmd == "memory") {
  366. singleton->_send_resource_usage();
  367. } else if (p_cmd == "draw") { // Forced redraw.
  368. // For camera override to stay live when the game is paused from the editor.
  369. double delta = 0.0;
  370. if (singleton->last_draw_time) {
  371. delta = (OS::get_singleton()->get_ticks_usec() - singleton->last_draw_time) / 1000000.0;
  372. }
  373. singleton->last_draw_time = OS::get_singleton()->get_ticks_usec();
  374. RenderingServer::get_singleton()->sync();
  375. if (RenderingServer::get_singleton()->has_changed()) {
  376. RenderingServer::get_singleton()->draw(true, delta);
  377. }
  378. EngineDebugger::get_singleton()->send_message("servers:drawn", Array());
  379. } else if (p_cmd == "foreground") {
  380. singleton->last_draw_time = 0.0;
  381. DisplayServer::get_singleton()->window_move_to_foreground();
  382. singleton->servers_profiler->skip_frame();
  383. } else {
  384. r_captured = false;
  385. }
  386. return OK;
  387. }
  388. void ServersDebugger::_send_resource_usage() {
  389. ServersDebugger::ResourceUsage usage;
  390. List<RS::TextureInfo> tinfo;
  391. RS::get_singleton()->texture_debug_usage(&tinfo);
  392. for (const RS::TextureInfo &E : tinfo) {
  393. ServersDebugger::ResourceInfo info;
  394. info.path = E.path;
  395. info.vram = E.bytes;
  396. info.id = E.texture;
  397. info.type = "Texture";
  398. if (E.depth == 0) {
  399. info.format = itos(E.width) + "x" + itos(E.height) + " " + Image::get_format_name(E.format);
  400. } else {
  401. info.format = itos(E.width) + "x" + itos(E.height) + "x" + itos(E.depth) + " " + Image::get_format_name(E.format);
  402. }
  403. usage.infos.push_back(info);
  404. }
  405. EngineDebugger::get_singleton()->send_message("servers:memory_usage", usage.serialize());
  406. }
  407. ServersDebugger::ServersDebugger() {
  408. singleton = this;
  409. // Generic servers profiler (audio/physics/...)
  410. servers_profiler.instantiate();
  411. servers_profiler->bind("servers");
  412. // Visual Profiler (cpu/gpu times)
  413. visual_profiler.instantiate();
  414. visual_profiler->bind("visual");
  415. EngineDebugger::Capture servers_cap(nullptr, &_capture);
  416. EngineDebugger::register_message_capture("servers", servers_cap);
  417. }
  418. ServersDebugger::~ServersDebugger() {
  419. EngineDebugger::unregister_message_capture("servers");
  420. singleton = nullptr;
  421. }