1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
|
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/trace_event/process_memory_dump.h"
#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"
namespace base {
namespace trace_event {
namespace {
const MemoryDumpArgs kDetailedDumpArgs = {MemoryDumpLevelOfDetail::DETAILED};
const char* const kTestDumpNameWhitelist[] = {
"Whitelisted/TestName", "Whitelisted/TestName_0x?",
"Whitelisted/0x?/TestName", nullptr};
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, kDetailedDumpArgs));
pmd1->CreateAllocatorDump("mad1");
pmd1->CreateAllocatorDump("mad2");
ASSERT_FALSE(pmd1->allocator_dumps().empty());
pmd1->process_totals()->set_resident_set_bytes(42);
pmd1->set_has_process_totals();
pmd1->process_mmaps()->AddVMRegion(ProcessMemoryMaps::VMRegion());
pmd1->set_has_process_mmaps();
pmd1->AddOwnershipEdge(MemoryAllocatorDumpGuid(42),
MemoryAllocatorDumpGuid(4242));
MemoryAllocatorDumpGuid shared_mad_guid1(1);
MemoryAllocatorDumpGuid shared_mad_guid2(2);
pmd1->CreateSharedGlobalAllocatorDump(shared_mad_guid1);
pmd1->CreateSharedGlobalAllocatorDump(shared_mad_guid2);
pmd1->Clear();
ASSERT_TRUE(pmd1->allocator_dumps().empty());
ASSERT_TRUE(pmd1->allocator_dumps_edges().empty());
ASSERT_EQ(nullptr, pmd1->GetAllocatorDump("mad1"));
ASSERT_EQ(nullptr, pmd1->GetAllocatorDump("mad2"));
ASSERT_FALSE(pmd1->has_process_totals());
ASSERT_FALSE(pmd1->has_process_mmaps());
ASSERT_TRUE(pmd1->process_mmaps()->vm_regions().empty());
ASSERT_EQ(nullptr, pmd1->GetSharedGlobalAllocatorDump(shared_mad_guid1));
ASSERT_EQ(nullptr, pmd1->GetSharedGlobalAllocatorDump(shared_mad_guid2));
// Check that calling AsValueInto() doesn't cause a crash.
std::unique_ptr<TracedValue> traced_value(new TracedValue);
pmd1->AsValueInto(traced_value.get());
// Check that the pmd can be reused and behaves as expected.
auto* mad1 = pmd1->CreateAllocatorDump("mad1");
auto* mad3 = pmd1->CreateAllocatorDump("mad3");
auto* shared_mad1 = pmd1->CreateSharedGlobalAllocatorDump(shared_mad_guid1);
auto* shared_mad2 =
pmd1->CreateWeakSharedGlobalAllocatorDump(shared_mad_guid2);
ASSERT_EQ(4u, pmd1->allocator_dumps().size());
ASSERT_EQ(mad1, pmd1->GetAllocatorDump("mad1"));
ASSERT_EQ(nullptr, pmd1->GetAllocatorDump("mad2"));
ASSERT_EQ(mad3, pmd1->GetAllocatorDump("mad3"));
ASSERT_EQ(shared_mad1, pmd1->GetSharedGlobalAllocatorDump(shared_mad_guid1));
ASSERT_EQ(MemoryAllocatorDump::Flags::DEFAULT, shared_mad1->flags());
ASSERT_EQ(shared_mad2, pmd1->GetSharedGlobalAllocatorDump(shared_mad_guid2));
ASSERT_EQ(MemoryAllocatorDump::Flags::WEAK, shared_mad2->flags());
traced_value.reset(new TracedValue);
pmd1->AsValueInto(traced_value.get());
pmd1.reset();
}
TEST(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;
scoped_refptr<MemoryDumpSessionState> session_state =
new MemoryDumpSessionState;
session_state->SetStackFrameDeduplicator(
WrapUnique(new StackFrameDeduplicator));
session_state->SetTypeNameDeduplicator(
WrapUnique(new TypeNameDeduplicator));
std::unique_ptr<ProcessMemoryDump> pmd1(
new ProcessMemoryDump(session_state.get(), kDetailedDumpArgs));
auto* mad1_1 = pmd1->CreateAllocatorDump("pmd1/mad1");
auto* mad1_2 = pmd1->CreateAllocatorDump("pmd1/mad2");
pmd1->AddOwnershipEdge(mad1_1->guid(), mad1_2->guid());
pmd1->DumpHeapUsage(metrics_by_context, overhead, "pmd1/heap_dump1");
pmd1->DumpHeapUsage(metrics_by_context, overhead, "pmd1/heap_dump2");
std::unique_ptr<ProcessMemoryDump> pmd2(
new ProcessMemoryDump(session_state.get(), kDetailedDumpArgs));
auto* mad2_1 = pmd2->CreateAllocatorDump("pmd2/mad1");
auto* mad2_2 = pmd2->CreateAllocatorDump("pmd2/mad2");
pmd2->AddOwnershipEdge(mad2_1->guid(), mad2_2->guid());
pmd2->DumpHeapUsage(metrics_by_context, overhead, "pmd2/heap_dump1");
pmd2->DumpHeapUsage(metrics_by_context, overhead, "pmd2/heap_dump2");
MemoryAllocatorDumpGuid shared_mad_guid1(1);
MemoryAllocatorDumpGuid shared_mad_guid2(2);
auto* shared_mad1 = pmd2->CreateSharedGlobalAllocatorDump(shared_mad_guid1);
auto* shared_mad2 =
pmd2->CreateWeakSharedGlobalAllocatorDump(shared_mad_guid2);
pmd1->TakeAllDumpsFrom(pmd2.get());
// Make sure that pmd2 is empty but still usable after it has been emptied.
ASSERT_TRUE(pmd2->allocator_dumps().empty());
ASSERT_TRUE(pmd2->allocator_dumps_edges().empty());
ASSERT_TRUE(pmd2->heap_dumps().empty());
pmd2->CreateAllocatorDump("pmd2/this_mad_stays_with_pmd2");
ASSERT_EQ(1u, pmd2->allocator_dumps().size());
ASSERT_EQ(1u, pmd2->allocator_dumps().count("pmd2/this_mad_stays_with_pmd2"));
pmd2->AddOwnershipEdge(MemoryAllocatorDumpGuid(42),
MemoryAllocatorDumpGuid(4242));
// Check that calling AsValueInto() doesn't cause a crash.
pmd2->AsValueInto(traced_value.get());
// Free the |pmd2| to check that the memory ownership of the two MAD(s)
// has been transferred to |pmd1|.
pmd2.reset();
// Now check that |pmd1| has been effectively merged.
ASSERT_EQ(6u, pmd1->allocator_dumps().size());
ASSERT_EQ(1u, pmd1->allocator_dumps().count("pmd1/mad1"));
ASSERT_EQ(1u, pmd1->allocator_dumps().count("pmd1/mad2"));
ASSERT_EQ(1u, pmd1->allocator_dumps().count("pmd2/mad1"));
ASSERT_EQ(1u, pmd1->allocator_dumps().count("pmd1/mad2"));
ASSERT_EQ(2u, pmd1->allocator_dumps_edges().size());
ASSERT_EQ(shared_mad1, pmd1->GetSharedGlobalAllocatorDump(shared_mad_guid1));
ASSERT_EQ(shared_mad2, pmd1->GetSharedGlobalAllocatorDump(shared_mad_guid2));
ASSERT_TRUE(MemoryAllocatorDump::Flags::WEAK & shared_mad2->flags());
ASSERT_EQ(4u, pmd1->heap_dumps().size());
ASSERT_TRUE(GetHeapDump(*pmd1, "pmd1/heap_dump1") != nullptr);
ASSERT_TRUE(GetHeapDump(*pmd1, "pmd1/heap_dump2") != nullptr);
ASSERT_TRUE(GetHeapDump(*pmd1, "pmd2/heap_dump1") != nullptr);
ASSERT_TRUE(GetHeapDump(*pmd1, "pmd2/heap_dump2") != nullptr);
// Check that calling AsValueInto() doesn't cause a crash.
traced_value.reset(new TracedValue);
pmd1->AsValueInto(traced_value.get());
pmd1.reset();
}
TEST(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);
// Create one allocation with an auto-assigned guid and mark it as a
// suballocation of "fakealloc/allocated_objects".
auto* pic1_dump = pmd->CreateAllocatorDump("picturemanager/picture1");
pmd->AddSuballocation(pic1_dump->guid(), allocator_dump_name);
// Same here, but this time create an allocation with an explicit guid.
auto* pic2_dump = pmd->CreateAllocatorDump("picturemanager/picture2",
MemoryAllocatorDumpGuid(0x42));
pmd->AddSuballocation(pic2_dump->guid(), allocator_dump_name);
// Now check that AddSuballocation() has created anonymous child dumps under
// "fakealloc/allocated_objects".
auto anon_node_1_it = pmd->allocator_dumps().find(
allocator_dump_name + "/__" + pic1_dump->guid().ToString());
ASSERT_NE(pmd->allocator_dumps().end(), anon_node_1_it);
auto anon_node_2_it =
pmd->allocator_dumps().find(allocator_dump_name + "/__42");
ASSERT_NE(pmd->allocator_dumps().end(), anon_node_2_it);
// Finally check that AddSuballocation() has created also the
// edges between the pictures and the anonymous allocator child dumps.
bool found_edge[2]{false, false};
for (const auto& e : pmd->allocator_dumps_edges()) {
found_edge[0] |= (e.source == pic1_dump->guid() &&
e.target == anon_node_1_it->second->guid());
found_edge[1] |= (e.source == pic2_dump->guid() &&
e.target == anon_node_2_it->second->guid());
}
ASSERT_TRUE(found_edge[0]);
ASSERT_TRUE(found_edge[1]);
// Check that calling AsValueInto() doesn't cause a crash.
std::unique_ptr<TracedValue> traced_value(new TracedValue);
pmd->AsValueInto(traced_value.get());
pmd.reset();
}
TEST(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());
ASSERT_EQ(MemoryAllocatorDump::Flags::WEAK, shared_mad1->flags());
auto* shared_mad2 = pmd->GetSharedGlobalAllocatorDump(shared_mad_guid);
ASSERT_EQ(shared_mad1, shared_mad2);
ASSERT_EQ(MemoryAllocatorDump::Flags::WEAK, shared_mad1->flags());
auto* shared_mad3 = pmd->CreateWeakSharedGlobalAllocatorDump(shared_mad_guid);
ASSERT_EQ(shared_mad1, shared_mad3);
ASSERT_EQ(MemoryAllocatorDump::Flags::WEAK, shared_mad1->flags());
auto* shared_mad4 = pmd->CreateSharedGlobalAllocatorDump(shared_mad_guid);
ASSERT_EQ(shared_mad1, shared_mad4);
ASSERT_EQ(MemoryAllocatorDump::Flags::DEFAULT, shared_mad1->flags());
auto* shared_mad5 = pmd->CreateWeakSharedGlobalAllocatorDump(shared_mad_guid);
ASSERT_EQ(shared_mad1, shared_mad5);
ASSERT_EQ(MemoryAllocatorDump::Flags::DEFAULT, shared_mad1->flags());
}
TEST(ProcessMemoryDumpTest, BackgroundModeTest) {
MemoryDumpArgs background_args = {MemoryDumpLevelOfDetail::BACKGROUND};
std::unique_ptr<ProcessMemoryDump> pmd(
new ProcessMemoryDump(nullptr, background_args));
ProcessMemoryDump::is_black_hole_non_fatal_for_testing_ = true;
SetAllocatorDumpNameWhitelistForTesting(kTestDumpNameWhitelist);
MemoryAllocatorDump* black_hole_mad = pmd->GetBlackHoleMad();
// Invalid dump names.
EXPECT_EQ(black_hole_mad,
pmd->CreateAllocatorDump("NotWhitelisted/TestName"));
EXPECT_EQ(black_hole_mad, pmd->CreateAllocatorDump("TestName"));
EXPECT_EQ(black_hole_mad, pmd->CreateAllocatorDump("Whitelisted/Test"));
EXPECT_EQ(black_hole_mad,
pmd->CreateAllocatorDump("Not/Whitelisted/TestName"));
EXPECT_EQ(black_hole_mad,
pmd->CreateAllocatorDump("Whitelisted/TestName/Google"));
EXPECT_EQ(black_hole_mad,
pmd->CreateAllocatorDump("Whitelisted/TestName/0x1a2Google"));
EXPECT_EQ(black_hole_mad,
pmd->CreateAllocatorDump("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, pmd->CreateAllocatorDump("Whitelisted/TestName"));
EXPECT_NE(black_hole_mad,
pmd->CreateAllocatorDump("Whitelisted/TestName_0xA1b2"));
EXPECT_NE(black_hole_mad,
pmd->CreateAllocatorDump("Whitelisted/0xaB/TestName"));
// GetAllocatorDump is consistent.
EXPECT_EQ(black_hole_mad, pmd->GetAllocatorDump("NotWhitelisted/TestName"));
EXPECT_NE(black_hole_mad, pmd->GetAllocatorDump("Whitelisted/TestName"));
}
#if defined(COUNT_RESIDENT_BYTES_SUPPORTED)
TEST(ProcessMemoryDumpTest, CountResidentBytes) {
const size_t page_size = ProcessMemoryDump::GetSystemPageSize();
// Allocate few page of dirty memory and check if it is resident.
const size_t size1 = 5 * page_size;
std::unique_ptr<char, base::AlignedFreeDeleter> memory1(
static_cast<char*>(base::AlignedAlloc(size1, page_size)));
memset(memory1.get(), 0, size1);
size_t res1 = ProcessMemoryDump::CountResidentBytes(memory1.get(), size1);
ASSERT_EQ(res1, size1);
// Allocate a large memory segment (> 8Mib).
const size_t kVeryLargeMemorySize = 15 * 1024 * 1024;
std::unique_ptr<char, base::AlignedFreeDeleter> memory2(
static_cast<char*>(base::AlignedAlloc(kVeryLargeMemorySize, page_size)));
memset(memory2.get(), 0, kVeryLargeMemorySize);
size_t res2 = ProcessMemoryDump::CountResidentBytes(memory2.get(),
kVeryLargeMemorySize);
ASSERT_EQ(res2, kVeryLargeMemorySize);
}
#endif // defined(COUNT_RESIDENT_BYTES_SUPPORTED)
} // namespace trace_event
} // namespace base
|