Skip to content

Commit 982eab6

Browse files
committed
Allow custom monitors to select desired type
1 parent 56d5624 commit 982eab6

File tree

9 files changed

+154
-28
lines changed

9 files changed

+154
-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" />
@@ -303,5 +310,17 @@
303310
<constant name="MONITOR_MAX" value="59" enum="Monitor">
304311
Represents the size of the [enum Monitor] enum.
305312
</constant>
313+
<constant name="MONITOR_TYPE_QUANTITY" value="0" enum="MonitorType">
314+
Monitor output is formatted as an integer value.
315+
</constant>
316+
<constant name="MONITOR_TYPE_MEMORY" value="1" enum="MonitorType">
317+
Monitor output is formatted as computer memory. Submitted values should represent a number of bytes.
318+
</constant>
319+
<constant name="MONITOR_TYPE_TIME" value="2" enum="MonitorType">
320+
Monitor output is formatted as time in milliseconds. Submitted values should represent a time in seconds (not milliseconds).
321+
</constant>
322+
<constant name="MONITOR_TYPE_PERCENTAGE" value="3" enum="MonitorType">
323+
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].
324+
</constant>
306325
</constants>
307326
</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 * 100).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
@@ -912,13 +912,24 @@ void ScriptEditorDebugger::_msg_show_selection_limit_warning(uint64_t p_thread_i
912912
}
913913

914914
void ScriptEditorDebugger::_msg_performance_profile_names(uint64_t p_thread_id, const Array &p_data) {
915+
Array name_data = p_data[0];
916+
Array type_data = p_data[1];
917+
915918
Vector<StringName> monitors;
916-
monitors.resize(p_data.size());
917-
for (int i = 0; i < p_data.size(); i++) {
918-
ERR_FAIL_COND(p_data[i].get_type() != Variant::STRING_NAME);
919-
monitors.set(i, p_data[i]);
919+
monitors.resize(name_data.size());
920+
for (int i = 0; i < name_data.size(); i++) {
921+
ERR_FAIL_COND(name_data[i].get_type() != Variant::STRING_NAME);
922+
monitors.set(i, name_data[i]);
920923
}
921-
performance_profiler->update_monitors(monitors);
924+
925+
PackedInt32Array types;
926+
types.resize(type_data.size());
927+
for (int i = 0; i < type_data.size(); i++) {
928+
ERR_FAIL_COND(type_data[i].get_type() != Variant::INT);
929+
types.set(i, type_data[i]);
930+
}
931+
932+
performance_profiler->update_monitors(monitors, types);
922933
}
923934

924935
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"
@@ -57,12 +58,13 @@ Performance *Performance::singleton = nullptr;
5758

5859
void Performance::_bind_methods() {
5960
ClassDB::bind_method(D_METHOD("get_monitor", "monitor"), &Performance::get_monitor);
60-
ClassDB::bind_method(D_METHOD("add_custom_monitor", "id", "callable", "arguments"), &Performance::add_custom_monitor, DEFVAL(Array()));
61+
ClassDB::bind_method(D_METHOD("add_custom_monitor", "id", "callable", "arguments", "type"), &Performance::add_custom_monitor, DEFVAL(Array()), DEFVAL(MONITOR_TYPE_QUANTITY));
6162
ClassDB::bind_method(D_METHOD("remove_custom_monitor", "id"), &Performance::remove_custom_monitor);
6263
ClassDB::bind_method(D_METHOD("has_custom_monitor", "id"), &Performance::has_custom_monitor);
6364
ClassDB::bind_method(D_METHOD("get_custom_monitor", "id"), &Performance::get_custom_monitor);
6465
ClassDB::bind_method(D_METHOD("get_monitor_modification_time"), &Performance::get_monitor_modification_time);
6566
ClassDB::bind_method(D_METHOD("get_custom_monitor_names"), &Performance::get_custom_monitor_names);
67+
ClassDB::bind_method(D_METHOD("get_custom_monitor_types"), &Performance::get_custom_monitor_types);
6668

6769
BIND_ENUM_CONSTANT(TIME_FPS);
6870
BIND_ENUM_CONSTANT(TIME_PROCESS);
@@ -132,6 +134,11 @@ void Performance::_bind_methods() {
132134
BIND_ENUM_CONSTANT(NAVIGATION_3D_OBSTACLE_COUNT);
133135
#endif // NAVIGATION_3D_DISABLED
134136
BIND_ENUM_CONSTANT(MONITOR_MAX);
137+
138+
BIND_ENUM_CONSTANT(MONITOR_TYPE_QUANTITY);
139+
BIND_ENUM_CONSTANT(MONITOR_TYPE_MEMORY);
140+
BIND_ENUM_CONSTANT(MONITOR_TYPE_TIME);
141+
BIND_ENUM_CONSTANT(MONITOR_TYPE_PERCENTAGE);
135142
}
136143

137144
int Performance::_get_node_count() const {
@@ -537,9 +544,9 @@ void Performance::set_navigation_process_time(double p_pt) {
537544
_navigation_process_time = p_pt;
538545
}
539546

540-
void Performance::add_custom_monitor(const StringName &p_id, const Callable &p_callable, const Vector<Variant> &p_args) {
547+
void Performance::add_custom_monitor(const StringName &p_id, const Callable &p_callable, const Vector<Variant> &p_args, MonitorType p_type = MONITOR_TYPE_QUANTITY) {
541548
ERR_FAIL_COND_MSG(has_custom_monitor(p_id), "Custom monitor with id '" + String(p_id) + "' already exists.");
542-
_monitor_map.insert(p_id, MonitorCall(p_callable, p_args));
549+
_monitor_map.insert(p_id, MonitorCall(p_type, p_callable, p_args));
543550
_monitor_modification_time = OS::get_singleton()->get_ticks_usec();
544551
}
545552

@@ -576,6 +583,20 @@ TypedArray<StringName> Performance::get_custom_monitor_names() {
576583
return return_array;
577584
}
578585

586+
TypedArray<int> Performance::get_custom_monitor_types() {
587+
if (!_monitor_map.size()) {
588+
return TypedArray<int>();
589+
}
590+
TypedArray<int> return_array;
591+
return_array.resize(_monitor_map.size());
592+
int index = 0;
593+
for (KeyValue<StringName, MonitorCall> i : _monitor_map) {
594+
return_array.set(index, (int)i.value.get_monitor_type());
595+
index++;
596+
}
597+
return return_array;
598+
}
599+
579600
uint64_t Performance::get_monitor_modification_time() {
580601
return _monitor_modification_time;
581602
}
@@ -588,7 +609,8 @@ Performance::Performance() {
588609
singleton = this;
589610
}
590611

591-
Performance::MonitorCall::MonitorCall(Callable p_callable, Vector<Variant> p_arguments) {
612+
Performance::MonitorCall::MonitorCall(Performance::MonitorType p_type, Callable p_callable, Vector<Variant> p_arguments) {
613+
_type = p_type;
592614
_callable = p_callable;
593615
_arguments = p_arguments;
594616
}
@@ -613,3 +635,7 @@ Variant Performance::MonitorCall::call(bool &r_error, String &r_error_message) {
613635
}
614636
return return_value;
615637
}
638+
639+
Performance::MonitorType Performance::MonitorCall::get_monitor_type() {
640+
return _type;
641+
}

main/performance.h

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,26 +45,18 @@ 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
int _get_orphan_node_count() const;
5055

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

55-
class MonitorCall {
56-
Callable _callable;
57-
Vector<Variant> _arguments;
58-
59-
public:
60-
MonitorCall(Callable p_callable, Vector<Variant> p_arguments);
61-
MonitorCall();
62-
Variant call(bool &r_error, String &r_error_message);
63-
};
64-
65-
HashMap<StringName, MonitorCall> _monitor_map;
66-
uint64_t _monitor_modification_time;
67-
6860
public:
6961
enum Monitor {
7062
TIME_FPS,
@@ -135,7 +127,8 @@ class Performance : public Object {
135127
enum MonitorType {
136128
MONITOR_TYPE_QUANTITY,
137129
MONITOR_TYPE_MEMORY,
138-
MONITOR_TYPE_TIME
130+
MONITOR_TYPE_TIME,
131+
MONITOR_TYPE_PERCENTAGE
139132
};
140133

141134
double get_monitor(Monitor p_monitor) const;
@@ -147,17 +140,35 @@ class Performance : public Object {
147140
void set_physics_process_time(double p_pt);
148141
void set_navigation_process_time(double p_pt);
149142

150-
void add_custom_monitor(const StringName &p_id, const Callable &p_callable, const Vector<Variant> &p_args);
143+
void add_custom_monitor(const StringName &p_id, const Callable &p_callable, const Vector<Variant> &p_args, MonitorType p_type);
151144
void remove_custom_monitor(const StringName &p_id);
152145
bool has_custom_monitor(const StringName &p_id);
153146
Variant get_custom_monitor(const StringName &p_id);
154147
TypedArray<StringName> get_custom_monitor_names();
148+
TypedArray<int> get_custom_monitor_types();
155149

156150
uint64_t get_monitor_modification_time();
157151

158152
static Performance *get_singleton() { return singleton; }
159153

160154
Performance();
155+
156+
private:
157+
class MonitorCall {
158+
MonitorType _type = MONITOR_TYPE_QUANTITY;
159+
Callable _callable;
160+
Vector<Variant> _arguments;
161+
162+
public:
163+
MonitorCall(MonitorType p_type, Callable p_callable, Vector<Variant> p_arguments);
164+
MonitorCall();
165+
Variant call(bool &r_error, String &r_error_message);
166+
MonitorType get_monitor_type();
167+
};
168+
169+
HashMap<StringName, MonitorCall> _monitor_map;
170+
uint64_t _monitor_modification_time;
161171
};
162172

163173
VARIANT_ENUM_CAST(Performance::Monitor);
174+
VARIANT_ENUM_CAST(Performance::MonitorType);

misc/extension_api_validation/4.5-stable.expected

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,10 @@ GH-111117
4040
Validate extension JSON: JSON file: Field was added in a way that breaks compatibility 'classes/LineEdit/methods/edit': arguments
4141

4242
Optional argument added. Compatibility method registered.
43+
44+
45+
GH-110433
46+
---------
47+
Validate extension JSON: Error: Field 'classes/Performance/methods/add_custom_monitor/arguments': size changed value in new API, from 3 to 4.
48+
49+
Optional argument added. Compatibility method registered.

0 commit comments

Comments
 (0)