Extension syncing: Introduce a NeedsSync pref
[chromium-blink-merge.git] / base / trace_event / process_memory_dump.cc
blob46ae1fc611822b606b730330a14ddaa7c0eeb373
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"
10 namespace base {
11 namespace trace_event {
13 namespace {
14 const char kEdgeTypeOwnership[] = "ownership";
16 std::string GetSharedGlobalAllocatorDumpName(
17 const MemoryAllocatorDumpGuid& guid) {
18 return "global/" + guid.ToString();
20 } // namespace
22 ProcessMemoryDump::ProcessMemoryDump(
23 const scoped_refptr<MemoryDumpSessionState>& session_state)
24 : has_process_totals_(false),
25 has_process_mmaps_(false),
26 session_state_(session_state) {
29 ProcessMemoryDump::~ProcessMemoryDump() {
32 MemoryAllocatorDump* ProcessMemoryDump::CreateAllocatorDump(
33 const std::string& absolute_name) {
34 MemoryAllocatorDump* mad = new MemoryAllocatorDump(absolute_name, this);
35 AddAllocatorDumpInternal(mad); // Takes ownership of |mad|.
36 return mad;
39 MemoryAllocatorDump* ProcessMemoryDump::CreateAllocatorDump(
40 const std::string& absolute_name,
41 const MemoryAllocatorDumpGuid& guid) {
42 MemoryAllocatorDump* mad = new MemoryAllocatorDump(absolute_name, this, guid);
43 AddAllocatorDumpInternal(mad); // Takes ownership of |mad|.
44 return mad;
47 void ProcessMemoryDump::AddAllocatorDumpInternal(MemoryAllocatorDump* mad) {
48 DCHECK_EQ(0ul, allocator_dumps_.count(mad->absolute_name()));
49 allocator_dumps_storage_.push_back(mad);
50 allocator_dumps_[mad->absolute_name()] = mad;
53 MemoryAllocatorDump* ProcessMemoryDump::GetAllocatorDump(
54 const std::string& absolute_name) const {
55 auto it = allocator_dumps_.find(absolute_name);
56 return it == allocator_dumps_.end() ? nullptr : it->second;
59 MemoryAllocatorDump* ProcessMemoryDump::CreateSharedGlobalAllocatorDump(
60 const MemoryAllocatorDumpGuid& guid) {
61 return CreateAllocatorDump(GetSharedGlobalAllocatorDumpName(guid), guid);
64 MemoryAllocatorDump* ProcessMemoryDump::GetSharedGlobalAllocatorDump(
65 const MemoryAllocatorDumpGuid& guid) const {
66 return GetAllocatorDump(GetSharedGlobalAllocatorDumpName(guid));
69 void ProcessMemoryDump::Clear() {
70 if (has_process_totals_) {
71 process_totals_.Clear();
72 has_process_totals_ = false;
75 if (has_process_mmaps_) {
76 process_mmaps_.Clear();
77 has_process_mmaps_ = false;
80 allocator_dumps_storage_.clear();
81 allocator_dumps_.clear();
82 allocator_dumps_edges_.clear();
85 void ProcessMemoryDump::TakeAllDumpsFrom(ProcessMemoryDump* other) {
86 DCHECK(!other->has_process_totals() && !other->has_process_mmaps());
88 // Moves the ownership of all MemoryAllocatorDump(s) contained in |other|
89 // into this ProcessMemoryDump.
90 for (MemoryAllocatorDump* mad : other->allocator_dumps_storage_) {
91 // Check that we don't merge duplicates.
92 DCHECK_EQ(0ul, allocator_dumps_.count(mad->absolute_name()));
93 allocator_dumps_storage_.push_back(mad);
94 allocator_dumps_[mad->absolute_name()] = mad;
96 other->allocator_dumps_storage_.weak_clear();
97 other->allocator_dumps_.clear();
99 // Move all the edges.
100 allocator_dumps_edges_.insert(allocator_dumps_edges_.end(),
101 other->allocator_dumps_edges_.begin(),
102 other->allocator_dumps_edges_.end());
103 other->allocator_dumps_edges_.clear();
106 void ProcessMemoryDump::AsValueInto(TracedValue* value) const {
107 if (has_process_totals_) {
108 value->BeginDictionary("process_totals");
109 process_totals_.AsValueInto(value);
110 value->EndDictionary();
113 if (has_process_mmaps_) {
114 value->BeginDictionary("process_mmaps");
115 process_mmaps_.AsValueInto(value);
116 value->EndDictionary();
119 if (allocator_dumps_storage_.size() > 0) {
120 value->BeginDictionary("allocators");
121 for (const MemoryAllocatorDump* allocator_dump : allocator_dumps_storage_)
122 allocator_dump->AsValueInto(value);
123 value->EndDictionary();
126 value->BeginArray("allocators_graph");
127 for (const MemoryAllocatorDumpEdge& edge : allocator_dumps_edges_) {
128 value->BeginDictionary();
129 value->SetString("source", edge.source.ToString());
130 value->SetString("target", edge.target.ToString());
131 value->SetInteger("importance", edge.importance);
132 value->SetString("type", edge.type);
133 value->EndDictionary();
135 value->EndArray();
138 void ProcessMemoryDump::AddOwnershipEdge(const MemoryAllocatorDumpGuid& source,
139 const MemoryAllocatorDumpGuid& target,
140 int importance) {
141 allocator_dumps_edges_.push_back(
142 {source, target, importance, kEdgeTypeOwnership});
145 void ProcessMemoryDump::AddOwnershipEdge(
146 const MemoryAllocatorDumpGuid& source,
147 const MemoryAllocatorDumpGuid& target) {
148 AddOwnershipEdge(source, target, 0 /* importance */);
151 void ProcessMemoryDump::AddSuballocation(const MemoryAllocatorDumpGuid& source,
152 const std::string& target_node_name) {
153 std::string child_mad_name = target_node_name + "/__" + source.ToString();
154 MemoryAllocatorDump* target_child_mad = CreateAllocatorDump(child_mad_name);
155 AddOwnershipEdge(source, target_child_mad->guid());
158 } // namespace trace_event
159 } // namespace base