Debian Codegraph @

modules/multiplayer/editor/editor_network_profiler.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* editor_network_profiler.cpp */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #include "editor_network_profiler.h"
32
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"
41
42 void EditorNetworkProfiler::_bind_methods() {
43 ADD_SIGNAL(MethodInfo("enable_profiling", PropertyInfo(Variant::BOOL, "enable")));
44 ADD_SIGNAL(MethodInfo("open_request", PropertyInfo(Variant::STRING, "path")));
45 }
46
47 void EditorNetworkProfiler::_notification(int p_what) {
48 switch (p_what) {
49 case NOTIFICATION_TRANSLATION_CHANGED: {
50 // TRANSLATORS: This is the label for the network profiler's incoming bandwidth.
51 down_label->set_text(TTR("Down", "Network"));
52 // TRANSLATORS: This is the label for the network profiler's outgoing bandwidth.
53 up_label->set_text(TTR("Up", "Network"));
54
55 set_bandwidth(incoming_bandwidth, outgoing_bandwidth);
56
57 if (is_ready()) {
58 refresh_rpc_data();
59 }
60 } break;
61
62 case NOTIFICATION_THEME_CHANGED: {
63 if (activate->is_pressed()) {
64 activate->set_button_icon(theme_cache.stop_icon);
65 } else {
66 activate->set_button_icon(theme_cache.play_icon);
67 }
68 clear_button->set_button_icon(theme_cache.clear_icon);
69
70 incoming_bandwidth_text->set_right_icon(theme_cache.incoming_bandwidth_icon);
71 outgoing_bandwidth_text->set_right_icon(theme_cache.outgoing_bandwidth_icon);
72
73 // This needs to be done here to set the faded color when the profiler is first opened
74 incoming_bandwidth_text->add_theme_color_override("font_uneditable_color", theme_cache.incoming_bandwidth_color * Color(1, 1, 1, 0.5));
75 outgoing_bandwidth_text->add_theme_color_override("font_uneditable_color", theme_cache.outgoing_bandwidth_color * Color(1, 1, 1, 0.5));
76 } break;
77 }
78 }
79
80 void EditorNetworkProfiler::_update_theme_item_cache() {
81 VBoxContainer::_update_theme_item_cache();
82
83 theme_cache.node_icon = get_theme_icon(SNAME("Node"), EditorStringName(EditorIcons));
84 theme_cache.stop_icon = get_theme_icon(SNAME("Stop"), EditorStringName(EditorIcons));
85 theme_cache.play_icon = get_theme_icon(SNAME("Play"), EditorStringName(EditorIcons));
86 theme_cache.clear_icon = get_theme_icon(SNAME("Clear"), EditorStringName(EditorIcons));
87
88 theme_cache.multiplayer_synchronizer_icon = get_theme_icon("MultiplayerSynchronizer", EditorStringName(EditorIcons));
89 theme_cache.instance_options_icon = get_theme_icon(SNAME("InstanceOptions"), EditorStringName(EditorIcons));
90
91 theme_cache.incoming_bandwidth_icon = get_theme_icon(SNAME("ArrowDown"), EditorStringName(EditorIcons));
92 theme_cache.outgoing_bandwidth_icon = get_theme_icon(SNAME("ArrowUp"), EditorStringName(EditorIcons));
93
94 theme_cache.incoming_bandwidth_color = get_theme_color(SceneStringName(font_color), EditorStringName(Editor));
95 theme_cache.outgoing_bandwidth_color = get_theme_color(SceneStringName(font_color), EditorStringName(Editor));
96 }
97
98 void EditorNetworkProfiler::_refresh() {
99 if (!dirty) {
100 return;
101 }
102 dirty = false;
103 refresh_rpc_data();
104 refresh_replication_data();
105 }
106
107 void EditorNetworkProfiler::refresh_rpc_data() {
108 counters_display->clear();
109
110 TreeItem *root = counters_display->create_item();
111 int cols = counters_display->get_columns();
112
113 for (const KeyValue<ObjectID, RPCNodeInfo> &E : rpc_data) {
114 TreeItem *node = counters_display->create_item(root);
115
116 for (int j = 0; j < cols; ++j) {
117 node->set_text_alignment(j, j > 0 ? HORIZONTAL_ALIGNMENT_RIGHT : HORIZONTAL_ALIGNMENT_LEFT);
118 }
119
120 node->set_text(0, E.value.node_path);
121 node->set_text(1, E.value.incoming_rpc == 0 ? "-" : vformat(TTR("%d (%s)"), E.value.incoming_rpc, String::humanize_size(E.value.incoming_size)));
122 node->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 }
125
126 void EditorNetworkProfiler::refresh_replication_data() {
127 replication_display->clear();
128
129 TreeItem *root = replication_display->create_item();
130
131 for (const KeyValue<ObjectID, SyncInfo> &E : sync_data) {
132 // Ensure the nodes have at least a temporary cache.
133 ObjectID ids[3] = { E.value.synchronizer, E.value.config, E.value.root_node };
134 for (uint32_t i = 0; i < 3; i++) {
135 const ObjectID &id = ids[i];
136 if (!node_data.has(id)) {
137 missing_node_data.insert(id);
138 node_data[id] = NodeInfo(id);
139 }
140 }
141
142 TreeItem *node = replication_display->create_item(root);
143
144 const NodeInfo &root_info = node_data[E.value.root_node];
145 const NodeInfo &sync_info = node_data[E.value.synchronizer];
146 const NodeInfo &cfg_info = node_data[E.value.config];
147
148 node->set_text(0, root_info.path.get_file());
149 node->set_icon(0, has_theme_icon(root_info.type, EditorStringName(EditorIcons)) ? get_theme_icon(root_info.type, EditorStringName(EditorIcons)) : theme_cache.node_icon);
150 node->set_tooltip_text(0, root_info.path);
151
152 node->set_text(1, sync_info.path.get_file());
153 node->set_icon(1, theme_cache.multiplayer_synchronizer_icon);
154 node->set_tooltip_text(1, sync_info.path);
155
156 int cfg_idx = cfg_info.path.find("::");
157 if (cfg_info.path.begins_with("res://") && ResourceLoader::exists(cfg_info.path) && cfg_idx > 0) {
158 String res_idstr = cfg_info.path.substr(cfg_idx + 2).replace("SceneReplicationConfig_", "");
159 String scene_path = cfg_info.path.substr(0, cfg_idx);
160 node->set_text(2, vformat("%s (%s)", res_idstr, scene_path.get_file()));
161 node->add_button(2, theme_cache.instance_options_icon);
162 node->set_tooltip_text(2, cfg_info.path);
163 node->set_metadata(2, scene_path);
164 } else {
165 node->set_text(2, cfg_info.path);
166 node->set_metadata(2, "");
167 }
168
169 node->set_text(3, vformat("%d - %d", E.value.incoming_syncs, E.value.outgoing_syncs));
170 node->set_text(4, vformat("%d - %d", E.value.incoming_size, E.value.outgoing_size));
171 }
172 }
173
174 Array EditorNetworkProfiler::pop_missing_node_data() {
175 Array out;
176 for (const ObjectID &id : missing_node_data) {
177 out.push_back(id);
178 }
179 missing_node_data.clear();
180 return out;
181 }
182
183 void EditorNetworkProfiler::add_node_data(const NodeInfo &p_info) {
184 ERR_FAIL_COND(!node_data.has(p_info.id));
185 node_data[p_info.id] = p_info;
186 dirty = true;
187 }
188
189 void EditorNetworkProfiler::_activate_pressed() {
190 _update_button_text();
191
192 if (activate->is_pressed()) {
193 refresh_timer->start();
194 } else {
195 refresh_timer->stop();
196 }
197
198 emit_signal(SNAME("enable_profiling"), activate->is_pressed());
199 }
200
201 void EditorNetworkProfiler::_update_button_text() {
202 if (activate->is_pressed()) {
203 activate->set_button_icon(theme_cache.stop_icon);
204 activate->set_text(TTRC("Stop"));
205 } else {
206 activate->set_button_icon(theme_cache.play_icon);
207 activate->set_text(TTRC("Start"));
208 }
209 }
210
211 void EditorNetworkProfiler::started() {
212 _clear_pressed();
213 activate->set_disabled(false);
214
215 if (EditorSettings::get_singleton()->get_project_metadata("debug_options", "autostart_network_profiler", false)) {
216 set_profiling(true);
217 refresh_timer->start();
218 }
219 }
220
221 void EditorNetworkProfiler::stopped() {
222 activate->set_disabled(true);
223 set_profiling(false);
224 refresh_timer->stop();
225 }
226
227 void EditorNetworkProfiler::set_profiling(bool p_pressed) {
228 activate->set_pressed(p_pressed);
229 _update_button_text();
230 emit_signal(SNAME("enable_profiling"), activate->is_pressed());
231 }
232
233 void EditorNetworkProfiler::_clear_pressed() {
234 rpc_data.clear();
235 sync_data.clear();
236 node_data.clear();
237 missing_node_data.clear();
238 set_bandwidth(0, 0);
239 refresh_rpc_data();
240 refresh_replication_data();
241 clear_button->set_disabled(true);
242 }
243
244 void EditorNetworkProfiler::_autostart_toggled(bool p_toggled_on) {
245 EditorSettings::get_singleton()->set_project_metadata("debug_options", "autostart_network_profiler", p_toggled_on);
246 EditorRunBar::get_singleton()->update_profiler_autostart_indicator();
247 }
248
249 void EditorNetworkProfiler::_replication_button_clicked(TreeItem *p_item, int p_column, int p_idx, MouseButton p_button) {
250 if (!p_item) {
251 return;
252 }
253 String meta = p_item->get_metadata(p_column);
254 if (meta.size() && ResourceLoader::exists(meta)) {
255 emit_signal("open_request", meta);
256 }
257 }
258
259 void EditorNetworkProfiler::add_rpc_frame_data(const RPCNodeInfo &p_frame) {
260 if (clear_button->is_disabled()) {
261 clear_button->set_disabled(false);
262 }
263 dirty = true;
264 if (!rpc_data.has(p_frame.node)) {
265 rpc_data.insert(p_frame.node, p_frame);
266 } else {
267 rpc_data[p_frame.node].incoming_rpc += p_frame.incoming_rpc;
268 rpc_data[p_frame.node].outgoing_rpc += p_frame.outgoing_rpc;
269 }
270 if (p_frame.incoming_rpc) {
271 rpc_data[p_frame.node].incoming_size = p_frame.incoming_size / p_frame.incoming_rpc;
272 }
273 if (p_frame.outgoing_rpc) {
274 rpc_data[p_frame.node].outgoing_size = p_frame.outgoing_size / p_frame.outgoing_rpc;
275 }
276 }
277
278 void EditorNetworkProfiler::add_sync_frame_data(const SyncInfo &p_frame) {
279 if (clear_button->is_disabled()) {
280 clear_button->set_disabled(false);
281 }
282 dirty = true;
283 if (!sync_data.has(p_frame.synchronizer)) {
284 sync_data[p_frame.synchronizer] = p_frame;
285 } else {
286 sync_data[p_frame.synchronizer].incoming_syncs += p_frame.incoming_syncs;
287 sync_data[p_frame.synchronizer].outgoing_syncs += p_frame.outgoing_syncs;
288 }
289 SyncInfo &info = sync_data[p_frame.synchronizer];
290 if (p_frame.incoming_syncs) {
291 info.incoming_size = p_frame.incoming_size / p_frame.incoming_syncs;
292 }
293 if (p_frame.outgoing_syncs) {
294 info.outgoing_size = p_frame.outgoing_size / p_frame.outgoing_syncs;
295 }
296 }
297
298 void EditorNetworkProfiler::set_bandwidth(int p_incoming, int p_outgoing) {
299 incoming_bandwidth = p_incoming;
300 outgoing_bandwidth = p_outgoing;
301
302 incoming_bandwidth_text->set_text(vformat(TTR("%s/s"), String::humanize_size(p_incoming)));
303 outgoing_bandwidth_text->set_text(vformat(TTR("%s/s"), String::humanize_size(p_outgoing)));
304
305 // Make labels more prominent when the bandwidth is greater than 0 to attract user attention
306 incoming_bandwidth_text->add_theme_color_override(
307 "font_uneditable_color",
308 theme_cache.incoming_bandwidth_color * Color(1, 1, 1, p_incoming > 0 ? 1 : 0.5));
309 outgoing_bandwidth_text->add_theme_color_override(
310 "font_uneditable_color",
311 theme_cache.outgoing_bandwidth_color * Color(1, 1, 1, p_outgoing > 0 ? 1 : 0.5));
312 }
313
314 bool EditorNetworkProfiler::is_profiling() {
315 return activate->is_pressed();
316 }
317
318 EditorNetworkProfiler::EditorNetworkProfiler() {
319 FlowContainer *container = memnew(FlowContainer);
320 container->add_theme_constant_override(SNAME("h_separation"), 8 * EDSCALE);
321 container->add_theme_constant_override(SNAME("v_separation"), 2 * EDSCALE);
322 add_child(container);
323
324 activate = memnew(Button);
325 activate->set_toggle_mode(true);
326 activate->set_text(TTRC("Start"));
327 activate->set_disabled(true);
328 activate->connect(SceneStringName(pressed), callable_mp(this, &EditorNetworkProfiler::_activate_pressed));
329 container->add_child(activate);
330
331 clear_button = memnew(Button);
332 clear_button->set_text(TTRC("Clear"));
333 clear_button->set_disabled(true);
334 clear_button->connect(SceneStringName(pressed), callable_mp(this, &EditorNetworkProfiler::_clear_pressed));
335 container->add_child(clear_button);
336
337 CheckBox *autostart_checkbox = memnew(CheckBox);
338 autostart_checkbox->set_text(TTRC("Autostart"));
339 autostart_checkbox->set_pressed(EditorSettings::get_singleton()->get_project_metadata("debug_options", "autostart_network_profiler", false));
340 autostart_checkbox->connect(SceneStringName(toggled), callable_mp(this, &EditorNetworkProfiler::_autostart_toggled));
341 container->add_child(autostart_checkbox);
342
343 Control *c = memnew(Control);
344 c->set_h_size_flags(SIZE_EXPAND_FILL);
345 container->add_child(c);
346
347 HBoxContainer *hb = memnew(HBoxContainer);
348 hb->add_theme_constant_override(SNAME("separation"), 8 * EDSCALE);
349 container->add_child(hb);
350
351 down_label = memnew(Label);
352 down_label->set_focus_mode(FOCUS_ACCESSIBILITY);
353 down_label->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
354 hb->add_child(down_label);
355
356 incoming_bandwidth_text = memnew(LineEdit);
357 incoming_bandwidth_text->set_editable(false);
358 incoming_bandwidth_text->set_custom_minimum_size(Size2(120, 0) * EDSCALE);
359 incoming_bandwidth_text->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
360 incoming_bandwidth_text->set_accessibility_name(TTRC("Incoming Bandwidth"));
361 hb->add_child(incoming_bandwidth_text);
362
363 Control *down_up_spacer = memnew(Control);
364 down_up_spacer->set_custom_minimum_size(Size2(30, 0) * EDSCALE);
365 hb->add_child(down_up_spacer);
366
367 up_label = memnew(Label);
368 up_label->set_focus_mode(FOCUS_ACCESSIBILITY);
369 up_label->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
370 hb->add_child(up_label);
371
372 outgoing_bandwidth_text = memnew(LineEdit);
373 outgoing_bandwidth_text->set_editable(false);
374 outgoing_bandwidth_text->set_custom_minimum_size(Size2(120, 0) * EDSCALE);
375 outgoing_bandwidth_text->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
376 outgoing_bandwidth_text->set_accessibility_name(TTRC("Outgoing Bandwidth"));
377 hb->add_child(outgoing_bandwidth_text);
378
379 HSplitContainer *sc = memnew(HSplitContainer);
380 add_child(sc);
381 sc->set_v_size_flags(SIZE_EXPAND_FILL);
382 sc->set_h_size_flags(SIZE_EXPAND_FILL);
383 sc->set_split_offset(100 * EDSCALE);
384
385 // RPC
386 counters_display = memnew(Tree);
387 counters_display->set_custom_minimum_size(Size2(280, 0) * EDSCALE);
388 counters_display->set_v_size_flags(SIZE_EXPAND_FILL);
389 counters_display->set_h_size_flags(SIZE_EXPAND_FILL);
390 counters_display->set_hide_folding(true);
391 counters_display->set_hide_root(true);
392 counters_display->set_columns(3);
393 counters_display->set_column_titles_visible(true);
394 counters_display->set_column_title(0, TTRC("Node"));
395 counters_display->set_column_expand(0, true);
396 counters_display->set_column_clip_content(0, true);
397 counters_display->set_column_custom_minimum_width(0, 60 * EDSCALE);
398 counters_display->set_column_title(1, TTRC("Incoming RPC"));
399 counters_display->set_column_expand(1, false);
400 counters_display->set_column_clip_content(1, true);
401 counters_display->set_column_custom_minimum_width(1, 120 * EDSCALE);
402 counters_display->set_column_title(2, TTRC("Outgoing RPC"));
403 counters_display->set_column_expand(2, false);
404 counters_display->set_column_clip_content(2, true);
405 counters_display->set_theme_type_variation("TreeSecondary");
406 counters_display->set_column_custom_minimum_width(2, 120 * EDSCALE);
407 sc->add_child(counters_display);
408
409 // Replication
410 replication_display = memnew(Tree);
411 replication_display->set_custom_minimum_size(Size2(280, 0) * EDSCALE);
412 replication_display->set_v_size_flags(SIZE_EXPAND_FILL);
413 replication_display->set_h_size_flags(SIZE_EXPAND_FILL);
414 replication_display->set_hide_folding(true);
415 replication_display->set_hide_root(true);
416 replication_display->set_columns(5);
417 replication_display->set_column_titles_visible(true);
418 replication_display->set_column_title(0, TTRC("Root"));
419 replication_display->set_column_expand(0, true);
420 replication_display->set_column_clip_content(0, true);
421 replication_display->set_column_custom_minimum_width(0, 80 * EDSCALE);
422 replication_display->set_column_title(1, TTRC("Synchronizer"));
423 replication_display->set_column_expand(1, true);
424 replication_display->set_column_clip_content(1, true);
425 replication_display->set_column_custom_minimum_width(1, 80 * EDSCALE);
426 replication_display->set_column_title(2, TTRC("Config"));
427 replication_display->set_column_expand(2, true);
428 replication_display->set_column_clip_content(2, true);
429 replication_display->set_column_custom_minimum_width(2, 80 * EDSCALE);
430 replication_display->set_column_title(3, TTRC("Count"));
431 replication_display->set_column_expand(3, false);
432 replication_display->set_column_clip_content(3, true);
433 replication_display->set_column_custom_minimum_width(3, 80 * EDSCALE);
434 replication_display->set_column_title(4, TTRC("Size"));
435 replication_display->set_column_expand(4, false);
436 replication_display->set_column_clip_content(4, true);
437 replication_display->set_column_custom_minimum_width(4, 80 * EDSCALE);
438 replication_display->set_theme_type_variation("TreeSecondary");
439 replication_display->connect("button_clicked", callable_mp(this, &EditorNetworkProfiler::_replication_button_clicked));
440 sc->add_child(replication_display);
441
442 refresh_timer = memnew(Timer);
443 refresh_timer->set_wait_time(0.5);
444 refresh_timer->connect("timeout", callable_mp(this, &EditorNetworkProfiler::_refresh));
445 add_child(refresh_timer);
446 }