Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/scheduler/renderer/renderer_scheduler_impl.h" | 5 #include "components/scheduler/renderer/renderer_scheduler_impl.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/debug/stack_trace.h" | 8 #include "base/debug/stack_trace.h" |
| 9 #include "base/trace_event/trace_event.h" | 9 #include "base/trace_event/trace_event.h" |
| 10 #include "base/trace_event/trace_event_argument.h" | 10 #include "base/trace_event/trace_event_argument.h" |
| 11 #include "cc/output/begin_frame_args.h" | 11 #include "cc/output/begin_frame_args.h" |
| 12 #include "components/scheduler/child/scheduler_task_runner_delegate.h" | 12 #include "components/scheduler/child/scheduler_task_runner_delegate.h" |
| 13 #include "components/scheduler/child/task_queue_impl.h" | 13 #include "components/scheduler/child/task_queue_impl.h" |
| 14 #include "components/scheduler/child/task_queue_selector.h" | 14 #include "components/scheduler/child/task_queue_selector.h" |
| 15 | 15 |
| 16 namespace scheduler { | 16 namespace scheduler { |
| 17 namespace { | |
| 18 const int kTimerTaskEstimationSampleCount = 4 * 60; | |
| 19 const double kTimerTaskEstimationPercentile = 80; | |
| 20 } | |
| 17 | 21 |
| 18 RendererSchedulerImpl::RendererSchedulerImpl( | 22 RendererSchedulerImpl::RendererSchedulerImpl( |
| 19 scoped_refptr<SchedulerTaskRunnerDelegate> main_task_runner) | 23 scoped_refptr<SchedulerTaskRunnerDelegate> main_task_runner) |
| 20 : helper_(main_task_runner, | 24 : helper_(main_task_runner, |
| 21 "renderer.scheduler", | 25 "renderer.scheduler", |
| 22 TRACE_DISABLED_BY_DEFAULT("renderer.scheduler"), | 26 TRACE_DISABLED_BY_DEFAULT("renderer.scheduler"), |
| 23 TRACE_DISABLED_BY_DEFAULT("renderer.scheduler.debug")), | 27 TRACE_DISABLED_BY_DEFAULT("renderer.scheduler.debug")), |
| 24 idle_helper_(&helper_, | 28 idle_helper_(&helper_, |
| 25 this, | 29 this, |
| 26 "renderer.scheduler", | 30 "renderer.scheduler", |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 41 base::Bind(&RendererSchedulerImpl::UpdatePolicy, | 45 base::Bind(&RendererSchedulerImpl::UpdatePolicy, |
| 42 base::Unretained(this)), | 46 base::Unretained(this)), |
| 43 helper_.ControlTaskRunner()), | 47 helper_.ControlTaskRunner()), |
| 44 policy_may_need_update_(&any_thread_lock_), | 48 policy_may_need_update_(&any_thread_lock_), |
| 45 weak_factory_(this) { | 49 weak_factory_(this) { |
| 46 update_policy_closure_ = base::Bind(&RendererSchedulerImpl::UpdatePolicy, | 50 update_policy_closure_ = base::Bind(&RendererSchedulerImpl::UpdatePolicy, |
| 47 weak_factory_.GetWeakPtr()); | 51 weak_factory_.GetWeakPtr()); |
| 48 end_renderer_hidden_idle_period_closure_.Reset(base::Bind( | 52 end_renderer_hidden_idle_period_closure_.Reset(base::Bind( |
| 49 &RendererSchedulerImpl::EndIdlePeriod, weak_factory_.GetWeakPtr())); | 53 &RendererSchedulerImpl::EndIdlePeriod, weak_factory_.GetWeakPtr())); |
| 50 | 54 |
| 55 timer_task_runner_->AddTaskObserver( | |
| 56 &MainThreadOnly().timer_task_cost_estimator_); | |
| 57 | |
| 51 TRACE_EVENT_OBJECT_CREATED_WITH_ID( | 58 TRACE_EVENT_OBJECT_CREATED_WITH_ID( |
| 52 TRACE_DISABLED_BY_DEFAULT("renderer.scheduler"), "RendererScheduler", | 59 TRACE_DISABLED_BY_DEFAULT("renderer.scheduler"), "RendererScheduler", |
| 53 this); | 60 this); |
| 54 } | 61 } |
| 55 | 62 |
| 56 RendererSchedulerImpl::~RendererSchedulerImpl() { | 63 RendererSchedulerImpl::~RendererSchedulerImpl() { |
| 57 TRACE_EVENT_OBJECT_DELETED_WITH_ID( | 64 TRACE_EVENT_OBJECT_DELETED_WITH_ID( |
| 58 TRACE_DISABLED_BY_DEFAULT("renderer.scheduler"), "RendererScheduler", | 65 TRACE_DISABLED_BY_DEFAULT("renderer.scheduler"), "RendererScheduler", |
| 59 this); | 66 this); |
| 67 timer_task_runner_->RemoveTaskObserver( | |
| 68 &MainThreadOnly().timer_task_cost_estimator_); | |
| 60 // Ensure the renderer scheduler was shut down explicitly, because otherwise | 69 // Ensure the renderer scheduler was shut down explicitly, because otherwise |
| 61 // we could end up having stale pointers to the Blink heap which has been | 70 // we could end up having stale pointers to the Blink heap which has been |
| 62 // terminated by this point. | 71 // terminated by this point. |
| 63 DCHECK(MainThreadOnly().was_shutdown_); | 72 DCHECK(MainThreadOnly().was_shutdown_); |
| 64 } | 73 } |
| 65 | 74 |
| 66 RendererSchedulerImpl::MainThreadOnly::MainThreadOnly() | 75 RendererSchedulerImpl::MainThreadOnly::MainThreadOnly() |
| 67 : current_policy_(Policy::NORMAL), | 76 : timer_task_cost_estimator_(kTimerTaskEstimationSampleCount, |
| 77 kTimerTaskEstimationPercentile), | |
| 78 current_policy_(Policy::NORMAL), | |
| 68 timer_queue_suspend_count_(0), | 79 timer_queue_suspend_count_(0), |
| 69 renderer_hidden_(false), | 80 renderer_hidden_(false), |
| 70 was_shutdown_(false) { | 81 was_shutdown_(false) {} |
| 71 } | |
| 72 | 82 |
| 73 RendererSchedulerImpl::AnyThread::AnyThread() | 83 RendererSchedulerImpl::AnyThread::AnyThread() |
| 74 : pending_main_thread_input_event_count_(0), | 84 : pending_main_thread_input_event_count_(0), |
| 75 awaiting_touch_start_response_(false), | 85 awaiting_touch_start_response_(false), |
| 76 in_idle_period_(false), | 86 in_idle_period_(false), |
| 77 begin_main_frame_on_critical_path_(false) { | 87 begin_main_frame_on_critical_path_(false), |
| 78 } | 88 timer_tasks_seem_expensive_(false) {} |
| 79 | 89 |
| 80 RendererSchedulerImpl::CompositorThreadOnly::CompositorThreadOnly() | 90 RendererSchedulerImpl::CompositorThreadOnly::CompositorThreadOnly() |
| 81 : last_input_type_(blink::WebInputEvent::Undefined) { | 91 : last_input_type_(blink::WebInputEvent::Undefined) { |
| 82 } | 92 } |
| 83 | 93 |
| 84 RendererSchedulerImpl::CompositorThreadOnly::~CompositorThreadOnly() { | 94 RendererSchedulerImpl::CompositorThreadOnly::~CompositorThreadOnly() { |
| 85 } | 95 } |
| 86 | 96 |
| 87 void RendererSchedulerImpl::Shutdown() { | 97 void RendererSchedulerImpl::Shutdown() { |
| 88 helper_.Shutdown(); | 98 helper_.Shutdown(); |
| (...skipping 319 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 408 | 418 |
| 409 void RendererSchedulerImpl::UpdatePolicyLocked(UpdateType update_type) { | 419 void RendererSchedulerImpl::UpdatePolicyLocked(UpdateType update_type) { |
| 410 helper_.CheckOnValidThread(); | 420 helper_.CheckOnValidThread(); |
| 411 any_thread_lock_.AssertAcquired(); | 421 any_thread_lock_.AssertAcquired(); |
| 412 if (helper_.IsShutdown()) | 422 if (helper_.IsShutdown()) |
| 413 return; | 423 return; |
| 414 | 424 |
| 415 base::TimeTicks now = helper_.Now(); | 425 base::TimeTicks now = helper_.Now(); |
| 416 policy_may_need_update_.SetWhileLocked(false); | 426 policy_may_need_update_.SetWhileLocked(false); |
| 417 | 427 |
| 428 AnyThread().timer_tasks_seem_expensive_ = | |
| 429 MainThreadOnly().timer_task_cost_estimator_.expected_task_duration() > | |
| 430 base::TimeDelta::FromMilliseconds(16); | |
|
alex clarke (OOO till 29th)
2015/08/21 17:13:30
I wonder if it makes more sense to compute how muc
Sami
2015/08/21 17:42:22
Good point, I've now implemented this in terms of
| |
| 431 | |
| 418 base::TimeDelta new_policy_duration; | 432 base::TimeDelta new_policy_duration; |
| 419 Policy new_policy = ComputeNewPolicy(now, &new_policy_duration); | 433 Policy new_policy = ComputeNewPolicy(now, &new_policy_duration); |
| 420 if (new_policy_duration > base::TimeDelta()) { | 434 if (new_policy_duration > base::TimeDelta()) { |
| 421 MainThreadOnly().current_policy_expiration_time_ = | 435 MainThreadOnly().current_policy_expiration_time_ = |
| 422 now + new_policy_duration; | 436 now + new_policy_duration; |
| 423 delayed_update_policy_runner_.SetDeadline(FROM_HERE, new_policy_duration, | 437 delayed_update_policy_runner_.SetDeadline(FROM_HERE, new_policy_duration, |
| 424 now); | 438 now); |
| 425 } else { | 439 } else { |
| 426 MainThreadOnly().current_policy_expiration_time_ = base::TimeTicks(); | 440 MainThreadOnly().current_policy_expiration_time_ = base::TimeTicks(); |
| 427 } | 441 } |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 501 RendererSchedulerImpl::Policy RendererSchedulerImpl::ComputeNewPolicy( | 515 RendererSchedulerImpl::Policy RendererSchedulerImpl::ComputeNewPolicy( |
| 502 base::TimeTicks now, | 516 base::TimeTicks now, |
| 503 base::TimeDelta* new_policy_duration) const { | 517 base::TimeDelta* new_policy_duration) const { |
| 504 any_thread_lock_.AssertAcquired(); | 518 any_thread_lock_.AssertAcquired(); |
| 505 // Above all else we want to be responsive to user input. | 519 // Above all else we want to be responsive to user input. |
| 506 *new_policy_duration = TimeLeftInInputEscalatedPolicy(now); | 520 *new_policy_duration = TimeLeftInInputEscalatedPolicy(now); |
| 507 if (*new_policy_duration > base::TimeDelta()) { | 521 if (*new_policy_duration > base::TimeDelta()) { |
| 508 if (AnyThread().awaiting_touch_start_response_) | 522 if (AnyThread().awaiting_touch_start_response_) |
| 509 return Policy::TOUCHSTART_PRIORITY; | 523 return Policy::TOUCHSTART_PRIORITY; |
| 510 // If BeginMainFrame is on the critical path, we want to try and prevent | 524 // If BeginMainFrame is on the critical path, we want to try and prevent |
| 511 // timers and loading tasks from running shortly before BeginMainFrame is | 525 // timers and loading tasks from running if we think they might be |
| 512 // due to be posted from the compositor, because they can delay | 526 // expensive. |
| 513 // BeginMainFrame's execution. We do this by limiting execution of timers to | 527 if (AnyThread().timer_tasks_seem_expensive_ && |
| 514 // idle periods, provided there has been at least one idle period recently. | 528 AnyThread().begin_main_frame_on_critical_path_) { |
| 515 // | |
| 516 // TODO(alexclarke): It's a shame in_idle_period_, | |
| 517 // begin_main_frame_on_critical_path_ and last_idle_period_end_time_ are in | |
| 518 // the AnyThread struct. Find a way to migrate them to MainThreadOnly. | |
| 519 if (!AnyThread().in_idle_period_ && | |
|
alex clarke (OOO till 29th)
2015/08/21 17:13:30
Can you add a todo to remove in_idle_period_ and
Sami
2015/08/21 17:42:22
Done.
| |
| 520 AnyThread().begin_main_frame_on_critical_path_ && | |
| 521 HadAnIdlePeriodRecently(now)) { | |
| 522 return Policy::COMPOSITOR_CRITICAL_PATH_PRIORITY; | 529 return Policy::COMPOSITOR_CRITICAL_PATH_PRIORITY; |
| 523 } | 530 } |
| 524 return Policy::COMPOSITOR_PRIORITY; | 531 return Policy::COMPOSITOR_PRIORITY; |
| 525 } | 532 } |
| 526 | 533 |
| 527 if (AnyThread().rails_loading_priority_deadline_ > now) { | 534 if (AnyThread().rails_loading_priority_deadline_ > now) { |
| 528 *new_policy_duration = AnyThread().rails_loading_priority_deadline_ - now; | 535 *new_policy_duration = AnyThread().rails_loading_priority_deadline_ - now; |
| 529 return Policy::LOADING_PRIORITY; | 536 return Policy::LOADING_PRIORITY; |
| 530 } | 537 } |
| 531 | 538 |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 633 base::TimeTicks()).InMillisecondsF()); | 640 base::TimeTicks()).InMillisecondsF()); |
| 634 state->SetDouble("last_idle_period_end_time", | 641 state->SetDouble("last_idle_period_end_time", |
| 635 (AnyThread().last_idle_period_end_time_ - base::TimeTicks()) | 642 (AnyThread().last_idle_period_end_time_ - base::TimeTicks()) |
| 636 .InMillisecondsF()); | 643 .InMillisecondsF()); |
| 637 state->SetInteger("pending_main_thread_input_event_count", | 644 state->SetInteger("pending_main_thread_input_event_count", |
| 638 AnyThread().pending_main_thread_input_event_count_); | 645 AnyThread().pending_main_thread_input_event_count_); |
| 639 state->SetBoolean("awaiting_touch_start_response", | 646 state->SetBoolean("awaiting_touch_start_response", |
| 640 AnyThread().awaiting_touch_start_response_); | 647 AnyThread().awaiting_touch_start_response_); |
| 641 state->SetBoolean("begin_main_frame_on_critical_path", | 648 state->SetBoolean("begin_main_frame_on_critical_path", |
| 642 AnyThread().begin_main_frame_on_critical_path_); | 649 AnyThread().begin_main_frame_on_critical_path_); |
| 650 state->SetDouble("expected_timer_task_duration", | |
| 651 MainThreadOnly() | |
| 652 .timer_task_cost_estimator_.expected_task_duration() | |
| 653 .InMillisecondsF()); | |
| 654 state->SetBoolean("timer_tasks_seem_expensive_", | |
|
alex clarke (OOO till 29th)
2015/08/21 17:13:30
nit: remove trailing _
Sami
2015/08/21 17:42:22
Well spotted, thanks.
| |
| 655 AnyThread().timer_tasks_seem_expensive_); | |
| 643 state->SetDouble("estimated_next_frame_begin", | 656 state->SetDouble("estimated_next_frame_begin", |
| 644 (MainThreadOnly().estimated_next_frame_begin_ - | 657 (MainThreadOnly().estimated_next_frame_begin_ - |
| 645 base::TimeTicks()).InMillisecondsF()); | 658 base::TimeTicks()).InMillisecondsF()); |
| 646 state->SetBoolean("in_idle_period", AnyThread().in_idle_period_); | 659 state->SetBoolean("in_idle_period", AnyThread().in_idle_period_); |
| 647 | 660 |
| 648 return state; | 661 return state; |
| 649 } | 662 } |
| 650 | 663 |
| 651 void RendererSchedulerImpl::OnIdlePeriodStarted() { | 664 void RendererSchedulerImpl::OnIdlePeriodStarted() { |
| 652 base::AutoLock lock(any_thread_lock_); | 665 base::AutoLock lock(any_thread_lock_); |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 669 UpdatePolicyLocked(UpdateType::MAY_EARLY_OUT_IF_POLICY_UNCHANGED); | 682 UpdatePolicyLocked(UpdateType::MAY_EARLY_OUT_IF_POLICY_UNCHANGED); |
| 670 } | 683 } |
| 671 | 684 |
| 672 bool RendererSchedulerImpl::HadAnIdlePeriodRecently(base::TimeTicks now) const { | 685 bool RendererSchedulerImpl::HadAnIdlePeriodRecently(base::TimeTicks now) const { |
| 673 return (now - AnyThread().last_idle_period_end_time_) <= | 686 return (now - AnyThread().last_idle_period_end_time_) <= |
| 674 base::TimeDelta::FromMilliseconds( | 687 base::TimeDelta::FromMilliseconds( |
| 675 kIdlePeriodStarvationThresholdMillis); | 688 kIdlePeriodStarvationThresholdMillis); |
| 676 } | 689 } |
| 677 | 690 |
| 678 } // namespace scheduler | 691 } // namespace scheduler |
| OLD | NEW |