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_) | |
Sami
2017/05/03 16:53:05
Is it fair to say the policy is that the current w
altimin
2017/05/04 10:50:06
Added a comment.
| |
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 base::TimeTicks WakeUpBudgetPool::GetNextAllowedRunTime( | |
74 base::TimeTicks desired_run_time) const { | |
75 if (!last_wakeup_) | |
76 return desired_run_time; | |
77 if (desired_run_time < last_wakeup_.value() + wakeup_duration_) | |
78 return desired_run_time; | |
79 return std::max(desired_run_time, NextWakeUp().value()); | |
80 } | |
81 | |
82 void WakeUpBudgetPool::OnQueueNextWakeUpChanged( | |
83 TaskQueue* queue, | |
84 base::TimeTicks now, | |
85 base::TimeTicks desired_run_time) { | |
86 budget_pool_controller_->UpdateQueueThrottlingState(now, queue); | |
87 } | |
88 | |
89 void WakeUpBudgetPool::OnWakeUp(base::TimeTicks now) { | |
90 last_wakeup_ = now; | |
91 } | |
92 | |
93 void WakeUpBudgetPool::AsValueInto(base::trace_event::TracedValue* state, | |
94 base::TimeTicks now) const { | |
95 state->BeginDictionary(name_); | |
96 | |
97 state->SetString("name", name_); | |
98 state->SetDouble("wakeups_per_second_rate", wakeups_per_second_); | |
99 state->SetDouble("wakeup_duration_in_seconds", wakeup_duration_.InSecondsF()); | |
100 if (last_wakeup_) { | |
101 state->SetDouble("last_wakeup_seconds_ago", | |
102 (now - last_wakeup_.value()).InSecondsF()); | |
103 } | |
104 state->SetBoolean("is_enabled", is_enabled_); | |
105 | |
106 state->BeginArray("task_queues"); | |
107 for (TaskQueue* queue : associated_task_queues_) { | |
108 state->AppendString(PointerToId(queue)); | |
109 } | |
110 state->EndArray(); | |
111 | |
112 state->EndDictionary(); | |
113 } | |
114 | |
115 } // namespace scheduler | |
116 } // namespace blink | |
OLD | NEW |