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

Side by Side Diff: content/child/child_discardable_shared_memory_manager.cc

Issue 1009203004: content: Add DeletedDiscardableSharedMemory IPC. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: avoid updating trace counter and crash key unless bytes allocated changed Created 5 years, 9 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
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/child/child_discardable_shared_memory_manager.h" 5 #include "content/child/child_discardable_shared_memory_manager.h"
6 6
7 #include "base/atomic_sequence_num.h"
8 #include "base/bind.h"
7 #include "base/debug/crash_logging.h" 9 #include "base/debug/crash_logging.h"
8 #include "base/memory/discardable_shared_memory.h" 10 #include "base/memory/discardable_shared_memory.h"
9 #include "base/metrics/histogram.h" 11 #include "base/metrics/histogram.h"
10 #include "base/process/process_metrics.h" 12 #include "base/process/process_metrics.h"
11 #include "base/strings/string_number_conversions.h" 13 #include "base/strings/string_number_conversions.h"
12 #include "base/trace_event/trace_event.h" 14 #include "base/trace_event/trace_event.h"
13 #include "content/common/child_process_messages.h" 15 #include "content/common/child_process_messages.h"
14 16
15 namespace content { 17 namespace content {
16 namespace { 18 namespace {
17 19
18 // Default allocation size. 20 // Default allocation size.
19 #if defined(OS_ANDROID) 21 #if defined(OS_ANDROID)
20 // Larger allocation size on Android to avoid reaching the FD-limit. 22 // Larger allocation size on Android to avoid reaching the FD-limit.
21 const size_t kAllocationSize = 32 * 1024 * 1024; 23 const size_t kAllocationSize = 32 * 1024 * 1024;
22 #else 24 #else
23 const size_t kAllocationSize = 4 * 1024 * 1024; 25 const size_t kAllocationSize = 4 * 1024 * 1024;
24 #endif 26 #endif
25 27
28 // Global atomic to generate unique discardable shared memory IDs.
29 base::StaticAtomicSequenceNumber g_next_discardable_shared_memory_id;
30
26 class DiscardableMemoryShmemChunkImpl 31 class DiscardableMemoryShmemChunkImpl
27 : public base::DiscardableMemoryShmemChunk { 32 : public base::DiscardableMemoryShmemChunk {
28 public: 33 public:
29 DiscardableMemoryShmemChunkImpl( 34 DiscardableMemoryShmemChunkImpl(
30 ChildDiscardableSharedMemoryManager* manager, 35 ChildDiscardableSharedMemoryManager* manager,
31 scoped_ptr<DiscardableSharedMemoryHeap::Span> span) 36 scoped_ptr<DiscardableSharedMemoryHeap::Span> span)
32 : manager_(manager), span_(span.Pass()) {} 37 : manager_(manager), span_(span.Pass()) {}
33 ~DiscardableMemoryShmemChunkImpl() override { 38 ~DiscardableMemoryShmemChunkImpl() override {
34 manager_->ReleaseSpan(span_.Pass()); 39 manager_->ReleaseSpan(span_.Pass());
35 } 40 }
36 41
37 // Overridden from DiscardableMemoryShmemChunk: 42 // Overridden from DiscardableMemoryShmemChunk:
38 bool Lock() override { return manager_->LockSpan(span_.get()); } 43 bool Lock() override { return manager_->LockSpan(span_.get()); }
39 void Unlock() override { manager_->UnlockSpan(span_.get()); } 44 void Unlock() override { manager_->UnlockSpan(span_.get()); }
40 void* Memory() const override { 45 void* Memory() const override {
41 return reinterpret_cast<void*>(span_->start() * base::GetPageSize()); 46 return reinterpret_cast<void*>(span_->start() * base::GetPageSize());
42 } 47 }
43 48
44 private: 49 private:
45 ChildDiscardableSharedMemoryManager* manager_; 50 ChildDiscardableSharedMemoryManager* manager_;
46 scoped_ptr<DiscardableSharedMemoryHeap::Span> span_; 51 scoped_ptr<DiscardableSharedMemoryHeap::Span> span_;
47 52
48 DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryShmemChunkImpl); 53 DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryShmemChunkImpl);
49 }; 54 };
50 55
56 void DeletedDiscardableSharedMemory(scoped_refptr<ThreadSafeSender> sender,
57 DiscardableSharedMemoryId id) {
58 sender->Send(new ChildProcessHostMsg_DeletedDiscardableSharedMemory(id));
59 }
60
51 } // namespace 61 } // namespace
52 62
53 ChildDiscardableSharedMemoryManager::ChildDiscardableSharedMemoryManager( 63 ChildDiscardableSharedMemoryManager::ChildDiscardableSharedMemoryManager(
54 ThreadSafeSender* sender) 64 ThreadSafeSender* sender)
55 : heap_(base::GetPageSize()), sender_(sender) { 65 : heap_(base::GetPageSize()), sender_(sender) {
56 } 66 }
57 67
58 ChildDiscardableSharedMemoryManager::~ChildDiscardableSharedMemoryManager() { 68 ChildDiscardableSharedMemoryManager::~ChildDiscardableSharedMemoryManager() {
59 // TODO(reveman): Determine if this DCHECK can be enabled. crbug.com/430533 69 // TODO(reveman): Determine if this DCHECK can be enabled. crbug.com/430533
60 // DCHECK_EQ(heap_.GetSize(), heap_.GetSizeOfFreeLists()); 70 // DCHECK_EQ(heap_.GetSize(), heap_.GetSizeOfFreeLists());
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
125 heap_.ReleasePurgedMemory(); 135 heap_.ReleasePurgedMemory();
126 136
127 // Make sure crash keys are up to date in case allocation fails. 137 // Make sure crash keys are up to date in case allocation fails.
128 if (heap_.GetSize() != heap_size_prior_to_releasing_purged_memory) 138 if (heap_.GetSize() != heap_size_prior_to_releasing_purged_memory)
129 MemoryUsageChanged(heap_.GetSize(), heap_.GetSizeOfFreeLists()); 139 MemoryUsageChanged(heap_.GetSize(), heap_.GetSizeOfFreeLists());
130 140
131 size_t pages_to_allocate = 141 size_t pages_to_allocate =
132 std::max(kAllocationSize / base::GetPageSize(), pages); 142 std::max(kAllocationSize / base::GetPageSize(), pages);
133 size_t allocation_size_in_bytes = pages_to_allocate * base::GetPageSize(); 143 size_t allocation_size_in_bytes = pages_to_allocate * base::GetPageSize();
134 144
145 DiscardableSharedMemoryId new_id =
146 g_next_discardable_shared_memory_id.GetNext();
147
135 // Ask parent process to allocate a new discardable shared memory segment. 148 // Ask parent process to allocate a new discardable shared memory segment.
136 scoped_ptr<base::DiscardableSharedMemory> shared_memory( 149 scoped_ptr<base::DiscardableSharedMemory> shared_memory(
137 AllocateLockedDiscardableSharedMemory(allocation_size_in_bytes)); 150 AllocateLockedDiscardableSharedMemory(allocation_size_in_bytes, new_id));
138 151
139 // Create span for allocated memory. 152 // Create span for allocated memory.
140 scoped_ptr<DiscardableSharedMemoryHeap::Span> new_span( 153 scoped_ptr<DiscardableSharedMemoryHeap::Span> new_span(
141 heap_.Grow(shared_memory.Pass(), allocation_size_in_bytes)); 154 heap_.Grow(shared_memory.Pass(), allocation_size_in_bytes,
155 base::Bind(&DeletedDiscardableSharedMemory, sender_, new_id)));
142 156
143 // Unlock and insert any left over memory into free lists. 157 // Unlock and insert any left over memory into free lists.
144 if (pages < pages_to_allocate) { 158 if (pages < pages_to_allocate) {
145 scoped_ptr<DiscardableSharedMemoryHeap::Span> leftover = 159 scoped_ptr<DiscardableSharedMemoryHeap::Span> leftover =
146 heap_.Split(new_span.get(), pages); 160 heap_.Split(new_span.get(), pages);
147 leftover->shared_memory()->Unlock( 161 leftover->shared_memory()->Unlock(
148 leftover->start() * base::GetPageSize() - 162 leftover->start() * base::GetPageSize() -
149 reinterpret_cast<size_t>(leftover->shared_memory()->memory()), 163 reinterpret_cast<size_t>(leftover->shared_memory()->memory()),
150 leftover->length() * base::GetPageSize()); 164 leftover->length() * base::GetPageSize());
151 heap_.MergeIntoFreeLists(leftover.Pass()); 165 heap_.MergeIntoFreeLists(leftover.Pass());
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
216 return; 230 return;
217 231
218 heap_.MergeIntoFreeLists(span.Pass()); 232 heap_.MergeIntoFreeLists(span.Pass());
219 233
220 // Bytes of free memory changed. 234 // Bytes of free memory changed.
221 MemoryUsageChanged(heap_.GetSize(), heap_.GetSizeOfFreeLists()); 235 MemoryUsageChanged(heap_.GetSize(), heap_.GetSizeOfFreeLists());
222 } 236 }
223 237
224 scoped_ptr<base::DiscardableSharedMemory> 238 scoped_ptr<base::DiscardableSharedMemory>
225 ChildDiscardableSharedMemoryManager::AllocateLockedDiscardableSharedMemory( 239 ChildDiscardableSharedMemoryManager::AllocateLockedDiscardableSharedMemory(
226 size_t size) { 240 size_t size,
227 TRACE_EVENT1("renderer", 241 DiscardableSharedMemoryId id) {
242 TRACE_EVENT2("renderer",
228 "ChildDiscardableSharedMemoryManager::" 243 "ChildDiscardableSharedMemoryManager::"
229 "AllocateLockedDiscardableSharedMemory", 244 "AllocateLockedDiscardableSharedMemory",
230 "size", 245 "size", size, "id", id);
231 size);
232 246
233 base::SharedMemoryHandle handle = base::SharedMemory::NULLHandle(); 247 base::SharedMemoryHandle handle = base::SharedMemory::NULLHandle();
234 sender_->Send( 248 sender_->Send(
235 new ChildProcessHostMsg_SyncAllocateLockedDiscardableSharedMemory( 249 new ChildProcessHostMsg_SyncAllocateLockedDiscardableSharedMemory(
236 size, &handle)); 250 size, id, &handle));
237 CHECK(base::SharedMemory::IsHandleValid(handle)); 251 CHECK(base::SharedMemory::IsHandleValid(handle));
238 scoped_ptr<base::DiscardableSharedMemory> memory( 252 scoped_ptr<base::DiscardableSharedMemory> memory(
239 new base::DiscardableSharedMemory(handle)); 253 new base::DiscardableSharedMemory(handle));
240 CHECK(memory->Map(size)); 254 CHECK(memory->Map(size));
241 return memory.Pass(); 255 return memory.Pass();
242 } 256 }
243 257
244 void ChildDiscardableSharedMemoryManager::MemoryUsageChanged( 258 void ChildDiscardableSharedMemoryManager::MemoryUsageChanged(
245 size_t new_bytes_total, 259 size_t new_bytes_total,
246 size_t new_bytes_free) const { 260 size_t new_bytes_free) const {
247 TRACE_COUNTER2("renderer", "DiscardableMemoryUsage", "allocated", 261 TRACE_COUNTER2("renderer", "DiscardableMemoryUsage", "allocated",
248 new_bytes_total - new_bytes_free, "free", new_bytes_free); 262 new_bytes_total - new_bytes_free, "free", new_bytes_free);
249 263
250 static const char kDiscardableMemoryUsageKey[] = "dm-usage"; 264 static const char kDiscardableMemoryUsageKey[] = "dm-usage";
251 base::debug::SetCrashKeyValue(kDiscardableMemoryUsageKey, 265 base::debug::SetCrashKeyValue(kDiscardableMemoryUsageKey,
252 base::Uint64ToString(new_bytes_total)); 266 base::Uint64ToString(new_bytes_total));
253 267
254 static const char kDiscardableMemoryUsageFreeKey[] = "dm-usage-free"; 268 static const char kDiscardableMemoryUsageFreeKey[] = "dm-usage-free";
255 base::debug::SetCrashKeyValue(kDiscardableMemoryUsageFreeKey, 269 base::debug::SetCrashKeyValue(kDiscardableMemoryUsageFreeKey,
256 base::Uint64ToString(new_bytes_free)); 270 base::Uint64ToString(new_bytes_free));
257 } 271 }
258 272
259 } // namespace content 273 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698