Path: blob/master/modules/multiplayer/editor/editor_network_profiler.cpp
20829 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 "core/io/resource_loader.h"33#include "editor/editor_string_names.h"34#include "editor/run/editor_run_bar.h"35#include "editor/settings/editor_settings.h"36#include "editor/themes/editor_scale.h"37#include "scene/gui/check_box.h"38#include "scene/gui/flow_container.h"39#include "scene/gui/line_edit.h"40#include "scene/main/timer.h"4142void EditorNetworkProfiler::_bind_methods() {43ADD_SIGNAL(MethodInfo("enable_profiling", PropertyInfo(Variant::BOOL, "enable")));44ADD_SIGNAL(MethodInfo("open_request", PropertyInfo(Variant::STRING, "path")));45}4647void EditorNetworkProfiler::_notification(int p_what) {48switch (p_what) {49case NOTIFICATION_TRANSLATION_CHANGED: {50// TRANSLATORS: This is the label for the network profiler's incoming bandwidth.51down_label->set_text(TTR("Down", "Network"));52// TRANSLATORS: This is the label for the network profiler's outgoing bandwidth.53up_label->set_text(TTR("Up", "Network"));5455set_bandwidth(incoming_bandwidth, outgoing_bandwidth);5657if (is_ready()) {58refresh_rpc_data();59}60} break;6162case NOTIFICATION_THEME_CHANGED: {63if (activate->is_pressed()) {64activate->set_button_icon(theme_cache.stop_icon);65} else {66activate->set_button_icon(theme_cache.play_icon);67}68clear_button->set_button_icon(theme_cache.clear_icon);6970incoming_bandwidth_text->set_right_icon(theme_cache.incoming_bandwidth_icon);71outgoing_bandwidth_text->set_right_icon(theme_cache.outgoing_bandwidth_icon);7273// This needs to be done here to set the faded color when the profiler is first opened74incoming_bandwidth_text->add_theme_color_override("font_uneditable_color", theme_cache.incoming_bandwidth_color * Color(1, 1, 1, 0.5));75outgoing_bandwidth_text->add_theme_color_override("font_uneditable_color", theme_cache.outgoing_bandwidth_color * Color(1, 1, 1, 0.5));76} break;77}78}7980void EditorNetworkProfiler::_update_theme_item_cache() {81VBoxContainer::_update_theme_item_cache();8283theme_cache.node_icon = get_theme_icon(SNAME("Node"), EditorStringName(EditorIcons));84theme_cache.stop_icon = get_theme_icon(SNAME("Stop"), EditorStringName(EditorIcons));85theme_cache.play_icon = get_theme_icon(SNAME("Play"), EditorStringName(EditorIcons));86theme_cache.clear_icon = get_theme_icon(SNAME("Clear"), EditorStringName(EditorIcons));8788theme_cache.multiplayer_synchronizer_icon = get_theme_icon("MultiplayerSynchronizer", EditorStringName(EditorIcons));89theme_cache.instance_options_icon = get_theme_icon(SNAME("InstanceOptions"), EditorStringName(EditorIcons));9091theme_cache.incoming_bandwidth_icon = get_theme_icon(SNAME("ArrowDown"), EditorStringName(EditorIcons));92theme_cache.outgoing_bandwidth_icon = get_theme_icon(SNAME("ArrowUp"), EditorStringName(EditorIcons));9394theme_cache.incoming_bandwidth_color = get_theme_color(SceneStringName(font_color), EditorStringName(Editor));95theme_cache.outgoing_bandwidth_color = get_theme_color(SceneStringName(font_color), EditorStringName(Editor));96}9798void EditorNetworkProfiler::_refresh() {99if (!dirty) {100return;101}102dirty = false;103refresh_rpc_data();104refresh_replication_data();105}106107void EditorNetworkProfiler::refresh_rpc_data() {108counters_display->clear();109110TreeItem *root = counters_display->create_item();111int cols = counters_display->get_columns();112113for (const KeyValue<ObjectID, RPCNodeInfo> &E : rpc_data) {114TreeItem *node = counters_display->create_item(root);115116for (int j = 0; j < cols; ++j) {117node->set_text_alignment(j, j > 0 ? HORIZONTAL_ALIGNMENT_RIGHT : HORIZONTAL_ALIGNMENT_LEFT);118}119120node->set_text(0, E.value.node_path);121node->set_text(1, E.value.incoming_rpc == 0 ? "-" : vformat(TTR("%d (%s)"), E.value.incoming_rpc, String::humanize_size(E.value.incoming_size)));122node->set_text(2, E.value.outgoing_rpc == 0 ? "-" : vformat(TTR("%d (%s)"), E.value.outgoing_rpc, String::humanize_size(E.value.outgoing_size)));123}124}125126void EditorNetworkProfiler::refresh_replication_data() {127replication_display->clear();128129TreeItem *root = replication_display->create_item();130131for (const KeyValue<ObjectID, SyncInfo> &E : sync_data) {132// Ensure the nodes have at least a temporary cache.133ObjectID ids[3] = { E.value.synchronizer, E.value.config, E.value.root_node };134for (uint32_t i = 0; i < 3; i++) {135const ObjectID &id = ids[i];136if (!node_data.has(id)) {137missing_node_data.insert(id);138node_data[id] = NodeInfo(id);139}140}141142TreeItem *node = replication_display->create_item(root);143144const NodeInfo &root_info = node_data[E.value.root_node];145const NodeInfo &sync_info = node_data[E.value.synchronizer];146const NodeInfo &cfg_info = node_data[E.value.config];147148node->set_text(0, root_info.path.get_file());149node->set_icon(0, has_theme_icon(root_info.type, EditorStringName(EditorIcons)) ? get_theme_icon(root_info.type, EditorStringName(EditorIcons)) : theme_cache.node_icon);150node->set_tooltip_text(0, root_info.path);151152node->set_text(1, sync_info.path.get_file());153node->set_icon(1, theme_cache.multiplayer_synchronizer_icon);154node->set_tooltip_text(1, sync_info.path);155156int cfg_idx = cfg_info.path.find("::");157if (cfg_info.path.begins_with("res://") && ResourceLoader::exists(cfg_info.path) && cfg_idx > 0) {158String res_idstr = cfg_info.path.substr(cfg_idx + 2).replace("SceneReplicationConfig_", "");159String scene_path = cfg_info.path.substr(0, cfg_idx);160node->set_text(2, vformat("%s (%s)", res_idstr, scene_path.get_file()));161node->add_button(2, theme_cache.instance_options_icon);162node->set_tooltip_text(2, cfg_info.path);163node->set_metadata(2, scene_path);164} else {165node->set_text(2, cfg_info.path);166node->set_metadata(2, "");167}168169node->set_text(3, vformat("%d - %d", E.value.incoming_syncs, E.value.outgoing_syncs));170node->set_text(4, vformat("%d - %d", E.value.incoming_size, E.value.outgoing_size));171}172}173174Array EditorNetworkProfiler::pop_missing_node_data() {175Array out;176for (const ObjectID &id : missing_node_data) {177out.push_back(id);178}179missing_node_data.clear();180return out;181}182183void EditorNetworkProfiler::add_node_data(const NodeInfo &p_info) {184ERR_FAIL_COND(!node_data.has(p_info.id));185node_data[p_info.id] = p_info;186dirty = true;187}188189void EditorNetworkProfiler::_activate_pressed() {190_update_button_text();191192if (activate->is_pressed()) {193refresh_timer->start();194} else {195refresh_timer->stop();196}197198emit_signal(SNAME("enable_profiling"), activate->is_pressed());199}200201void EditorNetworkProfiler::_update_button_text() {202if (activate->is_pressed()) {203activate->set_button_icon(theme_cache.stop_icon);204activate->set_text(TTRC("Stop"));205} else {206activate->set_button_icon(theme_cache.play_icon);207activate->set_text(TTRC("Start"));208}209}210211void EditorNetworkProfiler::started() {212_clear_pressed();213activate->set_disabled(false);214215if (EditorSettings::get_singleton()->get_project_metadata("debug_options", "autostart_network_profiler", false)) {216set_profiling(true);217refresh_timer->start();218}219}220221void EditorNetworkProfiler::stopped() {222activate->set_disabled(true);223set_profiling(false);224refresh_timer->stop();225}226227void EditorNetworkProfiler::set_profiling(bool p_pressed) {228activate->set_pressed(p_pressed);229_update_button_text();230emit_signal(SNAME("enable_profiling"), activate->is_pressed());231}232233void EditorNetworkProfiler::_clear_pressed() {234rpc_data.clear();235sync_data.clear();236node_data.clear();237missing_node_data.clear();238set_bandwidth(0, 0);239refresh_rpc_data();240refresh_replication_data();241clear_button->set_disabled(true);242}243244void EditorNetworkProfiler::_autostart_toggled(bool p_toggled_on) {245EditorSettings::get_singleton()->set_project_metadata("debug_options", "autostart_network_profiler", p_toggled_on);246EditorRunBar::get_singleton()->update_profiler_autostart_indicator();247}248249void EditorNetworkProfiler::_replication_button_clicked(TreeItem *p_item, int p_column, int p_idx, MouseButton p_button) {250if (!p_item) {251return;252}253String meta = p_item->get_metadata(p_column);254if (meta.size() && ResourceLoader::exists(meta)) {255emit_signal("open_request", meta);256}257}258259void EditorNetworkProfiler::add_rpc_frame_data(const RPCNodeInfo &p_frame) {260if (clear_button->is_disabled()) {261clear_button->set_disabled(false);262}263dirty = true;264if (!rpc_data.has(p_frame.node)) {265rpc_data.insert(p_frame.node, p_frame);266} else {267rpc_data[p_frame.node].incoming_rpc += p_frame.incoming_rpc;268rpc_data[p_frame.node].outgoing_rpc += p_frame.outgoing_rpc;269}270if (p_frame.incoming_rpc) {271rpc_data[p_frame.node].incoming_size = p_frame.incoming_size / p_frame.incoming_rpc;272}273if (p_frame.outgoing_rpc) {274rpc_data[p_frame.node].outgoing_size = p_frame.outgoing_size / p_frame.outgoing_rpc;275}276}277278void EditorNetworkProfiler::add_sync_frame_data(const SyncInfo &p_frame) {279if (clear_button->is_disabled()) {280clear_button->set_disabled(false);281}282dirty = true;283if (!sync_data.has(p_frame.synchronizer)) {284sync_data[p_frame.synchronizer] = p_frame;285} else {286sync_data[p_frame.synchronizer].incoming_syncs += p_frame.incoming_syncs;287sync_data[p_frame.synchronizer].outgoing_syncs += p_frame.outgoing_syncs;288}289SyncInfo &info = sync_data[p_frame.synchronizer];290if (p_frame.incoming_syncs) {291info.incoming_size = p_frame.incoming_size / p_frame.incoming_syncs;292}293if (p_frame.outgoing_syncs) {294info.outgoing_size = p_frame.outgoing_size / p_frame.outgoing_syncs;295}296}297298void EditorNetworkProfiler::set_bandwidth(int p_incoming, int p_outgoing) {299incoming_bandwidth = p_incoming;300outgoing_bandwidth = p_outgoing;301302incoming_bandwidth_text->set_text(vformat(TTR("%s/s"), String::humanize_size(p_incoming)));303outgoing_bandwidth_text->set_text(vformat(TTR("%s/s"), String::humanize_size(p_outgoing)));304305// Make labels more prominent when the bandwidth is greater than 0 to attract user attention306incoming_bandwidth_text->add_theme_color_override(307"font_uneditable_color",308theme_cache.incoming_bandwidth_color * Color(1, 1, 1, p_incoming > 0 ? 1 : 0.5));309outgoing_bandwidth_text->add_theme_color_override(310"font_uneditable_color",311theme_cache.outgoing_bandwidth_color * Color(1, 1, 1, p_outgoing > 0 ? 1 : 0.5));312}313314bool EditorNetworkProfiler::is_profiling() {315return activate->is_pressed();316}317318EditorNetworkProfiler::EditorNetworkProfiler() {319FlowContainer *container = memnew(FlowContainer);320container->add_theme_constant_override(SNAME("h_separation"), 8 * EDSCALE);321container->add_theme_constant_override(SNAME("v_separation"), 2 * EDSCALE);322add_child(container);323324activate = memnew(Button);325activate->set_toggle_mode(true);326activate->set_text(TTRC("Start"));327activate->set_disabled(true);328activate->connect(SceneStringName(pressed), callable_mp(this, &EditorNetworkProfiler::_activate_pressed));329container->add_child(activate);330331clear_button = memnew(Button);332clear_button->set_text(TTRC("Clear"));333clear_button->set_disabled(true);334clear_button->connect(SceneStringName(pressed), callable_mp(this, &EditorNetworkProfiler::_clear_pressed));335container->add_child(clear_button);336337CheckBox *autostart_checkbox = memnew(CheckBox);338autostart_checkbox->set_text(TTRC("Autostart"));339autostart_checkbox->set_pressed(EditorSettings::get_singleton()->get_project_metadata("debug_options", "autostart_network_profiler", false));340autostart_checkbox->connect(SceneStringName(toggled), callable_mp(this, &EditorNetworkProfiler::_autostart_toggled));341container->add_child(autostart_checkbox);342343Control *c = memnew(Control);344c->set_h_size_flags(SIZE_EXPAND_FILL);345container->add_child(c);346347HBoxContainer *hb = memnew(HBoxContainer);348hb->add_theme_constant_override(SNAME("separation"), 8 * EDSCALE);349container->add_child(hb);350351down_label = memnew(Label);352down_label->set_focus_mode(FOCUS_ACCESSIBILITY);353down_label->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);354hb->add_child(down_label);355356incoming_bandwidth_text = memnew(LineEdit);357incoming_bandwidth_text->set_editable(false);358incoming_bandwidth_text->set_custom_minimum_size(Size2(120, 0) * EDSCALE);359incoming_bandwidth_text->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);360incoming_bandwidth_text->set_accessibility_name(TTRC("Incoming Bandwidth"));361hb->add_child(incoming_bandwidth_text);362363Control *down_up_spacer = memnew(Control);364down_up_spacer->set_custom_minimum_size(Size2(30, 0) * EDSCALE);365hb->add_child(down_up_spacer);366367up_label = memnew(Label);368up_label->set_focus_mode(FOCUS_ACCESSIBILITY);369up_label->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);370hb->add_child(up_label);371372outgoing_bandwidth_text = memnew(LineEdit);373outgoing_bandwidth_text->set_editable(false);374outgoing_bandwidth_text->set_custom_minimum_size(Size2(120, 0) * EDSCALE);375outgoing_bandwidth_text->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);376outgoing_bandwidth_text->set_accessibility_name(TTRC("Outgoing Bandwidth"));377hb->add_child(outgoing_bandwidth_text);378379HSplitContainer *sc = memnew(HSplitContainer);380add_child(sc);381sc->set_v_size_flags(SIZE_EXPAND_FILL);382sc->set_h_size_flags(SIZE_EXPAND_FILL);383sc->set_split_offset(100 * EDSCALE);384385// RPC386counters_display = memnew(Tree);387counters_display->set_custom_minimum_size(Size2(280, 0) * EDSCALE);388counters_display->set_v_size_flags(SIZE_EXPAND_FILL);389counters_display->set_h_size_flags(SIZE_EXPAND_FILL);390counters_display->set_hide_folding(true);391counters_display->set_hide_root(true);392counters_display->set_columns(3);393counters_display->set_column_titles_visible(true);394counters_display->set_column_title(0, TTRC("Node"));395counters_display->set_column_expand(0, true);396counters_display->set_column_clip_content(0, true);397counters_display->set_column_custom_minimum_width(0, 60 * EDSCALE);398counters_display->set_column_title(1, TTRC("Incoming RPC"));399counters_display->set_column_expand(1, false);400counters_display->set_column_clip_content(1, true);401counters_display->set_column_custom_minimum_width(1, 120 * EDSCALE);402counters_display->set_column_title(2, TTRC("Outgoing RPC"));403counters_display->set_column_expand(2, false);404counters_display->set_column_clip_content(2, true);405counters_display->set_theme_type_variation("TreeSecondary");406counters_display->set_column_custom_minimum_width(2, 120 * EDSCALE);407sc->add_child(counters_display);408409// Replication410replication_display = memnew(Tree);411replication_display->set_custom_minimum_size(Size2(280, 0) * EDSCALE);412replication_display->set_v_size_flags(SIZE_EXPAND_FILL);413replication_display->set_h_size_flags(SIZE_EXPAND_FILL);414replication_display->set_hide_folding(true);415replication_display->set_hide_root(true);416replication_display->set_columns(5);417replication_display->set_column_titles_visible(true);418replication_display->set_column_title(0, TTRC("Root"));419replication_display->set_column_expand(0, true);420replication_display->set_column_clip_content(0, true);421replication_display->set_column_custom_minimum_width(0, 80 * EDSCALE);422replication_display->set_column_title(1, TTRC("Synchronizer"));423replication_display->set_column_expand(1, true);424replication_display->set_column_clip_content(1, true);425replication_display->set_column_custom_minimum_width(1, 80 * EDSCALE);426replication_display->set_column_title(2, TTRC("Config"));427replication_display->set_column_expand(2, true);428replication_display->set_column_clip_content(2, true);429replication_display->set_column_custom_minimum_width(2, 80 * EDSCALE);430replication_display->set_column_title(3, TTRC("Count"));431replication_display->set_column_expand(3, false);432replication_display->set_column_clip_content(3, true);433replication_display->set_column_custom_minimum_width(3, 80 * EDSCALE);434replication_display->set_column_title(4, TTRC("Size"));435replication_display->set_column_expand(4, false);436replication_display->set_column_clip_content(4, true);437replication_display->set_column_custom_minimum_width(4, 80 * EDSCALE);438replication_display->set_theme_type_variation("TreeSecondary");439replication_display->connect("button_clicked", callable_mp(this, &EditorNetworkProfiler::_replication_button_clicked));440sc->add_child(replication_display);441442refresh_timer = memnew(Timer);443refresh_timer->set_wait_time(0.5);444refresh_timer->connect("timeout", callable_mp(this, &EditorNetworkProfiler::_refresh));445add_child(refresh_timer);446}447448449