Skip to content

Commit 037b500

Browse files
committed
Allow custom monitors to select desired type
1 parent 2dd26a0 commit 037b500

File tree

7 files changed

+101
-28
lines changed

7 files changed

+101
-28
lines changed

core/debugger/remote_debugger.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,17 @@ class RemoteDebugger::PerformanceProfiler : public EngineProfiler {
6262
last_perf_time = pt;
6363

6464
Array custom_monitor_names = performance->call("get_custom_monitor_names");
65+
Array custom_monitor_types = performance->call("get_custom_monitor_types");
66+
67+
Array custom_monitor_data;
68+
custom_monitor_data.clear();
69+
custom_monitor_data.append_array(custom_monitor_names);
70+
custom_monitor_data.append_array(custom_monitor_types);
6571

6672
uint64_t monitor_modification_time = performance->call("get_monitor_modification_time");
6773
if (monitor_modification_time > last_monitor_modification_time) {
6874
last_monitor_modification_time = monitor_modification_time;
69-
EngineDebugger::get_singleton()->send_message("performance:profile_names", custom_monitor_names);
75+
EngineDebugger::get_singleton()->send_message("performance:profile_names", custom_monitor_data);
7076
}
7177

7278
int max = performance->get("MONITOR_MAX");

doc/classes/Performance.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
<param index="0" name="id" type="StringName" />
1919
<param index="1" name="callable" type="Callable" />
2020
<param index="2" name="arguments" type="Array" default="[]" />
21+
<param index="3" name="type" type="int" enum="Performance.MonitorType" default="0" />
2122
<description>
2223
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.
2324
[codeblocks]
@@ -84,6 +85,12 @@
8485
Returns the names of active custom monitors in an [Array].
8586
</description>
8687
</method>
88+
<method name="get_custom_monitor_types">
89+
<return type="int[]" />
90+
<description>
91+
Returns the [enum MonitorType] values of active custom monitors in an [Array].
92+
</description>
93+
</method>
8794
<method name="get_monitor" qualifiers="const">
8895
<return type="float" />
8996
<param index="0" name="monitor" type="int" enum="Performance.Monitor" />
@@ -302,5 +309,17 @@
302309
<constant name="MONITOR_MAX" value="59" enum="Monitor">
303310
Represents the size of the [enum Monitor] enum.
304311
</constant>
312+
<constant name="MONITOR_TYPE_QUANTITY" value="0" enum="MonitorType">
313+
Monitor output is formatted as an integer value.
314+
</constant>
315+
<constant name="MONITOR_TYPE_MEMORY" value="1" enum="MonitorType">
316+
Monitor output is formatted as computer memory, submitted values should represent number of bytes.
317+
</constant>
318+
<constant name="MONITOR_TYPE_TIME" value="2" enum="MonitorType">
319+
Monitor output is formatted as time in milliseconds, submitted values should represent time in seconds.
320+
</constant>
321+
<constant name="MONITOR_TYPE_PERCENTAGE" value="3" enum="MonitorType">
322+
Monitor output is formatted as a percentage.
323+
</constant>
305324
</constants>
306325
</class>

editor/debugger/editor_performance_profiler.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ String EditorPerformanceProfiler::_create_label(float p_value, Performance::Moni
8888
case Performance::MONITOR_TYPE_TIME: {
8989
return TS->format_number(rtos(p_value * 1000).pad_decimals(2)) + " " + TTR("ms");
9090
}
91+
case Performance::MONITOR_TYPE_PERCENTAGE: {
92+
return TS->format_number(rtos(p_value).pad_decimals(2)) + "%";
93+
}
9194
default: {
9295
return TS->format_number(rtos(p_value));
9396
}
@@ -317,7 +320,7 @@ void EditorPerformanceProfiler::reset() {
317320
monitor_draw->queue_redraw();
318321
}
319322

320-
void EditorPerformanceProfiler::update_monitors(const Vector<StringName> &p_names) {
323+
void EditorPerformanceProfiler::update_monitors(const Vector<StringName> &p_names, const PackedInt32Array &p_types) {
321324
HashMap<StringName, int> names;
322325
for (int i = 0; i < p_names.size(); i++) {
323326
names.insert("custom:" + p_names[i], Performance::MONITOR_MAX + i);
@@ -340,14 +343,17 @@ void EditorPerformanceProfiler::update_monitors(const Vector<StringName> &p_name
340343
}
341344
}
342345

346+
int index = 0;
343347
for (const KeyValue<StringName, int> &E : names) {
344348
String name = String(E.key).replace_first("custom:", "");
345349
String base = "Custom";
346350
if (name.get_slice_count("/") == 2) {
347351
base = name.get_slicec('/', 0);
348352
name = name.get_slicec('/', 1);
349353
}
350-
monitors.insert(E.key, Monitor(name, base, E.value, Performance::MONITOR_TYPE_QUANTITY, nullptr));
354+
Performance::MonitorType type = Performance::MonitorType(p_types[index]);
355+
monitors.insert(E.key, Monitor(name, base, E.value, type, nullptr));
356+
index++;
351357
}
352358

353359
_build_monitor_tree();

editor/debugger/editor_performance_profiler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class EditorPerformanceProfiler : public HSplitContainer {
8282

8383
public:
8484
void reset();
85-
void update_monitors(const Vector<StringName> &p_names);
85+
void update_monitors(const Vector<StringName> &p_names, const PackedInt32Array &p_types);
8686
void add_profile_frame(const Vector<float> &p_values);
8787
List<float> *get_monitor_data(const StringName &p_name);
8888
EditorPerformanceProfiler();

editor/debugger/script_editor_debugger.cpp

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -904,13 +904,24 @@ void ScriptEditorDebugger::_msg_show_selection_limit_warning(uint64_t p_thread_i
904904
}
905905

906906
void ScriptEditorDebugger::_msg_performance_profile_names(uint64_t p_thread_id, const Array &p_data) {
907+
Array name_data = p_data.slice(0, p_data.size() / 2);
908+
Array type_data = p_data.slice(p_data.size() / 2);
909+
907910
Vector<StringName> monitors;
908-
monitors.resize(p_data.size());
909-
for (int i = 0; i < p_data.size(); i++) {
910-
ERR_FAIL_COND(p_data[i].get_type() != Variant::STRING_NAME);
911-
monitors.set(i, p_data[i]);
911+
monitors.resize(name_data.size());
912+
for (int i = 0; i < name_data.size(); i++) {
913+
ERR_FAIL_COND(name_data[i].get_type() != Variant::STRING_NAME);
914+
monitors.set(i, name_data[i]);
912915
}
913-
performance_profiler->update_monitors(monitors);
916+
917+
PackedInt32Array types;
918+
types.resize(type_data.size());
919+
for (int i = 0; i < type_data.size(); i++) {
920+
ERR_FAIL_COND(type_data[i].get_type() != Variant::INT);
921+
types.set(i, type_data[i]);
922+
}
923+
924+
performance_profiler->update_monitors(monitors, types);
914925
}
915926

916927
void ScriptEditorDebugger::_msg_filesystem_update_file(uint64_t p_thread_id, const Array &p_data) {

main/performance.cpp

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,13 @@ Performance *Performance::singleton = nullptr;
5555

5656
void Performance::_bind_methods() {
5757
ClassDB::bind_method(D_METHOD("get_monitor", "monitor"), &Performance::get_monitor);
58-
ClassDB::bind_method(D_METHOD("add_custom_monitor", "id", "callable", "arguments"), &Performance::add_custom_monitor, DEFVAL(Array()));
58+
ClassDB::bind_method(D_METHOD("add_custom_monitor", "id", "callable", "arguments", "type"), &Performance::add_custom_monitor, DEFVAL(Array()), DEFVAL(MONITOR_TYPE_QUANTITY));
5959
ClassDB::bind_method(D_METHOD("remove_custom_monitor", "id"), &Performance::remove_custom_monitor);
6060
ClassDB::bind_method(D_METHOD("has_custom_monitor", "id"), &Performance::has_custom_monitor);
6161
ClassDB::bind_method(D_METHOD("get_custom_monitor", "id"), &Performance::get_custom_monitor);
6262
ClassDB::bind_method(D_METHOD("get_monitor_modification_time"), &Performance::get_monitor_modification_time);
6363
ClassDB::bind_method(D_METHOD("get_custom_monitor_names"), &Performance::get_custom_monitor_names);
64+
ClassDB::bind_method(D_METHOD("get_custom_monitor_types"), &Performance::get_custom_monitor_types);
6465

6566
BIND_ENUM_CONSTANT(TIME_FPS);
6667
BIND_ENUM_CONSTANT(TIME_PROCESS);
@@ -130,6 +131,11 @@ void Performance::_bind_methods() {
130131
BIND_ENUM_CONSTANT(NAVIGATION_3D_OBSTACLE_COUNT);
131132
#endif // NAVIGATION_3D_DISABLED
132133
BIND_ENUM_CONSTANT(MONITOR_MAX);
134+
135+
BIND_ENUM_CONSTANT(MONITOR_TYPE_QUANTITY);
136+
BIND_ENUM_CONSTANT(MONITOR_TYPE_MEMORY);
137+
BIND_ENUM_CONSTANT(MONITOR_TYPE_TIME);
138+
BIND_ENUM_CONSTANT(MONITOR_TYPE_PERCENTAGE);
133139
}
134140

135141
int Performance::_get_node_count() const {
@@ -522,9 +528,9 @@ void Performance::set_navigation_process_time(double p_pt) {
522528
_navigation_process_time = p_pt;
523529
}
524530

525-
void Performance::add_custom_monitor(const StringName &p_id, const Callable &p_callable, const Vector<Variant> &p_args) {
531+
void Performance::add_custom_monitor(const StringName &p_id, const Callable &p_callable, const Vector<Variant> &p_args, MonitorType p_type = MONITOR_TYPE_QUANTITY) {
526532
ERR_FAIL_COND_MSG(has_custom_monitor(p_id), "Custom monitor with id '" + String(p_id) + "' already exists.");
527-
_monitor_map.insert(p_id, MonitorCall(p_callable, p_args));
533+
_monitor_map.insert(p_id, MonitorCall(p_type, p_callable, p_args));
528534
_monitor_modification_time = OS::get_singleton()->get_ticks_usec();
529535
}
530536

@@ -561,6 +567,20 @@ TypedArray<StringName> Performance::get_custom_monitor_names() {
561567
return return_array;
562568
}
563569

570+
TypedArray<int> Performance::get_custom_monitor_types() {
571+
if (!_monitor_map.size()) {
572+
return TypedArray<int>();
573+
}
574+
TypedArray<int> return_array;
575+
return_array.resize(_monitor_map.size());
576+
int index = 0;
577+
for (KeyValue<StringName, MonitorCall> i : _monitor_map) {
578+
return_array.set(index, (int)i.value.get_monitor_type());
579+
index++;
580+
}
581+
return return_array;
582+
}
583+
564584
uint64_t Performance::get_monitor_modification_time() {
565585
return _monitor_modification_time;
566586
}
@@ -573,7 +593,8 @@ Performance::Performance() {
573593
singleton = this;
574594
}
575595

576-
Performance::MonitorCall::MonitorCall(Callable p_callable, Vector<Variant> p_arguments) {
596+
Performance::MonitorCall::MonitorCall(Performance::MonitorType p_type, Callable p_callable, Vector<Variant> p_arguments) {
597+
_type = p_type;
577598
_callable = p_callable;
578599
_arguments = p_arguments;
579600
}
@@ -598,3 +619,7 @@ Variant Performance::MonitorCall::call(bool &r_error, String &r_error_message) {
598619
}
599620
return return_value;
600621
}
622+
623+
Performance::MonitorType Performance::MonitorCall::get_monitor_type() {
624+
return _type;
625+
}

main/performance.h

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,6 @@ class Performance : public Object {
5151
double _physics_process_time;
5252
double _navigation_process_time;
5353

54-
class MonitorCall {
55-
Callable _callable;
56-
Vector<Variant> _arguments;
57-
58-
public:
59-
MonitorCall(Callable p_callable, Vector<Variant> p_arguments);
60-
MonitorCall();
61-
Variant call(bool &r_error, String &r_error_message);
62-
};
63-
64-
HashMap<StringName, MonitorCall> _monitor_map;
65-
uint64_t _monitor_modification_time;
66-
6754
public:
6855
enum Monitor {
6956
TIME_FPS,
@@ -131,7 +118,8 @@ class Performance : public Object {
131118
enum MonitorType {
132119
MONITOR_TYPE_QUANTITY,
133120
MONITOR_TYPE_MEMORY,
134-
MONITOR_TYPE_TIME
121+
MONITOR_TYPE_TIME,
122+
MONITOR_TYPE_PERCENTAGE
135123
};
136124

137125
double get_monitor(Monitor p_monitor) const;
@@ -143,17 +131,35 @@ class Performance : public Object {
143131
void set_physics_process_time(double p_pt);
144132
void set_navigation_process_time(double p_pt);
145133

146-
void add_custom_monitor(const StringName &p_id, const Callable &p_callable, const Vector<Variant> &p_args);
134+
void add_custom_monitor(const StringName &p_id, const Callable &p_callable, const Vector<Variant> &p_args, MonitorType p_type);
147135
void remove_custom_monitor(const StringName &p_id);
148136
bool has_custom_monitor(const StringName &p_id);
149137
Variant get_custom_monitor(const StringName &p_id);
150138
TypedArray<StringName> get_custom_monitor_names();
139+
TypedArray<int> get_custom_monitor_types();
151140

152141
uint64_t get_monitor_modification_time();
153142

154143
static Performance *get_singleton() { return singleton; }
155144

156145
Performance();
146+
147+
private:
148+
class MonitorCall {
149+
MonitorType _type = MONITOR_TYPE_QUANTITY;
150+
Callable _callable;
151+
Vector<Variant> _arguments;
152+
153+
public:
154+
MonitorCall(MonitorType p_type, Callable p_callable, Vector<Variant> p_arguments);
155+
MonitorCall();
156+
Variant call(bool &r_error, String &r_error_message);
157+
MonitorType get_monitor_type();
158+
};
159+
160+
HashMap<StringName, MonitorCall> _monitor_map;
161+
uint64_t _monitor_modification_time;
157162
};
158163

159164
VARIANT_ENUM_CAST(Performance::Monitor);
165+
VARIANT_ENUM_CAST(Performance::MonitorType);

0 commit comments

Comments
 (0)