Path: blob/master/modules/objectdb_profiler/snapshot_collector.cpp
11352 views
/**************************************************************************/1/* snapshot_collector.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 "snapshot_collector.h"3132#include "core/core_bind.h"33#include "core/debugger/engine_debugger.h"34#include "core/io/compression.h"35#include "core/os/time.h"36#include "core/version.h"37#include "scene/main/node.h"38#include "scene/main/window.h"3940void SnapshotCollector::initialize() {41pending_snapshots.clear();42EngineDebugger::register_message_capture("snapshot", EngineDebugger::Capture(nullptr, SnapshotCollector::parse_message));43}4445void SnapshotCollector::deinitialize() {46EngineDebugger::unregister_message_capture("snapshot");47pending_snapshots.clear();48}4950void SnapshotCollector::snapshot_objects(Array *p_arr, Dictionary &p_snapshot_context) {51print_verbose("Starting to snapshot");52p_arr->clear();5354// Gather all ObjectIDs first. The ObjectDB will be locked in debug_objects, so we can't serialize until it exits.5556// In rare cases, the object may be deleted as the snapshot is taken. So, we store the object's class name to give users a clue about what went wrong.57LocalVector<Pair<ObjectID, StringName>> debugger_object_ids;58debugger_object_ids.reserve(ObjectDB::get_object_count());5960ObjectDB::debug_objects(61[](Object *p_obj, void *p_user_data) {62LocalVector<Pair<ObjectID, StringName>> *debugger_object_ids_ptr = (LocalVector<Pair<ObjectID, StringName>> *)p_user_data;63debugger_object_ids_ptr->push_back(Pair<ObjectID, StringName>(p_obj->get_instance_id(), p_obj->get_class_name()));64},65(void *)&debugger_object_ids);6667// Get SnapshotDataTransportObject from ObjectID list now that DB is unlocked.68LocalVector<SnapshotDataTransportObject> debugger_objects;69debugger_objects.reserve(debugger_object_ids.size());70for (Pair<ObjectID, StringName> ids : debugger_object_ids) {71ObjectID oid = ids.first;72Object *obj = ObjectDB::get_instance(oid);73if (unlikely(obj == nullptr)) {74print_verbose(vformat("Object of class '%s' with ID %ud was found to be deleted after ObjectDB was snapshotted.", ids.second, (uint64_t)oid));75continue;76}7778if (ids.second == SNAME("EditorInterface")) {79// The EditorInterface + EditorNode is _kind of_ constructed in a debug game, but many properties are null80// We can prevent it from being constructed, but that would break other projects so better to just skip it.81continue;82}8384// This is the same way objects in the remote scene tree are serialized,85// but here we add a few extra properties via the extra_debug_data dictionary.86SnapshotDataTransportObject debug_data(obj);8788// If we're RefCounted, send over our RefCount too. Could add code here to add a few other interesting properties.89RefCounted *ref = Object::cast_to<RefCounted>(obj);90if (ref) {91debug_data.extra_debug_data["ref_count"] = ref->get_reference_count();92}9394Node *node = Object::cast_to<Node>(obj);95if (node) {96debug_data.extra_debug_data["node_name"] = node->get_name();97if (node->get_parent() != nullptr) {98debug_data.extra_debug_data["node_parent"] = node->get_parent()->get_instance_id();99}100101debug_data.extra_debug_data["node_is_scene_root"] = SceneTree::get_singleton()->get_root() == node;102103Array children;104for (int i = 0; i < node->get_child_count(); i++) {105children.push_back(node->get_child(i)->get_instance_id());106}107debug_data.extra_debug_data["node_children"] = children;108}109110debugger_objects.push_back(debug_data);111}112113// Add a header to the snapshot with general data about the state of the game, not tied to any particular object.114p_snapshot_context["mem_usage"] = Memory::get_mem_usage();115p_snapshot_context["mem_max_usage"] = Memory::get_mem_max_usage();116p_snapshot_context["timestamp"] = Time::get_singleton()->get_unix_time_from_system();117p_snapshot_context["game_version"] = get_godot_version_string();118p_arr->push_back(p_snapshot_context);119for (SnapshotDataTransportObject &debug_data : debugger_objects) {120debug_data.serialize(*p_arr);121p_arr->push_back(debug_data.extra_debug_data);122}123124print_verbose("Snapshot size: " + String::num_uint64(p_arr->size()));125}126127Error SnapshotCollector::parse_message(void *p_user, const String &p_msg, const Array &p_args, bool &r_captured) {128r_captured = true;129if (p_msg == "request_prepare_snapshot") {130int request_id = p_args[0];131Dictionary snapshot_context;132snapshot_context["editor_version"] = (String)p_args[1];133Array objects;134snapshot_objects(&objects, snapshot_context);135// Debugger networking has a limit on both how many objects can be queued to send and how136// many bytes can be queued to send. Serializing to a string means we never hit the object137// limit, and only have to deal with the byte limit.138// Compress the snapshot in the game client to make sending the snapshot from game to editor a little faster.139CoreBind::Marshalls *m = CoreBind::Marshalls::get_singleton();140Vector<uint8_t> objs_buffer = m->base64_to_raw(m->variant_to_base64(objects));141Vector<uint8_t> objs_buffer_compressed;142objs_buffer_compressed.resize(objs_buffer.size());143int new_size = Compression::compress(objs_buffer_compressed.ptrw(), objs_buffer.ptrw(), objs_buffer.size(), Compression::MODE_DEFLATE);144objs_buffer_compressed.resize(new_size);145pending_snapshots[request_id] = objs_buffer_compressed;146147// Tell the editor how long the snapshot is.148Array resp = { request_id, pending_snapshots[request_id].size() };149EngineDebugger::get_singleton()->send_message("snapshot:snapshot_prepared", resp);150151} else if (p_msg == "request_snapshot_chunk") {152int request_id = p_args[0];153int begin = p_args[1];154int end = p_args[2];155156Array resp = { request_id, pending_snapshots[request_id].slice(begin, end) };157EngineDebugger::get_singleton()->send_message("snapshot:snapshot_chunk", resp);158159// If we sent the last part of the string, delete it locally.160if (end >= pending_snapshots[request_id].size()) {161pending_snapshots.erase(request_id);162}163} else {164r_captured = false;165}166return OK;167}168169String SnapshotCollector::get_godot_version_string() {170String hash = String(VERSION_HASH);171if (hash.length() != 0) {172hash = " " + vformat("[%s]", hash.left(9));173}174return "v" VERSION_FULL_BUILD + hash;175}176177178