performance.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. return names[p_monitor];
  147. }
  148. double Performance::get_monitor(Monitor p_monitor) const {
  149. switch (p_monitor) {
  150. case TIME_FPS:
  151. return Engine::get_singleton()->get_frames_per_second();
  152. case TIME_PROCESS:
  153. return _process_time;
  154. case TIME_PHYSICS_PROCESS:
  155. return _physics_process_time;
  156. case TIME_NAVIGATION_PROCESS:
  157. return _navigation_process_time;
  158. case MEMORY_STATIC:
  159. return Memory::get_mem_usage();
  160. case MEMORY_STATIC_MAX:
  161. return Memory::get_mem_max_usage();
  162. case MEMORY_MESSAGE_BUFFER_MAX:
  163. return MessageQueue::get_singleton()->get_max_buffer_usage();
  164. case OBJECT_COUNT:
  165. return ObjectDB::get_object_count();
  166. case OBJECT_RESOURCE_COUNT:
  167. return ResourceCache::get_cached_resource_count();
  168. case OBJECT_NODE_COUNT:
  169. return _get_node_count();
  170. case OBJECT_ORPHAN_NODE_COUNT:
  171. return Node::orphan_node_count;
  172. case RENDER_TOTAL_OBJECTS_IN_FRAME:
  173. return RS::get_singleton()->get_rendering_info(RS::RENDERING_INFO_TOTAL_OBJECTS_IN_FRAME);
  174. case RENDER_TOTAL_PRIMITIVES_IN_FRAME:
  175. return RS::get_singleton()->get_rendering_info(RS::RENDERING_INFO_TOTAL_PRIMITIVES_IN_FRAME);
  176. case RENDER_TOTAL_DRAW_CALLS_IN_FRAME:
  177. return RS::get_singleton()->get_rendering_info(RS::RENDERING_INFO_TOTAL_DRAW_CALLS_IN_FRAME);
  178. case RENDER_VIDEO_MEM_USED:
  179. return RS::get_singleton()->get_rendering_info(RS::RENDERING_INFO_VIDEO_MEM_USED);
  180. case RENDER_TEXTURE_MEM_USED:
  181. return RS::get_singleton()->get_rendering_info(RS::RENDERING_INFO_TEXTURE_MEM_USED);
  182. case RENDER_BUFFER_MEM_USED:
  183. return RS::get_singleton()->get_rendering_info(RS::RENDERING_INFO_BUFFER_MEM_USED);
  184. case PIPELINE_COMPILATIONS_CANVAS:
  185. return RS::get_singleton()->get_rendering_info(RS::RENDERING_INFO_PIPELINE_COMPILATIONS_CANVAS);
  186. case PIPELINE_COMPILATIONS_MESH:
  187. return RS::get_singleton()->get_rendering_info(RS::RENDERING_INFO_PIPELINE_COMPILATIONS_MESH);
  188. case PIPELINE_COMPILATIONS_SURFACE:
  189. return RS::get_singleton()->get_rendering_info(RS::RENDERING_INFO_PIPELINE_COMPILATIONS_SURFACE);
  190. case PIPELINE_COMPILATIONS_DRAW:
  191. return RS::get_singleton()->get_rendering_info(RS::RENDERING_INFO_PIPELINE_COMPILATIONS_DRAW);
  192. case PIPELINE_COMPILATIONS_SPECIALIZATION:
  193. return RS::get_singleton()->get_rendering_info(RS::RENDERING_INFO_PIPELINE_COMPILATIONS_SPECIALIZATION);
  194. case PHYSICS_2D_ACTIVE_OBJECTS:
  195. return PhysicsServer2D::get_singleton()->get_process_info(PhysicsServer2D::INFO_ACTIVE_OBJECTS);
  196. case PHYSICS_2D_COLLISION_PAIRS:
  197. return PhysicsServer2D::get_singleton()->get_process_info(PhysicsServer2D::INFO_COLLISION_PAIRS);
  198. case PHYSICS_2D_ISLAND_COUNT:
  199. return PhysicsServer2D::get_singleton()->get_process_info(PhysicsServer2D::INFO_ISLAND_COUNT);
  200. #ifdef _3D_DISABLED
  201. case PHYSICS_3D_ACTIVE_OBJECTS:
  202. return 0;
  203. case PHYSICS_3D_COLLISION_PAIRS:
  204. return 0;
  205. case PHYSICS_3D_ISLAND_COUNT:
  206. return 0;
  207. #else
  208. case PHYSICS_3D_ACTIVE_OBJECTS:
  209. return PhysicsServer3D::get_singleton()->get_process_info(PhysicsServer3D::INFO_ACTIVE_OBJECTS);
  210. case PHYSICS_3D_COLLISION_PAIRS:
  211. return PhysicsServer3D::get_singleton()->get_process_info(PhysicsServer3D::INFO_COLLISION_PAIRS);
  212. case PHYSICS_3D_ISLAND_COUNT:
  213. return PhysicsServer3D::get_singleton()->get_process_info(PhysicsServer3D::INFO_ISLAND_COUNT);
  214. #endif // _3D_DISABLED
  215. case AUDIO_OUTPUT_LATENCY:
  216. return AudioServer::get_singleton()->get_output_latency();
  217. case NAVIGATION_ACTIVE_MAPS:
  218. return NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_ACTIVE_MAPS);
  219. case NAVIGATION_REGION_COUNT:
  220. return NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_REGION_COUNT);
  221. case NAVIGATION_AGENT_COUNT:
  222. return NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_AGENT_COUNT);
  223. case NAVIGATION_LINK_COUNT:
  224. return NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_LINK_COUNT);
  225. case NAVIGATION_POLYGON_COUNT:
  226. return NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_POLYGON_COUNT);
  227. case NAVIGATION_EDGE_COUNT:
  228. return NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_EDGE_COUNT);
  229. case NAVIGATION_EDGE_MERGE_COUNT:
  230. return NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_EDGE_MERGE_COUNT);
  231. case NAVIGATION_EDGE_CONNECTION_COUNT:
  232. return NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_EDGE_CONNECTION_COUNT);
  233. case NAVIGATION_EDGE_FREE_COUNT:
  234. return NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_EDGE_FREE_COUNT);
  235. case NAVIGATION_OBSTACLE_COUNT:
  236. return NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_OBSTACLE_COUNT);
  237. default: {
  238. }
  239. }
  240. return 0;
  241. }
  242. Performance::MonitorType Performance::get_monitor_type(Monitor p_monitor) const {
  243. ERR_FAIL_INDEX_V(p_monitor, MONITOR_MAX, MONITOR_TYPE_QUANTITY);
  244. // ugly
  245. static const MonitorType types[MONITOR_MAX] = {
  246. MONITOR_TYPE_QUANTITY,
  247. MONITOR_TYPE_TIME,
  248. MONITOR_TYPE_TIME,
  249. MONITOR_TYPE_TIME,
  250. MONITOR_TYPE_MEMORY,
  251. MONITOR_TYPE_MEMORY,
  252. MONITOR_TYPE_MEMORY,
  253. MONITOR_TYPE_QUANTITY,
  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_MEMORY,
  261. MONITOR_TYPE_MEMORY,
  262. MONITOR_TYPE_MEMORY,
  263. MONITOR_TYPE_QUANTITY,
  264. MONITOR_TYPE_QUANTITY,
  265. MONITOR_TYPE_QUANTITY,
  266. MONITOR_TYPE_QUANTITY,
  267. MONITOR_TYPE_QUANTITY,
  268. MONITOR_TYPE_QUANTITY,
  269. MONITOR_TYPE_TIME,
  270. MONITOR_TYPE_QUANTITY,
  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. };
  281. return types[p_monitor];
  282. }
  283. void Performance::set_process_time(double p_pt) {
  284. _process_time = p_pt;
  285. }
  286. void Performance::set_physics_process_time(double p_pt) {
  287. _physics_process_time = p_pt;
  288. }
  289. void Performance::set_navigation_process_time(double p_pt) {
  290. _navigation_process_time = p_pt;
  291. }
  292. void Performance::add_custom_monitor(const StringName &p_id, const Callable &p_callable, const Vector<Variant> &p_args) {
  293. ERR_FAIL_COND_MSG(has_custom_monitor(p_id), "Custom monitor with id '" + String(p_id) + "' already exists.");
  294. _monitor_map.insert(p_id, MonitorCall(p_callable, p_args));
  295. _monitor_modification_time = OS::get_singleton()->get_ticks_usec();
  296. }
  297. void Performance::remove_custom_monitor(const StringName &p_id) {
  298. ERR_FAIL_COND_MSG(!has_custom_monitor(p_id), "Custom monitor with id '" + String(p_id) + "' doesn't exists.");
  299. _monitor_map.erase(p_id);
  300. _monitor_modification_time = OS::get_singleton()->get_ticks_usec();
  301. }
  302. bool Performance::has_custom_monitor(const StringName &p_id) {
  303. return _monitor_map.has(p_id);
  304. }
  305. Variant Performance::get_custom_monitor(const StringName &p_id) {
  306. ERR_FAIL_COND_V_MSG(!has_custom_monitor(p_id), Variant(), "Custom monitor with id '" + String(p_id) + "' doesn't exists.");
  307. bool error;
  308. String error_message;
  309. Variant return_value = _monitor_map[p_id].call(error, error_message);
  310. ERR_FAIL_COND_V_MSG(error, return_value, "Error calling from custom monitor '" + String(p_id) + "' to callable: " + error_message);
  311. return return_value;
  312. }
  313. TypedArray<StringName> Performance::get_custom_monitor_names() {
  314. if (!_monitor_map.size()) {
  315. return TypedArray<StringName>();
  316. }
  317. TypedArray<StringName> return_array;
  318. return_array.resize(_monitor_map.size());
  319. int index = 0;
  320. for (KeyValue<StringName, MonitorCall> i : _monitor_map) {
  321. return_array.set(index, i.key);
  322. index++;
  323. }
  324. return return_array;
  325. }
  326. uint64_t Performance::get_monitor_modification_time() {
  327. return _monitor_modification_time;
  328. }
  329. Performance::Performance() {
  330. _process_time = 0;
  331. _physics_process_time = 0;
  332. _navigation_process_time = 0;
  333. _monitor_modification_time = 0;
  334. singleton = this;
  335. }
  336. Performance::MonitorCall::MonitorCall(Callable p_callable, Vector<Variant> p_arguments) {
  337. _callable = p_callable;
  338. _arguments = p_arguments;
  339. }
  340. Performance::MonitorCall::MonitorCall() {
  341. }
  342. Variant Performance::MonitorCall::call(bool &r_error, String &r_error_message) {
  343. Vector<const Variant *> arguments_mem;
  344. arguments_mem.resize(_arguments.size());
  345. for (int i = 0; i < _arguments.size(); i++) {
  346. arguments_mem.write[i] = &_arguments[i];
  347. }
  348. const Variant **args = (const Variant **)arguments_mem.ptr();
  349. int argc = _arguments.size();
  350. Variant return_value;
  351. Callable::CallError error;
  352. _callable.callp(args, argc, return_value, error);
  353. r_error = (error.error != Callable::CallError::CALL_OK);
  354. if (r_error) {
  355. r_error_message = Variant::get_callable_error_text(_callable, args, argc, error);
  356. }
  357. return return_value;
  358. }