performance.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*************************************************************************/
  2. /* performance.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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_2d_server.h"
  37. #include "servers/physics_server.h"
  38. #include "servers/visual_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_DYNAMIC);
  47. BIND_ENUM_CONSTANT(MEMORY_STATIC_MAX);
  48. BIND_ENUM_CONSTANT(MEMORY_DYNAMIC_MAX);
  49. BIND_ENUM_CONSTANT(MEMORY_MESSAGE_BUFFER_MAX);
  50. BIND_ENUM_CONSTANT(OBJECT_COUNT);
  51. BIND_ENUM_CONSTANT(OBJECT_RESOURCE_COUNT);
  52. BIND_ENUM_CONSTANT(OBJECT_NODE_COUNT);
  53. BIND_ENUM_CONSTANT(OBJECT_ORPHAN_NODE_COUNT);
  54. BIND_ENUM_CONSTANT(RENDER_OBJECTS_IN_FRAME);
  55. BIND_ENUM_CONSTANT(RENDER_VERTICES_IN_FRAME);
  56. BIND_ENUM_CONSTANT(RENDER_MATERIAL_CHANGES_IN_FRAME);
  57. BIND_ENUM_CONSTANT(RENDER_SHADER_CHANGES_IN_FRAME);
  58. BIND_ENUM_CONSTANT(RENDER_SURFACE_CHANGES_IN_FRAME);
  59. BIND_ENUM_CONSTANT(RENDER_DRAW_CALLS_IN_FRAME);
  60. BIND_ENUM_CONSTANT(RENDER_2D_ITEMS_IN_FRAME);
  61. BIND_ENUM_CONSTANT(RENDER_2D_DRAW_CALLS_IN_FRAME);
  62. BIND_ENUM_CONSTANT(RENDER_VIDEO_MEM_USED);
  63. BIND_ENUM_CONSTANT(RENDER_TEXTURE_MEM_USED);
  64. BIND_ENUM_CONSTANT(RENDER_VERTEX_MEM_USED);
  65. BIND_ENUM_CONSTANT(RENDER_USAGE_VIDEO_MEM_TOTAL);
  66. BIND_ENUM_CONSTANT(PHYSICS_2D_ACTIVE_OBJECTS);
  67. BIND_ENUM_CONSTANT(PHYSICS_2D_COLLISION_PAIRS);
  68. BIND_ENUM_CONSTANT(PHYSICS_2D_ISLAND_COUNT);
  69. BIND_ENUM_CONSTANT(PHYSICS_3D_ACTIVE_OBJECTS);
  70. BIND_ENUM_CONSTANT(PHYSICS_3D_COLLISION_PAIRS);
  71. BIND_ENUM_CONSTANT(PHYSICS_3D_ISLAND_COUNT);
  72. BIND_ENUM_CONSTANT(AUDIO_OUTPUT_LATENCY);
  73. BIND_ENUM_CONSTANT(MONITOR_MAX);
  74. }
  75. float Performance::_get_node_count() const {
  76. MainLoop *ml = OS::get_singleton()->get_main_loop();
  77. SceneTree *sml = Object::cast_to<SceneTree>(ml);
  78. if (!sml)
  79. return 0;
  80. return sml->get_node_count();
  81. }
  82. String Performance::get_monitor_name(Monitor p_monitor) const {
  83. ERR_FAIL_INDEX_V(p_monitor, MONITOR_MAX, String());
  84. static const char *names[MONITOR_MAX] = {
  85. "time/fps",
  86. "time/process",
  87. "time/physics_process",
  88. "memory/static",
  89. "memory/dynamic",
  90. "memory/static_max",
  91. "memory/dynamic_max",
  92. "memory/msg_buf_max",
  93. "object/objects",
  94. "object/resources",
  95. "object/nodes",
  96. "object/orphan_nodes",
  97. "raster/objects_drawn",
  98. "raster/vertices_drawn",
  99. "raster/mat_changes",
  100. "raster/shader_changes",
  101. "raster/surface_changes",
  102. "raster/draw_calls",
  103. "2d/items",
  104. "2d/draw_calls",
  105. "video/video_mem",
  106. "video/texture_mem",
  107. "video/vertex_mem",
  108. "video/video_mem_max",
  109. "physics_2d/active_objects",
  110. "physics_2d/collision_pairs",
  111. "physics_2d/islands",
  112. "physics_3d/active_objects",
  113. "physics_3d/collision_pairs",
  114. "physics_3d/islands",
  115. "audio/output_latency",
  116. };
  117. return names[p_monitor];
  118. }
  119. float Performance::get_monitor(Monitor p_monitor) const {
  120. switch (p_monitor) {
  121. case TIME_FPS: return Engine::get_singleton()->get_frames_per_second();
  122. case TIME_PROCESS: return _process_time;
  123. case TIME_PHYSICS_PROCESS: return _physics_process_time;
  124. case MEMORY_STATIC: return Memory::get_mem_usage();
  125. case MEMORY_DYNAMIC: return MemoryPool::total_memory;
  126. case MEMORY_STATIC_MAX: return Memory::get_mem_max_usage();
  127. case MEMORY_DYNAMIC_MAX: return MemoryPool::max_memory;
  128. case MEMORY_MESSAGE_BUFFER_MAX: return MessageQueue::get_singleton()->get_max_buffer_usage();
  129. case OBJECT_COUNT: return ObjectDB::get_object_count();
  130. case OBJECT_RESOURCE_COUNT: return ResourceCache::get_cached_resource_count();
  131. case OBJECT_NODE_COUNT: return _get_node_count();
  132. case OBJECT_ORPHAN_NODE_COUNT: return Node::orphan_node_count;
  133. case RENDER_OBJECTS_IN_FRAME: return VS::get_singleton()->get_render_info(VS::INFO_OBJECTS_IN_FRAME);
  134. case RENDER_VERTICES_IN_FRAME: return VS::get_singleton()->get_render_info(VS::INFO_VERTICES_IN_FRAME);
  135. case RENDER_MATERIAL_CHANGES_IN_FRAME: return VS::get_singleton()->get_render_info(VS::INFO_MATERIAL_CHANGES_IN_FRAME);
  136. case RENDER_SHADER_CHANGES_IN_FRAME: return VS::get_singleton()->get_render_info(VS::INFO_SHADER_CHANGES_IN_FRAME);
  137. case RENDER_SURFACE_CHANGES_IN_FRAME: return VS::get_singleton()->get_render_info(VS::INFO_SURFACE_CHANGES_IN_FRAME);
  138. case RENDER_DRAW_CALLS_IN_FRAME: return VS::get_singleton()->get_render_info(VS::INFO_DRAW_CALLS_IN_FRAME);
  139. case RENDER_2D_ITEMS_IN_FRAME: return VS::get_singleton()->get_render_info(VS::INFO_2D_ITEMS_IN_FRAME);
  140. case RENDER_2D_DRAW_CALLS_IN_FRAME: return VS::get_singleton()->get_render_info(VS::INFO_2D_DRAW_CALLS_IN_FRAME);
  141. case RENDER_VIDEO_MEM_USED: return VS::get_singleton()->get_render_info(VS::INFO_VIDEO_MEM_USED);
  142. case RENDER_TEXTURE_MEM_USED: return VS::get_singleton()->get_render_info(VS::INFO_TEXTURE_MEM_USED);
  143. case RENDER_VERTEX_MEM_USED: return VS::get_singleton()->get_render_info(VS::INFO_VERTEX_MEM_USED);
  144. case RENDER_USAGE_VIDEO_MEM_TOTAL: return VS::get_singleton()->get_render_info(VS::INFO_USAGE_VIDEO_MEM_TOTAL);
  145. case PHYSICS_2D_ACTIVE_OBJECTS: return Physics2DServer::get_singleton()->get_process_info(Physics2DServer::INFO_ACTIVE_OBJECTS);
  146. case PHYSICS_2D_COLLISION_PAIRS: return Physics2DServer::get_singleton()->get_process_info(Physics2DServer::INFO_COLLISION_PAIRS);
  147. case PHYSICS_2D_ISLAND_COUNT: return Physics2DServer::get_singleton()->get_process_info(Physics2DServer::INFO_ISLAND_COUNT);
  148. case PHYSICS_3D_ACTIVE_OBJECTS: return PhysicsServer::get_singleton()->get_process_info(PhysicsServer::INFO_ACTIVE_OBJECTS);
  149. case PHYSICS_3D_COLLISION_PAIRS: return PhysicsServer::get_singleton()->get_process_info(PhysicsServer::INFO_COLLISION_PAIRS);
  150. case PHYSICS_3D_ISLAND_COUNT: return PhysicsServer::get_singleton()->get_process_info(PhysicsServer::INFO_ISLAND_COUNT);
  151. case AUDIO_OUTPUT_LATENCY: return AudioServer::get_singleton()->get_output_latency();
  152. default: {
  153. }
  154. }
  155. return 0;
  156. }
  157. Performance::MonitorType Performance::get_monitor_type(Monitor p_monitor) const {
  158. ERR_FAIL_INDEX_V(p_monitor, MONITOR_MAX, MONITOR_TYPE_QUANTITY);
  159. // ugly
  160. static const MonitorType types[MONITOR_MAX] = {
  161. MONITOR_TYPE_QUANTITY,
  162. MONITOR_TYPE_TIME,
  163. MONITOR_TYPE_TIME,
  164. MONITOR_TYPE_MEMORY,
  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_QUANTITY,
  176. MONITOR_TYPE_QUANTITY,
  177. MONITOR_TYPE_QUANTITY,
  178. MONITOR_TYPE_QUANTITY,
  179. MONITOR_TYPE_QUANTITY,
  180. MONITOR_TYPE_QUANTITY,
  181. MONITOR_TYPE_MEMORY,
  182. MONITOR_TYPE_MEMORY,
  183. MONITOR_TYPE_MEMORY,
  184. MONITOR_TYPE_MEMORY,
  185. MONITOR_TYPE_QUANTITY,
  186. MONITOR_TYPE_QUANTITY,
  187. MONITOR_TYPE_QUANTITY,
  188. MONITOR_TYPE_QUANTITY,
  189. MONITOR_TYPE_QUANTITY,
  190. MONITOR_TYPE_QUANTITY,
  191. MONITOR_TYPE_TIME,
  192. };
  193. return types[p_monitor];
  194. }
  195. void Performance::set_process_time(float p_pt) {
  196. _process_time = p_pt;
  197. }
  198. void Performance::set_physics_process_time(float p_pt) {
  199. _physics_process_time = p_pt;
  200. }
  201. Performance::Performance() {
  202. _process_time = 0;
  203. _physics_process_time = 0;
  204. singleton = this;
  205. }