Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / base / trace_event / memory_dump_manager_unittest.cc
blob5c47721d71149f0c74e585ba1df4ace470499761
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
3 // found in the LICENSE file.
5 #include "base/trace_event/memory_dump_manager.h"
7 #include "base/bind_helpers.h"
8 #include "base/memory/scoped_vector.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/run_loop.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "base/threading/thread.h"
13 #include "base/trace_event/memory_dump_provider.h"
14 #include "base/trace_event/process_memory_dump.h"
15 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 using testing::_;
19 using testing::Between;
20 using testing::Invoke;
21 using testing::Return;
23 namespace base {
24 namespace trace_event {
26 // Testing MemoryDumpManagerDelegate which short-circuits dump requests locally
27 // instead of performing IPC dances.
28 class MemoryDumpManagerDelegateForTesting : public MemoryDumpManagerDelegate {
29 public:
30 void RequestGlobalMemoryDump(const MemoryDumpRequestArgs& args,
31 const MemoryDumpCallback& callback) override {
32 CreateProcessDump(args, callback);
35 bool IsCoordinatorProcess() const override { return false; }
38 class MemoryDumpManagerTest : public testing::Test {
39 public:
40 void SetUp() override {
41 last_callback_success_ = false;
42 message_loop_.reset(new MessageLoop());
43 mdm_.reset(new MemoryDumpManager());
44 MemoryDumpManager::SetInstanceForTesting(mdm_.get());
45 ASSERT_EQ(mdm_, MemoryDumpManager::GetInstance());
46 MemoryDumpManager::GetInstance()->Initialize();
47 MemoryDumpManager::GetInstance()->SetDelegate(&delegate_);
50 void TearDown() override {
51 MemoryDumpManager::SetInstanceForTesting(nullptr);
52 mdm_.reset();
53 message_loop_.reset();
54 TraceLog::DeleteForTesting();
57 void DumpCallbackAdapter(scoped_refptr<SingleThreadTaskRunner> task_runner,
58 Closure closure,
59 uint64 dump_guid,
60 bool success) {
61 last_callback_success_ = success;
62 task_runner->PostTask(FROM_HERE, closure);
65 protected:
66 const char* kTraceCategory = MemoryDumpManager::kTraceCategoryForTesting;
68 void EnableTracing(const char* category) {
69 TraceLog::GetInstance()->SetEnabled(
70 TraceConfig(category, ""), TraceLog::RECORDING_MODE);
73 void DisableTracing() { TraceLog::GetInstance()->SetDisabled(); }
75 scoped_ptr<MemoryDumpManager> mdm_;
76 bool last_callback_success_;
78 private:
79 scoped_ptr<MessageLoop> message_loop_;
80 MemoryDumpManagerDelegateForTesting delegate_;
82 // We want our singleton torn down after each test.
83 ShadowingAtExitManager at_exit_manager_;
86 class MockDumpProvider : public MemoryDumpProvider {
87 public:
88 MockDumpProvider()
89 : dump_provider_to_register_or_unregister(nullptr),
90 last_session_state_(nullptr) {}
92 // Ctor used by the RespectTaskRunnerAffinity test.
93 explicit MockDumpProvider(
94 const scoped_refptr<SingleThreadTaskRunner>& task_runner)
95 : last_session_state_(nullptr), task_runner_(task_runner) {}
97 virtual ~MockDumpProvider() {}
99 MOCK_METHOD1(OnMemoryDump, bool(ProcessMemoryDump* pmd));
101 // OnMemoryDump() override for the RespectTaskRunnerAffinity test.
102 bool OnMemoryDump_CheckTaskRunner(ProcessMemoryDump* pmd) {
103 EXPECT_TRUE(task_runner_->RunsTasksOnCurrentThread());
104 return true;
107 // OnMemoryDump() override for the SharedSessionState test.
108 bool OnMemoryDump_CheckSessionState(ProcessMemoryDump* pmd) {
109 MemoryDumpSessionState* cur_session_state = pmd->session_state().get();
110 if (last_session_state_)
111 EXPECT_EQ(last_session_state_, cur_session_state);
112 last_session_state_ = cur_session_state;
113 return true;
116 // OnMemoryDump() override for the RegisterDumperWhileDumping test.
117 bool OnMemoryDump_RegisterExtraDumpProvider(ProcessMemoryDump* pmd) {
118 MemoryDumpManager::GetInstance()->RegisterDumpProvider(
119 dump_provider_to_register_or_unregister);
120 return true;
123 // OnMemoryDump() override for the UnegisterDumperWhileDumping test.
124 bool OnMemoryDump_UnregisterDumpProvider(ProcessMemoryDump* pmd) {
125 MemoryDumpManager::GetInstance()->UnregisterDumpProvider(
126 dump_provider_to_register_or_unregister);
127 return true;
130 // Used by OnMemoryDump_(Un)RegisterExtraDumpProvider.
131 MemoryDumpProvider* dump_provider_to_register_or_unregister;
133 private:
134 MemoryDumpSessionState* last_session_state_;
135 scoped_refptr<SingleThreadTaskRunner> task_runner_;
138 TEST_F(MemoryDumpManagerTest, SingleDumper) {
139 MockDumpProvider mdp;
140 mdm_->RegisterDumpProvider(&mdp);
142 // Check that the dumper is not called if the memory category is not enabled.
143 EnableTracing("foo-and-bar-but-not-memory");
144 EXPECT_CALL(mdp, OnMemoryDump(_)).Times(0);
145 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED);
146 DisableTracing();
148 // Now repeat enabling the memory category and check that the dumper is
149 // invoked this time.
150 EnableTracing(kTraceCategory);
151 EXPECT_CALL(mdp, OnMemoryDump(_)).Times(3).WillRepeatedly(Return(true));
152 for (int i = 0; i < 3; ++i)
153 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED);
154 DisableTracing();
156 mdm_->UnregisterDumpProvider(&mdp);
158 // Finally check the unregister logic (no calls to the mdp after unregister).
159 EnableTracing(kTraceCategory);
160 EXPECT_CALL(mdp, OnMemoryDump(_)).Times(0);
161 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED);
162 TraceLog::GetInstance()->SetDisabled();
165 TEST_F(MemoryDumpManagerTest, SharedSessionState) {
166 MockDumpProvider mdp1;
167 MockDumpProvider mdp2;
168 mdm_->RegisterDumpProvider(&mdp1);
169 mdm_->RegisterDumpProvider(&mdp2);
171 EnableTracing(kTraceCategory);
172 EXPECT_CALL(mdp1, OnMemoryDump(_))
173 .Times(2)
174 .WillRepeatedly(
175 Invoke(&mdp1, &MockDumpProvider::OnMemoryDump_CheckSessionState));
176 EXPECT_CALL(mdp2, OnMemoryDump(_))
177 .Times(2)
178 .WillRepeatedly(
179 Invoke(&mdp2, &MockDumpProvider::OnMemoryDump_CheckSessionState));
181 for (int i = 0; i < 2; ++i)
182 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED);
184 DisableTracing();
187 TEST_F(MemoryDumpManagerTest, MultipleDumpers) {
188 MockDumpProvider mdp1;
189 MockDumpProvider mdp2;
191 // Enable only mdp1.
192 mdm_->RegisterDumpProvider(&mdp1);
193 EnableTracing(kTraceCategory);
194 EXPECT_CALL(mdp1, OnMemoryDump(_)).Times(1).WillRepeatedly(Return(true));
195 EXPECT_CALL(mdp2, OnMemoryDump(_)).Times(0);
196 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED);
197 DisableTracing();
199 // Invert: enable mdp1 and disable mdp2.
200 mdm_->UnregisterDumpProvider(&mdp1);
201 mdm_->RegisterDumpProvider(&mdp2);
202 EnableTracing(kTraceCategory);
203 EXPECT_CALL(mdp1, OnMemoryDump(_)).Times(0);
204 EXPECT_CALL(mdp2, OnMemoryDump(_)).Times(1).WillRepeatedly(Return(true));
205 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED);
206 DisableTracing();
208 // Enable both mdp1 and mdp2.
209 mdm_->RegisterDumpProvider(&mdp1);
210 EnableTracing(kTraceCategory);
211 EXPECT_CALL(mdp1, OnMemoryDump(_)).Times(1).WillRepeatedly(Return(true));
212 EXPECT_CALL(mdp2, OnMemoryDump(_)).Times(1).WillRepeatedly(Return(true));
213 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED);
214 DisableTracing();
217 // Checks that the MemoryDumpManager respects the thread affinity when a
218 // MemoryDumpProvider specifies a task_runner(). The test starts creating 8
219 // threads and registering a MemoryDumpProvider on each of them. At each
220 // iteration, one thread is removed, to check the live unregistration logic.
221 TEST_F(MemoryDumpManagerTest, RespectTaskRunnerAffinity) {
222 const uint32 kNumInitialThreads = 8;
224 ScopedVector<Thread> threads;
225 ScopedVector<MockDumpProvider> mdps;
227 // Create the threads and setup the expectations. Given that at each iteration
228 // we will pop out one thread/MemoryDumpProvider, each MDP is supposed to be
229 // invoked a number of times equal to its index.
230 for (uint32 i = kNumInitialThreads; i > 0; --i) {
231 threads.push_back(new Thread("test thread"));
232 threads.back()->Start();
233 mdps.push_back(new MockDumpProvider(threads.back()->task_runner()));
234 MockDumpProvider* mdp = mdps.back();
235 mdm_->RegisterDumpProvider(mdp, threads.back()->task_runner());
236 EXPECT_CALL(*mdp, OnMemoryDump(_))
237 .Times(i)
238 .WillRepeatedly(
239 Invoke(mdp, &MockDumpProvider::OnMemoryDump_CheckTaskRunner));
242 EnableTracing(kTraceCategory);
244 while (!threads.empty()) {
245 last_callback_success_ = false;
247 RunLoop run_loop;
248 MemoryDumpCallback callback =
249 Bind(&MemoryDumpManagerTest::DumpCallbackAdapter, Unretained(this),
250 MessageLoop::current()->task_runner(), run_loop.QuitClosure());
251 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED, callback);
252 // This nested message loop (|run_loop|) will be quit if and only if
253 // the RequestGlobalDump callback is invoked.
254 run_loop.Run();
256 EXPECT_TRUE(last_callback_success_);
258 // Unregister a MDP and destroy one thread at each iteration to check the
259 // live unregistration logic. The unregistration needs to happen on the same
260 // thread the MDP belongs to.
262 RunLoop run_loop;
263 Closure unregistration =
264 Bind(&MemoryDumpManager::UnregisterDumpProvider,
265 Unretained(mdm_.get()), Unretained(mdps.back()));
266 threads.back()->task_runner()->PostTaskAndReply(FROM_HERE, unregistration,
267 run_loop.QuitClosure());
268 run_loop.Run();
270 mdps.pop_back();
271 threads.back()->Stop();
272 threads.pop_back();
275 DisableTracing();
278 // Enable both dump providers, make sure that mdp gets disabled after 3 failures
279 // and not disabled after 1.
280 TEST_F(MemoryDumpManagerTest, DisableFailingDumpers) {
281 MockDumpProvider mdp1;
282 MockDumpProvider mdp2;
284 mdm_->RegisterDumpProvider(&mdp1);
285 mdm_->RegisterDumpProvider(&mdp2);
286 EnableTracing(kTraceCategory);
288 EXPECT_CALL(mdp1, OnMemoryDump(_))
289 .Times(MemoryDumpManager::kMaxConsecutiveFailuresCount)
290 .WillRepeatedly(Return(false));
292 EXPECT_CALL(mdp2, OnMemoryDump(_))
293 .Times(1 + MemoryDumpManager::kMaxConsecutiveFailuresCount)
294 .WillOnce(Return(false))
295 .WillRepeatedly(Return(true));
296 for (int i = 0; i < 1 + MemoryDumpManager::kMaxConsecutiveFailuresCount;
297 i++) {
298 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED);
301 DisableTracing();
304 // Sneakily register an extra memory dump provider while an existing one is
305 // dumping and expect it to take part in the already active tracing session.
306 TEST_F(MemoryDumpManagerTest, RegisterDumperWhileDumping) {
307 MockDumpProvider mdp1;
308 MockDumpProvider mdp2;
310 mdp1.dump_provider_to_register_or_unregister = &mdp2;
311 mdm_->RegisterDumpProvider(&mdp1);
312 EnableTracing(kTraceCategory);
314 EXPECT_CALL(mdp1, OnMemoryDump(_))
315 .Times(4)
316 .WillOnce(Return(true))
317 .WillOnce(Invoke(
318 &mdp1, &MockDumpProvider::OnMemoryDump_RegisterExtraDumpProvider))
319 .WillRepeatedly(Return(true));
321 // Depending on the insertion order (before or after mdp1), mdp2 might be
322 // called also immediately after it gets registered.
323 EXPECT_CALL(mdp2, OnMemoryDump(_))
324 .Times(Between(2, 3))
325 .WillRepeatedly(Return(true));
327 for (int i = 0; i < 4; i++) {
328 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED);
331 DisableTracing();
334 // Like the above, but suddenly unregister the dump provider.
335 TEST_F(MemoryDumpManagerTest, UnregisterDumperWhileDumping) {
336 MockDumpProvider mdp1;
337 MockDumpProvider mdp2;
339 mdm_->RegisterDumpProvider(&mdp1, ThreadTaskRunnerHandle::Get());
340 mdm_->RegisterDumpProvider(&mdp2, ThreadTaskRunnerHandle::Get());
341 mdp1.dump_provider_to_register_or_unregister = &mdp2;
342 EnableTracing(kTraceCategory);
344 EXPECT_CALL(mdp1, OnMemoryDump(_))
345 .Times(4)
346 .WillOnce(Return(true))
347 .WillOnce(Invoke(&mdp1,
348 &MockDumpProvider::OnMemoryDump_UnregisterDumpProvider))
349 .WillRepeatedly(Return(true));
351 // Depending on the insertion order (before or after mdp1), mdp2 might have
352 // been already called when OnMemoryDump_UnregisterDumpProvider happens.
353 EXPECT_CALL(mdp2, OnMemoryDump(_))
354 .Times(Between(1, 2))
355 .WillRepeatedly(Return(true));
357 for (int i = 0; i < 4; i++) {
358 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED);
361 DisableTracing();
364 // Ensures that a NACK callback is invoked if RequestGlobalDump is called when
365 // tracing is not enabled.
366 TEST_F(MemoryDumpManagerTest, CallbackCalledOnFailure) {
367 MockDumpProvider mdp1;
369 mdm_->RegisterDumpProvider(&mdp1);
370 EXPECT_CALL(mdp1, OnMemoryDump(_)).Times(0);
372 last_callback_success_ = true;
374 RunLoop run_loop;
375 MemoryDumpCallback callback =
376 Bind(&MemoryDumpManagerTest::DumpCallbackAdapter, Unretained(this),
377 MessageLoop::current()->task_runner(), run_loop.QuitClosure());
378 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED, callback);
379 run_loop.Run();
381 EXPECT_FALSE(last_callback_success_);
384 } // namespace trace_event
385 } // namespace base