From e083c7aa6be844ef54f67156f7b150fcade9c7b1 Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Mon, 15 Jun 2015 10:42:17 -0700 Subject: [PATCH] Introduce sky_packager sky_packager is a tool that generates a Dart script snapshot for Dart files intended to run with dart:sky. I plan to use this tool to experiment with startup performance. R=jackson@google.com Review URL: https://codereview.chromium.org/1178213003. --- engine/src/flutter/BUILD.gn | 1 + engine/src/flutter/tools/packager/BUILD.gn | 28 ++++ engine/src/flutter/tools/packager/loader.cc | 138 ++++++++++++++++++ engine/src/flutter/tools/packager/loader.h | 18 +++ engine/src/flutter/tools/packager/logging.cc | 37 +++++ engine/src/flutter/tools/packager/logging.h | 16 ++ engine/src/flutter/tools/packager/main.cc | 58 ++++++++ engine/src/flutter/tools/packager/scope.h | 33 +++++ engine/src/flutter/tools/packager/switches.cc | 8 + engine/src/flutter/tools/packager/switches.h | 11 ++ engine/src/flutter/tools/packager/vm.cc | 43 ++++++ engine/src/flutter/tools/packager/vm.h | 13 ++ 12 files changed, 404 insertions(+) create mode 100644 engine/src/flutter/tools/packager/BUILD.gn create mode 100644 engine/src/flutter/tools/packager/loader.cc create mode 100644 engine/src/flutter/tools/packager/loader.h create mode 100644 engine/src/flutter/tools/packager/logging.cc create mode 100644 engine/src/flutter/tools/packager/logging.h create mode 100644 engine/src/flutter/tools/packager/main.cc create mode 100644 engine/src/flutter/tools/packager/scope.h create mode 100644 engine/src/flutter/tools/packager/switches.cc create mode 100644 engine/src/flutter/tools/packager/switches.h create mode 100644 engine/src/flutter/tools/packager/vm.cc create mode 100644 engine/src/flutter/tools/packager/vm.h diff --git a/engine/src/flutter/BUILD.gn b/engine/src/flutter/BUILD.gn index ec9d6bc7ab..f1fcc7a77a 100644 --- a/engine/src/flutter/BUILD.gn +++ b/engine/src/flutter/BUILD.gn @@ -14,6 +14,7 @@ group("sky") { "//sky/engine/web:sky_unittests", "//sky/engine/wtf:unittests", "//sky/tools/imagediff", + "//sky/tools/packager($host_toolchain)", "//sky/tools/tester", "//sky/viewer", ":sky_apk", diff --git a/engine/src/flutter/tools/packager/BUILD.gn b/engine/src/flutter/tools/packager/BUILD.gn new file mode 100644 index 0000000000..b6d09b1ddf --- /dev/null +++ b/engine/src/flutter/tools/packager/BUILD.gn @@ -0,0 +1,28 @@ +# Copyright 2014 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +executable("packager") { + output_name = "sky_packager" + + sources = [ + "loader.cc", + "loader.h", + "logging.cc", + "logging.h", + "main.cc", + "scopes.h", + "switches.cc", + "switches.h", + "vm.cc", + "vm.h", + ] + + deps = [ + "//base", + "//dart/runtime/vm:libdart_platform", + "//dart/runtime:libdart", + "//sky/engine/bindings:snapshot_cc", + "//third_party/zlib", + ] +} diff --git a/engine/src/flutter/tools/packager/loader.cc b/engine/src/flutter/tools/packager/loader.cc new file mode 100644 index 0000000000..d130bd231a --- /dev/null +++ b/engine/src/flutter/tools/packager/loader.cc @@ -0,0 +1,138 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "sky/tools/packager/loader.h" + +#include + +#include "base/command_line.h" +#include "base/files/file_path.h" +#include "base/files/file_util.h" +#include "base/logging.h" +#include "base/strings/string_util.h" +#include "sky/tools/packager/logging.h" +#include "sky/tools/packager/scope.h" +#include "sky/tools/packager/switches.h" + +namespace { + +static const char kSkyInternalsLibraryName[] = "dart:sky.internals"; +static const char kSkyInternalsURL[] = "package:sky/internals.dart"; + +std::string Fetch(const std::string& url) { + base::FilePath path(url); + std::string source; + CHECK(base::ReadFileToString(path, &source)) << url; + return source; +} + +base::FilePath SimplifyPath(const base::FilePath& path) { + std::vector components; + path.GetComponents(&components); + base::FilePath result; + for (const auto& component : components) { + if (component == base::FilePath::kCurrentDirectory) + continue; + if (component == base::FilePath::kParentDirectory) + result = result.DirName(); + else + result = result.Append(component); + } + return result; +} + +class Loader { + public: + Loader(base::FilePath package_root); + + std::string CanonicalizePackageURL(std::string url); + Dart_Handle CanonicalizeURL(Dart_Handle library, Dart_Handle url); + Dart_Handle Import(Dart_Handle url); + Dart_Handle Source(Dart_Handle library, Dart_Handle url); + + private: + base::FilePath package_root_; + + DISALLOW_COPY_AND_ASSIGN(Loader); +}; + +Loader::Loader(base::FilePath package_root) + : package_root_(std::move(package_root)) { +} + +std::string Loader::CanonicalizePackageURL(std::string url) { + DCHECK(StartsWithASCII(url, "package:", true)); + ReplaceFirstSubstringAfterOffset(&url, 0, "package:", ""); + return package_root_.Append(url).AsUTF8Unsafe(); +} + +Dart_Handle Loader::CanonicalizeURL(Dart_Handle library, Dart_Handle url) { + std::string string = StringFromDart(url); + if (StartsWithASCII(string, "dart:", true)) + return url; + if (StartsWithASCII(string, "package:", true)) + return StringToDart(CanonicalizePackageURL(std::move(string))); + base::FilePath base_path(StringFromDart(Dart_LibraryUrl(library))); + base::FilePath resolved_path = base_path.DirName().Append(string); + base::FilePath normalized_path = SimplifyPath(resolved_path); + return StringToDart(normalized_path.AsUTF8Unsafe()); +} + +Dart_Handle Loader::Import(Dart_Handle url) { + Dart_Handle source = StringToDart(Fetch(StringFromDart(url))); + Dart_Handle result = Dart_LoadLibrary(url, source, 0, 0); + LogIfError(result); + return result; +} + +Dart_Handle Loader::Source(Dart_Handle library, Dart_Handle url) { + Dart_Handle source = StringToDart(Fetch(StringFromDart(url))); + Dart_Handle result = Dart_LoadSource(library, url, source, 0, 0); + LogIfError(result); + return result; +} + +Loader* g_loader = nullptr; + +Loader& GetLoader() { + if (!g_loader) { + base::CommandLine& command_line = *base::CommandLine::ForCurrentProcess(); + CHECK(command_line.HasSwitch(kPackageRoot)) << "Need --package-root"; + g_loader = new Loader(command_line.GetSwitchValuePath(kPackageRoot)); + } + return *g_loader; +} + +} // namespace + +Dart_Handle HandleLibraryTag(Dart_LibraryTag tag, + Dart_Handle library, + Dart_Handle url) { + CHECK(Dart_IsLibrary(library)); + CHECK(Dart_IsString(url)); + + if (tag == Dart_kCanonicalizeUrl) + return GetLoader().CanonicalizeURL(library, url); + + if (tag == Dart_kImportTag) + return GetLoader().Import(url); + + if (tag == Dart_kSourceTag) + return GetLoader().Source(library, url); + + return Dart_NewApiError("Unknown library tag."); +} + +void LoadSkyInternals() { + DartApiScope api_scope; + + Dart_Handle library_name = StringToDart(kSkyInternalsLibraryName); + std::string url = GetLoader().CanonicalizePackageURL(kSkyInternalsURL); + LogIfError(Dart_LoadLibrary(library_name, StringToDart(Fetch(url)), 0, 0)); +} + +void LoadScript(std::string url) { + LogIfError( + Dart_LoadScript(StringToDart(url), StringToDart(Fetch(url)), 0, 0)); +} diff --git a/engine/src/flutter/tools/packager/loader.h b/engine/src/flutter/tools/packager/loader.h new file mode 100644 index 0000000000..c74d42f2f6 --- /dev/null +++ b/engine/src/flutter/tools/packager/loader.h @@ -0,0 +1,18 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef SKY_TOOLS_PACKAGER_LOADER_H_ +#define SKY_TOOLS_PACKAGER_LOADER_H_ + +#include + +#include "dart/runtime/include/dart_api.h" + +Dart_Handle HandleLibraryTag(Dart_LibraryTag tag, + Dart_Handle library, + Dart_Handle url); +void LoadSkyInternals(); +void LoadScript(std::string url); + +#endif // SKY_TOOLS_PACKAGER_LOADER_H_ diff --git a/engine/src/flutter/tools/packager/logging.cc b/engine/src/flutter/tools/packager/logging.cc new file mode 100644 index 0000000000..e61820cb97 --- /dev/null +++ b/engine/src/flutter/tools/packager/logging.cc @@ -0,0 +1,37 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "sky/tools/packager/logging.h" + +#include "base/logging.h" + +bool LogIfError(Dart_Handle handle) { + if (Dart_IsError(handle)) { + LOG(ERROR) << Dart_GetError(handle); + + // Only unhandled exceptions have stacktraces. + if (!Dart_ErrorHasException(handle)) + return true; + + Dart_Handle stacktrace = Dart_ErrorGetStacktrace(handle); + const char* stacktrace_cstr = ""; + Dart_StringToCString(Dart_ToString(stacktrace), &stacktrace_cstr); + LOG(ERROR) << stacktrace_cstr; + return true; + } + return false; +} + +std::string StringFromDart(Dart_Handle string) { + CHECK(Dart_IsString(string)); + uint8_t* utf8_array; + intptr_t length; + Dart_StringToUTF8(string, &utf8_array, &length); + return std::string(reinterpret_cast(utf8_array), length); +} + +Dart_Handle StringToDart(const std::string& string) { + return Dart_NewStringFromUTF8(reinterpret_cast(string.data()), + string.length()); +} diff --git a/engine/src/flutter/tools/packager/logging.h b/engine/src/flutter/tools/packager/logging.h new file mode 100644 index 0000000000..227a9d8f5e --- /dev/null +++ b/engine/src/flutter/tools/packager/logging.h @@ -0,0 +1,16 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef SKY_TOOLS_PACKAGER_LOGGING_H_ +#define SKY_TOOLS_PACKAGER_LOGGING_H_ + +#include + +#include "dart/runtime/include/dart_api.h" + +bool LogIfError(Dart_Handle handle); +std::string StringFromDart(Dart_Handle string); +Dart_Handle StringToDart(const std::string& string); + +#endif // SKY_TOOLS_PACKAGER_LOGGING_H_ diff --git a/engine/src/flutter/tools/packager/main.cc b/engine/src/flutter/tools/packager/main.cc new file mode 100644 index 0000000000..7b5e1f3f2b --- /dev/null +++ b/engine/src/flutter/tools/packager/main.cc @@ -0,0 +1,58 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include + +#include "base/at_exit.h" +#include "base/basictypes.h" +#include "base/command_line.h" +#include "base/files/file_path.h" +#include "base/files/file_util.h" +#include "base/logging.h" +#include "base/process/memory.h" +#include "dart/runtime/include/dart_api.h" +#include "sky/tools/packager/loader.h" +#include "sky/tools/packager/logging.h" +#include "sky/tools/packager/scope.h" +#include "sky/tools/packager/switches.h" +#include "sky/tools/packager/vm.h" + +void WriteSnapshot(base::FilePath path) { + uint8_t* buffer; + intptr_t size; + CHECK(!LogIfError(Dart_CreateScriptSnapshot(&buffer, &size))); + + CHECK_EQ(base::WriteFile(path, reinterpret_cast(buffer), size), + size); + + std::cout << "Successfully wrote snapshot to " << path.LossyDisplayName() + << " (" << size << " bytes)." << std::endl; +} + +int main(int argc, const char* argv[]) { + base::AtExitManager exit_manager; + base::EnableTerminationOnHeapCorruption(); + base::CommandLine::Init(argc, argv); + + InitDartVM(); + Dart_Isolate isolate = CreateDartIsolate(); + CHECK(isolate); + + const base::CommandLine& command_line = + *base::CommandLine::ForCurrentProcess(); + + DartIsolateScope scope(isolate); + DartApiScope api_scope; + + auto args = command_line.GetArgs(); + CHECK(args.size() == 1); + LoadScript(args[0]); + + CHECK(!LogIfError(Dart_FinalizeLoading(true))); + + CHECK(command_line.HasSwitch(kSnapshot)) << "Need --snapshot"; + WriteSnapshot(command_line.GetSwitchValuePath(kSnapshot)); + + return 0; +} diff --git a/engine/src/flutter/tools/packager/scope.h b/engine/src/flutter/tools/packager/scope.h new file mode 100644 index 0000000000..b039d24dcc --- /dev/null +++ b/engine/src/flutter/tools/packager/scope.h @@ -0,0 +1,33 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef SKY_TOOLS_PACKAGER_SCOPE_H_ +#define SKY_TOOLS_PACKAGER_SCOPE_H_ + +#include "base/basictypes.h" +#include "base/logging.h" +#include "dart/runtime/include/dart_api.h" + +class DartIsolateScope { + public: + explicit DartIsolateScope(Dart_Isolate isolate) { + CHECK(!Dart_CurrentIsolate()); + Dart_EnterIsolate(isolate); + } + ~DartIsolateScope() { Dart_ExitIsolate(); } + + private: + DISALLOW_COPY_AND_ASSIGN(DartIsolateScope); +}; + +class DartApiScope { + public: + DartApiScope() { Dart_EnterScope(); } + ~DartApiScope() { Dart_ExitScope(); } + + private: + DISALLOW_COPY_AND_ASSIGN(DartApiScope); +}; + +#endif // SKY_TOOLS_PACKAGER_SCOPE_H_ diff --git a/engine/src/flutter/tools/packager/switches.cc b/engine/src/flutter/tools/packager/switches.cc new file mode 100644 index 0000000000..cf867e228d --- /dev/null +++ b/engine/src/flutter/tools/packager/switches.cc @@ -0,0 +1,8 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "sky/tools/packager/switches.h" + +const char kPackageRoot[] = "package-root"; +const char kSnapshot[] = "snapshot"; diff --git a/engine/src/flutter/tools/packager/switches.h b/engine/src/flutter/tools/packager/switches.h new file mode 100644 index 0000000000..976fef8890 --- /dev/null +++ b/engine/src/flutter/tools/packager/switches.h @@ -0,0 +1,11 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef SKY_TOOLS_PACKAGER_SWITCHES_H_ +#define SKY_TOOLS_PACKAGER_SWITCHES_H_ + +extern const char kPackageRoot[]; +extern const char kSnapshot[]; + +#endif // SKY_TOOLS_PACKAGER_SWITCHES_H_ diff --git a/engine/src/flutter/tools/packager/vm.cc b/engine/src/flutter/tools/packager/vm.cc new file mode 100644 index 0000000000..7472198dff --- /dev/null +++ b/engine/src/flutter/tools/packager/vm.cc @@ -0,0 +1,43 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "sky/tools/packager/vm.h" + +#include "base/logging.h" +#include "sky/tools/packager/loader.h" +#include "sky/tools/packager/logging.h" + +namespace blink { +extern const uint8_t* kDartVmIsolateSnapshotBuffer; +extern const uint8_t* kDartIsolateSnapshotBuffer; +} + +void InitDartVM() { + int argc = 0; + const char** argv = nullptr; + + CHECK(Dart_SetVMFlags(argc, argv)); + CHECK(Dart_Initialize(blink::kDartVmIsolateSnapshotBuffer, + nullptr, // Isolate created callback. + nullptr, // Isolate interrupt callback. + nullptr, // Unhandled exception callback. + nullptr, // Isolate shutdown callback. + // File IO callbacks. + nullptr, nullptr, nullptr, nullptr, nullptr)); +} + +Dart_Isolate CreateDartIsolate() { + CHECK(blink::kDartIsolateSnapshotBuffer); + char* error = nullptr; + Dart_Isolate isolate = + Dart_CreateIsolate("http://example.com", "main", + blink::kDartIsolateSnapshotBuffer, nullptr, &error); + + CHECK(isolate) << error; + CHECK(!LogIfError(Dart_SetLibraryTagHandler(HandleLibraryTag))); + LoadSkyInternals(); + + Dart_ExitIsolate(); + return isolate; +} diff --git a/engine/src/flutter/tools/packager/vm.h b/engine/src/flutter/tools/packager/vm.h new file mode 100644 index 0000000000..681fbc9e31 --- /dev/null +++ b/engine/src/flutter/tools/packager/vm.h @@ -0,0 +1,13 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef SKY_TOOLS_PACKAGER_VM_H_ +#define SKY_TOOLS_PACKAGER_VM_H_ + +#include "dart/runtime/include/dart_api.h" + +void InitDartVM(); +Dart_Isolate CreateDartIsolate(); + +#endif // SKY_TOOLS_PACKAGER_VM_H_