22#include "third_party/dart/runtime/include/bin/dart_io_api.h"
23#include "third_party/skia/include/core/SkExecutor.h"
36 "--enable_mirrors=false",
37 "--background_compilation",
47 "--concurrent_mark=false",
48 "--concurrent_sweep=false",
49 "--compactor_tasks=1",
50 "--scavenger_tasks=0",
57 "--no_write_protect_code",
62 "--no_use_integer_division",
72 "--pause_isolates_on_start",
76 "--timeline_recorder=endless",
80 "--timeline_recorder=systrace",
84 std::ostringstream oss;
85 oss <<
"--timeline_recorder=perfettofile:" <<
path;
95 "--timeline_streams=Dart,Embedder,GC,Microtask",
99 "--timeline_streams=Compiler,Dart,Debugger,Embedder,GC,Isolate,Microtask,"
104 "--timeline_streams=Compiler,Dart,Debugger,Embedder,GC,Isolate,Microtask,"
109 "--profile_microtasks",
113 std::ostringstream oss;
114 oss <<
"--old_gen_heap_size=" << heap_size;
129 if (stat(
path, &info) < 0) {
135 if (!info.st_mtime) {
149 return mtime > since;
159 dart::bin::SetCaptureStdout(
true);
162 dart::bin::SetCaptureStderr(
true);
170 dart::bin::SetCaptureStdout(
false);
172 dart::bin::SetCaptureStderr(
false);
177 return Dart_IsPrecompiledRuntime();
181 bool profile_startup) {
186 enable_profiling =
false;
192 if (enable_profiling) {
193 std::vector<const char*> flags = {
199#if FML_OS_IOS && FML_ARCH_CPU_ARM_FAMILY && FML_ARCH_CPU_ARMEL
207 "--profile_period=2000",
209 "--profile_period=1000",
213 if (profile_startup) {
218 flags.push_back(
"--profile_startup");
223 return {
"--no-profiler"};
230 for (
size_t i = 0;
i < argc; ++
i) {
236 info->version = DART_EMBEDDER_INFORMATION_CURRENT_VERSION;
237 dart::bin::GetIOEmbedderInformation(info);
238 info->name =
"Flutter";
241std::shared_ptr<DartVM> DartVM::Create(
242 const Settings& settings,
245 std::shared_ptr<IsolateNameServer> isolate_name_server) {
247 std::move(vm_snapshot),
248 std::move(isolate_snapshot)
252 FML_LOG(ERROR) <<
"Could not set up VM data to bootstrap the VM from.";
257 return std::shared_ptr<DartVM>(
258 new DartVM(vm_data, std::move(isolate_name_server)));
271DartVM::DartVM(
const std::shared_ptr<const DartVMData>& vm_data,
272 std::shared_ptr<IsolateNameServer> isolate_name_server)
274 concurrent_message_loop_(
fml::ConcurrentMessageLoop::Create(
275 std::clamp(
fml::EfficiencyCoreCount().value_or(
276 std::thread::hardware_concurrency()) /
280 skia_concurrent_executor_(
281 [runner = concurrent_message_loop_->GetTaskRunner()](
282 const
fml::closure& work) { runner->PostTask(work); }),
284 isolate_name_server_(std::move(isolate_name_server)),
285 service_protocol_(std::make_shared<ServiceProtocol>()) {
292 SkExecutor::SetDefault(&skia_concurrent_executor_);
300 dart::bin::BootstrapDartIo();
302 if (!
settings_.temp_directory_path.empty()) {
303 dart::bin::SetSystemTempDirectory(
settings_.temp_directory_path.c_str());
307 std::vector<const char*>
args;
313 args.push_back(
"--ignore-unrecognized-flags");
317 args.push_back(profiler_flag);
320 PushBackAll(&
args, kDartAllConfigsArgs, std::size(kDartAllConfigsArgs));
322 if (IsRunningPrecompiledCode()) {
324 std::size(kDartPrecompilationArgs));
329 bool enable_asserts = !
settings_.disable_dart_asserts;
332 if (IsRunningPrecompiledCode()) {
333 enable_asserts =
false;
337#if (FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG)
338#if !FML_OS_IOS && !FML_OS_MACOSX
342 std::size(kDartWriteProtectCodeArgs));
349 <<
"Tracing not enabled before attempting to run JIT mode VM.";
356 std::size(kDartDisableIntegerDivisionArgs));
361 if (enable_asserts) {
373 PushBackAll(&
args, kDartStartPausedArgs, std::size(kDartStartPausedArgs));
380 std::size(kDartEndlessTraceBufferArgs));
385 std::size(kDartSystraceTraceBufferArgs));
387 std::size(kDartSystraceTraceStreamsArgs));
390 std::string file_recorder_args;
393 args.push_back(file_recorder_args.c_str());
395 std::size(kDartSystraceTraceStreamsArgs));
400 std::size(kDartStartupTraceStreamsArgs));
403#if defined(OS_FUCHSIA)
405 std::size(kDartSystraceTraceBufferArgs));
407 std::size(kDartSystraceTraceStreamsArgs));
411 std::size(kDartDefaultTraceStreamsArgs));
417 std::size(kDartProfileMicrotasksArgs));
420 std::string old_gen_heap_size_args;
422 old_gen_heap_size_args =
424 args.push_back(old_gen_heap_size_args.c_str());
427 for (
size_t i = 0;
i <
settings_.dart_flags.size();
i++) {
431 char* flags_error = Dart_SetVMFlags(
args.size(),
args.data());
433 FML_LOG(FATAL) <<
"Error while setting Dart VM flags: " << flags_error;
437 dart::bin::SetExecutableName(
settings_.executable_name.c_str());
441 Dart_InitializeParams
params = {};
442 params.version = DART_INITIALIZE_PARAMS_CURRENT_VERSION;
443 params.vm_snapshot_data = vm_data_->GetVMSnapshot().GetDataMapping();
444 params.vm_snapshot_instructions =
445 vm_data_->GetVMSnapshot().GetInstructionsMapping();
446 params.create_group =
reinterpret_cast<decltype(
params.create_group)
>(
447 DartIsolate::DartIsolateGroupCreateCallback);
448 params.initialize_isolate =
449 reinterpret_cast<decltype(
params.initialize_isolate)
>(
450 DartIsolate::DartIsolateInitializeCallback);
452 reinterpret_cast<decltype(
params.shutdown_isolate)
>(
453 DartIsolate::DartIsolateShutdownCallback);
454 params.cleanup_isolate =
reinterpret_cast<decltype(
params.cleanup_isolate)
>(
455 DartIsolate::DartIsolateCleanupCallback);
456 params.cleanup_group =
reinterpret_cast<decltype(
params.cleanup_group)
>(
457 DartIsolate::DartIsolateGroupCleanupCallback);
459 params.file_open = dart::bin::OpenFile;
460 params.file_read = dart::bin::ReadFile;
461 params.file_write = dart::bin::WriteFile;
462 params.file_close = dart::bin::CloseFile;
463 params.entropy_source = dart::bin::GetEntropy;
474 int64_t micros = Dart_TimelineGetMicros();
475 Dart_RecordTimelineEvent(
"FlutterEngineMainEnter",
480 Dart_Timeline_Event_Instant,
487 Dart_SetFileModifiedCallback(&DartFileModifiedCallback);
490 Dart_SetServiceStreamCallbacks(&ServiceStreamListenCallback,
491 &ServiceStreamCancelCallback);
493 Dart_SetEmbedderInformationCallback(&EmbedderInformationCallback);
495 if (
settings_.dart_library_sources_kernel !=
nullptr) {
496 std::unique_ptr<fml::Mapping> dart_library_sources =
499 Dart_SetDartLibrarySourcesKernel(dart_library_sources->GetMapping(),
500 dart_library_sources->GetSize());
504 concurrent_message_loop_->PostTaskToAllWorkers(
505 [] { Dart_SetThreadName(
"FlutterConcurrentMessageLoopWorker"); });
511 SkExecutor::SetDefault(
nullptr);
513 if (Dart_CurrentIsolate() !=
nullptr) {
519 dart::bin::CleanupDartIo();
522std::shared_ptr<const DartVMData> DartVM::GetVMData()
const {
530std::shared_ptr<ServiceProtocol> DartVM::GetServiceProtocol()
const {
531 return service_protocol_;
534std::shared_ptr<IsolateNameServer> DartVM::GetIsolateNameServer()
const {
535 return isolate_name_server_;
538std::shared_ptr<fml::ConcurrentTaskRunner>
539DartVM::GetConcurrentWorkerTaskRunner()
const {
540 return concurrent_message_loop_->GetTaskRunner();
543std::shared_ptr<fml::ConcurrentMessageLoop> DartVM::GetConcurrentMessageLoop() {
544 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)