1 // Copyright 2015 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_TRACE_EVENT_PROCESS_MEMORY_DUMP_H_ 6 #define BASE_TRACE_EVENT_PROCESS_MEMORY_DUMP_H_ 7 8 #include <stddef.h> 9 10 #include <map> 11 #include <unordered_map> 12 #include <vector> 13 14 #include "base/base_export.h" 15 #include "base/macros.h" 16 #include "base/memory/ref_counted.h" 17 #include "base/trace_event/heap_profiler_allocation_context.h" 18 #include "base/trace_event/memory_allocator_dump.h" 19 #include "base/trace_event/memory_allocator_dump_guid.h" 20 #include "base/trace_event/memory_dump_request_args.h" 21 #include "build/build_config.h" 22 23 // Define COUNT_RESIDENT_BYTES_SUPPORTED if platform supports counting of the 24 // resident memory. 25 #if !defined(OS_NACL) 26 #define COUNT_RESIDENT_BYTES_SUPPORTED 27 #endif 28 29 namespace base { 30 31 class SharedMemory; 32 class UnguessableToken; 33 34 namespace trace_event { 35 36 class TracedValue; 37 38 // ProcessMemoryDump is as a strongly typed container which holds the dumps 39 // produced by the MemoryDumpProvider(s) for a specific process. 40 class BASE_EXPORT ProcessMemoryDump { 41 public: 42 struct BASE_EXPORT MemoryAllocatorDumpEdge { 43 bool operator==(const MemoryAllocatorDumpEdge&) const; 44 bool operator!=(const MemoryAllocatorDumpEdge&) const; 45 46 MemoryAllocatorDumpGuid source; 47 MemoryAllocatorDumpGuid target; 48 int importance = 0; 49 bool overridable = false; 50 }; 51 52 // Maps allocator dumps absolute names (allocator_name/heap/subheap) to 53 // MemoryAllocatorDump instances. 54 using AllocatorDumpsMap = 55 std::map<std::string, std::unique_ptr<MemoryAllocatorDump>>; 56 57 // Stores allocator dump edges indexed by source allocator dump GUID. 58 using AllocatorDumpEdgesMap = 59 std::map<MemoryAllocatorDumpGuid, MemoryAllocatorDumpEdge>; 60 61 #if defined(COUNT_RESIDENT_BYTES_SUPPORTED) 62 // Returns the number of bytes in a kernel memory page. Some platforms may 63 // have a different value for kernel page sizes from user page sizes. It is 64 // important to use kernel memory page sizes for resident bytes calculation. 65 // In most cases, the two are the same. 66 static size_t GetSystemPageSize(); 67 68 // Returns the total bytes resident for a virtual address range, with given 69 // |start_address| and |mapped_size|. |mapped_size| is specified in bytes. The 70 // value returned is valid only if the given range is currently mmapped by the 71 // process. The |start_address| must be page-aligned. 72 static size_t CountResidentBytes(void* start_address, size_t mapped_size); 73 74 // The same as above, but the given mapped range should belong to the 75 // shared_memory's mapped region. 76 static base::Optional<size_t> CountResidentBytesInSharedMemory( 77 void* start_address, 78 size_t mapped_size); 79 #endif 80 81 explicit ProcessMemoryDump(const MemoryDumpArgs& dump_args); 82 ProcessMemoryDump(ProcessMemoryDump&&); 83 ~ProcessMemoryDump(); 84 85 ProcessMemoryDump& operator=(ProcessMemoryDump&&); 86 87 // Creates a new MemoryAllocatorDump with the given name and returns the 88 // empty object back to the caller. 89 // Arguments: 90 // absolute_name: a name that uniquely identifies allocator dumps produced 91 // by this provider. It is possible to specify nesting by using a 92 // path-like string (e.g., v8/isolate1/heap1, v8/isolate1/heap2). 93 // Leading or trailing slashes are not allowed. 94 // guid: an optional identifier, unique among all processes within the 95 // scope of a global dump. This is only relevant when using 96 // AddOwnershipEdge() to express memory sharing. If omitted, 97 // it will be automatically generated. 98 // ProcessMemoryDump handles the memory ownership of its MemoryAllocatorDumps. 99 MemoryAllocatorDump* CreateAllocatorDump(const std::string& absolute_name); 100 MemoryAllocatorDump* CreateAllocatorDump(const std::string& absolute_name, 101 const MemoryAllocatorDumpGuid& guid); 102 103 // Looks up a MemoryAllocatorDump given its allocator and heap names, or 104 // nullptr if not found. 105 MemoryAllocatorDump* GetAllocatorDump(const std::string& absolute_name) const; 106 107 // Do NOT use this method. All dump providers should use 108 // CreateAllocatorDump(). Tries to create a new MemoryAllocatorDump only if it 109 // doesn't already exist. Creating multiple dumps with same name using 110 // GetOrCreateAllocatorDump() would override the existing scalars in MAD and 111 // cause misreporting. This method is used only in rare cases multiple 112 // components create allocator dumps with same name and only one of them adds 113 // size. 114 MemoryAllocatorDump* GetOrCreateAllocatorDump( 115 const std::string& absolute_name); 116 117 // Creates a shared MemoryAllocatorDump, to express cross-process sharing. 118 // Shared allocator dumps are allowed to have duplicate guids within the 119 // global scope, in order to reference the same dump from multiple processes. 120 // See the design doc goo.gl/keU6Bf for reference usage patterns. 121 MemoryAllocatorDump* CreateSharedGlobalAllocatorDump( 122 const MemoryAllocatorDumpGuid& guid); 123 124 // Creates a shared MemoryAllocatorDump as CreateSharedGlobalAllocatorDump, 125 // but with a WEAK flag. A weak dump will be discarded unless a non-weak dump 126 // is created using CreateSharedGlobalAllocatorDump by at least one process. 127 // The WEAK flag does not apply if a non-weak dump with the same GUID already 128 // exists or is created later. All owners and children of the discarded dump 129 // will also be discarded transitively. 130 MemoryAllocatorDump* CreateWeakSharedGlobalAllocatorDump( 131 const MemoryAllocatorDumpGuid& guid); 132 133 // Looks up a shared MemoryAllocatorDump given its guid. 134 MemoryAllocatorDump* GetSharedGlobalAllocatorDump( 135 const MemoryAllocatorDumpGuid& guid) const; 136 137 // Returns the map of the MemoryAllocatorDumps added to this dump. allocator_dumps()138 const AllocatorDumpsMap& allocator_dumps() const { return allocator_dumps_; } 139 mutable_allocator_dumps_for_serialization()140 AllocatorDumpsMap* mutable_allocator_dumps_for_serialization() const { 141 // Mojo takes a const input argument even for move-only types that can be 142 // mutate while serializing (like this one). Hence the const_cast. 143 return const_cast<AllocatorDumpsMap*>(&allocator_dumps_); 144 } 145 void SetAllocatorDumpsForSerialization( 146 std::vector<std::unique_ptr<MemoryAllocatorDump>>); 147 148 // Only for mojo serialization. 149 std::vector<MemoryAllocatorDumpEdge> GetAllEdgesForSerialization() const; 150 void SetAllEdgesForSerialization(const std::vector<MemoryAllocatorDumpEdge>&); 151 152 // Dumps heap usage with |allocator_name|. 153 void DumpHeapUsage( 154 const std::unordered_map<base::trace_event::AllocationContext, 155 base::trace_event::AllocationMetrics>& 156 metrics_by_context, 157 base::trace_event::TraceEventMemoryOverhead& overhead, 158 const char* allocator_name); 159 160 // Adds an ownership relationship between two MemoryAllocatorDump(s) with the 161 // semantics: |source| owns |target|, and has the effect of attributing 162 // the memory usage of |target| to |source|. |importance| is optional and 163 // relevant only for the cases of co-ownership, where it acts as a z-index: 164 // the owner with the highest importance will be attributed |target|'s memory. 165 // If an edge is present, its importance will not be updated unless 166 // |importance| is larger. 167 void AddOwnershipEdge(const MemoryAllocatorDumpGuid& source, 168 const MemoryAllocatorDumpGuid& target, 169 int importance); 170 void AddOwnershipEdge(const MemoryAllocatorDumpGuid& source, 171 const MemoryAllocatorDumpGuid& target); 172 173 // Adds edges that can be overriden by a later or earlier call to 174 // AddOwnershipEdge() with the same source and target with a different 175 // |importance| value. 176 void AddOverridableOwnershipEdge(const MemoryAllocatorDumpGuid& source, 177 const MemoryAllocatorDumpGuid& target, 178 int importance); 179 180 // Creates ownership edges for memory backed by base::SharedMemory. Handles 181 // the case of cross process sharing and importnace of ownership for the case 182 // with and without the base::SharedMemory dump provider. The new version 183 // should just use global dumps created by SharedMemoryTracker and this 184 // function handles the transition until we get SharedMemory IDs through mojo 185 // channel crbug.com/713763. The weak version creates a weak global dump. 186 // |client_local_dump_guid| The guid of the local dump created by the client 187 // of base::SharedMemory. 188 // |shared_memory_guid| The ID of the base::SharedMemory that is assigned 189 // globally, used to create global dump edges in the new model. 190 // |importance| Importance of the global dump edges to say if the current 191 // process owns the memory segment. 192 void CreateSharedMemoryOwnershipEdge( 193 const MemoryAllocatorDumpGuid& client_local_dump_guid, 194 const UnguessableToken& shared_memory_guid, 195 int importance); 196 void CreateWeakSharedMemoryOwnershipEdge( 197 const MemoryAllocatorDumpGuid& client_local_dump_guid, 198 const UnguessableToken& shared_memory_guid, 199 int importance); 200 allocator_dumps_edges()201 const AllocatorDumpEdgesMap& allocator_dumps_edges() const { 202 return allocator_dumps_edges_; 203 } 204 205 // Utility method to add a suballocation relationship with the following 206 // semantics: |source| is suballocated from |target_node_name|. 207 // This creates a child node of |target_node_name| and adds an ownership edge 208 // between |source| and the new child node. As a result, the UI will not 209 // account the memory of |source| in the target node. 210 void AddSuballocation(const MemoryAllocatorDumpGuid& source, 211 const std::string& target_node_name); 212 213 // Removes all the MemoryAllocatorDump(s) contained in this instance. This 214 // ProcessMemoryDump can be safely reused as if it was new once this returns. 215 void Clear(); 216 217 // Merges all MemoryAllocatorDump(s) contained in |other| inside this 218 // ProcessMemoryDump, transferring their ownership to this instance. 219 // |other| will be an empty ProcessMemoryDump after this method returns. 220 // This is to allow dump providers to pre-populate ProcessMemoryDump instances 221 // and later move their contents into the ProcessMemoryDump passed as argument 222 // of the MemoryDumpProvider::OnMemoryDump(ProcessMemoryDump*) callback. 223 void TakeAllDumpsFrom(ProcessMemoryDump* other); 224 225 // Populate the traced value with information about the memory allocator 226 // dumps. 227 void SerializeAllocatorDumpsInto(TracedValue* value) const; 228 dump_args()229 const MemoryDumpArgs& dump_args() const { return dump_args_; } 230 231 private: 232 FRIEND_TEST_ALL_PREFIXES(ProcessMemoryDumpTest, BackgroundModeTest); 233 FRIEND_TEST_ALL_PREFIXES(ProcessMemoryDumpTest, SharedMemoryOwnershipTest); 234 FRIEND_TEST_ALL_PREFIXES(ProcessMemoryDumpTest, GuidsTest); 235 236 MemoryAllocatorDump* AddAllocatorDumpInternal( 237 std::unique_ptr<MemoryAllocatorDump> mad); 238 239 // A per-process token, valid throughout all the lifetime of the current 240 // process, used to disambiguate dumps with the same name generated in 241 // different processes. process_token()242 const UnguessableToken& process_token() const { return process_token_; } set_process_token_for_testing(UnguessableToken token)243 void set_process_token_for_testing(UnguessableToken token) { 244 process_token_ = token; 245 }; 246 247 // Returns the Guid of the dump for the given |absolute_name| for 248 // for the given process' token. |process_token| is used to disambiguate GUIDs 249 // derived from the same name under different processes. 250 MemoryAllocatorDumpGuid GetDumpId(const std::string& absolute_name); 251 252 void CreateSharedMemoryOwnershipEdgeInternal( 253 const MemoryAllocatorDumpGuid& client_local_dump_guid, 254 const UnguessableToken& shared_memory_guid, 255 int importance, 256 bool is_weak); 257 258 MemoryAllocatorDump* GetBlackHoleMad(); 259 260 UnguessableToken process_token_; 261 AllocatorDumpsMap allocator_dumps_; 262 263 // Keeps track of relationships between MemoryAllocatorDump(s). 264 AllocatorDumpEdgesMap allocator_dumps_edges_; 265 266 // Level of detail of the current dump. 267 MemoryDumpArgs dump_args_; 268 269 // This allocator dump is returned when an invalid dump is created in 270 // background mode. The attributes of the dump are ignored and not added to 271 // the trace. 272 std::unique_ptr<MemoryAllocatorDump> black_hole_mad_; 273 274 // When set to true, the DCHECK(s) for invalid dump creations on the 275 // background mode are disabled for testing. 276 static bool is_black_hole_non_fatal_for_testing_; 277 278 DISALLOW_COPY_AND_ASSIGN(ProcessMemoryDump); 279 }; 280 281 } // namespace trace_event 282 } // namespace base 283 284 #endif // BASE_TRACE_EVENT_PROCESS_MEMORY_DUMP_H_ 285