| 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 11 matching lines...) Expand all Loading... |
| 22 #include "base/test/test_io_thread.h" | 22 #include "base/test/test_io_thread.h" |
| 23 #include "base/test/trace_event_analyzer.h" | 23 #include "base/test/trace_event_analyzer.h" |
| 24 #include "base/threading/platform_thread.h" | 24 #include "base/threading/platform_thread.h" |
| 25 #include "base/threading/sequenced_task_runner_handle.h" | 25 #include "base/threading/sequenced_task_runner_handle.h" |
| 26 #include "base/threading/sequenced_worker_pool.h" | 26 #include "base/threading/sequenced_worker_pool.h" |
| 27 #include "base/threading/thread.h" | 27 #include "base/threading/thread.h" |
| 28 #include "base/threading/thread_task_runner_handle.h" | 28 #include "base/threading/thread_task_runner_handle.h" |
| 29 #include "base/trace_event/memory_dump_provider.h" | 29 #include "base/trace_event/memory_dump_provider.h" |
| 30 #include "base/trace_event/memory_dump_scheduler.h" | 30 #include "base/trace_event/memory_dump_scheduler.h" |
| 31 #include "base/trace_event/memory_infra_background_whitelist.h" | 31 #include "base/trace_event/memory_infra_background_whitelist.h" |
| 32 #include "base/trace_event/memory_tracing_frontend.h" |
| 32 #include "base/trace_event/process_memory_dump.h" | 33 #include "base/trace_event/process_memory_dump.h" |
| 33 #include "base/trace_event/trace_buffer.h" | 34 #include "base/trace_event/trace_buffer.h" |
| 34 #include "base/trace_event/trace_config_memory_test_util.h" | 35 #include "base/trace_event/trace_config_memory_test_util.h" |
| 35 #include "build/build_config.h" | 36 #include "build/build_config.h" |
| 36 #include "testing/gmock/include/gmock/gmock.h" | 37 #include "testing/gmock/include/gmock/gmock.h" |
| 37 #include "testing/gtest/include/gtest/gtest.h" | 38 #include "testing/gtest/include/gtest/gtest.h" |
| 38 | 39 |
| 39 using testing::_; | 40 using testing::_; |
| 40 using testing::AnyNumber; | 41 using testing::AnyNumber; |
| 41 using testing::AtMost; | 42 using testing::AtMost; |
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 222 last_callback_success_ = false; | 223 last_callback_success_ = false; |
| 223 message_loop_.reset(new MessageLoop()); | 224 message_loop_.reset(new MessageLoop()); |
| 224 mdm_.reset(new MemoryDumpManager()); | 225 mdm_.reset(new MemoryDumpManager()); |
| 225 MemoryDumpManager::SetInstanceForTesting(mdm_.get()); | 226 MemoryDumpManager::SetInstanceForTesting(mdm_.get()); |
| 226 ASSERT_EQ(mdm_.get(), MemoryDumpManager::GetInstance()); | 227 ASSERT_EQ(mdm_.get(), MemoryDumpManager::GetInstance()); |
| 227 } | 228 } |
| 228 | 229 |
| 229 void TearDown() override { | 230 void TearDown() override { |
| 230 MemoryDumpManager::SetInstanceForTesting(nullptr); | 231 MemoryDumpManager::SetInstanceForTesting(nullptr); |
| 231 delegate_ = nullptr; | 232 delegate_ = nullptr; |
| 233 tracing_frontend_ = nullptr; |
| 232 mdm_.reset(); | 234 mdm_.reset(); |
| 233 message_loop_.reset(); | 235 message_loop_.reset(); |
| 234 TraceLog::DeleteForTesting(); | 236 TraceLog::DeleteForTesting(); |
| 235 } | 237 } |
| 236 | 238 |
| 237 // Turns a Closure into a MemoryDumpCallback, keeping track of the callback | 239 // Turns a Closure into a MemoryDumpCallback, keeping track of the callback |
| 238 // result and taking care of posting the closure on the correct task runner. | 240 // result and taking care of posting the closure on the correct task runner. |
| 239 void DumpCallbackAdapter(scoped_refptr<SingleThreadTaskRunner> task_runner, | 241 void DumpCallbackAdapter(scoped_refptr<SingleThreadTaskRunner> task_runner, |
| 240 Closure closure, | 242 Closure closure, |
| 241 uint64_t dump_guid, | 243 uint64_t dump_guid, |
| 242 bool success) { | 244 bool success) { |
| 243 last_callback_success_ = success; | 245 last_callback_success_ = success; |
| 244 task_runner->PostTask(FROM_HERE, closure); | 246 task_runner->PostTask(FROM_HERE, closure); |
| 245 } | 247 } |
| 246 | 248 |
| 247 protected: | 249 protected: |
| 248 void InitializeMemoryDumpManager(bool is_coordinator) { | 250 void InitializeMemoryDumpManager(bool is_coordinator) { |
| 249 mdm_->set_dumper_registrations_ignored_for_testing(true); | 251 mdm_->set_dumper_registrations_ignored_for_testing(true); |
| 250 delegate_ = new MemoryDumpManagerDelegateForTesting(is_coordinator); | 252 delegate_ = new MemoryDumpManagerDelegateForTesting(is_coordinator); |
| 251 mdm_->Initialize(base::WrapUnique(delegate_)); | 253 mdm_->Initialize(base::WrapUnique(delegate_)); |
| 254 auto* trace_log = TraceLog::GetInstance(); |
| 255 tracing_frontend_ = new MemoryTracingFrontend(trace_log, mdm_.get()); |
| 252 } | 256 } |
| 253 | 257 |
| 254 void RequestGlobalDumpAndWait(MemoryDumpType dump_type, | 258 void RequestGlobalDumpAndWait(MemoryDumpType dump_type, |
| 255 MemoryDumpLevelOfDetail level_of_detail) { | 259 MemoryDumpLevelOfDetail level_of_detail) { |
| 256 RunLoop run_loop; | 260 RunLoop run_loop; |
| 257 MemoryDumpCallback callback = | 261 MemoryDumpCallback callback = |
| 258 Bind(&MemoryDumpManagerTest::DumpCallbackAdapter, Unretained(this), | 262 Bind(&MemoryDumpManagerTest::DumpCallbackAdapter, Unretained(this), |
| 259 ThreadTaskRunnerHandle::Get(), run_loop.QuitClosure()); | 263 ThreadTaskRunnerHandle::Get(), run_loop.QuitClosure()); |
| 260 mdm_->RequestGlobalDump(dump_type, level_of_detail, callback); | 264 mdm_->RequestGlobalDump(dump_type, level_of_detail, callback); |
| 261 run_loop.Run(); | 265 run_loop.Run(); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 277 return MemoryDumpScheduler::GetInstance()->is_enabled_for_testing(); | 281 return MemoryDumpScheduler::GetInstance()->is_enabled_for_testing(); |
| 278 } | 282 } |
| 279 | 283 |
| 280 int GetMaxConsecutiveFailuresCount() const { | 284 int GetMaxConsecutiveFailuresCount() const { |
| 281 return MemoryDumpManager::kMaxConsecutiveFailuresCount; | 285 return MemoryDumpManager::kMaxConsecutiveFailuresCount; |
| 282 } | 286 } |
| 283 | 287 |
| 284 const MemoryDumpProvider::Options kDefaultOptions; | 288 const MemoryDumpProvider::Options kDefaultOptions; |
| 285 std::unique_ptr<MemoryDumpManager> mdm_; | 289 std::unique_ptr<MemoryDumpManager> mdm_; |
| 286 MemoryDumpManagerDelegateForTesting* delegate_; | 290 MemoryDumpManagerDelegateForTesting* delegate_; |
| 291 MemoryTracingFrontend* tracing_frontend_; |
| 287 bool last_callback_success_; | 292 bool last_callback_success_; |
| 288 | 293 |
| 289 private: | 294 private: |
| 290 std::unique_ptr<MessageLoop> message_loop_; | 295 std::unique_ptr<MessageLoop> message_loop_; |
| 291 | 296 |
| 292 // We want our singleton torn down after each test. | 297 // We want our singleton torn down after each test. |
| 293 ShadowingAtExitManager at_exit_manager_; | 298 ShadowingAtExitManager at_exit_manager_; |
| 294 }; | 299 }; |
| 295 | 300 |
| 296 // Basic sanity checks. Registers a memory dump provider and checks that it is | 301 // Basic sanity checks. Registers a memory dump provider and checks that it is |
| (...skipping 692 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 989 // Swallow all the final spurious calls until tracing gets disabled. | 994 // Swallow all the final spurious calls until tracing gets disabled. |
| 990 EXPECT_CALL(delegate, RequestGlobalMemoryDump(_, _)).Times(AnyNumber()); | 995 EXPECT_CALL(delegate, RequestGlobalMemoryDump(_, _)).Times(AnyNumber()); |
| 991 | 996 |
| 992 EnableTracingWithTraceConfig( | 997 EnableTracingWithTraceConfig( |
| 993 TraceConfigMemoryTestUtil::GetTraceConfig_PeriodicTriggers( | 998 TraceConfigMemoryTestUtil::GetTraceConfig_PeriodicTriggers( |
| 994 kLightDumpPeriodMs, kHeavyDumpPeriodMs)); | 999 kLightDumpPeriodMs, kHeavyDumpPeriodMs)); |
| 995 run_loop.Run(); | 1000 run_loop.Run(); |
| 996 DisableTracing(); | 1001 DisableTracing(); |
| 997 } | 1002 } |
| 998 | 1003 |
| 999 // Tests against race conditions that might arise when disabling tracing in the | |
| 1000 // middle of a global memory dump. | |
| 1001 // Flaky on iOS, see crbug.com/706961 | |
| 1002 #if defined(OS_IOS) | |
| 1003 #define MAYBE_DisableTracingWhileDumping DISABLED_DisableTracingWhileDumping | |
| 1004 #else | |
| 1005 #define MAYBE_DisableTracingWhileDumping DisableTracingWhileDumping | |
| 1006 #endif | |
| 1007 TEST_F(MemoryDumpManagerTest, MAYBE_DisableTracingWhileDumping) { | |
| 1008 base::WaitableEvent tracing_disabled_event( | |
| 1009 WaitableEvent::ResetPolicy::AUTOMATIC, | |
| 1010 WaitableEvent::InitialState::NOT_SIGNALED); | |
| 1011 InitializeMemoryDumpManager(false /* is_coordinator */); | |
| 1012 | |
| 1013 // Register a bound dump provider. | |
| 1014 std::unique_ptr<Thread> mdp_thread(new Thread("test thread")); | |
| 1015 mdp_thread->Start(); | |
| 1016 MockMemoryDumpProvider mdp_with_affinity; | |
| 1017 RegisterDumpProvider(&mdp_with_affinity, mdp_thread->task_runner(), | |
| 1018 kDefaultOptions); | |
| 1019 | |
| 1020 // Register also an unbound dump provider. Unbound dump providers are always | |
| 1021 // invoked after bound ones. | |
| 1022 MockMemoryDumpProvider unbound_mdp; | |
| 1023 RegisterDumpProvider(&unbound_mdp, nullptr, kDefaultOptions); | |
| 1024 | |
| 1025 EnableTracingWithLegacyCategories(MemoryDumpManager::kTraceCategory); | |
| 1026 EXPECT_CALL(*delegate_, RequestGlobalMemoryDump(_, _)).Times(1); | |
| 1027 EXPECT_CALL(mdp_with_affinity, OnMemoryDump(_, _)) | |
| 1028 .Times(1) | |
| 1029 .WillOnce( | |
| 1030 Invoke([&tracing_disabled_event](const MemoryDumpArgs&, | |
| 1031 ProcessMemoryDump* pmd) -> bool { | |
| 1032 tracing_disabled_event.Wait(); | |
| 1033 | |
| 1034 // At this point tracing has been disabled and the | |
| 1035 // MemoryDumpManager.dump_thread_ has been shut down. | |
| 1036 return true; | |
| 1037 })); | |
| 1038 | |
| 1039 // |unbound_mdp| should never be invoked because the thread for unbound dump | |
| 1040 // providers has been shutdown in the meanwhile. | |
| 1041 EXPECT_CALL(unbound_mdp, OnMemoryDump(_, _)).Times(0); | |
| 1042 | |
| 1043 last_callback_success_ = true; | |
| 1044 RunLoop run_loop; | |
| 1045 MemoryDumpCallback callback = | |
| 1046 Bind(&MemoryDumpManagerTest::DumpCallbackAdapter, Unretained(this), | |
| 1047 ThreadTaskRunnerHandle::Get(), run_loop.QuitClosure()); | |
| 1048 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED, | |
| 1049 MemoryDumpLevelOfDetail::DETAILED, callback); | |
| 1050 DisableTracing(); | |
| 1051 tracing_disabled_event.Signal(); | |
| 1052 run_loop.Run(); | |
| 1053 | |
| 1054 EXPECT_FALSE(last_callback_success_); | |
| 1055 } | |
| 1056 | |
| 1057 // Tests against race conditions that can happen if tracing is disabled before | 1004 // Tests against race conditions that can happen if tracing is disabled before |
| 1058 // the CreateProcessDump() call. Real-world regression: crbug.com/580295 . | 1005 // the CreateProcessDump() call. Real-world regression: crbug.com/580295 . |
| 1059 TEST_F(MemoryDumpManagerTest, DisableTracingRightBeforeStartOfDump) { | 1006 TEST_F(MemoryDumpManagerTest, DisableTracingRightBeforeStartOfDump) { |
| 1060 base::WaitableEvent tracing_disabled_event( | 1007 base::WaitableEvent tracing_disabled_event( |
| 1061 WaitableEvent::ResetPolicy::AUTOMATIC, | 1008 WaitableEvent::ResetPolicy::AUTOMATIC, |
| 1062 WaitableEvent::InitialState::NOT_SIGNALED); | 1009 WaitableEvent::InitialState::NOT_SIGNALED); |
| 1063 InitializeMemoryDumpManager(false /* is_coordinator */); | 1010 InitializeMemoryDumpManager(false /* is_coordinator */); |
| 1064 | 1011 |
| 1065 std::unique_ptr<Thread> mdp_thread(new Thread("test thread")); | 1012 std::unique_ptr<Thread> mdp_thread(new Thread("test thread")); |
| 1066 mdp_thread->Start(); | 1013 mdp_thread->Start(); |
| (...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1282 thread.Start(); | 1229 thread.Start(); |
| 1283 RegisterDumpProvider(&mdp1, thread.task_runner(), kDefaultOptions, | 1230 RegisterDumpProvider(&mdp1, thread.task_runner(), kDefaultOptions, |
| 1284 "BlacklistTestDumpProvider"); | 1231 "BlacklistTestDumpProvider"); |
| 1285 // Unregistering on wrong thread should not crash. | 1232 // Unregistering on wrong thread should not crash. |
| 1286 mdm_->UnregisterDumpProvider(&mdp1); | 1233 mdm_->UnregisterDumpProvider(&mdp1); |
| 1287 thread.Stop(); | 1234 thread.Stop(); |
| 1288 } | 1235 } |
| 1289 | 1236 |
| 1290 } // namespace trace_event | 1237 } // namespace trace_event |
| 1291 } // namespace base | 1238 } // namespace base |
| OLD | NEW |