performance.cpp 14 KB

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