Path: blob/master/modules/objectdb_profiler/editor/data_viewers/summary_view.cpp
21026 views
/**************************************************************************/1/* summary_view.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "summary_view.h"3132#include "core/os/time.h"33#include "editor/editor_node.h"34#include "editor/editor_string_names.h"35#include "scene/gui/center_container.h"36#include "scene/gui/label.h"37#include "scene/gui/panel_container.h"38#include "scene/gui/rich_text_label.h"3940SnapshotSummaryView::SnapshotSummaryView() {41set_name(TTRC("Summary"));4243set_v_size_flags(SizeFlags::SIZE_EXPAND_FILL);44set_h_size_flags(SizeFlags::SIZE_EXPAND_FILL);4546content_wrapper = memnew(PanelContainer);47content_wrapper->set_anchors_preset(LayoutPreset::PRESET_FULL_RECT);48add_child(content_wrapper);4950VBoxContainer *content = memnew(VBoxContainer);51content_wrapper->add_child(content);52content->set_anchors_preset(LayoutPreset::PRESET_FULL_RECT);5354title = memnew(Label(TTRC("ObjectDB Snapshot Summary")));55content->add_child(title);56title->set_horizontal_alignment(HorizontalAlignment::HORIZONTAL_ALIGNMENT_CENTER);57title->set_vertical_alignment(VerticalAlignment::VERTICAL_ALIGNMENT_CENTER);58title->set_anchors_preset(LayoutPreset::PRESET_TOP_WIDE);5960explainer_text = memnew(CenterContainer);61explainer_text->set_v_size_flags(SizeFlags::SIZE_EXPAND_FILL);62explainer_text->set_h_size_flags(SizeFlags::SIZE_EXPAND_FILL);63content->add_child(explainer_text);64VBoxContainer *explainer_lines = memnew(VBoxContainer);65explainer_text->add_child(explainer_lines);66Label *l1 = memnew(Label(TTRC("Press 'Take ObjectDB Snapshot' to snapshot the ObjectDB.")));67Label *l2 = memnew(Label(TTRC("Memory in Godot is either owned natively by the engine or owned by the ObjectDB.")));68Label *l3 = memnew(Label(TTRC("ObjectDB Snapshots capture only memory owned by the ObjectDB.")));69l1->set_horizontal_alignment(HorizontalAlignment::HORIZONTAL_ALIGNMENT_CENTER);70l2->set_horizontal_alignment(HorizontalAlignment::HORIZONTAL_ALIGNMENT_CENTER);71l3->set_horizontal_alignment(HorizontalAlignment::HORIZONTAL_ALIGNMENT_CENTER);72explainer_lines->add_child(l1);73explainer_lines->add_child(l2);74explainer_lines->add_child(l3);7576ScrollContainer *sc = memnew(ScrollContainer);77sc->set_anchors_preset(LayoutPreset::PRESET_FULL_RECT);78sc->set_v_size_flags(SizeFlags::SIZE_EXPAND_FILL);79sc->set_h_size_flags(SizeFlags::SIZE_EXPAND_FILL);80content->add_child(sc);8182blurb_list = memnew(VBoxContainer);83sc->add_child(blurb_list);84blurb_list->set_v_size_flags(SizeFlags::SIZE_EXPAND_FILL);85blurb_list->set_h_size_flags(SizeFlags::SIZE_EXPAND_FILL);86}8788void SnapshotSummaryView::_notification(int p_what) {89if (p_what == NOTIFICATION_THEME_CHANGED) {90content_wrapper->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SNAME("ObjectDBContentWrapper"), EditorStringName(EditorStyles)));91title->add_theme_style_override(SNAME("normal"), get_theme_stylebox(SNAME("ObjectDBTitle"), EditorStringName(EditorStyles)));92}93}9495void SnapshotSummaryView::show_snapshot(GameStateSnapshot *p_data, GameStateSnapshot *p_diff_data) {96SnapshotView::show_snapshot(p_data, p_diff_data);97explainer_text->set_visible(false);9899String snapshot_a_name = diff_data == nullptr ? TTR("Snapshot") : TTR("Snapshot A");100String snapshot_b_name = TTR("Snapshot B");101102_push_overview_blurb(snapshot_a_name + " " + TTR("Overview"), snapshot_data);103if (diff_data) {104_push_overview_blurb(snapshot_b_name + " " + TTR("Overview"), diff_data);105}106107_push_node_blurb(snapshot_a_name + " " + TTR("Nodes"), snapshot_data);108if (diff_data) {109_push_node_blurb(snapshot_b_name + " " + TTR("Nodes"), diff_data);110}111112_push_refcounted_blurb(snapshot_a_name + " " + TTR("RefCounteds"), snapshot_data);113if (diff_data) {114_push_refcounted_blurb(snapshot_b_name + " " + TTR("RefCounteds"), diff_data);115}116117_push_object_blurb(snapshot_a_name + " " + TTR("Objects"), snapshot_data);118if (diff_data) {119_push_object_blurb(snapshot_b_name + " " + TTR("Objects"), diff_data);120}121}122123void SnapshotSummaryView::clear_snapshot() {124// Just clear out the blurbs and leave the explainer.125for (int i = 0; i < blurb_list->get_child_count(); i++) {126blurb_list->get_child(i)->queue_free();127}128snapshot_data = nullptr;129diff_data = nullptr;130explainer_text->set_visible(true);131}132133SummaryBlurb::SummaryBlurb(const String &p_title, const String &p_rtl_content) {134add_theme_constant_override("margin_left", 2);135add_theme_constant_override("margin_right", 2);136add_theme_constant_override("margin_top", 2);137add_theme_constant_override("margin_bottom", 2);138139label = memnew(RichTextLabel);140label->add_theme_constant_override(SceneStringName(line_separation), 6);141label->set_text_direction(Control::TEXT_DIRECTION_INHERITED);142label->set_fit_content(true);143label->set_use_bbcode(true);144label->add_newline();145label->push_bold();146label->add_text(p_title);147label->pop();148label->add_newline();149label->add_newline();150label->append_text(p_rtl_content);151add_child(label);152}153154void SnapshotSummaryView::_push_overview_blurb(const String &p_title, GameStateSnapshot *p_snapshot) {155String c = "";156157c += "[ul]\n";158c += vformat(" [i]%s[/i] %s\n", TTR("Name:"), p_snapshot->name);159if (p_snapshot->snapshot_context.has("timestamp")) {160c += vformat(" [i]%s[/i] %s\n", TTR("Timestamp:"), Time::get_singleton()->get_datetime_string_from_unix_time((double)p_snapshot->snapshot_context["timestamp"]));161}162if (p_snapshot->snapshot_context.has("game_version")) {163c += vformat(" [i]%s[/i] %s\n", TTR("Game Version:"), (String)p_snapshot->snapshot_context["game_version"]);164}165if (p_snapshot->snapshot_context.has("editor_version")) {166c += vformat(" [i]%s[/i] %s\n", TTR("Editor Version:"), (String)p_snapshot->snapshot_context["editor_version"]);167}168169double bytes_to_mb = 0.000001;170if (p_snapshot->snapshot_context.has("mem_usage")) {171c += 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");172}173if (p_snapshot->snapshot_context.has("mem_max_usage")) {174c += 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");175}176c += vformat(" [i]%s[/i] %s\n", TTR("Total Objects:"), itos(p_snapshot->objects.size()));177178int node_count = 0;179for (const KeyValue<ObjectID, SnapshotDataObject *> &pair : p_snapshot->objects) {180if (pair.value->is_node()) {181node_count++;182}183}184c += vformat(" [i]%s[/i] %s\n", TTR("Total Nodes:"), itos(node_count));185c += "[/ul]\n";186187blurb_list->add_child(memnew(SummaryBlurb(p_title, c)));188}189190void SnapshotSummaryView::_push_node_blurb(const String &p_title, GameStateSnapshot *p_snapshot) {191LocalVector<String> nodes;192nodes.reserve(p_snapshot->objects.size());193194for (const KeyValue<ObjectID, SnapshotDataObject *> &pair : p_snapshot->objects) {195// if it's a node AND it doesn't have a parent node196if (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"]) {197String node_name = pair.value->extra_debug_data["node_name"];198nodes.push_back(node_name != "" ? node_name : pair.value->get_name());199}200}201202if (nodes.size() <= 1) {203return;204}205206String c = TTR("Multiple root nodes [i](possible call to 'remove_child' without 'queue_free')[/i]") + "\n";207c += "[ul]\n";208for (const String &node : nodes) {209c += " " + node + "\n";210}211c += "[/ul]\n";212213blurb_list->add_child(memnew(SummaryBlurb(p_title, c)));214}215216void SnapshotSummaryView::_push_refcounted_blurb(const String &p_title, GameStateSnapshot *p_snapshot) {217LocalVector<String> rcs;218rcs.reserve(p_snapshot->objects.size());219220for (const KeyValue<ObjectID, SnapshotDataObject *> &pair : p_snapshot->objects) {221if (pair.value->is_refcounted()) {222int ref_count = (uint64_t)pair.value->extra_debug_data["ref_count"];223Array ref_cycles = (Array)pair.value->extra_debug_data["ref_cycles"];224225if (ref_count == ref_cycles.size()) {226rcs.push_back(pair.value->get_name());227}228}229}230231if (rcs.is_empty()) {232return;233}234235String c = TTR("RefCounted objects only referenced in cycles [i](cycles often indicate a memory leaks)[/i]") + "\n";236c += "[ul]\n";237for (const String &rc : rcs) {238c += " " + rc + "\n";239}240c += "[/ul]\n";241242blurb_list->add_child(memnew(SummaryBlurb(p_title, c)));243}244245void SnapshotSummaryView::_push_object_blurb(const String &p_title, GameStateSnapshot *p_snapshot) {246LocalVector<String> objects;247objects.reserve(p_snapshot->objects.size());248249for (const KeyValue<ObjectID, SnapshotDataObject *> &pair : p_snapshot->objects) {250if (pair.value->inbound_references.is_empty() && pair.value->outbound_references.is_empty()) {251if (!pair.value->get_script().is_null()) {252// This blurb will have a lot of false positives, but we can at least suppress false positives253// from unreferenced nodes that are part of the scene tree.254if (pair.value->is_node() && (bool)pair.value->extra_debug_data["node_is_scene_root"]) {255objects.push_back(pair.value->get_name());256}257}258}259}260261if (objects.is_empty()) {262return;263}264265String c = TTR("Scripted objects not referenced by any other objects [i](unreferenced objects may indicate a memory leak)[/i]") + "\n";266c += "[ul]\n";267for (const String &object : objects) {268c += " " + object + "\n";269}270c += "[/ul]\n";271272blurb_list->add_child(memnew(SummaryBlurb(p_title, c)));273}274275276