OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "platform/scheduler/renderer/wake_up_budget_pool.h" |
| 6 |
| 7 #include <cstdint> |
| 8 |
| 9 #include "base/format_macros.h" |
| 10 #include "base/strings/stringprintf.h" |
| 11 #include "platform/scheduler/renderer/task_queue_throttler.h" |
| 12 |
| 13 namespace blink { |
| 14 namespace scheduler { |
| 15 |
| 16 namespace { |
| 17 |
| 18 std::string PointerToId(void* pointer) { |
| 19 return base::StringPrintf( |
| 20 "0x%" PRIx64, |
| 21 static_cast<uint64_t>(reinterpret_cast<uintptr_t>(pointer))); |
| 22 } |
| 23 |
| 24 } // namespace |
| 25 |
| 26 WakeUpBudgetPool::WakeUpBudgetPool(const char* name, |
| 27 BudgetPoolController* budget_pool_controller, |
| 28 base::TimeTicks now) |
| 29 : BudgetPool(name, budget_pool_controller), wakeups_per_second_(1) {} |
| 30 |
| 31 WakeUpBudgetPool::~WakeUpBudgetPool() {} |
| 32 |
| 33 QueueBlockType WakeUpBudgetPool::GetBlockType() const { |
| 34 return QueueBlockType::kNewTasksOnly; |
| 35 } |
| 36 |
| 37 void WakeUpBudgetPool::SetWakeUpRate(double wakeups_per_second) { |
| 38 wakeups_per_second_ = wakeups_per_second; |
| 39 } |
| 40 |
| 41 void WakeUpBudgetPool::SetWakeUpDuration(base::TimeDelta duration) { |
| 42 wakeup_duration_ = duration; |
| 43 } |
| 44 |
| 45 void WakeUpBudgetPool::RecordTaskRunTime(TaskQueue* queue, |
| 46 base::TimeTicks start_time, |
| 47 base::TimeTicks end_time) { |
| 48 budget_pool_controller_->UpdateQueueThrottlingState(end_time, queue); |
| 49 } |
| 50 |
| 51 base::Optional<base::TimeTicks> WakeUpBudgetPool::NextWakeUp() const { |
| 52 if (!last_wakeup_) |
| 53 return base::nullopt; |
| 54 // Subtract 1 microsecond to work with time alignment in task queue throttler. |
| 55 // This is needed due to alignment mechanism in task queue throttler -- |
| 56 // whole seconds need to be aligned to the next second to deal with immediate |
| 57 // tasks correctly. By subtracting 1 microsecond we ensure that next wakeup |
| 58 // gets aligned to a correct time. |
| 59 return last_wakeup_.value() + |
| 60 base::TimeDelta::FromSeconds(1 / wakeups_per_second_) - |
| 61 base::TimeDelta::FromMicroseconds(1); |
| 62 } |
| 63 |
| 64 bool WakeUpBudgetPool::CanRunTasksAt(base::TimeTicks now, |
| 65 bool is_wake_up) const { |
| 66 if (!last_wakeup_) |
| 67 return false; |
| 68 if (last_wakeup_ == now && is_wake_up) |
| 69 return true; |
| 70 return now < last_wakeup_.value() + wakeup_duration_; |
| 71 } |
| 72 |
| 73 bool WakeUpBudgetPool::CanRunTasksUntil(base::TimeTicks now, |
| 74 base::TimeTicks moment) const { |
| 75 if (!last_wakeup_) |
| 76 return false; |
| 77 DCHECK_LE(now, moment); |
| 78 return now < last_wakeup_.value() + wakeup_duration_ && |
| 79 moment < last_wakeup_.value() + wakeup_duration_; |
| 80 } |
| 81 |
| 82 base::TimeTicks WakeUpBudgetPool::GetNextAllowedRunTime( |
| 83 base::TimeTicks desired_run_time) const { |
| 84 if (!last_wakeup_) |
| 85 return desired_run_time; |
| 86 if (desired_run_time < last_wakeup_.value() + wakeup_duration_) |
| 87 return desired_run_time; |
| 88 return std::max(desired_run_time, NextWakeUp().value()); |
| 89 } |
| 90 |
| 91 void WakeUpBudgetPool::OnQueueNextWakeUpChanged( |
| 92 TaskQueue* queue, |
| 93 base::TimeTicks now, |
| 94 base::TimeTicks desired_run_time) { |
| 95 budget_pool_controller_->UpdateQueueThrottlingState(now, queue); |
| 96 } |
| 97 |
| 98 void WakeUpBudgetPool::OnWakeUp(base::TimeTicks now) { |
| 99 last_wakeup_ = now; |
| 100 } |
| 101 |
| 102 void WakeUpBudgetPool::AsValueInto(base::trace_event::TracedValue* state, |
| 103 base::TimeTicks now) const { |
| 104 state->BeginDictionary(name_); |
| 105 |
| 106 state->SetString("name", name_); |
| 107 state->SetDouble("wakeups_per_second_rate", wakeups_per_second_); |
| 108 state->SetDouble("wakeup_duration_in_seconds", wakeup_duration_.InSecondsF()); |
| 109 if (last_wakeup_) { |
| 110 state->SetDouble("last_wakeup_seconds_ago", |
| 111 (now - last_wakeup_.value()).InSecondsF()); |
| 112 } |
| 113 state->SetBoolean("is_enabled", is_enabled_); |
| 114 |
| 115 state->BeginArray("task_queues"); |
| 116 for (TaskQueue* queue : associated_task_queues_) { |
| 117 state->AppendString(PointerToId(queue)); |
| 118 } |
| 119 state->EndArray(); |
| 120 |
| 121 state->EndDictionary(); |
| 122 } |
| 123 |
| 124 } // namespace scheduler |
| 125 } // namespace blink |
OLD | NEW |