Path: blob/master/modules/multiplayer/editor/editor_network_profiler.cpp
10278 views
/**************************************************************************/1/* editor_network_profiler.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "editor_network_profiler.h"3132#include "editor/editor_string_names.h"33#include "editor/run/editor_run_bar.h"34#include "editor/settings/editor_settings.h"35#include "editor/themes/editor_scale.h"36#include "scene/gui/check_box.h"37#include "scene/gui/flow_container.h"38#include "scene/gui/line_edit.h"3940void EditorNetworkProfiler::_bind_methods() {41ADD_SIGNAL(MethodInfo("enable_profiling", PropertyInfo(Variant::BOOL, "enable")));42ADD_SIGNAL(MethodInfo("open_request", PropertyInfo(Variant::STRING, "path")));43}4445void EditorNetworkProfiler::_notification(int p_what) {46switch (p_what) {47case NOTIFICATION_THEME_CHANGED: {48if (activate->is_pressed()) {49activate->set_button_icon(theme_cache.stop_icon);50} else {51activate->set_button_icon(theme_cache.play_icon);52}53clear_button->set_button_icon(theme_cache.clear_icon);5455incoming_bandwidth_text->set_right_icon(theme_cache.incoming_bandwidth_icon);56outgoing_bandwidth_text->set_right_icon(theme_cache.outgoing_bandwidth_icon);5758// This needs to be done here to set the faded color when the profiler is first opened59incoming_bandwidth_text->add_theme_color_override("font_uneditable_color", theme_cache.incoming_bandwidth_color * Color(1, 1, 1, 0.5));60outgoing_bandwidth_text->add_theme_color_override("font_uneditable_color", theme_cache.outgoing_bandwidth_color * Color(1, 1, 1, 0.5));61} break;62}63}6465void EditorNetworkProfiler::_update_theme_item_cache() {66VBoxContainer::_update_theme_item_cache();6768theme_cache.node_icon = get_theme_icon(SNAME("Node"), EditorStringName(EditorIcons));69theme_cache.stop_icon = get_theme_icon(SNAME("Stop"), EditorStringName(EditorIcons));70theme_cache.play_icon = get_theme_icon(SNAME("Play"), EditorStringName(EditorIcons));71theme_cache.clear_icon = get_theme_icon(SNAME("Clear"), EditorStringName(EditorIcons));7273theme_cache.multiplayer_synchronizer_icon = get_theme_icon("MultiplayerSynchronizer", EditorStringName(EditorIcons));74theme_cache.instance_options_icon = get_theme_icon(SNAME("InstanceOptions"), EditorStringName(EditorIcons));7576theme_cache.incoming_bandwidth_icon = get_theme_icon(SNAME("ArrowDown"), EditorStringName(EditorIcons));77theme_cache.outgoing_bandwidth_icon = get_theme_icon(SNAME("ArrowUp"), EditorStringName(EditorIcons));7879theme_cache.incoming_bandwidth_color = get_theme_color(SceneStringName(font_color), EditorStringName(Editor));80theme_cache.outgoing_bandwidth_color = get_theme_color(SceneStringName(font_color), EditorStringName(Editor));81}8283void EditorNetworkProfiler::_refresh() {84if (!dirty) {85return;86}87dirty = false;88refresh_rpc_data();89refresh_replication_data();90}9192void EditorNetworkProfiler::refresh_rpc_data() {93counters_display->clear();9495TreeItem *root = counters_display->create_item();96int cols = counters_display->get_columns();9798for (const KeyValue<ObjectID, RPCNodeInfo> &E : rpc_data) {99TreeItem *node = counters_display->create_item(root);100101for (int j = 0; j < cols; ++j) {102node->set_text_alignment(j, j > 0 ? HORIZONTAL_ALIGNMENT_RIGHT : HORIZONTAL_ALIGNMENT_LEFT);103}104105node->set_text(0, E.value.node_path);106node->set_text(1, E.value.incoming_rpc == 0 ? "-" : vformat(TTR("%d (%s)"), E.value.incoming_rpc, String::humanize_size(E.value.incoming_size)));107node->set_text(2, E.value.outgoing_rpc == 0 ? "-" : vformat(TTR("%d (%s)"), E.value.outgoing_rpc, String::humanize_size(E.value.outgoing_size)));108}109}110111void EditorNetworkProfiler::refresh_replication_data() {112replication_display->clear();113114TreeItem *root = replication_display->create_item();115116for (const KeyValue<ObjectID, SyncInfo> &E : sync_data) {117// Ensure the nodes have at least a temporary cache.118ObjectID ids[3] = { E.value.synchronizer, E.value.config, E.value.root_node };119for (uint32_t i = 0; i < 3; i++) {120const ObjectID &id = ids[i];121if (!node_data.has(id)) {122missing_node_data.insert(id);123node_data[id] = NodeInfo(id);124}125}126127TreeItem *node = replication_display->create_item(root);128129const NodeInfo &root_info = node_data[E.value.root_node];130const NodeInfo &sync_info = node_data[E.value.synchronizer];131const NodeInfo &cfg_info = node_data[E.value.config];132133node->set_text(0, root_info.path.get_file());134node->set_icon(0, has_theme_icon(root_info.type, EditorStringName(EditorIcons)) ? get_theme_icon(root_info.type, EditorStringName(EditorIcons)) : theme_cache.node_icon);135node->set_tooltip_text(0, root_info.path);136137node->set_text(1, sync_info.path.get_file());138node->set_icon(1, theme_cache.multiplayer_synchronizer_icon);139node->set_tooltip_text(1, sync_info.path);140141int cfg_idx = cfg_info.path.find("::");142if (cfg_info.path.begins_with("res://") && ResourceLoader::exists(cfg_info.path) && cfg_idx > 0) {143String res_idstr = cfg_info.path.substr(cfg_idx + 2).replace("SceneReplicationConfig_", "");144String scene_path = cfg_info.path.substr(0, cfg_idx);145node->set_text(2, vformat("%s (%s)", res_idstr, scene_path.get_file()));146node->add_button(2, theme_cache.instance_options_icon);147node->set_tooltip_text(2, cfg_info.path);148node->set_metadata(2, scene_path);149} else {150node->set_text(2, cfg_info.path);151node->set_metadata(2, "");152}153154node->set_text(3, vformat("%d - %d", E.value.incoming_syncs, E.value.outgoing_syncs));155node->set_text(4, vformat("%d - %d", E.value.incoming_size, E.value.outgoing_size));156}157}158159Array EditorNetworkProfiler::pop_missing_node_data() {160Array out;161for (const ObjectID &id : missing_node_data) {162out.push_back(id);163}164missing_node_data.clear();165return out;166}167168void EditorNetworkProfiler::add_node_data(const NodeInfo &p_info) {169ERR_FAIL_COND(!node_data.has(p_info.id));170node_data[p_info.id] = p_info;171dirty = true;172}173174void EditorNetworkProfiler::_activate_pressed() {175_update_button_text();176177if (activate->is_pressed()) {178refresh_timer->start();179} else {180refresh_timer->stop();181}182183emit_signal(SNAME("enable_profiling"), activate->is_pressed());184}185186void EditorNetworkProfiler::_update_button_text() {187if (activate->is_pressed()) {188activate->set_button_icon(theme_cache.stop_icon);189activate->set_text(TTR("Stop"));190} else {191activate->set_button_icon(theme_cache.play_icon);192activate->set_text(TTR("Start"));193}194}195196void EditorNetworkProfiler::started() {197_clear_pressed();198activate->set_disabled(false);199200if (EditorSettings::get_singleton()->get_project_metadata("debug_options", "autostart_network_profiler", false)) {201set_profiling(true);202refresh_timer->start();203}204}205206void EditorNetworkProfiler::stopped() {207activate->set_disabled(true);208set_profiling(false);209refresh_timer->stop();210}211212void EditorNetworkProfiler::set_profiling(bool p_pressed) {213activate->set_pressed(p_pressed);214_update_button_text();215emit_signal(SNAME("enable_profiling"), activate->is_pressed());216}217218void EditorNetworkProfiler::_clear_pressed() {219rpc_data.clear();220sync_data.clear();221node_data.clear();222missing_node_data.clear();223set_bandwidth(0, 0);224refresh_rpc_data();225refresh_replication_data();226clear_button->set_disabled(true);227}228229void EditorNetworkProfiler::_autostart_toggled(bool p_toggled_on) {230EditorSettings::get_singleton()->set_project_metadata("debug_options", "autostart_network_profiler", p_toggled_on);231EditorRunBar::get_singleton()->update_profiler_autostart_indicator();232}233234void EditorNetworkProfiler::_replication_button_clicked(TreeItem *p_item, int p_column, int p_idx, MouseButton p_button) {235if (!p_item) {236return;237}238String meta = p_item->get_metadata(p_column);239if (meta.size() && ResourceLoader::exists(meta)) {240emit_signal("open_request", meta);241}242}243244void EditorNetworkProfiler::add_rpc_frame_data(const RPCNodeInfo &p_frame) {245if (clear_button->is_disabled()) {246clear_button->set_disabled(false);247}248dirty = true;249if (!rpc_data.has(p_frame.node)) {250rpc_data.insert(p_frame.node, p_frame);251} else {252rpc_data[p_frame.node].incoming_rpc += p_frame.incoming_rpc;253rpc_data[p_frame.node].outgoing_rpc += p_frame.outgoing_rpc;254}255if (p_frame.incoming_rpc) {256rpc_data[p_frame.node].incoming_size = p_frame.incoming_size / p_frame.incoming_rpc;257}258if (p_frame.outgoing_rpc) {259rpc_data[p_frame.node].outgoing_size = p_frame.outgoing_size / p_frame.outgoing_rpc;260}261}262263void EditorNetworkProfiler::add_sync_frame_data(const SyncInfo &p_frame) {264if (clear_button->is_disabled()) {265clear_button->set_disabled(false);266}267dirty = true;268if (!sync_data.has(p_frame.synchronizer)) {269sync_data[p_frame.synchronizer] = p_frame;270} else {271sync_data[p_frame.synchronizer].incoming_syncs += p_frame.incoming_syncs;272sync_data[p_frame.synchronizer].outgoing_syncs += p_frame.outgoing_syncs;273}274SyncInfo &info = sync_data[p_frame.synchronizer];275if (p_frame.incoming_syncs) {276info.incoming_size = p_frame.incoming_size / p_frame.incoming_syncs;277}278if (p_frame.outgoing_syncs) {279info.outgoing_size = p_frame.outgoing_size / p_frame.outgoing_syncs;280}281}282283void EditorNetworkProfiler::set_bandwidth(int p_incoming, int p_outgoing) {284incoming_bandwidth_text->set_text(vformat(TTR("%s/s"), String::humanize_size(p_incoming)));285outgoing_bandwidth_text->set_text(vformat(TTR("%s/s"), String::humanize_size(p_outgoing)));286287// Make labels more prominent when the bandwidth is greater than 0 to attract user attention288incoming_bandwidth_text->add_theme_color_override(289"font_uneditable_color",290theme_cache.incoming_bandwidth_color * Color(1, 1, 1, p_incoming > 0 ? 1 : 0.5));291outgoing_bandwidth_text->add_theme_color_override(292"font_uneditable_color",293theme_cache.outgoing_bandwidth_color * Color(1, 1, 1, p_outgoing > 0 ? 1 : 0.5));294}295296bool EditorNetworkProfiler::is_profiling() {297return activate->is_pressed();298}299300EditorNetworkProfiler::EditorNetworkProfiler() {301FlowContainer *container = memnew(FlowContainer);302container->add_theme_constant_override(SNAME("h_separation"), 8 * EDSCALE);303container->add_theme_constant_override(SNAME("v_separation"), 2 * EDSCALE);304add_child(container);305306activate = memnew(Button);307activate->set_toggle_mode(true);308activate->set_text(TTR("Start"));309activate->set_disabled(true);310activate->connect(SceneStringName(pressed), callable_mp(this, &EditorNetworkProfiler::_activate_pressed));311container->add_child(activate);312313clear_button = memnew(Button);314clear_button->set_text(TTR("Clear"));315clear_button->set_disabled(true);316clear_button->connect(SceneStringName(pressed), callable_mp(this, &EditorNetworkProfiler::_clear_pressed));317container->add_child(clear_button);318319CheckBox *autostart_checkbox = memnew(CheckBox);320autostart_checkbox->set_text(TTR("Autostart"));321autostart_checkbox->set_pressed(EditorSettings::get_singleton()->get_project_metadata("debug_options", "autostart_network_profiler", false));322autostart_checkbox->connect(SceneStringName(toggled), callable_mp(this, &EditorNetworkProfiler::_autostart_toggled));323container->add_child(autostart_checkbox);324325Control *c = memnew(Control);326c->set_h_size_flags(SIZE_EXPAND_FILL);327container->add_child(c);328329HBoxContainer *hb = memnew(HBoxContainer);330hb->add_theme_constant_override(SNAME("separation"), 8 * EDSCALE);331container->add_child(hb);332333Label *lb = memnew(Label);334// TRANSLATORS: This is the label for the network profiler's incoming bandwidth.335lb->set_focus_mode(FOCUS_ACCESSIBILITY);336lb->set_text(TTR("Down", "Network"));337hb->add_child(lb);338339incoming_bandwidth_text = memnew(LineEdit);340incoming_bandwidth_text->set_editable(false);341incoming_bandwidth_text->set_custom_minimum_size(Size2(120, 0) * EDSCALE);342incoming_bandwidth_text->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);343incoming_bandwidth_text->set_accessibility_name(TTRC("Incoming Bandwidth"));344hb->add_child(incoming_bandwidth_text);345346Control *down_up_spacer = memnew(Control);347down_up_spacer->set_custom_minimum_size(Size2(30, 0) * EDSCALE);348hb->add_child(down_up_spacer);349350lb = memnew(Label);351// TRANSLATORS: This is the label for the network profiler's outgoing bandwidth.352lb->set_focus_mode(FOCUS_ACCESSIBILITY);353lb->set_text(TTR("Up", "Network"));354hb->add_child(lb);355356outgoing_bandwidth_text = memnew(LineEdit);357outgoing_bandwidth_text->set_editable(false);358outgoing_bandwidth_text->set_custom_minimum_size(Size2(120, 0) * EDSCALE);359outgoing_bandwidth_text->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);360outgoing_bandwidth_text->set_accessibility_name(TTRC("Outgoing Bandwidth"));361hb->add_child(outgoing_bandwidth_text);362363// Set initial texts in the incoming/outgoing bandwidth labels364set_bandwidth(0, 0);365366HSplitContainer *sc = memnew(HSplitContainer);367add_child(sc);368sc->set_v_size_flags(SIZE_EXPAND_FILL);369sc->set_h_size_flags(SIZE_EXPAND_FILL);370sc->set_split_offset(100 * EDSCALE);371372// RPC373counters_display = memnew(Tree);374counters_display->set_custom_minimum_size(Size2(280, 0) * EDSCALE);375counters_display->set_v_size_flags(SIZE_EXPAND_FILL);376counters_display->set_h_size_flags(SIZE_EXPAND_FILL);377counters_display->set_hide_folding(true);378counters_display->set_hide_root(true);379counters_display->set_columns(3);380counters_display->set_column_titles_visible(true);381counters_display->set_column_title(0, TTR("Node"));382counters_display->set_column_expand(0, true);383counters_display->set_column_clip_content(0, true);384counters_display->set_column_custom_minimum_width(0, 60 * EDSCALE);385counters_display->set_column_title(1, TTR("Incoming RPC"));386counters_display->set_column_expand(1, false);387counters_display->set_column_clip_content(1, true);388counters_display->set_column_custom_minimum_width(1, 120 * EDSCALE);389counters_display->set_column_title(2, TTR("Outgoing RPC"));390counters_display->set_column_expand(2, false);391counters_display->set_column_clip_content(2, true);392counters_display->set_column_custom_minimum_width(2, 120 * EDSCALE);393sc->add_child(counters_display);394395// Replication396replication_display = memnew(Tree);397replication_display->set_custom_minimum_size(Size2(280, 0) * EDSCALE);398replication_display->set_v_size_flags(SIZE_EXPAND_FILL);399replication_display->set_h_size_flags(SIZE_EXPAND_FILL);400replication_display->set_hide_folding(true);401replication_display->set_hide_root(true);402replication_display->set_columns(5);403replication_display->set_column_titles_visible(true);404replication_display->set_column_title(0, TTR("Root"));405replication_display->set_column_expand(0, true);406replication_display->set_column_clip_content(0, true);407replication_display->set_column_custom_minimum_width(0, 80 * EDSCALE);408replication_display->set_column_title(1, TTR("Synchronizer"));409replication_display->set_column_expand(1, true);410replication_display->set_column_clip_content(1, true);411replication_display->set_column_custom_minimum_width(1, 80 * EDSCALE);412replication_display->set_column_title(2, TTR("Config"));413replication_display->set_column_expand(2, true);414replication_display->set_column_clip_content(2, true);415replication_display->set_column_custom_minimum_width(2, 80 * EDSCALE);416replication_display->set_column_title(3, TTR("Count"));417replication_display->set_column_expand(3, false);418replication_display->set_column_clip_content(3, true);419replication_display->set_column_custom_minimum_width(3, 80 * EDSCALE);420replication_display->set_column_title(4, TTR("Size"));421replication_display->set_column_expand(4, false);422replication_display->set_column_clip_content(4, true);423replication_display->set_column_custom_minimum_width(4, 80 * EDSCALE);424replication_display->connect("button_clicked", callable_mp(this, &EditorNetworkProfiler::_replication_button_clicked));425sc->add_child(replication_display);426427refresh_timer = memnew(Timer);428refresh_timer->set_wait_time(0.5);429refresh_timer->connect("timeout", callable_mp(this, &EditorNetworkProfiler::_refresh));430add_child(refresh_timer);431}432433434