Index: base/trace_event/memory_dump_manager_unittest.cc |
diff --git a/base/trace_event/memory_dump_manager_unittest.cc b/base/trace_event/memory_dump_manager_unittest.cc |
index e8c33af3e06b935a80911bf049ff3d6e8c3c4b01..7ecfc90c73ba2f6b7537f0e5fd69404bcd76f0ee 100644 |
--- a/base/trace_event/memory_dump_manager_unittest.cc |
+++ b/base/trace_event/memory_dump_manager_unittest.cc |
@@ -9,13 +9,17 @@ |
#include <memory> |
#include <vector> |
+#include "base/base_switches.h" |
#include "base/bind_helpers.h" |
+#include "base/json/json_reader.h" |
+#include "base/json/json_writer.h" |
#include "base/memory/ptr_util.h" |
#include "base/memory/ref_counted_memory.h" |
#include "base/message_loop/message_loop.h" |
#include "base/run_loop.h" |
#include "base/strings/stringprintf.h" |
#include "base/synchronization/waitable_event.h" |
+#include "base/test/scoped_command_line.h" |
#include "base/test/sequenced_worker_pool_owner.h" |
#include "base/test/test_io_thread.h" |
#include "base/test/trace_event_analyzer.h" |
@@ -24,6 +28,7 @@ |
#include "base/threading/sequenced_worker_pool.h" |
#include "base/threading/thread.h" |
#include "base/threading/thread_task_runner_handle.h" |
+#include "base/trace_event/heap_profiler_allocation_register.h" |
#include "base/trace_event/memory_dump_provider.h" |
#include "base/trace_event/memory_infra_background_whitelist.h" |
#include "base/trace_event/process_memory_dump.h" |
@@ -1283,5 +1288,72 @@ TEST_F(MemoryDumpManagerTest, TestBlacklistedUnsafeUnregistration) { |
thread.Stop(); |
} |
+//============================================================================== |
+ |
+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
|
+ public: |
+ HeapDumpProvider(const char* allocator_name) |
+ : allocator_name_(allocator_name) {} |
+ |
+ bool OnMemoryDump(const MemoryDumpArgs& args, |
+ ProcessMemoryDump* pmd) override { |
+ AllocationRegister allocation_register(100, 100); |
+ AllocationContext context; |
+ context.backtrace.frames[0] = StackFrame::FromThreadName("main"); |
+ context.backtrace.frames[1] = StackFrame::FromTraceEventName("foo()"); |
+ context.backtrace.frame_count = 2; |
+ context.type_name = "FooClass"; |
+ |
+ allocation_register.Insert(reinterpret_cast<const void*>(0x100), 100, |
+ context); |
+ |
+ pmd->DumpHeapUsage(allocation_register, allocator_name_.c_str()); |
+ return true; |
+ } |
+ |
+ private: |
+ std::string allocator_name_; |
+}; |
+ |
+TEST_F(MemoryDumpManagerTest, TraceJSON) { |
+ base::test::ScopedCommandLine scoped_command_line; |
+ scoped_command_line.GetProcessCommandLine()->AppendSwitch( |
+ switches::kEnableHeapProfiling); |
+ |
+ InitializeMemoryDumpManager(false /* is_coordinator */); |
+ |
+ // Standard provider with default options (create dump for current process). |
+ MemoryDumpProvider::Options options; |
+ HeapDumpProvider mdp1("malloc"); |
+ RegisterDumpProvider(&mdp1, nullptr, options); |
+ |
+ // Another provider with out-of-process dumping. |
+ HeapDumpProvider mdp3("blink_gc"); |
+ options.target_pid = 456; |
+ RegisterDumpProvider(&mdp3, nullptr, options); |
+ |
+ EnableTracingWithLegacyCategories(MemoryDumpManager::kTraceCategory); |
+ EXPECT_CALL(*delegate_, RequestGlobalMemoryDump(_, _)).Times(1); |
+ RequestGlobalDumpAndWait(MemoryDumpType::EXPLICITLY_TRIGGERED, |
+ MemoryDumpLevelOfDetail::DETAILED); |
+ DisableTracing(); |
+ |
+ // Flush the trace into JSON. |
+ trace_event::TraceResultBuffer buffer; |
+ TraceResultBuffer::SimpleOutput trace_output; |
+ buffer.SetOutputCallback(trace_output.GetCallback()); |
+ RunLoop run_loop; |
+ buffer.Start(); |
+ trace_event::TraceLog::GetInstance()->Flush( |
+ Bind(&OnTraceDataCollected, run_loop.QuitClosure(), Unretained(&buffer))); |
+ run_loop.Run(); |
+ buffer.Finish(); |
+ |
+ std::string pretty_json; |
+ JSONWriter::WriteWithOptions(*JSONReader::Read(trace_output.json_output), |
+ JSONWriter::OPTIONS_PRETTY_PRINT, &pretty_json); |
+ LOG(INFO) << "\n" << pretty_json; |
+} |
+ |
} // namespace trace_event |
} // namespace base |