Commit 40c17227 authored by Greg Kraynov's avatar Greg Kraynov Committed by Commit Bot

Add SetTimerSlack() method to SequenceManager.

We're moving away from using MessageLoop in Blink, hence,
we want to make SequenceManager support any missing pieces
and provide a good level of abstraction on top of a thread.

Specifically, this CL will unblock removing MessageLoop
references from content::RenderThreadImpl.

Bug: 828835
Change-Id: I5a785a26f35839eea3705ecb5abc61672c497457
Reviewed-on: https://chromium-review.googlesource.com/1167516Reviewed-by: default avatarAlexander Timin <altimin@chromium.org>
Reviewed-by: default avatarKentaro Hara <haraken@chromium.org>
Commit-Queue: Greg Kraynov <kraynov@chromium.org>
Cr-Commit-Position: refs/heads/master@{#581876}
parent e465430d
......@@ -9,6 +9,7 @@
#include <utility>
#include "base/message_loop/message_loop.h"
#include "base/message_loop/timer_slack.h"
#include "base/single_thread_task_runner.h"
#include "base/task/sequence_manager/task_queue_impl.h"
#include "base/task/sequence_manager/task_time_observer.h"
......@@ -113,6 +114,10 @@ class SequenceManager {
// logic at the cost of a potentially worse latency. 1 by default.
virtual void SetWorkBatchSize(int work_batch_size) = 0;
// Requests desired timer precision from the OS.
// Has no effect on some platforms.
virtual void SetTimerSlack(TimerSlack timer_slack) = 0;
// Enables crash keys that can be set in the scope of a task which help
// to identify the culprit if upcoming work results in a crash.
// Key names must be thread-specific to avoid races and corrupted crash dumps.
......
......@@ -582,6 +582,11 @@ void SequenceManagerImpl::SetWorkBatchSize(int work_batch_size) {
controller_->SetWorkBatchSize(work_batch_size);
}
void SequenceManagerImpl::SetTimerSlack(TimerSlack timer_slack) {
DCHECK_CALLED_ON_VALID_THREAD(associated_thread_->thread_checker);
controller_->SetTimerSlack(timer_slack);
}
void SequenceManagerImpl::AddTaskObserver(
MessageLoop::TaskObserver* task_observer) {
DCHECK_CALLED_ON_VALID_THREAD(associated_thread_->thread_checker);
......
......@@ -114,6 +114,7 @@ class BASE_EXPORT SequenceManagerImpl
void SweepCanceledDelayedTasks() override;
bool GetAndClearSystemIsQuiescentBit() override;
void SetWorkBatchSize(int work_batch_size) override;
void SetTimerSlack(TimerSlack timer_slack) override;
void EnableCrashKeys(const char* file_name_crash_key,
const char* function_name_crash_key) override;
const MetricRecordingSettings& GetMetricRecordingSettings() const override;
......
......@@ -5,6 +5,7 @@
#ifndef BASE_TASK_SEQUENCE_MANAGER_THREAD_CONTROLLER_H_
#define BASE_TASK_SEQUENCE_MANAGER_THREAD_CONTROLLER_H_
#include "base/message_loop/timer_slack.h"
#include "base/run_loop.h"
#include "base/single_thread_task_runner.h"
#include "base/task/sequence_manager/lazy_now.h"
......@@ -62,6 +63,10 @@ class ThreadController {
// Must be called before the first call to Schedule*Work().
virtual void SetSequencedTaskSource(SequencedTaskSource*) = 0;
// Requests desired timer precision from the OS.
// Has no effect on some platforms.
virtual void SetTimerSlack(TimerSlack timer_slack) = 0;
// TODO(altimin): Get rid of the methods below.
// These methods exist due to current integration of SequenceManager
// with MessageLoop.
......
......@@ -62,6 +62,12 @@ void ThreadControllerImpl::SetSequencedTaskSource(
sequence_ = sequence;
}
void ThreadControllerImpl::SetTimerSlack(TimerSlack timer_slack) {
if (!message_loop_)
return;
message_loop_->SetTimerSlack(timer_slack);
}
void ThreadControllerImpl::ScheduleWork() {
DCHECK(sequence_);
AutoLock lock(any_sequence_lock_);
......
......@@ -42,6 +42,7 @@ class BASE_EXPORT ThreadControllerImpl : public ThreadController,
void ScheduleWork() override;
void SetNextDelayedDoWork(LazyNow* lazy_now, TimeTicks run_time) override;
void SetSequencedTaskSource(SequencedTaskSource* sequence) override;
void SetTimerSlack(TimerSlack timer_slack) override;
bool RunsTasksInCurrentSequence() override;
const TickClock* GetClock() override;
void SetDefaultTaskRunner(scoped_refptr<SingleThreadTaskRunner>) override;
......
......@@ -49,6 +49,11 @@ void ThreadControllerWithMessagePumpImpl::SetWorkBatchSize(
main_thread_only().batch_size = work_batch_size;
}
void ThreadControllerWithMessagePumpImpl::SetTimerSlack(
TimerSlack timer_slack) {
pump_->SetTimerSlack(timer_slack);
}
void ThreadControllerWithMessagePumpImpl::WillQueueTask(
PendingTask* pending_task) {
task_annotator_.WillQueueTask("ThreadController::Task", pending_task);
......
......@@ -38,6 +38,7 @@ class BASE_EXPORT ThreadControllerWithMessagePumpImpl
void WillQueueTask(PendingTask* pending_task) override;
void ScheduleWork() override;
void SetNextDelayedDoWork(LazyNow* lazy_now, TimeTicks run_time) override;
void SetTimerSlack(TimerSlack timer_slack) override;
const TickClock* GetClock() override;
bool RunsTasksInCurrentSequence() override;
void SetDefaultTaskRunner(
......
......@@ -1717,12 +1717,6 @@ void RenderThreadImpl::SetSchedulerKeepActive(bool keep_active) {
}
void RenderThreadImpl::SetProcessBackgrounded(bool backgrounded) {
// Set timer slack to maximum on main thread when in background.
base::TimerSlack timer_slack = base::TIMER_SLACK_NONE;
if (backgrounded)
timer_slack = base::TIMER_SLACK_MAXIMUM;
main_message_loop_->SetTimerSlack(timer_slack);
main_thread_scheduler_->SetRendererBackgrounded(backgrounded);
if (backgrounded) {
needs_to_record_first_active_paint_ = false;
......
......@@ -147,6 +147,10 @@ base::TimeTicks SchedulerHelper::NowTicks() const {
return base::TimeTicks::Now();
}
void SchedulerHelper::SetTimerSlack(base::TimerSlack timer_slack) {
sequence_manager_->SetTimerSlack(timer_slack);
}
double SchedulerHelper::GetSamplingRateForRecordingCPUTime() const {
if (sequence_manager_) {
return sequence_manager_->GetMetricRecordingSettings()
......
......@@ -33,6 +33,7 @@ class PLATFORM_EXPORT SchedulerHelper
const base::TickClock* GetClock() const;
base::TimeTicks NowTicks() const;
void SetTimerSlack(base::TimerSlack timer_slack);
// Returns the default task queue.
virtual scoped_refptr<base::sequence_manager::TaskQueue>
......
......@@ -958,6 +958,13 @@ void MainThreadSchedulerImpl::SetRendererHidden(bool hidden) {
void MainThreadSchedulerImpl::SetRendererBackgrounded(bool backgrounded) {
helper_.CheckOnValidThread();
// Increasing timer slack helps the OS to coalesce timers efficiently.
base::TimerSlack timer_slack = base::TIMER_SLACK_NONE;
if (backgrounded)
timer_slack = base::TIMER_SLACK_MAXIMUM;
helper_.SetTimerSlack(timer_slack);
if (helper_.IsShutdown() ||
main_thread_only().renderer_backgrounded == backgrounded)
return;
......
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