| 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 | 9 |
| 9 #include "base/atomic_sequence_num.h" | 10 #include "base/atomic_sequence_num.h" |
| 10 #include "base/bind.h" | 11 #include "base/bind.h" |
| 11 #include "base/callback.h" | 12 #include "base/callback.h" |
| 12 #include "base/debug/crash_logging.h" | 13 #include "base/debug/crash_logging.h" |
| 13 #include "base/lazy_instance.h" | 14 #include "base/lazy_instance.h" |
| 14 #include "base/macros.h" | 15 #include "base/macros.h" |
| 15 #include "base/memory/discardable_memory.h" | 16 #include "base/memory/discardable_memory.h" |
| 16 #include "base/numerics/safe_math.h" | 17 #include "base/numerics/safe_math.h" |
| 17 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
| 18 #include "base/strings/stringprintf.h" | 19 #include "base/strings/stringprintf.h" |
| 19 #include "base/sys_info.h" | 20 #include "base/sys_info.h" |
| 20 #include "base/thread_task_runner_handle.h" | 21 #include "base/thread_task_runner_handle.h" |
| 21 #include "base/trace_event/memory_allocator_dump.h" | 22 #include "base/trace_event/memory_allocator_dump.h" |
| 22 #include "base/trace_event/memory_dump_manager.h" | 23 #include "base/trace_event/memory_dump_manager.h" |
| 23 #include "base/trace_event/process_memory_dump.h" | 24 #include "base/trace_event/process_memory_dump.h" |
| 24 #include "base/trace_event/trace_event.h" | 25 #include "base/trace_event/trace_event.h" |
| 25 #include "build/build_config.h" | 26 #include "build/build_config.h" |
| 26 #include "content/common/child_process_host_impl.h" | 27 #include "content/common/child_process_host_impl.h" |
| 27 #include "content/common/discardable_shared_memory_heap.h" | 28 #include "content/common/discardable_shared_memory_heap.h" |
| 28 #include "content/public/common/child_process_host.h" | 29 #include "content/public/common/child_process_host.h" |
| 29 | 30 |
| 30 namespace content { | 31 namespace content { |
| 31 namespace { | 32 namespace { |
| 32 | 33 |
| 33 class DiscardableMemoryImpl : public base::DiscardableMemory { | 34 class DiscardableMemoryImpl : public base::DiscardableMemory { |
| 34 public: | 35 public: |
| 35 DiscardableMemoryImpl(scoped_ptr<base::DiscardableSharedMemory> shared_memory, | 36 DiscardableMemoryImpl(scoped_ptr<base::DiscardableSharedMemory> shared_memory, |
| 36 const base::Closure& deleted_callback) | 37 const base::Closure& deleted_callback) |
| 37 : shared_memory_(shared_memory.Pass()), | 38 : shared_memory_(std::move(shared_memory)), |
| 38 deleted_callback_(deleted_callback), | 39 deleted_callback_(deleted_callback), |
| 39 is_locked_(true) {} | 40 is_locked_(true) {} |
| 40 | 41 |
| 41 ~DiscardableMemoryImpl() override { | 42 ~DiscardableMemoryImpl() override { |
| 42 if (is_locked_) | 43 if (is_locked_) |
| 43 shared_memory_->Unlock(0, 0); | 44 shared_memory_->Unlock(0, 0); |
| 44 | 45 |
| 45 deleted_callback_.Run(); | 46 deleted_callback_.Run(); |
| 46 } | 47 } |
| 47 | 48 |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 | 100 |
| 100 const int kEnforceMemoryPolicyDelayMs = 1000; | 101 const int kEnforceMemoryPolicyDelayMs = 1000; |
| 101 | 102 |
| 102 // Global atomic to generate unique discardable shared memory IDs. | 103 // Global atomic to generate unique discardable shared memory IDs. |
| 103 base::StaticAtomicSequenceNumber g_next_discardable_shared_memory_id; | 104 base::StaticAtomicSequenceNumber g_next_discardable_shared_memory_id; |
| 104 | 105 |
| 105 } // namespace | 106 } // namespace |
| 106 | 107 |
| 107 HostDiscardableSharedMemoryManager::MemorySegment::MemorySegment( | 108 HostDiscardableSharedMemoryManager::MemorySegment::MemorySegment( |
| 108 scoped_ptr<base::DiscardableSharedMemory> memory) | 109 scoped_ptr<base::DiscardableSharedMemory> memory) |
| 109 : memory_(memory.Pass()) { | 110 : memory_(std::move(memory)) {} |
| 110 } | |
| 111 | 111 |
| 112 HostDiscardableSharedMemoryManager::MemorySegment::~MemorySegment() { | 112 HostDiscardableSharedMemoryManager::MemorySegment::~MemorySegment() { |
| 113 } | 113 } |
| 114 | 114 |
| 115 HostDiscardableSharedMemoryManager::HostDiscardableSharedMemoryManager() | 115 HostDiscardableSharedMemoryManager::HostDiscardableSharedMemoryManager() |
| 116 : memory_limit_( | 116 : memory_limit_( |
| 117 // Allow 25% of physical memory to be used for discardable memory. | 117 // Allow 25% of physical memory to be used for discardable memory. |
| 118 std::min(base::SysInfo::AmountOfPhysicalMemory() / 4, | 118 std::min(base::SysInfo::AmountOfPhysicalMemory() / 4, |
| 119 base::SysInfo::IsLowEndDevice() | 119 base::SysInfo::IsLowEndDevice() |
| 120 ? | 120 ? |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 AllocateLockedDiscardableSharedMemory(current_process_handle, | 160 AllocateLockedDiscardableSharedMemory(current_process_handle, |
| 161 ChildProcessHost::kInvalidUniqueID, | 161 ChildProcessHost::kInvalidUniqueID, |
| 162 size, new_id, &handle); | 162 size, new_id, &handle); |
| 163 CHECK(base::SharedMemory::IsHandleValid(handle)); | 163 CHECK(base::SharedMemory::IsHandleValid(handle)); |
| 164 scoped_ptr<base::DiscardableSharedMemory> memory( | 164 scoped_ptr<base::DiscardableSharedMemory> memory( |
| 165 new base::DiscardableSharedMemory(handle)); | 165 new base::DiscardableSharedMemory(handle)); |
| 166 CHECK(memory->Map(size)); | 166 CHECK(memory->Map(size)); |
| 167 // Close file descriptor to avoid running out. | 167 // Close file descriptor to avoid running out. |
| 168 memory->Close(); | 168 memory->Close(); |
| 169 return make_scoped_ptr(new DiscardableMemoryImpl( | 169 return make_scoped_ptr(new DiscardableMemoryImpl( |
| 170 memory.Pass(), | 170 std::move(memory), |
| 171 base::Bind( | 171 base::Bind( |
| 172 &HostDiscardableSharedMemoryManager::DeletedDiscardableSharedMemory, | 172 &HostDiscardableSharedMemoryManager::DeletedDiscardableSharedMemory, |
| 173 base::Unretained(this), new_id, ChildProcessHost::kInvalidUniqueID))); | 173 base::Unretained(this), new_id, ChildProcessHost::kInvalidUniqueID))); |
| 174 } | 174 } |
| 175 | 175 |
| 176 bool HostDiscardableSharedMemoryManager::OnMemoryDump( | 176 bool HostDiscardableSharedMemoryManager::OnMemoryDump( |
| 177 const base::trace_event::MemoryDumpArgs& args, | 177 const base::trace_event::MemoryDumpArgs& args, |
| 178 base::trace_event::ProcessMemoryDump* pmd) { | 178 base::trace_event::ProcessMemoryDump* pmd) { |
| 179 base::AutoLock lock(lock_); | 179 base::AutoLock lock(lock_); |
| 180 for (const auto& process_entry : processes_) { | 180 for (const auto& process_entry : processes_) { |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 339 base::CheckedNumeric<size_t> checked_bytes_allocated = bytes_allocated_; | 339 base::CheckedNumeric<size_t> checked_bytes_allocated = bytes_allocated_; |
| 340 checked_bytes_allocated += memory->mapped_size(); | 340 checked_bytes_allocated += memory->mapped_size(); |
| 341 if (!checked_bytes_allocated.IsValid()) { | 341 if (!checked_bytes_allocated.IsValid()) { |
| 342 *shared_memory_handle = base::SharedMemory::NULLHandle(); | 342 *shared_memory_handle = base::SharedMemory::NULLHandle(); |
| 343 return; | 343 return; |
| 344 } | 344 } |
| 345 | 345 |
| 346 bytes_allocated_ = checked_bytes_allocated.ValueOrDie(); | 346 bytes_allocated_ = checked_bytes_allocated.ValueOrDie(); |
| 347 BytesAllocatedChanged(bytes_allocated_); | 347 BytesAllocatedChanged(bytes_allocated_); |
| 348 | 348 |
| 349 scoped_refptr<MemorySegment> segment(new MemorySegment(memory.Pass())); | 349 scoped_refptr<MemorySegment> segment(new MemorySegment(std::move(memory))); |
| 350 process_segments[id] = segment.get(); | 350 process_segments[id] = segment.get(); |
| 351 segments_.push_back(segment.get()); | 351 segments_.push_back(segment.get()); |
| 352 std::push_heap(segments_.begin(), segments_.end(), CompareMemoryUsageTime); | 352 std::push_heap(segments_.begin(), segments_.end(), CompareMemoryUsageTime); |
| 353 | 353 |
| 354 if (bytes_allocated_ > memory_limit_) | 354 if (bytes_allocated_ > memory_limit_) |
| 355 ScheduleEnforceMemoryPolicy(); | 355 ScheduleEnforceMemoryPolicy(); |
| 356 } | 356 } |
| 357 | 357 |
| 358 void HostDiscardableSharedMemoryManager::DeletedDiscardableSharedMemory( | 358 void HostDiscardableSharedMemoryManager::DeletedDiscardableSharedMemory( |
| 359 DiscardableSharedMemoryId id, | 359 DiscardableSharedMemoryId id, |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 494 return; | 494 return; |
| 495 | 495 |
| 496 enforce_memory_policy_pending_ = true; | 496 enforce_memory_policy_pending_ = true; |
| 497 DCHECK(enforce_memory_policy_task_runner_); | 497 DCHECK(enforce_memory_policy_task_runner_); |
| 498 enforce_memory_policy_task_runner_->PostDelayedTask( | 498 enforce_memory_policy_task_runner_->PostDelayedTask( |
| 499 FROM_HERE, enforce_memory_policy_callback_, | 499 FROM_HERE, enforce_memory_policy_callback_, |
| 500 base::TimeDelta::FromMilliseconds(kEnforceMemoryPolicyDelayMs)); | 500 base::TimeDelta::FromMilliseconds(kEnforceMemoryPolicyDelayMs)); |
| 501 } | 501 } |
| 502 | 502 |
| 503 } // namespace content | 503 } // namespace content |
| OLD | NEW |