OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/common/host_discardable_shared_memory_manager.h" | 5 #include "content/common/host_discardable_shared_memory_manager.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/atomic_sequence_num.h" | 10 #include "base/atomic_sequence_num.h" |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/callback.h" | 12 #include "base/callback.h" |
13 #include "base/debug/crash_logging.h" | 13 #include "base/debug/crash_logging.h" |
14 #include "base/lazy_instance.h" | 14 #include "base/lazy_instance.h" |
15 #include "base/macros.h" | 15 #include "base/macros.h" |
16 #include "base/memory/discardable_memory.h" | 16 #include "base/memory/discardable_memory.h" |
| 17 #include "base/memory/memory_coordinator_client_registry.h" |
17 #include "base/memory/ptr_util.h" | 18 #include "base/memory/ptr_util.h" |
18 #include "base/numerics/safe_math.h" | 19 #include "base/numerics/safe_math.h" |
19 #include "base/process/memory.h" | 20 #include "base/process/memory.h" |
20 #include "base/strings/string_number_conversions.h" | 21 #include "base/strings/string_number_conversions.h" |
21 #include "base/strings/stringprintf.h" | 22 #include "base/strings/stringprintf.h" |
22 #include "base/sys_info.h" | 23 #include "base/sys_info.h" |
23 #include "base/threading/thread_task_runner_handle.h" | 24 #include "base/threading/thread_task_runner_handle.h" |
24 #include "base/trace_event/memory_allocator_dump.h" | 25 #include "base/trace_event/memory_allocator_dump.h" |
25 #include "base/trace_event/memory_dump_manager.h" | 26 #include "base/trace_event/memory_dump_manager.h" |
26 #include "base/trace_event/process_memory_dump.h" | 27 #include "base/trace_event/process_memory_dump.h" |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 enforce_memory_policy_task_runner_(base::ThreadTaskRunnerHandle::Get()), | 173 enforce_memory_policy_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
173 enforce_memory_policy_pending_(false), | 174 enforce_memory_policy_pending_(false), |
174 weak_ptr_factory_(this) { | 175 weak_ptr_factory_(this) { |
175 DCHECK_NE(memory_limit_, 0u); | 176 DCHECK_NE(memory_limit_, 0u); |
176 enforce_memory_policy_callback_ = | 177 enforce_memory_policy_callback_ = |
177 base::Bind(&HostDiscardableSharedMemoryManager::EnforceMemoryPolicy, | 178 base::Bind(&HostDiscardableSharedMemoryManager::EnforceMemoryPolicy, |
178 weak_ptr_factory_.GetWeakPtr()); | 179 weak_ptr_factory_.GetWeakPtr()); |
179 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider( | 180 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider( |
180 this, "HostDiscardableSharedMemoryManager", | 181 this, "HostDiscardableSharedMemoryManager", |
181 base::ThreadTaskRunnerHandle::Get()); | 182 base::ThreadTaskRunnerHandle::Get()); |
| 183 base::MemoryCoordinatorClientRegistry::GetInstance()->Register(this); |
182 } | 184 } |
183 | 185 |
184 HostDiscardableSharedMemoryManager::~HostDiscardableSharedMemoryManager() { | 186 HostDiscardableSharedMemoryManager::~HostDiscardableSharedMemoryManager() { |
185 base::trace_event::MemoryDumpManager::GetInstance()->UnregisterDumpProvider( | 187 base::trace_event::MemoryDumpManager::GetInstance()->UnregisterDumpProvider( |
186 this); | 188 this); |
187 } | 189 } |
188 | 190 |
189 HostDiscardableSharedMemoryManager* | 191 HostDiscardableSharedMemoryManager* |
190 HostDiscardableSharedMemoryManager::current() { | 192 HostDiscardableSharedMemoryManager::current() { |
191 return g_discardable_shared_memory_manager.Pointer(); | 193 return g_discardable_shared_memory_manager.Pointer(); |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
339 enforce_memory_policy_pending_ = false; | 341 enforce_memory_policy_pending_ = false; |
340 ReduceMemoryUsageUntilWithinMemoryLimit(); | 342 ReduceMemoryUsageUntilWithinMemoryLimit(); |
341 } | 343 } |
342 | 344 |
343 size_t HostDiscardableSharedMemoryManager::GetBytesAllocated() { | 345 size_t HostDiscardableSharedMemoryManager::GetBytesAllocated() { |
344 base::AutoLock lock(lock_); | 346 base::AutoLock lock(lock_); |
345 | 347 |
346 return bytes_allocated_; | 348 return bytes_allocated_; |
347 } | 349 } |
348 | 350 |
| 351 void HostDiscardableSharedMemoryManager::OnMemoryStateChange( |
| 352 base::MemoryState state) { |
| 353 switch (state) { |
| 354 case base::MemoryState::NORMAL: |
| 355 SetMemoryLimit(GetDefaultMemoryLimit()); |
| 356 break; |
| 357 case base::MemoryState::THROTTLED: |
| 358 SetMemoryLimit(0); |
| 359 break; |
| 360 case base::MemoryState::SUSPENDED: |
| 361 // Note that SUSPENDED never occurs in the main browser process so far. |
| 362 // Fall through. |
| 363 case base::MemoryState::UNKNOWN: |
| 364 NOTREACHED(); |
| 365 break; |
| 366 } |
| 367 } |
| 368 |
349 void HostDiscardableSharedMemoryManager::AllocateLockedDiscardableSharedMemory( | 369 void HostDiscardableSharedMemoryManager::AllocateLockedDiscardableSharedMemory( |
350 base::ProcessHandle process_handle, | 370 base::ProcessHandle process_handle, |
351 int client_process_id, | 371 int client_process_id, |
352 size_t size, | 372 size_t size, |
353 DiscardableSharedMemoryId id, | 373 DiscardableSharedMemoryId id, |
354 base::SharedMemoryHandle* shared_memory_handle) { | 374 base::SharedMemoryHandle* shared_memory_handle) { |
355 base::AutoLock lock(lock_); | 375 base::AutoLock lock(lock_); |
356 | 376 |
357 // Make sure |id| is not already in use. | 377 // Make sure |id| is not already in use. |
358 MemorySegmentMap& process_segments = processes_[client_process_id]; | 378 MemorySegmentMap& process_segments = processes_[client_process_id]; |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
550 return; | 570 return; |
551 | 571 |
552 enforce_memory_policy_pending_ = true; | 572 enforce_memory_policy_pending_ = true; |
553 DCHECK(enforce_memory_policy_task_runner_); | 573 DCHECK(enforce_memory_policy_task_runner_); |
554 enforce_memory_policy_task_runner_->PostDelayedTask( | 574 enforce_memory_policy_task_runner_->PostDelayedTask( |
555 FROM_HERE, enforce_memory_policy_callback_, | 575 FROM_HERE, enforce_memory_policy_callback_, |
556 base::TimeDelta::FromMilliseconds(kEnforceMemoryPolicyDelayMs)); | 576 base::TimeDelta::FromMilliseconds(kEnforceMemoryPolicyDelayMs)); |
557 } | 577 } |
558 | 578 |
559 } // namespace content | 579 } // namespace content |
OLD | NEW |