| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 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 #ifndef THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_RENDERER_BUDGET_POOL_H_ | 5 #ifndef THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_RENDERER_BUDGET_POOL_H_ |
| 6 #define THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_RENDERER_BUDGET_POOL_H_ | 6 #define THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_RENDERER_BUDGET_POOL_H_ |
| 7 | 7 |
| 8 #include <unordered_set> | 8 #include <unordered_set> |
| 9 | 9 |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| 11 #include "base/gtest_prod_util.h" | 11 #include "base/gtest_prod_util.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/optional.h" | 13 #include "base/optional.h" |
| 14 #include "base/time/time.h" | 14 #include "base/time/time.h" |
| 15 #include "platform/scheduler/base/lazy_now.h" | 15 #include "platform/scheduler/base/lazy_now.h" |
| 16 | 16 |
| 17 namespace base { | 17 namespace base { |
| 18 namespace trace_event { | 18 namespace trace_event { |
| 19 class TracedValue; | 19 class TracedValue; |
| 20 } | 20 } |
| 21 } | 21 } |
| 22 | 22 |
| 23 namespace blink { | 23 namespace blink { |
| 24 namespace scheduler { | 24 namespace scheduler { |
| 25 | 25 |
| 26 class TaskQueue; | 26 class TaskQueue; |
| 27 class TaskQueueThrottler; | 27 class BudgetPoolController; |
| 28 | 28 |
| 29 // BudgetPool represents a group of task queues which share a limit | 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 | 30 // on a resource. This limit applies when task queues are already throttled |
| 31 // by TaskQueueThrottler. | 31 // by TaskQueueThrottler. |
| 32 class BLINK_PLATFORM_EXPORT BudgetPool { | 32 class BLINK_PLATFORM_EXPORT BudgetPool { |
| 33 public: | 33 public: |
| 34 virtual ~BudgetPool(); | 34 virtual ~BudgetPool(); |
| 35 | 35 |
| 36 virtual const char* Name() const = 0; | 36 virtual const char* Name() const = 0; |
| 37 | 37 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 54 // will be scheduled to enable this queues back again and respect | 54 // will be scheduled to enable this queues back again and respect |
| 55 // timer alignment. Internal budget level will not regenerate with time. | 55 // timer alignment. Internal budget level will not regenerate with time. |
| 56 virtual void DisableThrottling(LazyNow* now) = 0; | 56 virtual void DisableThrottling(LazyNow* now) = 0; |
| 57 | 57 |
| 58 virtual bool IsThrottlingEnabled() const = 0; | 58 virtual bool IsThrottlingEnabled() const = 0; |
| 59 | 59 |
| 60 // Report task run time to the budget pool. | 60 // Report task run time to the budget pool. |
| 61 virtual void RecordTaskRunTime(base::TimeTicks start_time, | 61 virtual void RecordTaskRunTime(base::TimeTicks start_time, |
| 62 base::TimeTicks end_time) = 0; | 62 base::TimeTicks end_time) = 0; |
| 63 | 63 |
| 64 // Block all associated queues and schedule them to run when appropriate. |
| 65 virtual void BlockThrottledQueues(base::TimeTicks now) = 0; |
| 66 |
| 64 // All queues should be removed before calling Close(). | 67 // All queues should be removed before calling Close(). |
| 65 virtual void Close() = 0; | 68 virtual void Close() = 0; |
| 66 | 69 |
| 67 // Retuns earliest time (can be in the past) when the next task can run. | 70 // Retuns earliest time (can be in the past) when the next task can run. |
| 68 virtual base::TimeTicks GetNextAllowedRunTime() = 0; | 71 virtual base::TimeTicks GetNextAllowedRunTime() = 0; |
| 69 | 72 |
| 70 // Returns true at a task can be run immediately at the given time. | 73 // Returns true at a task can be run immediately at the given time. |
| 71 virtual bool HasEnoughBudgetToRun(base::TimeTicks now) = 0; | 74 virtual bool HasEnoughBudgetToRun(base::TimeTicks now) = 0; |
| 72 | 75 |
| 73 // Returns state for tracing. | 76 // Returns state for tracing. |
| 74 virtual void AsValueInto(base::trace_event::TracedValue* state, | 77 virtual void AsValueInto(base::trace_event::TracedValue* state, |
| 75 base::TimeTicks now) const = 0; | 78 base::TimeTicks now) const = 0; |
| 76 }; | 79 }; |
| 77 | 80 |
| 78 // CPUTimeBudgetPool represents a collection of task queues which share a limit | 81 // CPUTimeBudgetPool represents a collection of task queues which share a limit |
| 79 // on total cpu time. | 82 // on total cpu time. |
| 80 class BLINK_PLATFORM_EXPORT CPUTimeBudgetPool : public BudgetPool { | 83 class BLINK_PLATFORM_EXPORT CPUTimeBudgetPool : public BudgetPool { |
| 81 public: | 84 public: |
| 85 CPUTimeBudgetPool(const char* name, |
| 86 BudgetPoolController* budget_pool_controller, |
| 87 base::TimeTicks now, |
| 88 base::Optional<base::TimeDelta> max_budget_level, |
| 89 base::Optional<base::TimeDelta> max_throttling_duration); |
| 90 |
| 82 ~CPUTimeBudgetPool(); | 91 ~CPUTimeBudgetPool(); |
| 83 | 92 |
| 84 // Throttle task queues from this time budget pool if tasks are running | 93 // Throttle task queues from this time budget pool if tasks are running |
| 85 // for more than |cpu_percentage| per cent of wall time. | 94 // for more than |cpu_percentage| per cent of wall time. |
| 86 // This function does not affect internal time budget level. | 95 // This function does not affect internal time budget level. |
| 87 void SetTimeBudgetRecoveryRate(base::TimeTicks now, double cpu_percentage); | 96 void SetTimeBudgetRecoveryRate(base::TimeTicks now, double cpu_percentage); |
| 88 | 97 |
| 89 // Increase budget level by given value. This function DOES NOT unblock | 98 // Increase budget level by given value. This function DOES NOT unblock |
| 90 // queues even if they are allowed to run with increased budget level. | 99 // queues even if they are allowed to run with increased budget level. |
| 91 void GrantAdditionalBudget(base::TimeTicks now, base::TimeDelta budget_level); | 100 void GrantAdditionalBudget(base::TimeTicks now, base::TimeDelta budget_level); |
| 92 | 101 |
| 93 // Set callback which will be called every time when this budget pool | 102 // Set callback which will be called every time when this budget pool |
| 94 // is throttled. Throttling duration (time until the queue is allowed | 103 // is throttled. Throttling duration (time until the queue is allowed |
| 95 // to run again) is passed as a parameter to callback. | 104 // to run again) is passed as a parameter to callback. |
| 96 void SetReportingCallback( | 105 void SetReportingCallback( |
| 97 base::Callback<void(base::TimeDelta)> reporting_callback); | 106 base::Callback<void(base::TimeDelta)> reporting_callback); |
| 98 | 107 |
| 99 // BudgetPool implementation: | 108 // BudgetPool implementation: |
| 100 const char* Name() const override; | 109 const char* Name() const override; |
| 101 void AddQueue(base::TimeTicks now, TaskQueue* queue) override; | 110 void AddQueue(base::TimeTicks now, TaskQueue* queue) override; |
| 102 void RemoveQueue(base::TimeTicks now, TaskQueue* queue) override; | 111 void RemoveQueue(base::TimeTicks now, TaskQueue* queue) override; |
| 103 void EnableThrottling(LazyNow* now) override; | 112 void EnableThrottling(LazyNow* now) override; |
| 104 void DisableThrottling(LazyNow* now) override; | 113 void DisableThrottling(LazyNow* now) override; |
| 105 bool IsThrottlingEnabled() const override; | 114 bool IsThrottlingEnabled() const override; |
| 106 void RecordTaskRunTime(base::TimeTicks start_time, | 115 void RecordTaskRunTime(base::TimeTicks start_time, |
| 107 base::TimeTicks end_time) override; | 116 base::TimeTicks end_time) override; |
| 117 void BlockThrottledQueues(base::TimeTicks now) override; |
| 108 void Close() override; | 118 void Close() override; |
| 109 bool HasEnoughBudgetToRun(base::TimeTicks now) override; | 119 bool HasEnoughBudgetToRun(base::TimeTicks now) override; |
| 110 base::TimeTicks GetNextAllowedRunTime() override; | 120 base::TimeTicks GetNextAllowedRunTime() override; |
| 111 void AsValueInto(base::trace_event::TracedValue* state, | 121 void AsValueInto(base::trace_event::TracedValue* state, |
| 112 base::TimeTicks now) const override; | 122 base::TimeTicks now) const override; |
| 113 | 123 |
| 114 private: | 124 private: |
| 115 friend class TaskQueueThrottler; | |
| 116 | |
| 117 FRIEND_TEST_ALL_PREFIXES(TaskQueueThrottlerTest, CPUTimeBudgetPool); | 125 FRIEND_TEST_ALL_PREFIXES(TaskQueueThrottlerTest, CPUTimeBudgetPool); |
| 118 | 126 |
| 119 CPUTimeBudgetPool(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 | 127 // Advances |last_checkpoint_| to |now| if needed and recalculates |
| 126 // budget level. | 128 // budget level. |
| 127 void Advance(base::TimeTicks now); | 129 void Advance(base::TimeTicks now); |
| 128 | 130 |
| 129 // Disable all associated throttled queues. | |
| 130 void BlockThrottledQueues(base::TimeTicks now); | |
| 131 | |
| 132 // Increase |current_budget_level_| to satisfy max throttling duration | 131 // Increase |current_budget_level_| to satisfy max throttling duration |
| 133 // condition if necessary. | 132 // condition if necessary. |
| 134 // Decrease |current_budget_level_| to satisfy max budget level | 133 // Decrease |current_budget_level_| to satisfy max budget level |
| 135 // condition if necessary. | 134 // condition if necessary. |
| 136 void EnforceBudgetLevelRestrictions(); | 135 void EnforceBudgetLevelRestrictions(); |
| 137 | 136 |
| 138 const char* name_; // NOT OWNED | 137 const char* name_; // NOT OWNED |
| 139 | 138 |
| 140 TaskQueueThrottler* task_queue_throttler_; | 139 BudgetPoolController* budget_pool_controller_; |
| 141 | 140 |
| 142 // Max budget level which we can accrue. | 141 // Max budget level which we can accrue. |
| 143 // Tasks will be allowed to run for this time before being throttled | 142 // Tasks will be allowed to run for this time before being throttled |
| 144 // after a very long period of inactivity. | 143 // after a very long period of inactivity. |
| 145 base::Optional<base::TimeDelta> max_budget_level_; | 144 base::Optional<base::TimeDelta> max_budget_level_; |
| 146 // Max throttling duration places a lower limit on time budget level, | 145 // Max throttling duration places a lower limit on time budget level, |
| 147 // ensuring that one long task does not cause extremely long throttling. | 146 // ensuring that one long task does not cause extremely long throttling. |
| 148 // Note that this is not the guarantee that every task will run | 147 // Note that this is not the guarantee that every task will run |
| 149 // after desired run time + max throttling duration, but a guarantee | 148 // after desired run time + max throttling duration, but a guarantee |
| 150 // that at least one task will be run every max_throttling_duration. | 149 // that at least one task will be run every max_throttling_duration. |
| 151 base::Optional<base::TimeDelta> max_throttling_duration_; | 150 base::Optional<base::TimeDelta> max_throttling_duration_; |
| 152 | 151 |
| 153 base::TimeDelta current_budget_level_; | 152 base::TimeDelta current_budget_level_; |
| 154 base::TimeTicks last_checkpoint_; | 153 base::TimeTicks last_checkpoint_; |
| 155 double cpu_percentage_; | 154 double cpu_percentage_; |
| 156 bool is_enabled_; | 155 bool is_enabled_; |
| 157 | 156 |
| 158 std::unordered_set<TaskQueue*> associated_task_queues_; | 157 std::unordered_set<TaskQueue*> associated_task_queues_; |
| 159 | 158 |
| 160 base::Callback<void(base::TimeDelta)> reporting_callback_; | 159 base::Callback<void(base::TimeDelta)> reporting_callback_; |
| 161 | 160 |
| 162 DISALLOW_COPY_AND_ASSIGN(CPUTimeBudgetPool); | 161 DISALLOW_COPY_AND_ASSIGN(CPUTimeBudgetPool); |
| 163 }; | 162 }; |
| 164 | 163 |
| 165 } // namespace scheduler | 164 } // namespace scheduler |
| 166 } // namespace blink | 165 } // namespace blink |
| 167 | 166 |
| 168 #endif // THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_RENDERER_BUDGET_POOL_H_ | 167 #endif // THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_RENDERER_BUDGET_POOL_H_ |
| OLD | NEW |