OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 "base/memory/shared_memory_tracker.h" | 5 #include "base/memory/shared_memory_tracker.h" |
6 | 6 |
7 #include "base/memory/shared_memory.h" | 7 #include "base/format_macros.h" |
8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
9 #include "base/trace_event/memory_dump_manager.h" | 9 #include "base/trace_event/memory_dump_manager.h" |
10 #include "base/trace_event/process_memory_dump.h" | 10 #include "base/trace_event/process_memory_dump.h" |
11 | 11 |
12 namespace base { | 12 namespace base { |
13 | 13 |
| 14 namespace { |
| 15 |
| 16 std::string SharedMemoryUniqueIdToString(const SharedMemory::UniqueId& id) { |
| 17 return StringPrintf("shared_memory/%" PRIu64 ".%" PRIu64, |
| 18 static_cast<uint64_t>(id.first), |
| 19 static_cast<uint64_t>(id.second)); |
| 20 } |
| 21 |
| 22 } // namespace |
| 23 |
14 SharedMemoryTracker::Usage::Usage() = default; | 24 SharedMemoryTracker::Usage::Usage() = default; |
15 | 25 |
16 SharedMemoryTracker::Usage::Usage(const Usage& rhs) = default; | 26 SharedMemoryTracker::Usage::Usage(const Usage& rhs) = default; |
17 | 27 |
18 SharedMemoryTracker::Usage::~Usage() = default; | 28 SharedMemoryTracker::Usage::~Usage() = default; |
19 | 29 |
20 // static | 30 // static |
21 SharedMemoryTracker* SharedMemoryTracker::GetInstance() { | 31 SharedMemoryTracker* SharedMemoryTracker::GetInstance() { |
22 static SharedMemoryTracker* instance = new SharedMemoryTracker; | 32 static SharedMemoryTracker* instance = new SharedMemoryTracker; |
23 return instance; | 33 return instance; |
24 } | 34 } |
25 | 35 |
| 36 // static |
| 37 bool SharedMemoryTracker::AddOwnershipEdges( |
| 38 trace_event::ProcessMemoryDump* pmd, |
| 39 const trace_event::MemoryAllocatorDumpGuid& source, |
| 40 const SharedMemoryHandle& shared_memory_handle, |
| 41 size_t size) { |
| 42 return AddOwnershipEdges(pmd, source, shared_memory_handle, size, |
| 43 trace_event::ProcessMemoryDump::kDefaultImportance); |
| 44 } |
| 45 |
| 46 // static |
| 47 bool SharedMemoryTracker::AddOwnershipEdges( |
| 48 trace_event::ProcessMemoryDump* pmd, |
| 49 const trace_event::MemoryAllocatorDumpGuid& source, |
| 50 const SharedMemoryHandle& shared_memory_handle, |
| 51 size_t size, |
| 52 int importance) { |
| 53 SharedMemory::UniqueId id; |
| 54 if (!SharedMemory::GetUniqueId(shared_memory_handle, &id)) |
| 55 return false; |
| 56 |
| 57 std::string id_str = SharedMemoryUniqueIdToString(id); |
| 58 auto in_process_dump_name = |
| 59 StringPrintf("shared_memory_in_process/%s", id_str.c_str()); |
| 60 auto* in_process_dump = pmd->CreateAllocatorDump(in_process_dump_name); |
| 61 pmd->AddOwnershipEdge(source, in_process_dump->guid()); |
| 62 in_process_dump->AddScalar(trace_event::MemoryAllocatorDump::kNameSize, |
| 63 trace_event::MemoryAllocatorDump::kUnitsBytes, |
| 64 size); |
| 65 |
| 66 trace_event::MemoryAllocatorDumpGuid global_guid(id_str); |
| 67 pmd->AddOwnershipEdge(in_process_dump->guid(), global_guid, importance); |
| 68 return true; |
| 69 } |
| 70 |
26 void SharedMemoryTracker::IncrementMemoryUsage( | 71 void SharedMemoryTracker::IncrementMemoryUsage( |
27 const SharedMemory& shared_memory) { | 72 const SharedMemory& shared_memory) { |
28 Usage usage; | 73 Usage usage; |
29 // |shared_memory|'s unique ID must be generated here and it'd be too late at | 74 // |shared_memory|'s unique ID must be generated here and it'd be too late at |
30 // OnMemoryDump. An ID is generated with a SharedMemoryHandle, but the handle | 75 // OnMemoryDump. An ID is generated with a SharedMemoryHandle, but the handle |
31 // might already be closed at that time. Now IncrementMemoryUsage is called | 76 // might already be closed at that time. Now IncrementMemoryUsage is called |
32 // just after mmap and the handle must live then. See the discussion at | 77 // just after mmap and the handle must live then. See the discussion at |
33 // crbug.com/604726#c30. | 78 // crbug.com/604726#c30. |
34 SharedMemory::UniqueId id; | 79 SharedMemory::UniqueId id; |
35 if (!shared_memory.GetUniqueId(&id)) | 80 if (!SharedMemory::GetUniqueId(shared_memory.handle(), &id)) |
36 return; | 81 return; |
37 usage.unique_id = id; | 82 usage.unique_id = id; |
38 usage.size = shared_memory.mapped_size(); | 83 usage.size = shared_memory.mapped_size(); |
39 AutoLock hold(usages_lock_); | 84 AutoLock hold(usages_lock_); |
40 usages_[&shared_memory] = usage; | 85 usages_[&shared_memory] = usage; |
41 } | 86 } |
42 | 87 |
43 void SharedMemoryTracker::DecrementMemoryUsage( | 88 void SharedMemoryTracker::DecrementMemoryUsage( |
44 const SharedMemory& shared_memory) { | 89 const SharedMemory& shared_memory) { |
45 AutoLock hold(usages_lock_); | 90 AutoLock hold(usages_lock_); |
46 usages_.erase(&shared_memory); | 91 usages_.erase(&shared_memory); |
47 } | 92 } |
48 | 93 |
49 bool SharedMemoryTracker::OnMemoryDump(const trace_event::MemoryDumpArgs& args, | 94 bool SharedMemoryTracker::OnMemoryDump(const trace_event::MemoryDumpArgs& args, |
50 trace_event::ProcessMemoryDump* pmd) { | 95 trace_event::ProcessMemoryDump* pmd) { |
51 std::unordered_map<SharedMemory::UniqueId, size_t, SharedMemory::UniqueIdHash> | 96 std::unordered_map<SharedMemory::UniqueId, size_t, SharedMemory::UniqueIdHash> |
52 sizes; | 97 sizes; |
53 { | 98 { |
54 AutoLock hold(usages_lock_); | 99 AutoLock hold(usages_lock_); |
55 for (const auto& usage : usages_) | 100 for (const auto& usage : usages_) |
56 sizes[usage.second.unique_id] += usage.second.size; | 101 sizes[usage.second.unique_id] += usage.second.size; |
57 } | 102 } |
58 for (auto& size : sizes) { | 103 for (auto& size : sizes) { |
59 const SharedMemory::UniqueId& id = size.first; | 104 const SharedMemory::UniqueId& id = size.first; |
60 std::string dump_name = StringPrintf("%s/%lld.%lld", "shared_memory", | 105 std::string dump_name = SharedMemoryUniqueIdToString(id); |
61 static_cast<long long>(id.first), | |
62 static_cast<long long>(id.second)); | |
63 auto guid = trace_event::MemoryAllocatorDumpGuid(dump_name); | 106 auto guid = trace_event::MemoryAllocatorDumpGuid(dump_name); |
64 trace_event::MemoryAllocatorDump* local_dump = | 107 trace_event::MemoryAllocatorDump* local_dump = |
65 pmd->CreateAllocatorDump(dump_name); | 108 pmd->CreateAllocatorDump(dump_name); |
66 // TODO(hajimehoshi): The size is not resident size but virtual size so far. | 109 // TODO(hajimehoshi): The size is not resident size but virtual size so far. |
67 // Fix this to record resident size. | 110 // Fix this to record resident size. |
68 local_dump->AddScalar(trace_event::MemoryAllocatorDump::kNameSize, | 111 local_dump->AddScalar(trace_event::MemoryAllocatorDump::kNameSize, |
69 trace_event::MemoryAllocatorDump::kUnitsBytes, | 112 trace_event::MemoryAllocatorDump::kUnitsBytes, |
70 size.second); | 113 size.second); |
71 trace_event::MemoryAllocatorDump* global_dump = | 114 trace_event::MemoryAllocatorDump* global_dump = |
72 pmd->CreateSharedGlobalAllocatorDump(guid); | 115 pmd->CreateSharedGlobalAllocatorDump(guid); |
(...skipping 10 matching lines...) Expand all Loading... |
83 } | 126 } |
84 | 127 |
85 SharedMemoryTracker::SharedMemoryTracker() { | 128 SharedMemoryTracker::SharedMemoryTracker() { |
86 trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider( | 129 trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider( |
87 this, "SharedMemoryTracker", nullptr); | 130 this, "SharedMemoryTracker", nullptr); |
88 } | 131 } |
89 | 132 |
90 SharedMemoryTracker::~SharedMemoryTracker() = default; | 133 SharedMemoryTracker::~SharedMemoryTracker() = default; |
91 | 134 |
92 } // namespace | 135 } // namespace |
OLD | NEW |