Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2017 The Chromium Authors |
hajimehoshi | 2acea43 | 2017-03-08 08:55:37 | [diff] [blame] | 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 | |
hajimehoshi | e370c17 | 2017-05-25 06:12:16 | [diff] [blame] | 8 | #include <map> |
Siddhartha | e241d0a | 2017-08-18 23:11:02 | [diff] [blame] | 9 | #include <string> |
brettw | 1ce49f6 | 2017-04-27 19:42:32 | [diff] [blame] | 10 | |
David Sanders | 6e70994 | 2022-04-05 06:49:26 | [diff] [blame] | 11 | #include "base/base_export.h" |
Alexandr Ilin | 7b88250 | 2018-03-27 07:44:53 | [diff] [blame] | 12 | #include "base/memory/shared_memory_mapping.h" |
hajimehoshi | 2acea43 | 2017-03-08 08:55:37 | [diff] [blame] | 13 | #include "base/synchronization/lock.h" |
Etienne Pierre-doray | fc7952f0 | 2025-06-06 00:04:33 | [diff] [blame] | 14 | #include "base/trace_event/memory_dump_provider.h" |
hajimehoshi | 2acea43 | 2017-03-08 08:55:37 | [diff] [blame] | 15 | |
| 16 | namespace base { |
| 17 | |
| 18 | namespace trace_event { |
Siddhartha | e241d0a | 2017-08-18 23:11:02 | [diff] [blame] | 19 | class MemoryAllocatorDump; |
Hajime Hoshi | 99042d0 | 2017-06-06 02:58:11 | [diff] [blame] | 20 | class MemoryAllocatorDumpGuid; |
hajimehoshi | 2acea43 | 2017-03-08 08:55:37 | [diff] [blame] | 21 | class ProcessMemoryDump; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 22 | } // namespace trace_event |
hajimehoshi | 2acea43 | 2017-03-08 08:55:37 | [diff] [blame] | 23 | |
| 24 | // SharedMemoryTracker tracks shared memory usage. |
hajimehoshi | e370c17 | 2017-05-25 06:12:16 | [diff] [blame] | 25 | class BASE_EXPORT SharedMemoryTracker : public trace_event::MemoryDumpProvider { |
hajimehoshi | 2acea43 | 2017-03-08 08:55:37 | [diff] [blame] | 26 | public: |
| 27 | // Returns a singleton instance. |
| 28 | static SharedMemoryTracker* GetInstance(); |
| 29 | |
Peter Boström | 75cd3c0 | 2021-09-28 15:23:18 | [diff] [blame] | 30 | SharedMemoryTracker(const SharedMemoryTracker&) = delete; |
| 31 | SharedMemoryTracker& operator=(const SharedMemoryTracker&) = delete; |
| 32 | |
Siddhartha | e241d0a | 2017-08-18 23:11:02 | [diff] [blame] | 33 | static std::string GetDumpNameForTracing(const UnguessableToken& id); |
Hajime Hoshi | 99042d0 | 2017-06-06 02:58:11 | [diff] [blame] | 34 | |
Hajime Hoshi | de0c27b | 2017-06-27 05:57:04 | [diff] [blame] | 35 | static trace_event::MemoryAllocatorDumpGuid GetGlobalDumpIdForTracing( |
Hajime Hoshi | 99042d0 | 2017-06-06 02:58:11 | [diff] [blame] | 36 | const UnguessableToken& id); |
| 37 | |
Siddhartha | e241d0a | 2017-08-18 23:11:02 | [diff] [blame] | 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( |
Alexandr Ilin | ae5e45c0 | 2018-04-25 06:52:41 | [diff] [blame] | 42 | const SharedMemoryMapping& shared_memory, |
| 43 | trace_event::ProcessMemoryDump* pmd); |
Siddhartha | e241d0a | 2017-08-18 23:11:02 | [diff] [blame] | 44 | |
Alexandr Ilin | 7b88250 | 2018-03-27 07:44:53 | [diff] [blame] | 45 | // Records shared memory usage on valid mapping. |
Alexandr Ilin | 7b88250 | 2018-03-27 07:44:53 | [diff] [blame] | 46 | void IncrementMemoryUsage(const SharedMemoryMapping& mapping); |
hajimehoshi | 2acea43 | 2017-03-08 08:55:37 | [diff] [blame] | 47 | |
| 48 | // Records shared memory usage on unmapping. |
Alexandr Ilin | 7b88250 | 2018-03-27 07:44:53 | [diff] [blame] | 49 | void DecrementMemoryUsage(const SharedMemoryMapping& mapping); |
hajimehoshi | 2acea43 | 2017-03-08 08:55:37 | [diff] [blame] | 50 | |
Lalit Maganti | 6b20dfea | 2017-11-07 15:49:21 | [diff] [blame] | 51 | // Root dump name for all shared memory dumps. |
| 52 | static const char kDumpRootName[]; |
| 53 | |
hajimehoshi | 2acea43 | 2017-03-08 08:55:37 | [diff] [blame] | 54 | private: |
hajimehoshi | 2acea43 | 2017-03-08 08:55:37 | [diff] [blame] | 55 | SharedMemoryTracker(); |
| 56 | ~SharedMemoryTracker() override; |
| 57 | |
hajimehoshi | e370c17 | 2017-05-25 06:12:16 | [diff] [blame] | 58 | // trace_event::MemoryDumpProvider implementation. |
| 59 | bool OnMemoryDump(const trace_event::MemoryDumpArgs& args, |
| 60 | trace_event::ProcessMemoryDump* pmd) override; |
hajimehoshi | 2acea43 | 2017-03-08 08:55:37 | [diff] [blame] | 61 | |
Alexandr Ilin | 7b88250 | 2018-03-27 07:44:53 | [diff] [blame] | 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 { |
| 70 | 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 | |
hajimehoshi | 2acea43 | 2017-03-08 08:55:37 | [diff] [blame] | 77 | Lock usages_lock_; |
Benoit Lize | 2585915 | 2020-07-09 11:52:09 | [diff] [blame] | 78 | std::map<void*, UsageInfo> usages_ GUARDED_BY(usages_lock_); |
hajimehoshi | 2acea43 | 2017-03-08 08:55:37 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | } // namespace base |
| 82 | |
| 83 | #endif // BASE_MEMORY_SHARED_MEMORY_TRACKER_H_ |