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_
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"
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
34 class BASE_EXPORT ProcessMemoryDump
{
36 struct MemoryAllocatorDumpEdge
{
37 MemoryAllocatorDumpGuid source
;
38 MemoryAllocatorDumpGuid target
;
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
);
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.
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.
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 // Returns the map of the MemoryAllocatorDumps added to this dump.
94 const AllocatorDumpsMap
& allocator_dumps() const { return allocator_dumps_
; }
96 // Adds an ownership relationship between two MemoryAllocatorDump(s) with the
97 // semantics: |source| owns |target|, and has the effect of attributing
98 // the memory usage of |target| to |source|. |importance| is optional and
99 // relevant only for the cases of co-ownership, where it acts as a z-index:
100 // the owner with the highest importance will be attributed |target|'s memory.
101 void AddOwnershipEdge(MemoryAllocatorDumpGuid source
,
102 MemoryAllocatorDumpGuid target
,
104 void AddOwnershipEdge(MemoryAllocatorDumpGuid source
,
105 MemoryAllocatorDumpGuid target
);
107 const std::vector
<MemoryAllocatorDumpEdge
>& allocator_dumps_edges() const {
108 return allocator_dumps_edges_
;
111 const scoped_refptr
<MemoryDumpSessionState
>& session_state() const {
112 return session_state_
;
116 void AddAllocatorDumpInternal(MemoryAllocatorDump
* mad
);
118 ProcessMemoryTotals process_totals_
;
119 bool has_process_totals_
;
121 ProcessMemoryMaps process_mmaps_
;
122 bool has_process_mmaps_
;
124 AllocatorDumpsMap allocator_dumps_
;
126 // ProcessMemoryDump handles the memory ownership of all its belongings.
127 ScopedVector
<MemoryAllocatorDump
> allocator_dumps_storage_
;
129 // State shared among all PMDs instances created in a given trace session.
130 scoped_refptr
<MemoryDumpSessionState
> session_state_
;
132 // Keeps track of relationships between MemoryAllocatorDump(s).
133 std::vector
<MemoryAllocatorDumpEdge
> allocator_dumps_edges_
;
135 DISALLOW_COPY_AND_ASSIGN(ProcessMemoryDump
);
138 } // namespace trace_event
141 #endif // BASE_TRACE_EVENT_PROCESS_MEMORY_DUMP_H_