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 |
| 31 // by TaskQueueThrottler. |
| 32 class BudgetPool { |
| 33 public: |
| 34 virtual ~BudgetPool(); |
| 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; |
| 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; |
| 76 }; |
| 77 |
| 78 // TimeBudgetPool represents a collection of task queues which share a limit |
| 79 // on total cpu time. |
| 80 class TimeBudgetPool : public BudgetPool { |
| 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 // Increase budget level by given value. This function DOES NOT unblock |
| 90 // queues even if they are allowed to run with increased budget level. |
| 91 void GrantAdditionalBudget(base::TimeTicks now, base::TimeDelta budget_level); |
| 92 |
| 93 // Set callback which will be called every time when this budget pool |
| 94 // is throttled. Throttling duration (time until the queue is allowed |
| 95 // to run again) is passed as a parameter to callback. |
| 96 void SetReportingCallback( |
| 97 base::Callback<void(base::TimeDelta)> reporting_callback); |
| 98 |
| 99 // BudgetPool implementation: |
| 100 const char* Name() const override; |
| 101 void AddQueue(base::TimeTicks now, TaskQueue* queue) override; |
| 102 void RemoveQueue(base::TimeTicks now, TaskQueue* queue) override; |
| 103 void EnableThrottling(LazyNow* now) override; |
| 104 void DisableThrottling(LazyNow* now) override; |
| 105 bool IsThrottlingEnabled() const override; |
| 106 void RecordTaskRunTime(base::TimeTicks start_time, |
| 107 base::TimeTicks end_time) override; |
| 108 void Close() override; |
| 109 bool HasEnoughBudgetToRun(base::TimeTicks now) override; |
| 110 base::TimeTicks GetNextAllowedRunTime() override; |
| 111 void AsValueInto(base::trace_event::TracedValue* state, |
| 112 base::TimeTicks now) const override; |
| 113 |
| 114 private: |
| 115 friend class TaskQueueThrottler; |
| 116 |
| 117 FRIEND_TEST_ALL_PREFIXES(TaskQueueThrottlerTest, TimeBudgetPool); |
| 118 |
| 119 TimeBudgetPool(const char* name, |
| 120 TaskQueueThrottler* task_queue_throttler, |
| 121 base::TimeTicks now, |
| 122 base::Optional<base::TimeDelta> max_budget_level, |
| 123 base::Optional<base::TimeDelta> max_throttling_duration); |
| 124 |
| 125 // Advances |last_checkpoint_| to |now| if needed and recalculates |
| 126 // budget level. |
| 127 void Advance(base::TimeTicks now); |
| 128 |
| 129 // Disable all associated throttled queues. |
| 130 void BlockThrottledQueues(base::TimeTicks now); |
| 131 |
| 132 // Increase |current_budget_level_| to satisfy max throttling duration |
| 133 // condition if necessary. |
| 134 // Decrease |current_budget_level_| to satisfy max budget level |
| 135 // condition if necessary. |
| 136 void EnforceBudgetLevelRestrictions(); |
| 137 |
| 138 const char* name_; // NOT OWNED |
| 139 |
| 140 TaskQueueThrottler* task_queue_throttler_; |
| 141 |
| 142 // Max budget level which we can accrue. |
| 143 // Tasks will be allowed to run for this time before being throttled |
| 144 // after a very long period of inactivity. |
| 145 base::Optional<base::TimeDelta> max_budget_level_; |
| 146 // Max throttling duration places a lower limit on time budget level, |
| 147 // ensuring that one long task does not cause extremely long throttling. |
| 148 // Note that this is not the guarantee that every task will run |
| 149 // after desired run time + max throttling duration, but a guarantee |
| 150 // that at least one task will be run every max_throttling_duration. |
| 151 base::Optional<base::TimeDelta> max_throttling_duration_; |
| 152 |
| 153 base::TimeDelta current_budget_level_; |
| 154 base::TimeTicks last_checkpoint_; |
| 155 double cpu_percentage_; |
| 156 bool is_enabled_; |
| 157 |
| 158 std::unordered_set<TaskQueue*> associated_task_queues_; |
| 159 |
| 160 base::Callback<void(base::TimeDelta)> reporting_callback_; |
| 161 |
| 162 DISALLOW_COPY_AND_ASSIGN(TimeBudgetPool); |
| 163 }; |
| 164 |
| 165 } // namespace scheduler |
| 166 } // namespace blink |
| 167 |
| 168 #endif // THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_RENDERER_BUDGET_POOL_H_ |
OLD | NEW |