Chromium Code Reviews| Index: base/trace_event/process_memory_dump_unittest.cc |
| diff --git a/base/trace_event/process_memory_dump_unittest.cc b/base/trace_event/process_memory_dump_unittest.cc |
| index baec8683f5fec1c1c080c1c8ef17eb7115814a65..45d8ecf181a9b4a88e75a80f5cf85e84c80dc526 100644 |
| --- a/base/trace_event/process_memory_dump_unittest.cc |
| +++ b/base/trace_event/process_memory_dump_unittest.cc |
| @@ -7,8 +7,10 @@ |
| #include <stddef.h> |
| #include "base/memory/aligned_memory.h" |
| +#include "base/memory/ptr_util.h" |
| #include "base/process/process_metrics.h" |
| #include "base/trace_event/memory_allocator_dump_guid.h" |
| +#include "base/trace_event/memory_infra_background_whitelist.h" |
| #include "base/trace_event/trace_event_argument.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| @@ -16,14 +18,29 @@ namespace base { |
| namespace trace_event { |
| namespace { |
| + |
| +const MemoryDumpArgs kDetailedDumpArgs = {MemoryDumpLevelOfDetail::DETAILED}; |
| +const char* const kTestDumpNameWhitelist[] = {"WhitelistedTestName", nullptr}; |
|
Primiano Tucci (use gerrit)
2016/06/02 20:24:04
I think you should move this below, because you re
ssid
2016/06/03 01:59:46
Hm it is bad i will be setting a global g_whitelis
|
| + |
| TracedValue* GetHeapDump(const ProcessMemoryDump& pmd, const char* name) { |
| auto it = pmd.heap_dumps().find(name); |
| return it == pmd.heap_dumps().end() ? nullptr : it->second.get(); |
| } |
| + |
| } // namespace |
| -TEST(ProcessMemoryDumpTest, Clear) { |
| - std::unique_ptr<ProcessMemoryDump> pmd1(new ProcessMemoryDump(nullptr)); |
| +class ProcessMemoryDumpTest : public testing::Test { |
| + protected: |
| + MemoryAllocatorDump* CreateAllocatorDumpInPMD(ProcessMemoryDump* pmd, |
| + const char* name) { |
| + return pmd->AddAllocatorDumpInternal(WrapUnique( |
| + new MemoryAllocatorDump(name, pmd, MemoryAllocatorDumpGuid()))); |
| + } |
| +}; |
| + |
| +TEST_F(ProcessMemoryDumpTest, Clear) { |
| + std::unique_ptr<ProcessMemoryDump> pmd1( |
| + new ProcessMemoryDump(nullptr, kDetailedDumpArgs)); |
| pmd1->CreateAllocatorDump("mad1"); |
| pmd1->CreateAllocatorDump("mad2"); |
| ASSERT_FALSE(pmd1->allocator_dumps().empty()); |
| @@ -78,14 +95,14 @@ TEST(ProcessMemoryDumpTest, Clear) { |
| pmd1.reset(); |
| } |
| -TEST(ProcessMemoryDumpTest, TakeAllDumpsFrom) { |
| +TEST_F(ProcessMemoryDumpTest, TakeAllDumpsFrom) { |
| std::unique_ptr<TracedValue> traced_value(new TracedValue); |
| hash_map<AllocationContext, AllocationMetrics> metrics_by_context; |
| metrics_by_context[AllocationContext()] = { 1, 1 }; |
| TraceEventMemoryOverhead overhead; |
| std::unique_ptr<ProcessMemoryDump> pmd1( |
| - new ProcessMemoryDump(new MemoryDumpSessionState())); |
| + new ProcessMemoryDump(new MemoryDumpSessionState(), kDetailedDumpArgs)); |
| auto mad1_1 = pmd1->CreateAllocatorDump("pmd1/mad1"); |
| auto mad1_2 = pmd1->CreateAllocatorDump("pmd1/mad2"); |
| pmd1->AddOwnershipEdge(mad1_1->guid(), mad1_2->guid()); |
| @@ -93,7 +110,7 @@ TEST(ProcessMemoryDumpTest, TakeAllDumpsFrom) { |
| pmd1->DumpHeapUsage(metrics_by_context, overhead, "pmd1/heap_dump2"); |
| std::unique_ptr<ProcessMemoryDump> pmd2( |
| - new ProcessMemoryDump(new MemoryDumpSessionState())); |
| + new ProcessMemoryDump(new MemoryDumpSessionState(), kDetailedDumpArgs)); |
| auto mad2_1 = pmd2->CreateAllocatorDump("pmd2/mad1"); |
| auto mad2_2 = pmd2->CreateAllocatorDump("pmd2/mad2"); |
| pmd2->AddOwnershipEdge(mad2_1->guid(), mad2_2->guid()); |
| @@ -148,8 +165,9 @@ TEST(ProcessMemoryDumpTest, TakeAllDumpsFrom) { |
| pmd1.reset(); |
| } |
| -TEST(ProcessMemoryDumpTest, Suballocations) { |
| - std::unique_ptr<ProcessMemoryDump> pmd(new ProcessMemoryDump(nullptr)); |
| +TEST_F(ProcessMemoryDumpTest, Suballocations) { |
| + std::unique_ptr<ProcessMemoryDump> pmd( |
| + new ProcessMemoryDump(nullptr, kDetailedDumpArgs)); |
| const std::string allocator_dump_name = "fakealloc/allocated_objects"; |
| pmd->CreateAllocatorDump(allocator_dump_name); |
| @@ -192,8 +210,9 @@ TEST(ProcessMemoryDumpTest, Suballocations) { |
| pmd.reset(); |
| } |
| -TEST(ProcessMemoryDumpTest, GlobalAllocatorDumpTest) { |
| - std::unique_ptr<ProcessMemoryDump> pmd(new ProcessMemoryDump(nullptr)); |
| +TEST_F(ProcessMemoryDumpTest, GlobalAllocatorDumpTest) { |
| + std::unique_ptr<ProcessMemoryDump> pmd( |
| + new ProcessMemoryDump(nullptr, kDetailedDumpArgs)); |
| MemoryAllocatorDumpGuid shared_mad_guid(1); |
| auto shared_mad1 = pmd->CreateWeakSharedGlobalAllocatorDump(shared_mad_guid); |
| ASSERT_EQ(shared_mad_guid, shared_mad1->guid()); |
| @@ -216,8 +235,59 @@ TEST(ProcessMemoryDumpTest, GlobalAllocatorDumpTest) { |
| ASSERT_EQ(MemoryAllocatorDump::Flags::DEFAULT, shared_mad1->flags()); |
| } |
| +TEST_F(ProcessMemoryDumpTest, BackgroundModeTest) { |
| + MemoryDumpArgs background_args = {MemoryDumpLevelOfDetail::BACKGROUND}; |
| + std::unique_ptr<ProcessMemoryDump> pmd( |
| + new ProcessMemoryDump(nullptr, background_args)); |
| + SetAllocatorDumpNameWhitelistForTesting(kTestDumpNameWhitelist); |
| + MemoryAllocatorDump* black_hole_mad = pmd->black_hole_mad_.get(); |
| + |
| + // Invalid dump names. |
| + EXPECT_EQ(black_hole_mad, |
| + CreateAllocatorDumpInPMD(pmd.get(), "NotWhitelistedTestName")); |
|
Primiano Tucci (use gerrit)
2016/06/02 20:24:04
Isn't this going to hit all those DCHECKs? I guess
ssid
2016/06/03 01:59:46
No, I had to make the test class and call AddInter
|
| + EXPECT_EQ(black_hole_mad, |
| + CreateAllocatorDumpInPMD(pmd.get(), "NotWhitelistedTestName")); |
|
Primiano Tucci (use gerrit)
2016/06/02 20:24:04
I think this is identical to the above?
ssid
2016/06/03 01:59:46
fixed.
|
| + EXPECT_EQ(black_hole_mad, CreateAllocatorDumpInPMD(pmd.get(), "TestName")); |
| + EXPECT_EQ(black_hole_mad, |
| + CreateAllocatorDumpInPMD(pmd.get(), "Whitelisted/Test")); |
| + EXPECT_EQ(black_hole_mad, |
| + CreateAllocatorDumpInPMD(pmd.get(), "Not/Whitelisted/TestName")); |
| + EXPECT_EQ(black_hole_mad, |
| + CreateAllocatorDumpInPMD(pmd.get(), "Whitelisted/TestName/Google")); |
| + EXPECT_EQ(black_hole_mad, CreateAllocatorDumpInPMD( |
| + pmd.get(), "Whitelisted/TestName/0x1a2Google")); |
| + EXPECT_EQ(black_hole_mad, CreateAllocatorDumpInPMD( |
| + pmd.get(), "Whitelisted/TestName/__12/Google")); |
| + |
| + // Global dumps. |
| + MemoryAllocatorDumpGuid guid(1); |
| + EXPECT_EQ(black_hole_mad, pmd->CreateSharedGlobalAllocatorDump(guid)); |
| + EXPECT_EQ(black_hole_mad, pmd->CreateWeakSharedGlobalAllocatorDump(guid)); |
| + EXPECT_EQ(black_hole_mad, pmd->GetSharedGlobalAllocatorDump(guid)); |
| + |
| + // Suballocations. |
| + pmd->AddSuballocation(guid, "malloc/allocated_objects"); |
| + EXPECT_EQ(0u, pmd->allocator_dumps_edges_.size()); |
| + EXPECT_EQ(0u, pmd->allocator_dumps_.size()); |
| + |
| + // Valid dump names. |
| + EXPECT_NE(black_hole_mad, |
| + CreateAllocatorDumpInPMD(pmd.get(), "WhitelistedTestName")); |
| + EXPECT_NE(black_hole_mad, |
| + CreateAllocatorDumpInPMD(pmd.get(), "Whitelisted/TestName/0xA1b2")); |
| + EXPECT_NE(black_hole_mad, CreateAllocatorDumpInPMD( |
| + pmd.get(), "Whitelisted/TestName_123/0xab")); |
| + EXPECT_NE(black_hole_mad, CreateAllocatorDumpInPMD( |
| + pmd.get(), "Whitelisted_12/0xaB/TestName")); |
| + |
| + // GetAllocatorDump is consistent. |
| + EXPECT_EQ(black_hole_mad, pmd->GetAllocatorDump("NotWhitelistedTestName")); |
| + auto mad = pmd->GetAllocatorDump("WhitelistedTestName"); |
| + EXPECT_NE(black_hole_mad, mad); |
| +} |
| + |
| #if defined(COUNT_RESIDENT_BYTES_SUPPORTED) |
| -TEST(ProcessMemoryDumpTest, CountResidentBytes) { |
| +TEST_F(ProcessMemoryDumpTest, CountResidentBytes) { |
| const size_t page_size = ProcessMemoryDump::GetSystemPageSize(); |
| // Allocate few page of dirty memory and check if it is resident. |