Skip to content

Commit

Permalink
Merge pull request #57715 from Faless/debugger/4.x_core_includes_and_…
Browse files Browse the repository at this point in the history
…servers

[Debugger] Move most profilers to ServersDebugger, fix core includes.
  • Loading branch information
akien-mga authored Feb 9, 2022
2 parents 2c28729 + 6583797 commit d22ac13
Show file tree
Hide file tree
Showing 26 changed files with 1,007 additions and 741 deletions.
65 changes: 10 additions & 55 deletions core/core_bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2376,21 +2376,18 @@ bool EngineDebugger::is_active() {
return ::EngineDebugger::is_active();
}

void EngineDebugger::register_profiler(const StringName &p_name, const Callable &p_toggle, const Callable &p_add, const Callable &p_tick) {
ERR_FAIL_COND_MSG(profilers.has(p_name) || has_profiler(p_name), "Profiler already registered: " + p_name);
profilers.insert(p_name, ProfilerCallable(p_toggle, p_add, p_tick));
ProfilerCallable &p = profilers[p_name];
::EngineDebugger::Profiler profiler(
&p,
&EngineDebugger::call_toggle,
&EngineDebugger::call_add,
&EngineDebugger::call_tick);
::EngineDebugger::register_profiler(p_name, profiler);
void EngineDebugger::register_profiler(const StringName &p_name, Ref<EngineProfiler> p_profiler) {
ERR_FAIL_COND(p_profiler.is_null());
ERR_FAIL_COND_MSG(p_profiler->is_bound(), "Profiler already registered.");
ERR_FAIL_COND_MSG(profilers.has(p_name) || has_profiler(p_name), "Profiler name already in use: " + p_name);
Error err = p_profiler->bind(p_name);
ERR_FAIL_COND_MSG(err != OK, "Profiler failed to register with error: " + itos(err));
profilers.insert(p_name, p_profiler);
}

void EngineDebugger::unregister_profiler(const StringName &p_name) {
ERR_FAIL_COND_MSG(!profilers.has(p_name), "Profiler not registered: " + p_name);
::EngineDebugger::unregister_profiler(p_name);
profilers[p_name]->unbind();
profilers.erase(p_name);
}

Expand Down Expand Up @@ -2435,45 +2432,6 @@ void EngineDebugger::send_message(const String &p_msg, const Array &p_data) {
::EngineDebugger::get_singleton()->send_message(p_msg, p_data);
}

void EngineDebugger::call_toggle(void *p_user, bool p_enable, const Array &p_opts) {
Callable &toggle = ((ProfilerCallable *)p_user)->callable_toggle;
if (toggle.is_null()) {
return;
}
Variant enable = p_enable, opts = p_opts;
const Variant *args[2] = { &enable, &opts };
Variant retval;
Callable::CallError err;
toggle.call(args, 2, retval, err);
ERR_FAIL_COND_MSG(err.error != Callable::CallError::CALL_OK, "Error calling 'toggle' to callable: " + Variant::get_callable_error_text(toggle, args, 2, err));
}

void EngineDebugger::call_add(void *p_user, const Array &p_data) {
Callable &add = ((ProfilerCallable *)p_user)->callable_add;
if (add.is_null()) {
return;
}
Variant data = p_data;
const Variant *args[1] = { &data };
Variant retval;
Callable::CallError err;
add.call(args, 1, retval, err);
ERR_FAIL_COND_MSG(err.error != Callable::CallError::CALL_OK, "Error calling 'add' to callable: " + Variant::get_callable_error_text(add, args, 1, err));
}

void EngineDebugger::call_tick(void *p_user, double p_frame_time, double p_idle_time, double p_physics_time, double p_physics_frame_time) {
Callable &tick = ((ProfilerCallable *)p_user)->callable_tick;
if (tick.is_null()) {
return;
}
Variant frame_time = p_frame_time, idle_time = p_idle_time, physics_time = p_physics_time, physics_frame_time = p_physics_frame_time;
const Variant *args[4] = { &frame_time, &idle_time, &physics_time, &physics_frame_time };
Variant retval;
Callable::CallError err;
tick.call(args, 4, retval, err);
ERR_FAIL_COND_MSG(err.error != Callable::CallError::CALL_OK, "Error calling 'tick' to callable: " + Variant::get_callable_error_text(tick, args, 4, err));
}

Error EngineDebugger::call_capture(void *p_user, const String &p_cmd, const Array &p_data, bool &r_captured) {
Callable &capture = *(Callable *)p_user;
if (capture.is_null()) {
Expand All @@ -2495,19 +2453,16 @@ EngineDebugger::~EngineDebugger() {
::EngineDebugger::unregister_message_capture(E.key);
}
captures.clear();
for (const KeyValue<StringName, ProfilerCallable> &E : profilers) {
::EngineDebugger::unregister_profiler(E.key);
}
profilers.clear();
}

EngineDebugger *EngineDebugger::singleton = nullptr;

void EngineDebugger::_bind_methods() {
ClassDB::bind_method(D_METHOD("is_active"), &EngineDebugger::is_active);

ClassDB::bind_method(D_METHOD("register_profiler", "name", "toggle", "add", "tick"), &EngineDebugger::register_profiler);
ClassDB::bind_method(D_METHOD("register_profiler", "name", "profiler"), &EngineDebugger::register_profiler);
ClassDB::bind_method(D_METHOD("unregister_profiler", "name"), &EngineDebugger::unregister_profiler);

ClassDB::bind_method(D_METHOD("is_profiling", "name"), &EngineDebugger::is_profiling);
ClassDB::bind_method(D_METHOD("has_profiler", "name"), &EngineDebugger::has_profiler);

Expand Down
25 changes: 3 additions & 22 deletions core/core_bind.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#ifndef CORE_BIND_H
#define CORE_BIND_H

#include "core/debugger/engine_profiler.h"
#include "core/io/compression.h"
#include "core/io/dir_access.h"
#include "core/io/file_access.h"
Expand Down Expand Up @@ -673,25 +674,8 @@ class Engine : public Object {
class EngineDebugger : public Object {
GDCLASS(EngineDebugger, Object);

class ProfilerCallable {
friend class EngineDebugger;

Callable callable_toggle;
Callable callable_add;
Callable callable_tick;

public:
ProfilerCallable() {}

ProfilerCallable(const Callable &p_toggle, const Callable &p_add, const Callable &p_tick) {
callable_toggle = p_toggle;
callable_add = p_add;
callable_tick = p_tick;
}
};

Map<StringName, Callable> captures;
Map<StringName, ProfilerCallable> profilers;
Map<StringName, Ref<EngineProfiler>> profilers;

protected:
static void _bind_methods();
Expand All @@ -702,7 +686,7 @@ class EngineDebugger : public Object {

bool is_active();

void register_profiler(const StringName &p_name, const Callable &p_toggle, const Callable &p_add, const Callable &p_tick);
void register_profiler(const StringName &p_name, Ref<EngineProfiler> p_profiler);
void unregister_profiler(const StringName &p_name);
bool is_profiling(const StringName &p_name);
bool has_profiler(const StringName &p_name);
Expand All @@ -715,9 +699,6 @@ class EngineDebugger : public Object {

void send_message(const String &p_msg, const Array &p_data);

static void call_toggle(void *p_user, bool p_enable, const Array &p_opts);
static void call_add(void *p_user, const Array &p_data);
static void call_tick(void *p_user, double p_frame_time, double p_idle_time, double p_physics_time, double p_physics_frame_time);
static Error call_capture(void *p_user, const String &p_cmd, const Array &p_data, bool &r_captured);

EngineDebugger() { singleton = this; }
Expand Down
183 changes: 0 additions & 183 deletions core/debugger/debugger_marshalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,159 +35,6 @@
#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()))
#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()))

Array DebuggerMarshalls::ResourceUsage::serialize() {
infos.sort();

Array arr;
arr.push_back(infos.size() * 4);
for (const ResourceInfo &E : infos) {
arr.push_back(E.path);
arr.push_back(E.format);
arr.push_back(E.type);
arr.push_back(E.vram);
}
return arr;
}

bool DebuggerMarshalls::ResourceUsage::deserialize(const Array &p_arr) {
CHECK_SIZE(p_arr, 1, "ResourceUsage");
uint32_t size = p_arr[0];
CHECK_SIZE(p_arr, size, "ResourceUsage");
int idx = 1;
for (uint32_t i = 0; i < size / 4; i++) {
ResourceInfo info;
info.path = p_arr[idx];
info.format = p_arr[idx + 1];
info.type = p_arr[idx + 2];
info.vram = p_arr[idx + 3];
infos.push_back(info);
}
CHECK_END(p_arr, idx, "ResourceUsage");
return true;
}

Array DebuggerMarshalls::ScriptFunctionSignature::serialize() {
Array arr;
arr.push_back(name);
arr.push_back(id);
return arr;
}

bool DebuggerMarshalls::ScriptFunctionSignature::deserialize(const Array &p_arr) {
CHECK_SIZE(p_arr, 2, "ScriptFunctionSignature");
name = p_arr[0];
id = p_arr[1];
CHECK_END(p_arr, 2, "ScriptFunctionSignature");
return true;
}

Array DebuggerMarshalls::NetworkProfilerFrame::serialize() {
Array arr;
arr.push_back(infos.size() * 6);
for (int i = 0; i < infos.size(); ++i) {
arr.push_back(uint64_t(infos[i].node));
arr.push_back(infos[i].node_path);
arr.push_back(infos[i].incoming_rpc);
arr.push_back(infos[i].incoming_rset);
arr.push_back(infos[i].outgoing_rpc);
arr.push_back(infos[i].outgoing_rset);
}
return arr;
}

bool DebuggerMarshalls::NetworkProfilerFrame::deserialize(const Array &p_arr) {
CHECK_SIZE(p_arr, 1, "NetworkProfilerFrame");
uint32_t size = p_arr[0];
CHECK_SIZE(p_arr, size, "NetworkProfilerFrame");
infos.resize(size);
int idx = 1;
for (uint32_t i = 0; i < size / 6; ++i) {
infos.write[i].node = uint64_t(p_arr[idx]);
infos.write[i].node_path = p_arr[idx + 1];
infos.write[i].incoming_rpc = p_arr[idx + 2];
infos.write[i].incoming_rset = p_arr[idx + 3];
infos.write[i].outgoing_rpc = p_arr[idx + 4];
infos.write[i].outgoing_rset = p_arr[idx + 5];
}
CHECK_END(p_arr, idx, "NetworkProfilerFrame");
return true;
}

Array DebuggerMarshalls::ServersProfilerFrame::serialize() {
Array arr;
arr.push_back(frame_number);
arr.push_back(frame_time);
arr.push_back(idle_time);
arr.push_back(physics_time);
arr.push_back(physics_frame_time);
arr.push_back(script_time);

arr.push_back(servers.size());
for (int i = 0; i < servers.size(); i++) {
ServerInfo &s = servers[i];
arr.push_back(s.name);
arr.push_back(s.functions.size() * 2);
for (int j = 0; j < s.functions.size(); j++) {
ServerFunctionInfo &f = s.functions[j];
arr.push_back(f.name);
arr.push_back(f.time);
}
}

arr.push_back(script_functions.size() * 4);
for (int i = 0; i < script_functions.size(); i++) {
arr.push_back(script_functions[i].sig_id);
arr.push_back(script_functions[i].call_count);
arr.push_back(script_functions[i].self_time);
arr.push_back(script_functions[i].total_time);
}
return arr;
}

bool DebuggerMarshalls::ServersProfilerFrame::deserialize(const Array &p_arr) {
CHECK_SIZE(p_arr, 7, "ServersProfilerFrame");
frame_number = p_arr[0];
frame_time = p_arr[1];
idle_time = p_arr[2];
physics_time = p_arr[3];
physics_frame_time = p_arr[4];
script_time = p_arr[5];
int servers_size = p_arr[6];
int idx = 7;
while (servers_size) {
CHECK_SIZE(p_arr, idx + 2, "ServersProfilerFrame");
servers_size--;
ServerInfo si;
si.name = p_arr[idx];
int sub_data_size = p_arr[idx + 1];
idx += 2;
CHECK_SIZE(p_arr, idx + sub_data_size, "ServersProfilerFrame");
for (int j = 0; j < sub_data_size / 2; j++) {
ServerFunctionInfo sf;
sf.name = p_arr[idx];
sf.time = p_arr[idx + 1];
idx += 2;
si.functions.push_back(sf);
}
servers.push_back(si);
}
CHECK_SIZE(p_arr, idx + 1, "ServersProfilerFrame");
int func_size = p_arr[idx];
idx += 1;
CHECK_SIZE(p_arr, idx + func_size, "ServersProfilerFrame");
for (int i = 0; i < func_size / 4; i++) {
ScriptFunctionInfo fi;
fi.sig_id = p_arr[idx];
fi.call_count = p_arr[idx + 1];
fi.self_time = p_arr[idx + 2];
fi.total_time = p_arr[idx + 3];
script_functions.push_back(fi);
idx += 4;
}
CHECK_END(p_arr, idx, "ServersProfilerFrame");
return true;
}

Array DebuggerMarshalls::ScriptStackDump::serialize() {
Array arr;
arr.push_back(frames.size() * 3);
Expand Down Expand Up @@ -298,33 +145,3 @@ bool DebuggerMarshalls::OutputError::deserialize(const Array &p_arr) {
CHECK_END(p_arr, idx, "OutputError");
return true;
}

Array DebuggerMarshalls::VisualProfilerFrame::serialize() {
Array arr;
arr.push_back(frame_number);
arr.push_back(areas.size() * 3);
for (int i = 0; i < areas.size(); i++) {
arr.push_back(areas[i].name);
arr.push_back(areas[i].cpu_msec);
arr.push_back(areas[i].gpu_msec);
}
return arr;
}

bool DebuggerMarshalls::VisualProfilerFrame::deserialize(const Array &p_arr) {
CHECK_SIZE(p_arr, 2, "VisualProfilerFrame");
frame_number = p_arr[0];
int size = p_arr[1];
CHECK_SIZE(p_arr, size, "VisualProfilerFrame");
int idx = 2;
areas.resize(size / 3);
RS::FrameProfileArea *w = areas.ptrw();
for (int i = 0; i < size / 3; i++) {
w[i].name = p_arr[idx];
w[i].cpu_msec = p_arr[idx + 1];
w[i].gpu_msec = p_arr[idx + 2];
idx += 3;
}
CHECK_END(p_arr, idx, "VisualProfilerFrame");
return true;
}
Loading

0 comments on commit d22ac13

Please sign in to comment.