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