| 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_coordinator_impl.h" | 5 #include "content/browser/memory/memory_coordinator_impl.h" |
| 6 | 6 |
| 7 #include "base/metrics/histogram_macros.h" | 7 #include "base/metrics/histogram_macros.h" |
| 8 #include "base/process/process_metrics.h" | 8 #include "base/process/process_metrics.h" |
| 9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/threading/thread_task_runner_handle.h" | 10 #include "base/threading/thread_task_runner_handle.h" |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 185 void MemoryCoordinatorImpl::OnChildAdded(int render_process_id) { | 185 void MemoryCoordinatorImpl::OnChildAdded(int render_process_id) { |
| 186 // Populate the global state as an initial state of a newly created process. | 186 // Populate the global state as an initial state of a newly created process. |
| 187 auto new_state = ToMojomMemoryState(GetGlobalMemoryState()); | 187 auto new_state = ToMojomMemoryState(GetGlobalMemoryState()); |
| 188 SetChildMemoryState(render_process_id, new_state); | 188 SetChildMemoryState(render_process_id, new_state); |
| 189 } | 189 } |
| 190 | 190 |
| 191 base::MemoryState MemoryCoordinatorImpl::GetGlobalMemoryState() const { | 191 base::MemoryState MemoryCoordinatorImpl::GetGlobalMemoryState() const { |
| 192 return current_state_; | 192 return current_state_; |
| 193 } | 193 } |
| 194 | 194 |
| 195 base::MemoryState MemoryCoordinatorImpl::GetCurrentMemoryState() const { | 195 base::WeakPtr<base::MemoryCoordinatorInterface> |
| 196 MemoryCoordinatorImpl::GetWeakPtr() { |
| 197 return weak_ptr_factory_.GetWeakPtr(); |
| 198 } |
| 199 |
| 200 base::MemoryState MemoryCoordinatorImpl::GetLocalMemoryState() { |
| 196 // SUSPENDED state may not make sense to the browser process. Use THROTTLED | 201 // SUSPENDED state may not make sense to the browser process. Use THROTTLED |
| 197 // instead when the global state is SUSPENDED. | 202 // instead when the global state is SUSPENDED. |
| 198 // TODO(bashi): Maybe worth considering another state for the browser. | 203 // TODO(bashi): Maybe worth considering another state for the browser. |
| 199 return current_state_ == MemoryState::SUSPENDED ? MemoryState::THROTTLED | 204 return current_state_ == MemoryState::SUSPENDED ? MemoryState::THROTTLED |
| 200 : current_state_; | 205 : current_state_; |
| 201 } | 206 } |
| 202 | 207 |
| 203 void MemoryCoordinatorImpl::SetCurrentMemoryStateForTesting( | 208 void MemoryCoordinatorImpl::SetMemoryStateForTesting( |
| 204 base::MemoryState memory_state) { | 209 base::MemoryState memory_state) { |
| 205 // This changes the current state temporariy for testing. The state will be | 210 // This changes the current state temporariy for testing. The state will be |
| 206 // updated 1 minute later. | 211 // updated 1 minute later. |
| 207 ForceSetGlobalState(memory_state, base::TimeDelta::FromMinutes(1)); | 212 ForceSetGlobalState(memory_state, base::TimeDelta::FromMinutes(1)); |
| 208 } | 213 } |
| 209 | 214 |
| 210 void MemoryCoordinatorImpl::ForceSetGlobalState(base::MemoryState new_state, | 215 void MemoryCoordinatorImpl::ForceSetGlobalState(base::MemoryState new_state, |
| 211 base::TimeDelta duration) { | 216 base::TimeDelta duration) { |
| 212 DCHECK(new_state != MemoryState::UNKNOWN); | 217 DCHECK(new_state != MemoryState::UNKNOWN); |
| 213 ChangeStateIfNeeded(current_state_, new_state); | 218 ChangeStateIfNeeded(current_state_, new_state); |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 294 void MemoryCoordinatorImpl::UpdateState() { | 299 void MemoryCoordinatorImpl::UpdateState() { |
| 295 MemoryState next_state = CalculateNextState(); | 300 MemoryState next_state = CalculateNextState(); |
| 296 if (ChangeStateIfNeeded(current_state_, next_state)) { | 301 if (ChangeStateIfNeeded(current_state_, next_state)) { |
| 297 ScheduleUpdateState(minimum_transition_period_); | 302 ScheduleUpdateState(minimum_transition_period_); |
| 298 } else { | 303 } else { |
| 299 ScheduleUpdateState(monitoring_interval_); | 304 ScheduleUpdateState(monitoring_interval_); |
| 300 } | 305 } |
| 301 } | 306 } |
| 302 | 307 |
| 303 void MemoryCoordinatorImpl::NotifyStateToClients() { | 308 void MemoryCoordinatorImpl::NotifyStateToClients() { |
| 304 auto state = GetCurrentMemoryState(); | 309 auto state = GetLocalMemoryState(); |
| 305 base::MemoryCoordinatorClientRegistry::GetInstance()->Notify(state); | 310 base::MemoryCoordinatorClientRegistry::GetInstance()->Notify(state); |
| 306 } | 311 } |
| 307 | 312 |
| 308 void MemoryCoordinatorImpl::NotifyStateToChildren() { | 313 void MemoryCoordinatorImpl::NotifyStateToChildren() { |
| 309 auto mojo_state = ToMojomMemoryState(current_state_); | 314 auto mojo_state = ToMojomMemoryState(current_state_); |
| 310 // It's OK to call SetChildMemoryState() unconditionally because it checks | 315 // It's OK to call SetChildMemoryState() unconditionally because it checks |
| 311 // whether this state transition is valid. | 316 // whether this state transition is valid. |
| 312 for (auto& iter : children()) | 317 for (auto& iter : children()) |
| 313 SetChildMemoryState(iter.first, mojo_state); | 318 SetChildMemoryState(iter.first, mojo_state); |
| 314 } | 319 } |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 381 } | 386 } |
| 382 | 387 |
| 383 bool MemoryCoordinatorImpl::ValidateParameters() { | 388 bool MemoryCoordinatorImpl::ValidateParameters() { |
| 384 return (new_renderers_until_throttled_ > new_renderers_until_suspended_) && | 389 return (new_renderers_until_throttled_ > new_renderers_until_suspended_) && |
| 385 (new_renderers_back_to_normal_ > new_renderers_back_to_throttled_) && | 390 (new_renderers_back_to_normal_ > new_renderers_back_to_throttled_) && |
| 386 (new_renderers_back_to_normal_ > new_renderers_until_throttled_) && | 391 (new_renderers_back_to_normal_ > new_renderers_until_throttled_) && |
| 387 (new_renderers_back_to_throttled_ > new_renderers_until_suspended_); | 392 (new_renderers_back_to_throttled_ > new_renderers_until_suspended_); |
| 388 } | 393 } |
| 389 | 394 |
| 390 } // namespace content | 395 } // namespace content |
| OLD | NEW |