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 <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include "base/memory/aligned_memory.h" | 9 #include "base/memory/aligned_memory.h" |
10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| 11 #include "base/memory/shared_memory_tracker.h" |
11 #include "base/process/process_metrics.h" | 12 #include "base/process/process_metrics.h" |
12 #include "base/trace_event/memory_allocator_dump_guid.h" | 13 #include "base/trace_event/memory_allocator_dump_guid.h" |
13 #include "base/trace_event/memory_infra_background_whitelist.h" | 14 #include "base/trace_event/memory_infra_background_whitelist.h" |
14 #include "base/trace_event/trace_event_argument.h" | 15 #include "base/trace_event/trace_event_argument.h" |
| 16 #include "base/unguessable_token.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
16 | 18 |
17 namespace base { | 19 namespace base { |
18 namespace trace_event { | 20 namespace trace_event { |
19 | 21 |
20 namespace { | 22 namespace { |
21 | 23 |
22 const MemoryDumpArgs kDetailedDumpArgs = {MemoryDumpLevelOfDetail::DETAILED}; | 24 const MemoryDumpArgs kDetailedDumpArgs = {MemoryDumpLevelOfDetail::DETAILED}; |
23 const char* const kTestDumpNameWhitelist[] = { | 25 const char* const kTestDumpNameWhitelist[] = { |
24 "Whitelisted/TestName", "Whitelisted/TestName_0x?", | 26 "Whitelisted/TestName", "Whitelisted/TestName_0x?", |
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
293 | 295 |
294 auto* shared_mad4 = pmd->CreateSharedGlobalAllocatorDump(shared_mad_guid); | 296 auto* shared_mad4 = pmd->CreateSharedGlobalAllocatorDump(shared_mad_guid); |
295 ASSERT_EQ(shared_mad1, shared_mad4); | 297 ASSERT_EQ(shared_mad1, shared_mad4); |
296 ASSERT_EQ(MemoryAllocatorDump::Flags::DEFAULT, shared_mad1->flags()); | 298 ASSERT_EQ(MemoryAllocatorDump::Flags::DEFAULT, shared_mad1->flags()); |
297 | 299 |
298 auto* shared_mad5 = pmd->CreateWeakSharedGlobalAllocatorDump(shared_mad_guid); | 300 auto* shared_mad5 = pmd->CreateWeakSharedGlobalAllocatorDump(shared_mad_guid); |
299 ASSERT_EQ(shared_mad1, shared_mad5); | 301 ASSERT_EQ(shared_mad1, shared_mad5); |
300 ASSERT_EQ(MemoryAllocatorDump::Flags::DEFAULT, shared_mad1->flags()); | 302 ASSERT_EQ(MemoryAllocatorDump::Flags::DEFAULT, shared_mad1->flags()); |
301 } | 303 } |
302 | 304 |
| 305 TEST(ProcessMemoryDumpTest, OldSharedMemoryOwnershipTest) { |
| 306 std::unique_ptr<ProcessMemoryDump> pmd( |
| 307 new ProcessMemoryDump(nullptr, kDetailedDumpArgs)); |
| 308 const ProcessMemoryDump::AllocatorDumpEdgesMap& edges = |
| 309 pmd->allocator_dumps_edges_for_testing(); |
| 310 |
| 311 auto* shm_dump1 = pmd->CreateAllocatorDump("shared_mem/seg1"); |
| 312 |
| 313 auto* client_dump1 = pmd->CreateAllocatorDump("discardable/segment1"); |
| 314 MemoryAllocatorDumpGuid client_global_guid1(1); |
| 315 auto shm_token1 = UnguessableToken::Create(); |
| 316 MemoryAllocatorDumpGuid shm_global_guid1 = |
| 317 SharedMemoryTracker::GetGlobalDumpGUIDForTracing(shm_token1); |
| 318 pmd->AddOverridableOwnershipEdge(shm_dump1->guid(), shm_global_guid1, |
| 319 0 /* importance */); |
| 320 |
| 321 pmd->CreateSharedMemoryOwnershipEdge(client_dump1->guid(), |
| 322 client_global_guid1, shm_token1, |
| 323 1 /* importance */); |
| 324 |
| 325 EXPECT_EQ(2u, edges.size()); |
| 326 EXPECT_EQ(shm_global_guid1, edges.find(shm_dump1->guid())->second.target); |
| 327 EXPECT_EQ(0, edges.find(shm_dump1->guid())->second.importance); |
| 328 EXPECT_TRUE(edges.find(shm_dump1->guid())->second.overridable); |
| 329 EXPECT_EQ(client_global_guid1, |
| 330 edges.find(client_dump1->guid())->second.target); |
| 331 EXPECT_EQ(1, edges.find(client_dump1->guid())->second.importance); |
| 332 EXPECT_FALSE(edges.find(client_dump1->guid())->second.overridable); |
| 333 } |
| 334 |
| 335 TEST(ProcessMemoryDumpTest, NewSharedMemoryOwnershipTest) { |
| 336 std::unique_ptr<ProcessMemoryDump> pmd( |
| 337 new ProcessMemoryDump(nullptr, kDetailedDumpArgs)); |
| 338 const ProcessMemoryDump::AllocatorDumpEdgesMap& edges = |
| 339 pmd->allocator_dumps_edges_for_testing(); |
| 340 MemoryAllocatorDumpGuid::SetUseSharedMemoryBasedGUIDsForTesting(); |
| 341 |
| 342 auto* client_dump2 = pmd->CreateAllocatorDump("discardable/segment2"); |
| 343 MemoryAllocatorDumpGuid client_global_guid2(2); |
| 344 auto shm_token2 = UnguessableToken::Create(); |
| 345 MemoryAllocatorDumpGuid shm_local_guid2 = |
| 346 SharedMemoryTracker::GetDumpGUIDForTracing(shm_token2); |
| 347 MemoryAllocatorDumpGuid shm_global_guid2 = |
| 348 SharedMemoryTracker::GetGlobalDumpGUIDForTracing(shm_token2); |
| 349 pmd->AddOverridableOwnershipEdge(shm_local_guid2, shm_global_guid2, |
| 350 0 /* importance */); |
| 351 |
| 352 pmd->CreateSharedMemoryOwnershipEdge(client_dump2->guid(), |
| 353 client_global_guid2, shm_token2, |
| 354 1 /* importance */); |
| 355 EXPECT_EQ(2u, edges.size()); |
| 356 |
| 357 EXPECT_EQ(shm_global_guid2, edges.find(shm_local_guid2)->second.target); |
| 358 EXPECT_EQ(1, edges.find(shm_local_guid2)->second.importance); |
| 359 EXPECT_FALSE(edges.find(shm_local_guid2)->second.overridable); |
| 360 EXPECT_EQ(shm_local_guid2, edges.find(client_dump2->guid())->second.target); |
| 361 EXPECT_EQ(0, edges.find(client_dump2->guid())->second.importance); |
| 362 EXPECT_FALSE(edges.find(client_dump2->guid())->second.overridable); |
| 363 } |
| 364 |
303 TEST(ProcessMemoryDumpTest, BackgroundModeTest) { | 365 TEST(ProcessMemoryDumpTest, BackgroundModeTest) { |
304 MemoryDumpArgs background_args = {MemoryDumpLevelOfDetail::BACKGROUND}; | 366 MemoryDumpArgs background_args = {MemoryDumpLevelOfDetail::BACKGROUND}; |
305 std::unique_ptr<ProcessMemoryDump> pmd( | 367 std::unique_ptr<ProcessMemoryDump> pmd( |
306 new ProcessMemoryDump(nullptr, background_args)); | 368 new ProcessMemoryDump(nullptr, background_args)); |
307 ProcessMemoryDump::is_black_hole_non_fatal_for_testing_ = true; | 369 ProcessMemoryDump::is_black_hole_non_fatal_for_testing_ = true; |
308 SetAllocatorDumpNameWhitelistForTesting(kTestDumpNameWhitelist); | 370 SetAllocatorDumpNameWhitelistForTesting(kTestDumpNameWhitelist); |
309 MemoryAllocatorDump* black_hole_mad = pmd->GetBlackHoleMad(); | 371 MemoryAllocatorDump* black_hole_mad = pmd->GetBlackHoleMad(); |
310 | 372 |
311 // Invalid dump names. | 373 // Invalid dump names. |
312 EXPECT_EQ(black_hole_mad, | 374 EXPECT_EQ(black_hole_mad, |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
363 static_cast<char*>(base::AlignedAlloc(kVeryLargeMemorySize, page_size))); | 425 static_cast<char*>(base::AlignedAlloc(kVeryLargeMemorySize, page_size))); |
364 memset(memory2.get(), 0, kVeryLargeMemorySize); | 426 memset(memory2.get(), 0, kVeryLargeMemorySize); |
365 size_t res2 = ProcessMemoryDump::CountResidentBytes(memory2.get(), | 427 size_t res2 = ProcessMemoryDump::CountResidentBytes(memory2.get(), |
366 kVeryLargeMemorySize); | 428 kVeryLargeMemorySize); |
367 ASSERT_EQ(res2, kVeryLargeMemorySize); | 429 ASSERT_EQ(res2, kVeryLargeMemorySize); |
368 } | 430 } |
369 #endif // defined(COUNT_RESIDENT_BYTES_SUPPORTED) | 431 #endif // defined(COUNT_RESIDENT_BYTES_SUPPORTED) |
370 | 432 |
371 } // namespace trace_event | 433 } // namespace trace_event |
372 } // namespace base | 434 } // namespace base |
OLD | NEW |