Skip to content

Commit 30d9b3d

Browse files
committed
Allow custom monitors to select desired type
1 parent 99a39ce commit 30d9b3d

File tree

9 files changed

+153
-28
lines changed

9 files changed

+153
-28
lines changed

core/debugger/remote_debugger.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,16 @@ 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.push_back(custom_monitor_names);
69+
custom_monitor_data.push_back(custom_monitor_types);
6570

6671
uint64_t monitor_modification_time = performance->call("get_monitor_modification_time");
6772
if (monitor_modification_time > last_monitor_modification_time) {
6873
last_monitor_modification_time = monitor_modification_time;
69-
EngineDebugger::get_singleton()->send_message("performance:profile_names", custom_monitor_names);
74+
EngineDebugger::get_singleton()->send_message("performance:profile_names", custom_monitor_data);
7075
}
7176

7277
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 a 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 a time in seconds (not milliseconds).
320+
</constant>
321+
<constant name="MONITOR_TYPE_PERCENTAGE" value="3" enum="MonitorType">
322+
Monitor output is formatted as a percentage. Submitted values should represent the percentage directly rather than a fraction, e.g. [code]100.0[/code] for [code]100.00%[/code].
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[0];
908+
Array type_data = p_data[1];
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.compat.inc

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**************************************************************************/
2+
/* performance.compat.inc */
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+
31+
#ifndef DISABLE_DEPRECATED
32+
33+
void Performance::_add_custom_monitor_bind_compat_110433(const StringName &p_id, const Callable &p_callable, const Vector<Variant> &p_args) {
34+
add_custom_monitor(p_id, p_callable, p_args, MONITOR_TYPE_QUANTITY);
35+
}
36+
37+
void Performance::_bind_compatibility_methods() {
38+
ClassDB::bind_compatibility_method(D_METHOD("add_custom_monitor", "id", "callable", "arguments"), &Performance::_add_custom_monitor_bind_compat_110433, DEFVAL(Array()));
39+
}
40+
41+
#endif

main/performance.cpp

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
/**************************************************************************/
3030

3131
#include "performance.h"
32+
#include "performance.compat.inc"
3233

3334
#include "core/os/os.h"
3435
#include "core/variant/typed_array.h"
@@ -55,12 +56,13 @@ Performance *Performance::singleton = nullptr;
5556

5657
void Performance::_bind_methods() {
5758
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()));
59+
ClassDB::bind_method(D_METHOD("add_custom_monitor", "id", "callable", "arguments", "type"), &Performance::add_custom_monitor, DEFVAL(Array()), DEFVAL(MONITOR_TYPE_QUANTITY));
5960
ClassDB::bind_method(D_METHOD("remove_custom_monitor", "id"), &Performance::remove_custom_monitor);
6061
ClassDB::bind_method(D_METHOD("has_custom_monitor", "id"), &Performance::has_custom_monitor);
6162
ClassDB::bind_method(D_METHOD("get_custom_monitor", "id"), &Performance::get_custom_monitor);
6263
ClassDB::bind_method(D_METHOD("get_monitor_modification_time"), &Performance::get_monitor_modification_time);
6364
ClassDB::bind_method(D_METHOD("get_custom_monitor_names"), &Performance::get_custom_monitor_names);
65+
ClassDB::bind_method(D_METHOD("get_custom_monitor_types"), &Performance::get_custom_monitor_types);
6466

6567
BIND_ENUM_CONSTANT(TIME_FPS);
6668
BIND_ENUM_CONSTANT(TIME_PROCESS);
@@ -130,6 +132,11 @@ void Performance::_bind_methods() {
130132
BIND_ENUM_CONSTANT(NAVIGATION_3D_OBSTACLE_COUNT);
131133
#endif // NAVIGATION_3D_DISABLED
132134
BIND_ENUM_CONSTANT(MONITOR_MAX);
135+
136+
BIND_ENUM_CONSTANT(MONITOR_TYPE_QUANTITY);
137+
BIND_ENUM_CONSTANT(MONITOR_TYPE_MEMORY);
138+
BIND_ENUM_CONSTANT(MONITOR_TYPE_TIME);
139+
BIND_ENUM_CONSTANT(MONITOR_TYPE_PERCENTAGE);
133140
}
134141

135142
int Performance::_get_node_count() const {
@@ -522,9 +529,9 @@ void Performance::set_navigation_process_time(double p_pt) {
522529
_navigation_process_time = p_pt;
523530
}
524531

525-
void Performance::add_custom_monitor(const StringName &p_id, const Callable &p_callable, const Vector<Variant> &p_args) {
532+
void Performance::add_custom_monitor(const StringName &p_id, const Callable &p_callable, const Vector<Variant> &p_args, MonitorType p_type = MONITOR_TYPE_QUANTITY) {
526533
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));
534+
_monitor_map.insert(p_id, MonitorCall(p_type, p_callable, p_args));
528535
_monitor_modification_time = OS::get_singleton()->get_ticks_usec();
529536
}
530537

@@ -561,6 +568,20 @@ TypedArray<StringName> Performance::get_custom_monitor_names() {
561568
return return_array;
562569
}
563570

571+
TypedArray<int> Performance::get_custom_monitor_types() {
572+
if (!_monitor_map.size()) {
573+
return TypedArray<int>();
574+
}
575+
TypedArray<int> return_array;
576+
return_array.resize(_monitor_map.size());
577+
int index = 0;
578+
for (KeyValue<StringName, MonitorCall> i : _monitor_map) {
579+
return_array.set(index, (int)i.value.get_monitor_type());
580+
index++;
581+
}
582+
return return_array;
583+
}
584+
564585
uint64_t Performance::get_monitor_modification_time() {
565586
return _monitor_modification_time;
566587
}
@@ -573,7 +594,8 @@ Performance::Performance() {
573594
singleton = this;
574595
}
575596

576-
Performance::MonitorCall::MonitorCall(Callable p_callable, Vector<Variant> p_arguments) {
597+
Performance::MonitorCall::MonitorCall(Performance::MonitorType p_type, Callable p_callable, Vector<Variant> p_arguments) {
598+
_type = p_type;
577599
_callable = p_callable;
578600
_arguments = p_arguments;
579601
}
@@ -598,3 +620,7 @@ Variant Performance::MonitorCall::call(bool &r_error, String &r_error_message) {
598620
}
599621
return return_value;
600622
}
623+
624+
Performance::MonitorType Performance::MonitorCall::get_monitor_type() {
625+
return _type;
626+
}

main/performance.h

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,25 +45,17 @@ class Performance : public Object {
4545
static Performance *singleton;
4646
static void _bind_methods();
4747

48+
#ifndef DISABLE_DEPRECATED
49+
void _add_custom_monitor_bind_compat_110433(const StringName &p_id, const Callable &p_callable, const Vector<Variant> &p_args);
50+
static void _bind_compatibility_methods();
51+
#endif
52+
4853
int _get_node_count() const;
4954

5055
double _process_time;
5156
double _physics_process_time;
5257
double _navigation_process_time;
5358

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-
6759
public:
6860
enum Monitor {
6961
TIME_FPS,
@@ -131,7 +123,8 @@ class Performance : public Object {
131123
enum MonitorType {
132124
MONITOR_TYPE_QUANTITY,
133125
MONITOR_TYPE_MEMORY,
134-
MONITOR_TYPE_TIME
126+
MONITOR_TYPE_TIME,
127+
MONITOR_TYPE_PERCENTAGE
135128
};
136129

137130
double get_monitor(Monitor p_monitor) const;
@@ -143,17 +136,35 @@ class Performance : public Object {
143136
void set_physics_process_time(double p_pt);
144137
void set_navigation_process_time(double p_pt);
145138

146-
void add_custom_monitor(const StringName &p_id, const Callable &p_callable, const Vector<Variant> &p_args);
139+
void add_custom_monitor(const StringName &p_id, const Callable &p_callable, const Vector<Variant> &p_args, MonitorType p_type);
147140
void remove_custom_monitor(const StringName &p_id);
148141
bool has_custom_monitor(const StringName &p_id);
149142
Variant get_custom_monitor(const StringName &p_id);
150143
TypedArray<StringName> get_custom_monitor_names();
144+
TypedArray<int> get_custom_monitor_types();
151145

152146
uint64_t get_monitor_modification_time();
153147

154148
static Performance *get_singleton() { return singleton; }
155149

156150
Performance();
151+
152+
private:
153+
class MonitorCall {
154+
MonitorType _type = MONITOR_TYPE_QUANTITY;
155+
Callable _callable;
156+
Vector<Variant> _arguments;
157+
158+
public:
159+
MonitorCall(MonitorType p_type, Callable p_callable, Vector<Variant> p_arguments);
160+
MonitorCall();
161+
Variant call(bool &r_error, String &r_error_message);
162+
MonitorType get_monitor_type();
163+
};
164+
165+
HashMap<StringName, MonitorCall> _monitor_map;
166+
uint64_t _monitor_modification_time;
157167
};
158168

159169
VARIANT_ENUM_CAST(Performance::Monitor);
170+
VARIANT_ENUM_CAST(Performance::MonitorType);

misc/extension_api_validation/4.5-stable.expected

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,9 @@ should instead be used to justify these changes and describe how users should wo
77
Add new entries at the end of the file.
88

99
## Changes between 4.5-stable and 4.6-stable
10+
11+
GH-110433
12+
---------
13+
Validate extension JSON: Error: Field 'classes/Performance/methods/add_custom_monitor/arguments': size changed value in new API, from 3 to 4.
14+
15+
Optional argument added. Compatibility method registered.

0 commit comments

Comments
 (0)