performance.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*************************************************************************/
  2. /* performance.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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 "scene/main/node.h"
  34. #include "scene/main/scene_tree.h"
  35. #include "servers/audio_server.h"
  36. #include "servers/physics_server_2d.h"
  37. #include "servers/physics_server_3d.h"
  38. #include "servers/rendering_server.h"
  39. Performance *Performance::singleton = nullptr;
  40. void Performance::_bind_methods() {
  41. ClassDB::bind_method(D_METHOD("get_monitor", "monitor"), &Performance::get_monitor);
  42. ClassDB::bind_method(D_METHOD("add_custom_monitor", "id", "callable", "arguments"), &Performance::add_custom_monitor, DEFVAL(Array()));
  43. ClassDB::bind_method(D_METHOD("remove_custom_monitor", "id"), &Performance::remove_custom_monitor);
  44. ClassDB::bind_method(D_METHOD("has_custom_monitor", "id"), &Performance::has_custom_monitor);
  45. ClassDB::bind_method(D_METHOD("get_custom_monitor", "id"), &Performance::get_custom_monitor);
  46. ClassDB::bind_method(D_METHOD("get_monitor_modification_time"), &Performance::get_monitor_modification_time);
  47. ClassDB::bind_method(D_METHOD("get_custom_monitor_names"), &Performance::get_custom_monitor_names);
  48. BIND_ENUM_CONSTANT(TIME_FPS);
  49. BIND_ENUM_CONSTANT(TIME_PROCESS);
  50. BIND_ENUM_CONSTANT(TIME_PHYSICS_PROCESS);
  51. BIND_ENUM_CONSTANT(MEMORY_STATIC);
  52. BIND_ENUM_CONSTANT(MEMORY_STATIC_MAX);
  53. BIND_ENUM_CONSTANT(MEMORY_MESSAGE_BUFFER_MAX);
  54. BIND_ENUM_CONSTANT(OBJECT_COUNT);
  55. BIND_ENUM_CONSTANT(OBJECT_RESOURCE_COUNT);
  56. BIND_ENUM_CONSTANT(OBJECT_NODE_COUNT);
  57. BIND_ENUM_CONSTANT(OBJECT_ORPHAN_NODE_COUNT);
  58. BIND_ENUM_CONSTANT(RENDER_TOTAL_OBJECTS_IN_FRAME);
  59. BIND_ENUM_CONSTANT(RENDER_TOTAL_PRIMITIVES_IN_FRAME);
  60. BIND_ENUM_CONSTANT(RENDER_TOTAL_DRAW_CALLS_IN_FRAME);
  61. BIND_ENUM_CONSTANT(RENDER_VIDEO_MEM_USED);
  62. BIND_ENUM_CONSTANT(RENDER_TEXTURE_MEM_USED);
  63. BIND_ENUM_CONSTANT(RENDER_BUFFER_MEM_USED);
  64. BIND_ENUM_CONSTANT(PHYSICS_2D_ACTIVE_OBJECTS);
  65. BIND_ENUM_CONSTANT(PHYSICS_2D_COLLISION_PAIRS);
  66. BIND_ENUM_CONSTANT(PHYSICS_2D_ISLAND_COUNT);
  67. BIND_ENUM_CONSTANT(PHYSICS_3D_ACTIVE_OBJECTS);
  68. BIND_ENUM_CONSTANT(PHYSICS_3D_COLLISION_PAIRS);
  69. BIND_ENUM_CONSTANT(PHYSICS_3D_ISLAND_COUNT);
  70. BIND_ENUM_CONSTANT(AUDIO_OUTPUT_LATENCY);
  71. BIND_ENUM_CONSTANT(MONITOR_MAX);
  72. }
  73. int Performance::_get_node_count() const {
  74. MainLoop *ml = OS::get_singleton()->get_main_loop();
  75. SceneTree *sml = Object::cast_to<SceneTree>(ml);
  76. if (!sml) {
  77. return 0;
  78. }
  79. return sml->get_node_count();
  80. }
  81. String Performance::get_monitor_name(Monitor p_monitor) const {
  82. ERR_FAIL_INDEX_V(p_monitor, MONITOR_MAX, String());
  83. static const char *names[MONITOR_MAX] = {
  84. "time/fps",
  85. "time/process",
  86. "time/physics_process",
  87. "memory/static",
  88. "memory/static_max",
  89. "memory/msg_buf_max",
  90. "object/objects",
  91. "object/resources",
  92. "object/nodes",
  93. "object/orphan_nodes",
  94. "raster/total_objects_drawn",
  95. "raster/total_primitives_drawn",
  96. "raster/total_draw_calls",
  97. "video/video_mem",
  98. "video/texture_mem",
  99. "video/buffer_mem",
  100. "physics_2d/active_objects",
  101. "physics_2d/collision_pairs",
  102. "physics_2d/islands",
  103. "physics_3d/active_objects",
  104. "physics_3d/collision_pairs",
  105. "physics_3d/islands",
  106. "audio/driver/output_latency",
  107. };
  108. return names[p_monitor];
  109. }
  110. double Performance::get_monitor(Monitor p_monitor) const {
  111. switch (p_monitor) {
  112. case TIME_FPS:
  113. return Engine::get_singleton()->get_frames_per_second();
  114. case TIME_PROCESS:
  115. return _process_time;
  116. case TIME_PHYSICS_PROCESS:
  117. return _physics_process_time;
  118. case MEMORY_STATIC:
  119. return Memory::get_mem_usage();
  120. case MEMORY_STATIC_MAX:
  121. return Memory::get_mem_max_usage();
  122. case MEMORY_MESSAGE_BUFFER_MAX:
  123. return MessageQueue::get_singleton()->get_max_buffer_usage();
  124. case OBJECT_COUNT:
  125. return ObjectDB::get_object_count();
  126. case OBJECT_RESOURCE_COUNT:
  127. return ResourceCache::get_cached_resource_count();
  128. case OBJECT_NODE_COUNT:
  129. return _get_node_count();
  130. case OBJECT_ORPHAN_NODE_COUNT:
  131. return Node::orphan_node_count;
  132. case RENDER_TOTAL_OBJECTS_IN_FRAME:
  133. return RS::get_singleton()->get_rendering_info(RS::RENDERING_INFO_TOTAL_OBJECTS_IN_FRAME);
  134. case RENDER_TOTAL_PRIMITIVES_IN_FRAME:
  135. return RS::get_singleton()->get_rendering_info(RS::RENDERING_INFO_TOTAL_PRIMITIVES_IN_FRAME);
  136. case RENDER_TOTAL_DRAW_CALLS_IN_FRAME:
  137. return RS::get_singleton()->get_rendering_info(RS::RENDERING_INFO_TOTAL_DRAW_CALLS_IN_FRAME);
  138. case RENDER_VIDEO_MEM_USED:
  139. return RS::get_singleton()->get_rendering_info(RS::RENDERING_INFO_VIDEO_MEM_USED);
  140. case RENDER_TEXTURE_MEM_USED:
  141. return RS::get_singleton()->get_rendering_info(RS::RENDERING_INFO_TEXTURE_MEM_USED);
  142. case RENDER_BUFFER_MEM_USED:
  143. return RS::get_singleton()->get_rendering_info(RS::RENDERING_INFO_BUFFER_MEM_USED);
  144. case PHYSICS_2D_ACTIVE_OBJECTS:
  145. return PhysicsServer2D::get_singleton()->get_process_info(PhysicsServer2D::INFO_ACTIVE_OBJECTS);
  146. case PHYSICS_2D_COLLISION_PAIRS:
  147. return PhysicsServer2D::get_singleton()->get_process_info(PhysicsServer2D::INFO_COLLISION_PAIRS);
  148. case PHYSICS_2D_ISLAND_COUNT:
  149. return PhysicsServer2D::get_singleton()->get_process_info(PhysicsServer2D::INFO_ISLAND_COUNT);
  150. case PHYSICS_3D_ACTIVE_OBJECTS:
  151. return PhysicsServer3D::get_singleton()->get_process_info(PhysicsServer3D::INFO_ACTIVE_OBJECTS);
  152. case PHYSICS_3D_COLLISION_PAIRS:
  153. return PhysicsServer3D::get_singleton()->get_process_info(PhysicsServer3D::INFO_COLLISION_PAIRS);
  154. case PHYSICS_3D_ISLAND_COUNT:
  155. return PhysicsServer3D::get_singleton()->get_process_info(PhysicsServer3D::INFO_ISLAND_COUNT);
  156. case AUDIO_OUTPUT_LATENCY:
  157. return AudioServer::get_singleton()->get_output_latency();
  158. default: {
  159. }
  160. }
  161. return 0;
  162. }
  163. Performance::MonitorType Performance::get_monitor_type(Monitor p_monitor) const {
  164. ERR_FAIL_INDEX_V(p_monitor, MONITOR_MAX, MONITOR_TYPE_QUANTITY);
  165. // ugly
  166. static const MonitorType types[MONITOR_MAX] = {
  167. MONITOR_TYPE_QUANTITY,
  168. MONITOR_TYPE_TIME,
  169. MONITOR_TYPE_TIME,
  170. MONITOR_TYPE_MEMORY,
  171. MONITOR_TYPE_MEMORY,
  172. MONITOR_TYPE_MEMORY,
  173. MONITOR_TYPE_QUANTITY,
  174. MONITOR_TYPE_QUANTITY,
  175. MONITOR_TYPE_QUANTITY,
  176. MONITOR_TYPE_QUANTITY,
  177. MONITOR_TYPE_QUANTITY,
  178. MONITOR_TYPE_QUANTITY,
  179. MONITOR_TYPE_QUANTITY,
  180. MONITOR_TYPE_MEMORY,
  181. MONITOR_TYPE_MEMORY,
  182. MONITOR_TYPE_MEMORY,
  183. MONITOR_TYPE_QUANTITY,
  184. MONITOR_TYPE_QUANTITY,
  185. MONITOR_TYPE_QUANTITY,
  186. MONITOR_TYPE_QUANTITY,
  187. MONITOR_TYPE_QUANTITY,
  188. MONITOR_TYPE_QUANTITY,
  189. MONITOR_TYPE_TIME,
  190. };
  191. return types[p_monitor];
  192. }
  193. void Performance::set_process_time(double p_pt) {
  194. _process_time = p_pt;
  195. }
  196. void Performance::set_physics_process_time(double p_pt) {
  197. _physics_process_time = p_pt;
  198. }
  199. void Performance::add_custom_monitor(const StringName &p_id, const Callable &p_callable, const Vector<Variant> &p_args) {
  200. ERR_FAIL_COND_MSG(has_custom_monitor(p_id), "Custom monitor with id '" + String(p_id) + "' already exists.");
  201. _monitor_map.insert(p_id, MonitorCall(p_callable, p_args));
  202. _monitor_modification_time = OS::get_singleton()->get_ticks_usec();
  203. }
  204. void Performance::remove_custom_monitor(const StringName &p_id) {
  205. ERR_FAIL_COND_MSG(!has_custom_monitor(p_id), "Custom monitor with id '" + String(p_id) + "' doesn't exists.");
  206. _monitor_map.erase(p_id);
  207. _monitor_modification_time = OS::get_singleton()->get_ticks_usec();
  208. }
  209. bool Performance::has_custom_monitor(const StringName &p_id) {
  210. return _monitor_map.has(p_id);
  211. }
  212. Variant Performance::get_custom_monitor(const StringName &p_id) {
  213. ERR_FAIL_COND_V_MSG(!has_custom_monitor(p_id), Variant(), "Custom monitor with id '" + String(p_id) + "' doesn't exists.");
  214. bool error;
  215. String error_message;
  216. Variant return_value = _monitor_map[p_id].call(error, error_message);
  217. ERR_FAIL_COND_V_MSG(error, return_value, "Error calling from custom monitor '" + String(p_id) + "' to callable: " + error_message);
  218. return return_value;
  219. }
  220. Array Performance::get_custom_monitor_names() {
  221. if (!_monitor_map.size()) {
  222. return Array();
  223. }
  224. Array return_array;
  225. return_array.resize(_monitor_map.size());
  226. int index = 0;
  227. for (KeyValue<StringName, MonitorCall> i : _monitor_map) {
  228. return_array.set(index, i.key);
  229. index++;
  230. }
  231. return return_array;
  232. }
  233. uint64_t Performance::get_monitor_modification_time() {
  234. return _monitor_modification_time;
  235. }
  236. Performance::Performance() {
  237. _process_time = 0;
  238. _physics_process_time = 0;
  239. _monitor_modification_time = 0;
  240. singleton = this;
  241. }
  242. Performance::MonitorCall::MonitorCall(Callable p_callable, Vector<Variant> p_arguments) {
  243. _callable = p_callable;
  244. _arguments = p_arguments;
  245. }
  246. Performance::MonitorCall::MonitorCall() {
  247. }
  248. Variant Performance::MonitorCall::call(bool &r_error, String &r_error_message) {
  249. Vector<const Variant *> arguments_mem;
  250. arguments_mem.resize(_arguments.size());
  251. for (int i = 0; i < _arguments.size(); i++) {
  252. arguments_mem.write[i] = &_arguments[i];
  253. }
  254. const Variant **args = (const Variant **)arguments_mem.ptr();
  255. int argc = _arguments.size();
  256. Variant return_value;
  257. Callable::CallError error;
  258. _callable.callp(args, argc, return_value, error);
  259. r_error = (error.error != Callable::CallError::CALL_OK);
  260. if (r_error) {
  261. r_error_message = Variant::get_callable_error_text(_callable, args, argc, error);
  262. }
  263. return return_value;
  264. }