Flutter Engine
The Flutter Engine
app_snapshot.h
Go to the documentation of this file.
1// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4
5#ifndef RUNTIME_VM_APP_SNAPSHOT_H_
6#define RUNTIME_VM_APP_SNAPSHOT_H_
7
8#include "platform/assert.h"
9#include "vm/allocation.h"
10#include "vm/bitfield.h"
11#include "vm/datastream.h"
12#include "vm/globals.h"
13#include "vm/growable_array.h"
14#include "vm/hash_map.h"
15#include "vm/object.h"
16#include "vm/snapshot.h"
17
18namespace dart {
19
20// For full snapshots, we use a clustered snapshot format that trades longer
21// serialization time for faster deserialization time and smaller snapshots.
22// Objects are clustered by class to allow writing type information once per
23// class instead once per object, and to allow filling the objects in a tight
24// loop. The snapshot has two major sections: the first describes how to
25// allocate the objects and the second describes how to initialize them.
26// Deserialization starts by allocating a reference array large enough to hold
27// the base objects (objects already available to both the serializer and
28// deserializer) and the objects written in the snapshot. The allocation section
29// is then read for each cluster, filling the reference array. Then the
30// initialization/fill secton is read for each cluster, using the indices into
31// the reference array to fill pointers. At this point, every object has been
32// touched exactly once and in order, making this approach very cache friendly.
33// Finally, each cluster is given an opportunity to perform some fix-ups that
34// require the graph has been fully loaded, such as rehashing, though most
35// clusters do not require fixups.
36
37// Forward declarations.
38class V8SnapshotProfileWriter;
39class ImageWriter;
40class Heap;
41
43 public:
46 : id_(id), parent_(parent), deferred_objects_(), objects_(nullptr) {}
47
48 intptr_t id() const { return id_; }
49 LoadingUnitSerializationData* parent() const { return parent_; }
50 void AddDeferredObject(CodePtr obj) {
51 deferred_objects_.Add(&Code::ZoneHandle(obj));
52 }
53 GrowableArray<Code*>* deferred_objects() { return &deferred_objects_; }
55 ASSERT(objects_ != nullptr);
56 return objects_;
57 }
59 ASSERT(objects_ == nullptr);
60 objects_ = objects;
61 }
62
63 private:
64 intptr_t id_;
66 GrowableArray<Code*> deferred_objects_;
68};
69
70// This class can be used to read version and features from a snapshot before
71// the VM has been initialized.
73 public:
74 static char* InitializeGlobalVMFlagsFromSnapshot(const Snapshot* snapshot);
75
76 explicit SnapshotHeaderReader(const Snapshot* snapshot)
77 : SnapshotHeaderReader(snapshot->kind(),
78 snapshot->Addr(),
79 snapshot->length()) {}
80
82 const uint8_t* buffer,
83 intptr_t size)
84 : kind_(kind), stream_(buffer, size) {
86 }
87
89
90 // Verifies the version and features in the snapshot are compatible with the
91 // current VM. If isolate is non-null it validates isolate-specific features.
92 //
93 // Returns null on success and a malloc()ed error on failure.
94 // The [offset] will be the next position in the snapshot stream after the
95 // features.
96 char* VerifyVersionAndFeatures(IsolateGroup* isolate_group, intptr_t* offset);
97
98 private:
99 char* VerifyVersion();
100 char* ReadFeatures(const char** features, intptr_t* features_length);
101 char* VerifyFeatures(IsolateGroup* isolate_group);
102 char* BuildError(const char* message);
103
104 Snapshot::Kind kind_;
105 ReadStream stream_;
106};
107
109 public:
110 static constexpr intptr_t kInitialSize = 64 * KB;
114 ImageWriter* vm_image_writer,
115 ImageWriter* iso_image_writer);
117
118 // Writes a full snapshot of the program(VM isolate, regular isolate group).
123 uint32_t program_hash);
124
125 intptr_t VmIsolateSnapshotSize() const { return vm_isolate_snapshot_size_; }
126 intptr_t IsolateSnapshotSize() const { return isolate_snapshot_size_; }
127
128 private:
129 Thread* thread() const { return thread_; }
130 Zone* zone() const { return thread_->zone(); }
131 IsolateGroup* isolate_group() const { return thread_->isolate_group(); }
132 Heap* heap() const { return isolate_group()->heap(); }
133
134 // Writes a snapshot of the VM Isolate.
135 ZoneGrowableArray<Object*>* WriteVMSnapshot();
136
137 // Writes a full snapshot of regular Dart isolate group.
138 void WriteProgramSnapshot(ZoneGrowableArray<Object*>* objects,
139 GrowableArray<LoadingUnitSerializationData*>* data);
140
141 Thread* thread_;
142 Snapshot::Kind kind_;
143 NonStreamingWriteStream* const vm_snapshot_data_;
144 NonStreamingWriteStream* const isolate_snapshot_data_;
145 intptr_t vm_isolate_snapshot_size_;
146 intptr_t isolate_snapshot_size_;
147 ImageWriter* vm_image_writer_;
148 ImageWriter* isolate_image_writer_;
149
150 // Stats for benchmarking.
151 intptr_t clustered_vm_size_ = 0;
152 intptr_t clustered_isolate_size_ = 0;
153 intptr_t mapped_data_size_ = 0;
154 intptr_t mapped_text_size_ = 0;
155 intptr_t heap_vm_size_ = 0;
156 intptr_t heap_isolate_size_ = 0;
157
158 V8SnapshotProfileWriter* profile_writer_ = nullptr;
159
160 DISALLOW_COPY_AND_ASSIGN(FullSnapshotWriter);
161};
162
164 public:
165 FullSnapshotReader(const Snapshot* snapshot,
166 const uint8_t* instructions_buffer,
167 Thread* thread);
169
170 ApiErrorPtr ReadVMSnapshot();
171 ApiErrorPtr ReadProgramSnapshot();
172 ApiErrorPtr ReadUnitSnapshot(const LoadingUnit& unit);
173
174 private:
175 IsolateGroup* isolate_group() const { return thread_->isolate_group(); }
176
177 ApiErrorPtr ConvertToApiError(char* message);
178 void InitializeBSS();
179
180 Snapshot::Kind kind_;
181 Thread* thread_;
182 const uint8_t* buffer_;
183 intptr_t size_;
184 const uint8_t* data_image_;
185 const uint8_t* instructions_image_;
186
187 DISALLOW_COPY_AND_ASSIGN(FullSnapshotReader);
188};
189
190} // namespace dart
191
192#endif // RUNTIME_VM_APP_SNAPSHOT_H_
ApiErrorPtr ReadUnitSnapshot(const LoadingUnit &unit)
ApiErrorPtr ReadProgramSnapshot()
FullSnapshotReader(const Snapshot *snapshot, const uint8_t *instructions_buffer, Thread *thread)
ApiErrorPtr ReadVMSnapshot()
FullSnapshotWriter(Snapshot::Kind kind, NonStreamingWriteStream *vm_snapshot_data, NonStreamingWriteStream *isolate_snapshot_data, ImageWriter *vm_image_writer, ImageWriter *iso_image_writer)
intptr_t VmIsolateSnapshotSize() const
Definition: app_snapshot.h:125
void WriteFullSnapshot(GrowableArray< LoadingUnitSerializationData * > *data=nullptr)
intptr_t IsolateSnapshotSize() const
Definition: app_snapshot.h:126
void WriteUnitSnapshot(GrowableArray< LoadingUnitSerializationData * > *units, LoadingUnitSerializationData *unit, uint32_t program_hash)
static constexpr intptr_t kInitialSize
Definition: app_snapshot.h:110
Heap * heap() const
Definition: isolate.h:296
void set_objects(ZoneGrowableArray< Object * > *objects)
Definition: app_snapshot.h:58
ZoneGrowableArray< Object * > * objects()
Definition: app_snapshot.h:54
LoadingUnitSerializationData * parent() const
Definition: app_snapshot.h:49
void AddDeferredObject(CodePtr obj)
Definition: app_snapshot.h:50
GrowableArray< Code * > * deferred_objects()
Definition: app_snapshot.h:53
LoadingUnitSerializationData(intptr_t id, LoadingUnitSerializationData *parent)
Definition: app_snapshot.h:44
static Object & ZoneHandle()
Definition: object.h:419
void SetPosition(intptr_t value)
Definition: datastream.h:128
SnapshotHeaderReader(Snapshot::Kind kind, const uint8_t *buffer, intptr_t size)
Definition: app_snapshot.h:81
void SetCoverageFromSnapshotFeatures(IsolateGroup *isolate_group)
SnapshotHeaderReader(const Snapshot *snapshot)
Definition: app_snapshot.h:76
char * VerifyVersionAndFeatures(IsolateGroup *isolate_group, intptr_t *offset)
static char * InitializeGlobalVMFlagsFromSnapshot(const Snapshot *snapshot)
static constexpr intptr_t kHeaderSize
Definition: snapshot.h:43
Zone * zone() const
Definition: thread_state.h:37
IsolateGroup * isolate_group() const
Definition: thread.h:541
#define ASSERT(E)
size_t length
Win32Message message
const uint8_t * isolate_snapshot_data
Definition: gen_snapshot.cc:69
const uint8_t * vm_snapshot_data
Definition: main_impl.cc:52
Definition: dart_vm.cc:33
constexpr intptr_t KB
Definition: globals.h:528
static int8_t data[kExtLength]
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace buffer
Definition: switches.h:126
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259
SeparatedVector2 offset