Path: blob/master/servers/debugger/servers_debugger.cpp
10277 views
/**************************************************************************/1/* servers_debugger.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 "servers_debugger.h"3132#include "core/config/project_settings.h"33#include "core/debugger/engine_debugger.h"34#include "core/debugger/engine_profiler.h"35#include "core/io/resource_loader.h"36#include "core/object/script_language.h"37#include "servers/display_server.h"3839#define CHECK_SIZE(arr, expected, what) ERR_FAIL_COND_V_MSG((uint32_t)arr.size() < (uint32_t)(expected), false, String("Malformed ") + what + " message from script debugger, message too short. Expected size: " + itos(expected) + ", actual size: " + itos(arr.size()))40#define CHECK_END(arr, expected, what) ERR_FAIL_COND_V_MSG((uint32_t)arr.size() > (uint32_t)expected, false, String("Malformed ") + what + " message from script debugger, message too long. Expected size: " + itos(expected) + ", actual size: " + itos(arr.size()))4142Array ServersDebugger::ResourceUsage::serialize() {43infos.sort();4445Array arr = { infos.size() * 4 };46for (const ResourceInfo &E : infos) {47arr.push_back(E.path);48arr.push_back(E.format);49arr.push_back(E.type);50arr.push_back(E.vram);51}52return arr;53}5455bool ServersDebugger::ResourceUsage::deserialize(const Array &p_arr) {56CHECK_SIZE(p_arr, 1, "ResourceUsage");57uint32_t size = p_arr[0];58ERR_FAIL_COND_V(size % 4, false);59CHECK_SIZE(p_arr, 1 + size, "ResourceUsage");60uint32_t idx = 1;61while (idx < 1 + size) {62ResourceInfo info;63info.path = p_arr[idx];64info.format = p_arr[idx + 1];65info.type = p_arr[idx + 2];66info.vram = p_arr[idx + 3];67infos.push_back(info);68idx += 4;69}70CHECK_END(p_arr, idx, "ResourceUsage");71return true;72}7374Array ServersDebugger::ScriptFunctionSignature::serialize() {75Array arr = { name, id };76return arr;77}7879bool ServersDebugger::ScriptFunctionSignature::deserialize(const Array &p_arr) {80CHECK_SIZE(p_arr, 2, "ScriptFunctionSignature");81name = p_arr[0];82id = p_arr[1];83CHECK_END(p_arr, 2, "ScriptFunctionSignature");84return true;85}8687Array ServersDebugger::ServersProfilerFrame::serialize() {88Array arr = { frame_number, frame_time, process_time, physics_time, physics_frame_time, script_time };8990arr.push_back(servers.size());91for (const ServerInfo &s : servers) {92arr.push_back(s.name);93arr.push_back(s.functions.size() * 2);94for (const ServerFunctionInfo &f : s.functions) {95arr.push_back(f.name);96arr.push_back(f.time);97}98}99100arr.push_back(script_functions.size() * 5);101for (int i = 0; i < script_functions.size(); i++) {102arr.push_back(script_functions[i].sig_id);103arr.push_back(script_functions[i].call_count);104arr.push_back(script_functions[i].self_time);105arr.push_back(script_functions[i].total_time);106arr.push_back(script_functions[i].internal_time);107}108return arr;109}110111bool ServersDebugger::ServersProfilerFrame::deserialize(const Array &p_arr) {112CHECK_SIZE(p_arr, 7, "ServersProfilerFrame");113frame_number = p_arr[0];114frame_time = p_arr[1];115process_time = p_arr[2];116physics_time = p_arr[3];117physics_frame_time = p_arr[4];118script_time = p_arr[5];119int servers_size = p_arr[6];120int idx = 7;121while (servers_size) {122CHECK_SIZE(p_arr, idx + 2, "ServersProfilerFrame");123servers_size--;124ServerInfo si;125si.name = p_arr[idx];126int sub_data_size = p_arr[idx + 1];127idx += 2;128CHECK_SIZE(p_arr, idx + sub_data_size, "ServersProfilerFrame");129for (int j = 0; j < sub_data_size / 2; j++) {130ServerFunctionInfo sf;131sf.name = p_arr[idx];132sf.time = p_arr[idx + 1];133idx += 2;134si.functions.push_back(sf);135}136servers.push_back(si);137}138CHECK_SIZE(p_arr, idx + 1, "ServersProfilerFrame");139int func_size = p_arr[idx];140idx += 1;141CHECK_SIZE(p_arr, idx + func_size, "ServersProfilerFrame");142for (int i = 0; i < func_size / 5; i++) {143ScriptFunctionInfo fi;144fi.sig_id = p_arr[idx];145fi.call_count = p_arr[idx + 1];146fi.self_time = p_arr[idx + 2];147fi.total_time = p_arr[idx + 3];148fi.internal_time = p_arr[idx + 4];149script_functions.push_back(fi);150idx += 5;151}152CHECK_END(p_arr, idx, "ServersProfilerFrame");153return true;154}155156Array ServersDebugger::VisualProfilerFrame::serialize() {157Array arr = { frame_number, areas.size() * 3 };158for (int i = 0; i < areas.size(); i++) {159arr.push_back(areas[i].name);160arr.push_back(areas[i].cpu_msec);161arr.push_back(areas[i].gpu_msec);162}163return arr;164}165166bool ServersDebugger::VisualProfilerFrame::deserialize(const Array &p_arr) {167CHECK_SIZE(p_arr, 2, "VisualProfilerFrame");168frame_number = p_arr[0];169int size = p_arr[1];170CHECK_SIZE(p_arr, size, "VisualProfilerFrame");171int idx = 2;172areas.resize(size / 3);173RS::FrameProfileArea *w = areas.ptrw();174for (int i = 0; i < size / 3; i++) {175w[i].name = p_arr[idx];176w[i].cpu_msec = p_arr[idx + 1];177w[i].gpu_msec = p_arr[idx + 2];178idx += 3;179}180CHECK_END(p_arr, idx, "VisualProfilerFrame");181return true;182}183class ServersDebugger::ScriptsProfiler : public EngineProfiler {184typedef ServersDebugger::ScriptFunctionSignature FunctionSignature;185typedef ServersDebugger::ScriptFunctionInfo FunctionInfo;186struct ProfileInfoSort {187bool operator()(ScriptLanguage::ProfilingInfo *A, ScriptLanguage::ProfilingInfo *B) const {188return A->total_time > B->total_time;189}190};191Vector<ScriptLanguage::ProfilingInfo> info;192Vector<ScriptLanguage::ProfilingInfo *> ptrs;193HashMap<StringName, int> sig_map;194int max_frame_functions = 16;195196public:197void toggle(bool p_enable, const Array &p_opts) {198if (p_enable) {199sig_map.clear();200for (int i = 0; i < ScriptServer::get_language_count(); i++) {201ScriptServer::get_language(i)->profiling_start();202if (p_opts.size() == 2 && p_opts[1].get_type() == Variant::BOOL) {203ScriptServer::get_language(i)->profiling_set_save_native_calls(p_opts[1]);204}205}206if (p_opts.size() > 0 && p_opts[0].get_type() == Variant::INT) {207max_frame_functions = MAX(0, int(p_opts[0]));208}209} else {210for (int i = 0; i < ScriptServer::get_language_count(); i++) {211ScriptServer::get_language(i)->profiling_stop();212}213}214}215216void write_frame_data(Vector<FunctionInfo> &r_funcs, uint64_t &r_total, bool p_accumulated) {217int ofs = 0;218for (int i = 0; i < ScriptServer::get_language_count(); i++) {219if (p_accumulated) {220ofs += ScriptServer::get_language(i)->profiling_get_accumulated_data(&info.write[ofs], info.size() - ofs);221} else {222ofs += ScriptServer::get_language(i)->profiling_get_frame_data(&info.write[ofs], info.size() - ofs);223}224}225226for (int i = 0; i < ofs; i++) {227ptrs.write[i] = &info.write[i];228}229230SortArray<ScriptLanguage::ProfilingInfo *, ProfileInfoSort> sa;231sa.sort(ptrs.ptrw(), ofs);232233int to_send = MIN(ofs, max_frame_functions);234235// Check signatures first, and compute total time.236r_total = 0;237for (int i = 0; i < to_send; i++) {238if (!sig_map.has(ptrs[i]->signature)) {239int idx = sig_map.size();240FunctionSignature sig;241sig.name = ptrs[i]->signature;242sig.id = idx;243EngineDebugger::get_singleton()->send_message("servers:function_signature", sig.serialize());244sig_map[ptrs[i]->signature] = idx;245}246r_total += ptrs[i]->self_time;247}248249// Send frame, script time, functions information then250r_funcs.resize(to_send);251252FunctionInfo *w = r_funcs.ptrw();253for (int i = 0; i < to_send; i++) {254if (sig_map.has(ptrs[i]->signature)) {255w[i].sig_id = sig_map[ptrs[i]->signature];256}257w[i].call_count = ptrs[i]->call_count;258w[i].total_time = ptrs[i]->total_time / 1000000.0;259w[i].self_time = ptrs[i]->self_time / 1000000.0;260w[i].internal_time = ptrs[i]->internal_time / 1000000.0;261}262}263264ScriptsProfiler() {265info.resize(GLOBAL_GET("debug/settings/profiler/max_functions"));266ptrs.resize(info.size());267}268};269270class ServersDebugger::ServersProfiler : public EngineProfiler {271bool skip_profile_frame = false;272typedef ServersDebugger::ServerInfo ServerInfo;273typedef ServersDebugger::ServerFunctionInfo ServerFunctionInfo;274275HashMap<StringName, ServerInfo> server_data;276ScriptsProfiler scripts_profiler;277278double frame_time = 0;279double process_time = 0;280double physics_time = 0;281double physics_frame_time = 0;282283void _send_frame_data(bool p_final) {284ServersDebugger::ServersProfilerFrame frame;285frame.frame_number = Engine::get_singleton()->get_process_frames();286frame.frame_time = frame_time;287frame.process_time = process_time;288frame.physics_time = physics_time;289frame.physics_frame_time = physics_frame_time;290HashMap<StringName, ServerInfo>::Iterator E = server_data.begin();291while (E) {292if (!p_final) {293frame.servers.push_back(E->value);294}295E->value.functions.clear();296++E;297}298uint64_t time = 0;299scripts_profiler.write_frame_data(frame.script_functions, time, p_final);300frame.script_time = USEC_TO_SEC(time);301if (skip_profile_frame) {302skip_profile_frame = false;303return;304}305if (p_final) {306EngineDebugger::get_singleton()->send_message("servers:profile_total", frame.serialize());307} else {308EngineDebugger::get_singleton()->send_message("servers:profile_frame", frame.serialize());309}310}311312public:313void toggle(bool p_enable, const Array &p_opts) {314skip_profile_frame = false;315if (p_enable) {316server_data.clear(); // Clear old profiling data.317} else {318_send_frame_data(true); // Send final frame.319}320scripts_profiler.toggle(p_enable, p_opts);321}322323void add(const Array &p_data) {324String name = p_data[0];325if (!server_data.has(name)) {326ServerInfo info;327info.name = name;328server_data[name] = info;329}330ServerInfo &srv = server_data[name];331332for (int idx = 1; idx < p_data.size() - 1; idx += 2) {333ServerFunctionInfo fi;334fi.name = p_data[idx];335fi.time = p_data[idx + 1];336srv.functions.push_back(fi);337}338}339340void tick(double p_frame_time, double p_process_time, double p_physics_time, double p_physics_frame_time) {341frame_time = p_frame_time;342process_time = p_process_time;343physics_time = p_physics_time;344physics_frame_time = p_physics_frame_time;345_send_frame_data(false);346}347348void skip_frame() {349skip_profile_frame = true;350}351};352353class ServersDebugger::VisualProfiler : public EngineProfiler {354typedef ServersDebugger::ServerInfo ServerInfo;355typedef ServersDebugger::ServerFunctionInfo ServerFunctionInfo;356357HashMap<StringName, ServerInfo> server_data;358359public:360void toggle(bool p_enable, const Array &p_opts) {361RS::get_singleton()->set_frame_profiling_enabled(p_enable);362363// Send hardware information from the remote device so that it's accurate for remote debugging.364Array hardware_info = {365OS::get_singleton()->get_processor_name(),366RenderingServer::get_singleton()->get_video_adapter_name()367};368EngineDebugger::get_singleton()->send_message("visual:hardware_info", hardware_info);369}370371void add(const Array &p_data) {}372373void tick(double p_frame_time, double p_process_time, double p_physics_time, double p_physics_frame_time) {374Vector<RS::FrameProfileArea> profile_areas = RS::get_singleton()->get_frame_profile();375ServersDebugger::VisualProfilerFrame frame;376if (!profile_areas.size()) {377return;378}379380frame.frame_number = RS::get_singleton()->get_frame_profile_frame();381frame.areas.append_array(profile_areas);382EngineDebugger::get_singleton()->send_message("visual:profile_frame", frame.serialize());383}384};385386ServersDebugger *ServersDebugger::singleton = nullptr;387388void ServersDebugger::initialize() {389if (EngineDebugger::is_active()) {390memnew(ServersDebugger);391}392}393394void ServersDebugger::deinitialize() {395if (singleton) {396memdelete(singleton);397}398}399400Error ServersDebugger::_capture(void *p_user, const String &p_cmd, const Array &p_data, bool &r_captured) {401ERR_FAIL_NULL_V(singleton, ERR_BUG);402r_captured = true;403if (p_cmd == "memory") {404singleton->_send_resource_usage();405} else if (p_cmd == "draw") { // Forced redraw.406// For camera override to stay live when the game is paused from the editor.407double delta = 0.0;408if (singleton->last_draw_time) {409delta = (OS::get_singleton()->get_ticks_usec() - singleton->last_draw_time) / 1000000.0;410}411singleton->last_draw_time = OS::get_singleton()->get_ticks_usec();412RenderingServer::get_singleton()->sync();413if (RenderingServer::get_singleton()->has_changed()) {414RenderingServer::get_singleton()->draw(true, delta);415}416EngineDebugger::get_singleton()->send_message("servers:drawn", Array());417} else if (p_cmd == "foreground") {418singleton->last_draw_time = 0.0;419DisplayServer::get_singleton()->window_move_to_foreground();420singleton->servers_profiler->skip_frame();421} else {422r_captured = false;423}424return OK;425}426427void ServersDebugger::_send_resource_usage() {428ServersDebugger::ResourceUsage usage;429430List<RS::TextureInfo> tinfo;431RS::get_singleton()->texture_debug_usage(&tinfo);432433for (const RS::TextureInfo &E : tinfo) {434ServersDebugger::ResourceInfo info;435info.path = E.path;436info.vram = E.bytes;437info.id = E.texture;438439switch (E.type) {440case RS::TextureType::TEXTURE_TYPE_2D:441info.type = "Texture2D";442break;443case RS::TextureType::TEXTURE_TYPE_3D:444info.type = "Texture3D";445break;446case RS::TextureType::TEXTURE_TYPE_LAYERED:447info.type = "TextureLayered";448break;449}450451String possible_type = _get_resource_type_from_path(E.path);452if (!possible_type.is_empty()) {453info.type = possible_type;454}455456if (E.depth == 0) {457info.format = itos(E.width) + "x" + itos(E.height) + " " + Image::get_format_name(E.format);458} else {459info.format = itos(E.width) + "x" + itos(E.height) + "x" + itos(E.depth) + " " + Image::get_format_name(E.format);460}461usage.infos.push_back(info);462}463464List<RS::MeshInfo> mesh_info;465RS::get_singleton()->mesh_debug_usage(&mesh_info);466467for (const RS::MeshInfo &E : mesh_info) {468ServersDebugger::ResourceInfo info;469info.path = E.path;470// We use 64-bit integers to avoid overflow, if for whatever reason, the sum is bigger than 4GB.471uint64_t vram = E.vertex_buffer_size + E.attribute_buffer_size + E.skin_buffer_size + E.index_buffer_size + E.blend_shape_buffer_size + E.lod_index_buffers_size;472// But can info.vram even hold that, and why is it an int instead of an uint?473info.vram = vram;474475// Even though these empty meshes can be indicative of issues somewhere else476// for UX reasons, we don't want to show them.477if (vram == 0 && E.path.is_empty()) {478continue;479}480481info.id = E.mesh;482info.type = "Mesh";483String possible_type = _get_resource_type_from_path(E.path);484if (!possible_type.is_empty()) {485info.type = possible_type;486}487488info.format = itos(E.vertex_count) + " Vertices";489usage.infos.push_back(info);490}491492EngineDebugger::get_singleton()->send_message("servers:memory_usage", usage.serialize());493}494495// Done on a best-effort basis.496String ServersDebugger::_get_resource_type_from_path(const String &p_path) {497if (p_path.is_empty()) {498return "";499}500501if (!ResourceLoader::exists(p_path)) {502return "";503}504505if (ResourceCache::has(p_path)) {506Ref<Resource> resource = ResourceCache::get_ref(p_path);507return resource->get_class();508} else {509// This doesn't work all the time for embedded resources.510String resource_type = ResourceLoader::get_resource_type(p_path);511if (resource_type != "") {512return resource_type;513}514}515516return "";517}518519ServersDebugger::ServersDebugger() {520singleton = this;521522// Generic servers profiler (audio/physics/...)523servers_profiler.instantiate();524servers_profiler->bind("servers");525526// Visual Profiler (cpu/gpu times)527visual_profiler.instantiate();528visual_profiler->bind("visual");529530EngineDebugger::Capture servers_cap(nullptr, &_capture);531EngineDebugger::register_message_capture("servers", servers_cap);532}533534ServersDebugger::~ServersDebugger() {535EngineDebugger::unregister_message_capture("servers");536singleton = nullptr;537}538539540