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/memory_dump_manager.h" | 5 #include "base/trace_event/memory_dump_manager.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/base_switches.h" | |
12 #include "base/bind_helpers.h" | 13 #include "base/bind_helpers.h" |
14 #include "base/json/json_reader.h" | |
15 #include "base/json/json_writer.h" | |
13 #include "base/memory/ptr_util.h" | 16 #include "base/memory/ptr_util.h" |
14 #include "base/memory/ref_counted_memory.h" | 17 #include "base/memory/ref_counted_memory.h" |
15 #include "base/message_loop/message_loop.h" | 18 #include "base/message_loop/message_loop.h" |
16 #include "base/run_loop.h" | 19 #include "base/run_loop.h" |
17 #include "base/strings/stringprintf.h" | 20 #include "base/strings/stringprintf.h" |
18 #include "base/synchronization/waitable_event.h" | 21 #include "base/synchronization/waitable_event.h" |
22 #include "base/test/scoped_command_line.h" | |
19 #include "base/test/sequenced_worker_pool_owner.h" | 23 #include "base/test/sequenced_worker_pool_owner.h" |
20 #include "base/test/test_io_thread.h" | 24 #include "base/test/test_io_thread.h" |
21 #include "base/test/trace_event_analyzer.h" | 25 #include "base/test/trace_event_analyzer.h" |
22 #include "base/threading/platform_thread.h" | 26 #include "base/threading/platform_thread.h" |
23 #include "base/threading/sequenced_task_runner_handle.h" | 27 #include "base/threading/sequenced_task_runner_handle.h" |
24 #include "base/threading/sequenced_worker_pool.h" | 28 #include "base/threading/sequenced_worker_pool.h" |
25 #include "base/threading/thread.h" | 29 #include "base/threading/thread.h" |
26 #include "base/threading/thread_task_runner_handle.h" | 30 #include "base/threading/thread_task_runner_handle.h" |
31 #include "base/trace_event/heap_profiler_allocation_register.h" | |
27 #include "base/trace_event/memory_dump_provider.h" | 32 #include "base/trace_event/memory_dump_provider.h" |
28 #include "base/trace_event/memory_infra_background_whitelist.h" | 33 #include "base/trace_event/memory_infra_background_whitelist.h" |
29 #include "base/trace_event/process_memory_dump.h" | 34 #include "base/trace_event/process_memory_dump.h" |
30 #include "base/trace_event/trace_buffer.h" | 35 #include "base/trace_event/trace_buffer.h" |
31 #include "base/trace_event/trace_config_memory_test_util.h" | 36 #include "base/trace_event/trace_config_memory_test_util.h" |
32 #include "testing/gmock/include/gmock/gmock.h" | 37 #include "testing/gmock/include/gmock/gmock.h" |
33 #include "testing/gtest/include/gtest/gtest.h" | 38 #include "testing/gtest/include/gtest/gtest.h" |
34 | 39 |
35 using testing::_; | 40 using testing::_; |
36 using testing::AnyNumber; | 41 using testing::AnyNumber; |
(...skipping 1239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1276 | 1281 |
1277 Thread thread("test thread"); | 1282 Thread thread("test thread"); |
1278 thread.Start(); | 1283 thread.Start(); |
1279 RegisterDumpProvider(&mdp1, thread.task_runner(), kDefaultOptions, | 1284 RegisterDumpProvider(&mdp1, thread.task_runner(), kDefaultOptions, |
1280 "BlacklistTestDumpProvider"); | 1285 "BlacklistTestDumpProvider"); |
1281 // Unregistering on wrong thread should not crash. | 1286 // Unregistering on wrong thread should not crash. |
1282 mdm_->UnregisterDumpProvider(&mdp1); | 1287 mdm_->UnregisterDumpProvider(&mdp1); |
1283 thread.Stop(); | 1288 thread.Stop(); |
1284 } | 1289 } |
1285 | 1290 |
1291 //============================================================================== | |
1292 | |
1293 class HeapDumpProvider : public MemoryDumpProvider { | |
Primiano Tucci (use gerrit)
2017/02/17 17:07:05
shouldn't these be in the *heap_profiler_something
DmitrySkiba
2017/02/23 07:17:19
This needs to be removed, it's a code I used durin
| |
1294 public: | |
1295 HeapDumpProvider(const char* allocator_name) | |
1296 : allocator_name_(allocator_name) {} | |
1297 | |
1298 bool OnMemoryDump(const MemoryDumpArgs& args, | |
1299 ProcessMemoryDump* pmd) override { | |
1300 AllocationRegister allocation_register(100, 100); | |
1301 AllocationContext context; | |
1302 context.backtrace.frames[0] = StackFrame::FromThreadName("main"); | |
1303 context.backtrace.frames[1] = StackFrame::FromTraceEventName("foo()"); | |
1304 context.backtrace.frame_count = 2; | |
1305 context.type_name = "FooClass"; | |
1306 | |
1307 allocation_register.Insert(reinterpret_cast<const void*>(0x100), 100, | |
1308 context); | |
1309 | |
1310 pmd->DumpHeapUsage(allocation_register, allocator_name_.c_str()); | |
1311 return true; | |
1312 } | |
1313 | |
1314 private: | |
1315 std::string allocator_name_; | |
1316 }; | |
1317 | |
1318 TEST_F(MemoryDumpManagerTest, TraceJSON) { | |
1319 base::test::ScopedCommandLine scoped_command_line; | |
1320 scoped_command_line.GetProcessCommandLine()->AppendSwitch( | |
1321 switches::kEnableHeapProfiling); | |
1322 | |
1323 InitializeMemoryDumpManager(false /* is_coordinator */); | |
1324 | |
1325 // Standard provider with default options (create dump for current process). | |
1326 MemoryDumpProvider::Options options; | |
1327 HeapDumpProvider mdp1("malloc"); | |
1328 RegisterDumpProvider(&mdp1, nullptr, options); | |
1329 | |
1330 // Another provider with out-of-process dumping. | |
1331 HeapDumpProvider mdp3("blink_gc"); | |
1332 options.target_pid = 456; | |
1333 RegisterDumpProvider(&mdp3, nullptr, options); | |
1334 | |
1335 EnableTracingWithLegacyCategories(MemoryDumpManager::kTraceCategory); | |
1336 EXPECT_CALL(*delegate_, RequestGlobalMemoryDump(_, _)).Times(1); | |
1337 RequestGlobalDumpAndWait(MemoryDumpType::EXPLICITLY_TRIGGERED, | |
1338 MemoryDumpLevelOfDetail::DETAILED); | |
1339 DisableTracing(); | |
1340 | |
1341 // Flush the trace into JSON. | |
1342 trace_event::TraceResultBuffer buffer; | |
1343 TraceResultBuffer::SimpleOutput trace_output; | |
1344 buffer.SetOutputCallback(trace_output.GetCallback()); | |
1345 RunLoop run_loop; | |
1346 buffer.Start(); | |
1347 trace_event::TraceLog::GetInstance()->Flush( | |
1348 Bind(&OnTraceDataCollected, run_loop.QuitClosure(), Unretained(&buffer))); | |
1349 run_loop.Run(); | |
1350 buffer.Finish(); | |
1351 | |
1352 std::string pretty_json; | |
1353 JSONWriter::WriteWithOptions(*JSONReader::Read(trace_output.json_output), | |
1354 JSONWriter::OPTIONS_PRETTY_PRINT, &pretty_json); | |
1355 LOG(INFO) << "\n" << pretty_json; | |
1356 } | |
1357 | |
1286 } // namespace trace_event | 1358 } // namespace trace_event |
1287 } // namespace base | 1359 } // namespace base |
OLD | NEW |