servers_debugger.cpp 15 KB

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