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