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 "components/discardable_memory/common/discardable_shared_memory_heap.h" | 5 #include "components/discardable_memory/common/discardable_shared_memory_heap.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/format_macros.h" | 10 #include "base/format_macros.h" |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/memory/discardable_shared_memory.h" | 12 #include "base/memory/discardable_shared_memory.h" |
13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
| 14 #include "base/memory/shared_memory_tracker.h" |
14 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
15 #include "base/trace_event/memory_dump_manager.h" | 16 #include "base/trace_event/memory_dump_manager.h" |
16 | 17 |
17 namespace discardable_memory { | 18 namespace discardable_memory { |
18 namespace { | 19 namespace { |
19 | 20 |
20 bool IsPowerOfTwo(size_t x) { | 21 bool IsPowerOfTwo(size_t x) { |
21 return (x & (x - 1)) == 0; | 22 return (x & (x - 1)) == 0; |
22 } | 23 } |
23 | 24 |
(...skipping 391 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
415 obj_dump->AddScalar("locked_size", | 416 obj_dump->AddScalar("locked_size", |
416 base::trace_event::MemoryAllocatorDump::kUnitsBytes, | 417 base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
417 locked_objects_size_in_bytes); | 418 locked_objects_size_in_bytes); |
418 | 419 |
419 // Emit an ownership edge towards a global allocator dump node. This allows | 420 // Emit an ownership edge towards a global allocator dump node. This allows |
420 // to avoid double-counting segments when both browser and client process emit | 421 // to avoid double-counting segments when both browser and client process emit |
421 // them. In the special case of single-process-mode, this will be the only | 422 // them. In the special case of single-process-mode, this will be the only |
422 // dumper active and the single ownership edge will become a no-op in the UI. | 423 // dumper active and the single ownership edge will become a no-op in the UI. |
423 // The global dump is created as a weak dump so that the segment is removed if | 424 // The global dump is created as a weak dump so that the segment is removed if |
424 // the browser does not dump it (segment was purged). | 425 // the browser does not dump it (segment was purged). |
425 const uint64_t tracing_process_id = | 426 base::SharedMemoryTracker::AddOwnershipEdges(pmd, segment_dump->guid(), |
426 base::trace_event::MemoryDumpManager::GetInstance() | 427 shared_memory->handle(), |
427 ->GetTracingProcessId(); | 428 allocated_objects_size_in_bytes); |
428 base::trace_event::MemoryAllocatorDumpGuid shared_segment_guid = | |
429 GetSegmentGUIDForTracing(tracing_process_id, segment_id); | |
430 // TODO(ssid): Make this weak once the GUID created is consistent | |
431 // crbug.com/661257. | |
432 pmd->CreateSharedGlobalAllocatorDump(shared_segment_guid); | |
433 | |
434 // The size is added to the global dump so that it gets propagated to both the | |
435 // dumps associated. | |
436 pmd->GetSharedGlobalAllocatorDump(shared_segment_guid) | |
437 ->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize, | |
438 base::trace_event::MemoryAllocatorDump::kUnitsBytes, | |
439 allocated_objects_size_in_bytes); | |
440 | |
441 // By creating an edge with a higher |importance| (w.r.t. browser-side dumps) | |
442 // the tracing UI will account the effective size of the segment to the | |
443 // client. | |
444 const int kImportance = 2; | |
445 pmd->AddOwnershipEdge(segment_dump->guid(), shared_segment_guid, kImportance); | |
446 } | |
447 | |
448 // static | |
449 base::trace_event::MemoryAllocatorDumpGuid | |
450 DiscardableSharedMemoryHeap::GetSegmentGUIDForTracing( | |
451 uint64_t tracing_process_id, | |
452 int32_t segment_id) { | |
453 return base::trace_event::MemoryAllocatorDumpGuid(base::StringPrintf( | |
454 "discardable-x-process/%" PRIx64 "/%d", tracing_process_id, segment_id)); | |
455 } | 429 } |
456 | 430 |
457 base::trace_event::MemoryAllocatorDump* | 431 base::trace_event::MemoryAllocatorDump* |
458 DiscardableSharedMemoryHeap::CreateMemoryAllocatorDump( | 432 DiscardableSharedMemoryHeap::CreateMemoryAllocatorDump( |
459 Span* span, | 433 Span* span, |
460 const char* name, | 434 const char* name, |
461 base::trace_event::ProcessMemoryDump* pmd) const { | 435 base::trace_event::ProcessMemoryDump* pmd) const { |
462 if (!span->shared_memory()) { | 436 if (!span->shared_memory()) { |
463 base::trace_event::MemoryAllocatorDump* dump = | 437 base::trace_event::MemoryAllocatorDump* dump = |
464 pmd->CreateAllocatorDump(name); | 438 pmd->CreateAllocatorDump(name); |
465 dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize, | 439 dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize, |
466 base::trace_event::MemoryAllocatorDump::kUnitsBytes, 0u); | 440 base::trace_event::MemoryAllocatorDump::kUnitsBytes, 0u); |
467 return dump; | 441 return dump; |
468 } | 442 } |
469 | 443 |
470 auto it = | 444 auto it = |
471 std::find_if(memory_segments_.begin(), memory_segments_.end(), | 445 std::find_if(memory_segments_.begin(), memory_segments_.end(), |
472 [span](const std::unique_ptr<ScopedMemorySegment>& segment) { | 446 [span](const std::unique_ptr<ScopedMemorySegment>& segment) { |
473 return segment->ContainsSpan(span); | 447 return segment->ContainsSpan(span); |
474 }); | 448 }); |
475 DCHECK(it != memory_segments_.end()); | 449 DCHECK(it != memory_segments_.end()); |
476 return (*it)->CreateMemoryAllocatorDump(span, block_size_, name, pmd); | 450 return (*it)->CreateMemoryAllocatorDump(span, block_size_, name, pmd); |
477 } | 451 } |
478 | 452 |
479 } // namespace discardable_memory | 453 } // namespace discardable_memory |
OLD | NEW |