Chromium Code Reviews| 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 static const char* kSharedMemoryDumpName = "shared_memory"; | |
|
Primiano Tucci (use gerrit)
2017/01/27 20:09:49
you use this only in one place. I'd just inline it
hajimehoshi
2017/02/07 12:33:44
Done.
| |
| 15 | |
| 16 SharedMemoryTracker* SharedMemoryTracker::GetInstance() { | |
| 17 return base::Singleton<SharedMemoryTracker, base::LeakySingletonTraits< | |
| 18 SharedMemoryTracker>>::get(); | |
| 19 } | |
| 20 | |
| 21 bool SharedMemoryTracker::OnMemoryDump( | |
| 22 const base::trace_event::MemoryDumpArgs& args, | |
| 23 base::trace_event::ProcessMemoryDump* pmd) { | |
| 24 for (auto& bucket : buckets_) { | |
| 25 base::AutoLock lock(bucket.lock); | |
| 26 for (const auto& usage : bucket.usages) { | |
| 27 SharedMemory::Id id; | |
| 28 usage.first->GetUniqueId(&id); | |
|
danakj
2017/01/27 16:56:42
I think you need to consolidate these ids first cu
Primiano Tucci (use gerrit)
2017/01/27 20:09:49
Hmm interesting. I would have *not* expected that
hajimehoshi
2017/02/07 12:33:44
Hmm, I also didn't expect such situation like prim
hajimehoshi
2017/02/10 11:05:58
FYI: This happens when the ID is 0. fstat(3) fails
| |
| 29 std::string dump_name = base::StringPrintf( | |
| 30 "%s/%lld.%lld", kSharedMemoryDumpName, | |
| 31 static_cast<long long>(id.first), static_cast<long long>(id.second)); | |
| 32 auto guid = base::trace_event::MemoryAllocatorDumpGuid(dump_name); | |
| 33 base::trace_event::MemoryAllocatorDump* local_dump = | |
| 34 pmd->CreateAllocatorDump(dump_name); | |
| 35 // TODO(hajimehoshi): The size is not resident size but virtual size so | |
| 36 // far. Fix this to record resident size. | |
| 37 local_dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize, | |
| 38 base::trace_event::MemoryAllocatorDump::kUnitsBytes, | |
| 39 usage.second); | |
| 40 base::trace_event::MemoryAllocatorDump* global_dump = | |
| 41 pmd->CreateSharedGlobalAllocatorDump(guid); | |
| 42 global_dump->AddScalar( | |
| 43 base::trace_event::MemoryAllocatorDump::kNameSize, | |
| 44 base::trace_event::MemoryAllocatorDump::kUnitsBytes, usage.second); | |
| 45 // TOOD(hajimehoshi): Detect which the shared memory comes from browser, | |
| 46 // renderer or GPU process. | |
| 47 // TODO(hajimehoshi): Shared memory reported by GPU and discardable is | |
| 48 // currently double-counted. Add ownership edges to avoid this. | |
| 49 pmd->AddOwnershipEdge(local_dump->guid(), global_dump->guid()); | |
| 50 } | |
| 51 } | |
| 52 return true; | |
| 53 } | |
| 54 | |
| 55 bool SharedMemoryTracker::IncrementMemoryUsage( | |
| 56 const SharedMemory& shared_memory) { | |
| 57 int bucket = reinterpret_cast<uintptr_t>(&shared_memory) % kTrackerBucketNum; | |
| 58 base::AutoLock lock(buckets_[bucket].lock); | |
|
danakj
2017/01/27 16:56:42
nit: name the AutoLock |hold|. It's not a lock, it
hajimehoshi
2017/02/07 12:33:44
Done.
| |
| 59 buckets_[bucket].usages[&shared_memory] = shared_memory.mapped_size(); | |
| 60 return true; | |
| 61 } | |
| 62 | |
| 63 bool SharedMemoryTracker::DecrementMemoryUsage( | |
| 64 const SharedMemory& shared_memory) { | |
| 65 int bucket = reinterpret_cast<uintptr_t>(&shared_memory) % kTrackerBucketNum; | |
| 66 base::AutoLock lock(buckets_[bucket].lock); | |
|
danakj
2017/01/27 16:56:42
same here
hajimehoshi
2017/02/07 12:33:44
Done.
| |
| 67 buckets_[bucket].usages.erase(&shared_memory); | |
| 68 return true; | |
| 69 } | |
| 70 | |
| 71 SharedMemoryTracker::SharedMemoryTracker() { | |
| 72 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider( | |
| 73 this, "SharedMemoryTracker", nullptr); | |
| 74 } | |
| 75 | |
| 76 SharedMemoryTracker::~SharedMemoryTracker() = default; | |
| 77 | |
| 78 SharedMemoryTracker::TrackerBucket::TrackerBucket() = default; | |
| 79 | |
| 80 SharedMemoryTracker::TrackerBucket::~TrackerBucket() = default; | |
| 81 | |
| 82 } // namespace | |
| OLD | NEW |