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 size_t size) { | |
48 return AddOwnershipEdges(pmd, source, shared_memory_handle, size, | |
49 trace_event::ProcessMemoryDump::kDefaultImportance); | |
50 } | |
51 | |
52 // static | |
53 bool SharedMemoryTracker::AddOwnershipEdges( | |
54 trace_event::ProcessMemoryDump* pmd, | |
55 const trace_event::MemoryAllocatorDumpGuid& source, | |
56 const SharedMemoryHandle& shared_memory_handle, | |
57 size_t size, | |
58 int importance) { | |
59 SharedMemory::UniqueId id; | |
60 if (!SharedMemory::GetUniqueId(shared_memory_handle, &id)) | |
61 return false; | |
62 | |
63 std::string id_str = SharedMemoryUniqueIdToString(id); | |
64 auto in_process_dump_name = | |
65 StringPrintf("shared_memory_in_process/%s", id_str.c_str()); | |
Primiano Tucci (use gerrit)
2017/03/29 17:19:50
why not just "shared_memory"?
hajimehoshi
2017/03/30 07:49:25
I thought this dump is necessary but I was wrong.
| |
66 auto* in_process_dump = pmd->CreateAllocatorDump(in_process_dump_name); | |
67 pmd->AddOwnershipEdge(source, in_process_dump->guid()); | |
68 in_process_dump->AddScalar(trace_event::MemoryAllocatorDump::kNameSize, | |
69 trace_event::MemoryAllocatorDump::kUnitsBytes, | |
70 size); | |
71 | |
72 trace_event::MemoryAllocatorDumpGuid global_guid(id_str); | |
73 pmd->AddOwnershipEdge(in_process_dump->guid(), global_guid, importance); | |
74 return true; | |
75 } | |
76 | |
26 void SharedMemoryTracker::IncrementMemoryUsage( | 77 void SharedMemoryTracker::IncrementMemoryUsage( |
27 const SharedMemory& shared_memory) { | 78 const SharedMemory& shared_memory) { |
28 Usage usage; | 79 Usage usage; |
29 // |shared_memory|'s unique ID must be generated here and it'd be too late at | 80 // |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 | 81 // OnMemoryDump. An ID is generated with a SharedMemoryHandle, but the handle |
31 // might already be closed at that time. Now IncrementMemoryUsage is called | 82 // 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 | 83 // just after mmap and the handle must live then. See the discussion at |
33 // crbug.com/604726#c30. | 84 // crbug.com/604726#c30. |
34 SharedMemory::UniqueId id; | 85 SharedMemory::UniqueId id; |
35 if (!shared_memory.GetUniqueId(&id)) | 86 if (!SharedMemory::GetUniqueId(shared_memory.handle(), &id)) |
36 return; | 87 return; |
37 usage.unique_id = id; | 88 usage.unique_id = id; |
38 usage.size = shared_memory.mapped_size(); | 89 usage.size = shared_memory.mapped_size(); |
39 AutoLock hold(usages_lock_); | 90 AutoLock hold(usages_lock_); |
40 usages_[&shared_memory] = usage; | 91 usages_[&shared_memory] = usage; |
41 } | 92 } |
42 | 93 |
43 void SharedMemoryTracker::DecrementMemoryUsage( | 94 void SharedMemoryTracker::DecrementMemoryUsage( |
44 const SharedMemory& shared_memory) { | 95 const SharedMemory& shared_memory) { |
45 AutoLock hold(usages_lock_); | 96 AutoLock hold(usages_lock_); |
46 usages_.erase(&shared_memory); | 97 usages_.erase(&shared_memory); |
47 } | 98 } |
48 | 99 |
49 bool SharedMemoryTracker::OnMemoryDump(const trace_event::MemoryDumpArgs& args, | 100 bool SharedMemoryTracker::OnMemoryDump(const trace_event::MemoryDumpArgs& args, |
50 trace_event::ProcessMemoryDump* pmd) { | 101 trace_event::ProcessMemoryDump* pmd) { |
51 std::unordered_map<SharedMemory::UniqueId, size_t, SharedMemory::UniqueIdHash> | 102 std::unordered_map<SharedMemory::UniqueId, size_t, SharedMemory::UniqueIdHash> |
52 sizes; | 103 sizes; |
53 { | 104 { |
54 AutoLock hold(usages_lock_); | 105 AutoLock hold(usages_lock_); |
55 for (const auto& usage : usages_) | 106 for (const auto& usage : usages_) |
56 sizes[usage.second.unique_id] += usage.second.size; | 107 sizes[usage.second.unique_id] += usage.second.size; |
57 } | 108 } |
58 for (auto& size : sizes) { | 109 for (auto& size : sizes) { |
59 const SharedMemory::UniqueId& id = size.first; | 110 const SharedMemory::UniqueId& id = size.first; |
60 std::string dump_name = StringPrintf("%s/%lld.%lld", "shared_memory", | 111 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); | 112 auto guid = trace_event::MemoryAllocatorDumpGuid(dump_name); |
64 trace_event::MemoryAllocatorDump* local_dump = | 113 trace_event::MemoryAllocatorDump* local_dump = |
65 pmd->CreateAllocatorDump(dump_name); | 114 pmd->CreateAllocatorDump(dump_name); |
66 // TODO(hajimehoshi): The size is not resident size but virtual size so far. | 115 // TODO(hajimehoshi): The size is not resident size but virtual size so far. |
67 // Fix this to record resident size. | 116 // Fix this to record resident size. |
68 local_dump->AddScalar(trace_event::MemoryAllocatorDump::kNameSize, | 117 local_dump->AddScalar(trace_event::MemoryAllocatorDump::kNameSize, |
69 trace_event::MemoryAllocatorDump::kUnitsBytes, | 118 trace_event::MemoryAllocatorDump::kUnitsBytes, |
70 size.second); | 119 size.second); |
71 trace_event::MemoryAllocatorDump* global_dump = | 120 trace_event::MemoryAllocatorDump* global_dump = |
72 pmd->CreateSharedGlobalAllocatorDump(guid); | 121 pmd->CreateSharedGlobalAllocatorDump(guid); |
(...skipping 10 matching lines...) Expand all Loading... | |
83 } | 132 } |
84 | 133 |
85 SharedMemoryTracker::SharedMemoryTracker() { | 134 SharedMemoryTracker::SharedMemoryTracker() { |
86 trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider( | 135 trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider( |
87 this, "SharedMemoryTracker", nullptr); | 136 this, "SharedMemoryTracker", nullptr); |
88 } | 137 } |
89 | 138 |
90 SharedMemoryTracker::~SharedMemoryTracker() = default; | 139 SharedMemoryTracker::~SharedMemoryTracker() = default; |
91 | 140 |
92 } // namespace | 141 } // namespace |
OLD | NEW |