Path: blob/master/modules/multiplayer/editor/editor_network_profiler.cpp
11354 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"39#include "scene/main/timer.h"4041void EditorNetworkProfiler::_bind_methods() {42ADD_SIGNAL(MethodInfo("enable_profiling", PropertyInfo(Variant::BOOL, "enable")));43ADD_SIGNAL(MethodInfo("open_request", PropertyInfo(Variant::STRING, "path")));44}4546void EditorNetworkProfiler::_notification(int p_what) {47switch (p_what) {48case NOTIFICATION_THEME_CHANGED: {49if (activate->is_pressed()) {50activate->set_button_icon(theme_cache.stop_icon);51} else {52activate->set_button_icon(theme_cache.play_icon);53}54clear_button->set_button_icon(theme_cache.clear_icon);5556incoming_bandwidth_text->set_right_icon(theme_cache.incoming_bandwidth_icon);57outgoing_bandwidth_text->set_right_icon(theme_cache.outgoing_bandwidth_icon);5859// This needs to be done here to set the faded color when the profiler is first opened60incoming_bandwidth_text->add_theme_color_override("font_uneditable_color", theme_cache.incoming_bandwidth_color * Color(1, 1, 1, 0.5));61outgoing_bandwidth_text->add_theme_color_override("font_uneditable_color", theme_cache.outgoing_bandwidth_color * Color(1, 1, 1, 0.5));62} break;63}64}6566void EditorNetworkProfiler::_update_theme_item_cache() {67VBoxContainer::_update_theme_item_cache();6869theme_cache.node_icon = get_theme_icon(SNAME("Node"), EditorStringName(EditorIcons));70theme_cache.stop_icon = get_theme_icon(SNAME("Stop"), EditorStringName(EditorIcons));71theme_cache.play_icon = get_theme_icon(SNAME("Play"), EditorStringName(EditorIcons));72theme_cache.clear_icon = get_theme_icon(SNAME("Clear"), EditorStringName(EditorIcons));7374theme_cache.multiplayer_synchronizer_icon = get_theme_icon("MultiplayerSynchronizer", EditorStringName(EditorIcons));75theme_cache.instance_options_icon = get_theme_icon(SNAME("InstanceOptions"), EditorStringName(EditorIcons));7677theme_cache.incoming_bandwidth_icon = get_theme_icon(SNAME("ArrowDown"), EditorStringName(EditorIcons));78theme_cache.outgoing_bandwidth_icon = get_theme_icon(SNAME("ArrowUp"), EditorStringName(EditorIcons));7980theme_cache.incoming_bandwidth_color = get_theme_color(SceneStringName(font_color), EditorStringName(Editor));81theme_cache.outgoing_bandwidth_color = get_theme_color(SceneStringName(font_color), EditorStringName(Editor));82}8384void EditorNetworkProfiler::_refresh() {85if (!dirty) {86return;87}88dirty = false;89refresh_rpc_data();90refresh_replication_data();91}9293void EditorNetworkProfiler::refresh_rpc_data() {94counters_display->clear();9596TreeItem *root = counters_display->create_item();97int cols = counters_display->get_columns();9899for (const KeyValue<ObjectID, RPCNodeInfo> &E : rpc_data) {100TreeItem *node = counters_display->create_item(root);101102for (int j = 0; j < cols; ++j) {103node->set_text_alignment(j, j > 0 ? HORIZONTAL_ALIGNMENT_RIGHT : HORIZONTAL_ALIGNMENT_LEFT);104}105106node->set_text(0, E.value.node_path);107node->set_text(1, E.value.incoming_rpc == 0 ? "-" : vformat(TTR("%d (%s)"), E.value.incoming_rpc, String::humanize_size(E.value.incoming_size)));108node->set_text(2, E.value.outgoing_rpc == 0 ? "-" : vformat(TTR("%d (%s)"), E.value.outgoing_rpc, String::humanize_size(E.value.outgoing_size)));109}110}111112void EditorNetworkProfiler::refresh_replication_data() {113replication_display->clear();114115TreeItem *root = replication_display->create_item();116117for (const KeyValue<ObjectID, SyncInfo> &E : sync_data) {118// Ensure the nodes have at least a temporary cache.119ObjectID ids[3] = { E.value.synchronizer, E.value.config, E.value.root_node };120for (uint32_t i = 0; i < 3; i++) {121const ObjectID &id = ids[i];122if (!node_data.has(id)) {123missing_node_data.insert(id);124node_data[id] = NodeInfo(id);125}126}127128TreeItem *node = replication_display->create_item(root);129130const NodeInfo &root_info = node_data[E.value.root_node];131const NodeInfo &sync_info = node_data[E.value.synchronizer];132const NodeInfo &cfg_info = node_data[E.value.config];133134node->set_text(0, root_info.path.get_file());135node->set_icon(0, has_theme_icon(root_info.type, EditorStringName(EditorIcons)) ? get_theme_icon(root_info.type, EditorStringName(EditorIcons)) : theme_cache.node_icon);136node->set_tooltip_text(0, root_info.path);137138node->set_text(1, sync_info.path.get_file());139node->set_icon(1, theme_cache.multiplayer_synchronizer_icon);140node->set_tooltip_text(1, sync_info.path);141142int cfg_idx = cfg_info.path.find("::");143if (cfg_info.path.begins_with("res://") && ResourceLoader::exists(cfg_info.path) && cfg_idx > 0) {144String res_idstr = cfg_info.path.substr(cfg_idx + 2).replace("SceneReplicationConfig_", "");145String scene_path = cfg_info.path.substr(0, cfg_idx);146node->set_text(2, vformat("%s (%s)", res_idstr, scene_path.get_file()));147node->add_button(2, theme_cache.instance_options_icon);148node->set_tooltip_text(2, cfg_info.path);149node->set_metadata(2, scene_path);150} else {151node->set_text(2, cfg_info.path);152node->set_metadata(2, "");153}154155node->set_text(3, vformat("%d - %d", E.value.incoming_syncs, E.value.outgoing_syncs));156node->set_text(4, vformat("%d - %d", E.value.incoming_size, E.value.outgoing_size));157}158}159160Array EditorNetworkProfiler::pop_missing_node_data() {161Array out;162for (const ObjectID &id : missing_node_data) {163out.push_back(id);164}165missing_node_data.clear();166return out;167}168169void EditorNetworkProfiler::add_node_data(const NodeInfo &p_info) {170ERR_FAIL_COND(!node_data.has(p_info.id));171node_data[p_info.id] = p_info;172dirty = true;173}174175void EditorNetworkProfiler::_activate_pressed() {176_update_button_text();177178if (activate->is_pressed()) {179refresh_timer->start();180} else {181refresh_timer->stop();182}183184emit_signal(SNAME("enable_profiling"), activate->is_pressed());185}186187void EditorNetworkProfiler::_update_button_text() {188if (activate->is_pressed()) {189activate->set_button_icon(theme_cache.stop_icon);190activate->set_text(TTR("Stop"));191} else {192activate->set_button_icon(theme_cache.play_icon);193activate->set_text(TTR("Start"));194}195}196197void EditorNetworkProfiler::started() {198_clear_pressed();199activate->set_disabled(false);200201if (EditorSettings::get_singleton()->get_project_metadata("debug_options", "autostart_network_profiler", false)) {202set_profiling(true);203refresh_timer->start();204}205}206207void EditorNetworkProfiler::stopped() {208activate->set_disabled(true);209set_profiling(false);210refresh_timer->stop();211}212213void EditorNetworkProfiler::set_profiling(bool p_pressed) {214activate->set_pressed(p_pressed);215_update_button_text();216emit_signal(SNAME("enable_profiling"), activate->is_pressed());217}218219void EditorNetworkProfiler::_clear_pressed() {220rpc_data.clear();221sync_data.clear();222node_data.clear();223missing_node_data.clear();224set_bandwidth(0, 0);225refresh_rpc_data();226refresh_replication_data();227clear_button->set_disabled(true);228}229230void EditorNetworkProfiler::_autostart_toggled(bool p_toggled_on) {231EditorSettings::get_singleton()->set_project_metadata("debug_options", "autostart_network_profiler", p_toggled_on);232EditorRunBar::get_singleton()->update_profiler_autostart_indicator();233}234235void EditorNetworkProfiler::_replication_button_clicked(TreeItem *p_item, int p_column, int p_idx, MouseButton p_button) {236if (!p_item) {237return;238}239String meta = p_item->get_metadata(p_column);240if (meta.size() && ResourceLoader::exists(meta)) {241emit_signal("open_request", meta);242}243}244245void EditorNetworkProfiler::add_rpc_frame_data(const RPCNodeInfo &p_frame) {246if (clear_button->is_disabled()) {247clear_button->set_disabled(false);248}249dirty = true;250if (!rpc_data.has(p_frame.node)) {251rpc_data.insert(p_frame.node, p_frame);252} else {253rpc_data[p_frame.node].incoming_rpc += p_frame.incoming_rpc;254rpc_data[p_frame.node].outgoing_rpc += p_frame.outgoing_rpc;255}256if (p_frame.incoming_rpc) {257rpc_data[p_frame.node].incoming_size = p_frame.incoming_size / p_frame.incoming_rpc;258}259if (p_frame.outgoing_rpc) {260rpc_data[p_frame.node].outgoing_size = p_frame.outgoing_size / p_frame.outgoing_rpc;261}262}263264void EditorNetworkProfiler::add_sync_frame_data(const SyncInfo &p_frame) {265if (clear_button->is_disabled()) {266clear_button->set_disabled(false);267}268dirty = true;269if (!sync_data.has(p_frame.synchronizer)) {270sync_data[p_frame.synchronizer] = p_frame;271} else {272sync_data[p_frame.synchronizer].incoming_syncs += p_frame.incoming_syncs;273sync_data[p_frame.synchronizer].outgoing_syncs += p_frame.outgoing_syncs;274}275SyncInfo &info = sync_data[p_frame.synchronizer];276if (p_frame.incoming_syncs) {277info.incoming_size = p_frame.incoming_size / p_frame.incoming_syncs;278}279if (p_frame.outgoing_syncs) {280info.outgoing_size = p_frame.outgoing_size / p_frame.outgoing_syncs;281}282}283284void EditorNetworkProfiler::set_bandwidth(int p_incoming, int p_outgoing) {285incoming_bandwidth_text->set_text(vformat(TTR("%s/s"), String::humanize_size(p_incoming)));286outgoing_bandwidth_text->set_text(vformat(TTR("%s/s"), String::humanize_size(p_outgoing)));287288// Make labels more prominent when the bandwidth is greater than 0 to attract user attention289incoming_bandwidth_text->add_theme_color_override(290"font_uneditable_color",291theme_cache.incoming_bandwidth_color * Color(1, 1, 1, p_incoming > 0 ? 1 : 0.5));292outgoing_bandwidth_text->add_theme_color_override(293"font_uneditable_color",294theme_cache.outgoing_bandwidth_color * Color(1, 1, 1, p_outgoing > 0 ? 1 : 0.5));295}296297bool EditorNetworkProfiler::is_profiling() {298return activate->is_pressed();299}300301EditorNetworkProfiler::EditorNetworkProfiler() {302FlowContainer *container = memnew(FlowContainer);303container->add_theme_constant_override(SNAME("h_separation"), 8 * EDSCALE);304container->add_theme_constant_override(SNAME("v_separation"), 2 * EDSCALE);305add_child(container);306307activate = memnew(Button);308activate->set_toggle_mode(true);309activate->set_text(TTR("Start"));310activate->set_disabled(true);311activate->connect(SceneStringName(pressed), callable_mp(this, &EditorNetworkProfiler::_activate_pressed));312container->add_child(activate);313314clear_button = memnew(Button);315clear_button->set_text(TTR("Clear"));316clear_button->set_disabled(true);317clear_button->connect(SceneStringName(pressed), callable_mp(this, &EditorNetworkProfiler::_clear_pressed));318container->add_child(clear_button);319320CheckBox *autostart_checkbox = memnew(CheckBox);321autostart_checkbox->set_text(TTR("Autostart"));322autostart_checkbox->set_pressed(EditorSettings::get_singleton()->get_project_metadata("debug_options", "autostart_network_profiler", false));323autostart_checkbox->connect(SceneStringName(toggled), callable_mp(this, &EditorNetworkProfiler::_autostart_toggled));324container->add_child(autostart_checkbox);325326Control *c = memnew(Control);327c->set_h_size_flags(SIZE_EXPAND_FILL);328container->add_child(c);329330HBoxContainer *hb = memnew(HBoxContainer);331hb->add_theme_constant_override(SNAME("separation"), 8 * EDSCALE);332container->add_child(hb);333334Label *lb = memnew(Label);335// TRANSLATORS: This is the label for the network profiler's incoming bandwidth.336lb->set_focus_mode(FOCUS_ACCESSIBILITY);337lb->set_text(TTR("Down", "Network"));338hb->add_child(lb);339340incoming_bandwidth_text = memnew(LineEdit);341incoming_bandwidth_text->set_editable(false);342incoming_bandwidth_text->set_custom_minimum_size(Size2(120, 0) * EDSCALE);343incoming_bandwidth_text->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);344incoming_bandwidth_text->set_accessibility_name(TTRC("Incoming Bandwidth"));345hb->add_child(incoming_bandwidth_text);346347Control *down_up_spacer = memnew(Control);348down_up_spacer->set_custom_minimum_size(Size2(30, 0) * EDSCALE);349hb->add_child(down_up_spacer);350351lb = memnew(Label);352// TRANSLATORS: This is the label for the network profiler's outgoing bandwidth.353lb->set_focus_mode(FOCUS_ACCESSIBILITY);354lb->set_text(TTR("Up", "Network"));355hb->add_child(lb);356357outgoing_bandwidth_text = memnew(LineEdit);358outgoing_bandwidth_text->set_editable(false);359outgoing_bandwidth_text->set_custom_minimum_size(Size2(120, 0) * EDSCALE);360outgoing_bandwidth_text->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);361outgoing_bandwidth_text->set_accessibility_name(TTRC("Outgoing Bandwidth"));362hb->add_child(outgoing_bandwidth_text);363364// Set initial texts in the incoming/outgoing bandwidth labels365set_bandwidth(0, 0);366367HSplitContainer *sc = memnew(HSplitContainer);368add_child(sc);369sc->set_v_size_flags(SIZE_EXPAND_FILL);370sc->set_h_size_flags(SIZE_EXPAND_FILL);371sc->set_split_offset(100 * EDSCALE);372373// RPC374counters_display = memnew(Tree);375counters_display->set_custom_minimum_size(Size2(280, 0) * EDSCALE);376counters_display->set_v_size_flags(SIZE_EXPAND_FILL);377counters_display->set_h_size_flags(SIZE_EXPAND_FILL);378counters_display->set_hide_folding(true);379counters_display->set_hide_root(true);380counters_display->set_columns(3);381counters_display->set_column_titles_visible(true);382counters_display->set_column_title(0, TTR("Node"));383counters_display->set_column_expand(0, true);384counters_display->set_column_clip_content(0, true);385counters_display->set_column_custom_minimum_width(0, 60 * EDSCALE);386counters_display->set_column_title(1, TTR("Incoming RPC"));387counters_display->set_column_expand(1, false);388counters_display->set_column_clip_content(1, true);389counters_display->set_column_custom_minimum_width(1, 120 * EDSCALE);390counters_display->set_column_title(2, TTR("Outgoing RPC"));391counters_display->set_column_expand(2, false);392counters_display->set_column_clip_content(2, true);393counters_display->set_column_custom_minimum_width(2, 120 * EDSCALE);394sc->add_child(counters_display);395396// Replication397replication_display = memnew(Tree);398replication_display->set_custom_minimum_size(Size2(280, 0) * EDSCALE);399replication_display->set_v_size_flags(SIZE_EXPAND_FILL);400replication_display->set_h_size_flags(SIZE_EXPAND_FILL);401replication_display->set_hide_folding(true);402replication_display->set_hide_root(true);403replication_display->set_columns(5);404replication_display->set_column_titles_visible(true);405replication_display->set_column_title(0, TTR("Root"));406replication_display->set_column_expand(0, true);407replication_display->set_column_clip_content(0, true);408replication_display->set_column_custom_minimum_width(0, 80 * EDSCALE);409replication_display->set_column_title(1, TTR("Synchronizer"));410replication_display->set_column_expand(1, true);411replication_display->set_column_clip_content(1, true);412replication_display->set_column_custom_minimum_width(1, 80 * EDSCALE);413replication_display->set_column_title(2, TTR("Config"));414replication_display->set_column_expand(2, true);415replication_display->set_column_clip_content(2, true);416replication_display->set_column_custom_minimum_width(2, 80 * EDSCALE);417replication_display->set_column_title(3, TTR("Count"));418replication_display->set_column_expand(3, false);419replication_display->set_column_clip_content(3, true);420replication_display->set_column_custom_minimum_width(3, 80 * EDSCALE);421replication_display->set_column_title(4, TTR("Size"));422replication_display->set_column_expand(4, false);423replication_display->set_column_clip_content(4, true);424replication_display->set_column_custom_minimum_width(4, 80 * EDSCALE);425replication_display->connect("button_clicked", callable_mp(this, &EditorNetworkProfiler::_replication_button_clicked));426sc->add_child(replication_display);427428refresh_timer = memnew(Timer);429refresh_timer->set_wait_time(0.5);430refresh_timer->connect("timeout", callable_mp(this, &EditorNetworkProfiler::_refresh));431add_child(refresh_timer);432}433434435