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