From b00c5498401fc33cf2e760e0c361fcc2532c0f97 Mon Sep 17 00:00:00 2001 From: David Worsham Date: Wed, 4 Aug 2021 18:05:02 -0700 Subject: [PATCH] Fix thread names + add test (flutter/engine#27897) --- .../shell/platform/fuchsia/flutter/BUILD.gn | 6 ++- .../platform/fuchsia/flutter/component.cc | 2 - .../shell/platform/fuchsia/flutter/engine.cc | 14 +++--- .../shell/platform/fuchsia/flutter/engine.h | 2 + .../shell/platform/fuchsia/flutter/main.cc | 2 - .../fuchsia/flutter/tests/engine_unittests.cc | 43 +++++++++++++++++++ 6 files changed, 58 insertions(+), 11 deletions(-) diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/BUILD.gn b/engine/src/flutter/shell/platform/fuchsia/flutter/BUILD.gn index 70dae94473..c4d126e84a 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/BUILD.gn +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/BUILD.gn @@ -21,6 +21,10 @@ shell_gpu_configuration("fuchsia_gpu_configuration") { enable_metal = false } +config("runner_base_config") { + defines = [ "FML_USED_ON_EMBEDDER" ] +} + config("runner_debug_config") { defines = [ "DEBUG" ] # Needed due to direct dart dependencies. } @@ -36,7 +40,7 @@ config("runner_product_config") { template("runner_sources") { assert(defined(invoker.product), "runner_sources must define product") - runner_configs = [] + runner_configs = [ ":runner_base_config" ] if (is_debug) { runner_configs += [ ":runner_debug_config" ] } diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/component.cc b/engine/src/flutter/shell/platform/fuchsia/flutter/component.cc index 3f75c3bd6a..c7efd208b5 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/component.cc +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/component.cc @@ -2,8 +2,6 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#define FML_USED_ON_EMBEDDER - #include "component.h" #include diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/engine.cc b/engine/src/flutter/shell/platform/fuchsia/flutter/engine.cc index 350e0e4091..502b799535 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/engine.cc +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/engine.cc @@ -2,8 +2,6 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#define FML_USED_ON_EMBEDDER - #include "engine.h" #include @@ -43,6 +41,13 @@ std::unique_ptr MakeLocalizationPlatformMessage( } // namespace +flutter::ThreadHost Engine::CreateThreadHost(const std::string& name_prefix) { + fml::Thread::SetCurrentThreadName(name_prefix + ".platform"); + return flutter::ThreadHost(name_prefix, flutter::ThreadHost::Type::RASTER | + flutter::ThreadHost::Type::UI | + flutter::ThreadHost::Type::IO); +} + Engine::Engine(Delegate& delegate, std::string thread_label, std::shared_ptr svc, @@ -55,10 +60,7 @@ Engine::Engine(Delegate& delegate, FlutterRunnerProductConfiguration product_config) : delegate_(delegate), thread_label_(std::move(thread_label)), - thread_host_(thread_label_ + ".", - flutter::ThreadHost::Type::RASTER | - flutter::ThreadHost::Type::UI | - flutter::ThreadHost::Type::IO), + thread_host_(CreateThreadHost(thread_label_)), intercept_all_input_(product_config.get_intercept_all_input()), weak_factory_(this) { // Get the task runners from the managed threads. The current thread will be diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/engine.h b/engine/src/flutter/shell/platform/fuchsia/flutter/engine.h index bc7a66fdc2..5108b030b9 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/engine.h +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/engine.h @@ -45,6 +45,8 @@ class Engine final { virtual void OnEngineTerminate(const Engine* holder) = 0; }; + static flutter::ThreadHost CreateThreadHost(const std::string& name_prefix); + Engine(Delegate& delegate, std::string thread_label, std::shared_ptr svc, diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/main.cc b/engine/src/flutter/shell/platform/fuchsia/flutter/main.cc index d5bfebb66f..cd34ab83cb 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/main.cc +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/main.cc @@ -2,8 +2,6 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#define FML_USED_ON_EMBEDDER - #include #include #include diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/tests/engine_unittests.cc b/engine/src/flutter/shell/platform/fuchsia/flutter/tests/engine_unittests.cc index b0778789ae..534aa6a7c7 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/tests/engine_unittests.cc +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/tests/engine_unittests.cc @@ -12,6 +12,7 @@ #include "flutter/fml/message_loop_impl.h" #include "flutter/fml/task_runner.h" #include "flutter/shell/common/serialization_callbacks.h" +#include "flutter/shell/common/thread_host.h" #include "flutter/shell/platform/fuchsia/flutter/gfx_session_connection.h" #include "flutter/shell/platform/fuchsia/flutter/logging.h" #include "flutter/shell/platform/fuchsia/flutter/runner.h" @@ -25,6 +26,13 @@ using namespace flutter; namespace flutter_runner { namespace testing { +namespace { + +std::string GetCurrentTestName() { + return ::testing::UnitTest::GetInstance()->current_test_info()->name(); +} + +} // namespace class MockTaskRunner : public fml::BasicTaskRunner { public: @@ -83,6 +91,41 @@ class EngineTest : public ::testing::Test { std::optional session_; }; +TEST_F(EngineTest, ThreadNames) { + std::string prefix = GetCurrentTestName(); + flutter::ThreadHost engine_thread_host = Engine::CreateThreadHost(prefix); + + char thread_name[ZX_MAX_NAME_LEN]; + zx::thread::self()->get_property(ZX_PROP_NAME, thread_name, + sizeof(thread_name)); + EXPECT_EQ(std::string(thread_name), prefix + std::string(".platform")); + EXPECT_EQ(engine_thread_host.platform_thread, nullptr); + + engine_thread_host.raster_thread->GetTaskRunner()->PostTask([&prefix]() { + char thread_name[ZX_MAX_NAME_LEN]; + zx::thread::self()->get_property(ZX_PROP_NAME, thread_name, + sizeof(thread_name)); + EXPECT_EQ(std::string(thread_name), prefix + std::string(".raster")); + }); + engine_thread_host.raster_thread->Join(); + + engine_thread_host.ui_thread->GetTaskRunner()->PostTask([&prefix]() { + char thread_name[ZX_MAX_NAME_LEN]; + zx::thread::self()->get_property(ZX_PROP_NAME, thread_name, + sizeof(thread_name)); + EXPECT_EQ(std::string(thread_name), prefix + std::string(".ui")); + }); + engine_thread_host.ui_thread->Join(); + + engine_thread_host.io_thread->GetTaskRunner()->PostTask([&prefix]() { + char thread_name[ZX_MAX_NAME_LEN]; + zx::thread::self()->get_property(ZX_PROP_NAME, thread_name, + sizeof(thread_name)); + EXPECT_EQ(std::string(thread_name), prefix + std::string(".io")); + }); + engine_thread_host.io_thread->Join(); +} + TEST_F(EngineTest, SkpWarmup) { SkISize draw_size = SkISize::Make(100, 100); SkPictureRecorder recorder;