Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/objectdb_profiler/snapshot_collector.cpp
11352 views
1
/**************************************************************************/
2
/* snapshot_collector.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 "snapshot_collector.h"
32
33
#include "core/core_bind.h"
34
#include "core/debugger/engine_debugger.h"
35
#include "core/io/compression.h"
36
#include "core/os/time.h"
37
#include "core/version.h"
38
#include "scene/main/node.h"
39
#include "scene/main/window.h"
40
41
void SnapshotCollector::initialize() {
42
pending_snapshots.clear();
43
EngineDebugger::register_message_capture("snapshot", EngineDebugger::Capture(nullptr, SnapshotCollector::parse_message));
44
}
45
46
void SnapshotCollector::deinitialize() {
47
EngineDebugger::unregister_message_capture("snapshot");
48
pending_snapshots.clear();
49
}
50
51
void SnapshotCollector::snapshot_objects(Array *p_arr, Dictionary &p_snapshot_context) {
52
print_verbose("Starting to snapshot");
53
p_arr->clear();
54
55
// Gather all ObjectIDs first. The ObjectDB will be locked in debug_objects, so we can't serialize until it exits.
56
57
// 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.
58
LocalVector<Pair<ObjectID, StringName>> debugger_object_ids;
59
debugger_object_ids.reserve(ObjectDB::get_object_count());
60
61
ObjectDB::debug_objects(
62
[](Object *p_obj, void *p_user_data) {
63
LocalVector<Pair<ObjectID, StringName>> *debugger_object_ids_ptr = (LocalVector<Pair<ObjectID, StringName>> *)p_user_data;
64
debugger_object_ids_ptr->push_back(Pair<ObjectID, StringName>(p_obj->get_instance_id(), p_obj->get_class_name()));
65
},
66
(void *)&debugger_object_ids);
67
68
// Get SnapshotDataTransportObject from ObjectID list now that DB is unlocked.
69
LocalVector<SnapshotDataTransportObject> debugger_objects;
70
debugger_objects.reserve(debugger_object_ids.size());
71
for (Pair<ObjectID, StringName> ids : debugger_object_ids) {
72
ObjectID oid = ids.first;
73
Object *obj = ObjectDB::get_instance(oid);
74
if (unlikely(obj == nullptr)) {
75
print_verbose(vformat("Object of class '%s' with ID %ud was found to be deleted after ObjectDB was snapshotted.", ids.second, (uint64_t)oid));
76
continue;
77
}
78
79
if (ids.second == SNAME("EditorInterface")) {
80
// The EditorInterface + EditorNode is _kind of_ constructed in a debug game, but many properties are null
81
// We can prevent it from being constructed, but that would break other projects so better to just skip it.
82
continue;
83
}
84
85
// This is the same way objects in the remote scene tree are serialized,
86
// but here we add a few extra properties via the extra_debug_data dictionary.
87
SnapshotDataTransportObject debug_data(obj);
88
89
// If we're RefCounted, send over our RefCount too. Could add code here to add a few other interesting properties.
90
RefCounted *ref = Object::cast_to<RefCounted>(obj);
91
if (ref) {
92
debug_data.extra_debug_data["ref_count"] = ref->get_reference_count();
93
}
94
95
Node *node = Object::cast_to<Node>(obj);
96
if (node) {
97
debug_data.extra_debug_data["node_name"] = node->get_name();
98
if (node->get_parent() != nullptr) {
99
debug_data.extra_debug_data["node_parent"] = node->get_parent()->get_instance_id();
100
}
101
102
debug_data.extra_debug_data["node_is_scene_root"] = SceneTree::get_singleton()->get_root() == node;
103
104
Array children;
105
for (int i = 0; i < node->get_child_count(); i++) {
106
children.push_back(node->get_child(i)->get_instance_id());
107
}
108
debug_data.extra_debug_data["node_children"] = children;
109
}
110
111
debugger_objects.push_back(debug_data);
112
}
113
114
// Add a header to the snapshot with general data about the state of the game, not tied to any particular object.
115
p_snapshot_context["mem_usage"] = Memory::get_mem_usage();
116
p_snapshot_context["mem_max_usage"] = Memory::get_mem_max_usage();
117
p_snapshot_context["timestamp"] = Time::get_singleton()->get_unix_time_from_system();
118
p_snapshot_context["game_version"] = get_godot_version_string();
119
p_arr->push_back(p_snapshot_context);
120
for (SnapshotDataTransportObject &debug_data : debugger_objects) {
121
debug_data.serialize(*p_arr);
122
p_arr->push_back(debug_data.extra_debug_data);
123
}
124
125
print_verbose("Snapshot size: " + String::num_uint64(p_arr->size()));
126
}
127
128
Error SnapshotCollector::parse_message(void *p_user, const String &p_msg, const Array &p_args, bool &r_captured) {
129
r_captured = true;
130
if (p_msg == "request_prepare_snapshot") {
131
int request_id = p_args[0];
132
Dictionary snapshot_context;
133
snapshot_context["editor_version"] = (String)p_args[1];
134
Array objects;
135
snapshot_objects(&objects, snapshot_context);
136
// Debugger networking has a limit on both how many objects can be queued to send and how
137
// many bytes can be queued to send. Serializing to a string means we never hit the object
138
// limit, and only have to deal with the byte limit.
139
// Compress the snapshot in the game client to make sending the snapshot from game to editor a little faster.
140
CoreBind::Marshalls *m = CoreBind::Marshalls::get_singleton();
141
Vector<uint8_t> objs_buffer = m->base64_to_raw(m->variant_to_base64(objects));
142
Vector<uint8_t> objs_buffer_compressed;
143
objs_buffer_compressed.resize(objs_buffer.size());
144
int new_size = Compression::compress(objs_buffer_compressed.ptrw(), objs_buffer.ptrw(), objs_buffer.size(), Compression::MODE_DEFLATE);
145
objs_buffer_compressed.resize(new_size);
146
pending_snapshots[request_id] = objs_buffer_compressed;
147
148
// Tell the editor how long the snapshot is.
149
Array resp = { request_id, pending_snapshots[request_id].size() };
150
EngineDebugger::get_singleton()->send_message("snapshot:snapshot_prepared", resp);
151
152
} else if (p_msg == "request_snapshot_chunk") {
153
int request_id = p_args[0];
154
int begin = p_args[1];
155
int end = p_args[2];
156
157
Array resp = { request_id, pending_snapshots[request_id].slice(begin, end) };
158
EngineDebugger::get_singleton()->send_message("snapshot:snapshot_chunk", resp);
159
160
// If we sent the last part of the string, delete it locally.
161
if (end >= pending_snapshots[request_id].size()) {
162
pending_snapshots.erase(request_id);
163
}
164
} else {
165
r_captured = false;
166
}
167
return OK;
168
}
169
170
String SnapshotCollector::get_godot_version_string() {
171
String hash = String(VERSION_HASH);
172
if (hash.length() != 0) {
173
hash = " " + vformat("[%s]", hash.left(9));
174
}
175
return "v" VERSION_FULL_BUILD + hash;
176
}
177
178