performance.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*************************************************************************/
  2. /* performance.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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/scene_tree.h"
  34. #include "servers/audio_server.h"
  35. #include "servers/physics_2d_server.h"
  36. #include "servers/physics_server.h"
  37. #include "servers/visual_server.h"
  38. Performance *Performance::singleton = NULL;
  39. void Performance::_bind_methods() {
  40. ClassDB::bind_method(D_METHOD("get_monitor", "monitor"), &Performance::get_monitor);
  41. BIND_ENUM_CONSTANT(TIME_FPS);
  42. BIND_ENUM_CONSTANT(TIME_PROCESS);
  43. BIND_ENUM_CONSTANT(TIME_PHYSICS_PROCESS);
  44. BIND_ENUM_CONSTANT(MEMORY_STATIC);
  45. BIND_ENUM_CONSTANT(MEMORY_DYNAMIC);
  46. BIND_ENUM_CONSTANT(MEMORY_STATIC_MAX);
  47. BIND_ENUM_CONSTANT(MEMORY_DYNAMIC_MAX);
  48. BIND_ENUM_CONSTANT(MEMORY_MESSAGE_BUFFER_MAX);
  49. BIND_ENUM_CONSTANT(OBJECT_COUNT);
  50. BIND_ENUM_CONSTANT(OBJECT_RESOURCE_COUNT);
  51. BIND_ENUM_CONSTANT(OBJECT_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. String Performance::get_monitor_name(Monitor p_monitor) const {
  72. ERR_FAIL_INDEX_V(p_monitor, MONITOR_MAX, String());
  73. static const char *names[MONITOR_MAX] = {
  74. "time/fps",
  75. "time/process",
  76. "time/physics_process",
  77. "memory/static",
  78. "memory/dynamic",
  79. "memory/static_max",
  80. "memory/dynamic_max",
  81. "memory/msg_buf_max",
  82. "object/objects",
  83. "object/resources",
  84. "object/nodes",
  85. "raster/objects_drawn",
  86. "raster/vertices_drawn",
  87. "raster/mat_changes",
  88. "raster/shader_changes",
  89. "raster/surface_changes",
  90. "raster/draw_calls",
  91. "video/video_mem",
  92. "video/texture_mem",
  93. "video/vertex_mem",
  94. "video/video_mem_max",
  95. "physics_2d/active_objects",
  96. "physics_2d/collision_pairs",
  97. "physics_2d/islands",
  98. "physics_3d/active_objects",
  99. "physics_3d/collision_pairs",
  100. "physics_3d/islands",
  101. "audio/output_latency",
  102. };
  103. return names[p_monitor];
  104. }
  105. float Performance::get_monitor(Monitor p_monitor) const {
  106. switch (p_monitor) {
  107. case TIME_FPS: return Engine::get_singleton()->get_frames_per_second();
  108. case TIME_PROCESS: return _process_time;
  109. case TIME_PHYSICS_PROCESS: return _physics_process_time;
  110. case MEMORY_STATIC: return Memory::get_mem_usage();
  111. case MEMORY_DYNAMIC: return MemoryPool::total_memory;
  112. case MEMORY_STATIC_MAX: return Memory::get_mem_max_usage();
  113. case MEMORY_DYNAMIC_MAX: return MemoryPool::max_memory;
  114. case MEMORY_MESSAGE_BUFFER_MAX: return MessageQueue::get_singleton()->get_max_buffer_usage();
  115. case OBJECT_COUNT: return ObjectDB::get_object_count();
  116. case OBJECT_RESOURCE_COUNT: return ResourceCache::get_cached_resource_count();
  117. case OBJECT_NODE_COUNT: {
  118. MainLoop *ml = OS::get_singleton()->get_main_loop();
  119. SceneTree *sml = Object::cast_to<SceneTree>(ml);
  120. if (!sml)
  121. return 0;
  122. return sml->get_node_count();
  123. };
  124. case RENDER_OBJECTS_IN_FRAME: return VS::get_singleton()->get_render_info(VS::INFO_OBJECTS_IN_FRAME);
  125. case RENDER_VERTICES_IN_FRAME: return VS::get_singleton()->get_render_info(VS::INFO_VERTICES_IN_FRAME);
  126. case RENDER_MATERIAL_CHANGES_IN_FRAME: return VS::get_singleton()->get_render_info(VS::INFO_MATERIAL_CHANGES_IN_FRAME);
  127. case RENDER_SHADER_CHANGES_IN_FRAME: return VS::get_singleton()->get_render_info(VS::INFO_SHADER_CHANGES_IN_FRAME);
  128. case RENDER_SURFACE_CHANGES_IN_FRAME: return VS::get_singleton()->get_render_info(VS::INFO_SURFACE_CHANGES_IN_FRAME);
  129. case RENDER_DRAW_CALLS_IN_FRAME: return VS::get_singleton()->get_render_info(VS::INFO_DRAW_CALLS_IN_FRAME);
  130. case RENDER_VIDEO_MEM_USED: return VS::get_singleton()->get_render_info(VS::INFO_VIDEO_MEM_USED);
  131. case RENDER_TEXTURE_MEM_USED: return VS::get_singleton()->get_render_info(VS::INFO_TEXTURE_MEM_USED);
  132. case RENDER_VERTEX_MEM_USED: return VS::get_singleton()->get_render_info(VS::INFO_VERTEX_MEM_USED);
  133. case RENDER_USAGE_VIDEO_MEM_TOTAL: return VS::get_singleton()->get_render_info(VS::INFO_USAGE_VIDEO_MEM_TOTAL);
  134. case PHYSICS_2D_ACTIVE_OBJECTS: return Physics2DServer::get_singleton()->get_process_info(Physics2DServer::INFO_ACTIVE_OBJECTS);
  135. case PHYSICS_2D_COLLISION_PAIRS: return Physics2DServer::get_singleton()->get_process_info(Physics2DServer::INFO_COLLISION_PAIRS);
  136. case PHYSICS_2D_ISLAND_COUNT: return Physics2DServer::get_singleton()->get_process_info(Physics2DServer::INFO_ISLAND_COUNT);
  137. case PHYSICS_3D_ACTIVE_OBJECTS: return PhysicsServer::get_singleton()->get_process_info(PhysicsServer::INFO_ACTIVE_OBJECTS);
  138. case PHYSICS_3D_COLLISION_PAIRS: return PhysicsServer::get_singleton()->get_process_info(PhysicsServer::INFO_COLLISION_PAIRS);
  139. case PHYSICS_3D_ISLAND_COUNT: return PhysicsServer::get_singleton()->get_process_info(PhysicsServer::INFO_ISLAND_COUNT);
  140. case AUDIO_OUTPUT_LATENCY: return AudioServer::get_singleton()->get_output_latency();
  141. default: {}
  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_MEMORY,
  156. MONITOR_TYPE_MEMORY,
  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_QUANTITY,
  166. MONITOR_TYPE_MEMORY,
  167. MONITOR_TYPE_MEMORY,
  168. MONITOR_TYPE_MEMORY,
  169. MONITOR_TYPE_MEMORY,
  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_TIME,
  177. };
  178. return types[p_monitor];
  179. }
  180. void Performance::set_process_time(float p_pt) {
  181. _process_time = p_pt;
  182. }
  183. void Performance::set_physics_process_time(float p_pt) {
  184. _physics_process_time = p_pt;
  185. }
  186. Performance::Performance() {
  187. _process_time = 0;
  188. _physics_process_time = 0;
  189. singleton = this;
  190. }