Commit 7f2e7237 authored by Carlos Caballero's avatar Carlos Caballero Committed by Commit Bot

[Reland] Feature to run Blink main thread using SequenceManager

This will enable us to start trialing the MessageLoop replacement by
SequenceManager.

We only do this in the Blink main thread for now. There is some work
needed around SequenceManager ownership to be done before this can be
enabled in worker threads.

Original change https://chromium-review.googlesource.com/c/1349316

TBR=dcheng@chromium.org

Bug: 891670
Change-Id: I68a59ec2d780106bb5fd76cee5976e236f9316f6
Reviewed-on: https://chromium-review.googlesource.com/c/1358452
Commit-Queue: Carlos Caballero <carlscab@google.com>
Reviewed-by: default avatarAlex Clarke <alexclarke@chromium.org>
Cr-Commit-Position: refs/heads/master@{#613132}
parent 354bbdb6
...@@ -172,9 +172,13 @@ class SequenceManager { ...@@ -172,9 +172,13 @@ class SequenceManager {
BASE_EXPORT std::unique_ptr<SequenceManager> BASE_EXPORT std::unique_ptr<SequenceManager>
CreateSequenceManagerOnCurrentThread(); CreateSequenceManagerOnCurrentThread();
// Create a SequenceManager for a MessagePump on the current thread. // Create a SequenceManager using the given MessagePump on the current thread.
// MessagePump instances can be created with
// MessageLoop::CreateMessagePumpForType().
BASE_EXPORT std::unique_ptr<SequenceManager> BASE_EXPORT std::unique_ptr<SequenceManager>
CreateSequenceManagerOnCurrentThreadWithPump(MessageLoop::Type type); CreateSequenceManagerOnCurrentThreadWithPump(
MessageLoop::Type type,
std::unique_ptr<MessagePump> message_pump);
// Create a SequenceManager for a future thread using the provided MessageLoop. // Create a SequenceManager for a future thread using the provided MessageLoop.
// The SequenceManager can be initialized on the current thread and then needs // The SequenceManager can be initialized on the current thread and then needs
......
...@@ -42,11 +42,11 @@ std::unique_ptr<SequenceManager> CreateSequenceManagerOnCurrentThread() { ...@@ -42,11 +42,11 @@ std::unique_ptr<SequenceManager> CreateSequenceManagerOnCurrentThread() {
} }
std::unique_ptr<SequenceManager> CreateSequenceManagerOnCurrentThreadWithPump( std::unique_ptr<SequenceManager> CreateSequenceManagerOnCurrentThreadWithPump(
MessageLoop::Type type) { MessageLoop::Type type,
std::unique_ptr<MessagePump> message_pump) {
std::unique_ptr<SequenceManager> sequence_manager = std::unique_ptr<SequenceManager> sequence_manager =
internal::SequenceManagerImpl::CreateUnboundWithPump(type); internal::SequenceManagerImpl::CreateUnboundWithPump(type);
sequence_manager->BindToMessagePump( sequence_manager->BindToMessagePump(std::move(message_pump));
MessageLoop::CreateMessagePumpForType(type));
return sequence_manager; return sequence_manager;
} }
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include "base/command_line.h" #include "base/command_line.h"
#include "base/debug/debugger.h" #include "base/debug/debugger.h"
#include "base/debug/leak_annotations.h" #include "base/debug/leak_annotations.h"
#include "base/feature_list.h"
#include "base/i18n/rtl.h" #include "base/i18n/rtl.h"
#include "base/message_loop/message_loop.h" #include "base/message_loop/message_loop.h"
#include "base/metrics/histogram_macros.h" #include "base/metrics/histogram_macros.h"
...@@ -18,6 +19,7 @@ ...@@ -18,6 +19,7 @@
#include "base/strings/string_number_conversions.h" #include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h" #include "base/strings/string_util.h"
#include "base/system/sys_info.h" #include "base/system/sys_info.h"
#include "base/task/sequence_manager/sequence_manager.h"
#include "base/threading/platform_thread.h" #include "base/threading/platform_thread.h"
#include "base/timer/hi_res_timer_manager.h" #include "base/timer/hi_res_timer_manager.h"
#include "base/trace_event/trace_event.h" #include "base/trace_event/trace_event.h"
...@@ -64,6 +66,10 @@ ...@@ -64,6 +66,10 @@
namespace content { namespace content {
namespace { namespace {
const base::Feature kMainThreadUsesSequenceManager{
"BlinkMainThreadUsesSequenceManager", base::FEATURE_DISABLED_BY_DEFAULT};
// This function provides some ways to test crash and assertion handling // This function provides some ways to test crash and assertion handling
// behavior of the renderer. // behavior of the renderer.
static void HandleRendererErrorTestParameters( static void HandleRendererErrorTestParameters(
...@@ -75,6 +81,18 @@ static void HandleRendererErrorTestParameters( ...@@ -75,6 +81,18 @@ static void HandleRendererErrorTestParameters(
WaitForDebugger("Renderer"); WaitForDebugger("Renderer");
} }
std::unique_ptr<base::MessagePump> CreateMainThreadMessagePump() {
#if defined(OS_MACOSX)
// As long as scrollbars on Mac are painted with Cocoa, the message pump
// needs to be backed by a Foundation-level loop to process NSTimers. See
// http://crbug.com/306348#c24 for details.
return std::make_unique<base::MessagePumpNSRunLoop>();
#else
return base::MessageLoop::CreateMessagePumpForType(
base::MessageLoop::TYPE_DEFAULT);
#endif
}
} // namespace } // namespace
// mainline routine for running as the Renderer process // mainline routine for running as the Renderer process
...@@ -126,17 +144,6 @@ int RendererMain(const MainFunctionParams& parameters) { ...@@ -126,17 +144,6 @@ int RendererMain(const MainFunctionParams& parameters) {
HandleRendererErrorTestParameters(command_line); HandleRendererErrorTestParameters(command_line);
RendererMainPlatformDelegate platform(parameters); RendererMainPlatformDelegate platform(parameters);
#if defined(OS_MACOSX)
// As long as scrollbars on Mac are painted with Cocoa, the message pump
// needs to be backed by a Foundation-level loop to process NSTimers. See
// http://crbug.com/306348#c24 for details.
std::unique_ptr<base::MessagePump> pump(new base::MessagePumpNSRunLoop());
std::unique_ptr<base::MessageLoop> main_message_loop(
new base::MessageLoop(std::move(pump)));
#else
// The main message loop of the renderer services doesn't have IO or UI tasks.
std::unique_ptr<base::MessageLoop> main_message_loop(new base::MessageLoop());
#endif
base::PlatformThread::SetName("CrRendererMain"); base::PlatformThread::SetName("CrRendererMain");
...@@ -154,9 +161,20 @@ int RendererMain(const MainFunctionParams& parameters) { ...@@ -154,9 +161,20 @@ int RendererMain(const MainFunctionParams& parameters) {
initial_virtual_time = base::Time::FromDoubleT(initial_time); initial_virtual_time = base::Time::FromDoubleT(initial_time);
} }
} }
std::unique_ptr<blink::scheduler::WebThreadScheduler> main_thread_scheduler(
std::unique_ptr<base::MessageLoop> main_message_loop;
std::unique_ptr<blink::scheduler::WebThreadScheduler> main_thread_scheduler;
if (!base::FeatureList::IsEnabled(kMainThreadUsesSequenceManager)) {
main_message_loop =
std::make_unique<base::MessageLoop>(CreateMainThreadMessagePump());
main_thread_scheduler =
blink::scheduler::WebThreadScheduler::CreateMainThreadScheduler( blink::scheduler::WebThreadScheduler::CreateMainThreadScheduler(
initial_virtual_time)); /*message_pump=*/nullptr, initial_virtual_time);
} else {
main_thread_scheduler =
blink::scheduler::WebThreadScheduler::CreateMainThreadScheduler(
CreateMainThreadMessagePump(), initial_virtual_time);
}
platform.PlatformInitialize(); platform.PlatformInitialize();
......
...@@ -6,6 +6,7 @@ include_rules = [ ...@@ -6,6 +6,7 @@ include_rules = [
"+base/memory/ptr_util.h", "+base/memory/ptr_util.h",
"+base/memory/ref_counted.h", "+base/memory/ref_counted.h",
"+base/message_loop/message_loop.h", "+base/message_loop/message_loop.h",
"+base/message_loop/message_pump.h",
"+base/optional.h", "+base/optional.h",
"+base/single_thread_task_runner.h", "+base/single_thread_task_runner.h",
"+base/threading/thread.h", "+base/threading/thread.h",
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include <memory> #include <memory>
#include "base/macros.h" #include "base/macros.h"
#include "base/message_loop/message_pump.h"
#include "base/optional.h" #include "base/optional.h"
#include "base/single_thread_task_runner.h" #include "base/single_thread_task_runner.h"
#include "base/time/time.h" #include "base/time/time.h"
...@@ -21,7 +22,7 @@ ...@@ -21,7 +22,7 @@
namespace base { namespace base {
namespace trace_event { namespace trace_event {
class BlameContext; class BlameContext;
} } // namespace trace_event
} // namespace base } // namespace base
namespace blink { namespace blink {
...@@ -31,7 +32,7 @@ class WebInputEvent; ...@@ -31,7 +32,7 @@ class WebInputEvent;
namespace viz { namespace viz {
struct BeginFrameArgs; struct BeginFrameArgs;
} } // namespace viz
namespace blink { namespace blink {
namespace scheduler { namespace scheduler {
...@@ -57,10 +58,13 @@ class BLINK_PLATFORM_EXPORT WebThreadScheduler { ...@@ -57,10 +58,13 @@ class BLINK_PLATFORM_EXPORT WebThreadScheduler {
// the main thread. They have default implementation that only does // the main thread. They have default implementation that only does
// NOTREACHED(), and are overridden only by the main thread scheduler. // NOTREACHED(), and are overridden only by the main thread scheduler.
// If |initial_virtual_time| is specified then the scheduler will be created // If |message_pump| is null caller must have registered one using
// with virtual time enabled and paused, and base::Time will be overridden to // base::MessageLoop.
// start at |initial_virtual_time|. // If |initial_virtual_time| is specified then the
// scheduler will be created with virtual time enabled and paused, and
// base::Time will be overridden to start at |initial_virtual_time|.
static std::unique_ptr<WebThreadScheduler> CreateMainThreadScheduler( static std::unique_ptr<WebThreadScheduler> CreateMainThreadScheduler(
std::unique_ptr<base::MessagePump> message_pump = nullptr,
base::Optional<base::Time> initial_virtual_time = base::nullopt); base::Optional<base::Time> initial_virtual_time = base::nullopt);
// Returns compositor thread scheduler for the compositor thread // Returns compositor thread scheduler for the compositor thread
......
...@@ -18,15 +18,20 @@ WebThreadScheduler::~WebThreadScheduler() = default; ...@@ -18,15 +18,20 @@ WebThreadScheduler::~WebThreadScheduler() = default;
// static // static
std::unique_ptr<WebThreadScheduler> std::unique_ptr<WebThreadScheduler>
WebThreadScheduler::CreateMainThreadScheduler( WebThreadScheduler::CreateMainThreadScheduler(
std::unique_ptr<base::MessagePump> message_pump,
base::Optional<base::Time> initial_virtual_time) { base::Optional<base::Time> initial_virtual_time) {
// Ensure categories appear as an option in chrome://tracing. // Ensure categories appear as an option in chrome://tracing.
WarmupTracingCategories(); WarmupTracingCategories();
// Workers might be short-lived, so placing warmup here. // Workers might be short-lived, so placing warmup here.
TRACE_EVENT_WARMUP_CATEGORY(TRACE_DISABLED_BY_DEFAULT("worker.scheduler")); TRACE_EVENT_WARMUP_CATEGORY(TRACE_DISABLED_BY_DEFAULT("worker.scheduler"));
auto sequence_manager =
message_pump
? base::sequence_manager::
CreateSequenceManagerOnCurrentThreadWithPump(
base::MessageLoop::TYPE_DEFAULT, std::move(message_pump))
: base::sequence_manager::CreateSequenceManagerOnCurrentThread();
std::unique_ptr<MainThreadSchedulerImpl> scheduler( std::unique_ptr<MainThreadSchedulerImpl> scheduler(
new MainThreadSchedulerImpl( new MainThreadSchedulerImpl(std::move(sequence_manager),
base::sequence_manager::CreateSequenceManagerOnCurrentThread(),
initial_virtual_time)); initial_virtual_time));
return std::move(scheduler); return std::move(scheduler);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment