multiplayer_debugger.cpp 12 KB

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