xref: /aosp_15_r20/external/cronet/base/memory/shared_memory_tracker.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2017 The Chromium Authors
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/base_export.h"
12 #include "base/memory/shared_memory_mapping.h"
13 #include "base/synchronization/lock.h"
14 #include "base/trace_event/base_tracing.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   SharedMemoryTracker(const SharedMemoryTracker&) = delete;
31   SharedMemoryTracker& operator=(const SharedMemoryTracker&) = delete;
32 
33   static std::string GetDumpNameForTracing(const UnguessableToken& id);
34 
35   static trace_event::MemoryAllocatorDumpGuid GetGlobalDumpIdForTracing(
36       const UnguessableToken& id);
37 
38   // Gets or creates if non-existant, a memory dump for the |shared_memory|
39   // inside the given |pmd|. Also adds the necessary edges for the dump when
40   // creating the dump.
41   static const trace_event::MemoryAllocatorDump* GetOrCreateSharedMemoryDump(
42       const SharedMemoryMapping& shared_memory,
43       trace_event::ProcessMemoryDump* pmd);
44 
45   // Records shared memory usage on valid mapping.
46   void IncrementMemoryUsage(const SharedMemoryMapping& mapping);
47 
48   // Records shared memory usage on unmapping.
49   void DecrementMemoryUsage(const SharedMemoryMapping& mapping);
50 
51   // Root dump name for all shared memory dumps.
52   static const char kDumpRootName[];
53 
54  private:
55   SharedMemoryTracker();
56   ~SharedMemoryTracker() override;
57 
58   // trace_event::MemoryDumpProvider implementation.
59   bool OnMemoryDump(const trace_event::MemoryDumpArgs& args,
60                     trace_event::ProcessMemoryDump* pmd) override;
61 
62   static const trace_event::MemoryAllocatorDump*
63   GetOrCreateSharedMemoryDumpInternal(void* mapped_memory,
64                                       size_t mapped_size,
65                                       const UnguessableToken& mapped_id,
66                                       trace_event::ProcessMemoryDump* pmd);
67 
68   // Information associated with each mapped address.
69   struct UsageInfo {
UsageInfoUsageInfo70     UsageInfo(size_t size, const UnguessableToken& id)
71         : mapped_size(size), mapped_id(id) {}
72 
73     size_t mapped_size;
74     UnguessableToken mapped_id;
75   };
76 
77   Lock usages_lock_;
78   std::map<void*, UsageInfo> usages_ GUARDED_BY(usages_lock_);
79 };
80 
81 }  // namespace base
82 
83 #endif  // BASE_MEMORY_SHARED_MEMORY_TRACKER_H_
84