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 AutoLock hold(usages_lock_); |
danakj
2017/05/09 14:54:18
wdyt of copying the usages_ into a local map with
hajimehoshi
2017/05/10 05:55:22
I don't have a strong opinion: OnMemoryDump is cal
Primiano Tucci (use gerrit)
2017/05/10 18:33:55
This is true right now, but very soon we want to u
| |
52 sizes; | 34 for (auto& usage : usages_) { |
53 { | 35 auto guid = usage.first->GetGUID(); |
54 AutoLock hold(usages_lock_); | 36 // TODO(hajimehoshi): As passing ID across mojo is not implemented yet |
danakj
2017/05/09 14:54:18
Since this patch effectively doesn't work until th
hajimehoshi
2017/05/10 05:55:22
Right, but the current memory-infra doesn't show r
Primiano Tucci (use gerrit)
2017/05/10 18:33:55
I think we want this before the mojo fix comes (sa
| |
55 for (const auto& usage : usages_) | 37 // (crbug/713763), ID can be empty. |
56 sizes[usage.second.unique_id] += usage.second.size; | 38 if (guid.is_empty()) |
57 } | 39 continue; |
58 for (auto& size : sizes) { | 40 std::string dump_name = "shared_memory/" + guid.ToString(); |
59 const SharedMemory::UniqueId& id = size.first; | 41 auto dump_guid = trace_event::MemoryAllocatorDumpGuid(dump_name); |
60 std::string dump_name = StringPrintf("%s/%lld.%lld", "shared_memory", | 42 // The dump might already be created on single-process mode. |
61 static_cast<long long>(id.first), | |
62 static_cast<long long>(id.second)); | |
63 auto guid = trace_event::MemoryAllocatorDumpGuid(dump_name); | |
64 trace_event::MemoryAllocatorDump* local_dump = | 43 trace_event::MemoryAllocatorDump* local_dump = |
65 pmd->CreateAllocatorDump(dump_name); | 44 pmd->GetAllocatorDump(dump_name); |
45 if (local_dump) | |
46 continue; | |
47 local_dump = pmd->GetOrCreateAllocatorDump(dump_name); | |
66 // TODO(hajimehoshi): The size is not resident size but virtual size so far. | 48 // TODO(hajimehoshi): The size is not resident size but virtual size so far. |
67 // Fix this to record resident size. | 49 // Fix this to record resident size. |
68 local_dump->AddScalar(trace_event::MemoryAllocatorDump::kNameSize, | 50 local_dump->AddScalar(trace_event::MemoryAllocatorDump::kNameSize, |
69 trace_event::MemoryAllocatorDump::kUnitsBytes, | 51 trace_event::MemoryAllocatorDump::kUnitsBytes, |
70 size.second); | 52 usage.second); |
71 trace_event::MemoryAllocatorDump* global_dump = | 53 trace_event::MemoryAllocatorDump* global_dump = |
72 pmd->CreateSharedGlobalAllocatorDump(guid); | 54 pmd->CreateSharedGlobalAllocatorDump(dump_guid); |
73 global_dump->AddScalar(trace_event::MemoryAllocatorDump::kNameSize, | 55 global_dump->AddScalar(trace_event::MemoryAllocatorDump::kNameSize, |
74 trace_event::MemoryAllocatorDump::kUnitsBytes, | 56 trace_event::MemoryAllocatorDump::kUnitsBytes, |
75 size.second); | 57 usage.second); |
76 // TOOD(hajimehoshi): Detect which the shared memory comes from browser, | 58 // TOOD(hajimehoshi): Detect which the shared memory comes from browser, |
77 // renderer or GPU process. | 59 // renderer or GPU process. |
78 // TODO(hajimehoshi): Shared memory reported by GPU and discardable is | 60 // TODO(hajimehoshi): Shared memory reported by GPU and discardable is |
79 // currently double-counted. Add ownership edges to avoid this. | 61 // currently double-counted. Add ownership edges to avoid this. |
80 pmd->AddOwnershipEdge(local_dump->guid(), global_dump->guid()); | 62 pmd->AddOwnershipEdge(local_dump->guid(), global_dump->guid()); |
81 } | 63 } |
82 return true; | 64 return true; |
83 } | 65 } |
84 | 66 |
85 SharedMemoryTracker::SharedMemoryTracker() { | 67 SharedMemoryTracker::SharedMemoryTracker() { |
86 trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider( | 68 trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider( |
87 this, "SharedMemoryTracker", nullptr); | 69 this, "SharedMemoryTracker", nullptr); |
88 } | 70 } |
89 | 71 |
90 SharedMemoryTracker::~SharedMemoryTracker() = default; | 72 SharedMemoryTracker::~SharedMemoryTracker() = default; |
91 | 73 |
92 } // namespace | 74 } // namespace |
OLD | NEW |