 Chromium Code Reviews
 Chromium Code Reviews Issue 2654073002:
  base: Introduce SharedMemoryTracker for POSIX (but not macOS)  (Closed)
    
  
    Issue 2654073002:
  base: Introduce SharedMemoryTracker for POSIX (but not macOS)  (Closed) 
  | 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 base::LazyInstance<SharedMemoryTracker> | |
| 15 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.
 | |
| 16 | |
| 17 SharedMemoryTracker* SharedMemoryTracker::GetInstance() { | |
| 18 return &g_shared_memory_tracker_instance.Get(); | |
| 19 } | |
| 20 | |
| 21 void SharedMemoryTracker::IncrementMemoryUsage( | |
| 22 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
 | |
| 23 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.
 | |
| 24 usages_[&shared_memory] = shared_memory.mapped_size(); | |
| 25 } | |
| 26 | |
| 27 void SharedMemoryTracker::DecrementMemoryUsage( | |
| 28 const SharedMemory& shared_memory) { | |
| 29 base::AutoLock hold(lock_); | |
| 30 usages_.erase(&shared_memory); | |
| 31 } | |
| 32 | |
| 33 bool SharedMemoryTracker::OnMemoryDump( | |
| 34 const base::trace_event::MemoryDumpArgs& args, | |
| 35 base::trace_event::ProcessMemoryDump* pmd) { | |
| 36 std::unordered_map<SharedMemory::UniqueId, size_t, SharedMemory::UniqueIdHash> | |
| 37 sizes; | |
| 38 { | |
| 39 base::AutoLock hold(lock_); | |
| 40 for (const auto& usage : usages_) { | |
| 41 SharedMemory::UniqueId id; | |
| 42 // TODO(hajimehoshi): There are some cases that the unique id is not | |
| 43 // available e.g. when the shared memory handler is handled after | |
| 44 // SharedMemory::TakeHandle is called. Fix such problems so that | |
| 45 // GetUniqueId to alwasys return id. | |
| 46 if (usage.first->GetUniqueId(&id)) | |
| 47 sizes[id] += usage.second; | |
| 48 } | |
| 49 } | |
| 50 for (auto& size : sizes) { | |
| 51 const SharedMemory::UniqueId& id = size.first; | |
| 52 std::string dump_name = base::StringPrintf( | |
| 53 "%s/%lld.%lld", "shared_memory", static_cast<long long>(id.first), | |
| 54 static_cast<long long>(id.second)); | |
| 55 auto guid = base::trace_event::MemoryAllocatorDumpGuid(dump_name); | |
| 56 base::trace_event::MemoryAllocatorDump* local_dump = | |
| 57 pmd->CreateAllocatorDump(dump_name); | |
| 58 // TODO(hajimehoshi): The size is not resident size but virtual size so far. | |
| 59 // Fix this to record resident size. | |
| 60 local_dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize, | |
| 61 base::trace_event::MemoryAllocatorDump::kUnitsBytes, | |
| 62 size.second); | |
| 63 base::trace_event::MemoryAllocatorDump* global_dump = | |
| 64 pmd->CreateSharedGlobalAllocatorDump(guid); | |
| 65 global_dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize, | |
| 66 base::trace_event::MemoryAllocatorDump::kUnitsBytes, | |
| 67 size.second); | |
| 68 // TOOD(hajimehoshi): Detect which the shared memory comes from browser, | |
| 69 // renderer or GPU process. | |
| 70 // TODO(hajimehoshi): Shared memory reported by GPU and discardable is | |
| 71 // currently double-counted. Add ownership edges to avoid this. | |
| 72 pmd->AddOwnershipEdge(local_dump->guid(), global_dump->guid()); | |
| 73 } | |
| 74 return true; | |
| 75 } | |
| 76 | |
| 77 SharedMemoryTracker::SharedMemoryTracker() { | |
| 78 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider( | |
| 79 this, "SharedMemoryTracker", nullptr); | |
| 80 } | |
| 81 | |
| 82 SharedMemoryTracker::~SharedMemoryTracker() = default; | |
| 83 | |
| 84 } // namespace | |
| OLD | NEW |