OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/trace_event/process_memory_dump.h" | 5 #include "base/trace_event/process_memory_dump.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 | 8 |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
12 #include "base/process/process_metrics.h" | 12 #include "base/process/process_metrics.h" |
13 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
14 #include "base/trace_event/heap_profiler_heap_dump_writer.h" | 14 #include "base/trace_event/heap_profiler_heap_dump_writer.h" |
15 #include "base/trace_event/memory_dump_request_args.h" | |
16 #include "base/trace_event/memory_infra_background_whitelist.h" | |
15 #include "base/trace_event/process_memory_totals.h" | 17 #include "base/trace_event/process_memory_totals.h" |
16 #include "base/trace_event/trace_event_argument.h" | 18 #include "base/trace_event/trace_event_argument.h" |
17 #include "build/build_config.h" | 19 #include "build/build_config.h" |
18 | 20 |
19 #if defined(OS_IOS) | 21 #if defined(OS_IOS) |
20 #include <sys/sysctl.h> | 22 #include <sys/sysctl.h> |
21 #endif | 23 #endif |
22 | 24 |
23 #if defined(OS_POSIX) | 25 #if defined(OS_POSIX) |
24 #include <sys/mman.h> | 26 #include <sys/mman.h> |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
141 DCHECK(!failure); | 143 DCHECK(!failure); |
142 if (failure) { | 144 if (failure) { |
143 total_resident_size = 0; | 145 total_resident_size = 0; |
144 LOG(ERROR) << "CountResidentBytes failed. The resident size is invalid"; | 146 LOG(ERROR) << "CountResidentBytes failed. The resident size is invalid"; |
145 } | 147 } |
146 return total_resident_size; | 148 return total_resident_size; |
147 } | 149 } |
148 #endif // defined(COUNT_RESIDENT_BYTES_SUPPORTED) | 150 #endif // defined(COUNT_RESIDENT_BYTES_SUPPORTED) |
149 | 151 |
150 ProcessMemoryDump::ProcessMemoryDump( | 152 ProcessMemoryDump::ProcessMemoryDump( |
151 scoped_refptr<MemoryDumpSessionState> session_state) | 153 scoped_refptr<MemoryDumpSessionState> session_state, |
154 const MemoryDumpArgs& dump_args) | |
152 : has_process_totals_(false), | 155 : has_process_totals_(false), |
153 has_process_mmaps_(false), | 156 has_process_mmaps_(false), |
154 session_state_(std::move(session_state)) {} | 157 session_state_(std::move(session_state)), |
158 dump_args_(dump_args) { | |
159 if (dump_args_.level_of_detail == MemoryDumpLevelOfDetail::BACKGROUND) { | |
160 black_hole_mad_.reset(new MemoryAllocatorDump("dummy", this)); | |
Primiano Tucci (use gerrit)
2016/06/02 20:24:04
I'd create this only if we need it. If we do every
ssid
2016/06/03 01:59:46
Added a get method. dcheck_ne was added because of
| |
161 } | |
162 } | |
155 | 163 |
156 ProcessMemoryDump::~ProcessMemoryDump() {} | 164 ProcessMemoryDump::~ProcessMemoryDump() {} |
157 | 165 |
158 MemoryAllocatorDump* ProcessMemoryDump::CreateAllocatorDump( | 166 MemoryAllocatorDump* ProcessMemoryDump::CreateAllocatorDump( |
159 const std::string& absolute_name) { | 167 const std::string& absolute_name) { |
160 return AddAllocatorDumpInternal( | 168 auto mad = AddAllocatorDumpInternal( |
161 WrapUnique(new MemoryAllocatorDump(absolute_name, this))); | 169 WrapUnique(new MemoryAllocatorDump(absolute_name, this))); |
170 DCHECK_NE(black_hole_mad_.get(), mad); | |
171 return mad; | |
Primiano Tucci (use gerrit)
2016/06/02 20:24:04
you can keep this as it was if you use the helper.
ssid
2016/06/03 01:59:46
This is what I tried to explain in my previous com
| |
162 } | 172 } |
163 | 173 |
164 MemoryAllocatorDump* ProcessMemoryDump::CreateAllocatorDump( | 174 MemoryAllocatorDump* ProcessMemoryDump::CreateAllocatorDump( |
165 const std::string& absolute_name, | 175 const std::string& absolute_name, |
166 const MemoryAllocatorDumpGuid& guid) { | 176 const MemoryAllocatorDumpGuid& guid) { |
167 return AddAllocatorDumpInternal( | 177 auto mad = AddAllocatorDumpInternal( |
168 WrapUnique(new MemoryAllocatorDump(absolute_name, this, guid))); | 178 WrapUnique(new MemoryAllocatorDump(absolute_name, this, guid))); |
179 DCHECK_NE(black_hole_mad_.get(), mad); | |
180 return mad; | |
169 } | 181 } |
170 | 182 |
171 MemoryAllocatorDump* ProcessMemoryDump::AddAllocatorDumpInternal( | 183 MemoryAllocatorDump* ProcessMemoryDump::AddAllocatorDumpInternal( |
172 std::unique_ptr<MemoryAllocatorDump> mad) { | 184 std::unique_ptr<MemoryAllocatorDump> mad) { |
185 // In background mode return dummy dump if invalid dump name is given. | |
186 if (dump_args_.level_of_detail == MemoryDumpLevelOfDetail::BACKGROUND && | |
187 !IsMemoryAllocatorDumpNameWhitelisted(mad->absolute_name())) { | |
188 return black_hole_mad_.get(); | |
189 } | |
190 | |
173 auto insertion_result = allocator_dumps_.insert( | 191 auto insertion_result = allocator_dumps_.insert( |
174 std::make_pair(mad->absolute_name(), std::move(mad))); | 192 std::make_pair(mad->absolute_name(), std::move(mad))); |
175 MemoryAllocatorDump* inserted_mad = insertion_result.first->second.get(); | 193 MemoryAllocatorDump* inserted_mad = insertion_result.first->second.get(); |
176 DCHECK(insertion_result.second) << "Duplicate name: " | 194 DCHECK(insertion_result.second) << "Duplicate name: " |
177 << inserted_mad->absolute_name(); | 195 << inserted_mad->absolute_name(); |
178 return inserted_mad; | 196 return inserted_mad; |
179 } | 197 } |
180 | 198 |
181 MemoryAllocatorDump* ProcessMemoryDump::GetAllocatorDump( | 199 MemoryAllocatorDump* ProcessMemoryDump::GetAllocatorDump( |
182 const std::string& absolute_name) const { | 200 const std::string& absolute_name) const { |
183 auto it = allocator_dumps_.find(absolute_name); | 201 auto it = allocator_dumps_.find(absolute_name); |
184 return it == allocator_dumps_.end() ? nullptr : it->second.get(); | 202 if (it != allocator_dumps_.end()) |
203 return it->second.get(); | |
204 return dump_args_.level_of_detail == MemoryDumpLevelOfDetail::BACKGROUND | |
Primiano Tucci (use gerrit)
2016/06/02 20:24:04
nit: At this point stay consistent and keep doing:
ssid
2016/06/03 01:59:46
Done.
| |
205 ? black_hole_mad_.get() | |
206 : nullptr; | |
185 } | 207 } |
186 | 208 |
187 MemoryAllocatorDump* ProcessMemoryDump::GetOrCreateAllocatorDump( | 209 MemoryAllocatorDump* ProcessMemoryDump::GetOrCreateAllocatorDump( |
188 const std::string& absolute_name) { | 210 const std::string& absolute_name) { |
189 MemoryAllocatorDump* mad = GetAllocatorDump(absolute_name); | 211 MemoryAllocatorDump* mad = GetAllocatorDump(absolute_name); |
190 return mad ? mad : CreateAllocatorDump(absolute_name); | 212 return mad ? mad : CreateAllocatorDump(absolute_name); |
191 } | 213 } |
192 | 214 |
193 MemoryAllocatorDump* ProcessMemoryDump::CreateSharedGlobalAllocatorDump( | 215 MemoryAllocatorDump* ProcessMemoryDump::CreateSharedGlobalAllocatorDump( |
194 const MemoryAllocatorDumpGuid& guid) { | 216 const MemoryAllocatorDumpGuid& guid) { |
217 // Global dumps are disabled in background mode. | |
218 if (dump_args_.level_of_detail == MemoryDumpLevelOfDetail::BACKGROUND) | |
219 return black_hole_mad_.get(); | |
220 | |
195 // A shared allocator dump can be shared within a process and the guid could | 221 // A shared allocator dump can be shared within a process and the guid could |
196 // have been created already. | 222 // have been created already. |
197 MemoryAllocatorDump* mad = GetSharedGlobalAllocatorDump(guid); | 223 MemoryAllocatorDump* mad = GetSharedGlobalAllocatorDump(guid); |
198 if (mad) { | 224 if (mad) { |
199 // The weak flag is cleared because this method should create a non-weak | 225 // The weak flag is cleared because this method should create a non-weak |
200 // dump. | 226 // dump. |
201 mad->clear_flags(MemoryAllocatorDump::Flags::WEAK); | 227 mad->clear_flags(MemoryAllocatorDump::Flags::WEAK); |
202 return mad; | 228 return mad; |
203 } | 229 } |
204 return CreateAllocatorDump(GetSharedGlobalAllocatorDumpName(guid), guid); | 230 return CreateAllocatorDump(GetSharedGlobalAllocatorDumpName(guid), guid); |
205 } | 231 } |
206 | 232 |
207 MemoryAllocatorDump* ProcessMemoryDump::CreateWeakSharedGlobalAllocatorDump( | 233 MemoryAllocatorDump* ProcessMemoryDump::CreateWeakSharedGlobalAllocatorDump( |
208 const MemoryAllocatorDumpGuid& guid) { | 234 const MemoryAllocatorDumpGuid& guid) { |
235 // Global dumps are disabled in background mode. | |
236 if (dump_args_.level_of_detail == MemoryDumpLevelOfDetail::BACKGROUND) | |
237 return black_hole_mad_.get(); | |
238 | |
209 MemoryAllocatorDump* mad = GetSharedGlobalAllocatorDump(guid); | 239 MemoryAllocatorDump* mad = GetSharedGlobalAllocatorDump(guid); |
210 if (mad) | 240 if (mad) |
211 return mad; | 241 return mad; |
212 mad = CreateAllocatorDump(GetSharedGlobalAllocatorDumpName(guid), guid); | 242 mad = CreateAllocatorDump(GetSharedGlobalAllocatorDumpName(guid), guid); |
213 mad->set_flags(MemoryAllocatorDump::Flags::WEAK); | 243 mad->set_flags(MemoryAllocatorDump::Flags::WEAK); |
214 return mad; | 244 return mad; |
215 } | 245 } |
216 | 246 |
217 MemoryAllocatorDump* ProcessMemoryDump::GetSharedGlobalAllocatorDump( | 247 MemoryAllocatorDump* ProcessMemoryDump::GetSharedGlobalAllocatorDump( |
218 const MemoryAllocatorDumpGuid& guid) const { | 248 const MemoryAllocatorDumpGuid& guid) const { |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
321 } | 351 } |
322 | 352 |
323 void ProcessMemoryDump::AddOwnershipEdge( | 353 void ProcessMemoryDump::AddOwnershipEdge( |
324 const MemoryAllocatorDumpGuid& source, | 354 const MemoryAllocatorDumpGuid& source, |
325 const MemoryAllocatorDumpGuid& target) { | 355 const MemoryAllocatorDumpGuid& target) { |
326 AddOwnershipEdge(source, target, 0 /* importance */); | 356 AddOwnershipEdge(source, target, 0 /* importance */); |
327 } | 357 } |
328 | 358 |
329 void ProcessMemoryDump::AddSuballocation(const MemoryAllocatorDumpGuid& source, | 359 void ProcessMemoryDump::AddSuballocation(const MemoryAllocatorDumpGuid& source, |
330 const std::string& target_node_name) { | 360 const std::string& target_node_name) { |
361 // Do not create new dumps for suballocations in background mode. | |
362 if (dump_args_.level_of_detail == MemoryDumpLevelOfDetail::BACKGROUND) | |
363 return; | |
Primiano Tucci (use gerrit)
2016/06/02 20:24:04
+ NOTREACHED
ssid
2016/06/03 01:59:46
Hmm, that needs an ugly fix of checking background
Primiano Tucci (use gerrit)
2016/06/03 16:24:36
Ahh I thought you were not expecting suballocation
| |
364 | |
331 std::string child_mad_name = target_node_name + "/__" + source.ToString(); | 365 std::string child_mad_name = target_node_name + "/__" + source.ToString(); |
332 MemoryAllocatorDump* target_child_mad = CreateAllocatorDump(child_mad_name); | 366 MemoryAllocatorDump* target_child_mad = CreateAllocatorDump(child_mad_name); |
333 AddOwnershipEdge(source, target_child_mad->guid()); | 367 AddOwnershipEdge(source, target_child_mad->guid()); |
334 } | 368 } |
335 | 369 |
336 } // namespace trace_event | 370 } // namespace trace_event |
337 } // namespace base | 371 } // namespace base |
OLD | NEW |