Skip to content

Commit

Permalink
[fuchsia] Plumbing for sharing between AOT snapshots. (flutter#5351)
Browse files Browse the repository at this point in the history
  • Loading branch information
rmacnak-google authored May 24, 2018
1 parent 416418f commit fb709e2
Show file tree
Hide file tree
Showing 13 changed files with 91 additions and 22 deletions.
40 changes: 26 additions & 14 deletions runtime/dart_isolate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ namespace blink {
fml::WeakPtr<DartIsolate> DartIsolate::CreateRootIsolate(
const DartVM* vm,
fxl::RefPtr<DartSnapshot> isolate_snapshot,
fxl::RefPtr<DartSnapshot> shared_snapshot,
TaskRunners task_runners,
std::unique_ptr<Window> window,
fml::WeakPtr<GrContext> resource_context,
Expand All @@ -54,6 +55,7 @@ fml::WeakPtr<DartIsolate> DartIsolate::CreateRootIsolate(
auto root_embedder_data = std::make_unique<DartIsolate>(
vm, // VM
std::move(isolate_snapshot), // isolate snapshot
std::move(shared_snapshot), // shared snapshot
task_runners, // task runners
std::move(resource_context), // resource context
std::move(unref_queue), // skia unref queue
Expand Down Expand Up @@ -94,6 +96,7 @@ fml::WeakPtr<DartIsolate> DartIsolate::CreateRootIsolate(

DartIsolate::DartIsolate(const DartVM* vm,
fxl::RefPtr<DartSnapshot> isolate_snapshot,
fxl::RefPtr<DartSnapshot> shared_snapshot,
TaskRunners task_runners,
fml::WeakPtr<GrContext> resource_context,
fxl::RefPtr<flow::SkiaUnrefQueue> unref_queue,
Expand All @@ -110,6 +113,7 @@ DartIsolate::DartIsolate(const DartVM* vm,
vm->GetSettings().log_tag),
vm_(vm),
isolate_snapshot_(std::move(isolate_snapshot)),
shared_snapshot_(std::move(shared_snapshot)),
child_isolate_preparer_(std::move(child_isolate_preparer)),
weak_factory_(std::make_unique<fml::WeakPtrFactory<DartIsolate>>(this)) {
FXL_DCHECK(isolate_snapshot_) << "Must contain a valid isolate snapshot.";
Expand Down Expand Up @@ -285,8 +289,8 @@ static bool LoadScriptSnapshot(std::shared_ptr<const fml::Mapping> mapping,

static bool LoadKernelSnapshot(std::shared_ptr<const fml::Mapping> mapping,
bool last_piece) {
Dart_Handle library = Dart_LoadLibraryFromKernel(mapping->GetMapping(),
mapping->GetSize());
Dart_Handle library =
Dart_LoadLibraryFromKernel(mapping->GetMapping(), mapping->GetSize());
if (tonic::LogIfError(library)) {
return false;
}
Expand Down Expand Up @@ -533,6 +537,7 @@ Dart_Isolate DartIsolate::DartCreateAndStartServiceIsolate(
DartIsolate::CreateRootIsolate(
vm.get(), // vm
vm->GetIsolateSnapshot(), // isolate snapshot
vm->GetSharedSnapshot(), // shared snapshot
null_task_runners, // task runners
nullptr, // window
{}, // resource context
Expand Down Expand Up @@ -657,6 +662,7 @@ DartIsolate::CreateDartVMAndEmbedderObjectPair(
embedder_isolate = std::make_unique<DartIsolate>(
vm, // vm
raw_embedder_isolate->GetIsolateSnapshot(), // isolate_snapshot
raw_embedder_isolate->GetSharedSnapshot(), // shared_snapshot
null_task_runners, // task_runners
fml::WeakPtr<GrContext>{}, // resource_context
nullptr, // unref_queue
Expand All @@ -678,18 +684,20 @@ DartIsolate::CreateDartVMAndEmbedderObjectPair(
embedder_isolate.get(), //
error //
)
: Dart_CreateIsolate(advisory_script_uri, //
advisory_script_entrypoint, //
embedder_isolate->GetIsolateSnapshot()
->GetData()
->GetSnapshotPointer(), //
embedder_isolate->GetIsolateSnapshot()
->GetInstructionsIfPresent(), //
nullptr, //
nullptr, //
flags, //
embedder_isolate.get(), //
error //
: Dart_CreateIsolate(
advisory_script_uri, //
advisory_script_entrypoint, //
embedder_isolate->GetIsolateSnapshot()
->GetData()
->GetSnapshotPointer(), //
embedder_isolate->GetIsolateSnapshot()
->GetInstructionsIfPresent(), //
embedder_isolate->GetSharedSnapshot()->GetDataIfPresent(), //
embedder_isolate->GetSharedSnapshot()
->GetInstructionsIfPresent(), //
flags, //
embedder_isolate.get(), //
error //
);

if (isolate == nullptr) {
Expand Down Expand Up @@ -753,6 +761,10 @@ fxl::RefPtr<DartSnapshot> DartIsolate::GetIsolateSnapshot() const {
return isolate_snapshot_;
}

fxl::RefPtr<DartSnapshot> DartIsolate::GetSharedSnapshot() const {
return shared_snapshot_;
}

fml::WeakPtr<DartIsolate> DartIsolate::GetWeakIsolatePtr() const {
return weak_factory_ ? weak_factory_->GetWeakPtr()
: fml::WeakPtr<DartIsolate>();
Expand Down
4 changes: 4 additions & 0 deletions runtime/dart_isolate.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class DartIsolate : public UIDartState {
static fml::WeakPtr<DartIsolate> CreateRootIsolate(
const DartVM* vm,
fxl::RefPtr<DartSnapshot> isolate_snapshot,
fxl::RefPtr<DartSnapshot> shared_snapshot,
TaskRunners task_runners,
std::unique_ptr<Window> window,
fml::WeakPtr<GrContext> resource_context,
Expand All @@ -51,6 +52,7 @@ class DartIsolate : public UIDartState {

DartIsolate(const DartVM* vm,
fxl::RefPtr<DartSnapshot> isolate_snapshot,
fxl::RefPtr<DartSnapshot> shared_snapshot,
TaskRunners task_runners,
fml::WeakPtr<GrContext> resource_context,
fxl::RefPtr<flow::SkiaUnrefQueue> unref_queue,
Expand Down Expand Up @@ -85,6 +87,7 @@ class DartIsolate : public UIDartState {
const DartVM* GetDartVM() const;

fxl::RefPtr<DartSnapshot> GetIsolateSnapshot() const;
fxl::RefPtr<DartSnapshot> GetSharedSnapshot() const;

fml::WeakPtr<DartIsolate> GetWeakIsolatePtr() const;

Expand All @@ -107,6 +110,7 @@ class DartIsolate : public UIDartState {
const DartVM* vm_ = nullptr;
Phase phase_ = Phase::Unknown;
const fxl::RefPtr<DartSnapshot> isolate_snapshot_;
const fxl::RefPtr<DartSnapshot> shared_snapshot_;
std::vector<std::unique_ptr<AutoFireClosure>> shutdown_callbacks_;
ChildIsolatePreparer child_isolate_preparer_;
std::unique_ptr<fml::WeakPtrFactory<DartIsolate>> weak_factory_;
Expand Down
3 changes: 3 additions & 0 deletions runtime/dart_isolate_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ TEST_F(DartIsolateTest, RootIsolateCreationAndShutdown) {
auto root_isolate = DartIsolate::CreateRootIsolate(
vm.get(), // vm
vm->GetIsolateSnapshot(), // isolate snapshot
vm->GetSharedSnapshot(), // shared snapshot
std::move(task_runners), // task runners
nullptr, // window
{}, // resource context
Expand All @@ -57,6 +58,7 @@ TEST_F(DartIsolateTest, IsolateCanAssociateSnapshot) {
auto root_isolate = DartIsolate::CreateRootIsolate(
vm.get(), // vm
vm->GetIsolateSnapshot(), // isolate snapshot
vm->GetSharedSnapshot(), // shared snapshot
std::move(task_runners), // task runners
nullptr, // window
{}, // resource context
Expand Down Expand Up @@ -85,6 +87,7 @@ TEST_F(DartIsolateTest, CanResolveAndInvokeMethod) {
auto root_isolate = DartIsolate::CreateRootIsolate(
vm.get(), // vm
vm->GetIsolateSnapshot(), // isolate snapshot
vm->GetSharedSnapshot(), // shared snapshot
std::move(task_runners), // task runners
nullptr, // window
{}, // resource context
Expand Down
8 changes: 8 additions & 0 deletions runtime/dart_snapshot.cc
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,10 @@ fxl::RefPtr<DartSnapshot> DartSnapshot::IsolateSnapshotFromSettings(
#endif
}

fxl::RefPtr<DartSnapshot> DartSnapshot::Empty() {
return fxl::MakeRefCounted<DartSnapshot>(nullptr, nullptr);
}

DartSnapshot::DartSnapshot(std::unique_ptr<DartSnapshotBuffer> data,
std::unique_ptr<DartSnapshotBuffer> instructions)
: data_(std::move(data)), instructions_(std::move(instructions)) {}
Expand All @@ -192,6 +196,10 @@ const DartSnapshotBuffer* DartSnapshot::GetInstructions() const {
return instructions_.get();
}

const uint8_t* DartSnapshot::GetDataIfPresent() const {
return data_ ? data_->GetSnapshotPointer() : nullptr;
}

const uint8_t* DartSnapshot::GetInstructionsIfPresent() const {
return instructions_ ? instructions_->GetSnapshotPointer() : nullptr;
}
Expand Down
4 changes: 4 additions & 0 deletions runtime/dart_snapshot.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class DartSnapshot : public fxl::RefCountedThreadSafe<DartSnapshot> {
static fxl::RefPtr<DartSnapshot> IsolateSnapshotFromSettings(
const Settings& settings);

static fxl::RefPtr<DartSnapshot> Empty();

bool IsValid() const;

bool IsValidForAOT() const;
Expand All @@ -36,6 +38,8 @@ class DartSnapshot : public fxl::RefCountedThreadSafe<DartSnapshot> {

const DartSnapshotBuffer* GetInstructions() const;

const uint8_t* GetDataIfPresent() const;

const uint8_t* GetInstructionsIfPresent() const;

private:
Expand Down
22 changes: 17 additions & 5 deletions runtime/dart_vm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ static void EmbedderInformationCallback(Dart_EmbedderInformation* info) {
}

fxl::RefPtr<DartVM> DartVM::ForProcess(Settings settings) {
return ForProcess(settings, nullptr, nullptr);
return ForProcess(settings, nullptr, nullptr, nullptr);
}

static std::once_flag gVMInitialization;
Expand All @@ -237,20 +237,26 @@ static fxl::RefPtr<DartVM> gVM;
fxl::RefPtr<DartVM> DartVM::ForProcess(
Settings settings,
fxl::RefPtr<DartSnapshot> vm_snapshot,
fxl::RefPtr<DartSnapshot> isolate_snapshot) {
fxl::RefPtr<DartSnapshot> isolate_snapshot,
fxl::RefPtr<DartSnapshot> shared_snapshot) {
std::call_once(gVMInitialization, [settings, //
vm_snapshot, //
isolate_snapshot //
isolate_snapshot, //
shared_snapshot //
]() mutable {
if (!vm_snapshot) {
vm_snapshot = DartSnapshot::VMSnapshotFromSettings(settings);
}
if (!isolate_snapshot) {
isolate_snapshot = DartSnapshot::IsolateSnapshotFromSettings(settings);
}
if (!shared_snapshot) {
shared_snapshot = DartSnapshot::Empty();
}
gVM = fxl::MakeRefCounted<DartVM>(settings, //
std::move(vm_snapshot), //
std::move(isolate_snapshot) //
std::move(isolate_snapshot), //
std::move(shared_snapshot) //
);
});
return gVM;
Expand All @@ -262,10 +268,12 @@ fxl::RefPtr<DartVM> DartVM::ForProcessIfInitialized() {

DartVM::DartVM(const Settings& settings,
fxl::RefPtr<DartSnapshot> vm_snapshot,
fxl::RefPtr<DartSnapshot> isolate_snapshot)
fxl::RefPtr<DartSnapshot> isolate_snapshot,
fxl::RefPtr<DartSnapshot> shared_snapshot)
: settings_(settings),
vm_snapshot_(std::move(vm_snapshot)),
isolate_snapshot_(std::move(isolate_snapshot)),
shared_snapshot_(std::move(shared_snapshot)),
platform_kernel_mapping_(
std::make_unique<fml::FileMapping>(settings.platform_kernel_path)),
weak_factory_(this) {
Expand Down Expand Up @@ -454,6 +462,10 @@ fxl::RefPtr<DartSnapshot> DartVM::GetIsolateSnapshot() const {
return isolate_snapshot_;
}

fxl::RefPtr<DartSnapshot> DartVM::GetSharedSnapshot() const {
return shared_snapshot_;
}

ServiceProtocol& DartVM::GetServiceProtocol() {
return service_protocol_;
}
Expand Down
8 changes: 6 additions & 2 deletions runtime/dart_vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ class DartVM : public fxl::RefCountedThreadSafe<DartVM> {
static fxl::RefPtr<DartVM> ForProcess(
Settings settings,
fxl::RefPtr<DartSnapshot> vm_snapshot,
fxl::RefPtr<DartSnapshot> isolate_snapshot);
fxl::RefPtr<DartSnapshot> isolate_snapshot,
fxl::RefPtr<DartSnapshot> shared_snapshot);

static fxl::RefPtr<DartVM> ForProcessIfInitialized();

Expand All @@ -43,6 +44,7 @@ class DartVM : public fxl::RefCountedThreadSafe<DartVM> {
const DartSnapshot& GetVMSnapshot() const;

fxl::RefPtr<DartSnapshot> GetIsolateSnapshot() const;
fxl::RefPtr<DartSnapshot> GetSharedSnapshot() const;

fxl::WeakPtr<DartVM> GetWeakPtr();

Expand All @@ -52,13 +54,15 @@ class DartVM : public fxl::RefCountedThreadSafe<DartVM> {
const Settings settings_;
const fxl::RefPtr<DartSnapshot> vm_snapshot_;
const fxl::RefPtr<DartSnapshot> isolate_snapshot_;
const fxl::RefPtr<DartSnapshot> shared_snapshot_;
std::unique_ptr<fml::Mapping> platform_kernel_mapping_;
ServiceProtocol service_protocol_;
fxl::WeakPtrFactory<DartVM> weak_factory_;

DartVM(const Settings& settings,
fxl::RefPtr<DartSnapshot> vm_snapshot,
fxl::RefPtr<DartSnapshot> isolate_snapshot);
fxl::RefPtr<DartSnapshot> isolate_snapshot,
fxl::RefPtr<DartSnapshot> shared_snapshot);

~DartVM();

Expand Down
6 changes: 6 additions & 0 deletions runtime/runtime_controller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ RuntimeController::RuntimeController(
RuntimeDelegate& p_client,
const DartVM* p_vm,
fxl::RefPtr<DartSnapshot> p_isolate_snapshot,
fxl::RefPtr<DartSnapshot> p_shared_snapshot,
TaskRunners p_task_runners,
fml::WeakPtr<GrContext> p_resource_context,
fxl::RefPtr<flow::SkiaUnrefQueue> p_unref_queue)
: RuntimeController(p_client,
p_vm,
std::move(p_isolate_snapshot),
std::move(p_shared_snapshot),
std::move(p_task_runners),
std::move(p_resource_context),
std::move(p_unref_queue),
Expand All @@ -37,20 +39,23 @@ RuntimeController::RuntimeController(
RuntimeDelegate& p_client,
const DartVM* p_vm,
fxl::RefPtr<DartSnapshot> p_isolate_snapshot,
fxl::RefPtr<DartSnapshot> p_shared_snapshot,
TaskRunners p_task_runners,
fml::WeakPtr<GrContext> p_resource_context,
fxl::RefPtr<flow::SkiaUnrefQueue> p_unref_queue,
WindowData p_window_data)
: client_(p_client),
vm_(p_vm),
isolate_snapshot_(std::move(p_isolate_snapshot)),
shared_snapshot_(std::move(p_shared_snapshot)),
task_runners_(p_task_runners),
resource_context_(p_resource_context),
unref_queue_(p_unref_queue),
window_data_(std::move(p_window_data)),
root_isolate_(
DartIsolate::CreateRootIsolate(vm_,
isolate_snapshot_,
shared_snapshot_,
task_runners_,
std::make_unique<Window>(this),
resource_context_,
Expand Down Expand Up @@ -94,6 +99,7 @@ std::unique_ptr<RuntimeController> RuntimeController::Clone() const {
client_, //
vm_, //
isolate_snapshot_, //
shared_snapshot_, //
task_runners_, //
resource_context_, //
unref_queue_, //
Expand Down
3 changes: 3 additions & 0 deletions runtime/runtime_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class RuntimeController final : public WindowClient {
RuntimeController(RuntimeDelegate& client,
const DartVM* vm,
fxl::RefPtr<DartSnapshot> isolate_snapshot,
fxl::RefPtr<DartSnapshot> shared_snapshot,
TaskRunners task_runners,
fml::WeakPtr<GrContext> resource_context,
fxl::RefPtr<flow::SkiaUnrefQueue> unref_queue);
Expand Down Expand Up @@ -81,6 +82,7 @@ class RuntimeController final : public WindowClient {
RuntimeDelegate& client_;
const DartVM* vm_;
fxl::RefPtr<DartSnapshot> isolate_snapshot_;
fxl::RefPtr<DartSnapshot> shared_snapshot_;
TaskRunners task_runners_;
fml::WeakPtr<GrContext> resource_context_;
fxl::RefPtr<flow::SkiaUnrefQueue> unref_queue_;
Expand All @@ -91,6 +93,7 @@ class RuntimeController final : public WindowClient {
RuntimeController(RuntimeDelegate& client,
const DartVM* vm,
fxl::RefPtr<DartSnapshot> isolate_snapshot,
fxl::RefPtr<DartSnapshot> shared_snapshot,
TaskRunners task_runners,
fml::WeakPtr<GrContext> resource_context,
fxl::RefPtr<flow::SkiaUnrefQueue> unref_queue,
Expand Down
2 changes: 2 additions & 0 deletions shell/common/engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ static constexpr char kSettingsChannel[] = "flutter/settings";
Engine::Engine(Delegate& delegate,
const blink::DartVM& vm,
fxl::RefPtr<blink::DartSnapshot> isolate_snapshot,
fxl::RefPtr<blink::DartSnapshot> shared_snapshot,
blink::TaskRunners task_runners,
blink::Settings settings,
std::unique_ptr<Animator> animator,
Expand All @@ -57,6 +58,7 @@ Engine::Engine(Delegate& delegate,
*this, // runtime delegate
&vm, // VM
std::move(isolate_snapshot), // isolate snapshot
std::move(shared_snapshot), // shared snapshot
std::move(task_runners), // task runners
std::move(resource_context), // resource context
std::move(unref_queue) // skia unref queue
Expand Down
1 change: 1 addition & 0 deletions shell/common/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class Engine final : public blink::RuntimeDelegate {
Engine(Delegate& delegate,
const blink::DartVM& vm,
fxl::RefPtr<blink::DartSnapshot> isolate_snapshot,
fxl::RefPtr<blink::DartSnapshot> shared_snapshot,
blink::TaskRunners task_runners,
blink::Settings settings,
std::unique_ptr<Animator> animator,
Expand Down
Loading

0 comments on commit fb709e2

Please sign in to comment.