performance.cpp 8.5 KB

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