Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(354)

Side by Side Diff: content/browser/memory/memory_condition_observer.cc

Issue 2731913002: NotForReview: Expose the global memory budget (chromium side)
Patch Set: Add --simulate-memory-pressure Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
55 DCHECK(value > 0); 55 DCHECK(value > 0);
56 *target = base::TimeDelta::FromSeconds(value); 56 *target = base::TimeDelta::FromSeconds(value);
57 } 57 }
58 } 58 }
59 59
60 } // namespace 60 } // namespace
61 61
62 MemoryConditionObserver::MemoryConditionObserver( 62 MemoryConditionObserver::MemoryConditionObserver(
63 MemoryCoordinatorImpl* coordinator, 63 MemoryCoordinatorImpl* coordinator,
64 scoped_refptr<base::SingleThreadTaskRunner> task_runner) 64 scoped_refptr<base::SingleThreadTaskRunner> task_runner)
65 : coordinator_(coordinator), task_runner_(task_runner) { 65 : coordinator_(coordinator),
66 task_runner_(task_runner),
67 current_available_(0) {
66 DCHECK(coordinator_); 68 DCHECK(coordinator_);
67 InitializeParameters(); 69 InitializeParameters();
68 DCHECK(ValidateParameters()); 70 DCHECK(ValidateParameters());
71 update_condition_closure_.Reset(base::Bind(
72 &MemoryConditionObserver::UpdateCondition, base::Unretained(this)));
69 } 73 }
70 74
71 MemoryConditionObserver::~MemoryConditionObserver() {} 75 MemoryConditionObserver::~MemoryConditionObserver() {}
72 76
73 void MemoryConditionObserver::ScheduleUpdateCondition(base::TimeDelta delta) { 77 void MemoryConditionObserver::ScheduleUpdateCondition(
74 update_condition_closure_.Reset(base::Bind( 78 base::TimeDelta interval) {
75 &MemoryConditionObserver::UpdateCondition, base::Unretained(this))); 79 if (!interval.is_zero() && interval != monitoring_interval_) {
80 monitoring_interval_ = interval;
81 update_condition_closure_.Reset(base::Bind(
82 &MemoryConditionObserver::UpdateCondition, base::Unretained(this)));
83 }
76 task_runner_->PostDelayedTask(FROM_HERE, update_condition_closure_.callback(), 84 task_runner_->PostDelayedTask(FROM_HERE, update_condition_closure_.callback(),
77 delta); 85 interval);
78 } 86 }
79 87
80 MemoryCondition MemoryConditionObserver::CalculateNextCondition() { 88 MemoryCondition MemoryConditionObserver::CalculateNextCondition() {
81 int available = 89 if (current_available_ <= 0)
82 coordinator_->memory_monitor()->GetFreeMemoryUntilCriticalMB();
83
84 // TODO(chrisha): Move this histogram recording to a better place when
85 // https://codereview.chromium.org/2479673002/ is landed.
86 UMA_HISTOGRAM_MEMORY_LARGE_MB("Memory.Coordinator.FreeMemoryUntilCritical",
87 available);
88
89 if (available <= 0)
90 return MemoryCondition::CRITICAL; 90 return MemoryCondition::CRITICAL;
91 91
92 auto current = coordinator_->GetMemoryCondition(); 92 auto current_condition = coordinator_->GetMemoryCondition();
93 int expected_renderer_count = available / expected_renderer_size_; 93 int expected_renderer_count = current_available_ / expected_renderer_size_;
94 94
95 switch (current) { 95 switch (current_condition) {
96 case MemoryCondition::NORMAL: 96 case MemoryCondition::NORMAL:
97 if (expected_renderer_count <= new_renderers_until_critical_) 97 if (expected_renderer_count <= new_renderers_until_critical_)
98 return MemoryCondition::CRITICAL; 98 return MemoryCondition::CRITICAL;
99 if (expected_renderer_count <= new_renderers_until_warning_) 99 if (expected_renderer_count <= new_renderers_until_warning_)
100 return MemoryCondition::WARNING; 100 return MemoryCondition::WARNING;
101 return MemoryCondition::NORMAL; 101 return MemoryCondition::NORMAL;
102 case MemoryCondition::WARNING: 102 case MemoryCondition::WARNING:
103 if (expected_renderer_count <= new_renderers_until_critical_) 103 if (expected_renderer_count <= new_renderers_until_critical_)
104 return MemoryCondition::CRITICAL; 104 return MemoryCondition::CRITICAL;
105 if (expected_renderer_count >= new_renderers_back_to_normal_) 105 if (expected_renderer_count >= new_renderers_back_to_normal_)
106 return MemoryCondition::NORMAL; 106 return MemoryCondition::NORMAL;
107 return MemoryCondition::WARNING; 107 return MemoryCondition::WARNING;
108 case MemoryCondition::CRITICAL: 108 case MemoryCondition::CRITICAL:
109 if (expected_renderer_count >= new_renderers_back_to_normal_) 109 if (expected_renderer_count >= new_renderers_back_to_normal_)
110 return MemoryCondition::NORMAL; 110 return MemoryCondition::NORMAL;
111 if (expected_renderer_count >= new_renderers_back_to_warning_) 111 if (expected_renderer_count >= new_renderers_back_to_warning_)
112 return MemoryCondition::WARNING; 112 return MemoryCondition::WARNING;
113 return MemoryCondition::CRITICAL; 113 return MemoryCondition::CRITICAL;
114 } 114 }
115 NOTREACHED(); 115 NOTREACHED();
116 return MemoryCondition::NORMAL; 116 return MemoryCondition::NORMAL;
117 } 117 }
118 118
119 void MemoryConditionObserver::UpdateCondition() { 119 void MemoryConditionObserver::UpdateCondition() {
120 int available =
121 coordinator_->memory_monitor()->GetFreeMemoryUntilCriticalMB();
122 UMA_HISTOGRAM_MEMORY_LARGE_MB("Memory.Coordinator.FreeMemoryUntilCritical",
123 available);
124 current_available_ = std::min(available, max_available_);
120 auto next_condition = CalculateNextCondition(); 125 auto next_condition = CalculateNextCondition();
126 coordinator_->UpdateGlobalBudget(current_available_);
121 coordinator_->UpdateConditionIfNeeded(next_condition); 127 coordinator_->UpdateConditionIfNeeded(next_condition);
122 ScheduleUpdateCondition(monitoring_interval_); 128 ScheduleUpdateCondition(monitoring_interval_);
123 } 129 }
124 130
125 void MemoryConditionObserver::InitializeParameters() { 131 void MemoryConditionObserver::InitializeParameters() {
126 expected_renderer_size_ = kDefaultExpectedRendererSizeMB; 132 expected_renderer_size_ = kDefaultExpectedRendererSizeMB;
127 new_renderers_until_warning_ = kDefaultNewRenderersUntilWarning; 133 new_renderers_until_warning_ = kDefaultNewRenderersUntilWarning;
128 new_renderers_until_critical_ = kDefaultNewRenderersUntilCritical; 134 new_renderers_until_critical_ = kDefaultNewRenderersUntilCritical;
129 new_renderers_back_to_normal_ = kDefaultNewRenderersBackToNormal; 135 new_renderers_back_to_normal_ = kDefaultNewRenderersBackToNormal;
130 new_renderers_back_to_warning_ = kDefaultNewRenderersBackToWarning; 136 new_renderers_back_to_warning_ = kDefaultNewRenderersBackToWarning;
131 monitoring_interval_ = 137 monitoring_interval_ =
132 base::TimeDelta::FromSeconds(kDefaultMonitoringIntervalSeconds); 138 base::TimeDelta::FromSeconds(kDefaultMonitoringIntervalSeconds);
133 139
134 // Override default parameters with variations. 140 // Override default parameters with variations.
135 static constexpr char kMemoryCoordinatorV0Trial[] = "MemoryCoordinatorV0"; 141 static constexpr char kMemoryCoordinatorV0Trial[] = "MemoryCoordinatorV0";
136 std::map<std::string, std::string> params; 142 std::map<std::string, std::string> params;
137 variations::GetVariationParams(kMemoryCoordinatorV0Trial, &params); 143 variations::GetVariationParams(kMemoryCoordinatorV0Trial, &params);
138 SetIntVariationParameter(params, "expected_renderer_size", 144 SetIntVariationParameter(params, "expected_renderer_size",
139 &expected_renderer_size_); 145 &expected_renderer_size_);
140 SetIntVariationParameter(params, "new_renderers_until_warning", 146 SetIntVariationParameter(params, "new_renderers_until_warning",
141 &new_renderers_until_warning_); 147 &new_renderers_until_warning_);
142 SetIntVariationParameter(params, "new_renderers_until_critical", 148 SetIntVariationParameter(params, "new_renderers_until_critical",
143 &new_renderers_until_critical_); 149 &new_renderers_until_critical_);
144 SetIntVariationParameter(params, "new_renderers_back_to_normal", 150 SetIntVariationParameter(params, "new_renderers_back_to_normal",
145 &new_renderers_back_to_normal_); 151 &new_renderers_back_to_normal_);
146 SetIntVariationParameter(params, "new_renderers_back_to_warning", 152 SetIntVariationParameter(params, "new_renderers_back_to_warning",
147 &new_renderers_back_to_warning_); 153 &new_renderers_back_to_warning_);
148 SetSecondsVariationParameter(params, "monitoring_interval", 154 SetSecondsVariationParameter(params, "monitoring_interval",
149 &monitoring_interval_); 155 &monitoring_interval_);
156
157 const std::string simulate_option =
158 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
159 "simulate-memory-pressure");
160 if (simulate_option == "warning") {
161 max_available_ = expected_renderer_size_ * new_renderers_until_warning_;
162 } else if (simulate_option == "critical") {
163 max_available_ = expected_renderer_size_ * new_renderers_until_critical_;
164 } else {
165 max_available_ = INT_MAX;
166 }
150 } 167 }
151 168
152 bool MemoryConditionObserver::ValidateParameters() { 169 bool MemoryConditionObserver::ValidateParameters() {
153 return (new_renderers_until_warning_ > new_renderers_until_critical_) && 170 return (new_renderers_until_warning_ > new_renderers_until_critical_) &&
154 (new_renderers_back_to_normal_ > new_renderers_back_to_warning_) && 171 (new_renderers_back_to_normal_ > new_renderers_back_to_warning_) &&
155 (new_renderers_back_to_normal_ > new_renderers_until_warning_) && 172 (new_renderers_back_to_normal_ > new_renderers_until_warning_) &&
156 (new_renderers_back_to_warning_ > new_renderers_until_critical_); 173 (new_renderers_back_to_warning_ > new_renderers_until_critical_);
157 } 174 }
158 175
159 } // namespace content 176 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/memory/memory_condition_observer.h ('k') | content/browser/memory/memory_coordinator_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698