servers_debugger.cpp 15 KB

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