Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/multiplayer/editor/editor_network_profiler.cpp
20829 views
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 "core/io/resource_loader.h"
34
#include "editor/editor_string_names.h"
35
#include "editor/run/editor_run_bar.h"
36
#include "editor/settings/editor_settings.h"
37
#include "editor/themes/editor_scale.h"
38
#include "scene/gui/check_box.h"
39
#include "scene/gui/flow_container.h"
40
#include "scene/gui/line_edit.h"
41
#include "scene/main/timer.h"
42
43
void EditorNetworkProfiler::_bind_methods() {
44
ADD_SIGNAL(MethodInfo("enable_profiling", PropertyInfo(Variant::BOOL, "enable")));
45
ADD_SIGNAL(MethodInfo("open_request", PropertyInfo(Variant::STRING, "path")));
46
}
47
48
void EditorNetworkProfiler::_notification(int p_what) {
49
switch (p_what) {
50
case NOTIFICATION_TRANSLATION_CHANGED: {
51
// TRANSLATORS: This is the label for the network profiler's incoming bandwidth.
52
down_label->set_text(TTR("Down", "Network"));
53
// TRANSLATORS: This is the label for the network profiler's outgoing bandwidth.
54
up_label->set_text(TTR("Up", "Network"));
55
56
set_bandwidth(incoming_bandwidth, outgoing_bandwidth);
57
58
if (is_ready()) {
59
refresh_rpc_data();
60
}
61
} break;
62
63
case NOTIFICATION_THEME_CHANGED: {
64
if (activate->is_pressed()) {
65
activate->set_button_icon(theme_cache.stop_icon);
66
} else {
67
activate->set_button_icon(theme_cache.play_icon);
68
}
69
clear_button->set_button_icon(theme_cache.clear_icon);
70
71
incoming_bandwidth_text->set_right_icon(theme_cache.incoming_bandwidth_icon);
72
outgoing_bandwidth_text->set_right_icon(theme_cache.outgoing_bandwidth_icon);
73
74
// This needs to be done here to set the faded color when the profiler is first opened
75
incoming_bandwidth_text->add_theme_color_override("font_uneditable_color", theme_cache.incoming_bandwidth_color * Color(1, 1, 1, 0.5));
76
outgoing_bandwidth_text->add_theme_color_override("font_uneditable_color", theme_cache.outgoing_bandwidth_color * Color(1, 1, 1, 0.5));
77
} break;
78
}
79
}
80
81
void EditorNetworkProfiler::_update_theme_item_cache() {
82
VBoxContainer::_update_theme_item_cache();
83
84
theme_cache.node_icon = get_theme_icon(SNAME("Node"), EditorStringName(EditorIcons));
85
theme_cache.stop_icon = get_theme_icon(SNAME("Stop"), EditorStringName(EditorIcons));
86
theme_cache.play_icon = get_theme_icon(SNAME("Play"), EditorStringName(EditorIcons));
87
theme_cache.clear_icon = get_theme_icon(SNAME("Clear"), EditorStringName(EditorIcons));
88
89
theme_cache.multiplayer_synchronizer_icon = get_theme_icon("MultiplayerSynchronizer", EditorStringName(EditorIcons));
90
theme_cache.instance_options_icon = get_theme_icon(SNAME("InstanceOptions"), EditorStringName(EditorIcons));
91
92
theme_cache.incoming_bandwidth_icon = get_theme_icon(SNAME("ArrowDown"), EditorStringName(EditorIcons));
93
theme_cache.outgoing_bandwidth_icon = get_theme_icon(SNAME("ArrowUp"), EditorStringName(EditorIcons));
94
95
theme_cache.incoming_bandwidth_color = get_theme_color(SceneStringName(font_color), EditorStringName(Editor));
96
theme_cache.outgoing_bandwidth_color = get_theme_color(SceneStringName(font_color), EditorStringName(Editor));
97
}
98
99
void EditorNetworkProfiler::_refresh() {
100
if (!dirty) {
101
return;
102
}
103
dirty = false;
104
refresh_rpc_data();
105
refresh_replication_data();
106
}
107
108
void EditorNetworkProfiler::refresh_rpc_data() {
109
counters_display->clear();
110
111
TreeItem *root = counters_display->create_item();
112
int cols = counters_display->get_columns();
113
114
for (const KeyValue<ObjectID, RPCNodeInfo> &E : rpc_data) {
115
TreeItem *node = counters_display->create_item(root);
116
117
for (int j = 0; j < cols; ++j) {
118
node->set_text_alignment(j, j > 0 ? HORIZONTAL_ALIGNMENT_RIGHT : HORIZONTAL_ALIGNMENT_LEFT);
119
}
120
121
node->set_text(0, E.value.node_path);
122
node->set_text(1, E.value.incoming_rpc == 0 ? "-" : vformat(TTR("%d (%s)"), E.value.incoming_rpc, String::humanize_size(E.value.incoming_size)));
123
node->set_text(2, E.value.outgoing_rpc == 0 ? "-" : vformat(TTR("%d (%s)"), E.value.outgoing_rpc, String::humanize_size(E.value.outgoing_size)));
124
}
125
}
126
127
void EditorNetworkProfiler::refresh_replication_data() {
128
replication_display->clear();
129
130
TreeItem *root = replication_display->create_item();
131
132
for (const KeyValue<ObjectID, SyncInfo> &E : sync_data) {
133
// Ensure the nodes have at least a temporary cache.
134
ObjectID ids[3] = { E.value.synchronizer, E.value.config, E.value.root_node };
135
for (uint32_t i = 0; i < 3; i++) {
136
const ObjectID &id = ids[i];
137
if (!node_data.has(id)) {
138
missing_node_data.insert(id);
139
node_data[id] = NodeInfo(id);
140
}
141
}
142
143
TreeItem *node = replication_display->create_item(root);
144
145
const NodeInfo &root_info = node_data[E.value.root_node];
146
const NodeInfo &sync_info = node_data[E.value.synchronizer];
147
const NodeInfo &cfg_info = node_data[E.value.config];
148
149
node->set_text(0, root_info.path.get_file());
150
node->set_icon(0, has_theme_icon(root_info.type, EditorStringName(EditorIcons)) ? get_theme_icon(root_info.type, EditorStringName(EditorIcons)) : theme_cache.node_icon);
151
node->set_tooltip_text(0, root_info.path);
152
153
node->set_text(1, sync_info.path.get_file());
154
node->set_icon(1, theme_cache.multiplayer_synchronizer_icon);
155
node->set_tooltip_text(1, sync_info.path);
156
157
int cfg_idx = cfg_info.path.find("::");
158
if (cfg_info.path.begins_with("res://") && ResourceLoader::exists(cfg_info.path) && cfg_idx > 0) {
159
String res_idstr = cfg_info.path.substr(cfg_idx + 2).replace("SceneReplicationConfig_", "");
160
String scene_path = cfg_info.path.substr(0, cfg_idx);
161
node->set_text(2, vformat("%s (%s)", res_idstr, scene_path.get_file()));
162
node->add_button(2, theme_cache.instance_options_icon);
163
node->set_tooltip_text(2, cfg_info.path);
164
node->set_metadata(2, scene_path);
165
} else {
166
node->set_text(2, cfg_info.path);
167
node->set_metadata(2, "");
168
}
169
170
node->set_text(3, vformat("%d - %d", E.value.incoming_syncs, E.value.outgoing_syncs));
171
node->set_text(4, vformat("%d - %d", E.value.incoming_size, E.value.outgoing_size));
172
}
173
}
174
175
Array EditorNetworkProfiler::pop_missing_node_data() {
176
Array out;
177
for (const ObjectID &id : missing_node_data) {
178
out.push_back(id);
179
}
180
missing_node_data.clear();
181
return out;
182
}
183
184
void EditorNetworkProfiler::add_node_data(const NodeInfo &p_info) {
185
ERR_FAIL_COND(!node_data.has(p_info.id));
186
node_data[p_info.id] = p_info;
187
dirty = true;
188
}
189
190
void EditorNetworkProfiler::_activate_pressed() {
191
_update_button_text();
192
193
if (activate->is_pressed()) {
194
refresh_timer->start();
195
} else {
196
refresh_timer->stop();
197
}
198
199
emit_signal(SNAME("enable_profiling"), activate->is_pressed());
200
}
201
202
void EditorNetworkProfiler::_update_button_text() {
203
if (activate->is_pressed()) {
204
activate->set_button_icon(theme_cache.stop_icon);
205
activate->set_text(TTRC("Stop"));
206
} else {
207
activate->set_button_icon(theme_cache.play_icon);
208
activate->set_text(TTRC("Start"));
209
}
210
}
211
212
void EditorNetworkProfiler::started() {
213
_clear_pressed();
214
activate->set_disabled(false);
215
216
if (EditorSettings::get_singleton()->get_project_metadata("debug_options", "autostart_network_profiler", false)) {
217
set_profiling(true);
218
refresh_timer->start();
219
}
220
}
221
222
void EditorNetworkProfiler::stopped() {
223
activate->set_disabled(true);
224
set_profiling(false);
225
refresh_timer->stop();
226
}
227
228
void EditorNetworkProfiler::set_profiling(bool p_pressed) {
229
activate->set_pressed(p_pressed);
230
_update_button_text();
231
emit_signal(SNAME("enable_profiling"), activate->is_pressed());
232
}
233
234
void EditorNetworkProfiler::_clear_pressed() {
235
rpc_data.clear();
236
sync_data.clear();
237
node_data.clear();
238
missing_node_data.clear();
239
set_bandwidth(0, 0);
240
refresh_rpc_data();
241
refresh_replication_data();
242
clear_button->set_disabled(true);
243
}
244
245
void EditorNetworkProfiler::_autostart_toggled(bool p_toggled_on) {
246
EditorSettings::get_singleton()->set_project_metadata("debug_options", "autostart_network_profiler", p_toggled_on);
247
EditorRunBar::get_singleton()->update_profiler_autostart_indicator();
248
}
249
250
void EditorNetworkProfiler::_replication_button_clicked(TreeItem *p_item, int p_column, int p_idx, MouseButton p_button) {
251
if (!p_item) {
252
return;
253
}
254
String meta = p_item->get_metadata(p_column);
255
if (meta.size() && ResourceLoader::exists(meta)) {
256
emit_signal("open_request", meta);
257
}
258
}
259
260
void EditorNetworkProfiler::add_rpc_frame_data(const RPCNodeInfo &p_frame) {
261
if (clear_button->is_disabled()) {
262
clear_button->set_disabled(false);
263
}
264
dirty = true;
265
if (!rpc_data.has(p_frame.node)) {
266
rpc_data.insert(p_frame.node, p_frame);
267
} else {
268
rpc_data[p_frame.node].incoming_rpc += p_frame.incoming_rpc;
269
rpc_data[p_frame.node].outgoing_rpc += p_frame.outgoing_rpc;
270
}
271
if (p_frame.incoming_rpc) {
272
rpc_data[p_frame.node].incoming_size = p_frame.incoming_size / p_frame.incoming_rpc;
273
}
274
if (p_frame.outgoing_rpc) {
275
rpc_data[p_frame.node].outgoing_size = p_frame.outgoing_size / p_frame.outgoing_rpc;
276
}
277
}
278
279
void EditorNetworkProfiler::add_sync_frame_data(const SyncInfo &p_frame) {
280
if (clear_button->is_disabled()) {
281
clear_button->set_disabled(false);
282
}
283
dirty = true;
284
if (!sync_data.has(p_frame.synchronizer)) {
285
sync_data[p_frame.synchronizer] = p_frame;
286
} else {
287
sync_data[p_frame.synchronizer].incoming_syncs += p_frame.incoming_syncs;
288
sync_data[p_frame.synchronizer].outgoing_syncs += p_frame.outgoing_syncs;
289
}
290
SyncInfo &info = sync_data[p_frame.synchronizer];
291
if (p_frame.incoming_syncs) {
292
info.incoming_size = p_frame.incoming_size / p_frame.incoming_syncs;
293
}
294
if (p_frame.outgoing_syncs) {
295
info.outgoing_size = p_frame.outgoing_size / p_frame.outgoing_syncs;
296
}
297
}
298
299
void EditorNetworkProfiler::set_bandwidth(int p_incoming, int p_outgoing) {
300
incoming_bandwidth = p_incoming;
301
outgoing_bandwidth = p_outgoing;
302
303
incoming_bandwidth_text->set_text(vformat(TTR("%s/s"), String::humanize_size(p_incoming)));
304
outgoing_bandwidth_text->set_text(vformat(TTR("%s/s"), String::humanize_size(p_outgoing)));
305
306
// Make labels more prominent when the bandwidth is greater than 0 to attract user attention
307
incoming_bandwidth_text->add_theme_color_override(
308
"font_uneditable_color",
309
theme_cache.incoming_bandwidth_color * Color(1, 1, 1, p_incoming > 0 ? 1 : 0.5));
310
outgoing_bandwidth_text->add_theme_color_override(
311
"font_uneditable_color",
312
theme_cache.outgoing_bandwidth_color * Color(1, 1, 1, p_outgoing > 0 ? 1 : 0.5));
313
}
314
315
bool EditorNetworkProfiler::is_profiling() {
316
return activate->is_pressed();
317
}
318
319
EditorNetworkProfiler::EditorNetworkProfiler() {
320
FlowContainer *container = memnew(FlowContainer);
321
container->add_theme_constant_override(SNAME("h_separation"), 8 * EDSCALE);
322
container->add_theme_constant_override(SNAME("v_separation"), 2 * EDSCALE);
323
add_child(container);
324
325
activate = memnew(Button);
326
activate->set_toggle_mode(true);
327
activate->set_text(TTRC("Start"));
328
activate->set_disabled(true);
329
activate->connect(SceneStringName(pressed), callable_mp(this, &EditorNetworkProfiler::_activate_pressed));
330
container->add_child(activate);
331
332
clear_button = memnew(Button);
333
clear_button->set_text(TTRC("Clear"));
334
clear_button->set_disabled(true);
335
clear_button->connect(SceneStringName(pressed), callable_mp(this, &EditorNetworkProfiler::_clear_pressed));
336
container->add_child(clear_button);
337
338
CheckBox *autostart_checkbox = memnew(CheckBox);
339
autostart_checkbox->set_text(TTRC("Autostart"));
340
autostart_checkbox->set_pressed(EditorSettings::get_singleton()->get_project_metadata("debug_options", "autostart_network_profiler", false));
341
autostart_checkbox->connect(SceneStringName(toggled), callable_mp(this, &EditorNetworkProfiler::_autostart_toggled));
342
container->add_child(autostart_checkbox);
343
344
Control *c = memnew(Control);
345
c->set_h_size_flags(SIZE_EXPAND_FILL);
346
container->add_child(c);
347
348
HBoxContainer *hb = memnew(HBoxContainer);
349
hb->add_theme_constant_override(SNAME("separation"), 8 * EDSCALE);
350
container->add_child(hb);
351
352
down_label = memnew(Label);
353
down_label->set_focus_mode(FOCUS_ACCESSIBILITY);
354
down_label->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
355
hb->add_child(down_label);
356
357
incoming_bandwidth_text = memnew(LineEdit);
358
incoming_bandwidth_text->set_editable(false);
359
incoming_bandwidth_text->set_custom_minimum_size(Size2(120, 0) * EDSCALE);
360
incoming_bandwidth_text->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
361
incoming_bandwidth_text->set_accessibility_name(TTRC("Incoming Bandwidth"));
362
hb->add_child(incoming_bandwidth_text);
363
364
Control *down_up_spacer = memnew(Control);
365
down_up_spacer->set_custom_minimum_size(Size2(30, 0) * EDSCALE);
366
hb->add_child(down_up_spacer);
367
368
up_label = memnew(Label);
369
up_label->set_focus_mode(FOCUS_ACCESSIBILITY);
370
up_label->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
371
hb->add_child(up_label);
372
373
outgoing_bandwidth_text = memnew(LineEdit);
374
outgoing_bandwidth_text->set_editable(false);
375
outgoing_bandwidth_text->set_custom_minimum_size(Size2(120, 0) * EDSCALE);
376
outgoing_bandwidth_text->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
377
outgoing_bandwidth_text->set_accessibility_name(TTRC("Outgoing Bandwidth"));
378
hb->add_child(outgoing_bandwidth_text);
379
380
HSplitContainer *sc = memnew(HSplitContainer);
381
add_child(sc);
382
sc->set_v_size_flags(SIZE_EXPAND_FILL);
383
sc->set_h_size_flags(SIZE_EXPAND_FILL);
384
sc->set_split_offset(100 * EDSCALE);
385
386
// RPC
387
counters_display = memnew(Tree);
388
counters_display->set_custom_minimum_size(Size2(280, 0) * EDSCALE);
389
counters_display->set_v_size_flags(SIZE_EXPAND_FILL);
390
counters_display->set_h_size_flags(SIZE_EXPAND_FILL);
391
counters_display->set_hide_folding(true);
392
counters_display->set_hide_root(true);
393
counters_display->set_columns(3);
394
counters_display->set_column_titles_visible(true);
395
counters_display->set_column_title(0, TTRC("Node"));
396
counters_display->set_column_expand(0, true);
397
counters_display->set_column_clip_content(0, true);
398
counters_display->set_column_custom_minimum_width(0, 60 * EDSCALE);
399
counters_display->set_column_title(1, TTRC("Incoming RPC"));
400
counters_display->set_column_expand(1, false);
401
counters_display->set_column_clip_content(1, true);
402
counters_display->set_column_custom_minimum_width(1, 120 * EDSCALE);
403
counters_display->set_column_title(2, TTRC("Outgoing RPC"));
404
counters_display->set_column_expand(2, false);
405
counters_display->set_column_clip_content(2, true);
406
counters_display->set_theme_type_variation("TreeSecondary");
407
counters_display->set_column_custom_minimum_width(2, 120 * EDSCALE);
408
sc->add_child(counters_display);
409
410
// Replication
411
replication_display = memnew(Tree);
412
replication_display->set_custom_minimum_size(Size2(280, 0) * EDSCALE);
413
replication_display->set_v_size_flags(SIZE_EXPAND_FILL);
414
replication_display->set_h_size_flags(SIZE_EXPAND_FILL);
415
replication_display->set_hide_folding(true);
416
replication_display->set_hide_root(true);
417
replication_display->set_columns(5);
418
replication_display->set_column_titles_visible(true);
419
replication_display->set_column_title(0, TTRC("Root"));
420
replication_display->set_column_expand(0, true);
421
replication_display->set_column_clip_content(0, true);
422
replication_display->set_column_custom_minimum_width(0, 80 * EDSCALE);
423
replication_display->set_column_title(1, TTRC("Synchronizer"));
424
replication_display->set_column_expand(1, true);
425
replication_display->set_column_clip_content(1, true);
426
replication_display->set_column_custom_minimum_width(1, 80 * EDSCALE);
427
replication_display->set_column_title(2, TTRC("Config"));
428
replication_display->set_column_expand(2, true);
429
replication_display->set_column_clip_content(2, true);
430
replication_display->set_column_custom_minimum_width(2, 80 * EDSCALE);
431
replication_display->set_column_title(3, TTRC("Count"));
432
replication_display->set_column_expand(3, false);
433
replication_display->set_column_clip_content(3, true);
434
replication_display->set_column_custom_minimum_width(3, 80 * EDSCALE);
435
replication_display->set_column_title(4, TTRC("Size"));
436
replication_display->set_column_expand(4, false);
437
replication_display->set_column_clip_content(4, true);
438
replication_display->set_column_custom_minimum_width(4, 80 * EDSCALE);
439
replication_display->set_theme_type_variation("TreeSecondary");
440
replication_display->connect("button_clicked", callable_mp(this, &EditorNetworkProfiler::_replication_button_clicked));
441
sc->add_child(replication_display);
442
443
refresh_timer = memnew(Timer);
444
refresh_timer->set_wait_time(0.5);
445
refresh_timer->connect("timeout", callable_mp(this, &EditorNetworkProfiler::_refresh));
446
add_child(refresh_timer);
447
}
448
449