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 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 // |task| is executed. | 111 // |task| is executed. |
112 event.Wait(); | 112 event.Wait(); |
113 } | 113 } |
114 | 114 |
115 } // namespace | 115 } // namespace |
116 | 116 |
117 // Testing MemoryDumpManagerDelegate which, by default, short-circuits dump | 117 // Testing MemoryDumpManagerDelegate which, by default, short-circuits dump |
118 // requests locally to the MemoryDumpManager instead of performing IPC dances. | 118 // requests locally to the MemoryDumpManager instead of performing IPC dances. |
119 class MemoryDumpManagerDelegateForTesting : public MemoryDumpManagerDelegate { | 119 class MemoryDumpManagerDelegateForTesting : public MemoryDumpManagerDelegate { |
120 public: | 120 public: |
121 MemoryDumpManagerDelegateForTesting() { | 121 MemoryDumpManagerDelegateForTesting(bool is_coordinator) |
| 122 : is_coordinator_(is_coordinator) { |
122 ON_CALL(*this, RequestGlobalMemoryDump(_, _)) | 123 ON_CALL(*this, RequestGlobalMemoryDump(_, _)) |
123 .WillByDefault(Invoke( | 124 .WillByDefault(Invoke( |
124 this, &MemoryDumpManagerDelegateForTesting::CreateProcessDump)); | 125 this, &MemoryDumpManagerDelegateForTesting::CreateProcessDump)); |
125 } | 126 } |
126 | 127 |
127 MOCK_METHOD2(RequestGlobalMemoryDump, | 128 MOCK_METHOD2(RequestGlobalMemoryDump, |
128 void(const MemoryDumpRequestArgs& args, | 129 void(const MemoryDumpRequestArgs& args, |
129 const MemoryDumpCallback& callback)); | 130 const MemoryDumpCallback& callback)); |
130 | 131 |
131 uint64_t GetTracingProcessId() const override { | 132 bool IsCoordinator() const override { return is_coordinator_; } |
132 NOTREACHED(); | |
133 return MemoryDumpManager::kInvalidTracingProcessId; | |
134 } | |
135 | 133 |
136 // Promote the CreateProcessDump to public so it can be used by test fixtures. | 134 // Promote the CreateProcessDump to public so it can be used by test fixtures. |
137 using MemoryDumpManagerDelegate::CreateProcessDump; | 135 using MemoryDumpManagerDelegate::CreateProcessDump; |
| 136 |
| 137 private: |
| 138 bool is_coordinator_; |
138 }; | 139 }; |
139 | 140 |
140 class MockMemoryDumpProvider : public MemoryDumpProvider { | 141 class MockMemoryDumpProvider : public MemoryDumpProvider { |
141 public: | 142 public: |
142 MOCK_METHOD0(Destructor, void()); | 143 MOCK_METHOD0(Destructor, void()); |
143 MOCK_METHOD2(OnMemoryDump, | 144 MOCK_METHOD2(OnMemoryDump, |
144 bool(const MemoryDumpArgs& args, ProcessMemoryDump* pmd)); | 145 bool(const MemoryDumpArgs& args, ProcessMemoryDump* pmd)); |
145 MOCK_METHOD1(PollFastMemoryTotal, void(uint64_t* memory_total)); | 146 MOCK_METHOD1(PollFastMemoryTotal, void(uint64_t* memory_total)); |
146 MOCK_METHOD0(SuspendFastMemoryPolling, void()); | 147 MOCK_METHOD0(SuspendFastMemoryPolling, void()); |
147 | 148 |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
212 class MemoryDumpManagerTest : public testing::Test { | 213 class MemoryDumpManagerTest : public testing::Test { |
213 public: | 214 public: |
214 MemoryDumpManagerTest() : testing::Test(), kDefaultOptions() {} | 215 MemoryDumpManagerTest() : testing::Test(), kDefaultOptions() {} |
215 | 216 |
216 void SetUp() override { | 217 void SetUp() override { |
217 last_callback_success_ = false; | 218 last_callback_success_ = false; |
218 message_loop_.reset(new MessageLoop()); | 219 message_loop_.reset(new MessageLoop()); |
219 mdm_.reset(new MemoryDumpManager()); | 220 mdm_.reset(new MemoryDumpManager()); |
220 MemoryDumpManager::SetInstanceForTesting(mdm_.get()); | 221 MemoryDumpManager::SetInstanceForTesting(mdm_.get()); |
221 ASSERT_EQ(mdm_.get(), MemoryDumpManager::GetInstance()); | 222 ASSERT_EQ(mdm_.get(), MemoryDumpManager::GetInstance()); |
222 delegate_.reset(new MemoryDumpManagerDelegateForTesting); | |
223 } | 223 } |
224 | 224 |
225 void TearDown() override { | 225 void TearDown() override { |
226 MemoryDumpManager::SetInstanceForTesting(nullptr); | 226 MemoryDumpManager::SetInstanceForTesting(nullptr); |
| 227 delegate_for_mdm_.reset(); |
| 228 delegate_ = nullptr; |
227 mdm_.reset(); | 229 mdm_.reset(); |
228 delegate_.reset(); | |
229 message_loop_.reset(); | 230 message_loop_.reset(); |
230 TraceLog::DeleteForTesting(); | 231 TraceLog::DeleteForTesting(); |
231 } | 232 } |
232 | 233 |
233 // Turns a Closure into a MemoryDumpCallback, keeping track of the callback | 234 // Turns a Closure into a MemoryDumpCallback, keeping track of the callback |
234 // 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. |
235 void DumpCallbackAdapter(scoped_refptr<SingleThreadTaskRunner> task_runner, | 236 void DumpCallbackAdapter(scoped_refptr<SingleThreadTaskRunner> task_runner, |
236 Closure closure, | 237 Closure closure, |
237 uint64_t dump_guid, | 238 uint64_t dump_guid, |
238 bool success) { | 239 bool success) { |
239 last_callback_success_ = success; | 240 last_callback_success_ = success; |
240 task_runner->PostTask(FROM_HERE, closure); | 241 task_runner->PostTask(FROM_HERE, closure); |
241 } | 242 } |
242 | 243 |
243 void PollFastMemoryTotal(uint64_t* memory_total) { | 244 void PollFastMemoryTotal(uint64_t* memory_total) { |
244 mdm_->PollFastMemoryTotal(memory_total); | 245 mdm_->PollFastMemoryTotal(memory_total); |
245 } | 246 } |
246 | 247 |
247 protected: | 248 protected: |
248 void InitializeMemoryDumpManager(bool is_coordinator) { | 249 void InitializeMemoryDumpManager(bool is_coordinator) { |
249 mdm_->set_dumper_registrations_ignored_for_testing(true); | 250 mdm_->set_dumper_registrations_ignored_for_testing(true); |
250 mdm_->Initialize(delegate_.get(), is_coordinator); | 251 delegate_for_mdm_.reset( |
| 252 new MemoryDumpManagerDelegateForTesting(is_coordinator)); |
| 253 delegate_ = delegate_for_mdm_.get(); |
| 254 mdm_->Initialize(std::move(delegate_for_mdm_)); |
251 } | 255 } |
252 | 256 |
253 void RequestGlobalDumpAndWait(MemoryDumpType dump_type, | 257 void RequestGlobalDumpAndWait(MemoryDumpType dump_type, |
254 MemoryDumpLevelOfDetail level_of_detail) { | 258 MemoryDumpLevelOfDetail level_of_detail) { |
255 RunLoop run_loop; | 259 RunLoop run_loop; |
256 MemoryDumpCallback callback = | 260 MemoryDumpCallback callback = |
257 Bind(&MemoryDumpManagerTest::DumpCallbackAdapter, Unretained(this), | 261 Bind(&MemoryDumpManagerTest::DumpCallbackAdapter, Unretained(this), |
258 ThreadTaskRunnerHandle::Get(), run_loop.QuitClosure()); | 262 ThreadTaskRunnerHandle::Get(), run_loop.QuitClosure()); |
259 mdm_->RequestGlobalDump(dump_type, level_of_detail, callback); | 263 mdm_->RequestGlobalDump(dump_type, level_of_detail, callback); |
260 run_loop.Run(); | 264 run_loop.Run(); |
(...skipping 18 matching lines...) Expand all Loading... |
279 int GetMaxConsecutiveFailuresCount() const { | 283 int GetMaxConsecutiveFailuresCount() const { |
280 return MemoryDumpManager::kMaxConsecutiveFailuresCount; | 284 return MemoryDumpManager::kMaxConsecutiveFailuresCount; |
281 } | 285 } |
282 | 286 |
283 scoped_refptr<SequencedTaskRunner> GetPollingTaskRunnerUnsafe() { | 287 scoped_refptr<SequencedTaskRunner> GetPollingTaskRunnerUnsafe() { |
284 return mdm_->dump_thread_->task_runner(); | 288 return mdm_->dump_thread_->task_runner(); |
285 } | 289 } |
286 | 290 |
287 const MemoryDumpProvider::Options kDefaultOptions; | 291 const MemoryDumpProvider::Options kDefaultOptions; |
288 std::unique_ptr<MemoryDumpManager> mdm_; | 292 std::unique_ptr<MemoryDumpManager> mdm_; |
289 std::unique_ptr<MemoryDumpManagerDelegateForTesting> delegate_; | 293 // The delegate that is going to be passed to MDM. |
| 294 std::unique_ptr<MemoryDumpManagerDelegateForTesting> delegate_for_mdm_; |
| 295 // A pointer to the delegate that is owned by MDM, after MD initialization. |
| 296 MemoryDumpManagerDelegateForTesting* delegate_; |
290 bool last_callback_success_; | 297 bool last_callback_success_; |
291 | 298 |
292 private: | 299 private: |
293 std::unique_ptr<MessageLoop> message_loop_; | 300 std::unique_ptr<MessageLoop> message_loop_; |
294 | 301 |
295 // We want our singleton torn down after each test. | 302 // We want our singleton torn down after each test. |
296 ShadowingAtExitManager at_exit_manager_; | 303 ShadowingAtExitManager at_exit_manager_; |
297 }; | 304 }; |
298 | 305 |
299 // Basic sanity checks. Registers a memory dump provider and checks that it is | 306 // Basic sanity checks. Registers a memory dump provider and checks that it is |
(...skipping 582 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
882 // began, it will still late-join the party (real use case: startup tracing). | 889 // began, it will still late-join the party (real use case: startup tracing). |
883 TEST_F(MemoryDumpManagerTest, InitializedAfterStartOfTracing) { | 890 TEST_F(MemoryDumpManagerTest, InitializedAfterStartOfTracing) { |
884 MockMemoryDumpProvider mdp; | 891 MockMemoryDumpProvider mdp; |
885 RegisterDumpProvider(&mdp, nullptr); | 892 RegisterDumpProvider(&mdp, nullptr); |
886 EnableTracingWithLegacyCategories(MemoryDumpManager::kTraceCategory); | 893 EnableTracingWithLegacyCategories(MemoryDumpManager::kTraceCategory); |
887 | 894 |
888 // First check that a RequestGlobalDump() issued before the MemoryDumpManager | 895 // First check that a RequestGlobalDump() issued before the MemoryDumpManager |
889 // initialization gets NACK-ed cleanly. | 896 // initialization gets NACK-ed cleanly. |
890 { | 897 { |
891 EXPECT_CALL(mdp, OnMemoryDump(_, _)).Times(0); | 898 EXPECT_CALL(mdp, OnMemoryDump(_, _)).Times(0); |
892 EXPECT_CALL(*delegate_, RequestGlobalMemoryDump(_, _)).Times(0); | |
893 RequestGlobalDumpAndWait(MemoryDumpType::EXPLICITLY_TRIGGERED, | 899 RequestGlobalDumpAndWait(MemoryDumpType::EXPLICITLY_TRIGGERED, |
894 MemoryDumpLevelOfDetail::DETAILED); | 900 MemoryDumpLevelOfDetail::DETAILED); |
895 EXPECT_FALSE(last_callback_success_); | 901 EXPECT_FALSE(last_callback_success_); |
896 } | 902 } |
897 | 903 |
898 // Now late-initialize the MemoryDumpManager and check that the | 904 // Now late-initialize the MemoryDumpManager and check that the |
899 // RequestGlobalDump completes successfully. | 905 // RequestGlobalDump completes successfully. |
900 { | 906 { |
| 907 InitializeMemoryDumpManager(false /* is_coordinator */); |
901 EXPECT_CALL(mdp, OnMemoryDump(_, _)).Times(1); | 908 EXPECT_CALL(mdp, OnMemoryDump(_, _)).Times(1); |
902 EXPECT_CALL(*delegate_, RequestGlobalMemoryDump(_, _)).Times(1); | 909 EXPECT_CALL(*delegate_, RequestGlobalMemoryDump(_, _)).Times(1); |
903 InitializeMemoryDumpManager(false /* is_coordinator */); | |
904 RequestGlobalDumpAndWait(MemoryDumpType::EXPLICITLY_TRIGGERED, | 910 RequestGlobalDumpAndWait(MemoryDumpType::EXPLICITLY_TRIGGERED, |
905 MemoryDumpLevelOfDetail::DETAILED); | 911 MemoryDumpLevelOfDetail::DETAILED); |
906 EXPECT_TRUE(last_callback_success_); | 912 EXPECT_TRUE(last_callback_success_); |
907 } | 913 } |
908 DisableTracing(); | 914 DisableTracing(); |
909 } | 915 } |
910 | 916 |
911 // This test (and the MemoryDumpManagerTestCoordinator below) crystallizes the | 917 // This test (and the MemoryDumpManagerTestCoordinator below) crystallizes the |
912 // expectations of the chrome://tracing UI and chrome telemetry w.r.t. periodic | 918 // expectations of the chrome://tracing UI and chrome telemetry w.r.t. periodic |
913 // dumps in memory-infra, handling gracefully the transition between the legacy | 919 // dumps in memory-infra, handling gracefully the transition between the legacy |
(...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1278 thread.Start(); | 1284 thread.Start(); |
1279 RegisterDumpProvider(&mdp1, thread.task_runner(), kDefaultOptions, | 1285 RegisterDumpProvider(&mdp1, thread.task_runner(), kDefaultOptions, |
1280 "BlacklistTestDumpProvider"); | 1286 "BlacklistTestDumpProvider"); |
1281 // Unregistering on wrong thread should not crash. | 1287 // Unregistering on wrong thread should not crash. |
1282 mdm_->UnregisterDumpProvider(&mdp1); | 1288 mdm_->UnregisterDumpProvider(&mdp1); |
1283 thread.Stop(); | 1289 thread.Stop(); |
1284 } | 1290 } |
1285 | 1291 |
1286 } // namespace trace_event | 1292 } // namespace trace_event |
1287 } // namespace base | 1293 } // namespace base |
OLD | NEW |