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

Side by Side Diff: components/discardable_memory/service/discardable_shared_memory_manager.cc

Issue 2459733002: Move discardable memory to //components from //content (Closed)
Patch Set: Fix review issues Created 4 years, 1 month 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/common/host_discardable_shared_memory_manager.h" 5 #include "components/discardable_memory/service/discardable_shared_memory_manage r.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/command_line.h"
13 #include "base/debug/crash_logging.h" 14 #include "base/debug/crash_logging.h"
14 #include "base/lazy_instance.h" 15 #include "base/lazy_instance.h"
15 #include "base/macros.h" 16 #include "base/macros.h"
16 #include "base/memory/discardable_memory.h" 17 #include "base/memory/discardable_memory.h"
17 #include "base/memory/memory_coordinator_client_registry.h" 18 #include "base/memory/memory_coordinator_client_registry.h"
18 #include "base/memory/ptr_util.h" 19 #include "base/memory/ptr_util.h"
19 #include "base/numerics/safe_math.h" 20 #include "base/numerics/safe_math.h"
20 #include "base/process/memory.h" 21 #include "base/process/memory.h"
21 #include "base/strings/string_number_conversions.h" 22 #include "base/strings/string_number_conversions.h"
22 #include "base/strings/stringprintf.h" 23 #include "base/strings/stringprintf.h"
23 #include "base/sys_info.h" 24 #include "base/sys_info.h"
24 #include "base/threading/thread_task_runner_handle.h" 25 #include "base/threading/thread_task_runner_handle.h"
25 #include "base/trace_event/memory_allocator_dump.h" 26 #include "base/trace_event/memory_allocator_dump.h"
26 #include "base/trace_event/memory_dump_manager.h" 27 #include "base/trace_event/memory_dump_manager.h"
27 #include "base/trace_event/process_memory_dump.h" 28 #include "base/trace_event/process_memory_dump.h"
28 #include "base/trace_event/trace_event.h" 29 #include "base/trace_event/trace_event.h"
29 #include "build/build_config.h" 30 #include "build/build_config.h"
30 #include "content/common/child_process_host_impl.h" 31 #include "components/discardable_memory/common/discardable_shared_memory_heap.h"
31 #include "content/common/discardable_shared_memory_heap.h"
32 #include "content/public/common/child_process_host.h"
33 32
34 #if defined(OS_LINUX) 33 #if defined(OS_LINUX)
35 #include "base/files/file_path.h" 34 #include "base/files/file_path.h"
36 #include "base/files/file_util.h" 35 #include "base/files/file_util.h"
37 #include "base/metrics/histogram_macros.h" 36 #include "base/metrics/histogram_macros.h"
38 #endif 37 #endif
39 38
40 namespace content { 39 namespace discardable_memory {
41 namespace { 40 namespace {
42 41
42 const char kSingleProcess[] = "single-process";
43
44 const int kInvalidUniqueClientID = -1;
45
46 const uint64_t kBrowserTracingProcessId = std::numeric_limits<uint64_t>::max();
47
48 uint64_t ClientProcessUniqueIdToTracingProcessId(int client_id) {
reveman 2016/11/01 18:49:23 nit: please file a bug about moving this to the ri
Peng 2016/11/01 19:10:36 Done.
49 if (base::CommandLine::ForCurrentProcess()->HasSwitch(kSingleProcess))
50 return kBrowserTracingProcessId;
51 // The hash value is incremented so that the tracing id is never equal to
52 // MemoryDumpManager::kInvalidTracingProcessId.
53 return static_cast<uint64_t>(base::Hash(
54 reinterpret_cast<const char*>(&client_id), sizeof(client_id))) +
55 1;
56 }
57
43 class DiscardableMemoryImpl : public base::DiscardableMemory { 58 class DiscardableMemoryImpl : public base::DiscardableMemory {
44 public: 59 public:
45 DiscardableMemoryImpl( 60 DiscardableMemoryImpl(
46 std::unique_ptr<base::DiscardableSharedMemory> shared_memory, 61 std::unique_ptr<base::DiscardableSharedMemory> shared_memory,
47 const base::Closure& deleted_callback) 62 const base::Closure& deleted_callback)
48 : shared_memory_(std::move(shared_memory)), 63 : shared_memory_(std::move(shared_memory)),
49 deleted_callback_(deleted_callback), 64 deleted_callback_(deleted_callback),
50 is_locked_(true) {} 65 is_locked_(true) {}
51 66
52 ~DiscardableMemoryImpl() override { 67 ~DiscardableMemoryImpl() override {
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
139 max_default_memory_limit = 154 max_default_memory_limit =
140 std::min(max_default_memory_limit, shmem_dir_amount_of_free_space / 2); 155 std::min(max_default_memory_limit, shmem_dir_amount_of_free_space / 2);
141 } 156 }
142 #endif 157 #endif
143 158
144 // Allow 25% of physical memory to be used for discardable memory. 159 // Allow 25% of physical memory to be used for discardable memory.
145 return std::min(max_default_memory_limit, 160 return std::min(max_default_memory_limit,
146 base::SysInfo::AmountOfPhysicalMemory() / 4); 161 base::SysInfo::AmountOfPhysicalMemory() / 4);
147 } 162 }
148 163
149 base::LazyInstance<HostDiscardableSharedMemoryManager> 164 base::LazyInstance<DiscardableSharedMemoryManager>
150 g_discardable_shared_memory_manager = LAZY_INSTANCE_INITIALIZER; 165 g_discardable_shared_memory_manager = LAZY_INSTANCE_INITIALIZER;
151 166
152 const int kEnforceMemoryPolicyDelayMs = 1000; 167 const int kEnforceMemoryPolicyDelayMs = 1000;
153 168
154 // Global atomic to generate unique discardable shared memory IDs. 169 // Global atomic to generate unique discardable shared memory IDs.
155 base::StaticAtomicSequenceNumber g_next_discardable_shared_memory_id; 170 base::StaticAtomicSequenceNumber g_next_discardable_shared_memory_id;
156 171
157 } // namespace 172 } // namespace
158 173
159 HostDiscardableSharedMemoryManager::MemorySegment::MemorySegment( 174 DiscardableSharedMemoryManager::MemorySegment::MemorySegment(
160 std::unique_ptr<base::DiscardableSharedMemory> memory) 175 std::unique_ptr<base::DiscardableSharedMemory> memory)
161 : memory_(std::move(memory)) {} 176 : memory_(std::move(memory)) {}
162 177
163 HostDiscardableSharedMemoryManager::MemorySegment::~MemorySegment() { 178 DiscardableSharedMemoryManager::MemorySegment::~MemorySegment() {}
164 }
165 179
166 HostDiscardableSharedMemoryManager::HostDiscardableSharedMemoryManager() 180 DiscardableSharedMemoryManager::DiscardableSharedMemoryManager()
167 : default_memory_limit_(GetDefaultMemoryLimit()), 181 : default_memory_limit_(GetDefaultMemoryLimit()),
168 memory_limit_(default_memory_limit_), 182 memory_limit_(default_memory_limit_),
169 bytes_allocated_(0), 183 bytes_allocated_(0),
170 memory_pressure_listener_(new base::MemoryPressureListener( 184 memory_pressure_listener_(new base::MemoryPressureListener(
171 base::Bind(&HostDiscardableSharedMemoryManager::OnMemoryPressure, 185 base::Bind(&DiscardableSharedMemoryManager::OnMemoryPressure,
172 base::Unretained(this)))), 186 base::Unretained(this)))),
173 // Current thread might not have a task runner in tests. 187 // Current thread might not have a task runner in tests.
174 enforce_memory_policy_task_runner_(base::ThreadTaskRunnerHandle::Get()), 188 enforce_memory_policy_task_runner_(base::ThreadTaskRunnerHandle::Get()),
175 enforce_memory_policy_pending_(false), 189 enforce_memory_policy_pending_(false),
176 weak_ptr_factory_(this) { 190 weak_ptr_factory_(this) {
177 DCHECK_NE(memory_limit_, 0u); 191 DCHECK_NE(memory_limit_, 0u);
178 enforce_memory_policy_callback_ = 192 enforce_memory_policy_callback_ =
179 base::Bind(&HostDiscardableSharedMemoryManager::EnforceMemoryPolicy, 193 base::Bind(&DiscardableSharedMemoryManager::EnforceMemoryPolicy,
180 weak_ptr_factory_.GetWeakPtr()); 194 weak_ptr_factory_.GetWeakPtr());
181 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider( 195 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider(
182 this, "HostDiscardableSharedMemoryManager", 196 this, "DiscardableSharedMemoryManager",
183 base::ThreadTaskRunnerHandle::Get()); 197 base::ThreadTaskRunnerHandle::Get());
184 base::MemoryCoordinatorClientRegistry::GetInstance()->Register(this); 198 base::MemoryCoordinatorClientRegistry::GetInstance()->Register(this);
185 } 199 }
186 200
187 HostDiscardableSharedMemoryManager::~HostDiscardableSharedMemoryManager() { 201 DiscardableSharedMemoryManager::~DiscardableSharedMemoryManager() {
188 base::trace_event::MemoryDumpManager::GetInstance()->UnregisterDumpProvider( 202 base::trace_event::MemoryDumpManager::GetInstance()->UnregisterDumpProvider(
189 this); 203 this);
190 } 204 }
191 205
192 HostDiscardableSharedMemoryManager* 206 DiscardableSharedMemoryManager* DiscardableSharedMemoryManager::current() {
193 HostDiscardableSharedMemoryManager::current() {
194 return g_discardable_shared_memory_manager.Pointer(); 207 return g_discardable_shared_memory_manager.Pointer();
195 } 208 }
196 209
197 std::unique_ptr<base::DiscardableMemory> 210 std::unique_ptr<base::DiscardableMemory>
198 HostDiscardableSharedMemoryManager::AllocateLockedDiscardableMemory( 211 DiscardableSharedMemoryManager::AllocateLockedDiscardableMemory(size_t size) {
199 size_t size) {
200 DCHECK_NE(size, 0u); 212 DCHECK_NE(size, 0u);
201 213
202 DiscardableSharedMemoryId new_id = 214 DiscardableSharedMemoryId new_id =
203 g_next_discardable_shared_memory_id.GetNext(); 215 g_next_discardable_shared_memory_id.GetNext();
204 base::ProcessHandle current_process_handle = base::GetCurrentProcessHandle(); 216 base::ProcessHandle current_process_handle = base::GetCurrentProcessHandle();
205 217
206 // Note: Use DiscardableSharedMemoryHeap for in-process allocation 218 // Note: Use DiscardableSharedMemoryHeap for in-process allocation
207 // of discardable memory if the cost of each allocation is too high. 219 // of discardable memory if the cost of each allocation is too high.
208 base::SharedMemoryHandle handle; 220 base::SharedMemoryHandle handle;
209 AllocateLockedDiscardableSharedMemory(current_process_handle, 221 AllocateLockedDiscardableSharedMemory(
210 ChildProcessHost::kInvalidUniqueID, 222 current_process_handle, kInvalidUniqueClientID, size, new_id, &handle);
211 size, new_id, &handle);
212 std::unique_ptr<base::DiscardableSharedMemory> memory( 223 std::unique_ptr<base::DiscardableSharedMemory> memory(
213 new base::DiscardableSharedMemory(handle)); 224 new base::DiscardableSharedMemory(handle));
214 if (!memory->Map(size)) 225 if (!memory->Map(size))
215 base::TerminateBecauseOutOfMemory(size); 226 base::TerminateBecauseOutOfMemory(size);
216 // Close file descriptor to avoid running out. 227 // Close file descriptor to avoid running out.
217 memory->Close(); 228 memory->Close();
218 return base::MakeUnique<DiscardableMemoryImpl>( 229 return base::MakeUnique<DiscardableMemoryImpl>(
219 std::move(memory), 230 std::move(memory),
220 base::Bind( 231 base::Bind(
221 &HostDiscardableSharedMemoryManager::DeletedDiscardableSharedMemory, 232 &DiscardableSharedMemoryManager::DeletedDiscardableSharedMemory,
222 base::Unretained(this), new_id, ChildProcessHost::kInvalidUniqueID)); 233 base::Unretained(this), new_id, kInvalidUniqueClientID));
223 } 234 }
224 235
225 bool HostDiscardableSharedMemoryManager::OnMemoryDump( 236 bool DiscardableSharedMemoryManager::OnMemoryDump(
226 const base::trace_event::MemoryDumpArgs& args, 237 const base::trace_event::MemoryDumpArgs& args,
227 base::trace_event::ProcessMemoryDump* pmd) { 238 base::trace_event::ProcessMemoryDump* pmd) {
228 if (args.level_of_detail == 239 if (args.level_of_detail ==
229 base::trace_event::MemoryDumpLevelOfDetail::BACKGROUND) { 240 base::trace_event::MemoryDumpLevelOfDetail::BACKGROUND) {
230 base::trace_event::MemoryAllocatorDump* total_dump = 241 base::trace_event::MemoryAllocatorDump* total_dump =
231 pmd->CreateAllocatorDump("discardable"); 242 pmd->CreateAllocatorDump("discardable");
232 total_dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize, 243 total_dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize,
233 base::trace_event::MemoryAllocatorDump::kUnitsBytes, 244 base::trace_event::MemoryAllocatorDump::kUnitsBytes,
234 GetBytesAllocated()); 245 GetBytesAllocated());
235 return true; 246 return true;
236 } 247 }
237 248
238 base::AutoLock lock(lock_); 249 base::AutoLock lock(lock_);
239 for (const auto& process_entry : processes_) { 250 for (const auto& client_entry : clients_) {
240 const int child_process_id = process_entry.first; 251 const int client_id = client_entry.first;
241 const MemorySegmentMap& process_segments = process_entry.second; 252 const MemorySegmentMap& client_segments = client_entry.second;
242 for (const auto& segment_entry : process_segments) { 253 for (const auto& segment_entry : client_segments) {
243 const int segment_id = segment_entry.first; 254 const int segment_id = segment_entry.first;
244 const MemorySegment* segment = segment_entry.second.get(); 255 const MemorySegment* segment = segment_entry.second.get();
245 if (!segment->memory()->mapped_size()) 256 if (!segment->memory()->mapped_size())
246 continue; 257 continue;
247 258
248 // The "size" will be inherited form the shared global dump. 259 // The "size" will be inherited form the shared global dump.
249 std::string dump_name = base::StringPrintf( 260 std::string dump_name = base::StringPrintf(
250 "discardable/process_%x/segment_%d", child_process_id, segment_id); 261 "discardable/process_%x/segment_%d", client_id, segment_id);
251 base::trace_event::MemoryAllocatorDump* dump = 262 base::trace_event::MemoryAllocatorDump* dump =
252 pmd->CreateAllocatorDump(dump_name); 263 pmd->CreateAllocatorDump(dump_name);
253 264
254 dump->AddScalar("virtual_size", 265 dump->AddScalar("virtual_size",
255 base::trace_event::MemoryAllocatorDump::kUnitsBytes, 266 base::trace_event::MemoryAllocatorDump::kUnitsBytes,
256 segment->memory()->mapped_size()); 267 segment->memory()->mapped_size());
257 268
258 // Host can only tell if whole segment is locked or not. 269 // Host can only tell if whole segment is locked or not.
259 dump->AddScalar( 270 dump->AddScalar(
260 "locked_size", base::trace_event::MemoryAllocatorDump::kUnitsBytes, 271 "locked_size", base::trace_event::MemoryAllocatorDump::kUnitsBytes,
261 segment->memory()->IsMemoryLocked() ? segment->memory()->mapped_size() 272 segment->memory()->IsMemoryLocked() ? segment->memory()->mapped_size()
262 : 0u); 273 : 0u);
263 274
264 // Create the cross-process ownership edge. If the child creates a 275 // Create the cross-process ownership edge. If the client creates a
265 // corresponding dump for the same segment, this will avoid to 276 // corresponding dump for the same segment, this will avoid to
266 // double-count them in tracing. If, instead, no other process will emit a 277 // double-count them in tracing. If, instead, no other process will emit a
267 // dump with the same guid, the segment will be accounted to the browser. 278 // dump with the same guid, the segment will be accounted to the browser.
268 const uint64_t child_tracing_process_id = 279 const uint64_t client_tracing_id =
269 ChildProcessHostImpl::ChildProcessUniqueIdToTracingProcessId( 280 ClientProcessUniqueIdToTracingProcessId(client_id);
270 child_process_id);
271 base::trace_event::MemoryAllocatorDumpGuid shared_segment_guid = 281 base::trace_event::MemoryAllocatorDumpGuid shared_segment_guid =
272 DiscardableSharedMemoryHeap::GetSegmentGUIDForTracing( 282 DiscardableSharedMemoryHeap::GetSegmentGUIDForTracing(
273 child_tracing_process_id, segment_id); 283 client_tracing_id, segment_id);
274 pmd->CreateSharedGlobalAllocatorDump(shared_segment_guid); 284 pmd->CreateSharedGlobalAllocatorDump(shared_segment_guid);
275 pmd->AddOwnershipEdge(dump->guid(), shared_segment_guid); 285 pmd->AddOwnershipEdge(dump->guid(), shared_segment_guid);
276 286
277 #if defined(COUNT_RESIDENT_BYTES_SUPPORTED) 287 #if defined(COUNT_RESIDENT_BYTES_SUPPORTED)
278 if (args.level_of_detail == 288 if (args.level_of_detail ==
279 base::trace_event::MemoryDumpLevelOfDetail::DETAILED) { 289 base::trace_event::MemoryDumpLevelOfDetail::DETAILED) {
280 size_t resident_size = 290 size_t resident_size =
281 base::trace_event::ProcessMemoryDump::CountResidentBytes( 291 base::trace_event::ProcessMemoryDump::CountResidentBytes(
282 segment->memory()->memory(), segment->memory()->mapped_size()); 292 segment->memory()->memory(), segment->memory()->mapped_size());
283 293
284 // This is added to the global dump since it has to be attributed to 294 // This is added to the global dump since it has to be attributed to
285 // both the allocator dumps involved. 295 // both the allocator dumps involved.
286 pmd->GetSharedGlobalAllocatorDump(shared_segment_guid) 296 pmd->GetSharedGlobalAllocatorDump(shared_segment_guid)
287 ->AddScalar("resident_size", 297 ->AddScalar("resident_size",
288 base::trace_event::MemoryAllocatorDump::kUnitsBytes, 298 base::trace_event::MemoryAllocatorDump::kUnitsBytes,
289 static_cast<uint64_t>(resident_size)); 299 static_cast<uint64_t>(resident_size));
290 } 300 }
291 #endif // defined(COUNT_RESIDENT_BYTES_SUPPORTED) 301 #endif // defined(COUNT_RESIDENT_BYTES_SUPPORTED)
292 } 302 }
293 } 303 }
294 return true; 304 return true;
295 } 305 }
296 306
297 void HostDiscardableSharedMemoryManager:: 307 void DiscardableSharedMemoryManager::
298 AllocateLockedDiscardableSharedMemoryForChild( 308 AllocateLockedDiscardableSharedMemoryForClient(
299 base::ProcessHandle process_handle, 309 base::ProcessHandle process_handle,
300 int child_process_id, 310 int client_id,
301 size_t size, 311 size_t size,
302 DiscardableSharedMemoryId id, 312 DiscardableSharedMemoryId id,
303 base::SharedMemoryHandle* shared_memory_handle) { 313 base::SharedMemoryHandle* shared_memory_handle) {
304 AllocateLockedDiscardableSharedMemory(process_handle, child_process_id, size, 314 AllocateLockedDiscardableSharedMemory(process_handle, client_id, size, id,
305 id, shared_memory_handle); 315 shared_memory_handle);
306 } 316 }
307 317
308 void HostDiscardableSharedMemoryManager::ChildDeletedDiscardableSharedMemory( 318 void DiscardableSharedMemoryManager::ClientDeletedDiscardableSharedMemory(
309 DiscardableSharedMemoryId id, 319 DiscardableSharedMemoryId id,
310 int child_process_id) { 320 int client_id) {
311 DeletedDiscardableSharedMemory(id, child_process_id); 321 DeletedDiscardableSharedMemory(id, client_id);
312 } 322 }
313 323
314 void HostDiscardableSharedMemoryManager::ProcessRemoved(int child_process_id) { 324 void DiscardableSharedMemoryManager::ClientRemoved(int client_id) {
315 base::AutoLock lock(lock_); 325 base::AutoLock lock(lock_);
316 326
317 ProcessMap::iterator process_it = processes_.find(child_process_id); 327 auto it = clients_.find(client_id);
318 if (process_it == processes_.end()) 328 if (it == clients_.end())
319 return; 329 return;
320 330
321 size_t bytes_allocated_before_releasing_memory = bytes_allocated_; 331 size_t bytes_allocated_before_releasing_memory = bytes_allocated_;
322 332
323 for (auto& segment_it : process_it->second) 333 for (auto& segment_it : it->second)
324 ReleaseMemory(segment_it.second->memory()); 334 ReleaseMemory(segment_it.second->memory());
325 335
326 processes_.erase(process_it); 336 clients_.erase(it);
327 337
328 if (bytes_allocated_ != bytes_allocated_before_releasing_memory) 338 if (bytes_allocated_ != bytes_allocated_before_releasing_memory)
329 BytesAllocatedChanged(bytes_allocated_); 339 BytesAllocatedChanged(bytes_allocated_);
330 } 340 }
331 341
332 void HostDiscardableSharedMemoryManager::SetMemoryLimit(size_t limit) { 342 void DiscardableSharedMemoryManager::SetMemoryLimit(size_t limit) {
333 base::AutoLock lock(lock_); 343 base::AutoLock lock(lock_);
334 344
335 memory_limit_ = limit; 345 memory_limit_ = limit;
336 ReduceMemoryUsageUntilWithinMemoryLimit(); 346 ReduceMemoryUsageUntilWithinMemoryLimit();
337 } 347 }
338 348
339 void HostDiscardableSharedMemoryManager::EnforceMemoryPolicy() { 349 void DiscardableSharedMemoryManager::EnforceMemoryPolicy() {
340 base::AutoLock lock(lock_); 350 base::AutoLock lock(lock_);
341 351
342 enforce_memory_policy_pending_ = false; 352 enforce_memory_policy_pending_ = false;
343 ReduceMemoryUsageUntilWithinMemoryLimit(); 353 ReduceMemoryUsageUntilWithinMemoryLimit();
344 } 354 }
345 355
346 size_t HostDiscardableSharedMemoryManager::GetBytesAllocated() { 356 size_t DiscardableSharedMemoryManager::GetBytesAllocated() {
347 base::AutoLock lock(lock_); 357 base::AutoLock lock(lock_);
348 358
349 return bytes_allocated_; 359 return bytes_allocated_;
350 } 360 }
351 361
352 void HostDiscardableSharedMemoryManager::OnMemoryStateChange( 362 void DiscardableSharedMemoryManager::OnMemoryStateChange(
353 base::MemoryState state) { 363 base::MemoryState state) {
354 switch (state) { 364 switch (state) {
355 case base::MemoryState::NORMAL: 365 case base::MemoryState::NORMAL:
356 SetMemoryLimit(default_memory_limit_); 366 SetMemoryLimit(default_memory_limit_);
357 break; 367 break;
358 case base::MemoryState::THROTTLED: 368 case base::MemoryState::THROTTLED:
359 SetMemoryLimit(0); 369 SetMemoryLimit(0);
360 break; 370 break;
361 case base::MemoryState::SUSPENDED: 371 case base::MemoryState::SUSPENDED:
362 // Note that SUSPENDED never occurs in the main browser process so far. 372 // Note that SUSPENDED never occurs in the main browser process so far.
363 // Fall through. 373 // Fall through.
364 case base::MemoryState::UNKNOWN: 374 case base::MemoryState::UNKNOWN:
365 NOTREACHED(); 375 NOTREACHED();
366 break; 376 break;
367 } 377 }
368 } 378 }
369 379
370 void HostDiscardableSharedMemoryManager::AllocateLockedDiscardableSharedMemory( 380 void DiscardableSharedMemoryManager::AllocateLockedDiscardableSharedMemory(
371 base::ProcessHandle process_handle, 381 base::ProcessHandle process_handle,
372 int client_process_id, 382 int client_id,
373 size_t size, 383 size_t size,
374 DiscardableSharedMemoryId id, 384 DiscardableSharedMemoryId id,
375 base::SharedMemoryHandle* shared_memory_handle) { 385 base::SharedMemoryHandle* shared_memory_handle) {
376 base::AutoLock lock(lock_); 386 base::AutoLock lock(lock_);
377 387
378 // Make sure |id| is not already in use. 388 // Make sure |id| is not already in use.
379 MemorySegmentMap& process_segments = processes_[client_process_id]; 389 MemorySegmentMap& client_segments = clients_[client_id];
380 if (process_segments.find(id) != process_segments.end()) { 390 if (client_segments.find(id) != client_segments.end()) {
381 LOG(ERROR) << "Invalid discardable shared memory ID"; 391 LOG(ERROR) << "Invalid discardable shared memory ID";
382 *shared_memory_handle = base::SharedMemory::NULLHandle(); 392 *shared_memory_handle = base::SharedMemory::NULLHandle();
383 return; 393 return;
384 } 394 }
385 395
386 // Memory usage must be reduced to prevent the addition of |size| from 396 // Memory usage must be reduced to prevent the addition of |size| from
387 // taking usage above the limit. Usage should be reduced to 0 in cases 397 // taking usage above the limit. Usage should be reduced to 0 in cases
388 // where |size| is greater than the limit. 398 // where |size| is greater than the limit.
389 size_t limit = 0; 399 size_t limit = 0;
390 // Note: the actual mapped size can be larger than requested and cause 400 // Note: the actual mapped size can be larger than requested and cause
(...skipping 26 matching lines...) Expand all
417 checked_bytes_allocated += memory->mapped_size(); 427 checked_bytes_allocated += memory->mapped_size();
418 if (!checked_bytes_allocated.IsValid()) { 428 if (!checked_bytes_allocated.IsValid()) {
419 *shared_memory_handle = base::SharedMemory::NULLHandle(); 429 *shared_memory_handle = base::SharedMemory::NULLHandle();
420 return; 430 return;
421 } 431 }
422 432
423 bytes_allocated_ = checked_bytes_allocated.ValueOrDie(); 433 bytes_allocated_ = checked_bytes_allocated.ValueOrDie();
424 BytesAllocatedChanged(bytes_allocated_); 434 BytesAllocatedChanged(bytes_allocated_);
425 435
426 scoped_refptr<MemorySegment> segment(new MemorySegment(std::move(memory))); 436 scoped_refptr<MemorySegment> segment(new MemorySegment(std::move(memory)));
427 process_segments[id] = segment.get(); 437 client_segments[id] = segment.get();
428 segments_.push_back(segment.get()); 438 segments_.push_back(segment.get());
429 std::push_heap(segments_.begin(), segments_.end(), CompareMemoryUsageTime); 439 std::push_heap(segments_.begin(), segments_.end(), CompareMemoryUsageTime);
430 440
431 if (bytes_allocated_ > memory_limit_) 441 if (bytes_allocated_ > memory_limit_)
432 ScheduleEnforceMemoryPolicy(); 442 ScheduleEnforceMemoryPolicy();
433 } 443 }
434 444
435 void HostDiscardableSharedMemoryManager::DeletedDiscardableSharedMemory( 445 void DiscardableSharedMemoryManager::DeletedDiscardableSharedMemory(
436 DiscardableSharedMemoryId id, 446 DiscardableSharedMemoryId id,
437 int client_process_id) { 447 int client_id) {
438 base::AutoLock lock(lock_); 448 base::AutoLock lock(lock_);
439 449
440 MemorySegmentMap& process_segments = processes_[client_process_id]; 450 MemorySegmentMap& client_segments = clients_[client_id];
441 451
442 MemorySegmentMap::iterator segment_it = process_segments.find(id); 452 MemorySegmentMap::iterator segment_it = client_segments.find(id);
443 if (segment_it == process_segments.end()) { 453 if (segment_it == client_segments.end()) {
444 LOG(ERROR) << "Invalid discardable shared memory ID"; 454 LOG(ERROR) << "Invalid discardable shared memory ID";
445 return; 455 return;
446 } 456 }
447 457
448 size_t bytes_allocated_before_releasing_memory = bytes_allocated_; 458 size_t bytes_allocated_before_releasing_memory = bytes_allocated_;
449 459
450 ReleaseMemory(segment_it->second->memory()); 460 ReleaseMemory(segment_it->second->memory());
451 461
452 process_segments.erase(segment_it); 462 client_segments.erase(segment_it);
453 463
454 if (bytes_allocated_ != bytes_allocated_before_releasing_memory) 464 if (bytes_allocated_ != bytes_allocated_before_releasing_memory)
455 BytesAllocatedChanged(bytes_allocated_); 465 BytesAllocatedChanged(bytes_allocated_);
456 } 466 }
457 467
458 void HostDiscardableSharedMemoryManager::OnMemoryPressure( 468 void DiscardableSharedMemoryManager::OnMemoryPressure(
459 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level) { 469 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level) {
460 base::AutoLock lock(lock_); 470 base::AutoLock lock(lock_);
461 471
462 switch (memory_pressure_level) { 472 switch (memory_pressure_level) {
463 case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE: 473 case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE:
464 break; 474 break;
465 case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE: 475 case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE:
466 // Purge memory until usage is within half of |memory_limit_|. 476 // Purge memory until usage is within half of |memory_limit_|.
467 ReduceMemoryUsageUntilWithinLimit(memory_limit_ / 2); 477 ReduceMemoryUsageUntilWithinLimit(memory_limit_ / 2);
468 break; 478 break;
469 case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL: 479 case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL:
470 // Purge everything possible when pressure is critical. 480 // Purge everything possible when pressure is critical.
471 ReduceMemoryUsageUntilWithinLimit(0); 481 ReduceMemoryUsageUntilWithinLimit(0);
472 break; 482 break;
473 } 483 }
474 } 484 }
475 485
476 void 486 void DiscardableSharedMemoryManager::ReduceMemoryUsageUntilWithinMemoryLimit() {
477 HostDiscardableSharedMemoryManager::ReduceMemoryUsageUntilWithinMemoryLimit() {
478 lock_.AssertAcquired(); 487 lock_.AssertAcquired();
479 488
480 if (bytes_allocated_ <= memory_limit_) 489 if (bytes_allocated_ <= memory_limit_)
481 return; 490 return;
482 491
483 ReduceMemoryUsageUntilWithinLimit(memory_limit_); 492 ReduceMemoryUsageUntilWithinLimit(memory_limit_);
484 if (bytes_allocated_ > memory_limit_) 493 if (bytes_allocated_ > memory_limit_)
485 ScheduleEnforceMemoryPolicy(); 494 ScheduleEnforceMemoryPolicy();
486 } 495 }
487 496
488 void HostDiscardableSharedMemoryManager::ReduceMemoryUsageUntilWithinLimit( 497 void DiscardableSharedMemoryManager::ReduceMemoryUsageUntilWithinLimit(
489 size_t limit) { 498 size_t limit) {
490 TRACE_EVENT1("renderer_host", 499 TRACE_EVENT1("renderer_host",
491 "HostDiscardableSharedMemoryManager::" 500 "DiscardableSharedMemoryManager::"
492 "ReduceMemoryUsageUntilWithinLimit", 501 "ReduceMemoryUsageUntilWithinLimit",
493 "bytes_allocated", 502 "bytes_allocated", bytes_allocated_);
494 bytes_allocated_);
495 503
496 // Usage time of currently locked segments are updated to this time and 504 // Usage time of currently locked segments are updated to this time and
497 // we stop eviction attempts as soon as we come across a segment that we've 505 // we stop eviction attempts as soon as we come across a segment that we've
498 // previously tried to evict but was locked. 506 // previously tried to evict but was locked.
499 base::Time current_time = Now(); 507 base::Time current_time = Now();
500 508
501 lock_.AssertAcquired(); 509 lock_.AssertAcquired();
502 size_t bytes_allocated_before_purging = bytes_allocated_; 510 size_t bytes_allocated_before_purging = bytes_allocated_;
503 while (!segments_.empty()) { 511 while (!segments_.empty()) {
504 if (bytes_allocated_ <= limit) 512 if (bytes_allocated_ <= limit)
(...skipping 22 matching lines...) Expand all
527 // Add memory segment (with updated usage timestamp) back on heap after 535 // Add memory segment (with updated usage timestamp) back on heap after
528 // failed attempt to purge it. 536 // failed attempt to purge it.
529 segments_.push_back(segment.get()); 537 segments_.push_back(segment.get());
530 std::push_heap(segments_.begin(), segments_.end(), CompareMemoryUsageTime); 538 std::push_heap(segments_.begin(), segments_.end(), CompareMemoryUsageTime);
531 } 539 }
532 540
533 if (bytes_allocated_ != bytes_allocated_before_purging) 541 if (bytes_allocated_ != bytes_allocated_before_purging)
534 BytesAllocatedChanged(bytes_allocated_); 542 BytesAllocatedChanged(bytes_allocated_);
535 } 543 }
536 544
537 void HostDiscardableSharedMemoryManager::ReleaseMemory( 545 void DiscardableSharedMemoryManager::ReleaseMemory(
538 base::DiscardableSharedMemory* memory) { 546 base::DiscardableSharedMemory* memory) {
539 lock_.AssertAcquired(); 547 lock_.AssertAcquired();
540 548
541 size_t size = memory->mapped_size(); 549 size_t size = memory->mapped_size();
542 DCHECK_GE(bytes_allocated_, size); 550 DCHECK_GE(bytes_allocated_, size);
543 bytes_allocated_ -= size; 551 bytes_allocated_ -= size;
544 552
545 // This will unmap the memory segment and drop our reference. The result 553 // This will unmap the memory segment and drop our reference. The result
546 // is that the memory will be released to the OS if the child process is 554 // is that the memory will be released to the OS if the client is no longer
547 // no longer referencing it. 555 // referencing it.
548 // Note: We intentionally leave the segment in the |segments| vector to 556 // Note: We intentionally leave the segment in the |segments| vector to
549 // avoid reconstructing the heap. The element will be removed from the heap 557 // avoid reconstructing the heap. The element will be removed from the heap
550 // when its last usage time is older than all other segments. 558 // when its last usage time is older than all other segments.
551 memory->Unmap(); 559 memory->Unmap();
552 memory->Close(); 560 memory->Close();
553 } 561 }
554 562
555 void HostDiscardableSharedMemoryManager::BytesAllocatedChanged( 563 void DiscardableSharedMemoryManager::BytesAllocatedChanged(
556 size_t new_bytes_allocated) const { 564 size_t new_bytes_allocated) const {
557 static const char kTotalDiscardableMemoryAllocatedKey[] = 565 static const char kTotalDiscardableMemoryAllocatedKey[] =
558 "total-discardable-memory-allocated"; 566 "total-discardable-memory-allocated";
559 base::debug::SetCrashKeyValue(kTotalDiscardableMemoryAllocatedKey, 567 base::debug::SetCrashKeyValue(kTotalDiscardableMemoryAllocatedKey,
560 base::Uint64ToString(new_bytes_allocated)); 568 base::Uint64ToString(new_bytes_allocated));
561 } 569 }
562 570
563 base::Time HostDiscardableSharedMemoryManager::Now() const { 571 base::Time DiscardableSharedMemoryManager::Now() const {
564 return base::Time::Now(); 572 return base::Time::Now();
565 } 573 }
566 574
567 void HostDiscardableSharedMemoryManager::ScheduleEnforceMemoryPolicy() { 575 void DiscardableSharedMemoryManager::ScheduleEnforceMemoryPolicy() {
568 lock_.AssertAcquired(); 576 lock_.AssertAcquired();
569 577
570 if (enforce_memory_policy_pending_) 578 if (enforce_memory_policy_pending_)
571 return; 579 return;
572 580
573 enforce_memory_policy_pending_ = true; 581 enforce_memory_policy_pending_ = true;
574 DCHECK(enforce_memory_policy_task_runner_); 582 DCHECK(enforce_memory_policy_task_runner_);
575 enforce_memory_policy_task_runner_->PostDelayedTask( 583 enforce_memory_policy_task_runner_->PostDelayedTask(
576 FROM_HERE, enforce_memory_policy_callback_, 584 FROM_HERE, enforce_memory_policy_callback_,
577 base::TimeDelta::FromMilliseconds(kEnforceMemoryPolicyDelayMs)); 585 base::TimeDelta::FromMilliseconds(kEnforceMemoryPolicyDelayMs));
578 } 586 }
579 587
580 } // namespace content 588 } // namespace discardable_memory
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698