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 #ifndef BASE_MEMORY_SHARED_MEMORY_TRACKER_H_ 6 #define BASE_MEMORY_SHARED_MEMORY_TRACKER_H_ 7 8 #include <map> 9 #include <string> 10 11 #include "base/memory/shared_memory.h" 12 #include "base/memory/shared_memory_mapping.h" 13 #include "base/synchronization/lock.h" 14 #include "base/trace_event/memory_dump_provider.h" 15 16 namespace base { 17 18 namespace trace_event { 19 class MemoryAllocatorDump; 20 class MemoryAllocatorDumpGuid; 21 class ProcessMemoryDump; 22 } 23 24 // SharedMemoryTracker tracks shared memory usage. 25 class BASE_EXPORT SharedMemoryTracker : public trace_event::MemoryDumpProvider { 26 public: 27 // Returns a singleton instance. 28 static SharedMemoryTracker* GetInstance(); 29 30 static std::string GetDumpNameForTracing(const UnguessableToken& id); 31 32 static trace_event::MemoryAllocatorDumpGuid GetGlobalDumpIdForTracing( 33 const UnguessableToken& id); 34 35 // Gets or creates if non-existant, a memory dump for the |shared_memory| 36 // inside the given |pmd|. Also adds the necessary edges for the dump when 37 // creating the dump. 38 static const trace_event::MemoryAllocatorDump* GetOrCreateSharedMemoryDump( 39 const SharedMemory* shared_memory, 40 trace_event::ProcessMemoryDump* pmd); 41 // We're in the middle of a refactor https://crbug.com/795291. Eventually, the 42 // first call will go away. 43 static const trace_event::MemoryAllocatorDump* GetOrCreateSharedMemoryDump( 44 const SharedMemoryMapping& shared_memory, 45 trace_event::ProcessMemoryDump* pmd); 46 47 // Records shared memory usage on valid mapping. 48 void IncrementMemoryUsage(const SharedMemory& shared_memory); 49 void IncrementMemoryUsage(const SharedMemoryMapping& mapping); 50 51 // Records shared memory usage on unmapping. 52 void DecrementMemoryUsage(const SharedMemory& shared_memory); 53 void DecrementMemoryUsage(const SharedMemoryMapping& mapping); 54 55 // Root dump name for all shared memory dumps. 56 static const char kDumpRootName[]; 57 58 private: 59 SharedMemoryTracker(); 60 ~SharedMemoryTracker() override; 61 62 // trace_event::MemoryDumpProvider implementation. 63 bool OnMemoryDump(const trace_event::MemoryDumpArgs& args, 64 trace_event::ProcessMemoryDump* pmd) override; 65 66 static const trace_event::MemoryAllocatorDump* 67 GetOrCreateSharedMemoryDumpInternal(void* mapped_memory, 68 size_t mapped_size, 69 const UnguessableToken& mapped_id, 70 trace_event::ProcessMemoryDump* pmd); 71 72 // Information associated with each mapped address. 73 struct UsageInfo { UsageInfoUsageInfo74 UsageInfo(size_t size, const UnguessableToken& id) 75 : mapped_size(size), mapped_id(id) {} 76 77 size_t mapped_size; 78 UnguessableToken mapped_id; 79 }; 80 81 // Used to lock when |usages_| is modified or read. 82 Lock usages_lock_; 83 std::map<void*, UsageInfo> usages_; 84 85 DISALLOW_COPY_AND_ASSIGN(SharedMemoryTracker); 86 }; 87 88 } // namespace base 89 90 #endif // BASE_MEMORY_SHARED_MEMORY_TRACKER_H_ 91