[tracing] API to express memory suballocation and cross-process sharing
[chromium-blink-merge.git] / base / trace_event / process_memory_dump.h
blob88ce28abe245a7ec2f8f58ebaffe3f39999611bb
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 enforces the data
29 // model for each memory dump and holds the dumps produced by the
30 // MemoryDumpProvider(s) for a specific process.
31 // At trace generation time (i.e. when AsValue() is called), ProcessMemoryDump
32 // will compose a key-value dictionary of the various dumps obtained at trace
33 // dump point time.
34 class BASE_EXPORT ProcessMemoryDump {
35 public:
36 struct MemoryAllocatorDumpEdge {
37 MemoryAllocatorDumpGuid source;
38 MemoryAllocatorDumpGuid target;
39 int importance;
40 const char* type;
43 // Maps allocator dumps absolute names (allocator_name/heap/subheap) to
44 // MemoryAllocatorDump instances.
45 using AllocatorDumpsMap =
46 SmallMap<hash_map<std::string, MemoryAllocatorDump*>>;
48 ProcessMemoryDump(const scoped_refptr<MemoryDumpSessionState>& session_state);
49 ~ProcessMemoryDump();
51 // Called at trace generation time to populate the TracedValue.
52 void AsValueInto(TracedValue* value) const;
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();
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);
66 ProcessMemoryTotals* process_totals() { return &process_totals_; }
67 bool has_process_totals() const { return has_process_totals_; }
68 void set_has_process_totals() { has_process_totals_ = true; }
70 ProcessMemoryMaps* process_mmaps() { return &process_mmaps_; }
71 bool has_process_mmaps() const { return has_process_mmaps_; }
72 void set_has_process_mmaps() { has_process_mmaps_ = true; }
74 // Creates a new MemoryAllocatorDump with the given name and returns the
75 // empty object back to the caller.
76 // Arguments:
77 // absolute_name: a name that uniquely identifies allocator dumps produced
78 // by this provider. It is possible to specify nesting by using a
79 // path-like string (e.g., v8/isolate1/heap1, v8/isolate1/heap2).
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.
84 // ProcessMemoryDump handles the memory ownership of its MemoryAllocatorDumps.
85 MemoryAllocatorDump* CreateAllocatorDump(const std::string& absolute_name);
86 MemoryAllocatorDump* CreateAllocatorDump(const std::string& absolute_name,
87 const MemoryAllocatorDumpGuid& guid);
89 // Looks up a MemoryAllocatorDump given its allocator and heap names, or
90 // nullptr if not found.
91 MemoryAllocatorDump* GetAllocatorDump(const std::string& absolute_name) const;
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);
100 // Looks up a shared MemoryAllocatorDump given its guid.
101 MemoryAllocatorDump* GetSharedGlobalAllocatorDump(
102 const MemoryAllocatorDumpGuid& guid) const;
104 // Returns the map of the MemoryAllocatorDumps added to this dump.
105 const AllocatorDumpsMap& allocator_dumps() const { return allocator_dumps_; }
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);
118 const std::vector<MemoryAllocatorDumpEdge>& allocator_dumps_edges() const {
119 return allocator_dumps_edges_;
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);
130 const scoped_refptr<MemoryDumpSessionState>& session_state() const {
131 return session_state_;
134 private:
135 void AddAllocatorDumpInternal(MemoryAllocatorDump* mad);
137 ProcessMemoryTotals process_totals_;
138 bool has_process_totals_;
140 ProcessMemoryMaps process_mmaps_;
141 bool has_process_mmaps_;
143 AllocatorDumpsMap allocator_dumps_;
145 // ProcessMemoryDump handles the memory ownership of all its belongings.
146 ScopedVector<MemoryAllocatorDump> allocator_dumps_storage_;
148 // State shared among all PMDs instances created in a given trace session.
149 scoped_refptr<MemoryDumpSessionState> session_state_;
151 // Keeps track of relationships between MemoryAllocatorDump(s).
152 std::vector<MemoryAllocatorDumpEdge> allocator_dumps_edges_;
154 DISALLOW_COPY_AND_ASSIGN(ProcessMemoryDump);
157 } // namespace trace_event
158 } // namespace base
160 #endif // BASE_TRACE_EVENT_PROCESS_MEMORY_DUMP_H_