| 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 <utility> | 10 #include <utility> |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 class MockMemoryDumpProvider : public MemoryDumpProvider { | 150 class MockMemoryDumpProvider : public MemoryDumpProvider { |
| 151 public: | 151 public: |
| 152 MOCK_METHOD0(Destructor, void()); | 152 MOCK_METHOD0(Destructor, void()); |
| 153 MOCK_METHOD2(OnMemoryDump, | 153 MOCK_METHOD2(OnMemoryDump, |
| 154 bool(const MemoryDumpArgs& args, ProcessMemoryDump* pmd)); | 154 bool(const MemoryDumpArgs& args, ProcessMemoryDump* pmd)); |
| 155 MOCK_METHOD1(PollFastMemoryTotal, void(uint64_t* memory_total)); | 155 MOCK_METHOD1(PollFastMemoryTotal, void(uint64_t* memory_total)); |
| 156 MOCK_METHOD0(SuspendFastMemoryPolling, void()); | 156 MOCK_METHOD0(SuspendFastMemoryPolling, void()); |
| 157 | 157 |
| 158 MockMemoryDumpProvider() : enable_mock_destructor(false) { | 158 MockMemoryDumpProvider() : enable_mock_destructor(false) { |
| 159 ON_CALL(*this, OnMemoryDump(_, _)) | 159 ON_CALL(*this, OnMemoryDump(_, _)) |
| 160 .WillByDefault(Invoke([](const MemoryDumpArgs&, | 160 .WillByDefault( |
| 161 ProcessMemoryDump* pmd) -> bool { | 161 Invoke([](const MemoryDumpArgs&, ProcessMemoryDump* pmd) -> bool { |
| 162 // |session_state| should not be null under any circumstances when | 162 // |heap_profiler_serialization_state| should not be null under |
| 163 // invoking a memory dump. The problem might arise in race conditions | 163 // any circumstances when invoking a memory dump. The problem |
| 164 // like crbug.com/600570 . | 164 // might arise in race conditions like crbug.com/600570 . |
| 165 EXPECT_TRUE(pmd->session_state().get() != nullptr); | 165 EXPECT_TRUE(pmd->heap_profiler_serialization_state().get() != |
| 166 return true; | 166 nullptr); |
| 167 })); | 167 return true; |
| 168 })); |
| 168 | 169 |
| 169 ON_CALL(*this, PollFastMemoryTotal(_)) | 170 ON_CALL(*this, PollFastMemoryTotal(_)) |
| 170 .WillByDefault( | 171 .WillByDefault( |
| 171 Invoke([](uint64_t* memory_total) -> void { NOTREACHED(); })); | 172 Invoke([](uint64_t* memory_total) -> void { NOTREACHED(); })); |
| 172 } | 173 } |
| 173 ~MockMemoryDumpProvider() override { | 174 ~MockMemoryDumpProvider() override { |
| 174 if (enable_mock_destructor) | 175 if (enable_mock_destructor) |
| 175 Destructor(); | 176 Destructor(); |
| 176 } | 177 } |
| 177 | 178 |
| (...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 400 RegisterDumpProvider(&mdp, ThreadTaskRunnerHandle::Get()); | 401 RegisterDumpProvider(&mdp, ThreadTaskRunnerHandle::Get()); |
| 401 EnableTracingWithLegacyCategories(MemoryDumpManager::kTraceCategory); | 402 EnableTracingWithLegacyCategories(MemoryDumpManager::kTraceCategory); |
| 402 EXPECT_CALL(global_dump_handler_, RequestGlobalMemoryDump(_, _)).Times(1); | 403 EXPECT_CALL(global_dump_handler_, RequestGlobalMemoryDump(_, _)).Times(1); |
| 403 EXPECT_CALL(mdp, OnMemoryDump(IsLightDump(), _)).WillOnce(Return(true)); | 404 EXPECT_CALL(mdp, OnMemoryDump(IsLightDump(), _)).WillOnce(Return(true)); |
| 404 RequestGlobalDumpAndWait(MemoryDumpType::EXPLICITLY_TRIGGERED, | 405 RequestGlobalDumpAndWait(MemoryDumpType::EXPLICITLY_TRIGGERED, |
| 405 MemoryDumpLevelOfDetail::LIGHT); | 406 MemoryDumpLevelOfDetail::LIGHT); |
| 406 DisableTracing(); | 407 DisableTracing(); |
| 407 mdm_->UnregisterDumpProvider(&mdp); | 408 mdm_->UnregisterDumpProvider(&mdp); |
| 408 } | 409 } |
| 409 | 410 |
| 410 // Checks that the SharedSessionState object is acqually shared over time. | 411 // Checks that the HeapProfilerSerializationState object is actually |
| 411 TEST_F(MemoryDumpManagerTest, SharedSessionState) { | 412 // shared over time. |
| 413 TEST_F(MemoryDumpManagerTest, HeapProfilerSerializationState) { |
| 412 InitializeMemoryDumpManager(false /* is_coordinator */); | 414 InitializeMemoryDumpManager(false /* is_coordinator */); |
| 413 MockMemoryDumpProvider mdp1; | 415 MockMemoryDumpProvider mdp1; |
| 414 MockMemoryDumpProvider mdp2; | 416 MockMemoryDumpProvider mdp2; |
| 415 RegisterDumpProvider(&mdp1, nullptr); | 417 RegisterDumpProvider(&mdp1, nullptr); |
| 416 RegisterDumpProvider(&mdp2, nullptr); | 418 RegisterDumpProvider(&mdp2, nullptr); |
| 417 | 419 |
| 418 EnableTracingWithLegacyCategories(MemoryDumpManager::kTraceCategory); | 420 EnableTracingWithLegacyCategories(MemoryDumpManager::kTraceCategory); |
| 419 const MemoryDumpSessionState* session_state = | 421 const HeapProfilerSerializationState* heap_profiler_serialization_state = |
| 420 mdm_->session_state_for_testing().get(); | 422 mdm_->heap_profiler_serialization_state_for_testing().get(); |
| 421 EXPECT_CALL(global_dump_handler_, RequestGlobalMemoryDump(_, _)).Times(2); | 423 EXPECT_CALL(global_dump_handler_, RequestGlobalMemoryDump(_, _)).Times(2); |
| 422 EXPECT_CALL(mdp1, OnMemoryDump(_, _)) | 424 EXPECT_CALL(mdp1, OnMemoryDump(_, _)) |
| 423 .Times(2) | 425 .Times(2) |
| 424 .WillRepeatedly(Invoke([session_state](const MemoryDumpArgs&, | 426 .WillRepeatedly( |
| 425 ProcessMemoryDump* pmd) -> bool { | 427 Invoke([heap_profiler_serialization_state]( |
| 426 EXPECT_EQ(session_state, pmd->session_state().get()); | 428 const MemoryDumpArgs&, ProcessMemoryDump* pmd) -> bool { |
| 427 return true; | 429 EXPECT_EQ(heap_profiler_serialization_state, |
| 428 })); | 430 pmd->heap_profiler_serialization_state().get()); |
| 431 return true; |
| 432 })); |
| 429 EXPECT_CALL(mdp2, OnMemoryDump(_, _)) | 433 EXPECT_CALL(mdp2, OnMemoryDump(_, _)) |
| 430 .Times(2) | 434 .Times(2) |
| 431 .WillRepeatedly(Invoke([session_state](const MemoryDumpArgs&, | 435 .WillRepeatedly( |
| 432 ProcessMemoryDump* pmd) -> bool { | 436 Invoke([heap_profiler_serialization_state]( |
| 433 EXPECT_EQ(session_state, pmd->session_state().get()); | 437 const MemoryDumpArgs&, ProcessMemoryDump* pmd) -> bool { |
| 434 return true; | 438 EXPECT_EQ(heap_profiler_serialization_state, |
| 435 })); | 439 pmd->heap_profiler_serialization_state().get()); |
| 440 return true; |
| 441 })); |
| 436 | 442 |
| 437 for (int i = 0; i < 2; ++i) { | 443 for (int i = 0; i < 2; ++i) { |
| 438 RequestGlobalDumpAndWait(MemoryDumpType::EXPLICITLY_TRIGGERED, | 444 RequestGlobalDumpAndWait(MemoryDumpType::EXPLICITLY_TRIGGERED, |
| 439 MemoryDumpLevelOfDetail::DETAILED); | 445 MemoryDumpLevelOfDetail::DETAILED); |
| 440 } | 446 } |
| 441 | 447 |
| 442 DisableTracing(); | 448 DisableTracing(); |
| 443 } | 449 } |
| 444 | 450 |
| 445 // Checks that the (Un)RegisterDumpProvider logic behaves sanely. | 451 // Checks that the (Un)RegisterDumpProvider logic behaves sanely. |
| (...skipping 797 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1243 EnableTracingWithLegacyCategories(MemoryDumpManager::kTraceCategory); | 1249 EnableTracingWithLegacyCategories(MemoryDumpManager::kTraceCategory); |
| 1244 EXPECT_FALSE(IsPeriodicDumpingEnabled()); | 1250 EXPECT_FALSE(IsPeriodicDumpingEnabled()); |
| 1245 RequestGlobalDumpAndWait(MemoryDumpType::EXPLICITLY_TRIGGERED, | 1251 RequestGlobalDumpAndWait(MemoryDumpType::EXPLICITLY_TRIGGERED, |
| 1246 MemoryDumpLevelOfDetail::BACKGROUND); | 1252 MemoryDumpLevelOfDetail::BACKGROUND); |
| 1247 DisableTracing(); | 1253 DisableTracing(); |
| 1248 } | 1254 } |
| 1249 | 1255 |
| 1250 TEST_F(MemoryDumpManagerTest, TestBackgroundTracingSetup) { | 1256 TEST_F(MemoryDumpManagerTest, TestBackgroundTracingSetup) { |
| 1251 InitializeMemoryDumpManager(true /* is_coordinator */); | 1257 InitializeMemoryDumpManager(true /* is_coordinator */); |
| 1252 | 1258 |
| 1259 // We now need an MDP to hit the code path where the dump will be rejected |
| 1260 // since this happens at the point you try to serialize a process dump. |
| 1261 MockMemoryDumpProvider mdp; |
| 1262 RegisterDumpProvider(&mdp, ThreadTaskRunnerHandle::Get()); |
| 1263 |
| 1253 RunLoop run_loop; | 1264 RunLoop run_loop; |
| 1254 auto test_task_runner = ThreadTaskRunnerHandle::Get(); | 1265 auto test_task_runner = ThreadTaskRunnerHandle::Get(); |
| 1255 auto quit_closure = run_loop.QuitClosure(); | 1266 auto quit_closure = run_loop.QuitClosure(); |
| 1256 | 1267 |
| 1257 testing::InSequence sequence; | 1268 testing::InSequence sequence; |
| 1258 EXPECT_CALL(global_dump_handler_, | 1269 EXPECT_CALL(global_dump_handler_, |
| 1259 RequestGlobalMemoryDump(IsBackgroundDump(), _)) | 1270 RequestGlobalMemoryDump(IsBackgroundDump(), _)) |
| 1260 .Times(5); | 1271 .Times(5); |
| 1261 EXPECT_CALL(global_dump_handler_, | 1272 EXPECT_CALL(global_dump_handler_, |
| 1262 RequestGlobalMemoryDump(IsBackgroundDump(), _)) | 1273 RequestGlobalMemoryDump(IsBackgroundDump(), _)) |
| 1263 .WillOnce(Invoke([test_task_runner, quit_closure]( | 1274 .WillOnce(Invoke([test_task_runner, quit_closure]( |
| 1264 const MemoryDumpRequestArgs& args, | 1275 const MemoryDumpRequestArgs& args, |
| 1265 const GlobalMemoryDumpCallback& callback) { | 1276 const GlobalMemoryDumpCallback& callback) { |
| 1266 test_task_runner->PostTask(FROM_HERE, quit_closure); | 1277 test_task_runner->PostTask(FROM_HERE, quit_closure); |
| 1267 })); | 1278 })); |
| 1268 EXPECT_CALL(global_dump_handler_, RequestGlobalMemoryDump(_, _)) | 1279 EXPECT_CALL(global_dump_handler_, RequestGlobalMemoryDump(_, _)) |
| 1269 .Times(AnyNumber()); | 1280 .Times(AnyNumber()); |
| 1270 | 1281 |
| 1271 EnableTracingWithTraceConfig( | 1282 EnableTracingWithTraceConfig( |
| 1272 TraceConfigMemoryTestUtil::GetTraceConfig_BackgroundTrigger( | 1283 TraceConfigMemoryTestUtil::GetTraceConfig_BackgroundTrigger( |
| 1273 1 /* period_ms */)); | 1284 1 /* period_ms */)); |
| 1274 | 1285 |
| 1286 run_loop.Run(); |
| 1287 |
| 1275 // Only background mode dumps should be allowed with the trace config. | 1288 // Only background mode dumps should be allowed with the trace config. |
| 1276 last_callback_success_ = false; | 1289 last_callback_success_ = false; |
| 1277 RequestGlobalDumpAndWait(MemoryDumpType::EXPLICITLY_TRIGGERED, | 1290 RequestGlobalDumpAndWait(MemoryDumpType::EXPLICITLY_TRIGGERED, |
| 1278 MemoryDumpLevelOfDetail::LIGHT); | 1291 MemoryDumpLevelOfDetail::LIGHT); |
| 1279 EXPECT_FALSE(last_callback_success_); | 1292 EXPECT_FALSE(last_callback_success_); |
| 1280 last_callback_success_ = false; | 1293 last_callback_success_ = false; |
| 1281 RequestGlobalDumpAndWait(MemoryDumpType::EXPLICITLY_TRIGGERED, | 1294 RequestGlobalDumpAndWait(MemoryDumpType::EXPLICITLY_TRIGGERED, |
| 1282 MemoryDumpLevelOfDetail::DETAILED); | 1295 MemoryDumpLevelOfDetail::DETAILED); |
| 1283 EXPECT_FALSE(last_callback_success_); | 1296 EXPECT_FALSE(last_callback_success_); |
| 1284 | 1297 |
| 1285 ASSERT_TRUE(IsPeriodicDumpingEnabled()); | 1298 ASSERT_TRUE(IsPeriodicDumpingEnabled()); |
| 1286 run_loop.Run(); | |
| 1287 DisableTracing(); | 1299 DisableTracing(); |
| 1288 } | 1300 } |
| 1289 | 1301 |
| 1290 // Tests that we can manually take a dump without enabling tracing. | 1302 // Tests that we can manually take a dump without enabling tracing. |
| 1291 TEST_F(MemoryDumpManagerTest, DumpWithTracingDisabled) { | 1303 TEST_F(MemoryDumpManagerTest, DumpWithTracingDisabled) { |
| 1292 InitializeMemoryDumpManager(false /* is_coordinator */); | 1304 InitializeMemoryDumpManager(false /* is_coordinator */); |
| 1293 MockMemoryDumpProvider mdp; | 1305 MockMemoryDumpProvider mdp; |
| 1294 RegisterDumpProvider(&mdp, ThreadTaskRunnerHandle::Get()); | 1306 RegisterDumpProvider(&mdp, ThreadTaskRunnerHandle::Get()); |
| 1295 | 1307 |
| 1296 DisableTracing(); | 1308 DisableTracing(); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 1316 mdm_->Disable(); | 1328 mdm_->Disable(); |
| 1317 | 1329 |
| 1318 mdm_->UnregisterDumpProvider(&mdp); | 1330 mdm_->UnregisterDumpProvider(&mdp); |
| 1319 } | 1331 } |
| 1320 | 1332 |
| 1321 TEST_F(MemoryDumpManagerTest, TestSummaryComputation) { | 1333 TEST_F(MemoryDumpManagerTest, TestSummaryComputation) { |
| 1322 InitializeMemoryDumpManager(false /* is_coordinator */); | 1334 InitializeMemoryDumpManager(false /* is_coordinator */); |
| 1323 MockMemoryDumpProvider mdp; | 1335 MockMemoryDumpProvider mdp; |
| 1324 RegisterDumpProvider(&mdp, ThreadTaskRunnerHandle::Get()); | 1336 RegisterDumpProvider(&mdp, ThreadTaskRunnerHandle::Get()); |
| 1325 | 1337 |
| 1326 const MemoryDumpSessionState* session_state = | 1338 const HeapProfilerSerializationState* heap_profiler_serialization_state = |
| 1327 mdm_->session_state_for_testing().get(); | 1339 mdm_->heap_profiler_serialization_state_for_testing().get(); |
| 1328 | 1340 |
| 1329 EXPECT_CALL(global_dump_handler_, RequestGlobalMemoryDump(_, _)) | 1341 EXPECT_CALL(global_dump_handler_, RequestGlobalMemoryDump(_, _)) |
| 1330 .WillOnce(Invoke([this](const MemoryDumpRequestArgs& args, | 1342 .WillOnce(Invoke([this](const MemoryDumpRequestArgs& args, |
| 1331 const GlobalMemoryDumpCallback& callback) { | 1343 const GlobalMemoryDumpCallback& callback) { |
| 1332 ProcessMemoryDumpCallback process_callback = | 1344 ProcessMemoryDumpCallback process_callback = |
| 1333 Bind(&MemoryDumpManagerTest_TestSummaryComputation_Test:: | 1345 Bind(&MemoryDumpManagerTest_TestSummaryComputation_Test:: |
| 1334 ProcessDumpRecordingCallbackAdapter, | 1346 ProcessDumpRecordingCallbackAdapter, |
| 1335 Unretained(this), callback); | 1347 Unretained(this), callback); |
| 1336 mdm_->CreateProcessDump(args, process_callback); | 1348 mdm_->CreateProcessDump(args, process_callback); |
| 1337 })); | 1349 })); |
| 1338 | 1350 |
| 1339 EXPECT_CALL(mdp, OnMemoryDump(_, _)) | 1351 EXPECT_CALL(mdp, OnMemoryDump(_, _)) |
| 1340 .Times(1) | 1352 .Times(1) |
| 1341 .WillRepeatedly(Invoke([session_state](const MemoryDumpArgs&, | 1353 .WillRepeatedly(Invoke([heap_profiler_serialization_state]( |
| 1342 ProcessMemoryDump* pmd) -> bool { | 1354 const MemoryDumpArgs&, |
| 1355 ProcessMemoryDump* pmd) -> bool { |
| 1343 auto* size = MemoryAllocatorDump::kNameSize; | 1356 auto* size = MemoryAllocatorDump::kNameSize; |
| 1344 auto* bytes = MemoryAllocatorDump::kUnitsBytes; | 1357 auto* bytes = MemoryAllocatorDump::kUnitsBytes; |
| 1345 const uint32_t kB = 1024; | 1358 const uint32_t kB = 1024; |
| 1346 | 1359 |
| 1347 pmd->CreateAllocatorDump("malloc")->AddScalar(size, bytes, 1 * kB); | 1360 pmd->CreateAllocatorDump("malloc")->AddScalar(size, bytes, 1 * kB); |
| 1348 pmd->CreateAllocatorDump("malloc/ignored") | 1361 pmd->CreateAllocatorDump("malloc/ignored") |
| 1349 ->AddScalar(size, bytes, 99 * kB); | 1362 ->AddScalar(size, bytes, 99 * kB); |
| 1350 | 1363 |
| 1351 pmd->CreateAllocatorDump("blink_gc")->AddScalar(size, bytes, 2 * kB); | 1364 pmd->CreateAllocatorDump("blink_gc")->AddScalar(size, bytes, 2 * kB); |
| 1352 pmd->CreateAllocatorDump("blink_gc/ignored") | 1365 pmd->CreateAllocatorDump("blink_gc/ignored") |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1392 EXPECT_EQ(3u, result.chrome_dump.v8_total_kb); | 1405 EXPECT_EQ(3u, result.chrome_dump.v8_total_kb); |
| 1393 // partition_alloc has partition_alloc/allocated_objects/* which is a subset | 1406 // partition_alloc has partition_alloc/allocated_objects/* which is a subset |
| 1394 // of partition_alloc/partitions/* so we only count the latter. | 1407 // of partition_alloc/partitions/* so we only count the latter. |
| 1395 EXPECT_EQ(4u, result.chrome_dump.partition_alloc_total_kb); | 1408 EXPECT_EQ(4u, result.chrome_dump.partition_alloc_total_kb); |
| 1396 // resident_set_kb should read from process_totals. | 1409 // resident_set_kb should read from process_totals. |
| 1397 EXPECT_EQ(5u, result.os_dump.resident_set_kb); | 1410 EXPECT_EQ(5u, result.os_dump.resident_set_kb); |
| 1398 }; | 1411 }; |
| 1399 | 1412 |
| 1400 } // namespace trace_event | 1413 } // namespace trace_event |
| 1401 } // namespace base | 1414 } // namespace base |
| OLD | NEW |