SupervisedUser SafeSites: Switch to the new SafeSearch API
[chromium-blink-merge.git] / base / trace_event / process_memory_dump.h
blob3b71a2c41ae0409bdf8fe05c19993ec740c0aa8a
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 // Creates a new MemoryAllocatorDump with the given name and returns the
52 // empty object back to the caller.
53 // Arguments:
54 // absolute_name: a name that uniquely identifies allocator dumps produced
55 // by this provider. It is possible to specify nesting by using a
56 // path-like string (e.g., v8/isolate1/heap1, v8/isolate1/heap2).
57 // Leading or trailing slashes are not allowed.
58 // guid: an optional identifier, unique among all processes within the
59 // scope of a global dump. This is only relevant when using
60 // AddOwnershipEdge() to express memory sharing. If omitted,
61 // it will be automatically generated.
62 // ProcessMemoryDump handles the memory ownership of its MemoryAllocatorDumps.
63 MemoryAllocatorDump* CreateAllocatorDump(const std::string& absolute_name);
64 MemoryAllocatorDump* CreateAllocatorDump(const std::string& absolute_name,
65 const MemoryAllocatorDumpGuid& guid);
67 // Looks up a MemoryAllocatorDump given its allocator and heap names, or
68 // nullptr if not found.
69 MemoryAllocatorDump* GetAllocatorDump(const std::string& absolute_name) const;
71 // Creates a shared MemoryAllocatorDump, to express cross-process sharing.
72 // Shared allocator dumps are allowed to have duplicate guids within the
73 // global scope, in order to reference the same dump from multiple processes.
74 // See the design doc goo.gl/keU6Bf for reference usage patterns.
75 MemoryAllocatorDump* CreateSharedGlobalAllocatorDump(
76 const MemoryAllocatorDumpGuid& guid);
78 // Looks up a shared MemoryAllocatorDump given its guid.
79 MemoryAllocatorDump* GetSharedGlobalAllocatorDump(
80 const MemoryAllocatorDumpGuid& guid) const;
82 // Returns the map of the MemoryAllocatorDumps added to this dump.
83 const AllocatorDumpsMap& allocator_dumps() const { return allocator_dumps_; }
85 // Adds an ownership relationship between two MemoryAllocatorDump(s) with the
86 // semantics: |source| owns |target|, and has the effect of attributing
87 // the memory usage of |target| to |source|. |importance| is optional and
88 // relevant only for the cases of co-ownership, where it acts as a z-index:
89 // the owner with the highest importance will be attributed |target|'s memory.
90 void AddOwnershipEdge(const MemoryAllocatorDumpGuid& source,
91 const MemoryAllocatorDumpGuid& target,
92 int importance);
93 void AddOwnershipEdge(const MemoryAllocatorDumpGuid& source,
94 const MemoryAllocatorDumpGuid& target);
96 const std::vector<MemoryAllocatorDumpEdge>& allocator_dumps_edges() const {
97 return allocator_dumps_edges_;
100 // Utility method to add a suballocation relationship with the following
101 // semantics: |source| is suballocated from |target_node_name|.
102 // This creates a child node of |target_node_name| and adds an ownership edge
103 // between |source| and the new child node. As a result, the UI will not
104 // account the memory of |source| in the target node.
105 void AddSuballocation(const MemoryAllocatorDumpGuid& source,
106 const std::string& target_node_name);
108 const scoped_refptr<MemoryDumpSessionState>& session_state() const {
109 return session_state_;
112 // Removes all the MemoryAllocatorDump(s) contained in this instance. This
113 // ProcessMemoryDump can be safely reused as if it was new once this returns.
114 void Clear();
116 // Merges all MemoryAllocatorDump(s) contained in |other| inside this
117 // ProcessMemoryDump, transferring their ownership to this instance.
118 // |other| will be an empty ProcessMemoryDump after this method returns.
119 // This is to allow dump providers to pre-populate ProcessMemoryDump instances
120 // and later move their contents into the ProcessMemoryDump passed as argument
121 // of the MemoryDumpProvider::OnMemoryDump(ProcessMemoryDump*) callback.
122 void TakeAllDumpsFrom(ProcessMemoryDump* other);
124 // Called at trace generation time to populate the TracedValue.
125 void AsValueInto(TracedValue* value) const;
127 ProcessMemoryTotals* process_totals() { return &process_totals_; }
128 bool has_process_totals() const { return has_process_totals_; }
129 void set_has_process_totals() { has_process_totals_ = true; }
131 ProcessMemoryMaps* process_mmaps() { return &process_mmaps_; }
132 bool has_process_mmaps() const { return has_process_mmaps_; }
133 void set_has_process_mmaps() { has_process_mmaps_ = true; }
135 private:
136 void AddAllocatorDumpInternal(MemoryAllocatorDump* mad);
138 ProcessMemoryTotals process_totals_;
139 bool has_process_totals_;
141 ProcessMemoryMaps process_mmaps_;
142 bool has_process_mmaps_;
144 AllocatorDumpsMap allocator_dumps_;
146 // ProcessMemoryDump handles the memory ownership of all its belongings.
147 ScopedVector<MemoryAllocatorDump> allocator_dumps_storage_;
149 // State shared among all PMDs instances created in a given trace session.
150 scoped_refptr<MemoryDumpSessionState> session_state_;
152 // Keeps track of relationships between MemoryAllocatorDump(s).
153 std::vector<MemoryAllocatorDumpEdge> allocator_dumps_edges_;
155 DISALLOW_COPY_AND_ASSIGN(ProcessMemoryDump);
158 } // namespace trace_event
159 } // namespace base
161 #endif // BASE_TRACE_EVENT_PROCESS_MEMORY_DUMP_H_