Flutter Engine
 
Loading...
Searching...
No Matches
dart_snapshot.h
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef FLUTTER_RUNTIME_DART_SNAPSHOT_H_
6#define FLUTTER_RUNTIME_DART_SNAPSHOT_H_
7
8#include <memory>
9
11#include "flutter/fml/macros.h"
13
14namespace flutter {
15
16//------------------------------------------------------------------------------
17/// @brief A read-only Dart heap snapshot, or, read-executable mapping of
18/// AOT compiled Dart code.
19///
20/// To make Dart code startup more performant, the Flutter tools on
21/// the host snapshot the state of the Dart heap at specific points
22/// and package the same with the Flutter application. When the Dart
23/// VM on the target is configured to run AOT compiled Dart code,
24/// the tools also compile the developer's Flutter application code
25/// to target specific machine code (instructions). This class deals
26/// with the mapping of both these buffers at runtime on the target.
27///
28/// A Flutter application typically needs two instances of this
29/// class at runtime to run Dart code. One instance is for the VM
30/// and is called the "core snapshot". The other is the isolate
31/// and called the "isolate snapshot". Different root isolates can
32/// be launched with different isolate snapshots.
33///
34/// These snapshots are typically memory-mapped at runtime, or,
35/// referenced directly as symbols present in Mach or ELF binaries.
36///
37/// In the case of the core snapshot, the snapshot is collected when
38/// the VM shuts down. The isolate snapshot is collected when the
39/// isolate group shuts down.
40///
41class DartSnapshot : public fml::RefCountedThreadSafe<DartSnapshot> {
42 public:
43 //----------------------------------------------------------------------------
44 /// The symbol name of the heap data of the core snapshot in a dynamic library
45 /// or currently loaded process.
46 ///
47 static const char* kVMDataSymbol;
48 //----------------------------------------------------------------------------
49 /// The symbol name of the instructions data of the core snapshot in a dynamic
50 /// library or currently loaded process.
51 ///
52 static const char* kVMInstructionsSymbol;
53 //----------------------------------------------------------------------------
54 /// The symbol name of the heap data of the isolate snapshot in a dynamic
55 /// library or currently loaded process.
56 ///
57 static const char* kIsolateDataSymbol;
58 //----------------------------------------------------------------------------
59 /// The symbol name of the instructions data of the isolate snapshot in a
60 /// dynamic library or currently loaded process.
61 ///
62 static const char* kIsolateInstructionsSymbol;
63
64 //----------------------------------------------------------------------------
65 /// @brief From the fields present in the given settings object, infer
66 /// the core snapshot.
67 ///
68 /// @attention Depending on the runtime mode of the Flutter application and
69 /// the target that Flutter is running on, a complex fallback
70 /// mechanism is in place to infer the locations of each snapshot
71 /// buffer. If the caller wants to explicitly specify the buffers
72 /// of the core snapshot, the `Settings::vm_snapshot_data` and
73 /// `Settings::vm_snapshots_instr` mapping fields may be used.
74 /// This specification takes precedence over all fallback search
75 /// paths.
76 ///
77 /// @param[in] settings The settings to infer the core snapshot from.
78 ///
79 /// @return A valid core snapshot or nullptr.
80 ///
82 const Settings& settings);
83
84 //----------------------------------------------------------------------------
85 /// @brief From the fields present in the given settings object, infer
86 /// the isolate snapshot.
87 ///
88 /// @attention Depending on the runtime mode of the Flutter application and
89 /// the target that Flutter is running on, a complex fallback
90 /// mechanism is in place to infer the locations of each snapshot
91 /// buffer. If the caller wants to explicitly specify the buffers
92 /// of the isolate snapshot, the `Settings::isolate_snapshot_data`
93 /// and `Settings::isolate_snapshots_instr` mapping fields may be
94 /// used. This specification takes precedence over all fallback
95 /// search paths.
96 ///
97 /// @param[in] settings The settings to infer the isolate snapshot from.
98 ///
99 /// @return A valid isolate snapshot or nullptr.
100 ///
102 const Settings& settings);
103
104 //----------------------------------------------------------------------------
105 /// @brief Create an isolate snapshot from existing fml::Mappings.
106 ///
107 /// @param[in] snapshot_data The mapping for the heap snapshot.
108 /// @param[in] snapshot_instructions The mapping for the instructions
109 /// snapshot.
110 ///
111 /// @return A valid isolate snapshot or nullptr.
113 const std::shared_ptr<const fml::Mapping>& snapshot_data,
114 const std::shared_ptr<const fml::Mapping>& snapshot_instructions);
115
116 //----------------------------------------------------------------------------
117 /// @brief Create an isolate snapshot specialized for launching the
118 /// service isolate. Returns nullptr if no such snapshot is
119 /// available.
120 ///
121 /// @return A valid isolate snapshot or nullptr.
123 const Settings& settings);
124
125 //----------------------------------------------------------------------------
126 /// @brief Determines if this snapshot contains a heap component. Since
127 /// the instructions component is optional, the method does not
128 /// check for its presence. Use `IsValidForAOT` to determine if
129 /// both the heap and instructions components of the snapshot are
130 /// present.
131 ///
132 /// @return Returns if the snapshot contains a heap component.
133 ///
134 bool IsValid() const;
135
136 //----------------------------------------------------------------------------
137 /// @brief Determines if this snapshot contains both the heap and
138 /// instructions components. This is only useful when determining
139 /// if the snapshot may be used to run AOT code. The instructions
140 /// component will be absent in JIT modes.
141 ///
142 /// @return Returns if the snapshot contains both a heap and instructions
143 /// component.
144 ///
145 bool IsValidForAOT() const;
146
147 //----------------------------------------------------------------------------
148 /// @brief Get a pointer to the read-only mapping to the heap snapshot.
149 ///
150 /// @return The data mapping.
151 ///
152 const uint8_t* GetDataMapping() const;
153
154 //----------------------------------------------------------------------------
155 /// @brief Get a pointer to the read-execute mapping to the instructions
156 /// snapshot.
157 ///
158 /// @return The instructions mapping.
159 ///
160 const uint8_t* GetInstructionsMapping() const;
161
162 //----------------------------------------------------------------------------
163 /// @brief Returns whether both the data and instructions mappings are
164 /// safe to use with madvise(DONTNEED).
165 bool IsDontNeedSafe() const;
166
168 const fml::Mapping* application_kernel_mapping) const;
169
170 private:
171 std::shared_ptr<const fml::Mapping> data_;
172 std::shared_ptr<const fml::Mapping> instructions_;
173
174 DartSnapshot(std::shared_ptr<const fml::Mapping> data,
175 std::shared_ptr<const fml::Mapping> instructions);
176
178
182};
183
184} // namespace flutter
185
186#endif // FLUTTER_RUNTIME_DART_SNAPSHOT_H_
A read-only Dart heap snapshot, or, read-executable mapping of AOT compiled Dart code.
bool IsNullSafetyEnabled(const fml::Mapping *application_kernel_mapping) const
bool IsValidForAOT() const
Determines if this snapshot contains both the heap and instructions components. This is only useful w...
static fml::RefPtr< DartSnapshot > VMServiceIsolateSnapshotFromSettings(const Settings &settings)
Create an isolate snapshot specialized for launching the service isolate. Returns nullptr if no such ...
static fml::RefPtr< const DartSnapshot > VMSnapshotFromSettings(const Settings &settings)
From the fields present in the given settings object, infer the core snapshot.
static fml::RefPtr< DartSnapshot > IsolateSnapshotFromMappings(const std::shared_ptr< const fml::Mapping > &snapshot_data, const std::shared_ptr< const fml::Mapping > &snapshot_instructions)
Create an isolate snapshot from existing fml::Mappings.
bool IsValid() const
Determines if this snapshot contains a heap component. Since the instructions component is optional,...
const uint8_t * GetInstructionsMapping() const
Get a pointer to the read-execute mapping to the instructions snapshot.
static const char * kIsolateDataSymbol
static const char * kVMDataSymbol
const uint8_t * GetDataMapping() const
Get a pointer to the read-only mapping to the heap snapshot.
static const char * kIsolateInstructionsSymbol
bool IsDontNeedSafe() const
Returns whether both the data and instructions mappings are safe to use with madvise(DONTNEED).
static fml::RefPtr< const DartSnapshot > IsolateSnapshotFromSettings(const Settings &settings)
From the fields present in the given settings object, infer the isolate snapshot.
static const char * kVMInstructionsSymbol
#define FML_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition macros.h:27
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot data
Definition switch_defs.h:36
#define FML_FRIEND_REF_COUNTED_THREAD_SAFE(T)
#define FML_FRIEND_MAKE_REF_COUNTED(T)