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" | |
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 |
14 SharedMemoryTracker::Usage::Usage() = default; | |
15 | |
16 SharedMemoryTracker::Usage::Usage(const Usage& rhs) = default; | |
17 | |
18 SharedMemoryTracker::Usage::~Usage() = default; | |
19 | |
20 // static | 13 // static |
21 SharedMemoryTracker* SharedMemoryTracker::GetInstance() { | 14 SharedMemoryTracker* SharedMemoryTracker::GetInstance() { |
22 static SharedMemoryTracker* instance = new SharedMemoryTracker; | 15 static SharedMemoryTracker* instance = new SharedMemoryTracker; |
23 return instance; | 16 return instance; |
24 } | 17 } |
25 | 18 |
26 void SharedMemoryTracker::IncrementMemoryUsage( | 19 void SharedMemoryTracker::IncrementMemoryUsage( |
27 const SharedMemory& shared_memory) { | 20 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_); | 21 AutoLock hold(usages_lock_); |
40 usages_[&shared_memory] = usage; | 22 usages_[&shared_memory] = shared_memory.mapped_size(); |
41 } | 23 } |
42 | 24 |
43 void SharedMemoryTracker::DecrementMemoryUsage( | 25 void SharedMemoryTracker::DecrementMemoryUsage( |
44 const SharedMemory& shared_memory) { | 26 const SharedMemory& shared_memory) { |
45 AutoLock hold(usages_lock_); | 27 AutoLock hold(usages_lock_); |
46 usages_.erase(&shared_memory); | 28 usages_.erase(&shared_memory); |
47 } | 29 } |
48 | 30 |
49 bool SharedMemoryTracker::OnMemoryDump(const trace_event::MemoryDumpArgs& args, | 31 bool SharedMemoryTracker::OnMemoryDump(const trace_event::MemoryDumpArgs& args, |
50 trace_event::ProcessMemoryDump* pmd) { | 32 trace_event::ProcessMemoryDump* pmd) { |
51 std::unordered_map<SharedMemory::UniqueId, size_t, SharedMemory::UniqueIdHash> | 33 std::vector<std::tuple<base::UnguessableToken, size_t>> usages; |
52 sizes; | |
53 { | 34 { |
54 AutoLock hold(usages_lock_); | 35 AutoLock hold(usages_lock_); |
55 for (const auto& usage : usages_) | 36 usages.reserve(usages_.size()); |
56 sizes[usage.second.unique_id] += usage.second.size; | 37 for (auto& usage : usages_) |
erikchen
2017/05/11 21:54:40
The C++ style guide says to only use auto "to avoi
hajimehoshi
2017/05/12 09:27:31
Done. I think auto for map iterators is encouraged
| |
38 usages.emplace_back(usage.first->handle().GetGUID(), usage.second); | |
57 } | 39 } |
58 for (auto& size : sizes) { | 40 for (auto& usage : usages) { |
59 const SharedMemory::UniqueId& id = size.first; | 41 auto guid = std::get<0>(usage); |
Primiano Tucci (use gerrit)
2017/05/11 15:10:43
const auto&?
hajimehoshi
2017/05/12 09:27:31
Done.
| |
60 std::string dump_name = StringPrintf("%s/%lld.%lld", "shared_memory", | 42 auto size = std::get<1>(usage); |
61 static_cast<long long>(id.first), | 43 // TODO(hajimehoshi): As passing ID across mojo is not implemented yet |
62 static_cast<long long>(id.second)); | 44 // (crbug/713763), ID can be empty. |
63 auto guid = trace_event::MemoryAllocatorDumpGuid(dump_name); | 45 if (guid.is_empty()) |
Primiano Tucci (use gerrit)
2017/05/11 15:10:43
see my previous comment, copy-pasting
----
However
hajimehoshi
2017/05/12 09:27:31
Ah sorry I missed that comment. As all shared memo
| |
46 continue; | |
47 std::string dump_name = "shared_memory/" + guid.ToString(); | |
48 auto dump_guid = trace_event::MemoryAllocatorDumpGuid(dump_name); | |
49 // The dump might already be created on single-process mode. | |
64 trace_event::MemoryAllocatorDump* local_dump = | 50 trace_event::MemoryAllocatorDump* local_dump = |
65 pmd->CreateAllocatorDump(dump_name); | 51 pmd->GetAllocatorDump(dump_name); |
52 if (local_dump) | |
erikchen
2017/05/11 21:54:40
This logic looks new? In general, we shouldn't nee
hajimehoshi
2017/05/12 09:27:31
Ah, in theory this conflict can happen even before
hajimehoshi
2017/05/17 10:13:35
ping primiano@
| |
53 continue; | |
54 local_dump = pmd->GetOrCreateAllocatorDump(dump_name); | |
66 // TODO(hajimehoshi): The size is not resident size but virtual size so far. | 55 // TODO(hajimehoshi): The size is not resident size but virtual size so far. |
67 // Fix this to record resident size. | 56 // Fix this to record resident size. |
68 local_dump->AddScalar(trace_event::MemoryAllocatorDump::kNameSize, | 57 local_dump->AddScalar(trace_event::MemoryAllocatorDump::kNameSize, |
69 trace_event::MemoryAllocatorDump::kUnitsBytes, | 58 trace_event::MemoryAllocatorDump::kUnitsBytes, size); |
70 size.second); | |
71 trace_event::MemoryAllocatorDump* global_dump = | 59 trace_event::MemoryAllocatorDump* global_dump = |
72 pmd->CreateSharedGlobalAllocatorDump(guid); | 60 pmd->CreateSharedGlobalAllocatorDump(dump_guid); |
73 global_dump->AddScalar(trace_event::MemoryAllocatorDump::kNameSize, | 61 global_dump->AddScalar(trace_event::MemoryAllocatorDump::kNameSize, |
74 trace_event::MemoryAllocatorDump::kUnitsBytes, | 62 trace_event::MemoryAllocatorDump::kUnitsBytes, size); |
75 size.second); | |
76 // TOOD(hajimehoshi): Detect which the shared memory comes from browser, | 63 // TOOD(hajimehoshi): Detect which the shared memory comes from browser, |
77 // renderer or GPU process. | 64 // renderer or GPU process. |
78 // TODO(hajimehoshi): Shared memory reported by GPU and discardable is | 65 // TODO(hajimehoshi): Shared memory reported by GPU and discardable is |
79 // currently double-counted. Add ownership edges to avoid this. | 66 // currently double-counted. Add ownership edges to avoid this. |
80 pmd->AddOwnershipEdge(local_dump->guid(), global_dump->guid()); | 67 pmd->AddOwnershipEdge(local_dump->guid(), global_dump->guid()); |
81 } | 68 } |
82 return true; | 69 return true; |
83 } | 70 } |
84 | 71 |
85 SharedMemoryTracker::SharedMemoryTracker() { | 72 SharedMemoryTracker::SharedMemoryTracker() { |
86 trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider( | 73 trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider( |
87 this, "SharedMemoryTracker", nullptr); | 74 this, "SharedMemoryTracker", nullptr); |
88 } | 75 } |
89 | 76 |
90 SharedMemoryTracker::~SharedMemoryTracker() = default; | 77 SharedMemoryTracker::~SharedMemoryTracker() = default; |
91 | 78 |
92 } // namespace | 79 } // namespace |
OLD | NEW |