Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/objectdb_profiler/editor/data_viewers/summary_view.cpp
21026 views
1
/**************************************************************************/
2
/* summary_view.cpp */
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
#include "summary_view.h"
32
33
#include "core/os/time.h"
34
#include "editor/editor_node.h"
35
#include "editor/editor_string_names.h"
36
#include "scene/gui/center_container.h"
37
#include "scene/gui/label.h"
38
#include "scene/gui/panel_container.h"
39
#include "scene/gui/rich_text_label.h"
40
41
SnapshotSummaryView::SnapshotSummaryView() {
42
set_name(TTRC("Summary"));
43
44
set_v_size_flags(SizeFlags::SIZE_EXPAND_FILL);
45
set_h_size_flags(SizeFlags::SIZE_EXPAND_FILL);
46
47
content_wrapper = memnew(PanelContainer);
48
content_wrapper->set_anchors_preset(LayoutPreset::PRESET_FULL_RECT);
49
add_child(content_wrapper);
50
51
VBoxContainer *content = memnew(VBoxContainer);
52
content_wrapper->add_child(content);
53
content->set_anchors_preset(LayoutPreset::PRESET_FULL_RECT);
54
55
title = memnew(Label(TTRC("ObjectDB Snapshot Summary")));
56
content->add_child(title);
57
title->set_horizontal_alignment(HorizontalAlignment::HORIZONTAL_ALIGNMENT_CENTER);
58
title->set_vertical_alignment(VerticalAlignment::VERTICAL_ALIGNMENT_CENTER);
59
title->set_anchors_preset(LayoutPreset::PRESET_TOP_WIDE);
60
61
explainer_text = memnew(CenterContainer);
62
explainer_text->set_v_size_flags(SizeFlags::SIZE_EXPAND_FILL);
63
explainer_text->set_h_size_flags(SizeFlags::SIZE_EXPAND_FILL);
64
content->add_child(explainer_text);
65
VBoxContainer *explainer_lines = memnew(VBoxContainer);
66
explainer_text->add_child(explainer_lines);
67
Label *l1 = memnew(Label(TTRC("Press 'Take ObjectDB Snapshot' to snapshot the ObjectDB.")));
68
Label *l2 = memnew(Label(TTRC("Memory in Godot is either owned natively by the engine or owned by the ObjectDB.")));
69
Label *l3 = memnew(Label(TTRC("ObjectDB Snapshots capture only memory owned by the ObjectDB.")));
70
l1->set_horizontal_alignment(HorizontalAlignment::HORIZONTAL_ALIGNMENT_CENTER);
71
l2->set_horizontal_alignment(HorizontalAlignment::HORIZONTAL_ALIGNMENT_CENTER);
72
l3->set_horizontal_alignment(HorizontalAlignment::HORIZONTAL_ALIGNMENT_CENTER);
73
explainer_lines->add_child(l1);
74
explainer_lines->add_child(l2);
75
explainer_lines->add_child(l3);
76
77
ScrollContainer *sc = memnew(ScrollContainer);
78
sc->set_anchors_preset(LayoutPreset::PRESET_FULL_RECT);
79
sc->set_v_size_flags(SizeFlags::SIZE_EXPAND_FILL);
80
sc->set_h_size_flags(SizeFlags::SIZE_EXPAND_FILL);
81
content->add_child(sc);
82
83
blurb_list = memnew(VBoxContainer);
84
sc->add_child(blurb_list);
85
blurb_list->set_v_size_flags(SizeFlags::SIZE_EXPAND_FILL);
86
blurb_list->set_h_size_flags(SizeFlags::SIZE_EXPAND_FILL);
87
}
88
89
void SnapshotSummaryView::_notification(int p_what) {
90
if (p_what == NOTIFICATION_THEME_CHANGED) {
91
content_wrapper->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SNAME("ObjectDBContentWrapper"), EditorStringName(EditorStyles)));
92
title->add_theme_style_override(SNAME("normal"), get_theme_stylebox(SNAME("ObjectDBTitle"), EditorStringName(EditorStyles)));
93
}
94
}
95
96
void SnapshotSummaryView::show_snapshot(GameStateSnapshot *p_data, GameStateSnapshot *p_diff_data) {
97
SnapshotView::show_snapshot(p_data, p_diff_data);
98
explainer_text->set_visible(false);
99
100
String snapshot_a_name = diff_data == nullptr ? TTR("Snapshot") : TTR("Snapshot A");
101
String snapshot_b_name = TTR("Snapshot B");
102
103
_push_overview_blurb(snapshot_a_name + " " + TTR("Overview"), snapshot_data);
104
if (diff_data) {
105
_push_overview_blurb(snapshot_b_name + " " + TTR("Overview"), diff_data);
106
}
107
108
_push_node_blurb(snapshot_a_name + " " + TTR("Nodes"), snapshot_data);
109
if (diff_data) {
110
_push_node_blurb(snapshot_b_name + " " + TTR("Nodes"), diff_data);
111
}
112
113
_push_refcounted_blurb(snapshot_a_name + " " + TTR("RefCounteds"), snapshot_data);
114
if (diff_data) {
115
_push_refcounted_blurb(snapshot_b_name + " " + TTR("RefCounteds"), diff_data);
116
}
117
118
_push_object_blurb(snapshot_a_name + " " + TTR("Objects"), snapshot_data);
119
if (diff_data) {
120
_push_object_blurb(snapshot_b_name + " " + TTR("Objects"), diff_data);
121
}
122
}
123
124
void SnapshotSummaryView::clear_snapshot() {
125
// Just clear out the blurbs and leave the explainer.
126
for (int i = 0; i < blurb_list->get_child_count(); i++) {
127
blurb_list->get_child(i)->queue_free();
128
}
129
snapshot_data = nullptr;
130
diff_data = nullptr;
131
explainer_text->set_visible(true);
132
}
133
134
SummaryBlurb::SummaryBlurb(const String &p_title, const String &p_rtl_content) {
135
add_theme_constant_override("margin_left", 2);
136
add_theme_constant_override("margin_right", 2);
137
add_theme_constant_override("margin_top", 2);
138
add_theme_constant_override("margin_bottom", 2);
139
140
label = memnew(RichTextLabel);
141
label->add_theme_constant_override(SceneStringName(line_separation), 6);
142
label->set_text_direction(Control::TEXT_DIRECTION_INHERITED);
143
label->set_fit_content(true);
144
label->set_use_bbcode(true);
145
label->add_newline();
146
label->push_bold();
147
label->add_text(p_title);
148
label->pop();
149
label->add_newline();
150
label->add_newline();
151
label->append_text(p_rtl_content);
152
add_child(label);
153
}
154
155
void SnapshotSummaryView::_push_overview_blurb(const String &p_title, GameStateSnapshot *p_snapshot) {
156
String c = "";
157
158
c += "[ul]\n";
159
c += vformat(" [i]%s[/i] %s\n", TTR("Name:"), p_snapshot->name);
160
if (p_snapshot->snapshot_context.has("timestamp")) {
161
c += vformat(" [i]%s[/i] %s\n", TTR("Timestamp:"), Time::get_singleton()->get_datetime_string_from_unix_time((double)p_snapshot->snapshot_context["timestamp"]));
162
}
163
if (p_snapshot->snapshot_context.has("game_version")) {
164
c += vformat(" [i]%s[/i] %s\n", TTR("Game Version:"), (String)p_snapshot->snapshot_context["game_version"]);
165
}
166
if (p_snapshot->snapshot_context.has("editor_version")) {
167
c += vformat(" [i]%s[/i] %s\n", TTR("Editor Version:"), (String)p_snapshot->snapshot_context["editor_version"]);
168
}
169
170
double bytes_to_mb = 0.000001;
171
if (p_snapshot->snapshot_context.has("mem_usage")) {
172
c += vformat(" [i]%s[/i] %s\n", TTR("Memory Used:"), String::num((double)((uint64_t)p_snapshot->snapshot_context["mem_usage"]) * bytes_to_mb, 3) + " MB");
173
}
174
if (p_snapshot->snapshot_context.has("mem_max_usage")) {
175
c += vformat(" [i]%s[/i] %s\n", TTR("Max Memory Used:"), String::num((double)((uint64_t)p_snapshot->snapshot_context["mem_max_usage"]) * bytes_to_mb, 3) + " MB");
176
}
177
c += vformat(" [i]%s[/i] %s\n", TTR("Total Objects:"), itos(p_snapshot->objects.size()));
178
179
int node_count = 0;
180
for (const KeyValue<ObjectID, SnapshotDataObject *> &pair : p_snapshot->objects) {
181
if (pair.value->is_node()) {
182
node_count++;
183
}
184
}
185
c += vformat(" [i]%s[/i] %s\n", TTR("Total Nodes:"), itos(node_count));
186
c += "[/ul]\n";
187
188
blurb_list->add_child(memnew(SummaryBlurb(p_title, c)));
189
}
190
191
void SnapshotSummaryView::_push_node_blurb(const String &p_title, GameStateSnapshot *p_snapshot) {
192
LocalVector<String> nodes;
193
nodes.reserve(p_snapshot->objects.size());
194
195
for (const KeyValue<ObjectID, SnapshotDataObject *> &pair : p_snapshot->objects) {
196
// if it's a node AND it doesn't have a parent node
197
if (pair.value->is_node() && !pair.value->extra_debug_data.has("node_parent") && pair.value->extra_debug_data.has("node_is_scene_root") && !pair.value->extra_debug_data["node_is_scene_root"]) {
198
String node_name = pair.value->extra_debug_data["node_name"];
199
nodes.push_back(node_name != "" ? node_name : pair.value->get_name());
200
}
201
}
202
203
if (nodes.size() <= 1) {
204
return;
205
}
206
207
String c = TTR("Multiple root nodes [i](possible call to 'remove_child' without 'queue_free')[/i]") + "\n";
208
c += "[ul]\n";
209
for (const String &node : nodes) {
210
c += " " + node + "\n";
211
}
212
c += "[/ul]\n";
213
214
blurb_list->add_child(memnew(SummaryBlurb(p_title, c)));
215
}
216
217
void SnapshotSummaryView::_push_refcounted_blurb(const String &p_title, GameStateSnapshot *p_snapshot) {
218
LocalVector<String> rcs;
219
rcs.reserve(p_snapshot->objects.size());
220
221
for (const KeyValue<ObjectID, SnapshotDataObject *> &pair : p_snapshot->objects) {
222
if (pair.value->is_refcounted()) {
223
int ref_count = (uint64_t)pair.value->extra_debug_data["ref_count"];
224
Array ref_cycles = (Array)pair.value->extra_debug_data["ref_cycles"];
225
226
if (ref_count == ref_cycles.size()) {
227
rcs.push_back(pair.value->get_name());
228
}
229
}
230
}
231
232
if (rcs.is_empty()) {
233
return;
234
}
235
236
String c = TTR("RefCounted objects only referenced in cycles [i](cycles often indicate a memory leaks)[/i]") + "\n";
237
c += "[ul]\n";
238
for (const String &rc : rcs) {
239
c += " " + rc + "\n";
240
}
241
c += "[/ul]\n";
242
243
blurb_list->add_child(memnew(SummaryBlurb(p_title, c)));
244
}
245
246
void SnapshotSummaryView::_push_object_blurb(const String &p_title, GameStateSnapshot *p_snapshot) {
247
LocalVector<String> objects;
248
objects.reserve(p_snapshot->objects.size());
249
250
for (const KeyValue<ObjectID, SnapshotDataObject *> &pair : p_snapshot->objects) {
251
if (pair.value->inbound_references.is_empty() && pair.value->outbound_references.is_empty()) {
252
if (!pair.value->get_script().is_null()) {
253
// This blurb will have a lot of false positives, but we can at least suppress false positives
254
// from unreferenced nodes that are part of the scene tree.
255
if (pair.value->is_node() && (bool)pair.value->extra_debug_data["node_is_scene_root"]) {
256
objects.push_back(pair.value->get_name());
257
}
258
}
259
}
260
}
261
262
if (objects.is_empty()) {
263
return;
264
}
265
266
String c = TTR("Scripted objects not referenced by any other objects [i](unreferenced objects may indicate a memory leak)[/i]") + "\n";
267
c += "[ul]\n";
268
for (const String &object : objects) {
269
c += " " + object + "\n";
270
}
271
c += "[/ul]\n";
272
273
blurb_list->add_child(memnew(SummaryBlurb(p_title, c)));
274
}
275
276