OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_TRACE_EVENT_PROCESS_MEMORY_DUMP_H_ | 5 #ifndef BASE_TRACE_EVENT_PROCESS_MEMORY_DUMP_H_ |
6 #define BASE_TRACE_EVENT_PROCESS_MEMORY_DUMP_H_ | 6 #define BASE_TRACE_EVENT_PROCESS_MEMORY_DUMP_H_ |
7 | 7 |
| 8 #include <vector> |
| 9 |
8 #include "base/base_export.h" | 10 #include "base/base_export.h" |
9 #include "base/containers/hash_tables.h" | 11 #include "base/containers/hash_tables.h" |
10 #include "base/containers/small_map.h" | 12 #include "base/containers/small_map.h" |
11 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
12 #include "base/memory/scoped_vector.h" | 14 #include "base/memory/scoped_vector.h" |
13 #include "base/trace_event/memory_allocator_dump.h" | 15 #include "base/trace_event/memory_allocator_dump.h" |
| 16 #include "base/trace_event/memory_allocator_dump_guid.h" |
14 #include "base/trace_event/memory_dump_session_state.h" | 17 #include "base/trace_event/memory_dump_session_state.h" |
15 #include "base/trace_event/process_memory_maps.h" | 18 #include "base/trace_event/process_memory_maps.h" |
16 #include "base/trace_event/process_memory_totals.h" | 19 #include "base/trace_event/process_memory_totals.h" |
17 | 20 |
18 namespace base { | 21 namespace base { |
19 namespace trace_event { | 22 namespace trace_event { |
20 | 23 |
21 class ConvertableToTraceFormat; | 24 class ConvertableToTraceFormat; |
22 class MemoryDumpManager; | 25 class MemoryDumpManager; |
23 class MemoryDumpSessionState; | 26 class MemoryDumpSessionState; |
24 | 27 |
25 // ProcessMemoryDump is as a strongly typed container which enforces the data | 28 // ProcessMemoryDump is as a strongly typed container which enforces the data |
26 // model for each memory dump and holds the dumps produced by the | 29 // model for each memory dump and holds the dumps produced by the |
27 // MemoryDumpProvider(s) for a specific process. | 30 // MemoryDumpProvider(s) for a specific process. |
28 // At trace generation time (i.e. when AsValue() is called), ProcessMemoryDump | 31 // At trace generation time (i.e. when AsValue() is called), ProcessMemoryDump |
29 // will compose a key-value dictionary of the various dumps obtained at trace | 32 // will compose a key-value dictionary of the various dumps obtained at trace |
30 // dump point time. | 33 // dump point time. |
31 class BASE_EXPORT ProcessMemoryDump { | 34 class BASE_EXPORT ProcessMemoryDump { |
32 public: | 35 public: |
| 36 struct MemoryAllocatorDumpEdge { |
| 37 MemoryAllocatorDumpGuid source; |
| 38 MemoryAllocatorDumpGuid target; |
| 39 int importance; |
| 40 const char* type; |
| 41 }; |
| 42 |
33 // Maps allocator dumps absolute names (allocator_name/heap/subheap) to | 43 // Maps allocator dumps absolute names (allocator_name/heap/subheap) to |
34 // MemoryAllocatorDump instances. | 44 // MemoryAllocatorDump instances. |
35 using AllocatorDumpsMap = | 45 using AllocatorDumpsMap = |
36 SmallMap<hash_map<std::string, MemoryAllocatorDump*>>; | 46 SmallMap<hash_map<std::string, MemoryAllocatorDump*>>; |
37 | 47 |
38 ProcessMemoryDump(const scoped_refptr<MemoryDumpSessionState>& session_state); | 48 ProcessMemoryDump(const scoped_refptr<MemoryDumpSessionState>& session_state); |
39 ~ProcessMemoryDump(); | 49 ~ProcessMemoryDump(); |
40 | 50 |
41 // Called at trace generation time to populate the TracedValue. | 51 // Called at trace generation time to populate the TracedValue. |
42 void AsValueInto(TracedValue* value) const; | 52 void AsValueInto(TracedValue* value) const; |
43 | 53 |
| 54 // Removes all the MemoryAllocatorDump(s) contained in this instance. This |
| 55 // ProcessMemoryDump can be safely reused as if it was new once this returns. |
| 56 void Clear(); |
| 57 |
| 58 // Merges all MemoryAllocatorDump(s) contained in |other| inside this |
| 59 // ProcessMemoryDump, transferring their ownership to this instance. |
| 60 // |other| will be an empty ProcessMemoryDump after this method returns. |
| 61 // This is to allow dump providers to pre-populate ProcessMemoryDump instances |
| 62 // and later move their contents into the ProcessMemoryDump passed as argument |
| 63 // of the MemoryDumpProvider::OnMemoryDump(ProcessMemoryDump*) callback. |
| 64 void TakeAllDumpsFrom(ProcessMemoryDump* other); |
| 65 |
44 ProcessMemoryTotals* process_totals() { return &process_totals_; } | 66 ProcessMemoryTotals* process_totals() { return &process_totals_; } |
45 bool has_process_totals() const { return has_process_totals_; } | 67 bool has_process_totals() const { return has_process_totals_; } |
46 void set_has_process_totals() { has_process_totals_ = true; } | 68 void set_has_process_totals() { has_process_totals_ = true; } |
47 | 69 |
48 ProcessMemoryMaps* process_mmaps() { return &process_mmaps_; } | 70 ProcessMemoryMaps* process_mmaps() { return &process_mmaps_; } |
49 bool has_process_mmaps() const { return has_process_mmaps_; } | 71 bool has_process_mmaps() const { return has_process_mmaps_; } |
50 void set_has_process_mmaps() { has_process_mmaps_ = true; } | 72 void set_has_process_mmaps() { has_process_mmaps_ = true; } |
51 | 73 |
52 // Creates a new MemoryAllocatorDump with the given name and returns the | 74 // Creates a new MemoryAllocatorDump with the given name and returns the |
53 // empty object back to the caller. | 75 // empty object back to the caller. |
54 // Arguments: | 76 // Arguments: |
55 // absolute_name: a name that uniquely identifies allocator dumps produced | 77 // absolute_name: a name that uniquely identifies allocator dumps produced |
56 // by this provider. It is possible to specify nesting by using a | 78 // by this provider. It is possible to specify nesting by using a |
57 // path-like string (e.g., v8/isolate1/heap1, v8/isolate1/heap2). | 79 // path-like string (e.g., v8/isolate1/heap1, v8/isolate1/heap2). |
58 // Leading or trailing slashes are not allowed. | 80 // Leading or trailing slashes are not allowed. |
| 81 // guid: an optional identifier, unique among all processes within the |
| 82 // scope of a global dump. This is only relevant when using |
| 83 // AddOwnershipEdge(). If omitted, it will be automatically generated. |
59 // ProcessMemoryDump handles the memory ownership of its MemoryAllocatorDumps. | 84 // ProcessMemoryDump handles the memory ownership of its MemoryAllocatorDumps. |
60 MemoryAllocatorDump* CreateAllocatorDump(const std::string& absolute_name); | 85 MemoryAllocatorDump* CreateAllocatorDump(const std::string& absolute_name); |
| 86 MemoryAllocatorDump* CreateAllocatorDump(const std::string& absolute_name, |
| 87 const MemoryAllocatorDumpGuid& guid); |
61 | 88 |
62 // Looks up a MemoryAllocatorDump given its allocator and heap names, or | 89 // Looks up a MemoryAllocatorDump given its allocator and heap names, or |
63 // nullptr if not found. | 90 // nullptr if not found. |
64 MemoryAllocatorDump* GetAllocatorDump(const std::string& absolute_name) const; | 91 MemoryAllocatorDump* GetAllocatorDump(const std::string& absolute_name) const; |
65 | 92 |
| 93 // Creates a shared MemoryAllocatorDump, to express cross-process sharing. |
| 94 // Shared allocator dumps are allowed to have duplicate guids within the |
| 95 // global scope, in order to reference the same dump from multiple processes. |
| 96 // See the design doc goo.gl/keU6Bf for reference usage patterns. |
| 97 MemoryAllocatorDump* CreateSharedGlobalAllocatorDump( |
| 98 const MemoryAllocatorDumpGuid& guid); |
| 99 |
| 100 // Looks up a shared MemoryAllocatorDump given its guid. |
| 101 MemoryAllocatorDump* GetSharedGlobalAllocatorDump( |
| 102 const MemoryAllocatorDumpGuid& guid) const; |
| 103 |
66 // Returns the map of the MemoryAllocatorDumps added to this dump. | 104 // Returns the map of the MemoryAllocatorDumps added to this dump. |
67 const AllocatorDumpsMap& allocator_dumps() const { return allocator_dumps_; } | 105 const AllocatorDumpsMap& allocator_dumps() const { return allocator_dumps_; } |
68 | 106 |
| 107 // Adds an ownership relationship between two MemoryAllocatorDump(s) with the |
| 108 // semantics: |source| owns |target|, and has the effect of attributing |
| 109 // the memory usage of |target| to |source|. |importance| is optional and |
| 110 // relevant only for the cases of co-ownership, where it acts as a z-index: |
| 111 // the owner with the highest importance will be attributed |target|'s memory. |
| 112 void AddOwnershipEdge(const MemoryAllocatorDumpGuid& source, |
| 113 const MemoryAllocatorDumpGuid& target, |
| 114 int importance); |
| 115 void AddOwnershipEdge(const MemoryAllocatorDumpGuid& source, |
| 116 const MemoryAllocatorDumpGuid& target); |
| 117 |
| 118 const std::vector<MemoryAllocatorDumpEdge>& allocator_dumps_edges() const { |
| 119 return allocator_dumps_edges_; |
| 120 } |
| 121 |
| 122 // Utility method to add a suballocation relationship with the following |
| 123 // semantics: |source| is suballocated from |target_node_name|. |
| 124 // This creates a child node of |target_node_name| and adds an ownership edge |
| 125 // between |source| and the new child node. As a result, the UI will not |
| 126 // account the memory of |source| in the target node. |
| 127 void AddSuballocation(const MemoryAllocatorDumpGuid& source, |
| 128 const std::string& target_node_name); |
| 129 |
69 const scoped_refptr<MemoryDumpSessionState>& session_state() const { | 130 const scoped_refptr<MemoryDumpSessionState>& session_state() const { |
70 return session_state_; | 131 return session_state_; |
71 } | 132 } |
72 | 133 |
73 private: | 134 private: |
| 135 void AddAllocatorDumpInternal(MemoryAllocatorDump* mad); |
| 136 |
74 ProcessMemoryTotals process_totals_; | 137 ProcessMemoryTotals process_totals_; |
75 bool has_process_totals_; | 138 bool has_process_totals_; |
76 | 139 |
77 ProcessMemoryMaps process_mmaps_; | 140 ProcessMemoryMaps process_mmaps_; |
78 bool has_process_mmaps_; | 141 bool has_process_mmaps_; |
79 | 142 |
80 AllocatorDumpsMap allocator_dumps_; | 143 AllocatorDumpsMap allocator_dumps_; |
81 | 144 |
82 // ProcessMemoryDump handles the memory ownership of all its belongings. | 145 // ProcessMemoryDump handles the memory ownership of all its belongings. |
83 ScopedVector<MemoryAllocatorDump> allocator_dumps_storage_; | 146 ScopedVector<MemoryAllocatorDump> allocator_dumps_storage_; |
84 | 147 |
85 // State shared among all PMDs instances created in a given trace session. | 148 // State shared among all PMDs instances created in a given trace session. |
86 scoped_refptr<MemoryDumpSessionState> session_state_; | 149 scoped_refptr<MemoryDumpSessionState> session_state_; |
87 | 150 |
| 151 // Keeps track of relationships between MemoryAllocatorDump(s). |
| 152 std::vector<MemoryAllocatorDumpEdge> allocator_dumps_edges_; |
| 153 |
88 DISALLOW_COPY_AND_ASSIGN(ProcessMemoryDump); | 154 DISALLOW_COPY_AND_ASSIGN(ProcessMemoryDump); |
89 }; | 155 }; |
90 | 156 |
91 } // namespace trace_event | 157 } // namespace trace_event |
92 } // namespace base | 158 } // namespace base |
93 | 159 |
94 #endif // BASE_TRACE_EVENT_PROCESS_MEMORY_DUMP_H_ | 160 #endif // BASE_TRACE_EVENT_PROCESS_MEMORY_DUMP_H_ |
OLD | NEW |