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/memory/shared_memory.h" |
8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/string_number_conversions.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 SharedMemoryTracker::Usage::Usage() = default; | |
15 | |
16 SharedMemoryTracker::Usage::Usage(const Usage& rhs) = default; | |
17 | |
18 SharedMemoryTracker::Usage::~Usage() = default; | |
19 | |
20 // static | 14 // static |
21 SharedMemoryTracker* SharedMemoryTracker::GetInstance() { | 15 SharedMemoryTracker* SharedMemoryTracker::GetInstance() { |
22 static SharedMemoryTracker* instance = new SharedMemoryTracker; | 16 static SharedMemoryTracker* instance = new SharedMemoryTracker; |
23 return instance; | 17 return instance; |
24 } | 18 } |
25 | 19 |
26 void SharedMemoryTracker::IncrementMemoryUsage( | 20 void SharedMemoryTracker::IncrementMemoryUsage( |
27 const SharedMemory& shared_memory) { | 21 const SharedMemory& shared_memory) { |
28 Usage usage; | |
29 // |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 | |
31 // 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 | |
33 // crbug.com/604726#c30. | |
34 SharedMemory::UniqueId id; | |
35 if (!shared_memory.GetUniqueId(&id)) | |
36 return; | |
37 usage.unique_id = id; | |
38 usage.size = shared_memory.mapped_size(); | |
39 AutoLock hold(usages_lock_); | 22 AutoLock hold(usages_lock_); |
40 usages_[&shared_memory] = usage; | 23 usages_[&shared_memory] = shared_memory.mapped_size(); |
41 } | 24 } |
42 | 25 |
43 void SharedMemoryTracker::DecrementMemoryUsage( | 26 void SharedMemoryTracker::DecrementMemoryUsage( |
44 const SharedMemory& shared_memory) { | 27 const SharedMemory& shared_memory) { |
45 AutoLock hold(usages_lock_); | 28 AutoLock hold(usages_lock_); |
46 usages_.erase(&shared_memory); | 29 usages_.erase(&shared_memory); |
47 } | 30 } |
48 | 31 |
49 bool SharedMemoryTracker::OnMemoryDump(const trace_event::MemoryDumpArgs& args, | 32 bool SharedMemoryTracker::OnMemoryDump(const trace_event::MemoryDumpArgs& args, |
50 trace_event::ProcessMemoryDump* pmd) { | 33 trace_event::ProcessMemoryDump* pmd) { |
51 std::unordered_map<SharedMemory::UniqueId, size_t, SharedMemory::UniqueIdHash> | 34 std::vector<std::tuple<UnguessableToken, uintptr_t, size_t>> usages; |
52 sizes; | |
53 { | 35 { |
54 AutoLock hold(usages_lock_); | 36 AutoLock hold(usages_lock_); |
55 for (const auto& usage : usages_) | 37 usages.reserve(usages_.size()); |
56 sizes[usage.second.unique_id] += usage.second.size; | 38 for (const auto& usage : usages_) { |
| 39 usages.emplace_back(usage.first->handle().GetGUID(), |
| 40 reinterpret_cast<uintptr_t>(usage.first->memory()), |
| 41 usage.second); |
| 42 } |
57 } | 43 } |
58 for (auto& size : sizes) { | 44 for (const auto& usage : usages) { |
59 const SharedMemory::UniqueId& id = size.first; | 45 const UnguessableToken& memory_guid = std::get<0>(usage); |
60 std::string dump_name = StringPrintf("%s/%lld.%lld", "shared_memory", | 46 uintptr_t address = std::get<1>(usage); |
61 static_cast<long long>(id.first), | 47 size_t size = std::get<2>(usage); |
62 static_cast<long long>(id.second)); | 48 std::string dump_name = "shared_memory/"; |
63 auto guid = trace_event::MemoryAllocatorDumpGuid(dump_name); | 49 if (memory_guid.is_empty()) { |
| 50 // TODO(hajimehoshi): As passing ID across mojo is not implemented yet |
| 51 // (crbug/713763), ID can be empty. For such case, use an address instead |
| 52 // of GUID so that approximate memory usages are available. |
| 53 dump_name += Uint64ToString(address); |
| 54 } else { |
| 55 dump_name += memory_guid.ToString(); |
| 56 } |
| 57 auto dump_guid = trace_event::MemoryAllocatorDumpGuid(dump_name); |
| 58 // Discard duplicates that might be seen in single-process mode. |
| 59 if (pmd->GetAllocatorDump(dump_name)) |
| 60 continue; |
64 trace_event::MemoryAllocatorDump* local_dump = | 61 trace_event::MemoryAllocatorDump* local_dump = |
65 pmd->CreateAllocatorDump(dump_name); | 62 pmd->CreateAllocatorDump(dump_name); |
66 // TODO(hajimehoshi): The size is not resident size but virtual size so far. | 63 // TODO(hajimehoshi): The size is not resident size but virtual size so far. |
67 // Fix this to record resident size. | 64 // Fix this to record resident size. |
68 local_dump->AddScalar(trace_event::MemoryAllocatorDump::kNameSize, | 65 local_dump->AddScalar(trace_event::MemoryAllocatorDump::kNameSize, |
69 trace_event::MemoryAllocatorDump::kUnitsBytes, | 66 trace_event::MemoryAllocatorDump::kUnitsBytes, size); |
70 size.second); | |
71 trace_event::MemoryAllocatorDump* global_dump = | 67 trace_event::MemoryAllocatorDump* global_dump = |
72 pmd->CreateSharedGlobalAllocatorDump(guid); | 68 pmd->CreateSharedGlobalAllocatorDump(dump_guid); |
73 global_dump->AddScalar(trace_event::MemoryAllocatorDump::kNameSize, | 69 global_dump->AddScalar(trace_event::MemoryAllocatorDump::kNameSize, |
74 trace_event::MemoryAllocatorDump::kUnitsBytes, | 70 trace_event::MemoryAllocatorDump::kUnitsBytes, size); |
75 size.second); | |
76 // TOOD(hajimehoshi): Detect which the shared memory comes from browser, | 71 // TOOD(hajimehoshi): Detect which the shared memory comes from browser, |
77 // renderer or GPU process. | 72 // renderer or GPU process. |
78 // TODO(hajimehoshi): Shared memory reported by GPU and discardable is | 73 // TODO(hajimehoshi): Shared memory reported by GPU and discardable is |
79 // currently double-counted. Add ownership edges to avoid this. | 74 // currently double-counted. Add ownership edges to avoid this. |
80 pmd->AddOwnershipEdge(local_dump->guid(), global_dump->guid()); | 75 pmd->AddOwnershipEdge(local_dump->guid(), global_dump->guid()); |
81 } | 76 } |
82 return true; | 77 return true; |
83 } | 78 } |
84 | 79 |
85 SharedMemoryTracker::SharedMemoryTracker() { | 80 SharedMemoryTracker::SharedMemoryTracker() { |
86 trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider( | 81 trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider( |
87 this, "SharedMemoryTracker", nullptr); | 82 this, "SharedMemoryTracker", nullptr); |
88 } | 83 } |
89 | 84 |
90 SharedMemoryTracker::~SharedMemoryTracker() = default; | 85 SharedMemoryTracker::~SharedMemoryTracker() = default; |
91 | 86 |
92 } // namespace | 87 } // namespace |
OLD | NEW |