performance.cpp 16 KB

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