performance.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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(MONITOR_MAX);
  88. }
  89. int Performance::_get_node_count() const {
  90. MainLoop *ml = OS::get_singleton()->get_main_loop();
  91. SceneTree *sml = Object::cast_to<SceneTree>(ml);
  92. if (!sml) {
  93. return 0;
  94. }
  95. return sml->get_node_count();
  96. }
  97. String Performance::get_monitor_name(Monitor p_monitor) const {
  98. ERR_FAIL_INDEX_V(p_monitor, MONITOR_MAX, String());
  99. static const char *names[MONITOR_MAX] = {
  100. PNAME("time/fps"),
  101. PNAME("time/process"),
  102. PNAME("time/physics_process"),
  103. PNAME("time/navigation_process"),
  104. PNAME("memory/static"),
  105. PNAME("memory/static_max"),
  106. PNAME("memory/msg_buf_max"),
  107. PNAME("object/objects"),
  108. PNAME("object/resources"),
  109. PNAME("object/nodes"),
  110. PNAME("object/orphan_nodes"),
  111. PNAME("raster/total_objects_drawn"),
  112. PNAME("raster/total_primitives_drawn"),
  113. PNAME("raster/total_draw_calls"),
  114. PNAME("video/video_mem"),
  115. PNAME("video/texture_mem"),
  116. PNAME("video/buffer_mem"),
  117. PNAME("physics_2d/active_objects"),
  118. PNAME("physics_2d/collision_pairs"),
  119. PNAME("physics_2d/islands"),
  120. #ifndef _3D_DISABLED
  121. PNAME("physics_3d/active_objects"),
  122. PNAME("physics_3d/collision_pairs"),
  123. PNAME("physics_3d/islands"),
  124. #endif // _3D_DISABLED
  125. PNAME("audio/driver/output_latency"),
  126. PNAME("navigation/active_maps"),
  127. PNAME("navigation/regions"),
  128. PNAME("navigation/agents"),
  129. PNAME("navigation/links"),
  130. PNAME("navigation/polygons"),
  131. PNAME("navigation/edges"),
  132. PNAME("navigation/edges_merged"),
  133. PNAME("navigation/edges_connected"),
  134. PNAME("navigation/edges_free"),
  135. };
  136. return names[p_monitor];
  137. }
  138. double Performance::get_monitor(Monitor p_monitor) const {
  139. switch (p_monitor) {
  140. case TIME_FPS:
  141. return Engine::get_singleton()->get_frames_per_second();
  142. case TIME_PROCESS:
  143. return _process_time;
  144. case TIME_PHYSICS_PROCESS:
  145. return _physics_process_time;
  146. case TIME_NAVIGATION_PROCESS:
  147. return _navigation_process_time;
  148. case MEMORY_STATIC:
  149. return Memory::get_mem_usage();
  150. case MEMORY_STATIC_MAX:
  151. return Memory::get_mem_max_usage();
  152. case MEMORY_MESSAGE_BUFFER_MAX:
  153. return MessageQueue::get_singleton()->get_max_buffer_usage();
  154. case OBJECT_COUNT:
  155. return ObjectDB::get_object_count();
  156. case OBJECT_RESOURCE_COUNT:
  157. return ResourceCache::get_cached_resource_count();
  158. case OBJECT_NODE_COUNT:
  159. return _get_node_count();
  160. case OBJECT_ORPHAN_NODE_COUNT:
  161. return Node::orphan_node_count;
  162. case RENDER_TOTAL_OBJECTS_IN_FRAME:
  163. return RS::get_singleton()->get_rendering_info(RS::RENDERING_INFO_TOTAL_OBJECTS_IN_FRAME);
  164. case RENDER_TOTAL_PRIMITIVES_IN_FRAME:
  165. return RS::get_singleton()->get_rendering_info(RS::RENDERING_INFO_TOTAL_PRIMITIVES_IN_FRAME);
  166. case RENDER_TOTAL_DRAW_CALLS_IN_FRAME:
  167. return RS::get_singleton()->get_rendering_info(RS::RENDERING_INFO_TOTAL_DRAW_CALLS_IN_FRAME);
  168. case RENDER_VIDEO_MEM_USED:
  169. return RS::get_singleton()->get_rendering_info(RS::RENDERING_INFO_VIDEO_MEM_USED);
  170. case RENDER_TEXTURE_MEM_USED:
  171. return RS::get_singleton()->get_rendering_info(RS::RENDERING_INFO_TEXTURE_MEM_USED);
  172. case RENDER_BUFFER_MEM_USED:
  173. return RS::get_singleton()->get_rendering_info(RS::RENDERING_INFO_BUFFER_MEM_USED);
  174. case PHYSICS_2D_ACTIVE_OBJECTS:
  175. return PhysicsServer2D::get_singleton()->get_process_info(PhysicsServer2D::INFO_ACTIVE_OBJECTS);
  176. case PHYSICS_2D_COLLISION_PAIRS:
  177. return PhysicsServer2D::get_singleton()->get_process_info(PhysicsServer2D::INFO_COLLISION_PAIRS);
  178. case PHYSICS_2D_ISLAND_COUNT:
  179. return PhysicsServer2D::get_singleton()->get_process_info(PhysicsServer2D::INFO_ISLAND_COUNT);
  180. #ifdef _3D_DISABLED
  181. case PHYSICS_3D_ACTIVE_OBJECTS:
  182. return 0;
  183. case PHYSICS_3D_COLLISION_PAIRS:
  184. return 0;
  185. case PHYSICS_3D_ISLAND_COUNT:
  186. return 0;
  187. #else
  188. case PHYSICS_3D_ACTIVE_OBJECTS:
  189. return PhysicsServer3D::get_singleton()->get_process_info(PhysicsServer3D::INFO_ACTIVE_OBJECTS);
  190. case PHYSICS_3D_COLLISION_PAIRS:
  191. return PhysicsServer3D::get_singleton()->get_process_info(PhysicsServer3D::INFO_COLLISION_PAIRS);
  192. case PHYSICS_3D_ISLAND_COUNT:
  193. return PhysicsServer3D::get_singleton()->get_process_info(PhysicsServer3D::INFO_ISLAND_COUNT);
  194. #endif // _3D_DISABLED
  195. case AUDIO_OUTPUT_LATENCY:
  196. return AudioServer::get_singleton()->get_output_latency();
  197. case NAVIGATION_ACTIVE_MAPS:
  198. return NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_ACTIVE_MAPS);
  199. case NAVIGATION_REGION_COUNT:
  200. return NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_REGION_COUNT);
  201. case NAVIGATION_AGENT_COUNT:
  202. return NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_AGENT_COUNT);
  203. case NAVIGATION_LINK_COUNT:
  204. return NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_LINK_COUNT);
  205. case NAVIGATION_POLYGON_COUNT:
  206. return NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_POLYGON_COUNT);
  207. case NAVIGATION_EDGE_COUNT:
  208. return NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_EDGE_COUNT);
  209. case NAVIGATION_EDGE_MERGE_COUNT:
  210. return NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_EDGE_MERGE_COUNT);
  211. case NAVIGATION_EDGE_CONNECTION_COUNT:
  212. return NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_EDGE_CONNECTION_COUNT);
  213. case NAVIGATION_EDGE_FREE_COUNT:
  214. return NavigationServer3D::get_singleton()->get_process_info(NavigationServer3D::INFO_EDGE_FREE_COUNT);
  215. default: {
  216. }
  217. }
  218. return 0;
  219. }
  220. Performance::MonitorType Performance::get_monitor_type(Monitor p_monitor) const {
  221. ERR_FAIL_INDEX_V(p_monitor, MONITOR_MAX, MONITOR_TYPE_QUANTITY);
  222. // ugly
  223. static const MonitorType types[MONITOR_MAX] = {
  224. MONITOR_TYPE_QUANTITY,
  225. MONITOR_TYPE_TIME,
  226. MONITOR_TYPE_TIME,
  227. MONITOR_TYPE_TIME,
  228. MONITOR_TYPE_MEMORY,
  229. MONITOR_TYPE_MEMORY,
  230. MONITOR_TYPE_MEMORY,
  231. MONITOR_TYPE_QUANTITY,
  232. MONITOR_TYPE_QUANTITY,
  233. MONITOR_TYPE_QUANTITY,
  234. MONITOR_TYPE_QUANTITY,
  235. MONITOR_TYPE_QUANTITY,
  236. MONITOR_TYPE_QUANTITY,
  237. MONITOR_TYPE_QUANTITY,
  238. MONITOR_TYPE_MEMORY,
  239. MONITOR_TYPE_MEMORY,
  240. MONITOR_TYPE_MEMORY,
  241. MONITOR_TYPE_QUANTITY,
  242. MONITOR_TYPE_QUANTITY,
  243. MONITOR_TYPE_QUANTITY,
  244. #ifndef _3D_DISABLED
  245. MONITOR_TYPE_QUANTITY,
  246. MONITOR_TYPE_QUANTITY,
  247. MONITOR_TYPE_QUANTITY,
  248. #endif // _3D_DISABLED
  249. MONITOR_TYPE_TIME,
  250. MONITOR_TYPE_QUANTITY,
  251. MONITOR_TYPE_QUANTITY,
  252. MONITOR_TYPE_QUANTITY,
  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. };
  260. return types[p_monitor];
  261. }
  262. void Performance::set_process_time(double p_pt) {
  263. _process_time = p_pt;
  264. }
  265. void Performance::set_physics_process_time(double p_pt) {
  266. _physics_process_time = p_pt;
  267. }
  268. void Performance::set_navigation_process_time(double p_pt) {
  269. _navigation_process_time = p_pt;
  270. }
  271. void Performance::add_custom_monitor(const StringName &p_id, const Callable &p_callable, const Vector<Variant> &p_args) {
  272. ERR_FAIL_COND_MSG(has_custom_monitor(p_id), "Custom monitor with id '" + String(p_id) + "' already exists.");
  273. _monitor_map.insert(p_id, MonitorCall(p_callable, p_args));
  274. _monitor_modification_time = OS::get_singleton()->get_ticks_usec();
  275. }
  276. void Performance::remove_custom_monitor(const StringName &p_id) {
  277. ERR_FAIL_COND_MSG(!has_custom_monitor(p_id), "Custom monitor with id '" + String(p_id) + "' doesn't exists.");
  278. _monitor_map.erase(p_id);
  279. _monitor_modification_time = OS::get_singleton()->get_ticks_usec();
  280. }
  281. bool Performance::has_custom_monitor(const StringName &p_id) {
  282. return _monitor_map.has(p_id);
  283. }
  284. Variant Performance::get_custom_monitor(const StringName &p_id) {
  285. ERR_FAIL_COND_V_MSG(!has_custom_monitor(p_id), Variant(), "Custom monitor with id '" + String(p_id) + "' doesn't exists.");
  286. bool error;
  287. String error_message;
  288. Variant return_value = _monitor_map[p_id].call(error, error_message);
  289. ERR_FAIL_COND_V_MSG(error, return_value, "Error calling from custom monitor '" + String(p_id) + "' to callable: " + error_message);
  290. return return_value;
  291. }
  292. TypedArray<StringName> Performance::get_custom_monitor_names() {
  293. if (!_monitor_map.size()) {
  294. return TypedArray<StringName>();
  295. }
  296. TypedArray<StringName> return_array;
  297. return_array.resize(_monitor_map.size());
  298. int index = 0;
  299. for (KeyValue<StringName, MonitorCall> i : _monitor_map) {
  300. return_array.set(index, i.key);
  301. index++;
  302. }
  303. return return_array;
  304. }
  305. uint64_t Performance::get_monitor_modification_time() {
  306. return _monitor_modification_time;
  307. }
  308. Performance::Performance() {
  309. _process_time = 0;
  310. _physics_process_time = 0;
  311. _navigation_process_time = 0;
  312. _monitor_modification_time = 0;
  313. singleton = this;
  314. }
  315. Performance::MonitorCall::MonitorCall(Callable p_callable, Vector<Variant> p_arguments) {
  316. _callable = p_callable;
  317. _arguments = p_arguments;
  318. }
  319. Performance::MonitorCall::MonitorCall() {
  320. }
  321. Variant Performance::MonitorCall::call(bool &r_error, String &r_error_message) {
  322. Vector<const Variant *> arguments_mem;
  323. arguments_mem.resize(_arguments.size());
  324. for (int i = 0; i < _arguments.size(); i++) {
  325. arguments_mem.write[i] = &_arguments[i];
  326. }
  327. const Variant **args = (const Variant **)arguments_mem.ptr();
  328. int argc = _arguments.size();
  329. Variant return_value;
  330. Callable::CallError error;
  331. _callable.callp(args, argc, return_value, error);
  332. r_error = (error.error != Callable::CallError::CALL_OK);
  333. if (r_error) {
  334. r_error_message = Variant::get_callable_error_text(_callable, args, argc, error);
  335. }
  336. return return_value;
  337. }