Debian Codegraph @

servers/debugger/servers_debugger.cpp

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