SupervisedUser SafeSites: Switch to the new SafeSearch API
[chromium-blink-merge.git] / base / trace_event / memory_dump_manager_unittest.cc
blob9835415f4e7ff0305c5c08d7a1a41e5aaab413c9
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 {
25 namespace {
26 MemoryDumpArgs high_detail_args = {MemoryDumpArgs::LevelOfDetail::HIGH};
27 MemoryDumpArgs low_detail_args = {MemoryDumpArgs::LevelOfDetail::LOW};
30 // Testing MemoryDumpManagerDelegate which short-circuits dump requests locally
31 // instead of performing IPC dances.
32 class MemoryDumpManagerDelegateForTesting : public MemoryDumpManagerDelegate {
33 public:
34 void RequestGlobalMemoryDump(const MemoryDumpRequestArgs& args,
35 const MemoryDumpCallback& callback) override {
36 CreateProcessDump(args, callback);
39 bool IsCoordinatorProcess() const override { return false; }
40 uint64 GetTracingProcessId() const override {
41 return MemoryDumpManager::kInvalidTracingProcessId;
45 class MemoryDumpManagerTest : public testing::Test {
46 public:
47 void SetUp() override {
48 last_callback_success_ = false;
49 message_loop_.reset(new MessageLoop());
50 mdm_.reset(new MemoryDumpManager());
51 MemoryDumpManager::SetInstanceForTesting(mdm_.get());
52 ASSERT_EQ(mdm_, MemoryDumpManager::GetInstance());
53 MemoryDumpManager::GetInstance()->Initialize();
54 MemoryDumpManager::GetInstance()->SetDelegate(&delegate_);
57 void TearDown() override {
58 MemoryDumpManager::SetInstanceForTesting(nullptr);
59 mdm_.reset();
60 message_loop_.reset();
61 TraceLog::DeleteForTesting();
64 void DumpCallbackAdapter(scoped_refptr<SingleThreadTaskRunner> task_runner,
65 Closure closure,
66 uint64 dump_guid,
67 bool success) {
68 last_callback_success_ = success;
69 task_runner->PostTask(FROM_HERE, closure);
72 protected:
73 const char* kTraceCategory = MemoryDumpManager::kTraceCategoryForTesting;
75 void EnableTracing(const char* category) {
76 TraceLog::GetInstance()->SetEnabled(
77 TraceConfig(category, ""), TraceLog::RECORDING_MODE);
80 void DisableTracing() { TraceLog::GetInstance()->SetDisabled(); }
82 scoped_ptr<MemoryDumpManager> mdm_;
83 bool last_callback_success_;
85 private:
86 scoped_ptr<MessageLoop> message_loop_;
87 MemoryDumpManagerDelegateForTesting delegate_;
89 // We want our singleton torn down after each test.
90 ShadowingAtExitManager at_exit_manager_;
93 class MockDumpProvider : public MemoryDumpProvider {
94 public:
95 MockDumpProvider()
96 : dump_provider_to_register_or_unregister(nullptr),
97 last_session_state_(nullptr),
98 level_of_detail_(MemoryDumpArgs::LevelOfDetail::HIGH) {}
100 // Ctor used by the RespectTaskRunnerAffinity test.
101 explicit MockDumpProvider(
102 const scoped_refptr<SingleThreadTaskRunner>& task_runner)
103 : last_session_state_(nullptr),
104 task_runner_(task_runner),
105 level_of_detail_(MemoryDumpArgs::LevelOfDetail::HIGH) {}
107 // Ctor used by CheckMemoryDumpArgs test.
108 explicit MockDumpProvider(const MemoryDumpArgs::LevelOfDetail level_of_detail)
109 : last_session_state_(nullptr), level_of_detail_(level_of_detail) {}
111 virtual ~MockDumpProvider() {}
113 MOCK_METHOD2(OnMemoryDump,
114 bool(const MemoryDumpArgs& args, ProcessMemoryDump* pmd));
116 // OnMemoryDump() override for the RespectTaskRunnerAffinity test.
117 bool OnMemoryDump_CheckTaskRunner(const MemoryDumpArgs& args,
118 ProcessMemoryDump* pmd) {
119 EXPECT_TRUE(task_runner_->RunsTasksOnCurrentThread());
120 return true;
123 // OnMemoryDump() override for the SharedSessionState test.
124 bool OnMemoryDump_CheckSessionState(const MemoryDumpArgs& args,
125 ProcessMemoryDump* pmd) {
126 MemoryDumpSessionState* cur_session_state = pmd->session_state().get();
127 if (last_session_state_)
128 EXPECT_EQ(last_session_state_, cur_session_state);
129 last_session_state_ = cur_session_state;
130 return true;
133 // OnMemoryDump() override for the RegisterDumperWhileDumping test.
134 bool OnMemoryDump_RegisterExtraDumpProvider(const MemoryDumpArgs& args,
135 ProcessMemoryDump* pmd) {
136 MemoryDumpManager::GetInstance()->RegisterDumpProvider(
137 dump_provider_to_register_or_unregister);
138 return true;
141 // OnMemoryDump() override for the UnegisterDumperWhileDumping test.
142 bool OnMemoryDump_UnregisterDumpProvider(const MemoryDumpArgs& args,
143 ProcessMemoryDump* pmd) {
144 MemoryDumpManager::GetInstance()->UnregisterDumpProvider(
145 dump_provider_to_register_or_unregister);
146 return true;
149 // OnMemoryDump() override for the CheckMemoryDumpArgs test.
150 bool OnMemoryDump_CheckMemoryDumpArgs(const MemoryDumpArgs& args,
151 ProcessMemoryDump* pmd) {
152 EXPECT_EQ(level_of_detail_, args.level_of_detail);
153 return true;
156 // Used by OnMemoryDump_(Un)RegisterExtraDumpProvider.
157 MemoryDumpProvider* dump_provider_to_register_or_unregister;
159 private:
160 MemoryDumpSessionState* last_session_state_;
161 scoped_refptr<SingleThreadTaskRunner> task_runner_;
162 const MemoryDumpArgs::LevelOfDetail level_of_detail_;
165 TEST_F(MemoryDumpManagerTest, SingleDumper) {
166 MockDumpProvider mdp;
167 mdm_->RegisterDumpProvider(&mdp);
169 // Check that the dumper is not called if the memory category is not enabled.
170 EnableTracing("foo-and-bar-but-not-memory");
171 EXPECT_CALL(mdp, OnMemoryDump(_, _)).Times(0);
172 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED,
173 high_detail_args);
174 DisableTracing();
176 // Now repeat enabling the memory category and check that the dumper is
177 // invoked this time.
178 EnableTracing(kTraceCategory);
179 EXPECT_CALL(mdp, OnMemoryDump(_, _)).Times(3).WillRepeatedly(Return(true));
180 for (int i = 0; i < 3; ++i)
181 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED,
182 high_detail_args);
183 DisableTracing();
185 mdm_->UnregisterDumpProvider(&mdp);
187 // Finally check the unregister logic (no calls to the mdp after unregister).
188 EnableTracing(kTraceCategory);
189 EXPECT_CALL(mdp, OnMemoryDump(_, _)).Times(0);
190 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED,
191 high_detail_args);
192 TraceLog::GetInstance()->SetDisabled();
195 TEST_F(MemoryDumpManagerTest, CheckMemoryDumpArgs) {
196 // Check that requesting dumps with high level of detail actually propagates
197 // to OnMemoryDump() call on dump providers.
198 MockDumpProvider mdp_high_detail(MemoryDumpArgs::LevelOfDetail::HIGH);
199 mdm_->RegisterDumpProvider(&mdp_high_detail);
201 EnableTracing(kTraceCategory);
202 EXPECT_CALL(mdp_high_detail, OnMemoryDump(_, _))
203 .Times(1)
204 .WillRepeatedly(
205 Invoke(&mdp_high_detail,
206 &MockDumpProvider::OnMemoryDump_CheckMemoryDumpArgs));
207 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED,
208 high_detail_args);
209 DisableTracing();
210 mdm_->UnregisterDumpProvider(&mdp_high_detail);
212 // Check that requesting dumps with low level of detail actually propagates to
213 // OnMemoryDump() call on dump providers.
214 MockDumpProvider mdp_low_detail(MemoryDumpArgs::LevelOfDetail::LOW);
215 mdm_->RegisterDumpProvider(&mdp_low_detail);
217 EnableTracing(kTraceCategory);
218 EXPECT_CALL(mdp_low_detail, OnMemoryDump(_, _))
219 .Times(1)
220 .WillRepeatedly(
221 Invoke(&mdp_low_detail,
222 &MockDumpProvider::OnMemoryDump_CheckMemoryDumpArgs));
223 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED,
224 low_detail_args);
225 DisableTracing();
226 mdm_->UnregisterDumpProvider(&mdp_low_detail);
229 TEST_F(MemoryDumpManagerTest, SharedSessionState) {
230 MockDumpProvider mdp1;
231 MockDumpProvider mdp2;
232 mdm_->RegisterDumpProvider(&mdp1);
233 mdm_->RegisterDumpProvider(&mdp2);
235 EnableTracing(kTraceCategory);
236 EXPECT_CALL(mdp1, OnMemoryDump(_, _))
237 .Times(2)
238 .WillRepeatedly(
239 Invoke(&mdp1, &MockDumpProvider::OnMemoryDump_CheckSessionState));
240 EXPECT_CALL(mdp2, OnMemoryDump(_, _))
241 .Times(2)
242 .WillRepeatedly(
243 Invoke(&mdp2, &MockDumpProvider::OnMemoryDump_CheckSessionState));
245 for (int i = 0; i < 2; ++i)
246 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED,
247 high_detail_args);
249 DisableTracing();
252 TEST_F(MemoryDumpManagerTest, MultipleDumpers) {
253 MockDumpProvider mdp1;
254 MockDumpProvider mdp2;
256 // Enable only mdp1.
257 mdm_->RegisterDumpProvider(&mdp1);
258 EnableTracing(kTraceCategory);
259 EXPECT_CALL(mdp1, OnMemoryDump(_, _)).Times(1).WillRepeatedly(Return(true));
260 EXPECT_CALL(mdp2, OnMemoryDump(_, _)).Times(0);
261 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED,
262 high_detail_args);
263 DisableTracing();
265 // Invert: enable mdp1 and disable mdp2.
266 mdm_->UnregisterDumpProvider(&mdp1);
267 mdm_->RegisterDumpProvider(&mdp2);
268 EnableTracing(kTraceCategory);
269 EXPECT_CALL(mdp1, OnMemoryDump(_, _)).Times(0);
270 EXPECT_CALL(mdp2, OnMemoryDump(_, _)).Times(1).WillRepeatedly(Return(true));
271 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED,
272 high_detail_args);
273 DisableTracing();
275 // Enable both mdp1 and mdp2.
276 mdm_->RegisterDumpProvider(&mdp1);
277 EnableTracing(kTraceCategory);
278 EXPECT_CALL(mdp1, OnMemoryDump(_, _)).Times(1).WillRepeatedly(Return(true));
279 EXPECT_CALL(mdp2, OnMemoryDump(_, _)).Times(1).WillRepeatedly(Return(true));
280 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED,
281 high_detail_args);
282 DisableTracing();
285 // Checks that the MemoryDumpManager respects the thread affinity when a
286 // MemoryDumpProvider specifies a task_runner(). The test starts creating 8
287 // threads and registering a MemoryDumpProvider on each of them. At each
288 // iteration, one thread is removed, to check the live unregistration logic.
289 TEST_F(MemoryDumpManagerTest, RespectTaskRunnerAffinity) {
290 const uint32 kNumInitialThreads = 8;
292 ScopedVector<Thread> threads;
293 ScopedVector<MockDumpProvider> mdps;
295 // Create the threads and setup the expectations. Given that at each iteration
296 // we will pop out one thread/MemoryDumpProvider, each MDP is supposed to be
297 // invoked a number of times equal to its index.
298 for (uint32 i = kNumInitialThreads; i > 0; --i) {
299 threads.push_back(new Thread("test thread"));
300 threads.back()->Start();
301 mdps.push_back(new MockDumpProvider(threads.back()->task_runner()));
302 MockDumpProvider* mdp = mdps.back();
303 mdm_->RegisterDumpProvider(mdp, threads.back()->task_runner());
304 EXPECT_CALL(*mdp, OnMemoryDump(_, _))
305 .Times(i)
306 .WillRepeatedly(
307 Invoke(mdp, &MockDumpProvider::OnMemoryDump_CheckTaskRunner));
310 EnableTracing(kTraceCategory);
312 while (!threads.empty()) {
313 last_callback_success_ = false;
315 RunLoop run_loop;
316 MemoryDumpCallback callback =
317 Bind(&MemoryDumpManagerTest::DumpCallbackAdapter, Unretained(this),
318 MessageLoop::current()->task_runner(), run_loop.QuitClosure());
319 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED,
320 high_detail_args, callback);
321 // This nested message loop (|run_loop|) will be quit if and only if
322 // the RequestGlobalDump callback is invoked.
323 run_loop.Run();
325 EXPECT_TRUE(last_callback_success_);
327 // Unregister a MDP and destroy one thread at each iteration to check the
328 // live unregistration logic. The unregistration needs to happen on the same
329 // thread the MDP belongs to.
331 RunLoop run_loop;
332 Closure unregistration =
333 Bind(&MemoryDumpManager::UnregisterDumpProvider,
334 Unretained(mdm_.get()), Unretained(mdps.back()));
335 threads.back()->task_runner()->PostTaskAndReply(FROM_HERE, unregistration,
336 run_loop.QuitClosure());
337 run_loop.Run();
339 mdps.pop_back();
340 threads.back()->Stop();
341 threads.pop_back();
344 DisableTracing();
347 // Enable both dump providers, make sure that mdp gets disabled after 3 failures
348 // and not disabled after 1.
349 TEST_F(MemoryDumpManagerTest, DisableFailingDumpers) {
350 MockDumpProvider mdp1;
351 MockDumpProvider mdp2;
353 mdm_->RegisterDumpProvider(&mdp1);
354 mdm_->RegisterDumpProvider(&mdp2);
355 EnableTracing(kTraceCategory);
357 EXPECT_CALL(mdp1, OnMemoryDump(_, _))
358 .Times(MemoryDumpManager::kMaxConsecutiveFailuresCount)
359 .WillRepeatedly(Return(false));
361 EXPECT_CALL(mdp2, OnMemoryDump(_, _))
362 .Times(1 + MemoryDumpManager::kMaxConsecutiveFailuresCount)
363 .WillOnce(Return(false))
364 .WillRepeatedly(Return(true));
365 for (int i = 0; i < 1 + MemoryDumpManager::kMaxConsecutiveFailuresCount;
366 i++) {
367 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED,
368 high_detail_args);
371 DisableTracing();
374 // Sneakily register an extra memory dump provider while an existing one is
375 // dumping and expect it to take part in the already active tracing session.
376 TEST_F(MemoryDumpManagerTest, RegisterDumperWhileDumping) {
377 MockDumpProvider mdp1;
378 MockDumpProvider mdp2;
380 mdp1.dump_provider_to_register_or_unregister = &mdp2;
381 mdm_->RegisterDumpProvider(&mdp1);
382 EnableTracing(kTraceCategory);
384 EXPECT_CALL(mdp1, OnMemoryDump(_, _))
385 .Times(4)
386 .WillOnce(Return(true))
387 .WillOnce(Invoke(
388 &mdp1, &MockDumpProvider::OnMemoryDump_RegisterExtraDumpProvider))
389 .WillRepeatedly(Return(true));
391 // Depending on the insertion order (before or after mdp1), mdp2 might be
392 // called also immediately after it gets registered.
393 EXPECT_CALL(mdp2, OnMemoryDump(_, _))
394 .Times(Between(2, 3))
395 .WillRepeatedly(Return(true));
397 for (int i = 0; i < 4; i++) {
398 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED,
399 high_detail_args);
402 DisableTracing();
405 // Like the above, but suddenly unregister the dump provider.
406 TEST_F(MemoryDumpManagerTest, UnregisterDumperWhileDumping) {
407 MockDumpProvider mdp1;
408 MockDumpProvider mdp2;
410 mdm_->RegisterDumpProvider(&mdp1, ThreadTaskRunnerHandle::Get());
411 mdm_->RegisterDumpProvider(&mdp2, ThreadTaskRunnerHandle::Get());
412 mdp1.dump_provider_to_register_or_unregister = &mdp2;
413 EnableTracing(kTraceCategory);
415 EXPECT_CALL(mdp1, OnMemoryDump(_, _))
416 .Times(4)
417 .WillOnce(Return(true))
418 .WillOnce(
419 Invoke(&mdp1, &MockDumpProvider::OnMemoryDump_UnregisterDumpProvider))
420 .WillRepeatedly(Return(true));
422 // Depending on the insertion order (before or after mdp1), mdp2 might have
423 // been already called when OnMemoryDump_UnregisterDumpProvider happens.
424 EXPECT_CALL(mdp2, OnMemoryDump(_, _))
425 .Times(Between(1, 2))
426 .WillRepeatedly(Return(true));
428 for (int i = 0; i < 4; i++) {
429 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED,
430 high_detail_args);
433 DisableTracing();
436 // Ensures that a NACK callback is invoked if RequestGlobalDump is called when
437 // tracing is not enabled.
438 TEST_F(MemoryDumpManagerTest, CallbackCalledOnFailure) {
439 MockDumpProvider mdp1;
441 mdm_->RegisterDumpProvider(&mdp1);
442 EXPECT_CALL(mdp1, OnMemoryDump(_, _)).Times(0);
444 last_callback_success_ = true;
446 RunLoop run_loop;
447 MemoryDumpCallback callback =
448 Bind(&MemoryDumpManagerTest::DumpCallbackAdapter, Unretained(this),
449 MessageLoop::current()->task_runner(), run_loop.QuitClosure());
450 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED,
451 high_detail_args, callback);
452 run_loop.Run();
454 EXPECT_FALSE(last_callback_success_);
457 } // namespace trace_event
458 } // namespace base