| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 #include "content/browser/memory/memory_state_updater.h" | 5 #include "content/browser/memory/memory_condition_observer.h" |
| 6 | 6 |
| 7 #include "base/metrics/histogram_macros.h" | 7 #include "base/metrics/histogram_macros.h" |
| 8 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
| 9 #include "components/variations/variations_associated_data.h" | 9 #include "components/variations/variations_associated_data.h" |
| 10 #include "content/browser/memory/memory_monitor.h" | 10 #include "content/browser/memory/memory_monitor.h" |
| 11 | 11 |
| 12 namespace content { | 12 namespace content { |
| 13 | 13 |
| 14 namespace { | 14 namespace { |
| 15 | 15 |
| 16 // A expected renderer size. These values come from the median of appropriate | 16 // A expected renderer size. These values come from the median of appropriate |
| 17 // UMA stats. | 17 // UMA stats. |
| 18 #if defined(OS_ANDROID) || defined(OS_IOS) | 18 #if defined(OS_ANDROID) || defined(OS_IOS) |
| 19 const int kDefaultExpectedRendererSizeMB = 40; | 19 const int kDefaultExpectedRendererSizeMB = 40; |
| 20 #elif defined(OS_WIN) | 20 #elif defined(OS_WIN) |
| 21 const int kDefaultExpectedRendererSizeMB = 70; | 21 const int kDefaultExpectedRendererSizeMB = 70; |
| 22 #else // Mac, Linux, and ChromeOS | 22 #else // Mac, Linux, and ChromeOS |
| 23 const int kDefaultExpectedRendererSizeMB = 120; | 23 const int kDefaultExpectedRendererSizeMB = 120; |
| 24 #endif | 24 #endif |
| 25 | 25 |
| 26 // Default values for parameters to determine the global state. | 26 // Default values for parameters to determine the global state. |
| 27 const int kDefaultNewRenderersUntilThrottled = 4; | 27 const int kDefaultNewRenderersUntilWarning = 4; |
| 28 const int kDefaultNewRenderersUntilSuspended = 2; | 28 const int kDefaultNewRenderersUntilCritical = 2; |
| 29 const int kDefaultNewRenderersBackToNormal = 5; | 29 const int kDefaultNewRenderersBackToNormal = 5; |
| 30 const int kDefaultNewRenderersBackToThrottled = 3; | 30 const int kDefaultNewRenderersBackToWarning = 3; |
| 31 const int kDefaultMinimumTransitionPeriodSeconds = 30; | |
| 32 const int kDefaultMonitoringIntervalSeconds = 5; | 31 const int kDefaultMonitoringIntervalSeconds = 5; |
| 33 | 32 |
| 34 void SetIntVariationParameter(const std::map<std::string, std::string> params, | 33 void SetIntVariationParameter(const std::map<std::string, std::string> params, |
| 35 const char* name, | 34 const char* name, |
| 36 int* target) { | 35 int* target) { |
| 37 const auto& iter = params.find(name); | 36 const auto& iter = params.find(name); |
| 38 if (iter == params.end()) | 37 if (iter == params.end()) |
| 39 return; | 38 return; |
| 40 int value; | 39 int value; |
| 41 if (!iter->second.empty() && base::StringToInt(iter->second, &value)) { | 40 if (!iter->second.empty() && base::StringToInt(iter->second, &value)) { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 53 return; | 52 return; |
| 54 int value; | 53 int value; |
| 55 if (!iter->second.empty() && base::StringToInt(iter->second, &value)) { | 54 if (!iter->second.empty() && base::StringToInt(iter->second, &value)) { |
| 56 DCHECK(value > 0); | 55 DCHECK(value > 0); |
| 57 *target = base::TimeDelta::FromSeconds(value); | 56 *target = base::TimeDelta::FromSeconds(value); |
| 58 } | 57 } |
| 59 } | 58 } |
| 60 | 59 |
| 61 } // namespace | 60 } // namespace |
| 62 | 61 |
| 63 MemoryStateUpdater::MemoryStateUpdater( | 62 MemoryConditionObserver::MemoryConditionObserver( |
| 64 MemoryCoordinatorImpl* coordinator, | 63 MemoryCoordinatorImpl* coordinator, |
| 65 scoped_refptr<base::SingleThreadTaskRunner> task_runner) | 64 scoped_refptr<base::SingleThreadTaskRunner> task_runner) |
| 66 : coordinator_(coordinator), task_runner_(task_runner) { | 65 : coordinator_(coordinator), task_runner_(task_runner) { |
| 67 DCHECK(coordinator_); | 66 DCHECK(coordinator_); |
| 68 InitializeParameters(); | 67 InitializeParameters(); |
| 69 DCHECK(ValidateParameters()); | 68 DCHECK(ValidateParameters()); |
| 70 } | 69 } |
| 71 | 70 |
| 72 MemoryStateUpdater::~MemoryStateUpdater() {} | 71 MemoryConditionObserver::~MemoryConditionObserver() {} |
| 73 | 72 |
| 74 base::MemoryState MemoryStateUpdater::CalculateNextState() { | 73 void MemoryConditionObserver::ScheduleUpdateCondition(base::TimeDelta delta) { |
| 75 using MemoryState = base::MemoryState; | 74 update_condition_closure_.Reset(base::Bind( |
| 75 &MemoryConditionObserver::UpdateCondition, base::Unretained(this))); |
| 76 task_runner_->PostDelayedTask(FROM_HERE, update_condition_closure_.callback(), |
| 77 delta); |
| 78 } |
| 76 | 79 |
| 80 MemoryCondition MemoryConditionObserver::CalculateNextCondition() { |
| 77 int available = | 81 int available = |
| 78 coordinator_->memory_monitor()->GetFreeMemoryUntilCriticalMB(); | 82 coordinator_->memory_monitor()->GetFreeMemoryUntilCriticalMB(); |
| 79 | 83 |
| 80 // TODO(chrisha): Move this histogram recording to a better place when | 84 // TODO(chrisha): Move this histogram recording to a better place when |
| 81 // https://codereview.chromium.org/2479673002/ is landed. | 85 // https://codereview.chromium.org/2479673002/ is landed. |
| 82 UMA_HISTOGRAM_MEMORY_LARGE_MB("Memory.Coordinator.FreeMemoryUntilCritical", | 86 UMA_HISTOGRAM_MEMORY_LARGE_MB("Memory.Coordinator.FreeMemoryUntilCritical", |
| 83 available); | 87 available); |
| 84 | 88 |
| 85 if (available <= 0) | 89 if (available <= 0) |
| 86 return MemoryState::SUSPENDED; | 90 return MemoryCondition::CRITICAL; |
| 87 | 91 |
| 88 auto current_state = coordinator_->GetGlobalMemoryState(); | 92 auto current = coordinator_->GetMemoryCondition(); |
| 89 int expected_renderer_count = available / expected_renderer_size_; | 93 int expected_renderer_count = available / expected_renderer_size_; |
| 90 | 94 |
| 91 switch (current_state) { | 95 switch (current) { |
| 92 case MemoryState::NORMAL: | 96 case MemoryCondition::NORMAL: |
| 93 if (expected_renderer_count <= new_renderers_until_suspended_) | 97 if (expected_renderer_count <= new_renderers_until_critical_) |
| 94 return MemoryState::SUSPENDED; | 98 return MemoryCondition::CRITICAL; |
| 95 if (expected_renderer_count <= new_renderers_until_throttled_) | 99 if (expected_renderer_count <= new_renderers_until_warning_) |
| 96 return MemoryState::THROTTLED; | 100 return MemoryCondition::WARNING; |
| 97 return MemoryState::NORMAL; | 101 return MemoryCondition::NORMAL; |
| 98 case MemoryState::THROTTLED: | 102 case MemoryCondition::WARNING: |
| 99 if (expected_renderer_count <= new_renderers_until_suspended_) | 103 if (expected_renderer_count <= new_renderers_until_critical_) |
| 100 return MemoryState::SUSPENDED; | 104 return MemoryCondition::CRITICAL; |
| 101 if (expected_renderer_count >= new_renderers_back_to_normal_) | 105 if (expected_renderer_count >= new_renderers_back_to_normal_) |
| 102 return MemoryState::NORMAL; | 106 return MemoryCondition::NORMAL; |
| 103 return MemoryState::THROTTLED; | 107 return MemoryCondition::WARNING; |
| 104 case MemoryState::SUSPENDED: | 108 case MemoryCondition::CRITICAL: |
| 105 if (expected_renderer_count >= new_renderers_back_to_normal_) | 109 if (expected_renderer_count >= new_renderers_back_to_normal_) |
| 106 return MemoryState::NORMAL; | 110 return MemoryCondition::NORMAL; |
| 107 if (expected_renderer_count >= new_renderers_back_to_throttled_) | 111 if (expected_renderer_count >= new_renderers_back_to_warning_) |
| 108 return MemoryState::THROTTLED; | 112 return MemoryCondition::WARNING; |
| 109 return MemoryState::SUSPENDED; | 113 return MemoryCondition::CRITICAL; |
| 110 case MemoryState::UNKNOWN: | |
| 111 // Fall through | |
| 112 default: | |
| 113 NOTREACHED(); | |
| 114 return MemoryState::UNKNOWN; | |
| 115 } | 114 } |
| 116 } | 115 } |
| 117 | 116 |
| 118 void MemoryStateUpdater::UpdateState() { | 117 void MemoryConditionObserver::UpdateCondition() { |
| 119 auto current_state = coordinator_->GetGlobalMemoryState(); | 118 auto next_condition = CalculateNextCondition(); |
| 120 auto next_state = CalculateNextState(); | 119 coordinator_->UpdateConditionIfNeeded(next_condition); |
| 121 if (coordinator_->ChangeStateIfNeeded(current_state, next_state)) { | 120 ScheduleUpdateCondition(monitoring_interval_); |
| 122 ScheduleUpdateState(minimum_transition_period_); | |
| 123 } else { | |
| 124 ScheduleUpdateState(monitoring_interval_); | |
| 125 } | |
| 126 } | 121 } |
| 127 | 122 |
| 128 void MemoryStateUpdater::ScheduleUpdateState(base::TimeDelta delta) { | 123 void MemoryConditionObserver::InitializeParameters() { |
| 129 update_state_closure_.Reset(base::Bind(&MemoryStateUpdater::UpdateState, | |
| 130 base::Unretained(this))); | |
| 131 task_runner_->PostDelayedTask(FROM_HERE, update_state_closure_.callback(), | |
| 132 delta); | |
| 133 } | |
| 134 | |
| 135 void MemoryStateUpdater::InitializeParameters() { | |
| 136 expected_renderer_size_ = kDefaultExpectedRendererSizeMB; | 124 expected_renderer_size_ = kDefaultExpectedRendererSizeMB; |
| 137 new_renderers_until_throttled_ = kDefaultNewRenderersUntilThrottled; | 125 new_renderers_until_warning_ = kDefaultNewRenderersUntilWarning; |
| 138 new_renderers_until_suspended_ = kDefaultNewRenderersUntilSuspended; | 126 new_renderers_until_critical_ = kDefaultNewRenderersUntilCritical; |
| 139 new_renderers_back_to_normal_ = kDefaultNewRenderersBackToNormal; | 127 new_renderers_back_to_normal_ = kDefaultNewRenderersBackToNormal; |
| 140 new_renderers_back_to_throttled_ = kDefaultNewRenderersBackToThrottled; | 128 new_renderers_back_to_warning_ = kDefaultNewRenderersBackToWarning; |
| 141 minimum_transition_period_ = | |
| 142 base::TimeDelta::FromSeconds(kDefaultMinimumTransitionPeriodSeconds); | |
| 143 monitoring_interval_ = | 129 monitoring_interval_ = |
| 144 base::TimeDelta::FromSeconds(kDefaultMonitoringIntervalSeconds); | 130 base::TimeDelta::FromSeconds(kDefaultMonitoringIntervalSeconds); |
| 145 | 131 |
| 146 // Override default parameters with variations. | 132 // Override default parameters with variations. |
| 147 static constexpr char kMemoryCoordinatorV0Trial[] = "MemoryCoordinatorV0"; | 133 static constexpr char kMemoryCoordinatorV0Trial[] = "MemoryCoordinatorV0"; |
| 148 std::map<std::string, std::string> params; | 134 std::map<std::string, std::string> params; |
| 149 variations::GetVariationParams(kMemoryCoordinatorV0Trial, ¶ms); | 135 variations::GetVariationParams(kMemoryCoordinatorV0Trial, ¶ms); |
| 150 // TODO(bashi): Renaming (throttled -> warning, suspended -> critical) is | 136 // TODO(bashi): Renaming (throttled -> warning, suspended -> critical) is |
| 151 // ongoing. Get variation parameters from both until server-side change | 137 // ongoing. Get variation parameters from both until server-side change |
| 152 // is done. crbug.com/696844 | 138 // is done. crbug.com/696844 |
| 153 SetIntVariationParameter(params, "expected_renderer_size", | 139 SetIntVariationParameter(params, "expected_renderer_size", |
| 154 &expected_renderer_size_); | 140 &expected_renderer_size_); |
| 155 SetIntVariationParameter(params, "new_renderers_until_throttled", | 141 SetIntVariationParameter(params, "new_renderers_until_throttled", |
| 156 &new_renderers_until_throttled_); | 142 &new_renderers_until_warning_); |
| 157 SetIntVariationParameter(params, "new_renderers_until_warning", | 143 SetIntVariationParameter(params, "new_renderers_until_warning", |
| 158 &new_renderers_until_throttled_); | 144 &new_renderers_until_warning_); |
| 159 SetIntVariationParameter(params, "new_renderers_until_suspended", | 145 SetIntVariationParameter(params, "new_renderers_until_suspended", |
| 160 &new_renderers_until_suspended_); | 146 &new_renderers_until_critical_); |
| 161 SetIntVariationParameter(params, "new_renderers_until_critical", | 147 SetIntVariationParameter(params, "new_renderers_until_critical", |
| 162 &new_renderers_until_suspended_); | 148 &new_renderers_until_critical_); |
| 163 SetIntVariationParameter(params, "new_renderers_back_to_normal", | 149 SetIntVariationParameter(params, "new_renderers_back_to_normal", |
| 164 &new_renderers_back_to_normal_); | 150 &new_renderers_back_to_normal_); |
| 165 SetIntVariationParameter(params, "new_renderers_back_to_throttled", | 151 SetIntVariationParameter(params, "new_renderers_back_to_throttled", |
| 166 &new_renderers_back_to_throttled_); | 152 &new_renderers_back_to_warning_); |
| 167 SetIntVariationParameter(params, "new_renderers_back_to_warning", | 153 SetIntVariationParameter(params, "new_renderers_back_to_warning", |
| 168 &new_renderers_back_to_throttled_); | 154 &new_renderers_back_to_warning_); |
| 169 SetSecondsVariationParameter(params, "minimum_transition_period", | |
| 170 &minimum_transition_period_); | |
| 171 SetSecondsVariationParameter(params, "monitoring_interval", | 155 SetSecondsVariationParameter(params, "monitoring_interval", |
| 172 &monitoring_interval_); | 156 &monitoring_interval_); |
| 173 } | 157 } |
| 174 | 158 |
| 175 bool MemoryStateUpdater::ValidateParameters() { | 159 bool MemoryConditionObserver::ValidateParameters() { |
| 176 return (new_renderers_until_throttled_ > new_renderers_until_suspended_) && | 160 return (new_renderers_until_warning_ > new_renderers_until_critical_) && |
| 177 (new_renderers_back_to_normal_ > new_renderers_back_to_throttled_) && | 161 (new_renderers_back_to_normal_ > new_renderers_back_to_warning_) && |
| 178 (new_renderers_back_to_normal_ > new_renderers_until_throttled_) && | 162 (new_renderers_back_to_normal_ > new_renderers_until_warning_) && |
| 179 (new_renderers_back_to_throttled_ > new_renderers_until_suspended_); | 163 (new_renderers_back_to_warning_ > new_renderers_until_critical_); |
| 180 } | 164 } |
| 181 | 165 |
| 182 } // namespace content | 166 } // namespace content |
| OLD | NEW |