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

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

Issue 2535213002: [WIP] Add SharedMemoryTracker to dump base::SharedMemory usage
Patch Set: Implement buckets Created 3 years, 11 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
(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 const char* kSharedMemoryDumpName = "sharedmemory";
15
16 SharedMemoryTracker* SharedMemoryTracker::GetInstance() {
17 return base::Singleton<SharedMemoryTracker, base::LeakySingletonTraits<
18 SharedMemoryTracker>>::get();
19 }
20
21 bool SharedMemoryTracker::AddOwnershipEdgeToSharedGlobalDump(
22 trace_event::ProcessMemoryDump* pmd,
23 const trace_event::MemoryAllocatorDumpGuid& source,
24 const SharedMemoryHandle& shared_memory_handle,
25 size_t mapped_size) {
26 SharedMemoryHandleID id;
27 if (!GetIDFromSharedMemoryHandle(shared_memory_handle, &id)) {
28 return false;
29 }
30 std::string id_str = GetSharedMemoryHandleIDString(id);
31 std::string dump_name =
32 base::StringPrintf("%s/%s", kSharedMemoryDumpName, id_str.c_str());
33 auto global_dump = pmd->CreateSharedGlobalAllocatorDump(
34 base::trace_event::MemoryAllocatorDumpGuid(dump_name));
35 global_dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize,
36 base::trace_event::MemoryAllocatorDump::kUnitsBytes,
37 mapped_size);
38 pmd->AddOwnershipEdge(source, global_dump->guid());
39 return true;
40 }
41
42 bool SharedMemoryTracker::OnMemoryDump(
43 const base::trace_event::MemoryDumpArgs& args,
44 base::trace_event::ProcessMemoryDump* pmd) {
45 for (auto& bucket : buckets_) {
46 base::AutoLock lock(bucket.lock);
47 for (const auto& usage_in_device : bucket.usage) {
48 for (const auto& usage : usage_in_device.second) {
49 SharedMemoryHandleID id;
50 id.device_id = usage_in_device.first;
51 id.file_id = usage.first;
52 std::string id_str = GetSharedMemoryHandleIDString(id);
53 std::string dump_name =
54 base::StringPrintf("%s/%s", kSharedMemoryDumpName, id_str.c_str());
55 auto guid = base::trace_event::MemoryAllocatorDumpGuid(dump_name);
56 base::trace_event::MemoryAllocatorDump* local_dump =
57 pmd->CreateAllocatorDump(dump_name);
58 local_dump->AddScalar(
59 base::trace_event::MemoryAllocatorDump::kNameSize,
60 base::trace_event::MemoryAllocatorDump::kUnitsBytes, usage.second);
61 base::trace_event::MemoryAllocatorDump* global_dump =
62 pmd->CreateSharedGlobalAllocatorDump(guid);
63 global_dump->AddScalar(
64 base::trace_event::MemoryAllocatorDump::kNameSize,
65 base::trace_event::MemoryAllocatorDump::kUnitsBytes, usage.second);
66 pmd->AddOwnershipEdge(local_dump->guid(), global_dump->guid());
67 }
68 }
69 }
70 return true;
71 }
72
73 bool SharedMemoryTracker::IncrementMemoryUsage(
74 const SharedMemory& shared_memory) {
75 SharedMemoryHandleID id;
76 if (!GetIDFromSharedMemoryHandle(shared_memory.handle(), &id)) {
77 return false;
78 }
79 int bucket = id.file_id % kTrackerBucketNum;
80 base::AutoLock lock(buckets_[bucket].lock);
81 buckets_[bucket].usage[id.device_id][id.file_id] =
82 shared_memory.mapped_size();
83 return true;
84 }
85
86 bool SharedMemoryTracker::DecrementMemoryUsage(
87 const SharedMemory& shared_memory) {
88 SharedMemoryHandleID id;
89 if (!GetIDFromSharedMemoryHandle(shared_memory.handle(), &id)) {
90 return false;
91 }
92 int bucket = id.file_id % kTrackerBucketNum;
93 base::AutoLock lock(buckets_[bucket].lock);
94 buckets_[bucket].usage[id.device_id].erase(id.file_id);
95 return true;
96 }
97
98 SharedMemoryTracker::SharedMemoryTracker() {
99 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider(
100 this, "SharedMemoryTracker", nullptr);
101 }
102
103 SharedMemoryTracker::~SharedMemoryTracker() {}
104
105 SharedMemoryTracker::TrackerBucket::TrackerBucket() {}
106
107 SharedMemoryTracker::TrackerBucket::~TrackerBucket() {}
108
109 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698