Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebSocket profiler for HTML5 platform [4.0] #37099

Merged
merged 4 commits into from
May 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 29 additions & 15 deletions core/debugger/engine_debugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

#include "core/debugger/local_debugger.h"
#include "core/debugger/remote_debugger.h"
#include "core/debugger/remote_debugger_peer.h"
#include "core/debugger/script_debugger.h"
#include "core/os/os.h"

Expand All @@ -40,6 +41,7 @@ ScriptDebugger *EngineDebugger::script_debugger = nullptr;

Map<StringName, EngineDebugger::Profiler> EngineDebugger::profilers;
Map<StringName, EngineDebugger::Capture> EngineDebugger::captures;
Map<String, EngineDebugger::CreatePeerFunc> EngineDebugger::protocols;

void EngineDebugger::register_profiler(const StringName &p_name, const Profiler &p_func) {
ERR_FAIL_COND_MSG(profilers.has(p_name), "Profiler already registered: " + p_name);
Expand All @@ -66,6 +68,11 @@ void EngineDebugger::unregister_message_capture(const StringName &p_name) {
captures.erase(p_name);
}

void EngineDebugger::register_uri_handler(const String &p_protocol, CreatePeerFunc p_func) {
ERR_FAIL_COND_MSG(protocols.has(p_protocol), "Protocol handler already registered: " + p_protocol);
protocols.insert(p_protocol, p_func);
}

void EngineDebugger::profiler_enable(const StringName &p_name, bool p_enabled, const Array &p_opts) {
ERR_FAIL_COND_MSG(!profilers.has(p_name), "Can't change profiler state, no profiler: " + p_name);
Profiler &p = profilers[p_name];
Expand Down Expand Up @@ -125,17 +132,22 @@ void EngineDebugger::iteration(uint64_t p_frame_ticks, uint64_t p_idle_ticks, ui
}

void EngineDebugger::initialize(const String &p_uri, bool p_skip_breakpoints, Vector<String> p_breakpoints) {
register_uri_handler("tcp://", RemoteDebuggerPeerTCP::create); // TCP is the default protocol. Platforms/modules can add more.
if (p_uri.empty())
return;
if (p_uri == "local://") {
singleton = memnew(LocalDebugger);
script_debugger = memnew(ScriptDebugger);
// Tell the OS that we want to handle termination signals.
OS::get_singleton()->initialize_debugging();
} else {
singleton = RemoteDebugger::create_for_uri(p_uri);
if (!singleton)
} else if (p_uri.find("://") >= 0) {
const String proto = p_uri.substr(0, p_uri.find("://") + 3);
if (!protocols.has(proto))
return;
RemoteDebuggerPeer *peer = protocols[proto](p_uri);
if (!peer)
return;
singleton = memnew(RemoteDebugger(Ref<RemoteDebuggerPeer>(peer)));
script_debugger = memnew(ScriptDebugger);
// Notify editor of our pid (to allow focus stealing).
Array msg;
Expand All @@ -160,22 +172,24 @@ void EngineDebugger::initialize(const String &p_uri, bool p_skip_breakpoints, Ve
}

void EngineDebugger::deinitialize() {
if (!singleton)
return;

// Stop all profilers
for (Map<StringName, Profiler>::Element *E = profilers.front(); E; E = E->next()) {
if (E->get().active)
singleton->profiler_enable(E->key(), false);
if (singleton) {
// Stop all profilers
for (Map<StringName, Profiler>::Element *E = profilers.front(); E; E = E->next()) {
if (E->get().active)
singleton->profiler_enable(E->key(), false);
}

// Flush any remaining message
singleton->poll_events(false);

memdelete(singleton);
singleton = nullptr;
}

// Flush any remaining message
singleton->poll_events(false);

memdelete(singleton);
singleton = nullptr;
// Clear profilers/captuers/protocol handlers.
profilers.clear();
captures.clear();
protocols.clear();
}

EngineDebugger::~EngineDebugger() {
Expand Down
7 changes: 7 additions & 0 deletions core/debugger/engine_debugger.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,19 @@
#include "core/variant.h"
#include "core/vector.h"

class RemoteDebuggerPeer;
class ScriptDebugger;

class EngineDebugger {
public:
typedef void (*ProfilingToggle)(void *p_user, bool p_enable, const Array &p_opts);
typedef void (*ProfilingTick)(void *p_user, float p_frame_time, float p_idle_time, float p_physics_time, float p_physics_frame_time);
typedef void (*ProfilingAdd)(void *p_user, const Array &p_arr);

typedef Error (*CaptureFunc)(void *p_user, const String &p_msg, const Array &p_args, bool &r_captured);

typedef RemoteDebuggerPeer *(*CreatePeerFunc)(const String &p_uri);

class Profiler {
friend class EngineDebugger;

Expand Down Expand Up @@ -94,6 +98,7 @@ class EngineDebugger {

static Map<StringName, Profiler> profilers;
static Map<StringName, Capture> captures;
static Map<String, CreatePeerFunc> protocols;

public:
_FORCE_INLINE_ static EngineDebugger *get_singleton() { return singleton; }
Expand All @@ -113,6 +118,8 @@ class EngineDebugger {
static void unregister_message_capture(const StringName &p_name);
static bool has_capture(const StringName &p_name);

static void register_uri_handler(const String &p_protocol, CreatePeerFunc p_func);

void iteration(uint64_t p_frame_ticks, uint64_t p_idle_ticks, uint64_t p_physics_ticks, float p_physics_frame_time);
void profiler_enable(const StringName &p_name, bool p_enabled, const Array &p_opts = Array());
Error capture_parse(const StringName &p_name, const String &p_msg, const Array &p_args, bool &r_captured);
Expand Down
10 changes: 3 additions & 7 deletions core/debugger/remote_debugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,9 @@ void RemoteDebugger::debug(bool p_can_continue, bool p_is_error_breakpoint) {

ERR_FAIL_COND_MSG(!is_peer_connected(), "Script Debugger failed to connect, but being used anyway.");

if (!peer->can_block())
return; // Peer does not support blocking IO. We could at least send the error though.

ScriptLanguage *script_lang = script_debugger->get_break_language();
const String error_str = script_lang ? script_lang->debug_get_error() : "";
Array msg;
Expand Down Expand Up @@ -886,13 +889,6 @@ Error RemoteDebugger::_profiler_capture(const String &p_cmd, const Array &p_data
return OK;
}

RemoteDebugger *RemoteDebugger::create_for_uri(const String &p_uri) {
Ref<RemoteDebuggerPeer> peer = RemoteDebuggerPeer::create_from_uri(p_uri);
if (peer.is_valid())
return memnew(RemoteDebugger(peer));
return nullptr;
}

RemoteDebugger::RemoteDebugger(Ref<RemoteDebuggerPeer> p_peer) {
peer = p_peer;
max_chars_per_second = GLOBAL_GET("network/limits/debugger/max_chars_per_second");
Expand Down
2 changes: 0 additions & 2 deletions core/debugger/remote_debugger.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,6 @@ class RemoteDebugger : public EngineDebugger {
Error _try_capture(const String &p_name, const Array &p_data, bool &r_captured);

public:
static RemoteDebugger *create_for_uri(const String &p_uri);

// Overrides
void poll_events(bool p_is_idle);
void send_message(const String &p_message, const Array &p_args);
Expand Down
14 changes: 8 additions & 6 deletions core/debugger/remote_debugger_peer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,8 @@ void RemoteDebuggerPeerTCP::_poll() {
}
}

Ref<RemoteDebuggerPeer> RemoteDebuggerPeer::create_from_uri(const String p_uri) {
if (!p_uri.begins_with("tcp://"))
return Ref<RemoteDebuggerPeer>(); // Only TCP supported for now, more to come.
RemoteDebuggerPeer *RemoteDebuggerPeerTCP::create(const String &p_uri) {
ERR_FAIL_COND_V(!p_uri.begins_with("tcp://"), nullptr);

String debug_host = p_uri.replace("tcp://", "");
uint16_t debug_port = 6007;
Expand All @@ -230,10 +229,13 @@ Ref<RemoteDebuggerPeer> RemoteDebuggerPeer::create_from_uri(const String p_uri)
debug_port = debug_host.substr(sep_pos + 1).to_int();
debug_host = debug_host.substr(0, sep_pos);
}
Ref<RemoteDebuggerPeerTCP> peer = Ref<RemoteDebuggerPeer>(memnew(RemoteDebuggerPeerTCP));

RemoteDebuggerPeerTCP *peer = memnew(RemoteDebuggerPeerTCP);
Error err = peer->connect_to_host(debug_host, debug_port);
if (err != OK)
return Ref<RemoteDebuggerPeer>();
if (err != OK) {
memdelete(peer);
return nullptr;
}
return peer;
}

Expand Down
4 changes: 3 additions & 1 deletion core/debugger/remote_debugger_peer.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ class RemoteDebuggerPeer : public Reference {
int max_queued_messages = 4096;

public:
static Ref<RemoteDebuggerPeer> create_from_uri(const String p_uri);
virtual bool is_peer_connected() = 0;
virtual bool has_message() = 0;
virtual Error put_message(const Array &p_arr) = 0;
virtual Array get_message() = 0;
virtual void close() = 0;
virtual void poll() = 0;
virtual int get_max_message_size() const = 0;
virtual bool can_block() const { return true; } // If blocking io is allowed on main thread (debug).

RemoteDebuggerPeer();
};
Expand Down Expand Up @@ -77,6 +77,8 @@ class RemoteDebuggerPeerTCP : public RemoteDebuggerPeer {
void _read_in();

public:
static RemoteDebuggerPeer *create(const String &p_uri);

Error connect_to_host(const String &p_host, uint16_t p_port);

void poll();
Expand Down
12 changes: 2 additions & 10 deletions editor/debugger/editor_debugger_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,15 @@ ScriptEditorDebugger *EditorDebuggerNode::get_default_debugger() const {
return Object::cast_to<ScriptEditorDebugger>(tabs->get_tab_control(0));
}

Error EditorDebuggerNode::start() {
Error EditorDebuggerNode::start(const String &p_protocol) {
stop();
if (EDITOR_GET("run/output/always_open_output_on_play")) {
EditorNode::get_singleton()->make_bottom_panel_item_visible(EditorNode::get_log());
} else {
EditorNode::get_singleton()->make_bottom_panel_item_visible(this);
}

server = Ref<EditorDebuggerServer>(EditorDebuggerServer::create_default());
server = Ref<EditorDebuggerServer>(EditorDebuggerServer::create(p_protocol));
const Error err = server->start();
if (err != OK) {
return err;
Expand Down Expand Up @@ -213,14 +213,6 @@ void EditorDebuggerNode::stop() {

void EditorDebuggerNode::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_ENTER_TREE: {
EditorNode::get_singleton()->connect("play_pressed", callable_mp(this, &EditorDebuggerNode::start));
EditorNode::get_singleton()->connect("stop_pressed", callable_mp(this, &EditorDebuggerNode::stop));
} break;
case NOTIFICATION_EXIT_TREE: {
EditorNode::get_singleton()->disconnect("play_pressed", callable_mp(this, &EditorDebuggerNode::start));
EditorNode::get_singleton()->disconnect("stop_pressed", callable_mp(this, &EditorDebuggerNode::stop));
} break;
case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
if (tabs->get_tab_count() > 1) {
add_theme_constant_override("margin_left", -EditorNode::get_singleton()->get_gui_base()->get_theme_stylebox("BottomPanelDebuggerOverride", "EditorStyles")->get_margin(MARGIN_LEFT));
Expand Down
2 changes: 1 addition & 1 deletion editor/debugger/editor_debugger_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ class EditorDebuggerNode : public MarginContainer {
void set_camera_override(CameraOverride p_override) { camera_override = p_override; }
CameraOverride get_camera_override() { return camera_override; }

Error start();
Error start(const String &p_protocol = "tcp://");

void stop();
};
Expand Down
27 changes: 25 additions & 2 deletions editor/debugger/editor_debugger_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class EditorDebuggerServerTCP : public EditorDebuggerServer {
Ref<TCP_Server> server;

public:
static EditorDebuggerServer *create(const String &p_protocol);
virtual void poll() {}
virtual Error start();
virtual void stop();
Expand All @@ -54,6 +55,11 @@ class EditorDebuggerServerTCP : public EditorDebuggerServer {
EditorDebuggerServerTCP();
};

EditorDebuggerServer *EditorDebuggerServerTCP::create(const String &p_protocol) {
ERR_FAIL_COND_V(p_protocol != "tcp://", nullptr);
return memnew(EditorDebuggerServerTCP);
}

EditorDebuggerServerTCP::EditorDebuggerServerTCP() {
server.instance();
}
Expand Down Expand Up @@ -85,6 +91,23 @@ Ref<RemoteDebuggerPeer> EditorDebuggerServerTCP::take_connection() {
return memnew(RemoteDebuggerPeerTCP(server->take_connection()));
}

EditorDebuggerServer *EditorDebuggerServer::create_default() {
return memnew(EditorDebuggerServerTCP);
/// EditorDebuggerServer
Map<StringName, EditorDebuggerServer::CreateServerFunc> EditorDebuggerServer::protocols;

EditorDebuggerServer *EditorDebuggerServer::create(const String &p_protocol) {
ERR_FAIL_COND_V(!protocols.has(p_protocol), nullptr);
return protocols[p_protocol](p_protocol);
}

void EditorDebuggerServer::register_protocol_handler(const String &p_protocol, CreateServerFunc p_func) {
ERR_FAIL_COND(protocols.has(p_protocol));
protocols[p_protocol] = p_func;
}

void EditorDebuggerServer::initialize() {
register_protocol_handler("tcp://", EditorDebuggerServerTCP::create);
}

void EditorDebuggerServer::deinitialize() {
protocols.clear();
}
12 changes: 11 additions & 1 deletion editor/debugger/editor_debugger_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,17 @@
class EditorDebuggerServer : public Reference {

public:
static EditorDebuggerServer *create_default();
typedef EditorDebuggerServer *(*CreateServerFunc)(const String &p_uri);

private:
static Map<StringName, CreateServerFunc> protocols;

public:
static void initialize();
static void deinitialize();

static void register_protocol_handler(const String &p_protocol, CreateServerFunc p_func);
static EditorDebuggerServer *create(const String &p_protocol);
virtual void poll() = 0;
virtual Error start() = 0;
virtual void stop() = 0;
Expand Down
2 changes: 2 additions & 0 deletions editor/debugger/script_editor_debugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,8 @@ void ScriptEditorDebugger::_notification(int p_what) {

if (is_session_active()) {

peer->poll();

if (camera_override == CameraOverride::OVERRIDE_2D) {
CanvasItemEditor *editor = CanvasItemEditor::get_singleton();

Expand Down
4 changes: 2 additions & 2 deletions editor/editor_export.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ void EditorExportPlatform::gen_debug_flags(Vector<String> &r_flags, int p_flags)

r_flags.push_back("--remote-debug");

r_flags.push_back(host + ":" + String::num(remote_port));
r_flags.push_back(get_debug_protocol() + host + ":" + String::num(remote_port));

List<String> breakpoints;
ScriptEditor::get_singleton()->get_breakpoints(&breakpoints);
Expand Down Expand Up @@ -1127,7 +1127,7 @@ void EditorExportPlatform::gen_export_flags(Vector<String> &r_flags, int p_flags

r_flags.push_back("--remote-debug");

r_flags.push_back(host + ":" + String::num(remote_port));
r_flags.push_back(get_debug_protocol() + host + ":" + String::num(remote_port));

List<String> breakpoints;
ScriptEditor::get_singleton()->get_breakpoints(&breakpoints);
Expand Down
1 change: 1 addition & 0 deletions editor/editor_export.h
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ class EditorExportPlatform : public Reference {
virtual Error export_zip(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags = 0);
virtual void get_platform_features(List<String> *r_features) = 0;
virtual void resolve_platform_feature_priorities(const Ref<EditorExportPreset> &p_preset, Set<String> &p_features) = 0;
virtual String get_debug_protocol() const { return "tcp://"; }

EditorExportPlatform();
};
Expand Down
Loading