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 "base/memory/shared_memory.h" 9 #include "base/synchronization/lock.h" 10 #include "base/trace_event/memory_dump_provider.h" 11 12 namespace base { 13 14 namespace trace_event { 15 class ProcessMemoryDump; 16 } 17 18 // SharedMemoryTracker tracks shared memory usage. 19 class BASE_EXPORT SharedMemoryTracker 20 : public base::trace_event::MemoryDumpProvider { 21 public: 22 // Returns a singleton instance. 23 static SharedMemoryTracker* GetInstance(); 24 25 // Records shared memory usage on mapping. 26 void IncrementMemoryUsage(const SharedMemory& shared_memory); 27 28 // Records shared memory usage on unmapping. 29 void DecrementMemoryUsage(const SharedMemory& shared_memory); 30 31 private: 32 struct Usage { 33 Usage(); 34 Usage(const Usage& rhs); 35 ~Usage(); 36 SharedMemory::UniqueId unique_id; 37 size_t size; 38 }; 39 40 SharedMemoryTracker(); 41 ~SharedMemoryTracker() override; 42 43 // base::trace_event::MemoryDumpProvider implementation. 44 bool OnMemoryDump(const base::trace_event::MemoryDumpArgs& args, 45 base::trace_event::ProcessMemoryDump* pmd) override; 46 47 // Used to lock when |usages_| is modified or read. 48 Lock usages_lock_; 49 std::unordered_map<const SharedMemory*, Usage> usages_; 50 51 DISALLOW_COPY_AND_ASSIGN(SharedMemoryTracker); 52 }; 53 54 } // namespace base 55 56 #endif // BASE_MEMORY_SHARED_MEMORY_TRACKER_H_ 57