Chromium Code Reviews| 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 #ifndef THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_RENDERER_BUDGET_POOL_H_ | |
| 6 #define THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_RENDERER_BUDGET_POOL_H_ | |
| 7 | |
| 8 #include <unordered_set> | |
| 9 | |
| 10 #include "base/callback.h" | |
| 11 #include "base/gtest_prod_util.h" | |
| 12 #include "base/macros.h" | |
| 13 #include "base/optional.h" | |
| 14 #include "base/time/time.h" | |
| 15 #include "platform/scheduler/base/lazy_now.h" | |
| 16 | |
| 17 namespace base { | |
| 18 namespace trace_event { | |
| 19 class TracedValue; | |
| 20 } | |
| 21 } | |
| 22 | |
| 23 namespace blink { | |
| 24 namespace scheduler { | |
| 25 | |
| 26 class TaskQueue; | |
| 27 class TaskQueueThrottler; | |
| 28 | |
| 29 // BudgetPool represents a group of task queues which share a limit | |
| 30 // on a resource. This limit applies when task queues are already throttled | |
|
alex clarke (OOO till 29th)
2017/03/09 12:33:05
Maybe mention that the resource is measured in tim
| |
| 31 // by TaskQueueThrottler. | |
| 32 class BudgetPool { | |
| 33 public: | |
| 34 ~BudgetPool(); | |
|
Sami
2017/03/08 16:18:01
virtual
altimin
2017/03/08 16:50:33
Shame. Shame. Shame.
| |
| 35 | |
| 36 virtual const char* Name() const = 0; | |
| 37 | |
| 38 // Adds |queue| to given pool. If the pool restriction does not allow | |
| 39 // a task to be run immediately and |queue| is throttled, |queue| becomes | |
| 40 // disabled. | |
| 41 virtual void AddQueue(base::TimeTicks now, TaskQueue* queue) = 0; | |
| 42 | |
| 43 // Removes |queue| from given pool. If it is throttled, it does not | |
| 44 // become enabled immediately, but a call to |PumpThrottledTasks| | |
| 45 // is scheduled. | |
| 46 virtual void RemoveQueue(base::TimeTicks now, TaskQueue* queue) = 0; | |
| 47 | |
| 48 // Enables this time budget pool. Queues from this pool will be | |
| 49 // throttled based on their run time. | |
| 50 virtual void EnableThrottling(LazyNow* now) = 0; | |
| 51 | |
| 52 // Disables with time budget pool. Queues from this pool will not be | |
| 53 // throttled based on their run time. A call to |PumpThrottledTasks| | |
| 54 // will be scheduled to enable this queues back again and respect | |
| 55 // timer alignment. Internal budget level will not regenerate with time. | |
| 56 virtual void DisableThrottling(LazyNow* now) = 0; | |
| 57 | |
| 58 virtual bool IsThrottlingEnabled() const = 0; | |
| 59 | |
| 60 // Report task run time to the budget pool. | |
| 61 virtual void RecordTaskRunTime(base::TimeTicks start_time, | |
| 62 base::TimeTicks end_time) = 0; | |
| 63 | |
| 64 // All queues should be removed before calling Close(). | |
| 65 virtual void Close() = 0; | |
| 66 | |
| 67 // Returns true at a task can be run immediately at the given time. | |
| 68 virtual bool HasEnoughBudgetToRun(base::TimeTicks now) = 0; | |
|
alex clarke (OOO till 29th)
2017/03/09 12:33:05
Does this need to be virtual? Could it be:
bool H
altimin
2017/03/09 13:36:02
At this moment HasEnoughBudgetToRun updates last k
| |
| 69 | |
| 70 // Retuns earliest time (can be in the past) when the next task can run. | |
| 71 virtual base::TimeTicks GetNextAllowedRunTime() = 0; | |
| 72 | |
| 73 // Returns state for tracing. | |
| 74 virtual void AsValueInto(base::trace_event::TracedValue* state, | |
| 75 base::TimeTicks now) const = 0; | |
|
Sami
2017/03/08 16:18:01
Is this formatted right?
altimin
2017/03/08 16:50:32
Done.
alex clarke (OOO till 29th)
2017/03/09 12:33:05
nit: indent looks odd.
| |
| 76 }; | |
| 77 | |
| 78 // TimeBudgetPool represents a collection of task queues which share a limit | |
| 79 // on total cpu time. | |
| 80 class TimeBudgetPool : public BudgetPool { | |
|
alex clarke (OOO till 29th)
2017/03/09 12:33:05
bike-shed: CpuTimeBudgetPool? I was wondering why
altimin
2017/03/09 13:36:02
Done.
| |
| 81 public: | |
| 82 ~TimeBudgetPool(); | |
| 83 | |
| 84 // Throttle task queues from this time budget pool if tasks are running | |
| 85 // for more than |cpu_percentage| per cent of wall time. | |
| 86 // This function does not affect internal time budget level. | |
| 87 void SetTimeBudgetRecoveryRate(base::TimeTicks now, double cpu_percentage); | |
| 88 | |
| 89 | |
| 90 // Increase budget level by given value. This function DOES NOT unblock | |
| 91 // queues even if they are allowed to run with increased budget level. | |
| 92 void GrantAdditionalBudget(base::TimeTicks now, | |
| 93 base::TimeDelta budget_level); | |
| 94 | |
| 95 // Set callback which will be called every time when this budget pool | |
| 96 // is throttled. Throttling duration (time until the queue is allowed | |
| 97 // to run again) is passed as a parameter to callback. | |
| 98 void SetReportingCallback( | |
| 99 base::Callback<void(base::TimeDelta)> reporting_callback); | |
| 100 | |
| 101 // BudgetPool implementation: | |
| 102 const char* Name() const override; | |
| 103 void AddQueue(base::TimeTicks now, TaskQueue* queue) override; | |
| 104 void RemoveQueue(base::TimeTicks now, TaskQueue* queue) override; | |
| 105 void EnableThrottling(LazyNow* now) override; | |
| 106 void DisableThrottling(LazyNow* now) override; | |
| 107 bool IsThrottlingEnabled() const override; | |
| 108 void RecordTaskRunTime(base::TimeTicks start_time, | |
| 109 base::TimeTicks end_time) override; | |
| 110 void Close() override; | |
| 111 bool HasEnoughBudgetToRun(base::TimeTicks now) override; | |
| 112 base::TimeTicks GetNextAllowedRunTime() override; | |
| 113 void AsValueInto(base::trace_event::TracedValue* state, base::TimeTicks now) c onst override; | |
|
Sami
2017/03/08 16:18:01
Line too long?
altimin
2017/03/08 16:50:32
Done.
| |
| 114 | |
| 115 private: | |
| 116 friend class TaskQueueThrottler; | |
| 117 | |
| 118 FRIEND_TEST_ALL_PREFIXES(TaskQueueThrottlerTest, TimeBudgetPool); | |
| 119 | |
| 120 TimeBudgetPool(const char* name, | |
| 121 TaskQueueThrottler* task_queue_throttler, | |
| 122 base::TimeTicks now, | |
| 123 base::Optional<base::TimeDelta> max_budget_level, | |
| 124 base::Optional<base::TimeDelta> max_throttling_duration); | |
| 125 | |
| 126 // Advances |last_checkpoint_| to |now| if needed and recalculates | |
| 127 // budget level. | |
| 128 void Advance(base::TimeTicks now); | |
| 129 | |
| 130 // Disable all associated throttled queues. | |
| 131 void BlockThrottledQueues(base::TimeTicks now); | |
| 132 | |
| 133 // Increase |current_budget_level_| to satisfy max throttling duration | |
| 134 // condition if necessary. | |
| 135 // Decrease |current_budget_level_| to satisfy max budget level | |
| 136 // condition if necessary. | |
| 137 void EnforceBudgetLevelRestrictions(); | |
| 138 | |
| 139 const char* name_; // NOT OWNED | |
| 140 | |
| 141 TaskQueueThrottler* task_queue_throttler_; | |
| 142 | |
| 143 // Max budget level which we can accrue. | |
| 144 // Tasks will be allowed to run for this time before being throttled | |
| 145 // after a very long period of inactivity. | |
| 146 base::Optional<base::TimeDelta> max_budget_level_; | |
| 147 // Max throttling duration places a lower limit on time budget level, | |
| 148 // ensuring that one long task does not cause extremely long throttling. | |
| 149 // Note that this is not the guarantee that every task will run | |
| 150 // after desired run time + max throttling duration, but a guarantee | |
| 151 // that at least one task will be run every max_throttling_duration. | |
| 152 base::Optional<base::TimeDelta> max_throttling_duration_; | |
| 153 | |
| 154 base::TimeDelta current_budget_level_; | |
| 155 base::TimeTicks last_checkpoint_; | |
| 156 double cpu_percentage_; | |
| 157 bool is_enabled_; | |
| 158 | |
| 159 std::unordered_set<TaskQueue*> associated_task_queues_; | |
| 160 | |
| 161 base::Callback<void(base::TimeDelta)> reporting_callback_; | |
| 162 | |
| 163 DISALLOW_COPY_AND_ASSIGN(TimeBudgetPool); | |
| 164 }; | |
| 165 | |
| 166 } // namespace scheduler | |
| 167 } // namespace blink | |
| 168 | |
| 169 #endif // THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_RENDERER_BUDGET_POOL_H_ | |
| OLD | NEW |