Skip to content

Commit cd95fcc

Browse files
a-sivacommit-bot@chromium.org
authored andcommitted
[VM] Start the kernel isolate only when compilations are needed
- Start the kernel isolate only when a compilation is needed - Remove some dead code and state in the loader path Change-Id: I39e14cd79cda18b155af6334a11635c44e2fc11f Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/121923 Reviewed-by: Ryan Macnak <rmacnak@google.com> Commit-Queue: Siva Annamalai <asiva@google.com>
1 parent 21972da commit cd95fcc

File tree

11 files changed

+59
-129
lines changed

11 files changed

+59
-129
lines changed

runtime/bin/loader.cc

Lines changed: 18 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ extern DFE dfe;
2626
#endif
2727

2828
// Keep in sync with loader.dart.
29+
static const intptr_t _Dart_kInitLoader = 4;
2930
static const intptr_t _Dart_kImportExtension = 9;
3031
static const intptr_t _Dart_kResolveAsFilePath = 10;
3132

@@ -136,9 +137,6 @@ void Loader::Init(const char* package_root,
136137
Dart_Port loader_port = Builtin::LoadPort();
137138
ASSERT(loader_port != ILLEGAL_PORT);
138139

139-
// Keep in sync with loader.dart.
140-
const intptr_t _Dart_kInitLoader = 4;
141-
142140
Dart_Handle request = Dart_NewList(9);
143141
Dart_ListSetAt(request, 0, trace_loader ? Dart_True() : Dart_False());
144142
Dart_ListSetAt(request, 1, Dart_NewInteger(Dart_GetMainPortId()));
@@ -552,12 +550,6 @@ Dart_Handle Loader::SendAndProcessReply(intptr_t tag,
552550
return Dart_Null();
553551
}
554552

555-
Dart_Handle Loader::LoadUrlContents(Dart_Handle url,
556-
uint8_t** payload,
557-
intptr_t* payload_length) {
558-
return SendAndProcessReply(Dart_kScriptTag, url, payload, payload_length);
559-
}
560-
561553
Dart_Handle Loader::ResolveAsFilePath(Dart_Handle url,
562554
uint8_t** payload,
563555
intptr_t* payload_length) {
@@ -575,14 +567,6 @@ Dart_Handle Loader::LibraryTagHandler(Dart_LibraryTag tag,
575567
Dart_Handle url) {
576568
return Dart_Null();
577569
}
578-
579-
Dart_Handle Loader::DartColonLibraryTagHandler(Dart_LibraryTag tag,
580-
Dart_Handle library,
581-
Dart_Handle url,
582-
const char* library_url_string,
583-
const char* url_string) {
584-
return Dart_Null();
585-
}
586570
#else
587571
static void MallocFinalizer(void* isolate_callback_data,
588572
Dart_WeakPersistentHandle handle,
@@ -593,18 +577,28 @@ static void MallocFinalizer(void* isolate_callback_data,
593577
Dart_Handle Loader::LibraryTagHandler(Dart_LibraryTag tag,
594578
Dart_Handle library,
595579
Dart_Handle url) {
580+
const char* url_string = NULL;
581+
Dart_Handle result = Dart_StringToCString(url, &url_string);
582+
if (Dart_IsError(result)) {
583+
return result;
584+
}
596585
if (tag == Dart_kCanonicalizeUrl) {
597586
Dart_Handle library_url = Dart_LibraryUrl(library);
598587
if (Dart_IsError(library_url)) {
599588
return library_url;
600589
}
590+
const char* library_url_string = NULL;
591+
result = Dart_StringToCString(library_url, &library_url_string);
592+
if (Dart_IsError(result)) {
593+
return result;
594+
}
595+
bool is_dart_scheme_url = DartUtils::IsDartSchemeURL(url_string);
596+
bool is_dart_library = DartUtils::IsDartSchemeURL(library_url_string);
597+
if (is_dart_scheme_url || is_dart_library) {
598+
return url;
599+
}
601600
return Dart_DefaultCanonicalizeUrl(library_url, url);
602601
}
603-
const char* url_string = NULL;
604-
Dart_Handle result = Dart_StringToCString(url, &url_string);
605-
if (Dart_IsError(result)) {
606-
return result;
607-
}
608602
if (tag == Dart_kKernelTag) {
609603
uint8_t* kernel_buffer = NULL;
610604
intptr_t kernel_buffer_size = 0;
@@ -650,27 +644,6 @@ Dart_Handle Loader::LibraryTagHandler(Dart_LibraryTag tag,
650644
free(lib_path);
651645
return result;
652646
}
653-
if (tag != Dart_kScriptTag) {
654-
// Special case for handling dart: imports and parts.
655-
// Grab the library's url.
656-
Dart_Handle library_url = Dart_LibraryUrl(library);
657-
if (Dart_IsError(library_url)) {
658-
return library_url;
659-
}
660-
const char* library_url_string = NULL;
661-
result = Dart_StringToCString(library_url, &library_url_string);
662-
if (Dart_IsError(result)) {
663-
return result;
664-
}
665-
666-
bool is_dart_scheme_url = DartUtils::IsDartSchemeURL(url_string);
667-
bool is_dart_library = DartUtils::IsDartSchemeURL(library_url_string);
668-
669-
if (is_dart_scheme_url || is_dart_library) {
670-
return DartColonLibraryTagHandler(tag, library, url, library_url_string,
671-
url_string);
672-
}
673-
}
674647
if (DartUtils::IsDartExtensionSchemeURL(url_string)) {
675648
// Handle early error cases for dart-ext: imports.
676649
if (tag != Dart_kImportTag) {
@@ -685,15 +658,7 @@ Dart_Handle Loader::LibraryTagHandler(Dart_LibraryTag tag,
685658

686659
auto isolate_data = reinterpret_cast<IsolateData*>(Dart_CurrentIsolateData());
687660
ASSERT(isolate_data != NULL);
688-
if ((tag == Dart_kScriptTag) && Dart_IsString(library)) {
689-
// Update packages file for isolate.
690-
const char* packages_file = NULL;
691-
Dart_Handle result = Dart_StringToCString(library, &packages_file);
692-
if (Dart_IsError(result)) {
693-
return result;
694-
}
695-
isolate_data->UpdatePackagesFile(packages_file);
696-
}
661+
697662
// Grab this isolate's loader.
698663
Loader* loader = NULL;
699664

@@ -712,10 +677,8 @@ Dart_Handle Loader::LibraryTagHandler(Dart_LibraryTag tag,
712677
loader = new Loader(isolate_data);
713678
loader->Init(isolate_data->isolate_group_data()->package_root,
714679
isolate_data->packages_file(),
715-
DartUtils::original_working_directory,
716-
(tag == Dart_kScriptTag) ? url_string : NULL);
680+
DartUtils::original_working_directory, NULL);
717681
} else {
718-
ASSERT(tag != Dart_kScriptTag);
719682
// The isolate has a loader -- this is an inner invocation that will queue
720683
// work with the service isolate.
721684
// Use the existing loader.
@@ -793,21 +756,6 @@ Dart_Handle Loader::LibraryTagHandler(Dart_LibraryTag tag,
793756
}
794757
return Dart_Null();
795758
}
796-
797-
Dart_Handle Loader::DartColonLibraryTagHandler(Dart_LibraryTag tag,
798-
Dart_Handle library,
799-
Dart_Handle url,
800-
const char* library_url_string,
801-
const char* url_string) {
802-
// Handle canonicalization, 'import' and 'part' of 'dart:' libraries.
803-
if (tag == Dart_kCanonicalizeUrl) {
804-
// These will be handled internally.
805-
return url;
806-
}
807-
// All cases should have been handled above.
808-
UNREACHABLE();
809-
return Dart_Null();
810-
}
811759
#endif // !defined(DART_PRECOMPILED_RUNTIME)
812760

813761
void Loader::InitOnce() {

runtime/bin/loader.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@ class Loader {
2525

2626
static Dart_Handle ReloadNativeExtensions();
2727

28-
// Loads contents of the specified url.
29-
static Dart_Handle LoadUrlContents(Dart_Handle url,
30-
uint8_t** payload,
31-
intptr_t* payload_length);
32-
3328
static void ResolveDependenciesAsFilePaths();
3429

3530
// A static tag handler that hides all usage of a loader for an isolate.
@@ -122,13 +117,6 @@ class Loader {
122117
/// Returns false if an error occurred and the loader should quit.
123118
bool ProcessQueueLocked(ProcessResult process_result);
124119

125-
// Special inner tag handler for dart: uris.
126-
static Dart_Handle DartColonLibraryTagHandler(Dart_LibraryTag tag,
127-
Dart_Handle library,
128-
Dart_Handle url,
129-
const char* library_url_string,
130-
const char* url_string);
131-
132120
// We use one native message handler callback for N loaders. The native
133121
// message handler callback provides us with the Dart_Port which we use as a
134122
// key into our map of active loaders from |port| to |isolate_data|.

runtime/bin/main.cc

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -408,11 +408,7 @@ static Dart_Isolate IsolateSetupHelper(Dart_Isolate isolate,
408408
Dart_Handle uri =
409409
DartUtils::ResolveScript(Dart_NewStringFromCString(script_uri));
410410
CHECK_RESULT(uri);
411-
if (kernel_buffer == NULL) {
412-
result = Loader::LibraryTagHandler(Dart_kScriptTag, Dart_Null(), uri);
413-
CHECK_RESULT(result);
414-
} else {
415-
// Various core-library parts will send requests to the Loader to resolve
411+
if (kernel_buffer != NULL) {
416412
// relative URIs and perform other related tasks. We need Loader to be
417413
// initialized for this to work because loading from Kernel binary
418414
// bypasses normal source code loading paths that initialize it.
@@ -421,7 +417,6 @@ static Dart_Isolate IsolateSetupHelper(Dart_Isolate isolate,
421417
CHECK_RESULT(result);
422418
Loader::InitForSnapshot(resolved_script_uri, isolate_data);
423419
}
424-
425420
Dart_TimelineEvent("LoadScript", Dart_TimelineGetMicros(),
426421
Dart_GetMainPortId(), Dart_Timeline_Event_Async_End, 0,
427422
NULL, NULL);

runtime/bin/vmservice/loader.dart

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -952,9 +952,6 @@ void shutdownLoaders() {
952952

953953
// See Dart_LibraryTag in dart_api.h
954954
const _Dart_kCanonicalizeUrl = 0; // Canonicalize the URL.
955-
const _Dart_kScriptTag = 1; // Load the root script.
956-
const _Dart_kSourceTag = 2; // Load a part source.
957-
const _Dart_kImportTag = 3; // Import a library.
958955

959956
// Extra requests. Keep these in sync between loader.dart and builtin.dart.
960957
const _Dart_kInitLoader = 4; // Initialize the loader.

runtime/include/dart_api.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2908,8 +2908,6 @@ DART_EXPORT Dart_Handle Dart_GetNativeSymbol(Dart_Handle library,
29082908

29092909
typedef enum {
29102910
Dart_kCanonicalizeUrl = 0,
2911-
Dart_kScriptTag,
2912-
Dart_kSourceTag,
29132911
Dart_kImportTag,
29142912
Dart_kKernelTag,
29152913
Dart_kImportExtensionTag,
@@ -2930,14 +2928,6 @@ typedef enum {
29302928
* of this tag. The return value should be a string holding the
29312929
* canonicalized url.
29322930
*
2933-
* Dart_kScriptTag
2934-
*
2935-
* No longer used.
2936-
*
2937-
* Dart_kSourceTag
2938-
*
2939-
* No longer used.
2940-
*
29412931
* Dart_kImportTag
29422932
*
29432933
* This tag is used to load a library from IsolateMirror.loadUri. The embedder

runtime/vm/dart.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ char* Dart::Init(const uint8_t* vm_isolate_snapshot,
401401

402402
#ifndef DART_PRECOMPILED_RUNTIME
403403
if (start_kernel_isolate) {
404-
KernelIsolate::Run();
404+
KernelIsolate::InitializeState();
405405
}
406406
#endif // DART_PRECOMPILED_RUNTIME
407407

runtime/vm/kernel_isolate.cc

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ const int KernelIsolate::kNotifyIsolateShutdown = 6;
6161
const char* KernelIsolate::kName = DART_KERNEL_ISOLATE_NAME;
6262
Dart_IsolateGroupCreateCallback KernelIsolate::create_group_callback_ = NULL;
6363
Monitor* KernelIsolate::monitor_ = new Monitor();
64-
KernelIsolate::State KernelIsolate::state_ = KernelIsolate::kStopped;
64+
KernelIsolate::State KernelIsolate::state_ = KernelIsolate::kNotStarted;
6565
Isolate* KernelIsolate::isolate_ = NULL;
6666
Dart_Port KernelIsolate::kernel_port_ = ILLEGAL_PORT;
6767

@@ -217,30 +217,45 @@ class RunKernelTask : public ThreadPool::Task {
217217
}
218218
};
219219

220-
void KernelIsolate::Run() {
221-
{
222-
MonitorLocker ml(monitor_);
223-
ASSERT(state_ == kStopped);
224-
state_ = kStarting;
225-
ml.NotifyAll();
226-
}
220+
void KernelIsolate::InitializeState() {
227221
// Grab the isolate create callback here to avoid race conditions with tests
228222
// that change this after Dart_Initialize returns.
223+
if (FLAG_trace_kernel) {
224+
OS::PrintErr(DART_KERNEL_ISOLATE_NAME ": InitializeState\n");
225+
}
229226
create_group_callback_ = Isolate::CreateGroupCallback();
230227
if (create_group_callback_ == NULL) {
231228
KernelIsolate::InitializingFailed();
232229
return;
233230
}
234-
bool task_started = Dart::thread_pool()->Run<RunKernelTask>();
235-
ASSERT(task_started);
231+
}
232+
233+
bool KernelIsolate::Start() {
234+
bool start_task = false;
235+
{
236+
MonitorLocker ml(monitor_);
237+
if (state_ == kNotStarted) {
238+
if (FLAG_trace_kernel) {
239+
OS::PrintErr(DART_KERNEL_ISOLATE_NAME ": Start\n");
240+
}
241+
start_task = true;
242+
state_ = kStarting;
243+
ml.NotifyAll();
244+
}
245+
}
246+
bool task_started = true;
247+
if (start_task) {
248+
task_started = Dart::thread_pool()->Run<RunKernelTask>();
249+
}
250+
return task_started;
236251
}
237252

238253
void KernelIsolate::Shutdown() {
239254
MonitorLocker ml(monitor_);
240255
while (state_ == kStarting) {
241256
ml.Wait();
242257
}
243-
if (state_ == kStopped) {
258+
if (state_ == kStopped || state_ == kNotStarted) {
244259
return;
245260
}
246261
ASSERT(state_ == kStarted);
@@ -824,6 +839,14 @@ Dart_KernelCompilationResult KernelIsolate::CompileToKernel(
824839
const char* package_config,
825840
const char* multiroot_filepaths,
826841
const char* multiroot_scheme) {
842+
// Start the kernel Isolate if it is not already running.
843+
if (!Start()) {
844+
Dart_KernelCompilationResult result = {};
845+
result.status = Dart_KernelCompilationStatus_Unknown;
846+
result.error = strdup("Error while starting Kernel isolate task");
847+
return result;
848+
}
849+
827850
// This must be the main script to be loaded. Wait for Kernel isolate
828851
// to finish initialization.
829852
Dart_Port kernel_port = WaitForKernelPort();

runtime/vm/kernel_isolate.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ class KernelIsolate : public AllStatic {
2929
static const int kListDependenciesTag;
3030
static const int kNotifyIsolateShutdown;
3131

32-
static void Run();
32+
static void InitializeState();
33+
static bool Start();
3334
static void Shutdown();
3435

3536
static bool NameEquals(const char* name);
@@ -83,6 +84,7 @@ class KernelIsolate : public AllStatic {
8384
static Dart_IsolateGroupCreateCallback create_group_callback_;
8485
static Monitor* monitor_;
8586
enum State {
87+
kNotStarted,
8688
kStopped,
8789
kStarting,
8890
kStarted,

runtime/vm/unit_test.cc

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -359,11 +359,6 @@ static Dart_Handle LibraryTagHandler(Dart_LibraryTag tag,
359359
}
360360
return Dart_DefaultCanonicalizeUrl(library_url, url);
361361
}
362-
if (tag == Dart_kScriptTag) {
363-
// Reload request.
364-
UNREACHABLE();
365-
return Dart_Null();
366-
}
367362
if (!Dart_IsLibrary(library)) {
368363
return Dart_NewApiError("not a library");
369364
}
@@ -428,14 +423,8 @@ static Dart_Handle LibraryTagHandler(Dart_LibraryTag tag,
428423
// Do sync loading since unit_test doesn't support async.
429424
Dart_Handle source = DartUtils::ReadStringFromFile(resolved_url_chars);
430425
EXPECT_VALID(source);
431-
if (tag == Dart_kImportTag) {
432-
UNREACHABLE();
433-
return Dart_Null();
434-
} else {
435-
ASSERT(tag == Dart_kSourceTag);
436-
UNREACHABLE();
437-
return Dart_Null();
438-
}
426+
UNREACHABLE();
427+
return Dart_Null();
439428
}
440429

441430
static intptr_t BuildSourceFilesArray(

sdk/lib/_internal/vm/bin/builtin.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ int _isolateId;
4545
// Requests made to the service isolate over the load port.
4646

4747
// Extra requests. Keep these in sync between loader.dart and builtin.dart.
48-
const _Dart_kInitLoader = 4; // Initialize the loader.
4948
const _Dart_kResourceLoad = 5; // Resource class support.
5049
const _Dart_kGetPackageRootUri = 6; // Uri of the packages/ directory.
5150
const _Dart_kGetPackageConfigUri = 7; // Uri of the .packages file.

0 commit comments

Comments
 (0)