process_memory_dump.h 13 KB

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