Chromium Code Reviews| Index: base/memory/shared_memory_tracker.cc |
| diff --git a/base/memory/shared_memory_tracker.cc b/base/memory/shared_memory_tracker.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a27a01ead9b81a988577665926383e8eb481287b |
| --- /dev/null |
| +++ b/base/memory/shared_memory_tracker.cc |
| @@ -0,0 +1,84 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/memory/shared_memory_tracker.h" |
| + |
| +#include "base/memory/shared_memory.h" |
| +#include "base/strings/stringprintf.h" |
| +#include "base/trace_event/memory_dump_manager.h" |
| +#include "base/trace_event/process_memory_dump.h" |
| + |
| +namespace base { |
| + |
| +static base::LazyInstance<SharedMemoryTracker> |
| + g_shared_memory_tracker_instance = LAZY_INSTANCE_INITIALIZER; |
|
Primiano Tucci (use gerrit)
2017/02/27 14:10:50
1. I think you want this to be a leaky instance as
hajimehoshi
2017/02/28 06:59:25
That's good to know. Done.
|
| + |
| +SharedMemoryTracker* SharedMemoryTracker::GetInstance() { |
| + return &g_shared_memory_tracker_instance.Get(); |
| +} |
| + |
| +void SharedMemoryTracker::IncrementMemoryUsage( |
| + const SharedMemory& shared_memory) { |
|
Primiano Tucci (use gerrit)
2017/02/27 14:10:50
I thought I added a comment before, might have got
hajimehoshi
2017/02/28 06:59:25
Hmm, sounds a good idea, but Increment/DecrementMe
|
| + base::AutoLock hold(lock_); |
|
Primiano Tucci (use gerrit)
2017/02/27 14:10:50
you are already in namespace base here, so don't n
hajimehoshi
2017/02/28 06:59:25
Done.
|
| + usages_[&shared_memory] = shared_memory.mapped_size(); |
| +} |
| + |
| +void SharedMemoryTracker::DecrementMemoryUsage( |
| + const SharedMemory& shared_memory) { |
| + base::AutoLock hold(lock_); |
| + usages_.erase(&shared_memory); |
| +} |
| + |
| +bool SharedMemoryTracker::OnMemoryDump( |
| + const base::trace_event::MemoryDumpArgs& args, |
| + base::trace_event::ProcessMemoryDump* pmd) { |
| + std::unordered_map<SharedMemory::UniqueId, size_t, SharedMemory::UniqueIdHash> |
| + sizes; |
| + { |
| + base::AutoLock hold(lock_); |
| + for (const auto& usage : usages_) { |
| + SharedMemory::UniqueId id; |
| + // TODO(hajimehoshi): There are some cases that the unique id is not |
| + // available e.g. when the shared memory handler is handled after |
| + // SharedMemory::TakeHandle is called. Fix such problems so that |
| + // GetUniqueId to alwasys return id. |
| + if (usage.first->GetUniqueId(&id)) |
| + sizes[id] += usage.second; |
| + } |
| + } |
| + for (auto& size : sizes) { |
| + const SharedMemory::UniqueId& id = size.first; |
| + std::string dump_name = base::StringPrintf( |
| + "%s/%lld.%lld", "shared_memory", static_cast<long long>(id.first), |
| + static_cast<long long>(id.second)); |
| + auto guid = base::trace_event::MemoryAllocatorDumpGuid(dump_name); |
| + base::trace_event::MemoryAllocatorDump* local_dump = |
| + pmd->CreateAllocatorDump(dump_name); |
| + // TODO(hajimehoshi): The size is not resident size but virtual size so far. |
| + // Fix this to record resident size. |
| + local_dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize, |
| + base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
| + size.second); |
| + base::trace_event::MemoryAllocatorDump* global_dump = |
| + pmd->CreateSharedGlobalAllocatorDump(guid); |
| + global_dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize, |
| + base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
| + size.second); |
| + // TOOD(hajimehoshi): Detect which the shared memory comes from browser, |
| + // renderer or GPU process. |
| + // TODO(hajimehoshi): Shared memory reported by GPU and discardable is |
| + // currently double-counted. Add ownership edges to avoid this. |
| + pmd->AddOwnershipEdge(local_dump->guid(), global_dump->guid()); |
| + } |
| + return true; |
| +} |
| + |
| +SharedMemoryTracker::SharedMemoryTracker() { |
| + base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider( |
| + this, "SharedMemoryTracker", nullptr); |
| +} |
| + |
| +SharedMemoryTracker::~SharedMemoryTracker() = default; |
| + |
| +} // namespace |