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