Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2438)

Unified Diff: content/renderer/scheduler/renderer_scheduler_impl.cc

Issue 968073003: [content]: Add support for long idle times in the Blink Scheduler. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@long_idle_4
Patch Set: Review comments Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: content/renderer/scheduler/renderer_scheduler_impl.cc
diff --git a/content/renderer/scheduler/renderer_scheduler_impl.cc b/content/renderer/scheduler/renderer_scheduler_impl.cc
index 6f7821690393a3b8c61b9956f28deb523e766a6b..17ce03f36a3c25371b733d5a2a0729fd74f0320f 100644
--- a/content/renderer/scheduler/renderer_scheduler_impl.cc
+++ b/content/renderer/scheduler/renderer_scheduler_impl.cc
@@ -41,6 +41,13 @@ RendererSchedulerImpl::RendererSchedulerImpl(
weak_renderer_scheduler_ptr_);
end_idle_period_closure_.Reset(base::Bind(
&RendererSchedulerImpl::EndIdlePeriod, weak_renderer_scheduler_ptr_));
+ initiate_next_long_idle_period_closure_.Reset(base::Bind(
+ &RendererSchedulerImpl::InitiateLongIdlePeriod,
+ weak_renderer_scheduler_ptr_));
+ after_wakeup_initiate_next_long_idle_period_closure_.Reset(base::Bind(
+ &RendererSchedulerImpl::InitiateLongIdlePeriodAfterWakeup,
+ weak_renderer_scheduler_ptr_));
+
idle_task_runner_ = make_scoped_refptr(new SingleThreadIdleTaskRunner(
task_queue_manager_->TaskRunnerForQueue(IDLE_TASK_QUEUE),
control_task_after_wakeup_runner_,
@@ -142,10 +149,14 @@ void RendererSchedulerImpl::BeginFrameNotExpectedSoon() {
TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("renderer.scheduler"),
"RendererSchedulerImpl::BeginFrameNotExpectedSoon");
DCHECK(main_thread_checker_.CalledOnValidThread());
+ if (!task_queue_manager_)
+ return;
+
// TODO(skyostil): Wire up real notification of input events processing
// instead of this approximation.
DidProcessInputEvent(base::TimeTicks());
- // TODO(rmcilroy): Implement long idle times.
+
+ InitiateLongIdlePeriod();
}
void RendererSchedulerImpl::DidReceiveInputEventOnCompositorThread(
@@ -285,8 +296,12 @@ void RendererSchedulerImpl::UpdatePolicy() {
base::TimeDelta new_policy_duration;
Policy new_policy = ComputeNewPolicy(&new_policy_duration);
- if (new_policy_duration > base::TimeDelta())
+ if (new_policy_duration > base::TimeDelta()) {
+ current_policy_expiration_time_ = now + new_policy_duration;
PostUpdatePolicyOnControlRunner(new_policy_duration);
+ } else {
+ current_policy_expiration_time_ = base::TimeTicks();
+ }
if (new_policy == current_policy_)
return;
@@ -336,39 +351,137 @@ RendererSchedulerImpl::Policy RendererSchedulerImpl::ComputeNewPolicy(
if (input_stream_state_ == InputStreamState::INACTIVE)
return new_policy;
- base::TimeDelta new_priority_duration =
- base::TimeDelta::FromMilliseconds(kPriorityEscalationAfterInputMillis);
Policy input_priority_policy =
input_stream_state_ ==
InputStreamState::ACTIVE_AND_AWAITING_TOUCHSTART_RESPONSE
? Policy::TOUCHSTART_PRIORITY
: Policy::COMPOSITOR_PRIORITY;
+ base::TimeDelta time_left_in_policy = TimeLeftInInputEscalatedPolicy();
+ if (time_left_in_policy > base::TimeDelta()) {
+ new_policy = input_priority_policy;
+ *new_policy_duration = time_left_in_policy;
+ } else {
+ // Reset |input_stream_state_| to ensure
+ // DidReceiveInputEventOnCompositorThread will post an UpdatePolicy task
+ // when it's next called.
+ input_stream_state_ = InputStreamState::INACTIVE;
+ }
+ return new_policy;
+}
+
+base::TimeDelta RendererSchedulerImpl::TimeLeftInInputEscalatedPolicy() const {
+ DCHECK(main_thread_checker_.CalledOnValidThread());
+ // TODO(rmcilroy): Change this to DCHECK_EQ when crbug.com/463869 is fixed.
+ DCHECK(input_stream_state_ != InputStreamState::INACTIVE);
+ incoming_signals_lock_.AssertAcquired();
- // If the input event is still pending, go into input prioritized policy
- // and check again later.
+ base::TimeDelta escalated_priority_duration =
+ base::TimeDelta::FromMilliseconds(kPriorityEscalationAfterInputMillis);
+ base::TimeDelta time_left_in_policy;
if (last_input_process_time_on_main_.is_null() &&
!task_queue_manager_->IsQueueEmpty(COMPOSITOR_TASK_QUEUE)) {
- new_policy = input_priority_policy;
- *new_policy_duration = new_priority_duration;
+ // If the input event is still pending, go into input prioritized policy
+ // and check again later.
+ time_left_in_policy = escalated_priority_duration;
} else {
// Otherwise make sure the input prioritization policy ends on time.
base::TimeTicks new_priority_end(
std::max(last_input_receipt_time_on_compositor_,
last_input_process_time_on_main_) +
- new_priority_duration);
- base::TimeDelta time_left_in_policy = new_priority_end - Now();
-
- if (time_left_in_policy > base::TimeDelta()) {
- new_policy = input_priority_policy;
- *new_policy_duration = time_left_in_policy;
- } else {
- // Reset |input_stream_state_| to ensure
- // DidReceiveInputEventOnCompositorThread will post an UpdatePolicy task
- // when it's next called.
- input_stream_state_ = InputStreamState::INACTIVE;
- }
+ escalated_priority_duration);
+ time_left_in_policy = new_priority_end - Now();
}
- return new_policy;
+ return time_left_in_policy;
+}
+
+bool RendererSchedulerImpl::ShouldStartLongIdlePeriod(
+ const base::TimeTicks now,
+ base::TimeDelta* next_long_idle_period_delay_out) {
+ DCHECK(main_thread_checker_.CalledOnValidThread());
+
+ MaybeUpdatePolicy();
+ if (SchedulerPolicy() == Policy::TOUCHSTART_PRIORITY) {
+ // Don't start a long idle task in touch start priority, try again when
+ // the policy is scheduled to end.
+ *next_long_idle_period_delay_out = current_policy_expiration_time_ - now;
+ return false;
+ }
+
+ base::TimeTicks next_pending_delayed_task =
+ task_queue_manager_->NextPendingDelayedTaskRunTime();
+
+ base::TimeDelta long_idle_period_duration =
+ base::TimeDelta::FromMilliseconds(kMaximumIdlePeriodMillis);
+ if (!next_pending_delayed_task.is_null()) {
+ // Limit the idle period duration to be before the next pending task.
+ long_idle_period_duration = std::min(
+ next_pending_delayed_task - now, long_idle_period_duration);
+ }
+
+ if (long_idle_period_duration > base::TimeDelta()) {
+ *next_long_idle_period_delay_out = long_idle_period_duration;
+ return true;
+ } else {
+ // If we can't start the idle period yet then try again after wakeup.
+ *next_long_idle_period_delay_out = base::TimeDelta::FromMilliseconds(
+ kRetryInitiateLongIdlePeriodDelayMillis);
+ return false;
+ }
+}
+
+void RendererSchedulerImpl::InitiateLongIdlePeriod() {
+ TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("renderer.scheduler"),
+ "InitiateLongIdlePeriod");
+ DCHECK(main_thread_checker_.CalledOnValidThread());
+
+ // End any previous idle period.
+ EndIdlePeriod();
+
+ base::TimeTicks now(Now());
+ base::TimeDelta next_long_idle_period_delay;
+ if (ShouldStartLongIdlePeriod(now, &next_long_idle_period_delay)) {
+ estimated_next_frame_begin_ = now + next_long_idle_period_delay;
+ StartIdlePeriod();
+ }
+
+ if (task_queue_manager_->IsQueueEmpty(IDLE_TASK_QUEUE)) {
+ // If there are no current idle tasks or we need to wait for a pending
+ // delayed task to run then post the call to initiate the next idle for
+ // execution after wakeup (at which point after-wakeup idle tasks might
+ // be eligible to run or more idle tasks posted).
+ control_task_after_wakeup_runner_->PostDelayedTask(
+ FROM_HERE,
+ after_wakeup_initiate_next_long_idle_period_closure_.callback(),
+ next_long_idle_period_delay);
+ } else {
+ // Otherwise post on the normal control task queue.
+ control_task_runner_->PostDelayedTask(
+ FROM_HERE,
+ initiate_next_long_idle_period_closure_.callback(),
+ next_long_idle_period_delay);
+ }
+}
+
+void RendererSchedulerImpl::InitiateLongIdlePeriodAfterWakeup() {
+ TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("renderer.scheduler"),
+ "AfterWakeupInitiateLongIdlePeriod");
+ DCHECK(main_thread_checker_.CalledOnValidThread());
+
+ if (in_idle_period_) {
+ // Since we were asleep until now, end the async idle period trace event at
+ // the time when it would have ended were we awake.
+ TRACE_EVENT_ASYNC_END_WITH_TIMESTAMP0(
+ "renderer.scheduler", "RendererSchedulerIdlePeriod", this,
+ std::min(estimated_next_frame_begin_, Now()).ToInternalValue());
+ }
Sami 2015/03/04 18:26:40 Forgot to reset in_idle_period_? Also in EndIdlePe
Sami 2015/03/05 12:01:51 Missed this comment?
rmcilroy 2015/03/05 12:29:41 opps, yes thanks. Done for EndIdlePeriod but I do
+
+ // Post a task to initiate the next long idle period rather than calling it
+ // directly to allow all pending PostIdleTaskAfterWakeup tasks to get enqueued
+ // on the idle task queue before the next idle period starts so they are
+ // eligible to be run during the new idle period.
+ control_task_runner_->PostTask(
+ FROM_HERE,
+ initiate_next_long_idle_period_closure_.callback());
}
void RendererSchedulerImpl::StartIdlePeriod() {
@@ -378,9 +491,20 @@ void RendererSchedulerImpl::StartIdlePeriod() {
renderer_task_queue_selector_->EnableQueue(
IDLE_TASK_QUEUE, RendererTaskQueueSelector::BEST_EFFORT_PRIORITY);
task_queue_manager_->PumpQueue(IDLE_TASK_QUEUE);
+ in_idle_period_ = true;
}
void RendererSchedulerImpl::EndIdlePeriod() {
+ DCHECK(main_thread_checker_.CalledOnValidThread());
+
+ end_idle_period_closure_.Cancel();
+ initiate_next_long_idle_period_closure_.Cancel();
+ after_wakeup_initiate_next_long_idle_period_closure_.Cancel();
+
+ if (!in_idle_period_)
+ return;
+
+ renderer_task_queue_selector_->DisableQueue(IDLE_TASK_QUEUE);
bool is_tracing;
TRACE_EVENT_CATEGORY_GROUP_ENABLED("renderer.scheduler", &is_tracing);
if (is_tracing && !estimated_next_frame_begin_.is_null() &&
@@ -394,9 +518,6 @@ void RendererSchedulerImpl::EndIdlePeriod() {
}
TRACE_EVENT_ASYNC_END0("renderer.scheduler",
"RendererSchedulerIdlePeriod", this);
- DCHECK(main_thread_checker_.CalledOnValidThread());
- end_idle_period_closure_.Cancel();
- renderer_task_queue_selector_->DisableQueue(IDLE_TASK_QUEUE);
}
void RendererSchedulerImpl::SetTimeSourceForTesting(

Powered by Google App Engine
This is Rietveld 408576698