22#include "third_party/dart/runtime/bin/platform.h"
23#include "third_party/dart/runtime/include/bin/dart_io_api.h"
24#include "third_party/skia/include/core/SkExecutor.h"
37 "--enable_mirrors=false",
38 "--background_compilation",
48 "--concurrent_mark=false",
49 "--concurrent_sweep=false",
50 "--compactor_tasks=1",
51 "--scavenger_tasks=0",
58 "--no_write_protect_code",
63 "--no_use_integer_division",
73 "--pause_isolates_on_start",
77 "--timeline_recorder=endless",
81 "--timeline_recorder=systrace",
85 std::ostringstream oss;
86 oss <<
"--timeline_recorder=perfettofile:" <<
path;
96 "--timeline_streams=Dart,Embedder,GC,Microtask",
100 "--timeline_streams=Compiler,Dart,Debugger,Embedder,GC,Isolate,Microtask,"
105 "--timeline_streams=Compiler,Dart,Debugger,Embedder,GC,Isolate,Microtask,"
110 "--profile_microtasks",
114 std::ostringstream oss;
115 oss <<
"--old_gen_heap_size=" << heap_size;
130 if (stat(
path, &info) < 0) {
136 if (!info.st_mtime) {
150 return mtime > since;
160 dart::bin::SetCaptureStdout(
true);
163 dart::bin::SetCaptureStderr(
true);
171 dart::bin::SetCaptureStdout(
false);
173 dart::bin::SetCaptureStderr(
false);
178 return Dart_IsPrecompiledRuntime();
182 bool profile_startup) {
187 enable_profiling =
false;
193 if (enable_profiling) {
194 std::vector<const char*> flags = {
200#if FML_OS_IOS && FML_ARCH_CPU_ARM_FAMILY && FML_ARCH_CPU_ARMEL
208 "--profile_period=2000",
210 "--profile_period=1000",
214 if (profile_startup) {
219 flags.push_back(
"--profile_startup");
224 return {
"--no-profiler"};
231 for (
size_t i = 0;
i < argc; ++
i) {
237 info->version = DART_EMBEDDER_INFORMATION_CURRENT_VERSION;
238 dart::bin::GetIOEmbedderInformation(info);
239 info->name =
"Flutter";
242std::shared_ptr<DartVM> DartVM::Create(
243 const Settings& settings,
246 std::shared_ptr<IsolateNameServer> isolate_name_server) {
248 std::move(vm_snapshot),
249 std::move(isolate_snapshot)
253 FML_LOG(ERROR) <<
"Could not set up VM data to bootstrap the VM from.";
258 return std::shared_ptr<DartVM>(
259 new DartVM(vm_data, std::move(isolate_name_server)));
272DartVM::DartVM(
const std::shared_ptr<const DartVMData>& vm_data,
273 std::shared_ptr<IsolateNameServer> isolate_name_server)
275 concurrent_message_loop_(
fml::ConcurrentMessageLoop::Create(
276 std::clamp(
fml::EfficiencyCoreCount().value_or(
277 std::thread::hardware_concurrency()) /
281 skia_concurrent_executor_(
282 [runner = concurrent_message_loop_->GetTaskRunner()](
283 const
fml::closure& work) { runner->PostTask(work); }),
285 isolate_name_server_(std::move(isolate_name_server)),
286 service_protocol_(std::make_shared<ServiceProtocol>()) {
293 SkExecutor::SetDefault(&skia_concurrent_executor_);
299 if (!dart::bin::Platform::Initialize(
false)) {
300 FML_LOG(FATAL) <<
"Dart platform-specific initialization failed";
305 dart::bin::BootstrapDartIo();
307 if (!
settings_.temp_directory_path.empty()) {
308 dart::bin::SetSystemTempDirectory(
settings_.temp_directory_path.c_str());
312 std::vector<const char*>
args;
318 args.push_back(
"--ignore-unrecognized-flags");
322 args.push_back(profiler_flag);
325 PushBackAll(&
args, kDartAllConfigsArgs, std::size(kDartAllConfigsArgs));
327 if (IsRunningPrecompiledCode()) {
329 std::size(kDartPrecompilationArgs));
334 bool enable_asserts = !
settings_.disable_dart_asserts;
337 if (IsRunningPrecompiledCode()) {
338 enable_asserts =
false;
342#if (FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG)
343#if !FML_OS_IOS && !FML_OS_MACOSX
347 std::size(kDartWriteProtectCodeArgs));
354 <<
"Tracing not enabled before attempting to run JIT mode VM.";
361 std::size(kDartDisableIntegerDivisionArgs));
366 if (enable_asserts) {
378 PushBackAll(&
args, kDartStartPausedArgs, std::size(kDartStartPausedArgs));
385 std::size(kDartEndlessTraceBufferArgs));
390 std::size(kDartSystraceTraceBufferArgs));
392 std::size(kDartSystraceTraceStreamsArgs));
395 std::string file_recorder_args;
398 args.push_back(file_recorder_args.c_str());
400 std::size(kDartSystraceTraceStreamsArgs));
405 std::size(kDartStartupTraceStreamsArgs));
408#if defined(OS_FUCHSIA)
410 std::size(kDartSystraceTraceBufferArgs));
412 std::size(kDartSystraceTraceStreamsArgs));
416 std::size(kDartDefaultTraceStreamsArgs));
422 std::size(kDartProfileMicrotasksArgs));
425 std::string old_gen_heap_size_args;
427 old_gen_heap_size_args =
429 args.push_back(old_gen_heap_size_args.c_str());
432 for (
size_t i = 0;
i <
settings_.dart_flags.size();
i++) {
436 char* flags_error = Dart_SetVMFlags(
args.size(),
args.data());
438 FML_LOG(FATAL) <<
"Error while setting Dart VM flags: " << flags_error;
442 dart::bin::SetExecutableName(
settings_.executable_name.c_str());
446 Dart_InitializeParams
params = {};
447 params.version = DART_INITIALIZE_PARAMS_CURRENT_VERSION;
448 params.vm_snapshot_data = vm_data_->GetVMSnapshot().GetDataMapping();
449 params.vm_snapshot_instructions =
450 vm_data_->GetVMSnapshot().GetInstructionsMapping();
451 params.create_group =
reinterpret_cast<decltype(
params.create_group)
>(
452 DartIsolate::DartIsolateGroupCreateCallback);
453 params.initialize_isolate =
454 reinterpret_cast<decltype(
params.initialize_isolate)
>(
455 DartIsolate::DartIsolateInitializeCallback);
457 reinterpret_cast<decltype(
params.shutdown_isolate)
>(
458 DartIsolate::DartIsolateShutdownCallback);
459 params.cleanup_isolate =
reinterpret_cast<decltype(
params.cleanup_isolate)
>(
460 DartIsolate::DartIsolateCleanupCallback);
461 params.cleanup_group =
reinterpret_cast<decltype(
params.cleanup_group)
>(
462 DartIsolate::DartIsolateGroupCleanupCallback);
464 params.file_open = dart::bin::OpenFile;
465 params.file_read = dart::bin::ReadFile;
466 params.file_write = dart::bin::WriteFile;
467 params.file_close = dart::bin::CloseFile;
468 params.entropy_source = dart::bin::GetEntropy;
479 int64_t micros = Dart_TimelineGetMicros();
480 Dart_RecordTimelineEvent(
"FlutterEngineMainEnter",
485 Dart_Timeline_Event_Instant,
492 Dart_SetFileModifiedCallback(&DartFileModifiedCallback);
495 Dart_SetServiceStreamCallbacks(&ServiceStreamListenCallback,
496 &ServiceStreamCancelCallback);
498 Dart_SetEmbedderInformationCallback(&EmbedderInformationCallback);
500 if (
settings_.dart_library_sources_kernel !=
nullptr) {
501 std::unique_ptr<fml::Mapping> dart_library_sources =
504 Dart_SetDartLibrarySourcesKernel(dart_library_sources->GetMapping(),
505 dart_library_sources->GetSize());
509 concurrent_message_loop_->PostTaskToAllWorkers(
510 [] { Dart_SetThreadName(
"FlutterConcurrentMessageLoopWorker"); });
516 SkExecutor::SetDefault(
nullptr);
518 if (Dart_CurrentIsolate() !=
nullptr) {
524 dart::bin::CleanupDartIo();
527std::shared_ptr<const DartVMData> DartVM::GetVMData()
const {
535std::shared_ptr<ServiceProtocol> DartVM::GetServiceProtocol()
const {
536 return service_protocol_;
539std::shared_ptr<IsolateNameServer> DartVM::GetIsolateNameServer()
const {
540 return isolate_name_server_;
543std::shared_ptr<fml::ConcurrentTaskRunner>
544DartVM::GetConcurrentWorkerTaskRunner()
const {
545 return concurrent_message_loop_->GetTaskRunner();
548std::shared_ptr<fml::ConcurrentMessageLoop> DartVM::GetConcurrentMessageLoop() {
549 return concurrent_message_loop_;
static void Initialize(Dart_InitializeParams *params, bool enable_timeline_event_handler, bool trace_systrace)
static std::shared_ptr< const DartVMData > Create(const Settings &settings, fml::RefPtr< const DartSnapshot > vm_snapshot, fml::RefPtr< const DartSnapshot > isolate_snapshot)
Creates a new instance of DartVMData. Both the VM and isolate snapshot members are optional and may b...
static size_t GetVMLaunchCount()
The number of times the VM has been launched in the process. This call is inherently racy because the...
static bool IsRunningPrecompiledCode()
Checks if VM instances in the process can run precompiled code. This call can be made at any time and...
static constexpr TimeDelta FromSeconds(int64_t seconds)
static constexpr TimeDelta FromMilliseconds(int64_t millis)
const EmbeddedViewParams * params
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
#define FML_LOG(severity)
#define FML_CHECK(condition)
#define FML_DCHECK(condition)
void ThreadExitCallback()
static const char kStderrStreamId[]
static constexpr size_t kMaxCount
static const char * kDartPrecompilationArgs[]
static const char * kSerialGCArgs[]
static std::string DartFileRecorderArgs(const std::string &path)
static const char * kDartDisableIntegerDivisionArgs[]
constexpr size_t kFileUriPrefixLength
void PushBackAll(std::vector< const char * > *args, const char **argv, size_t argc)
static std::string DartOldGenHeapSizeArgs(uint64_t heap_size)
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
static const char * kDartAllConfigsArgs[]
static const char * kDartStartPausedArgs[]
static void EmbedderInformationCallback(Dart_EmbedderInformation *info)
static const char * kDartStartupTraceStreamsArgs[]
static void ServiceStreamCancelCallback(const char *stream_id)
static constexpr size_t kMinCount
constexpr char kFileUriPrefix[]
static const char * kDartEndlessTraceBufferArgs[]
static const char * kDartAssertArgs[]
bool EnableTracingIfNecessary(const Settings &vm_settings)
Enables tracing in the process so that JIT mode VMs may be launched. Explicitly enabling tracing is n...
static const char * kDartSystraceTraceBufferArgs[]
static std::atomic_size_t gVMLaunchCount
static const char kStdoutStreamId[]
static const char * kDartWriteProtectCodeArgs[]
static const char * kDartDefaultTraceStreamsArgs[]
static const char * kDartSystraceTraceStreamsArgs[]
bool DartFileModifiedCallback(const char *source_url, int64_t since_ms)
static bool ServiceStreamListenCallback(const char *stream_id)
static std::vector< const char * > ProfilingFlags(bool enable_profiling, bool profile_startup)
static const char * kDartProfileMicrotasksArgs[]
#define TRACE_EVENT0(category_group, name)