performance.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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/os/os.h"
  32. #include "core/variant/typed_array.h"
  33. #include "scene/main/node.h"
  34. #include "scene/main/scene_tree.h"
  35. #include "servers/audio_server.h"
  36. #include "servers/navigation_server_3d.h"
  37. #include "servers/rendering_server.h"
  38. // 2D
  39. #include "servers/physics_server_2d.h"
  40. #ifndef _3D_DISABLED
  41. #include "servers/physics_server_3d.h"
  42. #endif // _3D_DISABLED
  43. Performance *Performance::singleton = nullptr;
  44. void Performance::_bind_methods() {
  45. ClassDB::bind_method(D_METHOD("get_monitor", "monitor"), &Performance::get_monitor);
  46. ClassDB::bind_method(D_METHOD("add_custom_monitor", "id", "callable", "arguments"), &Performance::add_custom_monitor, DEFVAL(Array()));
  47. ClassDB::bind_method(D_METHOD("remove_custom_monitor", "id"), &Performance::remove_custom_monitor);
  48. ClassDB::bind_method(D_METHOD("has_custom_monitor", "id"), &Performance::has_custom_monitor);
  49. ClassDB::bind_method(D_METHOD("get_custom_monitor", "id"), &Performance::get_custom_monitor);
  50. ClassDB::bind_method(D_METHOD("get_monitor_modification_time"), &Performance::get_monitor_modification_time);
  51. ClassDB::bind_method(D_METHOD("get_custom_monitor_names"), &Performance::get_custom_monitor_names);
  52. BIND_ENUM_CONSTANT(TIME_FPS);
  53. BIND_ENUM_CONSTANT(TIME_PROCESS);
  54. BIND_ENUM_CONSTANT(TIME_PHYSICS_PROCESS);
  55. BIND_ENUM_CONSTANT(TIME_NAVIGATION_PROCESS);
  56. BIND_ENUM_CONSTANT(MEMORY_STATIC);
  57. BIND_ENUM_CONSTANT(MEMORY_STATIC_MAX);
  58. BIND_ENUM_CONSTANT(MEMORY_MESSAGE_BUFFER_MAX);
  59. BIND_ENUM_CONSTANT(OBJECT_COUNT);
  60. BIND_ENUM_CONSTANT(OBJECT_RESOURCE_COUNT);
  61. BIND_ENUM_CONSTANT(OBJECT_NODE_COUNT);
  62. BIND_ENUM_CONSTANT(OBJECT_ORPHAN_NODE_COUNT);
  63. BIND_ENUM_CONSTANT(RENDER_TOTAL_OBJECTS_IN_FRAME);
  64. BIND_ENUM_CONSTANT(RENDER_TOTAL_PRIMITIVES_IN_FRAME);
  65. BIND_ENUM_CONSTANT(RENDER_TOTAL_DRAW_CALLS_IN_FRAME);
  66. BIND_ENUM_CONSTANT(RENDER_VIDEO_MEM_USED);
  67. BIND_ENUM_CONSTANT(RENDER_TEXTURE_MEM_USED);
  68. BIND_ENUM_CONSTANT(RENDER_BUFFER_MEM_USED);
  69. BIND_ENUM_CONSTANT(PHYSICS_2D_ACTIVE_OBJECTS);
  70. BIND_ENUM_CONSTANT(PHYSICS_2D_COLLISION_PAIRS);
  71. BIND_ENUM_CONSTANT(PHYSICS_2D_ISLAND_COUNT);
  72. #ifndef _3D_DISABLED
  73. BIND_ENUM_CONSTANT(PHYSICS_3D_ACTIVE_OBJECTS);
  74. BIND_ENUM_CONSTANT(PHYSICS_3D_COLLISION_PAIRS);
  75. BIND_ENUM_CONSTANT(PHYSICS_3D_ISLAND_COUNT);
  76. #endif // _3D_DISABLED
  77. BIND_ENUM_CONSTANT(AUDIO_OUTPUT_LATENCY);
  78. BIND_ENUM_CONSTANT(NAVIGATION_ACTIVE_MAPS);
  79. BIND_ENUM_CONSTANT(NAVIGATION_REGION_COUNT);
  80. BIND_ENUM_CONSTANT(NAVIGATION_AGENT_COUNT);
  81. BIND_ENUM_CONSTANT(NAVIGATION_LINK_COUNT);
  82. BIND_ENUM_CONSTANT(NAVIGATION_POLYGON_COUNT);
  83. BIND_ENUM_CONSTANT(NAVIGATION_EDGE_COUNT);
  84. BIND_ENUM_CONSTANT(NAVIGATION_EDGE_MERGE_COUNT);
  85. BIND_ENUM_CONSTANT(NAVIGATION_EDGE_CONNECTION_COUNT);
  86. BIND_ENUM_CONSTANT(NAVIGATION_EDGE_FREE_COUNT);
  87. BIND_ENUM_CONSTANT(NAVIGATION_OBSTACLE_COUNT);
  88. BIND_ENUM_CONSTANT(PIPELINE_COMPILATIONS_CANVAS);
  89. BIND_ENUM_CONSTANT(PIPELINE_COMPILATIONS_MESH);
  90. BIND_ENUM_CONSTANT(PIPELINE_COMPILATIONS_SURFACE);
  91. BIND_ENUM_CONSTANT(PIPELINE_COMPILATIONS_DRAW);
  92. BIND_ENUM_CONSTANT(PIPELINE_COMPILATIONS_SPECIALIZATION);
  93. BIND_ENUM_CONSTANT(MONITOR_MAX);
  94. }
  95. int Performance::_get_node_count() const {
  96. MainLoop *ml = OS::get_singleton()->get_main_loop();
  97. SceneTree *sml = Object::cast_to<SceneTree>(ml);
  98. if (!sml) {
  99. return 0;
  100. }
  101. return sml->get_node_count();
  102. }
  103. String Performance::get_monitor_name(Monitor p_monitor) const {
  104. ERR_FAIL_INDEX_V(p_monitor, MONITOR_MAX, String());
  105. static const char *names[MONITOR_MAX] = {
  106. PNAME("time/fps"),
  107. PNAME("time/process"),
  108. PNAME("time/physics_process"),
  109. PNAME("time/navigation_process"),
  110. PNAME("memory/static"),
  111. PNAME("memory/static_max"),
  112. PNAME("memory/msg_buf_max"),
  113. PNAME("object/objects"),
  114. PNAME("object/resources"),
  115. PNAME("object/nodes"),
  116. PNAME("object/orphan_nodes"),
  117. PNAME("raster/total_objects_drawn"),
  118. PNAME("raster/total_primitives_drawn"),
  119. PNAME("raster/total_draw_calls"),
  120. PNAME("video/video_mem"),
  121. PNAME("video/texture_mem"),
  122. PNAME("video/buffer_mem"),
  123. PNAME("physics_2d/active_objects"),
  124. PNAME("physics_2d/collision_pairs"),
  125. PNAME("physics_2d/islands"),
  126. PNAME("physics_3d/active_objects"),
  127. PNAME("physics_3d/collision_pairs"),
  128. PNAME("physics_3d/islands"),
  129. PNAME("audio/driver/output_latency"),
  130. PNAME("navigation/active_maps"),
  131. PNAME("navigation/regions"),
  132. PNAME("navigation/agents"),
  133. PNAME("navigation/links"),
  134. PNAME("navigation/polygons"),
  135. PNAME("navigation/edges"),
  136. PNAME("navigation/edges_merged"),
  137. PNAME("navigation/edges_connected"),
  138. PNAME("navigation/edges_free"),
  139. PNAME("navigation/obstacles"),
  140. PNAME("pipeline/compilations_canvas"),
  141. PNAME("pipeline/compilations_mesh"),
  142. PNAME("pipeline/compilations_surface"),
  143. PNAME("pipeline/compilations_draw"),
  144. PNAME("pipeline/compilations_specialization"),
  145. };
  146. static_assert((sizeof(names) / sizeof(const char *)) == MONITOR_MAX);
  147. return names[p_monitor];
  148. }
  149. double Performance::get_monitor(Monitor p_monitor) const {
  150. switch (p_monitor) {
  151. case TIME_FPS:
  152. return Engine::get_singleton()->get_frames_per_second();
  153. case TIME_PROCESS:
  154. return _process_time;
  155. case TIME_PHYSICS_PROCESS:
  156. return _physics_process_time;
  157. case TIME_NAVIGATION_PROCESS:
  158. return _navigation_process_time;
  159. case MEMORY_STATIC:
  160. return Memory::get_mem_usage();
  161. case MEMORY_STATIC_MAX:
  162. return Memory::get_mem_max_usage();
  163. case MEMORY_MESSAGE_BUFFER_MAX:
  164. return MessageQueue::get_singleton()->get_max_buffer_usage();
  165. case OBJECT_COUNT:
  166. return ObjectDB::get_object_count();
  167. case OBJECT_RESOURCE_COUNT:
  168. return ResourceCache::get_cached_resource_count();
  169. case OBJECT_NODE_COUNT:
  170. return _get_node_count();
  171. case OBJECT_ORPHAN_NODE_COUNT:
  172. return Node::orphan_node_count;
  173. case RENDER_TOTAL_OBJECTS_IN_FRAME:
  174. return RS::get_singleton()->get_rendering_info(RS::RENDERING_INFO_TOTAL_OBJECTS_IN_FRAME);
  175. case RENDER_TOTAL_PRIMITIVES_IN_FRAME:
  176. return RS::get_singleton()->get_rendering_info(RS::RENDERING_INFO_TOTAL_PRIMITIVES_IN_FRAME);
  177. case RENDER_TOTAL_DRAW_CALLS_IN_FRAME:
  178. return RS::get_singleton()->get_rendering_info(RS::RENDERING_INFO_TOTAL_DRAW_CALLS_IN_FRAME);
  179. case RENDER_VIDEO_MEM_USED:
  180. return RS::get_singleton()->get_rendering_info(RS::RENDERING_INFO_VIDEO_MEM_USED);
  181. case RENDER_TEXTURE_MEM_USED:
  182. return RS::get_singleton()->get_rendering_info(RS::RENDERING_INFO_TEXTURE_MEM_USED);
  183. case RENDER_BUFFER_MEM_USED:
  184. return RS::get_singleton()->get_rendering_info(RS::RENDERING_INFO_BUFFER_MEM_USED);
  185. case PIPELINE_COMPILATIONS_CANVAS:
  186. return RS::get_singleton()->get_rendering_info(RS::RENDERING_INFO_PIPELINE_COMPILATIONS_CANVAS);
  187. case PIPELINE_COMPILATIONS_MESH:
  188. return RS::get_singleton()->get_rendering_info(RS::RENDERING_INFO_PIPELINE_COMPILATIONS_MESH);
  189. case PIPELINE_COMPILATIONS_SURFACE:
  190. return RS::get_singleton()->get_rendering_info(RS::RENDERING_INFO_PIPELINE_COMPILATIONS_SURFACE);
  191. case PIPELINE_COMPILATIONS_DRAW:
  192. return RS::get_singleton()->get_rendering_info(RS::RENDERING_INFO_PIPELINE_COMPILATIONS_DRAW);
  193. case PIPELINE_COMPILATIONS_SPECIALIZATION:
  194. return RS::get_singleton()->get_rendering_info(RS::RENDERING_INFO_PIPELINE_COMPILATIONS_SPECIALIZATION);
  195. case PHYSICS_2D_ACTIVE_OBJECTS:
  196. return PhysicsServer2D::get_singleton()->get_process_info(PhysicsServer2D::INFO_ACTIVE_OBJECTS);
  197. case PHYSICS_2D_COLLISION_PAIRS:
  198. return PhysicsServer2D::get_singleton()->get_process_info(PhysicsServer2D::INFO_COLLISION_PAIRS);
  199. case PHYSICS_2D_ISLAND_COUNT:
  200. return PhysicsServer2D::get_singleton()->get_process_info(PhysicsServer2D::INFO_ISLAND_COUNT);
  201. #ifdef _3D_DISABLED
  202. case PHYSICS_3D_ACTIVE_OBJECTS:
  203. return 0;
  204. case PHYSICS_3D_COLLISION_PAIRS:
  205. return 0;
  206. case PHYSICS_3D_ISLAND_COUNT:
  207. return 0;
  208. #else
  209. case PHYSICS_3D_ACTIVE_OBJECTS:
  210. return PhysicsServer3D::get_singleton()->get_process_info(PhysicsServer3D::INFO_ACTIVE_OBJECTS);
  211. case PHYSICS_3D_COLLISION_PAIRS:
  212. return PhysicsServer3D::get_singleton()->get_process_info(PhysicsServer3D::INFO_COLLISION_PAIRS);
  213. case PHYSICS_3D_ISLAND_COUNT:
  214. return PhysicsServer3D::get_singleton()->get_process_info(PhysicsServer3D::INFO_ISLAND_COUNT);
  215. #endif // _3D_DISABLED
  216. case AUDIO_OUTPUT_LATENCY:
  217. return AudioServer::get_singleton()->get_output_latency();
  218. case NAVIGATION_ACTIVE_MAPS:
  219. return NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_ACTIVE_MAPS);
  220. case NAVIGATION_REGION_COUNT:
  221. return NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_REGION_COUNT);
  222. case NAVIGATION_AGENT_COUNT:
  223. return NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_AGENT_COUNT);
  224. case NAVIGATION_LINK_COUNT:
  225. return NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_LINK_COUNT);
  226. case NAVIGATION_POLYGON_COUNT:
  227. return NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_POLYGON_COUNT);
  228. case NAVIGATION_EDGE_COUNT:
  229. return NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_EDGE_COUNT);
  230. case NAVIGATION_EDGE_MERGE_COUNT:
  231. return NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_EDGE_MERGE_COUNT);
  232. case NAVIGATION_EDGE_CONNECTION_COUNT:
  233. return NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_EDGE_CONNECTION_COUNT);
  234. case NAVIGATION_EDGE_FREE_COUNT:
  235. return NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_EDGE_FREE_COUNT);
  236. case NAVIGATION_OBSTACLE_COUNT:
  237. return NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_OBSTACLE_COUNT);
  238. default: {
  239. }
  240. }
  241. return 0;
  242. }
  243. Performance::MonitorType Performance::get_monitor_type(Monitor p_monitor) const {
  244. ERR_FAIL_INDEX_V(p_monitor, MONITOR_MAX, MONITOR_TYPE_QUANTITY);
  245. // ugly
  246. static const MonitorType types[MONITOR_MAX] = {
  247. MONITOR_TYPE_QUANTITY,
  248. MONITOR_TYPE_TIME,
  249. MONITOR_TYPE_TIME,
  250. MONITOR_TYPE_TIME,
  251. MONITOR_TYPE_MEMORY,
  252. MONITOR_TYPE_MEMORY,
  253. MONITOR_TYPE_MEMORY,
  254. MONITOR_TYPE_QUANTITY,
  255. MONITOR_TYPE_QUANTITY,
  256. MONITOR_TYPE_QUANTITY,
  257. MONITOR_TYPE_QUANTITY,
  258. MONITOR_TYPE_QUANTITY,
  259. MONITOR_TYPE_QUANTITY,
  260. MONITOR_TYPE_QUANTITY,
  261. MONITOR_TYPE_MEMORY,
  262. MONITOR_TYPE_MEMORY,
  263. MONITOR_TYPE_MEMORY,
  264. MONITOR_TYPE_QUANTITY,
  265. MONITOR_TYPE_QUANTITY,
  266. MONITOR_TYPE_QUANTITY,
  267. MONITOR_TYPE_QUANTITY,
  268. MONITOR_TYPE_QUANTITY,
  269. MONITOR_TYPE_QUANTITY,
  270. MONITOR_TYPE_TIME,
  271. MONITOR_TYPE_QUANTITY,
  272. MONITOR_TYPE_QUANTITY,
  273. MONITOR_TYPE_QUANTITY,
  274. MONITOR_TYPE_QUANTITY,
  275. MONITOR_TYPE_QUANTITY,
  276. MONITOR_TYPE_QUANTITY,
  277. MONITOR_TYPE_QUANTITY,
  278. MONITOR_TYPE_QUANTITY,
  279. MONITOR_TYPE_QUANTITY,
  280. MONITOR_TYPE_QUANTITY,
  281. MONITOR_TYPE_QUANTITY,
  282. MONITOR_TYPE_QUANTITY,
  283. MONITOR_TYPE_QUANTITY,
  284. MONITOR_TYPE_QUANTITY,
  285. MONITOR_TYPE_QUANTITY,
  286. };
  287. static_assert((sizeof(types) / sizeof(MonitorType)) == MONITOR_MAX);
  288. return types[p_monitor];
  289. }
  290. void Performance::set_process_time(double p_pt) {
  291. _process_time = p_pt;
  292. }
  293. void Performance::set_physics_process_time(double p_pt) {
  294. _physics_process_time = p_pt;
  295. }
  296. void Performance::set_navigation_process_time(double p_pt) {
  297. _navigation_process_time = p_pt;
  298. }
  299. void Performance::add_custom_monitor(const StringName &p_id, const Callable &p_callable, const Vector<Variant> &p_args) {
  300. ERR_FAIL_COND_MSG(has_custom_monitor(p_id), "Custom monitor with id '" + String(p_id) + "' already exists.");
  301. _monitor_map.insert(p_id, MonitorCall(p_callable, p_args));
  302. _monitor_modification_time = OS::get_singleton()->get_ticks_usec();
  303. }
  304. void Performance::remove_custom_monitor(const StringName &p_id) {
  305. ERR_FAIL_COND_MSG(!has_custom_monitor(p_id), "Custom monitor with id '" + String(p_id) + "' doesn't exists.");
  306. _monitor_map.erase(p_id);
  307. _monitor_modification_time = OS::get_singleton()->get_ticks_usec();
  308. }
  309. bool Performance::has_custom_monitor(const StringName &p_id) {
  310. return _monitor_map.has(p_id);
  311. }
  312. Variant Performance::get_custom_monitor(const StringName &p_id) {
  313. ERR_FAIL_COND_V_MSG(!has_custom_monitor(p_id), Variant(), "Custom monitor with id '" + String(p_id) + "' doesn't exists.");
  314. bool error;
  315. String error_message;
  316. Variant return_value = _monitor_map[p_id].call(error, error_message);
  317. ERR_FAIL_COND_V_MSG(error, return_value, "Error calling from custom monitor '" + String(p_id) + "' to callable: " + error_message);
  318. return return_value;
  319. }
  320. TypedArray<StringName> Performance::get_custom_monitor_names() {
  321. if (!_monitor_map.size()) {
  322. return TypedArray<StringName>();
  323. }
  324. TypedArray<StringName> return_array;
  325. return_array.resize(_monitor_map.size());
  326. int index = 0;
  327. for (KeyValue<StringName, MonitorCall> i : _monitor_map) {
  328. return_array.set(index, i.key);
  329. index++;
  330. }
  331. return return_array;
  332. }
  333. uint64_t Performance::get_monitor_modification_time() {
  334. return _monitor_modification_time;
  335. }
  336. Performance::Performance() {
  337. _process_time = 0;
  338. _physics_process_time = 0;
  339. _navigation_process_time = 0;
  340. _monitor_modification_time = 0;
  341. singleton = this;
  342. }
  343. Performance::MonitorCall::MonitorCall(Callable p_callable, Vector<Variant> p_arguments) {
  344. _callable = p_callable;
  345. _arguments = p_arguments;
  346. }
  347. Performance::MonitorCall::MonitorCall() {
  348. }
  349. Variant Performance::MonitorCall::call(bool &r_error, String &r_error_message) {
  350. Vector<const Variant *> arguments_mem;
  351. arguments_mem.resize(_arguments.size());
  352. for (int i = 0; i < _arguments.size(); i++) {
  353. arguments_mem.write[i] = &_arguments[i];
  354. }
  355. const Variant **args = (const Variant **)arguments_mem.ptr();
  356. int argc = _arguments.size();
  357. Variant return_value;
  358. Callable::CallError error;
  359. _callable.callp(args, argc, return_value, error);
  360. r_error = (error.error != Callable::CallError::CALL_OK);
  361. if (r_error) {
  362. r_error_message = Variant::get_callable_error_text(_callable, args, argc, error);
  363. }
  364. return return_value;
  365. }