| 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 997 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1008 // Swallow all the final spurious calls until tracing gets disabled. | 1008 // Swallow all the final spurious calls until tracing gets disabled. |
| 1009 EXPECT_CALL(delegate, RequestGlobalMemoryDump(_, _)).Times(AnyNumber()); | 1009 EXPECT_CALL(delegate, RequestGlobalMemoryDump(_, _)).Times(AnyNumber()); |
| 1010 | 1010 |
| 1011 EnableTracingWithTraceConfig( | 1011 EnableTracingWithTraceConfig( |
| 1012 TraceConfigMemoryTestUtil::GetTraceConfig_PeriodicTriggers( | 1012 TraceConfigMemoryTestUtil::GetTraceConfig_PeriodicTriggers( |
| 1013 kLightDumpPeriodMs, kHeavyDumpPeriodMs)); | 1013 kLightDumpPeriodMs, kHeavyDumpPeriodMs)); |
| 1014 run_loop.Run(); | 1014 run_loop.Run(); |
| 1015 DisableTracing(); | 1015 DisableTracing(); |
| 1016 } | 1016 } |
| 1017 | 1017 |
| 1018 // Tests against race conditions that might arise when disabling tracing in the | |
| 1019 // middle of a global memory dump. | |
| 1020 // Flaky on iOS, see crbug.com/706961 | |
| 1021 #if defined(OS_IOS) | |
| 1022 #define MAYBE_DisableTracingWhileDumping DISABLED_DisableTracingWhileDumping | |
| 1023 #else | |
| 1024 #define MAYBE_DisableTracingWhileDumping DisableTracingWhileDumping | |
| 1025 #endif | |
| 1026 TEST_F(MemoryDumpManagerTest, MAYBE_DisableTracingWhileDumping) { | |
| 1027 base::WaitableEvent tracing_disabled_event( | |
| 1028 WaitableEvent::ResetPolicy::AUTOMATIC, | |
| 1029 WaitableEvent::InitialState::NOT_SIGNALED); | |
| 1030 InitializeMemoryDumpManager(false /* is_coordinator */); | |
| 1031 | |
| 1032 // Register a bound dump provider. | |
| 1033 std::unique_ptr<Thread> mdp_thread(new Thread("test thread")); | |
| 1034 mdp_thread->Start(); | |
| 1035 MockMemoryDumpProvider mdp_with_affinity; | |
| 1036 RegisterDumpProvider(&mdp_with_affinity, mdp_thread->task_runner(), | |
| 1037 kDefaultOptions); | |
| 1038 | |
| 1039 // Register also an unbound dump provider. Unbound dump providers are always | |
| 1040 // invoked after bound ones. | |
| 1041 MockMemoryDumpProvider unbound_mdp; | |
| 1042 RegisterDumpProvider(&unbound_mdp, nullptr, kDefaultOptions); | |
| 1043 | |
| 1044 EnableTracingWithLegacyCategories(MemoryDumpManager::kTraceCategory); | |
| 1045 EXPECT_CALL(*delegate_, RequestGlobalMemoryDump(_, _)).Times(1); | |
| 1046 EXPECT_CALL(mdp_with_affinity, OnMemoryDump(_, _)) | |
| 1047 .Times(1) | |
| 1048 .WillOnce( | |
| 1049 Invoke([&tracing_disabled_event](const MemoryDumpArgs&, | |
| 1050 ProcessMemoryDump* pmd) -> bool { | |
| 1051 tracing_disabled_event.Wait(); | |
| 1052 | |
| 1053 // At this point tracing has been disabled and the | |
| 1054 // MemoryDumpManager.dump_thread_ has been shut down. | |
| 1055 return true; | |
| 1056 })); | |
| 1057 | |
| 1058 // |unbound_mdp| should never be invoked because the thread for unbound dump | |
| 1059 // providers has been shutdown in the meanwhile. | |
| 1060 EXPECT_CALL(unbound_mdp, OnMemoryDump(_, _)).Times(0); | |
| 1061 | |
| 1062 last_callback_success_ = true; | |
| 1063 RunLoop run_loop; | |
| 1064 GlobalMemoryDumpCallback callback = | |
| 1065 Bind(&MemoryDumpManagerTest::GlobalDumpCallbackAdapter, Unretained(this), | |
| 1066 ThreadTaskRunnerHandle::Get(), run_loop.QuitClosure()); | |
| 1067 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED, | |
| 1068 MemoryDumpLevelOfDetail::DETAILED, callback); | |
| 1069 DisableTracing(); | |
| 1070 tracing_disabled_event.Signal(); | |
| 1071 run_loop.Run(); | |
| 1072 | |
| 1073 EXPECT_FALSE(last_callback_success_); | |
| 1074 } | |
| 1075 | |
| 1076 // Tests against race conditions that can happen if tracing is disabled before | 1018 // Tests against race conditions that can happen if tracing is disabled before |
| 1077 // the CreateProcessDump() call. Real-world regression: crbug.com/580295 . | 1019 // the CreateProcessDump() call. Real-world regression: crbug.com/580295 . |
| 1078 TEST_F(MemoryDumpManagerTest, DisableTracingRightBeforeStartOfDump) { | 1020 TEST_F(MemoryDumpManagerTest, DisableTracingRightBeforeStartOfDump) { |
| 1079 base::WaitableEvent tracing_disabled_event( | 1021 base::WaitableEvent tracing_disabled_event( |
| 1080 WaitableEvent::ResetPolicy::AUTOMATIC, | 1022 WaitableEvent::ResetPolicy::AUTOMATIC, |
| 1081 WaitableEvent::InitialState::NOT_SIGNALED); | 1023 WaitableEvent::InitialState::NOT_SIGNALED); |
| 1082 InitializeMemoryDumpManager(false /* is_coordinator */); | 1024 InitializeMemoryDumpManager(false /* is_coordinator */); |
| 1083 | 1025 |
| 1084 std::unique_ptr<Thread> mdp_thread(new Thread("test thread")); | 1026 std::unique_ptr<Thread> mdp_thread(new Thread("test thread")); |
| 1085 mdp_thread->Start(); | 1027 mdp_thread->Start(); |
| (...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1301 | 1243 |
| 1302 Thread thread("test thread"); | 1244 Thread thread("test thread"); |
| 1303 thread.Start(); | 1245 thread.Start(); |
| 1304 RegisterDumpProvider(&mdp1, thread.task_runner(), kDefaultOptions, | 1246 RegisterDumpProvider(&mdp1, thread.task_runner(), kDefaultOptions, |
| 1305 "BlacklistTestDumpProvider"); | 1247 "BlacklistTestDumpProvider"); |
| 1306 // Unregistering on wrong thread should not crash. | 1248 // Unregistering on wrong thread should not crash. |
| 1307 mdm_->UnregisterDumpProvider(&mdp1); | 1249 mdm_->UnregisterDumpProvider(&mdp1); |
| 1308 thread.Stop(); | 1250 thread.Stop(); |
| 1309 } | 1251 } |
| 1310 | 1252 |
| 1253 // Tests that we can manually take a dump without enabling tracing. |
| 1254 TEST_F(MemoryDumpManagerTest, DumpWithTracingDisabled) { |
| 1255 InitializeMemoryDumpManager(false /* is_coordinator */); |
| 1256 MockMemoryDumpProvider mdp; |
| 1257 RegisterDumpProvider(&mdp, ThreadTaskRunnerHandle::Get()); |
| 1258 |
| 1259 DisableTracing(); |
| 1260 |
| 1261 const TraceConfig& trace_config = |
| 1262 TraceConfig(TraceConfigMemoryTestUtil::GetTraceConfig_NoTriggers()); |
| 1263 const TraceConfig::MemoryDumpConfig& memory_dump_config = |
| 1264 trace_config.memory_dump_config(); |
| 1265 |
| 1266 mdm_->Enable(memory_dump_config); |
| 1267 |
| 1268 EXPECT_CALL(*delegate_, RequestGlobalMemoryDump(_, _)).Times(3); |
| 1269 EXPECT_CALL(mdp, OnMemoryDump(_, _)).Times(3).WillRepeatedly(Return(true)); |
| 1270 last_callback_success_ = true; |
| 1271 for (int i = 0; i < 3; ++i) |
| 1272 RequestGlobalDumpAndWait(MemoryDumpType::EXPLICITLY_TRIGGERED, |
| 1273 MemoryDumpLevelOfDetail::DETAILED); |
| 1274 // The callback result should actually be false since (for the moment at |
| 1275 // least) a true result means that as well as the dump generally being |
| 1276 // successful we also managed to add the dump to the trace. |
| 1277 EXPECT_FALSE(last_callback_success_); |
| 1278 |
| 1279 mdm_->Disable(); |
| 1280 |
| 1281 mdm_->UnregisterDumpProvider(&mdp); |
| 1282 } |
| 1283 |
| 1311 } // namespace trace_event | 1284 } // namespace trace_event |
| 1312 } // namespace base | 1285 } // namespace base |
| OLD | NEW |