| 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 <vector> | 10 #include <vector> |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 // |task| is executed. | 112 // |task| is executed. |
| 113 event.Wait(); | 113 event.Wait(); |
| 114 } | 114 } |
| 115 | 115 |
| 116 } // namespace | 116 } // namespace |
| 117 | 117 |
| 118 // Testing MemoryDumpManagerDelegate which, by default, short-circuits dump | 118 // Testing MemoryDumpManagerDelegate which, by default, short-circuits dump |
| 119 // requests locally to the MemoryDumpManager instead of performing IPC dances. | 119 // requests locally to the MemoryDumpManager instead of performing IPC dances. |
| 120 class MemoryDumpManagerDelegateForTesting : public MemoryDumpManagerDelegate { | 120 class MemoryDumpManagerDelegateForTesting : public MemoryDumpManagerDelegate { |
| 121 public: | 121 public: |
| 122 MemoryDumpManagerDelegateForTesting(bool is_coordinator) | 122 MemoryDumpManagerDelegateForTesting() { |
| 123 : is_coordinator_(is_coordinator) { | |
| 124 ON_CALL(*this, RequestGlobalMemoryDump(_, _)) | 123 ON_CALL(*this, RequestGlobalMemoryDump(_, _)) |
| 125 .WillByDefault(Invoke( | 124 .WillByDefault(Invoke( |
| 126 this, &MemoryDumpManagerDelegateForTesting::CreateProcessDump)); | 125 this, &MemoryDumpManagerDelegateForTesting::CreateProcessDump)); |
| 127 } | 126 } |
| 128 | 127 |
| 129 MOCK_METHOD2(RequestGlobalMemoryDump, | 128 MOCK_METHOD2(RequestGlobalMemoryDump, |
| 130 void(const MemoryDumpRequestArgs& args, | 129 void(const MemoryDumpRequestArgs& args, |
| 131 const MemoryDumpCallback& callback)); | 130 const MemoryDumpCallback& callback)); |
| 132 | 131 |
| 133 bool IsCoordinator() const override { return is_coordinator_; } | 132 uint64_t GetTracingProcessId() const override { |
| 133 NOTREACHED(); |
| 134 return MemoryDumpManager::kInvalidTracingProcessId; |
| 135 } |
| 134 | 136 |
| 135 // Promote the CreateProcessDump to public so it can be used by test fixtures. | 137 // Promote the CreateProcessDump to public so it can be used by test fixtures. |
| 136 using MemoryDumpManagerDelegate::CreateProcessDump; | 138 using MemoryDumpManagerDelegate::CreateProcessDump; |
| 137 | |
| 138 private: | |
| 139 bool is_coordinator_; | |
| 140 }; | 139 }; |
| 141 | 140 |
| 142 class MockMemoryDumpProvider : public MemoryDumpProvider { | 141 class MockMemoryDumpProvider : public MemoryDumpProvider { |
| 143 public: | 142 public: |
| 144 MOCK_METHOD0(Destructor, void()); | 143 MOCK_METHOD0(Destructor, void()); |
| 145 MOCK_METHOD2(OnMemoryDump, | 144 MOCK_METHOD2(OnMemoryDump, |
| 146 bool(const MemoryDumpArgs& args, ProcessMemoryDump* pmd)); | 145 bool(const MemoryDumpArgs& args, ProcessMemoryDump* pmd)); |
| 147 MOCK_METHOD1(PollFastMemoryTotal, void(uint64_t* memory_total)); | 146 MOCK_METHOD1(PollFastMemoryTotal, void(uint64_t* memory_total)); |
| 148 MOCK_METHOD0(SuspendFastMemoryPolling, void()); | 147 MOCK_METHOD0(SuspendFastMemoryPolling, void()); |
| 149 | 148 |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 214 class MemoryDumpManagerTest : public testing::Test { | 213 class MemoryDumpManagerTest : public testing::Test { |
| 215 public: | 214 public: |
| 216 MemoryDumpManagerTest() : testing::Test(), kDefaultOptions() {} | 215 MemoryDumpManagerTest() : testing::Test(), kDefaultOptions() {} |
| 217 | 216 |
| 218 void SetUp() override { | 217 void SetUp() override { |
| 219 last_callback_success_ = false; | 218 last_callback_success_ = false; |
| 220 message_loop_.reset(new MessageLoop()); | 219 message_loop_.reset(new MessageLoop()); |
| 221 mdm_.reset(new MemoryDumpManager()); | 220 mdm_.reset(new MemoryDumpManager()); |
| 222 MemoryDumpManager::SetInstanceForTesting(mdm_.get()); | 221 MemoryDumpManager::SetInstanceForTesting(mdm_.get()); |
| 223 ASSERT_EQ(mdm_.get(), MemoryDumpManager::GetInstance()); | 222 ASSERT_EQ(mdm_.get(), MemoryDumpManager::GetInstance()); |
| 223 delegate_.reset(new MemoryDumpManagerDelegateForTesting); |
| 224 } | 224 } |
| 225 | 225 |
| 226 void TearDown() override { | 226 void TearDown() override { |
| 227 MemoryDumpManager::SetInstanceForTesting(nullptr); | 227 MemoryDumpManager::SetInstanceForTesting(nullptr); |
| 228 mdm_.reset(); |
| 228 delegate_.reset(); | 229 delegate_.reset(); |
| 229 mdm_.reset(); | |
| 230 message_loop_.reset(); | 230 message_loop_.reset(); |
| 231 TraceLog::DeleteForTesting(); | 231 TraceLog::DeleteForTesting(); |
| 232 } | 232 } |
| 233 | 233 |
| 234 // Turns a Closure into a MemoryDumpCallback, keeping track of the callback | 234 // Turns a Closure into a MemoryDumpCallback, keeping track of the callback |
| 235 // result and taking care of posting the closure on the correct task runner. | 235 // result and taking care of posting the closure on the correct task runner. |
| 236 void DumpCallbackAdapter(scoped_refptr<SingleThreadTaskRunner> task_runner, | 236 void DumpCallbackAdapter(scoped_refptr<SingleThreadTaskRunner> task_runner, |
| 237 Closure closure, | 237 Closure closure, |
| 238 uint64_t dump_guid, | 238 uint64_t dump_guid, |
| 239 bool success) { | 239 bool success) { |
| 240 last_callback_success_ = success; | 240 last_callback_success_ = success; |
| 241 task_runner->PostTask(FROM_HERE, closure); | 241 task_runner->PostTask(FROM_HERE, closure); |
| 242 } | 242 } |
| 243 | 243 |
| 244 void PollFastMemoryTotal(uint64_t* memory_total) { | 244 void PollFastMemoryTotal(uint64_t* memory_total) { |
| 245 mdm_->PollFastMemoryTotal(memory_total); | 245 mdm_->PollFastMemoryTotal(memory_total); |
| 246 } | 246 } |
| 247 | 247 |
| 248 protected: | 248 protected: |
| 249 void InitializeMemoryDumpManager(bool is_coordinator) { | 249 void InitializeMemoryDumpManager(bool is_coordinator) { |
| 250 mdm_->set_dumper_registrations_ignored_for_testing(true); | 250 mdm_->set_dumper_registrations_ignored_for_testing(true); |
| 251 delegate_.reset(new MemoryDumpManagerDelegateForTesting(is_coordinator)); | 251 mdm_->Initialize(delegate_.get(), is_coordinator); |
| 252 mdm_->Initialize(delegate_.get()); | |
| 253 } | 252 } |
| 254 | 253 |
| 255 void RequestGlobalDumpAndWait(MemoryDumpType dump_type, | 254 void RequestGlobalDumpAndWait(MemoryDumpType dump_type, |
| 256 MemoryDumpLevelOfDetail level_of_detail) { | 255 MemoryDumpLevelOfDetail level_of_detail) { |
| 257 RunLoop run_loop; | 256 RunLoop run_loop; |
| 258 MemoryDumpCallback callback = | 257 MemoryDumpCallback callback = |
| 259 Bind(&MemoryDumpManagerTest::DumpCallbackAdapter, Unretained(this), | 258 Bind(&MemoryDumpManagerTest::DumpCallbackAdapter, Unretained(this), |
| 260 ThreadTaskRunnerHandle::Get(), run_loop.QuitClosure()); | 259 ThreadTaskRunnerHandle::Get(), run_loop.QuitClosure()); |
| 261 mdm_->RequestGlobalDump(dump_type, level_of_detail, callback); | 260 mdm_->RequestGlobalDump(dump_type, level_of_detail, callback); |
| 262 run_loop.Run(); | 261 run_loop.Run(); |
| (...skipping 628 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 891 // began, it will still late-join the party (real use case: startup tracing). | 890 // began, it will still late-join the party (real use case: startup tracing). |
| 892 TEST_F(MemoryDumpManagerTest, InitializedAfterStartOfTracing) { | 891 TEST_F(MemoryDumpManagerTest, InitializedAfterStartOfTracing) { |
| 893 MockMemoryDumpProvider mdp; | 892 MockMemoryDumpProvider mdp; |
| 894 RegisterDumpProvider(&mdp, nullptr); | 893 RegisterDumpProvider(&mdp, nullptr); |
| 895 EnableTracingWithLegacyCategories(MemoryDumpManager::kTraceCategory); | 894 EnableTracingWithLegacyCategories(MemoryDumpManager::kTraceCategory); |
| 896 | 895 |
| 897 // First check that a RequestGlobalDump() issued before the MemoryDumpManager | 896 // First check that a RequestGlobalDump() issued before the MemoryDumpManager |
| 898 // initialization gets NACK-ed cleanly. | 897 // initialization gets NACK-ed cleanly. |
| 899 { | 898 { |
| 900 EXPECT_CALL(mdp, OnMemoryDump(_, _)).Times(0); | 899 EXPECT_CALL(mdp, OnMemoryDump(_, _)).Times(0); |
| 900 EXPECT_CALL(*delegate_, RequestGlobalMemoryDump(_, _)).Times(0); |
| 901 RequestGlobalDumpAndWait(MemoryDumpType::EXPLICITLY_TRIGGERED, | 901 RequestGlobalDumpAndWait(MemoryDumpType::EXPLICITLY_TRIGGERED, |
| 902 MemoryDumpLevelOfDetail::DETAILED); | 902 MemoryDumpLevelOfDetail::DETAILED); |
| 903 EXPECT_FALSE(last_callback_success_); | 903 EXPECT_FALSE(last_callback_success_); |
| 904 } | 904 } |
| 905 | 905 |
| 906 // Now late-initialize the MemoryDumpManager and check that the | 906 // Now late-initialize the MemoryDumpManager and check that the |
| 907 // RequestGlobalDump completes successfully. | 907 // RequestGlobalDump completes successfully. |
| 908 { | 908 { |
| 909 InitializeMemoryDumpManager(false /* is_coordinator */); | |
| 910 EXPECT_CALL(mdp, OnMemoryDump(_, _)).Times(1); | 909 EXPECT_CALL(mdp, OnMemoryDump(_, _)).Times(1); |
| 911 EXPECT_CALL(*delegate_, RequestGlobalMemoryDump(_, _)).Times(1); | 910 EXPECT_CALL(*delegate_, RequestGlobalMemoryDump(_, _)).Times(1); |
| 911 InitializeMemoryDumpManager(false /* is_coordinator */); |
| 912 RequestGlobalDumpAndWait(MemoryDumpType::EXPLICITLY_TRIGGERED, | 912 RequestGlobalDumpAndWait(MemoryDumpType::EXPLICITLY_TRIGGERED, |
| 913 MemoryDumpLevelOfDetail::DETAILED); | 913 MemoryDumpLevelOfDetail::DETAILED); |
| 914 EXPECT_TRUE(last_callback_success_); | 914 EXPECT_TRUE(last_callback_success_); |
| 915 } | 915 } |
| 916 DisableTracing(); | 916 DisableTracing(); |
| 917 } | 917 } |
| 918 | 918 |
| 919 // This test (and the MemoryDumpManagerTestCoordinator below) crystallizes the | 919 // This test (and the MemoryDumpManagerTestCoordinator below) crystallizes the |
| 920 // expectations of the chrome://tracing UI and chrome telemetry w.r.t. periodic | 920 // expectations of the chrome://tracing UI and chrome telemetry w.r.t. periodic |
| 921 // dumps in memory-infra, handling gracefully the transition between the legacy | 921 // dumps in memory-infra, handling gracefully the transition between the legacy |
| (...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1286 thread.Start(); | 1286 thread.Start(); |
| 1287 RegisterDumpProvider(&mdp1, thread.task_runner(), kDefaultOptions, | 1287 RegisterDumpProvider(&mdp1, thread.task_runner(), kDefaultOptions, |
| 1288 "BlacklistTestDumpProvider"); | 1288 "BlacklistTestDumpProvider"); |
| 1289 // Unregistering on wrong thread should not crash. | 1289 // Unregistering on wrong thread should not crash. |
| 1290 mdm_->UnregisterDumpProvider(&mdp1); | 1290 mdm_->UnregisterDumpProvider(&mdp1); |
| 1291 thread.Stop(); | 1291 thread.Stop(); |
| 1292 } | 1292 } |
| 1293 | 1293 |
| 1294 } // namespace trace_event | 1294 } // namespace trace_event |
| 1295 } // namespace base | 1295 } // namespace base |
| OLD | NEW |