| 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 uint64_t GetTracingProcessId() const override { |
| 132 NOTREACHED(); | 133 NOTREACHED(); |
| 133 return MemoryDumpManager::kInvalidTracingProcessId; | 134 return MemoryDumpManager::kInvalidTracingProcessId; |
| 134 } | 135 } |
| 135 | 136 |
| 137 bool IsCoordinator() const override { return is_coordinator_; } |
| 138 void SetIsCoordinator(bool is_coordinator) { |
| 139 is_coordinator_ = is_coordinator; |
| 140 } |
| 141 |
| 136 // Promote the CreateProcessDump to public so it can be used by test fixtures. | 142 // Promote the CreateProcessDump to public so it can be used by test fixtures. |
| 137 using MemoryDumpManagerDelegate::CreateProcessDump; | 143 using MemoryDumpManagerDelegate::CreateProcessDump; |
| 144 |
| 145 private: |
| 146 bool is_coordinator_; |
| 138 }; | 147 }; |
| 139 | 148 |
| 140 class MockMemoryDumpProvider : public MemoryDumpProvider { | 149 class MockMemoryDumpProvider : public MemoryDumpProvider { |
| 141 public: | 150 public: |
| 142 MOCK_METHOD0(Destructor, void()); | 151 MOCK_METHOD0(Destructor, void()); |
| 143 MOCK_METHOD2(OnMemoryDump, | 152 MOCK_METHOD2(OnMemoryDump, |
| 144 bool(const MemoryDumpArgs& args, ProcessMemoryDump* pmd)); | 153 bool(const MemoryDumpArgs& args, ProcessMemoryDump* pmd)); |
| 145 MOCK_METHOD1(PollFastMemoryTotal, void(uint64_t* memory_total)); | 154 MOCK_METHOD1(PollFastMemoryTotal, void(uint64_t* memory_total)); |
| 146 MOCK_METHOD0(SuspendFastMemoryPolling, void()); | 155 MOCK_METHOD0(SuspendFastMemoryPolling, void()); |
| 147 | 156 |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 212 class MemoryDumpManagerTest : public testing::Test { | 221 class MemoryDumpManagerTest : public testing::Test { |
| 213 public: | 222 public: |
| 214 MemoryDumpManagerTest() : testing::Test(), kDefaultOptions() {} | 223 MemoryDumpManagerTest() : testing::Test(), kDefaultOptions() {} |
| 215 | 224 |
| 216 void SetUp() override { | 225 void SetUp() override { |
| 217 last_callback_success_ = false; | 226 last_callback_success_ = false; |
| 218 message_loop_.reset(new MessageLoop()); | 227 message_loop_.reset(new MessageLoop()); |
| 219 mdm_.reset(new MemoryDumpManager()); | 228 mdm_.reset(new MemoryDumpManager()); |
| 220 MemoryDumpManager::SetInstanceForTesting(mdm_.get()); | 229 MemoryDumpManager::SetInstanceForTesting(mdm_.get()); |
| 221 ASSERT_EQ(mdm_.get(), MemoryDumpManager::GetInstance()); | 230 ASSERT_EQ(mdm_.get(), MemoryDumpManager::GetInstance()); |
| 222 delegate_.reset(new MemoryDumpManagerDelegateForTesting); | 231 delegate_for_mdm_.reset(new MemoryDumpManagerDelegateForTesting(false)); |
| 232 delegate_ = delegate_for_mdm_.get(); |
| 223 } | 233 } |
| 224 | 234 |
| 225 void TearDown() override { | 235 void TearDown() override { |
| 226 MemoryDumpManager::SetInstanceForTesting(nullptr); | 236 MemoryDumpManager::SetInstanceForTesting(nullptr); |
| 237 delegate_for_mdm_.reset(); |
| 238 delegate_ = nullptr; |
| 227 mdm_.reset(); | 239 mdm_.reset(); |
| 228 delegate_.reset(); | |
| 229 message_loop_.reset(); | 240 message_loop_.reset(); |
| 230 TraceLog::DeleteForTesting(); | 241 TraceLog::DeleteForTesting(); |
| 231 } | 242 } |
| 232 | 243 |
| 233 // Turns a Closure into a MemoryDumpCallback, keeping track of the callback | 244 // 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. | 245 // result and taking care of posting the closure on the correct task runner. |
| 235 void DumpCallbackAdapter(scoped_refptr<SingleThreadTaskRunner> task_runner, | 246 void DumpCallbackAdapter(scoped_refptr<SingleThreadTaskRunner> task_runner, |
| 236 Closure closure, | 247 Closure closure, |
| 237 uint64_t dump_guid, | 248 uint64_t dump_guid, |
| 238 bool success) { | 249 bool success) { |
| 239 last_callback_success_ = success; | 250 last_callback_success_ = success; |
| 240 task_runner->PostTask(FROM_HERE, closure); | 251 task_runner->PostTask(FROM_HERE, closure); |
| 241 } | 252 } |
| 242 | 253 |
| 243 void PollFastMemoryTotal(uint64_t* memory_total) { | 254 void PollFastMemoryTotal(uint64_t* memory_total) { |
| 244 mdm_->PollFastMemoryTotal(memory_total); | 255 mdm_->PollFastMemoryTotal(memory_total); |
| 245 } | 256 } |
| 246 | 257 |
| 247 protected: | 258 protected: |
| 248 void InitializeMemoryDumpManager(bool is_coordinator) { | 259 void InitializeMemoryDumpManager(bool is_coordinator) { |
| 249 mdm_->set_dumper_registrations_ignored_for_testing(true); | 260 mdm_->set_dumper_registrations_ignored_for_testing(true); |
| 250 mdm_->Initialize(delegate_.get(), is_coordinator); | 261 delegate_for_mdm_->SetIsCoordinator(is_coordinator); |
| 262 mdm_->Initialize(std::move(delegate_for_mdm_)); |
| 251 } | 263 } |
| 252 | 264 |
| 253 void RequestGlobalDumpAndWait(MemoryDumpType dump_type, | 265 void RequestGlobalDumpAndWait(MemoryDumpType dump_type, |
| 254 MemoryDumpLevelOfDetail level_of_detail) { | 266 MemoryDumpLevelOfDetail level_of_detail) { |
| 255 RunLoop run_loop; | 267 RunLoop run_loop; |
| 256 MemoryDumpCallback callback = | 268 MemoryDumpCallback callback = |
| 257 Bind(&MemoryDumpManagerTest::DumpCallbackAdapter, Unretained(this), | 269 Bind(&MemoryDumpManagerTest::DumpCallbackAdapter, Unretained(this), |
| 258 ThreadTaskRunnerHandle::Get(), run_loop.QuitClosure()); | 270 ThreadTaskRunnerHandle::Get(), run_loop.QuitClosure()); |
| 259 mdm_->RequestGlobalDump(dump_type, level_of_detail, callback); | 271 mdm_->RequestGlobalDump(dump_type, level_of_detail, callback); |
| 260 run_loop.Run(); | 272 run_loop.Run(); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 279 int GetMaxConsecutiveFailuresCount() const { | 291 int GetMaxConsecutiveFailuresCount() const { |
| 280 return MemoryDumpManager::kMaxConsecutiveFailuresCount; | 292 return MemoryDumpManager::kMaxConsecutiveFailuresCount; |
| 281 } | 293 } |
| 282 | 294 |
| 283 scoped_refptr<SequencedTaskRunner> GetPollingTaskRunnerUnsafe() { | 295 scoped_refptr<SequencedTaskRunner> GetPollingTaskRunnerUnsafe() { |
| 284 return mdm_->dump_thread_->task_runner(); | 296 return mdm_->dump_thread_->task_runner(); |
| 285 } | 297 } |
| 286 | 298 |
| 287 const MemoryDumpProvider::Options kDefaultOptions; | 299 const MemoryDumpProvider::Options kDefaultOptions; |
| 288 std::unique_ptr<MemoryDumpManager> mdm_; | 300 std::unique_ptr<MemoryDumpManager> mdm_; |
| 289 std::unique_ptr<MemoryDumpManagerDelegateForTesting> delegate_; | 301 // The delegate that is going to be passed to MDM. |
| 302 std::unique_ptr<MemoryDumpManagerDelegateForTesting> delegate_for_mdm_; |
| 303 // A pointer to the delegate that is owned by MDM, after MD initialization. |
| 304 MemoryDumpManagerDelegateForTesting* delegate_; |
| 290 bool last_callback_success_; | 305 bool last_callback_success_; |
| 291 | 306 |
| 292 private: | 307 private: |
| 293 std::unique_ptr<MessageLoop> message_loop_; | 308 std::unique_ptr<MessageLoop> message_loop_; |
| 294 | 309 |
| 295 // We want our singleton torn down after each test. | 310 // We want our singleton torn down after each test. |
| 296 ShadowingAtExitManager at_exit_manager_; | 311 ShadowingAtExitManager at_exit_manager_; |
| 297 }; | 312 }; |
| 298 | 313 |
| 299 // Basic sanity checks. Registers a memory dump provider and checks that it is | 314 // Basic sanity checks. Registers a memory dump provider and checks that it is |
| (...skipping 978 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1278 thread.Start(); | 1293 thread.Start(); |
| 1279 RegisterDumpProvider(&mdp1, thread.task_runner(), kDefaultOptions, | 1294 RegisterDumpProvider(&mdp1, thread.task_runner(), kDefaultOptions, |
| 1280 "BlacklistTestDumpProvider"); | 1295 "BlacklistTestDumpProvider"); |
| 1281 // Unregistering on wrong thread should not crash. | 1296 // Unregistering on wrong thread should not crash. |
| 1282 mdm_->UnregisterDumpProvider(&mdp1); | 1297 mdm_->UnregisterDumpProvider(&mdp1); |
| 1283 thread.Stop(); | 1298 thread.Stop(); |
| 1284 } | 1299 } |
| 1285 | 1300 |
| 1286 } // namespace trace_event | 1301 } // namespace trace_event |
| 1287 } // namespace base | 1302 } // namespace base |
| OLD | NEW |