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<base::UnguessableToken, uintptr_t, size_t>> usages; |
danakj
2017/05/24 16:39:45
no base:: inside base, throughout this CL
hajimehoshi
2017/05/25 00:33:30
Done.
| |
52 sizes; | |
53 { | 35 { |
54 AutoLock hold(usages_lock_); | 36 AutoLock hold(usages_lock_); |
37 usages.reserve(usages_.size()); | |
55 for (const auto& usage : usages_) | 38 for (const auto& usage : usages_) |
Primiano Tucci (use gerrit)
2017/05/24 11:23:37
small nit: add a curly brace around the for if the
hajimehoshi
2017/05/25 00:33:30
Done.
| |
56 sizes[usage.second.unique_id] += usage.second.size; | 39 usages.emplace_back(usage.first->handle().GetGUID(), |
hajimehoshi
2017/05/22 08:32:01
I found that this implementation (before this CL)
erikchen
2017/05/22 16:19:31
We need to fix the interface of ProcessMemoryDump
| |
40 reinterpret_cast<uintptr_t>(usage.first->memory()), | |
41 usage.second); | |
57 } | 42 } |
58 for (auto& size : sizes) { | 43 for (const auto& usage : usages) { |
59 const SharedMemory::UniqueId& id = size.first; | 44 const base::UnguessableToken& guid = std::get<0>(usage); |
danakj
2017/05/24 16:39:45
nit: memory_guid? since there's 2 guids in this fu
hajimehoshi
2017/05/25 00:33:30
Done.
| |
60 std::string dump_name = StringPrintf("%s/%lld.%lld", "shared_memory", | 45 uintptr_t address = std::get<1>(usage); |
61 static_cast<long long>(id.first), | 46 size_t size = std::get<2>(usage); |
62 static_cast<long long>(id.second)); | 47 std::string dump_name = "shared_memory/"; |
63 auto guid = trace_event::MemoryAllocatorDumpGuid(dump_name); | 48 if (guid.is_empty()) { |
49 // TODO(hajimehoshi): As passing ID across mojo is not implemented yet | |
50 // (crbug/713763), ID can be empty. For such case, use an address instead | |
51 // of GUID so that approximate memory usages are available. | |
52 dump_name += "(address)" + base::Uint64ToString(address); | |
Primiano Tucci (use gerrit)
2017/05/24 11:23:37
just uint64tostring should be fine, without the ex
hajimehoshi
2017/05/25 00:33:29
Hmm, so both will be just hex-integer strings so I
| |
53 } else { | |
54 dump_name += guid.ToString(); | |
55 } | |
56 auto dump_guid = trace_event::MemoryAllocatorDumpGuid(dump_name); | |
57 // The dump might already be created on single-process mode. | |
Primiano Tucci (use gerrit)
2017/05/24 11:23:37
took me a while to realize that this was referring
danakj
2017/05/24 16:39:45
I would not use the local variable for GetAllocato
hajimehoshi
2017/05/25 00:33:30
Done.
| |
64 trace_event::MemoryAllocatorDump* local_dump = | 58 trace_event::MemoryAllocatorDump* local_dump = |
65 pmd->CreateAllocatorDump(dump_name); | 59 pmd->GetAllocatorDump(dump_name); |
60 if (local_dump) | |
61 continue; | |
62 local_dump = 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 |