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