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::Usage::Usage() = default; | |
15 | |
16 SharedMemoryTracker::Usage::Usage(const Usage& rhs) = default; | |
17 | |
18 SharedMemoryTracker::Usage::~Usage() = default; | |
19 | |
20 // static | |
21 SharedMemoryTracker* SharedMemoryTracker::GetInstance() { | |
22 static SharedMemoryTracker* instance = new SharedMemoryTracker; | |
23 return instance; | |
24 } | |
25 | |
26 void SharedMemoryTracker::IncrementMemoryUsage( | |
27 const SharedMemory& shared_memory) { | |
28 AutoLock hold(lock_); | |
danakj
2017/03/06 16:06:04
can you just put the lock around the smallest numb
hajimehoshi
2017/03/07 06:22:34
Done.
| |
29 Usage usage; | |
30 SharedMemory::UniqueId id; | |
31 if (!shared_memory.GetUniqueId(&id)) | |
danakj
2017/03/06 16:06:04
One more Q: Why do you do this here instead of in
hajimehoshi
2017/03/07 06:22:34
I've not told the reason in this CL, but discussed
danakj
2017/03/07 16:14:22
Ah right, Close() might be called, and then fstat
hajimehoshi
2017/03/08 06:11:57
Done.
| |
32 return; | |
33 usage.unique_id = id; | |
34 usage.size = shared_memory.mapped_size(); | |
35 usages_[&shared_memory] = usage; | |
36 } | |
37 | |
38 void SharedMemoryTracker::DecrementMemoryUsage( | |
39 const SharedMemory& shared_memory) { | |
40 AutoLock hold(lock_); | |
41 usages_.erase(&shared_memory); | |
42 } | |
43 | |
44 bool SharedMemoryTracker::OnMemoryDump(const trace_event::MemoryDumpArgs& args, | |
45 trace_event::ProcessMemoryDump* pmd) { | |
46 std::unordered_map<SharedMemory::UniqueId, size_t, SharedMemory::UniqueIdHash> | |
47 sizes; | |
48 { | |
49 AutoLock hold(lock_); | |
50 for (const auto& usage : usages_) | |
51 sizes[usage.second.unique_id] += usage.second.size; | |
52 } | |
53 for (auto& size : sizes) { | |
54 const SharedMemory::UniqueId& id = size.first; | |
55 std::string dump_name = StringPrintf("%s/%lld.%lld", "shared_memory", | |
56 static_cast<long long>(id.first), | |
57 static_cast<long long>(id.second)); | |
58 auto guid = trace_event::MemoryAllocatorDumpGuid(dump_name); | |
59 trace_event::MemoryAllocatorDump* local_dump = | |
60 pmd->CreateAllocatorDump(dump_name); | |
61 // TODO(hajimehoshi): The size is not resident size but virtual size so far. | |
62 // Fix this to record resident size. | |
63 local_dump->AddScalar(trace_event::MemoryAllocatorDump::kNameSize, | |
64 trace_event::MemoryAllocatorDump::kUnitsBytes, | |
65 size.second); | |
66 trace_event::MemoryAllocatorDump* global_dump = | |
67 pmd->CreateSharedGlobalAllocatorDump(guid); | |
68 global_dump->AddScalar(trace_event::MemoryAllocatorDump::kNameSize, | |
69 trace_event::MemoryAllocatorDump::kUnitsBytes, | |
70 size.second); | |
71 // TOOD(hajimehoshi): Detect which the shared memory comes from browser, | |
72 // renderer or GPU process. | |
73 // TODO(hajimehoshi): Shared memory reported by GPU and discardable is | |
74 // currently double-counted. Add ownership edges to avoid this. | |
75 pmd->AddOwnershipEdge(local_dump->guid(), global_dump->guid()); | |
76 } | |
77 return true; | |
78 } | |
79 | |
80 SharedMemoryTracker::SharedMemoryTracker() { | |
81 trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider( | |
82 this, "SharedMemoryTracker", nullptr); | |
83 } | |
84 | |
85 SharedMemoryTracker::~SharedMemoryTracker() = default; | |
86 | |
87 } // namespace | |
OLD | NEW |