OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "base/memory/shared_memory_tracker.h" | |
6 | |
7 #include "base/memory/shared_memory.h" | |
8 #include "base/strings/stringprintf.h" | |
9 #include "base/trace_event/memory_dump_manager.h" | |
10 #include "base/trace_event/process_memory_dump.h" | |
11 | |
12 namespace base { | |
13 | |
14 SharedMemoryTracker* SharedMemoryTracker::GetInstance() { | |
15 return base::Singleton<SharedMemoryTracker, base::LeakySingletonTraits< | |
danakj
2017/02/24 15:30:28
I *think* the result of the "[chromium-dev] Making
hajimehoshi
2017/02/27 07:51:54
Done.
| |
16 SharedMemoryTracker>>::get(); | |
17 } | |
18 | |
19 bool SharedMemoryTracker::OnMemoryDump( | |
20 const base::trace_event::MemoryDumpArgs& args, | |
21 base::trace_event::ProcessMemoryDump* pmd) { | |
22 std::unordered_map<SharedMemory::Id, size_t, SharedMemory::IdHash> sizes; | |
23 { | |
24 base::AutoLock hold(lock_); | |
25 for (const auto& usage : usages_) { | |
26 SharedMemory::Id id; | |
27 usage.first->GetUniqueId(&id); | |
danakj
2017/02/24 15:30:28
What if this returns false?
hajimehoshi
2017/02/27 07:51:54
The sizes should not be recorded when it returns f
| |
28 sizes[id] += usage.second; | |
29 } | |
30 } | |
31 for (auto& size : sizes) { | |
32 const SharedMemory::Id& id = size.first; | |
33 std::string dump_name = base::StringPrintf( | |
34 "%s/%lld.%lld", "shared_memory", static_cast<long long>(id.first), | |
35 static_cast<long long>(id.second)); | |
36 auto guid = base::trace_event::MemoryAllocatorDumpGuid(dump_name); | |
37 base::trace_event::MemoryAllocatorDump* local_dump = | |
38 pmd->CreateAllocatorDump(dump_name); | |
39 // TODO(hajimehoshi): The size is not resident size but virtual size so far. | |
40 // Fix this to record resident size. | |
41 local_dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize, | |
42 base::trace_event::MemoryAllocatorDump::kUnitsBytes, | |
43 size.second); | |
44 base::trace_event::MemoryAllocatorDump* global_dump = | |
45 pmd->CreateSharedGlobalAllocatorDump(guid); | |
46 global_dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize, | |
47 base::trace_event::MemoryAllocatorDump::kUnitsBytes, | |
48 size.second); | |
49 // TOOD(hajimehoshi): Detect which the shared memory comes from browser, | |
50 // renderer or GPU process. | |
51 // TODO(hajimehoshi): Shared memory reported by GPU and discardable is | |
52 // currently double-counted. Add ownership edges to avoid this. | |
53 pmd->AddOwnershipEdge(local_dump->guid(), global_dump->guid()); | |
54 } | |
55 return true; | |
56 } | |
57 | |
58 void SharedMemoryTracker::IncrementMemoryUsage( | |
59 const SharedMemory& shared_memory) { | |
60 base::AutoLock hold(lock_); | |
61 usages_[&shared_memory] = shared_memory.mapped_size(); | |
62 } | |
63 | |
64 void SharedMemoryTracker::DecrementMemoryUsage( | |
65 const SharedMemory& shared_memory) { | |
66 base::AutoLock hold(lock_); | |
67 usages_.erase(&shared_memory); | |
68 } | |
69 | |
70 SharedMemoryTracker::SharedMemoryTracker() { | |
71 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider( | |
72 this, "SharedMemoryTracker", nullptr); | |
73 } | |
74 | |
75 SharedMemoryTracker::~SharedMemoryTracker() = default; | |
76 | |
77 } // namespace | |
OLD | NEW |