Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/multiplayer/multiplayer_debugger.cpp
10277 views
1
/**************************************************************************/
2
/* multiplayer_debugger.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 "multiplayer_debugger.h"
32
33
#include "multiplayer_synchronizer.h"
34
#include "scene_replication_config.h"
35
36
#include "core/debugger/engine_debugger.h"
37
#include "scene/main/node.h"
38
39
List<Ref<EngineProfiler>> multiplayer_profilers;
40
41
void MultiplayerDebugger::initialize() {
42
Ref<BandwidthProfiler> bandwidth;
43
bandwidth.instantiate();
44
bandwidth->bind("multiplayer:bandwidth");
45
multiplayer_profilers.push_back(bandwidth);
46
47
Ref<RPCProfiler> rpc_profiler;
48
rpc_profiler.instantiate();
49
rpc_profiler->bind("multiplayer:rpc");
50
multiplayer_profilers.push_back(rpc_profiler);
51
52
Ref<ReplicationProfiler> replication_profiler;
53
replication_profiler.instantiate();
54
replication_profiler->bind("multiplayer:replication");
55
multiplayer_profilers.push_back(replication_profiler);
56
57
EngineDebugger::register_message_capture("multiplayer", EngineDebugger::Capture(nullptr, &_capture));
58
}
59
60
void MultiplayerDebugger::deinitialize() {
61
multiplayer_profilers.clear();
62
}
63
64
Error MultiplayerDebugger::_capture(void *p_user, const String &p_msg, const Array &p_args, bool &r_captured) {
65
if (p_msg == "cache") {
66
Array out;
67
for (int i = 0; i < p_args.size(); i++) {
68
ObjectID id = p_args[i].operator ObjectID();
69
Object *obj = ObjectDB::get_instance(id);
70
ERR_CONTINUE(!obj);
71
if (Object::cast_to<SceneReplicationConfig>(obj)) {
72
out.push_back(id);
73
out.push_back(obj->get_class());
74
out.push_back(((SceneReplicationConfig *)obj)->get_path());
75
} else if (Object::cast_to<Node>(obj)) {
76
out.push_back(id);
77
out.push_back(obj->get_class());
78
out.push_back(String(((Node *)obj)->get_path()));
79
} else {
80
ERR_FAIL_V(FAILED);
81
}
82
}
83
EngineDebugger::get_singleton()->send_message("multiplayer:cache", out);
84
return OK;
85
}
86
ERR_FAIL_V(FAILED);
87
}
88
89
// BandwidthProfiler
90
91
int MultiplayerDebugger::BandwidthProfiler::bandwidth_usage(const Vector<BandwidthFrame> &p_buffer, int p_pointer) {
92
ERR_FAIL_COND_V(p_buffer.is_empty(), 0);
93
int total_bandwidth = 0;
94
95
uint64_t timestamp = OS::get_singleton()->get_ticks_msec();
96
uint64_t final_timestamp = timestamp - 1000;
97
98
int i = (p_pointer + p_buffer.size() - 1) % p_buffer.size();
99
100
while (i != p_pointer && p_buffer[i].packet_size > 0) {
101
if (p_buffer[i].timestamp < final_timestamp) {
102
return total_bandwidth;
103
}
104
total_bandwidth += p_buffer[i].packet_size;
105
i = (i + p_buffer.size() - 1) % p_buffer.size();
106
}
107
108
ERR_FAIL_COND_V_MSG(i == p_pointer, total_bandwidth, "Reached the end of the bandwidth profiler buffer, values might be inaccurate.");
109
return total_bandwidth;
110
}
111
112
void MultiplayerDebugger::BandwidthProfiler::toggle(bool p_enable, const Array &p_opts) {
113
if (!p_enable) {
114
bandwidth_in.clear();
115
bandwidth_out.clear();
116
} else {
117
bandwidth_in_ptr = 0;
118
bandwidth_in.resize(16384); // ~128kB
119
for (int i = 0; i < bandwidth_in.size(); ++i) {
120
bandwidth_in.write[i].packet_size = -1;
121
}
122
bandwidth_out_ptr = 0;
123
bandwidth_out.resize(16384); // ~128kB
124
for (int i = 0; i < bandwidth_out.size(); ++i) {
125
bandwidth_out.write[i].packet_size = -1;
126
}
127
}
128
}
129
130
void MultiplayerDebugger::BandwidthProfiler::add(const Array &p_data) {
131
ERR_FAIL_COND(p_data.size() < 3);
132
const String inout = p_data[0];
133
int time = p_data[1];
134
int size = p_data[2];
135
if (inout == "in") {
136
bandwidth_in.write[bandwidth_in_ptr].timestamp = time;
137
bandwidth_in.write[bandwidth_in_ptr].packet_size = size;
138
bandwidth_in_ptr = (bandwidth_in_ptr + 1) % bandwidth_in.size();
139
} else if (inout == "out") {
140
bandwidth_out.write[bandwidth_out_ptr].timestamp = time;
141
bandwidth_out.write[bandwidth_out_ptr].packet_size = size;
142
bandwidth_out_ptr = (bandwidth_out_ptr + 1) % bandwidth_out.size();
143
}
144
}
145
146
void MultiplayerDebugger::BandwidthProfiler::tick(double p_frame_time, double p_process_time, double p_physics_time, double p_physics_frame_time) {
147
uint64_t pt = OS::get_singleton()->get_ticks_msec();
148
if (pt - last_bandwidth_time > 200) {
149
last_bandwidth_time = pt;
150
int incoming_bandwidth = bandwidth_usage(bandwidth_in, bandwidth_in_ptr);
151
int outgoing_bandwidth = bandwidth_usage(bandwidth_out, bandwidth_out_ptr);
152
153
Array arr = { incoming_bandwidth, outgoing_bandwidth };
154
EngineDebugger::get_singleton()->send_message("multiplayer:bandwidth", arr);
155
}
156
}
157
158
// RPCProfiler
159
160
Array MultiplayerDebugger::RPCFrame::serialize() {
161
Array arr = { infos.size() * 6 };
162
for (int i = 0; i < infos.size(); ++i) {
163
arr.push_back(uint64_t(infos[i].node));
164
arr.push_back(infos[i].node_path);
165
arr.push_back(infos[i].incoming_rpc);
166
arr.push_back(infos[i].incoming_size);
167
arr.push_back(infos[i].outgoing_rpc);
168
arr.push_back(infos[i].outgoing_size);
169
}
170
return arr;
171
}
172
173
bool MultiplayerDebugger::RPCFrame::deserialize(const Array &p_arr) {
174
ERR_FAIL_COND_V(p_arr.is_empty(), false);
175
uint32_t size = p_arr[0];
176
ERR_FAIL_COND_V(size % 6, false);
177
ERR_FAIL_COND_V((uint32_t)p_arr.size() != size + 1, false);
178
infos.resize(size / 6);
179
int idx = 1;
180
for (uint32_t i = 0; i < size / 6; i++) {
181
infos.write[i].node = uint64_t(p_arr[idx]);
182
infos.write[i].node_path = p_arr[idx + 1];
183
infos.write[i].incoming_rpc = p_arr[idx + 2];
184
infos.write[i].incoming_size = p_arr[idx + 3];
185
infos.write[i].outgoing_rpc = p_arr[idx + 4];
186
infos.write[i].outgoing_size = p_arr[idx + 5];
187
idx += 6;
188
}
189
return true;
190
}
191
192
void MultiplayerDebugger::RPCProfiler::init_node(const ObjectID p_node) {
193
if (rpc_node_data.has(p_node)) {
194
return;
195
}
196
rpc_node_data.insert(p_node, RPCNodeInfo());
197
rpc_node_data[p_node].node = p_node;
198
rpc_node_data[p_node].node_path = String(ObjectDB::get_instance<Node>(p_node)->get_path());
199
}
200
201
void MultiplayerDebugger::RPCProfiler::toggle(bool p_enable, const Array &p_opts) {
202
rpc_node_data.clear();
203
}
204
205
void MultiplayerDebugger::RPCProfiler::add(const Array &p_data) {
206
ERR_FAIL_COND(p_data.size() != 3);
207
const String what = p_data[0];
208
const ObjectID id = p_data[1];
209
const int size = p_data[2];
210
init_node(id);
211
RPCNodeInfo &info = rpc_node_data[id];
212
if (what == "rpc_in") {
213
info.incoming_rpc++;
214
info.incoming_size += size;
215
} else if (what == "rpc_out") {
216
info.outgoing_rpc++;
217
info.outgoing_size += size;
218
}
219
}
220
221
void MultiplayerDebugger::RPCProfiler::tick(double p_frame_time, double p_process_time, double p_physics_time, double p_physics_frame_time) {
222
uint64_t pt = OS::get_singleton()->get_ticks_msec();
223
if (pt - last_profile_time > 100) {
224
last_profile_time = pt;
225
RPCFrame frame;
226
for (const KeyValue<ObjectID, RPCNodeInfo> &E : rpc_node_data) {
227
frame.infos.push_back(E.value);
228
}
229
rpc_node_data.clear();
230
EngineDebugger::get_singleton()->send_message("multiplayer:rpc", frame.serialize());
231
}
232
}
233
234
// ReplicationProfiler
235
236
MultiplayerDebugger::SyncInfo::SyncInfo(MultiplayerSynchronizer *p_sync) {
237
ERR_FAIL_NULL(p_sync);
238
synchronizer = p_sync->get_instance_id();
239
if (p_sync->get_replication_config_ptr()) {
240
config = p_sync->get_replication_config_ptr()->get_instance_id();
241
}
242
if (p_sync->get_root_node()) {
243
root_node = p_sync->get_root_node()->get_instance_id();
244
}
245
}
246
247
void MultiplayerDebugger::SyncInfo::write_to_array(Array &r_arr) const {
248
r_arr.push_back(synchronizer);
249
r_arr.push_back(config);
250
r_arr.push_back(root_node);
251
r_arr.push_back(incoming_syncs);
252
r_arr.push_back(incoming_size);
253
r_arr.push_back(outgoing_syncs);
254
r_arr.push_back(outgoing_size);
255
}
256
257
bool MultiplayerDebugger::SyncInfo::read_from_array(const Array &p_arr, int p_offset) {
258
ERR_FAIL_COND_V(p_arr.size() - p_offset < 7, false);
259
synchronizer = int64_t(p_arr[p_offset]);
260
config = int64_t(p_arr[p_offset + 1]);
261
root_node = int64_t(p_arr[p_offset + 2]);
262
incoming_syncs = p_arr[p_offset + 3];
263
incoming_size = p_arr[p_offset + 4];
264
outgoing_syncs = p_arr[p_offset + 5];
265
outgoing_size = p_arr[p_offset + 6];
266
return true;
267
}
268
269
Array MultiplayerDebugger::ReplicationFrame::serialize() {
270
Array arr = { infos.size() * 7 };
271
for (const KeyValue<ObjectID, SyncInfo> &E : infos) {
272
E.value.write_to_array(arr);
273
}
274
return arr;
275
}
276
277
bool MultiplayerDebugger::ReplicationFrame::deserialize(const Array &p_arr) {
278
ERR_FAIL_COND_V(p_arr.is_empty(), false);
279
uint32_t size = p_arr[0];
280
ERR_FAIL_COND_V(size % 7, false);
281
ERR_FAIL_COND_V((uint32_t)p_arr.size() != size + 1, false);
282
int idx = 1;
283
for (uint32_t i = 0; i < size / 7; i++) {
284
SyncInfo info;
285
if (!info.read_from_array(p_arr, idx)) {
286
return false;
287
}
288
infos[info.synchronizer] = info;
289
idx += 7;
290
}
291
return true;
292
}
293
294
void MultiplayerDebugger::ReplicationProfiler::toggle(bool p_enable, const Array &p_opts) {
295
sync_data.clear();
296
}
297
298
void MultiplayerDebugger::ReplicationProfiler::add(const Array &p_data) {
299
ERR_FAIL_COND(p_data.size() != 3);
300
const String what = p_data[0];
301
const ObjectID id = p_data[1];
302
const uint64_t size = p_data[2];
303
MultiplayerSynchronizer *sync = ObjectDB::get_instance<MultiplayerSynchronizer>(id);
304
ERR_FAIL_NULL(sync);
305
if (!sync_data.has(id)) {
306
sync_data[id] = SyncInfo(sync);
307
}
308
SyncInfo &info = sync_data[id];
309
if (what == "sync_in") {
310
info.incoming_syncs++;
311
info.incoming_size += size;
312
} else if (what == "sync_out") {
313
info.outgoing_syncs++;
314
info.outgoing_size += size;
315
}
316
}
317
318
void MultiplayerDebugger::ReplicationProfiler::tick(double p_frame_time, double p_process_time, double p_physics_time, double p_physics_frame_time) {
319
uint64_t pt = OS::get_singleton()->get_ticks_msec();
320
if (pt - last_profile_time > 100) {
321
last_profile_time = pt;
322
ReplicationFrame frame;
323
for (const KeyValue<ObjectID, SyncInfo> &E : sync_data) {
324
frame.infos[E.key] = E.value;
325
}
326
sync_data.clear();
327
EngineDebugger::get_singleton()->send_message("multiplayer:syncs", frame.serialize());
328
}
329
}
330
331