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

Side by Side Diff: content/common/host_discardable_shared_memory_manager.cc

Issue 1146103002: content: Close in-process discardable memory segments. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix comment typo Created 5 years, 7 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
« no previous file with comments | « base/memory/discardable_shared_memory_unittest.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 8
9 #include "base/atomic_sequence_num.h" 9 #include "base/atomic_sequence_num.h"
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
116 116
117 // Note: Use DiscardableSharedMemoryHeap for in-process allocation 117 // Note: Use DiscardableSharedMemoryHeap for in-process allocation
118 // of discardable memory if the cost of each allocation is too high. 118 // of discardable memory if the cost of each allocation is too high.
119 base::SharedMemoryHandle handle; 119 base::SharedMemoryHandle handle;
120 AllocateLockedDiscardableSharedMemory(current_process_handle, size, new_id, 120 AllocateLockedDiscardableSharedMemory(current_process_handle, size, new_id,
121 &handle); 121 &handle);
122 CHECK(base::SharedMemory::IsHandleValid(handle)); 122 CHECK(base::SharedMemory::IsHandleValid(handle));
123 scoped_ptr<base::DiscardableSharedMemory> memory( 123 scoped_ptr<base::DiscardableSharedMemory> memory(
124 new base::DiscardableSharedMemory(handle)); 124 new base::DiscardableSharedMemory(handle));
125 CHECK(memory->Map(size)); 125 CHECK(memory->Map(size));
126 // Close file descriptor to avoid running out.
127 memory->Close();
126 return make_scoped_ptr(new DiscardableMemoryImpl( 128 return make_scoped_ptr(new DiscardableMemoryImpl(
127 memory.Pass(), 129 memory.Pass(),
128 base::Bind( 130 base::Bind(
129 &HostDiscardableSharedMemoryManager::DeletedDiscardableSharedMemory, 131 &HostDiscardableSharedMemoryManager::DeletedDiscardableSharedMemory,
130 base::Unretained(this), new_id, current_process_handle))); 132 base::Unretained(this), new_id, current_process_handle)));
131 } 133 }
132 134
133 void HostDiscardableSharedMemoryManager:: 135 void HostDiscardableSharedMemoryManager::
134 AllocateLockedDiscardableSharedMemoryForChild( 136 AllocateLockedDiscardableSharedMemoryForChild(
135 base::ProcessHandle process_handle, 137 base::ProcessHandle process_handle,
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
230 base::CheckedNumeric<size_t> checked_bytes_allocated = bytes_allocated_; 232 base::CheckedNumeric<size_t> checked_bytes_allocated = bytes_allocated_;
231 checked_bytes_allocated += memory->mapped_size(); 233 checked_bytes_allocated += memory->mapped_size();
232 if (!checked_bytes_allocated.IsValid()) { 234 if (!checked_bytes_allocated.IsValid()) {
233 *shared_memory_handle = base::SharedMemory::NULLHandle(); 235 *shared_memory_handle = base::SharedMemory::NULLHandle();
234 return; 236 return;
235 } 237 }
236 238
237 bytes_allocated_ = checked_bytes_allocated.ValueOrDie(); 239 bytes_allocated_ = checked_bytes_allocated.ValueOrDie();
238 BytesAllocatedChanged(bytes_allocated_); 240 BytesAllocatedChanged(bytes_allocated_);
239 241
242 #if !defined(DISCARDABLE_SHARED_MEMORY_SHRINKING)
243 // Close file descriptor to avoid running out.
244 memory->Close();
245 #endif
246
240 scoped_refptr<MemorySegment> segment(new MemorySegment(memory.Pass())); 247 scoped_refptr<MemorySegment> segment(new MemorySegment(memory.Pass()));
241 process_segments[id] = segment.get(); 248 process_segments[id] = segment.get();
242 segments_.push_back(segment.get()); 249 segments_.push_back(segment.get());
243 std::push_heap(segments_.begin(), segments_.end(), CompareMemoryUsageTime); 250 std::push_heap(segments_.begin(), segments_.end(), CompareMemoryUsageTime);
244 251
245 if (bytes_allocated_ > memory_limit_) 252 if (bytes_allocated_ > memory_limit_)
246 ScheduleEnforceMemoryPolicy(); 253 ScheduleEnforceMemoryPolicy();
247 } 254 }
248 255
249 void HostDiscardableSharedMemoryManager::DeletedDiscardableSharedMemory( 256 void HostDiscardableSharedMemoryManager::DeletedDiscardableSharedMemory(
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
358 size_t size = memory->mapped_size(); 365 size_t size = memory->mapped_size();
359 DCHECK_GE(bytes_allocated_, size); 366 DCHECK_GE(bytes_allocated_, size);
360 bytes_allocated_ -= size; 367 bytes_allocated_ -= size;
361 368
362 // This will unmap the memory segment and drop our reference. The result 369 // This will unmap the memory segment and drop our reference. The result
363 // is that the memory will be released to the OS if the child process is 370 // is that the memory will be released to the OS if the child process is
364 // no longer referencing it. 371 // no longer referencing it.
365 // Note: We intentionally leave the segment in the |segments| vector to 372 // Note: We intentionally leave the segment in the |segments| vector to
366 // avoid reconstructing the heap. The element will be removed from the heap 373 // avoid reconstructing the heap. The element will be removed from the heap
367 // when its last usage time is older than all other segments. 374 // when its last usage time is older than all other segments.
375 memory->Unmap();
368 memory->Close(); 376 memory->Close();
369 } 377 }
370 378
371 void HostDiscardableSharedMemoryManager::BytesAllocatedChanged( 379 void HostDiscardableSharedMemoryManager::BytesAllocatedChanged(
372 size_t new_bytes_allocated) const { 380 size_t new_bytes_allocated) const {
373 TRACE_COUNTER1("renderer_host", "TotalDiscardableMemoryUsage", 381 TRACE_COUNTER1("renderer_host", "TotalDiscardableMemoryUsage",
374 new_bytes_allocated); 382 new_bytes_allocated);
375 383
376 static const char kTotalDiscardableMemoryAllocatedKey[] = 384 static const char kTotalDiscardableMemoryAllocatedKey[] =
377 "total-discardable-memory-allocated"; 385 "total-discardable-memory-allocated";
(...skipping 13 matching lines...) Expand all
391 399
392 enforce_memory_policy_pending_ = true; 400 enforce_memory_policy_pending_ = true;
393 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( 401 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
394 FROM_HERE, 402 FROM_HERE,
395 base::Bind(&HostDiscardableSharedMemoryManager::EnforceMemoryPolicy, 403 base::Bind(&HostDiscardableSharedMemoryManager::EnforceMemoryPolicy,
396 weak_ptr_factory_.GetWeakPtr()), 404 weak_ptr_factory_.GetWeakPtr()),
397 base::TimeDelta::FromMilliseconds(kEnforceMemoryPolicyDelayMs)); 405 base::TimeDelta::FromMilliseconds(kEnforceMemoryPolicyDelayMs));
398 } 406 }
399 407
400 } // namespace content 408 } // namespace content
OLDNEW
« no previous file with comments | « base/memory/discardable_shared_memory_unittest.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698