performance.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*************************************************************************/
  2. /* performance.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "performance.h"
  31. #include "core/message_queue.h"
  32. #include "core/os/os.h"
  33. #include "scene/main/node.h"
  34. #include "scene/main/scene_tree.h"
  35. #include "servers/audio_server.h"
  36. #include "servers/physics_server_2d.h"
  37. #include "servers/physics_server_3d.h"
  38. #include "servers/rendering_server.h"
  39. Performance *Performance::singleton = NULL;
  40. void Performance::_bind_methods() {
  41. ClassDB::bind_method(D_METHOD("get_monitor", "monitor"), &Performance::get_monitor);
  42. BIND_ENUM_CONSTANT(TIME_FPS);
  43. BIND_ENUM_CONSTANT(TIME_PROCESS);
  44. BIND_ENUM_CONSTANT(TIME_PHYSICS_PROCESS);
  45. BIND_ENUM_CONSTANT(MEMORY_STATIC);
  46. BIND_ENUM_CONSTANT(MEMORY_STATIC_MAX);
  47. BIND_ENUM_CONSTANT(MEMORY_MESSAGE_BUFFER_MAX);
  48. BIND_ENUM_CONSTANT(OBJECT_COUNT);
  49. BIND_ENUM_CONSTANT(OBJECT_RESOURCE_COUNT);
  50. BIND_ENUM_CONSTANT(OBJECT_NODE_COUNT);
  51. BIND_ENUM_CONSTANT(OBJECT_ORPHAN_NODE_COUNT);
  52. BIND_ENUM_CONSTANT(RENDER_OBJECTS_IN_FRAME);
  53. BIND_ENUM_CONSTANT(RENDER_VERTICES_IN_FRAME);
  54. BIND_ENUM_CONSTANT(RENDER_MATERIAL_CHANGES_IN_FRAME);
  55. BIND_ENUM_CONSTANT(RENDER_SHADER_CHANGES_IN_FRAME);
  56. BIND_ENUM_CONSTANT(RENDER_SURFACE_CHANGES_IN_FRAME);
  57. BIND_ENUM_CONSTANT(RENDER_DRAW_CALLS_IN_FRAME);
  58. BIND_ENUM_CONSTANT(RENDER_VIDEO_MEM_USED);
  59. BIND_ENUM_CONSTANT(RENDER_TEXTURE_MEM_USED);
  60. BIND_ENUM_CONSTANT(RENDER_VERTEX_MEM_USED);
  61. BIND_ENUM_CONSTANT(RENDER_USAGE_VIDEO_MEM_TOTAL);
  62. BIND_ENUM_CONSTANT(PHYSICS_2D_ACTIVE_OBJECTS);
  63. BIND_ENUM_CONSTANT(PHYSICS_2D_COLLISION_PAIRS);
  64. BIND_ENUM_CONSTANT(PHYSICS_2D_ISLAND_COUNT);
  65. BIND_ENUM_CONSTANT(PHYSICS_3D_ACTIVE_OBJECTS);
  66. BIND_ENUM_CONSTANT(PHYSICS_3D_COLLISION_PAIRS);
  67. BIND_ENUM_CONSTANT(PHYSICS_3D_ISLAND_COUNT);
  68. BIND_ENUM_CONSTANT(AUDIO_OUTPUT_LATENCY);
  69. BIND_ENUM_CONSTANT(MONITOR_MAX);
  70. }
  71. float Performance::_get_node_count() const {
  72. MainLoop *ml = OS::get_singleton()->get_main_loop();
  73. SceneTree *sml = Object::cast_to<SceneTree>(ml);
  74. if (!sml)
  75. return 0;
  76. return sml->get_node_count();
  77. }
  78. String Performance::get_monitor_name(Monitor p_monitor) const {
  79. ERR_FAIL_INDEX_V(p_monitor, MONITOR_MAX, String());
  80. static const char *names[MONITOR_MAX] = {
  81. "time/fps",
  82. "time/process",
  83. "time/physics_process",
  84. "memory/static",
  85. "memory/static_max",
  86. "memory/msg_buf_max",
  87. "object/objects",
  88. "object/resources",
  89. "object/nodes",
  90. "object/orphan_nodes",
  91. "raster/objects_drawn",
  92. "raster/vertices_drawn",
  93. "raster/mat_changes",
  94. "raster/shader_changes",
  95. "raster/surface_changes",
  96. "raster/draw_calls",
  97. "video/video_mem",
  98. "video/texture_mem",
  99. "video/vertex_mem",
  100. "video/video_mem_max",
  101. "physics_2d/active_objects",
  102. "physics_2d/collision_pairs",
  103. "physics_2d/islands",
  104. "physics_3d/active_objects",
  105. "physics_3d/collision_pairs",
  106. "physics_3d/islands",
  107. "audio/output_latency",
  108. };
  109. return names[p_monitor];
  110. }
  111. float Performance::get_monitor(Monitor p_monitor) const {
  112. switch (p_monitor) {
  113. case TIME_FPS: return Engine::get_singleton()->get_frames_per_second();
  114. case TIME_PROCESS: return _process_time;
  115. case TIME_PHYSICS_PROCESS: return _physics_process_time;
  116. case MEMORY_STATIC: return Memory::get_mem_usage();
  117. case MEMORY_STATIC_MAX: return Memory::get_mem_max_usage();
  118. case MEMORY_MESSAGE_BUFFER_MAX: return MessageQueue::get_singleton()->get_max_buffer_usage();
  119. case OBJECT_COUNT: return ObjectDB::get_object_count();
  120. case OBJECT_RESOURCE_COUNT: return ResourceCache::get_cached_resource_count();
  121. case OBJECT_NODE_COUNT: return _get_node_count();
  122. case OBJECT_ORPHAN_NODE_COUNT: return Node::orphan_node_count;
  123. case RENDER_OBJECTS_IN_FRAME: return RS::get_singleton()->get_render_info(RS::INFO_OBJECTS_IN_FRAME);
  124. case RENDER_VERTICES_IN_FRAME: return RS::get_singleton()->get_render_info(RS::INFO_VERTICES_IN_FRAME);
  125. case RENDER_MATERIAL_CHANGES_IN_FRAME: return RS::get_singleton()->get_render_info(RS::INFO_MATERIAL_CHANGES_IN_FRAME);
  126. case RENDER_SHADER_CHANGES_IN_FRAME: return RS::get_singleton()->get_render_info(RS::INFO_SHADER_CHANGES_IN_FRAME);
  127. case RENDER_SURFACE_CHANGES_IN_FRAME: return RS::get_singleton()->get_render_info(RS::INFO_SURFACE_CHANGES_IN_FRAME);
  128. case RENDER_DRAW_CALLS_IN_FRAME: return RS::get_singleton()->get_render_info(RS::INFO_DRAW_CALLS_IN_FRAME);
  129. case RENDER_VIDEO_MEM_USED: return RS::get_singleton()->get_render_info(RS::INFO_VIDEO_MEM_USED);
  130. case RENDER_TEXTURE_MEM_USED: return RS::get_singleton()->get_render_info(RS::INFO_TEXTURE_MEM_USED);
  131. case RENDER_VERTEX_MEM_USED: return RS::get_singleton()->get_render_info(RS::INFO_VERTEX_MEM_USED);
  132. case RENDER_USAGE_VIDEO_MEM_TOTAL: return RS::get_singleton()->get_render_info(RS::INFO_USAGE_VIDEO_MEM_TOTAL);
  133. case PHYSICS_2D_ACTIVE_OBJECTS: return PhysicsServer2D::get_singleton()->get_process_info(PhysicsServer2D::INFO_ACTIVE_OBJECTS);
  134. case PHYSICS_2D_COLLISION_PAIRS: return PhysicsServer2D::get_singleton()->get_process_info(PhysicsServer2D::INFO_COLLISION_PAIRS);
  135. case PHYSICS_2D_ISLAND_COUNT: return PhysicsServer2D::get_singleton()->get_process_info(PhysicsServer2D::INFO_ISLAND_COUNT);
  136. case PHYSICS_3D_ACTIVE_OBJECTS: return PhysicsServer3D::get_singleton()->get_process_info(PhysicsServer3D::INFO_ACTIVE_OBJECTS);
  137. case PHYSICS_3D_COLLISION_PAIRS: return PhysicsServer3D::get_singleton()->get_process_info(PhysicsServer3D::INFO_COLLISION_PAIRS);
  138. case PHYSICS_3D_ISLAND_COUNT: return PhysicsServer3D::get_singleton()->get_process_info(PhysicsServer3D::INFO_ISLAND_COUNT);
  139. case AUDIO_OUTPUT_LATENCY: return AudioServer::get_singleton()->get_output_latency();
  140. default: {
  141. }
  142. }
  143. return 0;
  144. }
  145. Performance::MonitorType Performance::get_monitor_type(Monitor p_monitor) const {
  146. ERR_FAIL_INDEX_V(p_monitor, MONITOR_MAX, MONITOR_TYPE_QUANTITY);
  147. // ugly
  148. static const MonitorType types[MONITOR_MAX] = {
  149. MONITOR_TYPE_QUANTITY,
  150. MONITOR_TYPE_TIME,
  151. MONITOR_TYPE_TIME,
  152. MONITOR_TYPE_MEMORY,
  153. MONITOR_TYPE_MEMORY,
  154. MONITOR_TYPE_MEMORY,
  155. MONITOR_TYPE_QUANTITY,
  156. MONITOR_TYPE_QUANTITY,
  157. MONITOR_TYPE_QUANTITY,
  158. MONITOR_TYPE_QUANTITY,
  159. MONITOR_TYPE_QUANTITY,
  160. MONITOR_TYPE_QUANTITY,
  161. MONITOR_TYPE_QUANTITY,
  162. MONITOR_TYPE_QUANTITY,
  163. MONITOR_TYPE_QUANTITY,
  164. MONITOR_TYPE_QUANTITY,
  165. MONITOR_TYPE_MEMORY,
  166. MONITOR_TYPE_MEMORY,
  167. MONITOR_TYPE_MEMORY,
  168. MONITOR_TYPE_MEMORY,
  169. MONITOR_TYPE_QUANTITY,
  170. MONITOR_TYPE_QUANTITY,
  171. MONITOR_TYPE_QUANTITY,
  172. MONITOR_TYPE_QUANTITY,
  173. MONITOR_TYPE_QUANTITY,
  174. MONITOR_TYPE_QUANTITY,
  175. MONITOR_TYPE_TIME,
  176. };
  177. return types[p_monitor];
  178. }
  179. void Performance::set_process_time(float p_pt) {
  180. _process_time = p_pt;
  181. }
  182. void Performance::set_physics_process_time(float p_pt) {
  183. _physics_process_time = p_pt;
  184. }
  185. Performance::Performance() {
  186. _process_time = 0;
  187. _physics_process_time = 0;
  188. singleton = this;
  189. }