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