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 void SetIsCoordinator(bool is_coordinator) { |
133 return MemoryDumpManager::kInvalidTracingProcessId; | 134 is_coordinator_ = is_coordinator; |
134 } | 135 } |
135 | 136 |
136 // 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. |
137 using MemoryDumpManagerDelegate::CreateProcessDump; | 138 using MemoryDumpManagerDelegate::CreateProcessDump; |
139 | |
140 private: | |
141 bool is_coordinator_; | |
138 }; | 142 }; |
139 | 143 |
140 class MockMemoryDumpProvider : public MemoryDumpProvider { | 144 class MockMemoryDumpProvider : public MemoryDumpProvider { |
141 public: | 145 public: |
142 MOCK_METHOD0(Destructor, void()); | 146 MOCK_METHOD0(Destructor, void()); |
143 MOCK_METHOD2(OnMemoryDump, | 147 MOCK_METHOD2(OnMemoryDump, |
144 bool(const MemoryDumpArgs& args, ProcessMemoryDump* pmd)); | 148 bool(const MemoryDumpArgs& args, ProcessMemoryDump* pmd)); |
145 MOCK_METHOD1(PollFastMemoryTotal, void(uint64_t* memory_total)); | 149 MOCK_METHOD1(PollFastMemoryTotal, void(uint64_t* memory_total)); |
146 MOCK_METHOD0(SuspendFastMemoryPolling, void()); | 150 MOCK_METHOD0(SuspendFastMemoryPolling, void()); |
147 | 151 |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
212 class MemoryDumpManagerTest : public testing::Test { | 216 class MemoryDumpManagerTest : public testing::Test { |
213 public: | 217 public: |
214 MemoryDumpManagerTest() : testing::Test(), kDefaultOptions() {} | 218 MemoryDumpManagerTest() : testing::Test(), kDefaultOptions() {} |
215 | 219 |
216 void SetUp() override { | 220 void SetUp() override { |
217 last_callback_success_ = false; | 221 last_callback_success_ = false; |
218 message_loop_.reset(new MessageLoop()); | 222 message_loop_.reset(new MessageLoop()); |
219 mdm_.reset(new MemoryDumpManager()); | 223 mdm_.reset(new MemoryDumpManager()); |
220 MemoryDumpManager::SetInstanceForTesting(mdm_.get()); | 224 MemoryDumpManager::SetInstanceForTesting(mdm_.get()); |
221 ASSERT_EQ(mdm_.get(), MemoryDumpManager::GetInstance()); | 225 ASSERT_EQ(mdm_.get(), MemoryDumpManager::GetInstance()); |
222 delegate_.reset(new MemoryDumpManagerDelegateForTesting); | 226 delegate_for_mdm_.reset(new MemoryDumpManagerDelegateForTesting(false)); |
227 delegate_ = delegate_for_mdm_.get(); | |
oystein (OOO til 10th of July)
2017/02/16 20:50:00
This is confusing to me, why are you storing the r
chiniforooshan
2017/02/16 22:54:15
It is because MDM assumes that the ownership of th
oystein (OOO til 10th of July)
2017/02/16 23:19:49
Can the MDM have a get_delegate_for_testing() whic
chiniforooshan
2017/02/17 17:20:42
How? That function would return a MemoryDumpManage
| |
223 } | 228 } |
224 | 229 |
225 void TearDown() override { | 230 void TearDown() override { |
226 MemoryDumpManager::SetInstanceForTesting(nullptr); | 231 MemoryDumpManager::SetInstanceForTesting(nullptr); |
232 delegate_for_mdm_.reset(); | |
233 delegate_ = nullptr; | |
227 mdm_.reset(); | 234 mdm_.reset(); |
228 delegate_.reset(); | |
229 message_loop_.reset(); | 235 message_loop_.reset(); |
230 TraceLog::DeleteForTesting(); | 236 TraceLog::DeleteForTesting(); |
231 } | 237 } |
232 | 238 |
233 // Turns a Closure into a MemoryDumpCallback, keeping track of the callback | 239 // 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. | 240 // result and taking care of posting the closure on the correct task runner. |
235 void DumpCallbackAdapter(scoped_refptr<SingleThreadTaskRunner> task_runner, | 241 void DumpCallbackAdapter(scoped_refptr<SingleThreadTaskRunner> task_runner, |
236 Closure closure, | 242 Closure closure, |
237 uint64_t dump_guid, | 243 uint64_t dump_guid, |
238 bool success) { | 244 bool success) { |
239 last_callback_success_ = success; | 245 last_callback_success_ = success; |
240 task_runner->PostTask(FROM_HERE, closure); | 246 task_runner->PostTask(FROM_HERE, closure); |
241 } | 247 } |
242 | 248 |
243 void PollFastMemoryTotal(uint64_t* memory_total) { | 249 void PollFastMemoryTotal(uint64_t* memory_total) { |
244 mdm_->PollFastMemoryTotal(memory_total); | 250 mdm_->PollFastMemoryTotal(memory_total); |
245 } | 251 } |
246 | 252 |
247 protected: | 253 protected: |
248 void InitializeMemoryDumpManager(bool is_coordinator) { | 254 void InitializeMemoryDumpManager(bool is_coordinator) { |
249 mdm_->set_dumper_registrations_ignored_for_testing(true); | 255 mdm_->set_dumper_registrations_ignored_for_testing(true); |
250 mdm_->Initialize(delegate_.get(), is_coordinator); | 256 delegate_for_mdm_->SetIsCoordinator(is_coordinator); |
oystein (OOO til 10th of July)
2017/02/16 20:50:00
Can you just create the MemoryDumpManagerDelegateF
chiniforooshan
2017/02/16 22:54:15
No, because of the test case "InitializedAfterStar
oystein (OOO til 10th of July)
2017/02/16 23:19:49
Okay, though that line seems to be testing the tes
chiniforooshan
2017/02/17 17:20:42
Agreed. Let me delete that EXPECT_CALL unless Prim
| |
257 mdm_->Initialize(std::move(delegate_for_mdm_)); | |
251 } | 258 } |
252 | 259 |
253 void RequestGlobalDumpAndWait(MemoryDumpType dump_type, | 260 void RequestGlobalDumpAndWait(MemoryDumpType dump_type, |
254 MemoryDumpLevelOfDetail level_of_detail) { | 261 MemoryDumpLevelOfDetail level_of_detail) { |
255 RunLoop run_loop; | 262 RunLoop run_loop; |
256 MemoryDumpCallback callback = | 263 MemoryDumpCallback callback = |
257 Bind(&MemoryDumpManagerTest::DumpCallbackAdapter, Unretained(this), | 264 Bind(&MemoryDumpManagerTest::DumpCallbackAdapter, Unretained(this), |
258 ThreadTaskRunnerHandle::Get(), run_loop.QuitClosure()); | 265 ThreadTaskRunnerHandle::Get(), run_loop.QuitClosure()); |
259 mdm_->RequestGlobalDump(dump_type, level_of_detail, callback); | 266 mdm_->RequestGlobalDump(dump_type, level_of_detail, callback); |
260 run_loop.Run(); | 267 run_loop.Run(); |
(...skipping 18 matching lines...) Expand all Loading... | |
279 int GetMaxConsecutiveFailuresCount() const { | 286 int GetMaxConsecutiveFailuresCount() const { |
280 return MemoryDumpManager::kMaxConsecutiveFailuresCount; | 287 return MemoryDumpManager::kMaxConsecutiveFailuresCount; |
281 } | 288 } |
282 | 289 |
283 scoped_refptr<SequencedTaskRunner> GetPollingTaskRunnerUnsafe() { | 290 scoped_refptr<SequencedTaskRunner> GetPollingTaskRunnerUnsafe() { |
284 return mdm_->dump_thread_->task_runner(); | 291 return mdm_->dump_thread_->task_runner(); |
285 } | 292 } |
286 | 293 |
287 const MemoryDumpProvider::Options kDefaultOptions; | 294 const MemoryDumpProvider::Options kDefaultOptions; |
288 std::unique_ptr<MemoryDumpManager> mdm_; | 295 std::unique_ptr<MemoryDumpManager> mdm_; |
289 std::unique_ptr<MemoryDumpManagerDelegateForTesting> delegate_; | 296 // The delegate that is going to be passed to MDM. |
297 std::unique_ptr<MemoryDumpManagerDelegateForTesting> delegate_for_mdm_; | |
298 // A pointer to the delegate that is owned by MDM, after MD initialization. | |
299 MemoryDumpManagerDelegateForTesting* delegate_; | |
290 bool last_callback_success_; | 300 bool last_callback_success_; |
291 | 301 |
292 private: | 302 private: |
293 std::unique_ptr<MessageLoop> message_loop_; | 303 std::unique_ptr<MessageLoop> message_loop_; |
294 | 304 |
295 // We want our singleton torn down after each test. | 305 // We want our singleton torn down after each test. |
296 ShadowingAtExitManager at_exit_manager_; | 306 ShadowingAtExitManager at_exit_manager_; |
297 }; | 307 }; |
298 | 308 |
299 // Basic sanity checks. Registers a memory dump provider and checks that it is | 309 // 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(); | 1288 thread.Start(); |
1279 RegisterDumpProvider(&mdp1, thread.task_runner(), kDefaultOptions, | 1289 RegisterDumpProvider(&mdp1, thread.task_runner(), kDefaultOptions, |
1280 "BlacklistTestDumpProvider"); | 1290 "BlacklistTestDumpProvider"); |
1281 // Unregistering on wrong thread should not crash. | 1291 // Unregistering on wrong thread should not crash. |
1282 mdm_->UnregisterDumpProvider(&mdp1); | 1292 mdm_->UnregisterDumpProvider(&mdp1); |
1283 thread.Stop(); | 1293 thread.Stop(); |
1284 } | 1294 } |
1285 | 1295 |
1286 } // namespace trace_event | 1296 } // namespace trace_event |
1287 } // namespace base | 1297 } // namespace base |
OLD | NEW |