Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

[engine] set platform thread name to ui. #55362

Merged
merged 5 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
testing
  • Loading branch information
jonahwilliams committed Sep 25, 2024
commit 9d2491b5cacde522b7bb72bc166b9b49b4d714a1
12 changes: 9 additions & 3 deletions runtime/dart_isolate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -633,10 +633,16 @@ bool DartIsolate::UpdateThreadPoolNames() const {
}

if (auto task_runner = task_runners.GetPlatformTaskRunner()) {
bool is_merged_platform_ui_thread =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should have a static method that returns if current thread is the platform main thread (with platform specific implementations for checks).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what sort of platform specific checks would we perform?

task_runner == task_runners.GetUITaskRunner();
std::string label;
if (is_merged_platform_ui_thread) {
label = task_runners.GetLabel() + std::string{".ui"};
} else {
label = task_runners.GetLabel() + std::string{".platform"};
}
task_runner->PostTask(
[label = task_runners.GetLabel() + std::string{".platform"}]() {
Dart_SetThreadName(label.c_str());
});
[label = std::move(label)]() { Dart_SetThreadName(label.c_str()); });
}

return true;
Expand Down
61 changes: 61 additions & 0 deletions runtime/dart_isolate_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "third_party/dart/runtime/include/dart_api.h"
#include "third_party/tonic/converter/dart_converter.h"
#include "third_party/tonic/scopes/dart_isolate_scope.h"
#include "vm/os_thread.h"

// CREATE_NATIVE_ENTRY is leaky by design
// NOLINTBEGIN(clang-analyzer-core.StackAddressEscape)
Expand Down Expand Up @@ -1185,6 +1186,66 @@ TEST_F(DartIsolateTest, PlatformIsolateMainThrowsError) {
// root isolate will be auto-shutdown
}

TEST_F(DartIsolateTest, DartPlatformThreadNameIsSetWithMergedThreads) {
ASSERT_FALSE(DartVMRef::IsInstanceRunning());

const auto settings = CreateSettingsForFixture();
auto vm_ref = DartVMRef::Create(settings);
auto platform_thread = CreateNewThread();
auto other_thread = CreateNewThread();
// Set platform and UI thread runner to the same thread.
TaskRunners task_runners("io.flutter", //
platform_thread, //
other_thread, //
platform_thread, //
other_thread //
);
auto isolate = RunDartCodeInIsolate(vm_ref, settings, task_runners, "main",
{}, GetDefaultKernelFilePath());
ASSERT_TRUE(isolate);
ASSERT_EQ(isolate->get()->GetPhase(), DartIsolate::Phase::Running);

fml::AutoResetWaitableEvent latch;
std::string thread_name;
platform_thread->PostTask([&latch, &thread_name]() {
dart::OSThread* thread = dart::OSThread::Current();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this isn't really a public API :(

thread_name = std::string(thread->name());
latch.Signal();
});
latch.Wait();

EXPECT_EQ(thread_name, "io.flutter.ui");
}

TEST_F(DartIsolateTest, DartPlatformThreadNameIsSet) {
ASSERT_FALSE(DartVMRef::IsInstanceRunning());

const auto settings = CreateSettingsForFixture();
auto vm_ref = DartVMRef::Create(settings);
// Set platform and UI thread runner to the same thread.
TaskRunners task_runners("io.flutter", //
CreateNewThread(), //
CreateNewThread(), //
CreateNewThread(), //
CreateNewThread() //
);
auto isolate = RunDartCodeInIsolate(vm_ref, settings, task_runners, "main",
{}, GetDefaultKernelFilePath());
ASSERT_TRUE(isolate);
ASSERT_EQ(isolate->get()->GetPhase(), DartIsolate::Phase::Running);

fml::AutoResetWaitableEvent latch;
std::string thread_name;
task_runners.GetPlatformTaskRunner()->PostTask([&latch, &thread_name]() {
dart::OSThread* thread = dart::OSThread::Current();
thread_name = std::string(thread->name());
latch.Signal();
});
latch.Wait();

EXPECT_EQ(thread_name, "io.flutter.platform");
}

} // namespace testing
} // namespace flutter

Expand Down
9 changes: 1 addition & 8 deletions shell/platform/android/android_shell_holder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,10 @@ AndroidShellHolder::AndroidShellHolder(

flutter::ThreadHost::ThreadHostConfig host_config(
thread_label, mask, AndroidPlatformThreadConfigSetter);

auto ui_config = fml::Thread::ThreadConfig(
host_config.ui_config = fml::Thread::ThreadConfig(
flutter::ThreadHost::ThreadHostConfig::MakeThreadName(
flutter::ThreadHost::Type::kUi, thread_label),
fml::Thread::ThreadPriority::kDisplay);
if (!settings.merged_platform_ui_thread) {
host_config.ui_config = ui_config;
} else {
fml::Thread::SetCurrentThreadName(ui_config);
}

host_config.raster_config = fml::Thread::ThreadConfig(
flutter::ThreadHost::ThreadHostConfig::MakeThreadName(
flutter::ThreadHost::Type::kRaster, thread_label),
Expand Down
8 changes: 1 addition & 7 deletions shell/platform/darwin/ios/framework/Source/FlutterEngine.mm
Original file line number Diff line number Diff line change
Expand Up @@ -805,16 +805,10 @@ + (NSString*)generateThreadLabel:(NSString*)labelPrefix {
flutter::ThreadHost::ThreadHostConfig host_config(thread_label.UTF8String, threadHostType,
IOSPlatformThreadConfigSetter);

auto ui_config =
host_config.ui_config =
fml::Thread::ThreadConfig(flutter::ThreadHost::ThreadHostConfig::MakeThreadName(
flutter::ThreadHost::Type::kUi, thread_label.UTF8String),
fml::Thread::ThreadPriority::kDisplay);
if (!settings.enable_impeller) {
host_config.ui_config = ui_config;
} else {
// fml::Thread::SetCurrentThreadName(ui_config);
}

host_config.raster_config =
fml::Thread::ThreadConfig(flutter::ThreadHost::ThreadHostConfig::MakeThreadName(
flutter::ThreadHost::Type::kRaster, thread_label.UTF8String),
Expand Down