process_memory_dump.h 12 KB

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