performance.cpp 11 KB

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