performance.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /**************************************************************************/
  2. /* performance.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 = nullptr;
  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. }
  81. return sml->get_node_count();
  82. }
  83. String Performance::get_monitor_name(Monitor p_monitor) const {
  84. ERR_FAIL_INDEX_V(p_monitor, MONITOR_MAX, String());
  85. static const char *names[MONITOR_MAX] = {
  86. "time/fps",
  87. "time/process",
  88. "time/physics_process",
  89. "memory/static",
  90. "memory/dynamic",
  91. "memory/static_max",
  92. "memory/dynamic_max",
  93. "memory/msg_buf_max",
  94. "object/objects",
  95. "object/resources",
  96. "object/nodes",
  97. "object/orphan_nodes",
  98. "raster/objects_drawn",
  99. "raster/vertices_drawn",
  100. "raster/mat_changes",
  101. "raster/shader_changes",
  102. "raster/surface_changes",
  103. "raster/draw_calls",
  104. "2d/items",
  105. "2d/draw_calls",
  106. "video/video_mem",
  107. "video/texture_mem",
  108. "video/vertex_mem",
  109. "video/video_mem_max",
  110. "physics_2d/active_objects",
  111. "physics_2d/collision_pairs",
  112. "physics_2d/islands",
  113. "physics_3d/active_objects",
  114. "physics_3d/collision_pairs",
  115. "physics_3d/islands",
  116. "audio/output_latency",
  117. };
  118. return names[p_monitor];
  119. }
  120. float Performance::get_monitor(Monitor p_monitor) const {
  121. switch (p_monitor) {
  122. case TIME_FPS:
  123. return Engine::get_singleton()->get_frames_per_second();
  124. case TIME_PROCESS:
  125. return _process_time;
  126. case TIME_PHYSICS_PROCESS:
  127. return _physics_process_time;
  128. case MEMORY_STATIC:
  129. return Memory::get_mem_usage();
  130. case MEMORY_DYNAMIC:
  131. return MemoryPool::total_memory;
  132. case MEMORY_STATIC_MAX:
  133. return Memory::get_mem_max_usage();
  134. case MEMORY_DYNAMIC_MAX:
  135. return MemoryPool::max_memory;
  136. case MEMORY_MESSAGE_BUFFER_MAX:
  137. return MessageQueue::get_singleton()->get_max_buffer_usage();
  138. case OBJECT_COUNT:
  139. return ObjectDB::get_object_count();
  140. case OBJECT_RESOURCE_COUNT:
  141. return ResourceCache::get_cached_resource_count();
  142. case OBJECT_NODE_COUNT:
  143. return _get_node_count();
  144. case OBJECT_ORPHAN_NODE_COUNT:
  145. return Node::orphan_node_count;
  146. case RENDER_OBJECTS_IN_FRAME:
  147. return VS::get_singleton()->get_render_info(VS::INFO_OBJECTS_IN_FRAME);
  148. case RENDER_VERTICES_IN_FRAME:
  149. return VS::get_singleton()->get_render_info(VS::INFO_VERTICES_IN_FRAME);
  150. case RENDER_MATERIAL_CHANGES_IN_FRAME:
  151. return VS::get_singleton()->get_render_info(VS::INFO_MATERIAL_CHANGES_IN_FRAME);
  152. case RENDER_SHADER_CHANGES_IN_FRAME:
  153. return VS::get_singleton()->get_render_info(VS::INFO_SHADER_CHANGES_IN_FRAME);
  154. case RENDER_SURFACE_CHANGES_IN_FRAME:
  155. return VS::get_singleton()->get_render_info(VS::INFO_SURFACE_CHANGES_IN_FRAME);
  156. case RENDER_DRAW_CALLS_IN_FRAME:
  157. return VS::get_singleton()->get_render_info(VS::INFO_DRAW_CALLS_IN_FRAME);
  158. case RENDER_2D_ITEMS_IN_FRAME:
  159. return VS::get_singleton()->get_render_info(VS::INFO_2D_ITEMS_IN_FRAME);
  160. case RENDER_2D_DRAW_CALLS_IN_FRAME:
  161. return VS::get_singleton()->get_render_info(VS::INFO_2D_DRAW_CALLS_IN_FRAME);
  162. case RENDER_VIDEO_MEM_USED:
  163. return VS::get_singleton()->get_render_info(VS::INFO_VIDEO_MEM_USED);
  164. case RENDER_TEXTURE_MEM_USED:
  165. return VS::get_singleton()->get_render_info(VS::INFO_TEXTURE_MEM_USED);
  166. case RENDER_VERTEX_MEM_USED:
  167. return VS::get_singleton()->get_render_info(VS::INFO_VERTEX_MEM_USED);
  168. case RENDER_USAGE_VIDEO_MEM_TOTAL:
  169. return VS::get_singleton()->get_render_info(VS::INFO_USAGE_VIDEO_MEM_TOTAL);
  170. case PHYSICS_2D_ACTIVE_OBJECTS:
  171. return Physics2DServer::get_singleton()->get_process_info(Physics2DServer::INFO_ACTIVE_OBJECTS);
  172. case PHYSICS_2D_COLLISION_PAIRS:
  173. return Physics2DServer::get_singleton()->get_process_info(Physics2DServer::INFO_COLLISION_PAIRS);
  174. case PHYSICS_2D_ISLAND_COUNT:
  175. return Physics2DServer::get_singleton()->get_process_info(Physics2DServer::INFO_ISLAND_COUNT);
  176. case PHYSICS_3D_ACTIVE_OBJECTS:
  177. return PhysicsServer::get_singleton()->get_process_info(PhysicsServer::INFO_ACTIVE_OBJECTS);
  178. case PHYSICS_3D_COLLISION_PAIRS:
  179. return PhysicsServer::get_singleton()->get_process_info(PhysicsServer::INFO_COLLISION_PAIRS);
  180. case PHYSICS_3D_ISLAND_COUNT:
  181. return PhysicsServer::get_singleton()->get_process_info(PhysicsServer::INFO_ISLAND_COUNT);
  182. case AUDIO_OUTPUT_LATENCY:
  183. return AudioServer::get_singleton()->get_output_latency();
  184. default: {
  185. }
  186. }
  187. return 0;
  188. }
  189. Performance::MonitorType Performance::get_monitor_type(Monitor p_monitor) const {
  190. ERR_FAIL_INDEX_V(p_monitor, MONITOR_MAX, MONITOR_TYPE_QUANTITY);
  191. // ugly
  192. static const MonitorType types[MONITOR_MAX] = {
  193. MONITOR_TYPE_QUANTITY,
  194. MONITOR_TYPE_TIME,
  195. MONITOR_TYPE_TIME,
  196. MONITOR_TYPE_MEMORY,
  197. MONITOR_TYPE_MEMORY,
  198. MONITOR_TYPE_MEMORY,
  199. MONITOR_TYPE_MEMORY,
  200. MONITOR_TYPE_MEMORY,
  201. MONITOR_TYPE_QUANTITY,
  202. MONITOR_TYPE_QUANTITY,
  203. MONITOR_TYPE_QUANTITY,
  204. MONITOR_TYPE_QUANTITY,
  205. MONITOR_TYPE_QUANTITY,
  206. MONITOR_TYPE_QUANTITY,
  207. MONITOR_TYPE_QUANTITY,
  208. MONITOR_TYPE_QUANTITY,
  209. MONITOR_TYPE_QUANTITY,
  210. MONITOR_TYPE_QUANTITY,
  211. MONITOR_TYPE_QUANTITY,
  212. MONITOR_TYPE_QUANTITY,
  213. MONITOR_TYPE_MEMORY,
  214. MONITOR_TYPE_MEMORY,
  215. MONITOR_TYPE_MEMORY,
  216. MONITOR_TYPE_MEMORY,
  217. MONITOR_TYPE_QUANTITY,
  218. MONITOR_TYPE_QUANTITY,
  219. MONITOR_TYPE_QUANTITY,
  220. MONITOR_TYPE_QUANTITY,
  221. MONITOR_TYPE_QUANTITY,
  222. MONITOR_TYPE_QUANTITY,
  223. MONITOR_TYPE_TIME,
  224. };
  225. return types[p_monitor];
  226. }
  227. void Performance::set_process_time(float p_pt) {
  228. _process_time = p_pt;
  229. }
  230. void Performance::set_physics_process_time(float p_pt) {
  231. _physics_process_time = p_pt;
  232. }
  233. Performance::Performance() {
  234. _process_time = 0;
  235. _physics_process_time = 0;
  236. singleton = this;
  237. }