editor_network_profiler.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*************************************************************************/
  2. /* editor_network_profiler.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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 "editor_network_profiler.h"
  31. #include "core/os/os.h"
  32. #include "editor/editor_scale.h"
  33. #include "editor/editor_settings.h"
  34. void EditorNetworkProfiler::_bind_methods() {
  35. ADD_SIGNAL(MethodInfo("enable_profiling", PropertyInfo(Variant::BOOL, "enable")));
  36. }
  37. void EditorNetworkProfiler::_notification(int p_what) {
  38. switch (p_what) {
  39. case NOTIFICATION_ENTER_TREE:
  40. case NOTIFICATION_THEME_CHANGED: {
  41. activate->set_icon(get_theme_icon(SNAME("Play"), SNAME("EditorIcons")));
  42. clear_button->set_icon(get_theme_icon(SNAME("Clear"), SNAME("EditorIcons")));
  43. incoming_bandwidth_text->set_right_icon(get_theme_icon(SNAME("ArrowDown"), SNAME("EditorIcons")));
  44. outgoing_bandwidth_text->set_right_icon(get_theme_icon(SNAME("ArrowUp"), SNAME("EditorIcons")));
  45. // This needs to be done here to set the faded color when the profiler is first opened
  46. incoming_bandwidth_text->add_theme_color_override("font_uneditable_color", get_theme_color(SNAME("font_color"), SNAME("Editor")) * Color(1, 1, 1, 0.5));
  47. outgoing_bandwidth_text->add_theme_color_override("font_uneditable_color", get_theme_color(SNAME("font_color"), SNAME("Editor")) * Color(1, 1, 1, 0.5));
  48. } break;
  49. }
  50. }
  51. void EditorNetworkProfiler::_update_frame() {
  52. counters_display->clear();
  53. TreeItem *root = counters_display->create_item();
  54. for (const KeyValue<ObjectID, SceneDebugger::RPCNodeInfo> &E : nodes_data) {
  55. TreeItem *node = counters_display->create_item(root);
  56. for (int j = 0; j < counters_display->get_columns(); ++j) {
  57. node->set_text_alignment(j, j > 0 ? HORIZONTAL_ALIGNMENT_RIGHT : HORIZONTAL_ALIGNMENT_LEFT);
  58. }
  59. node->set_text(0, E.value.node_path);
  60. node->set_text(1, E.value.incoming_rpc == 0 ? "-" : itos(E.value.incoming_rpc));
  61. node->set_text(2, E.value.outgoing_rpc == 0 ? "-" : itos(E.value.outgoing_rpc));
  62. }
  63. }
  64. void EditorNetworkProfiler::_activate_pressed() {
  65. if (activate->is_pressed()) {
  66. activate->set_icon(get_theme_icon(SNAME("Stop"), SNAME("EditorIcons")));
  67. activate->set_text(TTR("Stop"));
  68. } else {
  69. activate->set_icon(get_theme_icon(SNAME("Play"), SNAME("EditorIcons")));
  70. activate->set_text(TTR("Start"));
  71. }
  72. emit_signal(SNAME("enable_profiling"), activate->is_pressed());
  73. }
  74. void EditorNetworkProfiler::_clear_pressed() {
  75. nodes_data.clear();
  76. set_bandwidth(0, 0);
  77. if (frame_delay->is_stopped()) {
  78. frame_delay->set_wait_time(0.1);
  79. frame_delay->start();
  80. }
  81. }
  82. void EditorNetworkProfiler::add_node_frame_data(const SceneDebugger::RPCNodeInfo p_frame) {
  83. if (!nodes_data.has(p_frame.node)) {
  84. nodes_data.insert(p_frame.node, p_frame);
  85. } else {
  86. nodes_data[p_frame.node].incoming_rpc += p_frame.incoming_rpc;
  87. nodes_data[p_frame.node].outgoing_rpc += p_frame.outgoing_rpc;
  88. }
  89. if (frame_delay->is_stopped()) {
  90. frame_delay->set_wait_time(0.1);
  91. frame_delay->start();
  92. }
  93. }
  94. void EditorNetworkProfiler::set_bandwidth(int p_incoming, int p_outgoing) {
  95. incoming_bandwidth_text->set_text(vformat(TTR("%s/s"), String::humanize_size(p_incoming)));
  96. outgoing_bandwidth_text->set_text(vformat(TTR("%s/s"), String::humanize_size(p_outgoing)));
  97. // Make labels more prominent when the bandwidth is greater than 0 to attract user attention
  98. incoming_bandwidth_text->add_theme_color_override(
  99. "font_uneditable_color",
  100. get_theme_color(SNAME("font_color"), SNAME("Editor")) * Color(1, 1, 1, p_incoming > 0 ? 1 : 0.5));
  101. outgoing_bandwidth_text->add_theme_color_override(
  102. "font_uneditable_color",
  103. get_theme_color(SNAME("font_color"), SNAME("Editor")) * Color(1, 1, 1, p_outgoing > 0 ? 1 : 0.5));
  104. }
  105. bool EditorNetworkProfiler::is_profiling() {
  106. return activate->is_pressed();
  107. }
  108. EditorNetworkProfiler::EditorNetworkProfiler() {
  109. HBoxContainer *hb = memnew(HBoxContainer);
  110. hb->add_theme_constant_override("separation", 8 * EDSCALE);
  111. add_child(hb);
  112. activate = memnew(Button);
  113. activate->set_toggle_mode(true);
  114. activate->set_text(TTR("Start"));
  115. activate->connect("pressed", callable_mp(this, &EditorNetworkProfiler::_activate_pressed));
  116. hb->add_child(activate);
  117. clear_button = memnew(Button);
  118. clear_button->set_text(TTR("Clear"));
  119. clear_button->connect("pressed", callable_mp(this, &EditorNetworkProfiler::_clear_pressed));
  120. hb->add_child(clear_button);
  121. hb->add_spacer();
  122. Label *lb = memnew(Label);
  123. lb->set_text(TTR("Down"));
  124. hb->add_child(lb);
  125. incoming_bandwidth_text = memnew(LineEdit);
  126. incoming_bandwidth_text->set_editable(false);
  127. incoming_bandwidth_text->set_custom_minimum_size(Size2(120, 0) * EDSCALE);
  128. incoming_bandwidth_text->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
  129. hb->add_child(incoming_bandwidth_text);
  130. Control *down_up_spacer = memnew(Control);
  131. down_up_spacer->set_custom_minimum_size(Size2(30, 0) * EDSCALE);
  132. hb->add_child(down_up_spacer);
  133. lb = memnew(Label);
  134. lb->set_text(TTR("Up"));
  135. hb->add_child(lb);
  136. outgoing_bandwidth_text = memnew(LineEdit);
  137. outgoing_bandwidth_text->set_editable(false);
  138. outgoing_bandwidth_text->set_custom_minimum_size(Size2(120, 0) * EDSCALE);
  139. outgoing_bandwidth_text->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
  140. hb->add_child(outgoing_bandwidth_text);
  141. // Set initial texts in the incoming/outgoing bandwidth labels
  142. set_bandwidth(0, 0);
  143. counters_display = memnew(Tree);
  144. counters_display->set_custom_minimum_size(Size2(300, 0) * EDSCALE);
  145. counters_display->set_v_size_flags(SIZE_EXPAND_FILL);
  146. counters_display->set_hide_folding(true);
  147. counters_display->set_hide_root(true);
  148. counters_display->set_columns(3);
  149. counters_display->set_column_titles_visible(true);
  150. counters_display->set_column_title(0, TTR("Node"));
  151. counters_display->set_column_expand(0, true);
  152. counters_display->set_column_clip_content(0, true);
  153. counters_display->set_column_custom_minimum_width(0, 60 * EDSCALE);
  154. counters_display->set_column_title(1, TTR("Incoming RPC"));
  155. counters_display->set_column_expand(1, false);
  156. counters_display->set_column_clip_content(1, true);
  157. counters_display->set_column_custom_minimum_width(1, 120 * EDSCALE);
  158. counters_display->set_column_title(2, TTR("Outgoing RPC"));
  159. counters_display->set_column_expand(2, false);
  160. counters_display->set_column_clip_content(2, true);
  161. counters_display->set_column_custom_minimum_width(2, 120 * EDSCALE);
  162. add_child(counters_display);
  163. frame_delay = memnew(Timer);
  164. frame_delay->set_wait_time(0.1);
  165. frame_delay->set_one_shot(true);
  166. add_child(frame_delay);
  167. frame_delay->connect("timeout", callable_mp(this, &EditorNetworkProfiler::_update_frame));
  168. }