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_condition_observer.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 // An expected renderer size. These values come from the median of appropriate | 16 // An 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 kExpectedRendererSizeMB = 40; |
20 #elif defined(OS_WIN) | 20 #elif defined(OS_WIN) |
21 const int kDefaultExpectedRendererSizeMB = 70; | 21 const int kExpectedRendererSizeMB = 70; |
22 #else // Mac, Linux, and ChromeOS | 22 #else // Mac, Linux, and ChromeOS |
23 const int kDefaultExpectedRendererSizeMB = 120; | 23 const int kExpectedRendererSizeMB = 120; |
24 #endif | 24 #endif |
25 | 25 |
26 // Default values for parameters to determine the global state. | 26 const int kNewRenderersUntilCritical = 2; |
27 const int kDefaultNewRenderersUntilWarning = 4; | |
28 const int kDefaultNewRenderersUntilCritical = 2; | |
29 const int kDefaultNewRenderersBackToNormal = 5; | |
30 const int kDefaultNewRenderersBackToWarning = 3; | |
31 const int kDefaultMonitoringIntervalSeconds = 1; | 27 const int kDefaultMonitoringIntervalSeconds = 1; |
32 const int kMonitoringIntervalBackgroundedSeconds = 120; | 28 const int kMonitoringIntervalBackgroundedSeconds = 120; |
33 | 29 |
34 void SetIntVariationParameter(const std::map<std::string, std::string>& params, | |
35 const char* name, | |
36 int* target) { | |
37 const auto& iter = params.find(name); | |
38 if (iter == params.end()) | |
39 return; | |
40 int value; | |
41 if (!iter->second.empty() && base::StringToInt(iter->second, &value)) { | |
42 DCHECK(value > 0); | |
43 *target = value; | |
44 } | |
45 } | |
46 | |
47 void SetSecondsVariationParameter( | |
48 const std::map<std::string, std::string>& params, | |
49 const char* name, | |
50 base::TimeDelta* target) { | |
51 const auto& iter = params.find(name); | |
52 if (iter == params.end()) | |
53 return; | |
54 int value; | |
55 if (!iter->second.empty() && base::StringToInt(iter->second, &value)) { | |
56 DCHECK(value > 0); | |
57 *target = base::TimeDelta::FromSeconds(value); | |
58 } | |
59 } | |
60 | |
61 } // namespace | 30 } // namespace |
62 | 31 |
63 MemoryConditionObserver::MemoryConditionObserver( | 32 MemoryConditionObserver::MemoryConditionObserver( |
64 MemoryCoordinatorImpl* coordinator, | 33 MemoryCoordinatorImpl* coordinator, |
65 scoped_refptr<base::SingleThreadTaskRunner> task_runner) | 34 scoped_refptr<base::SingleThreadTaskRunner> task_runner) |
66 : coordinator_(coordinator), task_runner_(task_runner) { | 35 : coordinator_(coordinator), task_runner_(task_runner) { |
67 DCHECK(coordinator_); | 36 DCHECK(coordinator_); |
68 InitializeParameters(); | 37 monitoring_interval_ = |
69 DCHECK(ValidateParameters()); | 38 base::TimeDelta::FromSeconds(kDefaultMonitoringIntervalSeconds); |
| 39 monitoring_interval_foregrounded_ = |
| 40 base::TimeDelta::FromSeconds(kDefaultMonitoringIntervalSeconds); |
| 41 monitoring_interval_backgrounded_ = |
| 42 base::TimeDelta::FromSeconds(kMonitoringIntervalBackgroundedSeconds); |
70 } | 43 } |
71 | 44 |
72 MemoryConditionObserver::~MemoryConditionObserver() {} | 45 MemoryConditionObserver::~MemoryConditionObserver() {} |
73 | 46 |
74 void MemoryConditionObserver::ScheduleUpdateCondition(base::TimeDelta delay) { | 47 void MemoryConditionObserver::ScheduleUpdateCondition(base::TimeDelta delay) { |
75 update_condition_closure_.Reset(base::Bind( | 48 update_condition_closure_.Reset(base::Bind( |
76 &MemoryConditionObserver::UpdateCondition, base::Unretained(this))); | 49 &MemoryConditionObserver::UpdateCondition, base::Unretained(this))); |
77 task_runner_->PostDelayedTask(FROM_HERE, update_condition_closure_.callback(), | 50 task_runner_->PostDelayedTask(FROM_HERE, update_condition_closure_.callback(), |
78 delay); | 51 delay); |
79 } | 52 } |
(...skipping 16 matching lines...) Expand all Loading... |
96 | 69 |
97 MemoryCondition MemoryConditionObserver::CalculateNextCondition() { | 70 MemoryCondition MemoryConditionObserver::CalculateNextCondition() { |
98 int available = | 71 int available = |
99 coordinator_->memory_monitor()->GetFreeMemoryUntilCriticalMB(); | 72 coordinator_->memory_monitor()->GetFreeMemoryUntilCriticalMB(); |
100 | 73 |
101 // TODO(chrisha): Move this histogram recording to a better place when | 74 // TODO(chrisha): Move this histogram recording to a better place when |
102 // https://codereview.chromium.org/2479673002/ is landed. | 75 // https://codereview.chromium.org/2479673002/ is landed. |
103 UMA_HISTOGRAM_MEMORY_LARGE_MB("Memory.Coordinator.FreeMemoryUntilCritical", | 76 UMA_HISTOGRAM_MEMORY_LARGE_MB("Memory.Coordinator.FreeMemoryUntilCritical", |
104 available); | 77 available); |
105 | 78 |
106 if (available <= 0) | 79 int expected_renderer_count = available / kExpectedRendererSizeMB; |
| 80 if (available <= 0 || expected_renderer_count < kNewRenderersUntilCritical) |
107 return MemoryCondition::CRITICAL; | 81 return MemoryCondition::CRITICAL; |
108 | |
109 auto current = coordinator_->GetMemoryCondition(); | |
110 int expected_renderer_count = available / expected_renderer_size_; | |
111 | |
112 switch (current) { | |
113 case MemoryCondition::NORMAL: | |
114 if (expected_renderer_count <= new_renderers_until_critical_) | |
115 return MemoryCondition::CRITICAL; | |
116 if (expected_renderer_count <= new_renderers_until_warning_) | |
117 return MemoryCondition::WARNING; | |
118 return MemoryCondition::NORMAL; | |
119 case MemoryCondition::WARNING: | |
120 if (expected_renderer_count <= new_renderers_until_critical_) | |
121 return MemoryCondition::CRITICAL; | |
122 if (expected_renderer_count >= new_renderers_back_to_normal_) | |
123 return MemoryCondition::NORMAL; | |
124 return MemoryCondition::WARNING; | |
125 case MemoryCondition::CRITICAL: | |
126 if (expected_renderer_count >= new_renderers_back_to_normal_) | |
127 return MemoryCondition::NORMAL; | |
128 if (expected_renderer_count >= new_renderers_back_to_warning_) | |
129 return MemoryCondition::WARNING; | |
130 return MemoryCondition::CRITICAL; | |
131 } | |
132 NOTREACHED(); | |
133 return MemoryCondition::NORMAL; | 82 return MemoryCondition::NORMAL; |
134 } | 83 } |
135 | 84 |
136 void MemoryConditionObserver::UpdateCondition() { | 85 void MemoryConditionObserver::UpdateCondition() { |
137 auto next_condition = CalculateNextCondition(); | 86 auto next_condition = CalculateNextCondition(); |
138 coordinator_->UpdateConditionIfNeeded(next_condition); | 87 coordinator_->UpdateConditionIfNeeded(next_condition); |
139 ScheduleUpdateCondition(monitoring_interval_); | 88 ScheduleUpdateCondition(monitoring_interval_); |
140 } | 89 } |
141 | 90 |
142 void MemoryConditionObserver::InitializeParameters() { | |
143 expected_renderer_size_ = kDefaultExpectedRendererSizeMB; | |
144 new_renderers_until_warning_ = kDefaultNewRenderersUntilWarning; | |
145 new_renderers_until_critical_ = kDefaultNewRenderersUntilCritical; | |
146 new_renderers_back_to_normal_ = kDefaultNewRenderersBackToNormal; | |
147 new_renderers_back_to_warning_ = kDefaultNewRenderersBackToWarning; | |
148 monitoring_interval_ = | |
149 base::TimeDelta::FromSeconds(kDefaultMonitoringIntervalSeconds); | |
150 monitoring_interval_foregrounded_ = | |
151 base::TimeDelta::FromSeconds(kDefaultMonitoringIntervalSeconds); | |
152 monitoring_interval_backgrounded_ = | |
153 base::TimeDelta::FromSeconds(kMonitoringIntervalBackgroundedSeconds); | |
154 | |
155 // Override default parameters with variations. | |
156 static constexpr char kMemoryCoordinatorV0Trial[] = "MemoryCoordinatorV0"; | |
157 std::map<std::string, std::string> params; | |
158 variations::GetVariationParams(kMemoryCoordinatorV0Trial, ¶ms); | |
159 SetIntVariationParameter(params, "expected_renderer_size", | |
160 &expected_renderer_size_); | |
161 SetIntVariationParameter(params, "new_renderers_until_warning", | |
162 &new_renderers_until_warning_); | |
163 SetIntVariationParameter(params, "new_renderers_until_critical", | |
164 &new_renderers_until_critical_); | |
165 SetIntVariationParameter(params, "new_renderers_back_to_normal", | |
166 &new_renderers_back_to_normal_); | |
167 SetIntVariationParameter(params, "new_renderers_back_to_warning", | |
168 &new_renderers_back_to_warning_); | |
169 SetSecondsVariationParameter(params, "monitoring_interval", | |
170 &monitoring_interval_); | |
171 SetSecondsVariationParameter(params, "monitoring_interval_foregrounded", | |
172 &monitoring_interval_foregrounded_); | |
173 SetSecondsVariationParameter(params, "monitoring_interval_backgrounded", | |
174 &monitoring_interval_backgrounded_); | |
175 } | |
176 | |
177 bool MemoryConditionObserver::ValidateParameters() { | |
178 return (new_renderers_until_warning_ > new_renderers_until_critical_) && | |
179 (new_renderers_back_to_normal_ > new_renderers_back_to_warning_) && | |
180 (new_renderers_back_to_normal_ > new_renderers_until_warning_) && | |
181 (new_renderers_back_to_warning_ > new_renderers_until_critical_); | |
182 } | |
183 | 91 |
184 } // namespace content | 92 } // namespace content |
OLD | NEW |