Update V8 to version 4.6.72.
[chromium-blink-merge.git] / content / common / host_discardable_shared_memory_manager.cc
blob7590c1f9bb4c6848f950a21da491494ab04ea0d8
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
3 // found in the LICENSE file.
5 #include "content/common/host_discardable_shared_memory_manager.h"
7 #include <algorithm>
9 #include "base/atomic_sequence_num.h"
10 #include "base/bind.h"
11 #include "base/callback.h"
12 #include "base/debug/crash_logging.h"
13 #include "base/lazy_instance.h"
14 #include "base/memory/discardable_memory.h"
15 #include "base/numerics/safe_math.h"
16 #include "base/strings/string_number_conversions.h"
17 #include "base/strings/stringprintf.h"
18 #include "base/sys_info.h"
19 #include "base/thread_task_runner_handle.h"
20 #include "base/trace_event/memory_allocator_dump.h"
21 #include "base/trace_event/memory_dump_manager.h"
22 #include "base/trace_event/process_memory_dump.h"
23 #include "base/trace_event/trace_event.h"
24 #include "content/common/child_process_host_impl.h"
25 #include "content/common/discardable_shared_memory_heap.h"
26 #include "content/public/common/child_process_host.h"
28 namespace content {
29 namespace {
31 class DiscardableMemoryImpl : public base::DiscardableMemory {
32 public:
33 DiscardableMemoryImpl(scoped_ptr<base::DiscardableSharedMemory> shared_memory,
34 const base::Closure& deleted_callback)
35 : shared_memory_(shared_memory.Pass()),
36 deleted_callback_(deleted_callback),
37 is_locked_(true) {}
39 ~DiscardableMemoryImpl() override {
40 if (is_locked_)
41 shared_memory_->Unlock(0, 0);
43 deleted_callback_.Run();
46 // Overridden from base::DiscardableMemory:
47 bool Lock() override {
48 DCHECK(!is_locked_);
50 if (shared_memory_->Lock(0, 0) != base::DiscardableSharedMemory::SUCCESS)
51 return false;
53 is_locked_ = true;
54 return true;
56 void Unlock() override {
57 DCHECK(is_locked_);
59 shared_memory_->Unlock(0, 0);
60 is_locked_ = false;
62 void* data() const override {
63 DCHECK(is_locked_);
64 return shared_memory_->memory();
67 private:
68 scoped_ptr<base::DiscardableSharedMemory> shared_memory_;
69 const base::Closure deleted_callback_;
70 bool is_locked_;
72 DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryImpl);
75 base::LazyInstance<HostDiscardableSharedMemoryManager>
76 g_discardable_shared_memory_manager = LAZY_INSTANCE_INITIALIZER;
78 #if defined(OS_ANDROID)
79 // Limits the number of FDs used to 32, assuming a 4MB allocation size.
80 const int64_t kMaxDefaultMemoryLimit = 128 * 1024 * 1024;
81 #else
82 const int64_t kMaxDefaultMemoryLimit = 512 * 1024 * 1024;
83 #endif
85 const int kEnforceMemoryPolicyDelayMs = 1000;
87 // Global atomic to generate unique discardable shared memory IDs.
88 base::StaticAtomicSequenceNumber g_next_discardable_shared_memory_id;
90 } // namespace
92 HostDiscardableSharedMemoryManager::MemorySegment::MemorySegment(
93 scoped_ptr<base::DiscardableSharedMemory> memory)
94 : memory_(memory.Pass()) {
97 HostDiscardableSharedMemoryManager::MemorySegment::~MemorySegment() {
100 HostDiscardableSharedMemoryManager::HostDiscardableSharedMemoryManager()
101 : memory_limit_(
102 // Allow 25% of physical memory to be used for discardable memory.
103 std::min(base::SysInfo::AmountOfPhysicalMemory() / 4,
104 base::SysInfo::IsLowEndDevice()
106 // Use 1/8th of discardable memory on low-end devices.
107 kMaxDefaultMemoryLimit / 8
108 : kMaxDefaultMemoryLimit)),
109 bytes_allocated_(0),
110 memory_pressure_listener_(new base::MemoryPressureListener(
111 base::Bind(&HostDiscardableSharedMemoryManager::OnMemoryPressure,
112 base::Unretained(this)))),
113 enforce_memory_policy_pending_(false),
114 weak_ptr_factory_(this) {
115 DCHECK_NE(memory_limit_, 0u);
116 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider(
117 this);
120 HostDiscardableSharedMemoryManager::~HostDiscardableSharedMemoryManager() {
121 base::trace_event::MemoryDumpManager::GetInstance()->UnregisterDumpProvider(
122 this);
125 HostDiscardableSharedMemoryManager*
126 HostDiscardableSharedMemoryManager::current() {
127 return g_discardable_shared_memory_manager.Pointer();
130 scoped_ptr<base::DiscardableMemory>
131 HostDiscardableSharedMemoryManager::AllocateLockedDiscardableMemory(
132 size_t size) {
133 DiscardableSharedMemoryId new_id =
134 g_next_discardable_shared_memory_id.GetNext();
135 base::ProcessHandle current_process_handle = base::GetCurrentProcessHandle();
137 // Note: Use DiscardableSharedMemoryHeap for in-process allocation
138 // of discardable memory if the cost of each allocation is too high.
139 base::SharedMemoryHandle handle;
140 AllocateLockedDiscardableSharedMemory(current_process_handle,
141 ChildProcessHost::kInvalidUniqueID,
142 size, new_id, &handle);
143 CHECK(base::SharedMemory::IsHandleValid(handle));
144 scoped_ptr<base::DiscardableSharedMemory> memory(
145 new base::DiscardableSharedMemory(handle));
146 CHECK(memory->Map(size));
147 // Close file descriptor to avoid running out.
148 memory->Close();
149 return make_scoped_ptr(new DiscardableMemoryImpl(
150 memory.Pass(),
151 base::Bind(
152 &HostDiscardableSharedMemoryManager::DeletedDiscardableSharedMemory,
153 base::Unretained(this), new_id, ChildProcessHost::kInvalidUniqueID)));
156 bool HostDiscardableSharedMemoryManager::OnMemoryDump(
157 const base::trace_event::MemoryDumpArgs& args,
158 base::trace_event::ProcessMemoryDump* pmd) {
159 base::AutoLock lock(lock_);
160 for (const auto& process_entry : processes_) {
161 const int child_process_id = process_entry.first;
162 const MemorySegmentMap& process_segments = process_entry.second;
163 for (const auto& segment_entry : process_segments) {
164 const int segment_id = segment_entry.first;
165 const MemorySegment* segment = segment_entry.second.get();
166 std::string dump_name = base::StringPrintf(
167 "discardable/process_%x/segment_%d", child_process_id, segment_id);
168 base::trace_event::MemoryAllocatorDump* dump =
169 pmd->CreateAllocatorDump(dump_name);
170 dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize,
171 base::trace_event::MemoryAllocatorDump::kUnitsBytes,
172 segment->memory()->mapped_size());
174 // Create the cross-process ownership edge. If the child creates a
175 // corresponding dump for the same segment, this will avoid to
176 // double-count them in tracing. If, instead, no other process will emit a
177 // dump with the same guid, the segment will be accounted to the browser.
178 const uint64 child_tracing_process_id =
179 ChildProcessHostImpl::ChildProcessUniqueIdToTracingProcessId(
180 child_process_id);
181 base::trace_event::MemoryAllocatorDumpGuid shared_segment_guid =
182 DiscardableSharedMemoryHeap::GetSegmentGUIDForTracing(
183 child_tracing_process_id, segment_id);
184 pmd->CreateSharedGlobalAllocatorDump(shared_segment_guid);
185 pmd->AddOwnershipEdge(dump->guid(), shared_segment_guid);
188 return true;
191 void HostDiscardableSharedMemoryManager::
192 AllocateLockedDiscardableSharedMemoryForChild(
193 base::ProcessHandle process_handle,
194 int child_process_id,
195 size_t size,
196 DiscardableSharedMemoryId id,
197 base::SharedMemoryHandle* shared_memory_handle) {
198 AllocateLockedDiscardableSharedMemory(process_handle, child_process_id, size,
199 id, shared_memory_handle);
202 void HostDiscardableSharedMemoryManager::ChildDeletedDiscardableSharedMemory(
203 DiscardableSharedMemoryId id,
204 int child_process_id) {
205 DeletedDiscardableSharedMemory(id, child_process_id);
208 void HostDiscardableSharedMemoryManager::ProcessRemoved(int child_process_id) {
209 base::AutoLock lock(lock_);
211 ProcessMap::iterator process_it = processes_.find(child_process_id);
212 if (process_it == processes_.end())
213 return;
215 size_t bytes_allocated_before_releasing_memory = bytes_allocated_;
217 for (auto& segment_it : process_it->second)
218 ReleaseMemory(segment_it.second->memory());
220 processes_.erase(process_it);
222 if (bytes_allocated_ != bytes_allocated_before_releasing_memory)
223 BytesAllocatedChanged(bytes_allocated_);
226 void HostDiscardableSharedMemoryManager::SetMemoryLimit(size_t limit) {
227 base::AutoLock lock(lock_);
229 memory_limit_ = limit;
230 ReduceMemoryUsageUntilWithinMemoryLimit();
233 void HostDiscardableSharedMemoryManager::EnforceMemoryPolicy() {
234 base::AutoLock lock(lock_);
236 enforce_memory_policy_pending_ = false;
237 ReduceMemoryUsageUntilWithinMemoryLimit();
240 size_t HostDiscardableSharedMemoryManager::GetBytesAllocated() {
241 base::AutoLock lock(lock_);
243 return bytes_allocated_;
246 void HostDiscardableSharedMemoryManager::AllocateLockedDiscardableSharedMemory(
247 base::ProcessHandle process_handle,
248 int client_process_id,
249 size_t size,
250 DiscardableSharedMemoryId id,
251 base::SharedMemoryHandle* shared_memory_handle) {
252 base::AutoLock lock(lock_);
254 // Make sure |id| is not already in use.
255 MemorySegmentMap& process_segments = processes_[client_process_id];
256 if (process_segments.find(id) != process_segments.end()) {
257 LOG(ERROR) << "Invalid discardable shared memory ID";
258 *shared_memory_handle = base::SharedMemory::NULLHandle();
259 return;
262 // Memory usage must be reduced to prevent the addition of |size| from
263 // taking usage above the limit. Usage should be reduced to 0 in cases
264 // where |size| is greater than the limit.
265 size_t limit = 0;
266 // Note: the actual mapped size can be larger than requested and cause
267 // |bytes_allocated_| to temporarily be larger than |memory_limit_|. The
268 // error is minimized by incrementing |bytes_allocated_| with the actual
269 // mapped size rather than |size| below.
270 if (size < memory_limit_)
271 limit = memory_limit_ - size;
273 if (bytes_allocated_ > limit)
274 ReduceMemoryUsageUntilWithinLimit(limit);
276 scoped_ptr<base::DiscardableSharedMemory> memory(
277 new base::DiscardableSharedMemory);
278 if (!memory->CreateAndMap(size)) {
279 *shared_memory_handle = base::SharedMemory::NULLHandle();
280 return;
283 if (!memory->ShareToProcess(process_handle, shared_memory_handle)) {
284 LOG(ERROR) << "Cannot share discardable memory segment";
285 *shared_memory_handle = base::SharedMemory::NULLHandle();
286 return;
289 base::CheckedNumeric<size_t> checked_bytes_allocated = bytes_allocated_;
290 checked_bytes_allocated += memory->mapped_size();
291 if (!checked_bytes_allocated.IsValid()) {
292 *shared_memory_handle = base::SharedMemory::NULLHandle();
293 return;
296 bytes_allocated_ = checked_bytes_allocated.ValueOrDie();
297 BytesAllocatedChanged(bytes_allocated_);
299 #if !defined(DISCARDABLE_SHARED_MEMORY_SHRINKING)
300 // Close file descriptor to avoid running out.
301 memory->Close();
302 #endif
304 scoped_refptr<MemorySegment> segment(new MemorySegment(memory.Pass()));
305 process_segments[id] = segment.get();
306 segments_.push_back(segment.get());
307 std::push_heap(segments_.begin(), segments_.end(), CompareMemoryUsageTime);
309 if (bytes_allocated_ > memory_limit_)
310 ScheduleEnforceMemoryPolicy();
313 void HostDiscardableSharedMemoryManager::DeletedDiscardableSharedMemory(
314 DiscardableSharedMemoryId id,
315 int client_process_id) {
316 base::AutoLock lock(lock_);
318 MemorySegmentMap& process_segments = processes_[client_process_id];
320 MemorySegmentMap::iterator segment_it = process_segments.find(id);
321 if (segment_it == process_segments.end()) {
322 LOG(ERROR) << "Invalid discardable shared memory ID";
323 return;
326 size_t bytes_allocated_before_releasing_memory = bytes_allocated_;
328 ReleaseMemory(segment_it->second->memory());
330 process_segments.erase(segment_it);
332 if (bytes_allocated_ != bytes_allocated_before_releasing_memory)
333 BytesAllocatedChanged(bytes_allocated_);
336 void HostDiscardableSharedMemoryManager::OnMemoryPressure(
337 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level) {
338 base::AutoLock lock(lock_);
340 switch (memory_pressure_level) {
341 case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE:
342 break;
343 case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE:
344 // Purge memory until usage is within half of |memory_limit_|.
345 ReduceMemoryUsageUntilWithinLimit(memory_limit_ / 2);
346 break;
347 case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL:
348 // Purge everything possible when pressure is critical.
349 ReduceMemoryUsageUntilWithinLimit(0);
350 break;
354 void
355 HostDiscardableSharedMemoryManager::ReduceMemoryUsageUntilWithinMemoryLimit() {
356 lock_.AssertAcquired();
358 if (bytes_allocated_ <= memory_limit_)
359 return;
361 ReduceMemoryUsageUntilWithinLimit(memory_limit_);
362 if (bytes_allocated_ > memory_limit_)
363 ScheduleEnforceMemoryPolicy();
366 void HostDiscardableSharedMemoryManager::ReduceMemoryUsageUntilWithinLimit(
367 size_t limit) {
368 TRACE_EVENT1("renderer_host",
369 "HostDiscardableSharedMemoryManager::"
370 "ReduceMemoryUsageUntilWithinLimit",
371 "bytes_allocated",
372 bytes_allocated_);
374 // Usage time of currently locked segments are updated to this time and
375 // we stop eviction attempts as soon as we come across a segment that we've
376 // previously tried to evict but was locked.
377 base::Time current_time = Now();
379 lock_.AssertAcquired();
380 size_t bytes_allocated_before_purging = bytes_allocated_;
381 while (!segments_.empty()) {
382 if (bytes_allocated_ <= limit)
383 break;
385 // Stop eviction attempts when the LRU segment is currently in use.
386 if (segments_.front()->memory()->last_known_usage() >= current_time)
387 break;
389 std::pop_heap(segments_.begin(), segments_.end(), CompareMemoryUsageTime);
390 scoped_refptr<MemorySegment> segment = segments_.back();
391 segments_.pop_back();
393 // Attempt to purge LRU segment. When successful, released the memory.
394 if (segment->memory()->Purge(current_time)) {
395 #if defined(DISCARDABLE_SHARED_MEMORY_SHRINKING)
396 size_t size = segment->memory()->mapped_size();
397 DCHECK_GE(bytes_allocated_, size);
398 bytes_allocated_ -= size;
399 // Shrink memory segment. This will immediately release the memory to
400 // the OS.
401 segment->memory()->Shrink();
402 DCHECK_EQ(segment->memory()->mapped_size(), 0u);
403 #endif
404 ReleaseMemory(segment->memory());
405 continue;
408 // Add memory segment (with updated usage timestamp) back on heap after
409 // failed attempt to purge it.
410 segments_.push_back(segment.get());
411 std::push_heap(segments_.begin(), segments_.end(), CompareMemoryUsageTime);
414 if (bytes_allocated_ != bytes_allocated_before_purging)
415 BytesAllocatedChanged(bytes_allocated_);
418 void HostDiscardableSharedMemoryManager::ReleaseMemory(
419 base::DiscardableSharedMemory* memory) {
420 lock_.AssertAcquired();
422 size_t size = memory->mapped_size();
423 DCHECK_GE(bytes_allocated_, size);
424 bytes_allocated_ -= size;
426 // This will unmap the memory segment and drop our reference. The result
427 // is that the memory will be released to the OS if the child process is
428 // no longer referencing it.
429 // Note: We intentionally leave the segment in the |segments| vector to
430 // avoid reconstructing the heap. The element will be removed from the heap
431 // when its last usage time is older than all other segments.
432 memory->Unmap();
433 memory->Close();
436 void HostDiscardableSharedMemoryManager::BytesAllocatedChanged(
437 size_t new_bytes_allocated) const {
438 TRACE_COUNTER1("renderer_host", "TotalDiscardableMemoryUsage",
439 new_bytes_allocated);
441 static const char kTotalDiscardableMemoryAllocatedKey[] =
442 "total-discardable-memory-allocated";
443 base::debug::SetCrashKeyValue(kTotalDiscardableMemoryAllocatedKey,
444 base::Uint64ToString(new_bytes_allocated));
447 base::Time HostDiscardableSharedMemoryManager::Now() const {
448 return base::Time::Now();
451 void HostDiscardableSharedMemoryManager::ScheduleEnforceMemoryPolicy() {
452 lock_.AssertAcquired();
454 if (enforce_memory_policy_pending_)
455 return;
457 enforce_memory_policy_pending_ = true;
458 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
459 FROM_HERE,
460 base::Bind(&HostDiscardableSharedMemoryManager::EnforceMemoryPolicy,
461 weak_ptr_factory_.GetWeakPtr()),
462 base::TimeDelta::FromMilliseconds(kEnforceMemoryPolicyDelayMs));
465 } // namespace content