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 #include "base/trace_event/process_memory_dump.h"
7 #include "base/trace_event/process_memory_totals.h"
8 #include "base/trace_event/trace_event_argument.h"
11 namespace trace_event
{
14 const char kEdgeTypeOwnership
[] = "ownership";
17 ProcessMemoryDump::ProcessMemoryDump(
18 const scoped_refptr
<MemoryDumpSessionState
>& session_state
)
19 : has_process_totals_(false),
20 has_process_mmaps_(false),
21 session_state_(session_state
) {
24 ProcessMemoryDump::~ProcessMemoryDump() {
27 MemoryAllocatorDump
* ProcessMemoryDump::CreateAllocatorDump(
28 const std::string
& absolute_name
) {
29 MemoryAllocatorDump
* mad
= new MemoryAllocatorDump(absolute_name
, this);
30 AddAllocatorDumpInternal(mad
); // Takes ownership of |mad|.
34 MemoryAllocatorDump
* ProcessMemoryDump::CreateAllocatorDump(
35 const std::string
& absolute_name
,
36 const MemoryAllocatorDumpGuid
& guid
) {
37 MemoryAllocatorDump
* mad
= new MemoryAllocatorDump(absolute_name
, this, guid
);
38 AddAllocatorDumpInternal(mad
); // Takes ownership of |mad|.
42 void ProcessMemoryDump::AddAllocatorDumpInternal(MemoryAllocatorDump
* mad
) {
43 DCHECK_EQ(0ul, allocator_dumps_
.count(mad
->absolute_name()));
44 allocator_dumps_storage_
.push_back(mad
);
45 allocator_dumps_
[mad
->absolute_name()] = mad
;
48 MemoryAllocatorDump
* ProcessMemoryDump::GetAllocatorDump(
49 const std::string
& absolute_name
) const {
50 auto it
= allocator_dumps_
.find(absolute_name
);
51 return it
== allocator_dumps_
.end() ? nullptr : it
->second
;
54 void ProcessMemoryDump::Clear() {
55 if (has_process_totals_
) {
56 process_totals_
.Clear();
57 has_process_totals_
= false;
60 if (has_process_mmaps_
) {
61 process_mmaps_
.Clear();
62 has_process_mmaps_
= false;
65 allocator_dumps_storage_
.clear();
66 allocator_dumps_
.clear();
67 allocator_dumps_edges_
.clear();
70 void ProcessMemoryDump::TakeAllDumpsFrom(ProcessMemoryDump
* other
) {
71 DCHECK(!other
->has_process_totals() && !other
->has_process_mmaps());
73 // Moves the ownership of all MemoryAllocatorDump(s) contained in |other|
74 // into this ProcessMemoryDump.
75 for (MemoryAllocatorDump
* mad
: other
->allocator_dumps_storage_
) {
76 // Check that we don't merge duplicates.
77 DCHECK_EQ(0ul, allocator_dumps_
.count(mad
->absolute_name()));
78 allocator_dumps_storage_
.push_back(mad
);
79 allocator_dumps_
[mad
->absolute_name()] = mad
;
81 other
->allocator_dumps_storage_
.weak_clear();
82 other
->allocator_dumps_
.clear();
84 // Move all the edges.
85 allocator_dumps_edges_
.insert(allocator_dumps_edges_
.end(),
86 other
->allocator_dumps_edges_
.begin(),
87 other
->allocator_dumps_edges_
.end());
88 other
->allocator_dumps_edges_
.clear();
91 void ProcessMemoryDump::AsValueInto(TracedValue
* value
) const {
92 if (has_process_totals_
) {
93 value
->BeginDictionary("process_totals");
94 process_totals_
.AsValueInto(value
);
95 value
->EndDictionary();
98 if (has_process_mmaps_
) {
99 value
->BeginDictionary("process_mmaps");
100 process_mmaps_
.AsValueInto(value
);
101 value
->EndDictionary();
104 if (allocator_dumps_storage_
.size() > 0) {
105 value
->BeginDictionary("allocators");
106 for (const MemoryAllocatorDump
* allocator_dump
: allocator_dumps_storage_
)
107 allocator_dump
->AsValueInto(value
);
108 value
->EndDictionary();
111 value
->BeginArray("allocators_graph");
112 for (const MemoryAllocatorDumpEdge
& edge
: allocator_dumps_edges_
) {
113 value
->BeginDictionary();
114 value
->SetString("source", edge
.source
.ToString());
115 value
->SetString("target", edge
.target
.ToString());
116 value
->SetInteger("importance", edge
.importance
);
117 value
->SetString("type", edge
.type
);
118 value
->EndDictionary();
123 void ProcessMemoryDump::AddOwnershipEdge(MemoryAllocatorDumpGuid source
,
124 MemoryAllocatorDumpGuid target
,
126 allocator_dumps_edges_
.push_back(
127 {source
, target
, importance
, kEdgeTypeOwnership
});
130 void ProcessMemoryDump::AddOwnershipEdge(MemoryAllocatorDumpGuid source
,
131 MemoryAllocatorDumpGuid target
) {
132 AddOwnershipEdge(source
, target
, 0 /* importance */);
135 } // namespace trace_event