Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(405)

Side by Side Diff: base/memory/shared_memory_tracker.cc

Issue 2873433004: Replace SharedMemory::UniqueID usages with SharedMemoryHandle's guid (Closed)
Patch Set: Address on reviews Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 std::unordered_map<const SharedMemory*, size_t> usages;
Primiano Tucci (use gerrit) 2017/05/10 18:33:55 see brett 's PSA on [chromium-dev] Please avoid st
hajimehoshi 2017/05/11 10:26:00 Thanks, I adopted std::map since I couldn't estima
52 sizes;
53 { 34 {
54 AutoLock hold(usages_lock_); 35 AutoLock hold(usages_lock_);
55 for (const auto& usage : usages_) 36 usages = usages_;
56 sizes[usage.second.unique_id] += usage.second.size;
57 } 37 }
58 for (auto& size : sizes) { 38 for (auto& usage : usages) {
59 const SharedMemory::UniqueId& id = size.first; 39 auto guid = usage.first->GetGUID();
Primiano Tucci (use gerrit) 2017/05/10 18:33:55 This seems racy. since you don't own the SharedMem
hajimehoshi 2017/05/11 10:26:00 Done.
60 std::string dump_name = StringPrintf("%s/%lld.%lld", "shared_memory", 40 // TODO(hajimehoshi): As passing ID across mojo is not implemented yet
61 static_cast<long long>(id.first), 41 // (crbug/713763), ID can be empty.
62 static_cast<long long>(id.second)); 42 if (guid.is_empty())
63 auto guid = trace_event::MemoryAllocatorDumpGuid(dump_name); 43 continue;
44 std::string dump_name = "shared_memory/" + guid.ToString();
45 auto dump_guid = trace_event::MemoryAllocatorDumpGuid(dump_name);
46 // The dump might already be created on single-process mode.
64 trace_event::MemoryAllocatorDump* local_dump = 47 trace_event::MemoryAllocatorDump* local_dump =
65 pmd->CreateAllocatorDump(dump_name); 48 pmd->GetAllocatorDump(dump_name);
49 if (local_dump)
50 continue;
51 local_dump = pmd->GetOrCreateAllocatorDump(dump_name);
66 // TODO(hajimehoshi): The size is not resident size but virtual size so far. 52 // TODO(hajimehoshi): The size is not resident size but virtual size so far.
67 // Fix this to record resident size. 53 // Fix this to record resident size.
68 local_dump->AddScalar(trace_event::MemoryAllocatorDump::kNameSize, 54 local_dump->AddScalar(trace_event::MemoryAllocatorDump::kNameSize,
69 trace_event::MemoryAllocatorDump::kUnitsBytes, 55 trace_event::MemoryAllocatorDump::kUnitsBytes,
70 size.second); 56 usage.second);
71 trace_event::MemoryAllocatorDump* global_dump = 57 trace_event::MemoryAllocatorDump* global_dump =
72 pmd->CreateSharedGlobalAllocatorDump(guid); 58 pmd->CreateSharedGlobalAllocatorDump(dump_guid);
73 global_dump->AddScalar(trace_event::MemoryAllocatorDump::kNameSize, 59 global_dump->AddScalar(trace_event::MemoryAllocatorDump::kNameSize,
74 trace_event::MemoryAllocatorDump::kUnitsBytes, 60 trace_event::MemoryAllocatorDump::kUnitsBytes,
75 size.second); 61 usage.second);
76 // TOOD(hajimehoshi): Detect which the shared memory comes from browser, 62 // TOOD(hajimehoshi): Detect which the shared memory comes from browser,
77 // renderer or GPU process. 63 // renderer or GPU process.
78 // TODO(hajimehoshi): Shared memory reported by GPU and discardable is 64 // TODO(hajimehoshi): Shared memory reported by GPU and discardable is
79 // currently double-counted. Add ownership edges to avoid this. 65 // currently double-counted. Add ownership edges to avoid this.
80 pmd->AddOwnershipEdge(local_dump->guid(), global_dump->guid()); 66 pmd->AddOwnershipEdge(local_dump->guid(), global_dump->guid());
81 } 67 }
82 return true; 68 return true;
83 } 69 }
84 70
85 SharedMemoryTracker::SharedMemoryTracker() { 71 SharedMemoryTracker::SharedMemoryTracker() {
86 trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider( 72 trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider(
87 this, "SharedMemoryTracker", nullptr); 73 this, "SharedMemoryTracker", nullptr);
88 } 74 }
89 75
90 SharedMemoryTracker::~SharedMemoryTracker() = default; 76 SharedMemoryTracker::~SharedMemoryTracker() = default;
91 77
92 } // namespace 78 } // namespace
OLDNEW
« base/memory/shared_memory.cc ('K') | « base/memory/shared_memory_tracker.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698