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