From 1a8306bbc12f531d9ec894413f24316510694a19 Mon Sep 17 00:00:00 2001 From: devloglogan Date: Tue, 9 Sep 2025 21:34:22 -0500 Subject: [PATCH] Allow custom monitors to select desired type --- core/debugger/remote_debugger.cpp | 7 +++- doc/classes/Performance.xml | 19 +++++++++ .../debugger/editor_performance_profiler.cpp | 10 ++++- editor/debugger/editor_performance_profiler.h | 2 +- editor/debugger/script_editor_debugger.cpp | 22 +++++++--- main/performance.compat.inc | 41 +++++++++++++++++++ main/performance.cpp | 30 ++++++++++++-- main/performance.h | 41 ++++++++++++------- .../4.5-stable.expected | 7 ++++ 9 files changed, 151 insertions(+), 28 deletions(-) create mode 100644 main/performance.compat.inc diff --git a/core/debugger/remote_debugger.cpp b/core/debugger/remote_debugger.cpp index 40073b21490..d20280ce7bf 100644 --- a/core/debugger/remote_debugger.cpp +++ b/core/debugger/remote_debugger.cpp @@ -62,11 +62,16 @@ public: last_perf_time = pt; Array custom_monitor_names = performance->call("get_custom_monitor_names"); + Array custom_monitor_types = performance->call("get_custom_monitor_types"); + + Array custom_monitor_data; + custom_monitor_data.push_back(custom_monitor_names); + custom_monitor_data.push_back(custom_monitor_types); uint64_t monitor_modification_time = performance->call("get_monitor_modification_time"); if (monitor_modification_time > last_monitor_modification_time) { last_monitor_modification_time = monitor_modification_time; - EngineDebugger::get_singleton()->send_message("performance:profile_names", custom_monitor_names); + EngineDebugger::get_singleton()->send_message("performance:profile_names", custom_monitor_data); } int max = performance->get("MONITOR_MAX"); diff --git a/doc/classes/Performance.xml b/doc/classes/Performance.xml index 6925e3d70a8..d9dd5b32b27 100644 --- a/doc/classes/Performance.xml +++ b/doc/classes/Performance.xml @@ -18,6 +18,7 @@ + Adds a custom monitor with the name [param id]. You can specify the category of the monitor using slash delimiters in [param id] (for example: [code]"Game/NumberOfNPCs"[/code]). If there is more than one slash delimiter, then the default category is used. The default category is [code]"Custom"[/code]. Prints an error if given [param id] is already present. [codeblocks] @@ -84,6 +85,12 @@ Returns the names of active custom monitors in an [Array]. + + + + Returns the [enum MonitorType] values of active custom monitors in an [Array]. + + @@ -303,5 +310,17 @@ Represents the size of the [enum Monitor] enum. + + Monitor output is formatted as an integer value. + + + Monitor output is formatted as computer memory. Submitted values should represent a number of bytes. + + + Monitor output is formatted as time in milliseconds. Submitted values should represent a time in seconds (not milliseconds). + + + Monitor output is formatted as a percentage. Submitted values should represent a fractional value rather than the percentage directly, e.g. [code]0.5[/code] for [code]50.00%[/code]. + diff --git a/editor/debugger/editor_performance_profiler.cpp b/editor/debugger/editor_performance_profiler.cpp index 6cfa91391ba..6ca7c5d53b1 100644 --- a/editor/debugger/editor_performance_profiler.cpp +++ b/editor/debugger/editor_performance_profiler.cpp @@ -88,6 +88,9 @@ String EditorPerformanceProfiler::_create_label(float p_value, Performance::Moni case Performance::MONITOR_TYPE_TIME: { return TS->format_number(rtos(p_value * 1000).pad_decimals(2)) + " " + TTR("ms"); } + case Performance::MONITOR_TYPE_PERCENTAGE: { + return TS->format_number(rtos(p_value * 100).pad_decimals(2)) + "%"; + } default: { return TS->format_number(rtos(p_value)); } @@ -317,7 +320,7 @@ void EditorPerformanceProfiler::reset() { monitor_draw->queue_redraw(); } -void EditorPerformanceProfiler::update_monitors(const Vector &p_names) { +void EditorPerformanceProfiler::update_monitors(const Vector &p_names, const PackedInt32Array &p_types) { HashMap names; for (int i = 0; i < p_names.size(); i++) { names.insert("custom:" + p_names[i], Performance::MONITOR_MAX + i); @@ -340,6 +343,7 @@ void EditorPerformanceProfiler::update_monitors(const Vector &p_name } } + int index = 0; for (const KeyValue &E : names) { String name = String(E.key).replace_first("custom:", ""); String base = "Custom"; @@ -347,7 +351,9 @@ void EditorPerformanceProfiler::update_monitors(const Vector &p_name base = name.get_slicec('/', 0); name = name.get_slicec('/', 1); } - monitors.insert(E.key, Monitor(name, base, E.value, Performance::MONITOR_TYPE_QUANTITY, nullptr)); + Performance::MonitorType type = Performance::MonitorType(p_types[index]); + monitors.insert(E.key, Monitor(name, base, E.value, type, nullptr)); + index++; } _build_monitor_tree(); diff --git a/editor/debugger/editor_performance_profiler.h b/editor/debugger/editor_performance_profiler.h index 6b6710382b6..5f2b31e8a16 100644 --- a/editor/debugger/editor_performance_profiler.h +++ b/editor/debugger/editor_performance_profiler.h @@ -82,7 +82,7 @@ protected: public: void reset(); - void update_monitors(const Vector &p_names); + void update_monitors(const Vector &p_names, const PackedInt32Array &p_types); void add_profile_frame(const Vector &p_values); List *get_monitor_data(const StringName &p_name); EditorPerformanceProfiler(); diff --git a/editor/debugger/script_editor_debugger.cpp b/editor/debugger/script_editor_debugger.cpp index dedc3d79d54..8b009ff918e 100644 --- a/editor/debugger/script_editor_debugger.cpp +++ b/editor/debugger/script_editor_debugger.cpp @@ -912,13 +912,25 @@ void ScriptEditorDebugger::_msg_show_selection_limit_warning(uint64_t p_thread_i } void ScriptEditorDebugger::_msg_performance_profile_names(uint64_t p_thread_id, const Array &p_data) { + ERR_FAIL_COND(p_data.size() != 2); + Array name_data = p_data[0]; + Array type_data = p_data[1]; + Vector monitors; - monitors.resize(p_data.size()); - for (int i = 0; i < p_data.size(); i++) { - ERR_FAIL_COND(p_data[i].get_type() != Variant::STRING_NAME); - monitors.set(i, p_data[i]); + monitors.resize(name_data.size()); + for (int i = 0; i < name_data.size(); i++) { + ERR_FAIL_COND(name_data[i].get_type() != Variant::STRING_NAME); + monitors.set(i, name_data[i]); } - performance_profiler->update_monitors(monitors); + + PackedInt32Array types; + types.resize(type_data.size()); + for (int i = 0; i < type_data.size(); i++) { + ERR_FAIL_COND(type_data[i].get_type() != Variant::INT); + types.set(i, type_data[i]); + } + + performance_profiler->update_monitors(monitors, types); } void ScriptEditorDebugger::_msg_filesystem_update_file(uint64_t p_thread_id, const Array &p_data) { diff --git a/main/performance.compat.inc b/main/performance.compat.inc new file mode 100644 index 00000000000..5046590e37b --- /dev/null +++ b/main/performance.compat.inc @@ -0,0 +1,41 @@ +/**************************************************************************/ +/* performance.compat.inc */ +/**************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/**************************************************************************/ +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/**************************************************************************/ + +#ifndef DISABLE_DEPRECATED + +void Performance::_add_custom_monitor_bind_compat_110433(const StringName &p_id, const Callable &p_callable, const Vector &p_args) { + add_custom_monitor(p_id, p_callable, p_args, MONITOR_TYPE_QUANTITY); +} + +void Performance::_bind_compatibility_methods() { + ClassDB::bind_compatibility_method(D_METHOD("add_custom_monitor", "id", "callable", "arguments"), &Performance::_add_custom_monitor_bind_compat_110433, DEFVAL(Array())); +} + +#endif diff --git a/main/performance.cpp b/main/performance.cpp index 799aee0a183..0e32c9a8485 100644 --- a/main/performance.cpp +++ b/main/performance.cpp @@ -29,6 +29,7 @@ /**************************************************************************/ #include "performance.h" +#include "performance.compat.inc" #include "core/os/os.h" #include "core/variant/typed_array.h" @@ -57,12 +58,13 @@ Performance *Performance::singleton = nullptr; void Performance::_bind_methods() { ClassDB::bind_method(D_METHOD("get_monitor", "monitor"), &Performance::get_monitor); - ClassDB::bind_method(D_METHOD("add_custom_monitor", "id", "callable", "arguments"), &Performance::add_custom_monitor, DEFVAL(Array())); + ClassDB::bind_method(D_METHOD("add_custom_monitor", "id", "callable", "arguments", "type"), &Performance::add_custom_monitor, DEFVAL(Array()), DEFVAL(MONITOR_TYPE_QUANTITY)); ClassDB::bind_method(D_METHOD("remove_custom_monitor", "id"), &Performance::remove_custom_monitor); ClassDB::bind_method(D_METHOD("has_custom_monitor", "id"), &Performance::has_custom_monitor); ClassDB::bind_method(D_METHOD("get_custom_monitor", "id"), &Performance::get_custom_monitor); ClassDB::bind_method(D_METHOD("get_monitor_modification_time"), &Performance::get_monitor_modification_time); ClassDB::bind_method(D_METHOD("get_custom_monitor_names"), &Performance::get_custom_monitor_names); + ClassDB::bind_method(D_METHOD("get_custom_monitor_types"), &Performance::get_custom_monitor_types); BIND_ENUM_CONSTANT(TIME_FPS); BIND_ENUM_CONSTANT(TIME_PROCESS); @@ -132,6 +134,11 @@ void Performance::_bind_methods() { BIND_ENUM_CONSTANT(NAVIGATION_3D_OBSTACLE_COUNT); #endif // NAVIGATION_3D_DISABLED BIND_ENUM_CONSTANT(MONITOR_MAX); + + BIND_ENUM_CONSTANT(MONITOR_TYPE_QUANTITY); + BIND_ENUM_CONSTANT(MONITOR_TYPE_MEMORY); + BIND_ENUM_CONSTANT(MONITOR_TYPE_TIME); + BIND_ENUM_CONSTANT(MONITOR_TYPE_PERCENTAGE); } int Performance::_get_node_count() const { @@ -537,9 +544,9 @@ void Performance::set_navigation_process_time(double p_pt) { _navigation_process_time = p_pt; } -void Performance::add_custom_monitor(const StringName &p_id, const Callable &p_callable, const Vector &p_args) { +void Performance::add_custom_monitor(const StringName &p_id, const Callable &p_callable, const Vector &p_args, MonitorType p_type) { ERR_FAIL_COND_MSG(has_custom_monitor(p_id), "Custom monitor with id '" + String(p_id) + "' already exists."); - _monitor_map.insert(p_id, MonitorCall(p_callable, p_args)); + _monitor_map.insert(p_id, MonitorCall(p_type, p_callable, p_args)); _monitor_modification_time = OS::get_singleton()->get_ticks_usec(); } @@ -576,6 +583,20 @@ TypedArray Performance::get_custom_monitor_names() { return return_array; } +Vector Performance::get_custom_monitor_types() { + if (_monitor_map.is_empty()) { + return Vector(); + } + Vector ret; + ret.resize(_monitor_map.size()); + int index = 0; + for (const KeyValue &i : _monitor_map) { + ret.set(index, (int)i.value.get_monitor_type()); + index++; + } + return ret; +} + uint64_t Performance::get_monitor_modification_time() { return _monitor_modification_time; } @@ -588,7 +609,8 @@ Performance::Performance() { singleton = this; } -Performance::MonitorCall::MonitorCall(Callable p_callable, Vector p_arguments) { +Performance::MonitorCall::MonitorCall(Performance::MonitorType p_type, const Callable &p_callable, const Vector &p_arguments) { + _type = p_type; _callable = p_callable; _arguments = p_arguments; } diff --git a/main/performance.h b/main/performance.h index b71ec1af234..f3d0668ff40 100644 --- a/main/performance.h +++ b/main/performance.h @@ -45,6 +45,11 @@ class Performance : public Object { static Performance *singleton; static void _bind_methods(); +#ifndef DISABLE_DEPRECATED + void _add_custom_monitor_bind_compat_110433(const StringName &p_id, const Callable &p_callable, const Vector &p_args); + static void _bind_compatibility_methods(); +#endif + int _get_node_count() const; int _get_orphan_node_count() const; @@ -52,19 +57,6 @@ class Performance : public Object { double _physics_process_time; double _navigation_process_time; - class MonitorCall { - Callable _callable; - Vector _arguments; - - public: - MonitorCall(Callable p_callable, Vector p_arguments); - MonitorCall(); - Variant call(bool &r_error, String &r_error_message); - }; - - HashMap _monitor_map; - uint64_t _monitor_modification_time; - public: enum Monitor { TIME_FPS, @@ -135,7 +127,8 @@ public: enum MonitorType { MONITOR_TYPE_QUANTITY, MONITOR_TYPE_MEMORY, - MONITOR_TYPE_TIME + MONITOR_TYPE_TIME, + MONITOR_TYPE_PERCENTAGE, }; double get_monitor(Monitor p_monitor) const; @@ -147,17 +140,35 @@ public: void set_physics_process_time(double p_pt); void set_navigation_process_time(double p_pt); - void add_custom_monitor(const StringName &p_id, const Callable &p_callable, const Vector &p_args); + void add_custom_monitor(const StringName &p_id, const Callable &p_callable, const Vector &p_args, MonitorType p_type = MONITOR_TYPE_QUANTITY); void remove_custom_monitor(const StringName &p_id); bool has_custom_monitor(const StringName &p_id); Variant get_custom_monitor(const StringName &p_id); TypedArray get_custom_monitor_names(); + Vector get_custom_monitor_types(); uint64_t get_monitor_modification_time(); static Performance *get_singleton() { return singleton; } Performance(); + +private: + class MonitorCall { + MonitorType _type = MONITOR_TYPE_QUANTITY; + Callable _callable; + Vector _arguments; + + public: + MonitorCall(MonitorType p_type, const Callable &p_callable, const Vector &p_arguments); + MonitorCall(); + Variant call(bool &r_error, String &r_error_message); + inline MonitorType get_monitor_type() const { return _type; } + }; + + HashMap _monitor_map; + uint64_t _monitor_modification_time; }; VARIANT_ENUM_CAST(Performance::Monitor); +VARIANT_ENUM_CAST(Performance::MonitorType); diff --git a/misc/extension_api_validation/4.5-stable.expected b/misc/extension_api_validation/4.5-stable.expected index 10389196aa1..4fef411d057 100644 --- a/misc/extension_api_validation/4.5-stable.expected +++ b/misc/extension_api_validation/4.5-stable.expected @@ -61,3 +61,10 @@ GH-111439 Validate extension JSON: Error: Field 'classes/FileDialog/methods/add_filter/arguments': size changed value in new API, from 2 to 3. Optional argument added. Compatibility method registered. + + +GH-110433 +--------- +Validate extension JSON: Error: Field 'classes/Performance/methods/add_custom_monitor/arguments': size changed value in new API, from 3 to 4. + +Optional argument added. Compatibility method registered.