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