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"
19 using testing::Between
;
20 using testing::Invoke
;
21 using testing::Return
;
24 namespace trace_event
{
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
{
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
{
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);
60 message_loop_
.reset();
61 TraceLog::DeleteForTesting();
64 void DumpCallbackAdapter(scoped_refptr
<SingleThreadTaskRunner
> task_runner
,
68 last_callback_success_
= success
;
69 task_runner
->PostTask(FROM_HERE
, closure
);
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_
;
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
{
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());
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
;
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
);
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
);
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
);
156 // Used by OnMemoryDump_(Un)RegisterExtraDumpProvider.
157 MemoryDumpProvider
* dump_provider_to_register_or_unregister
;
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
,
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
,
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
,
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(_
, _
))
205 Invoke(&mdp_high_detail
,
206 &MockDumpProvider::OnMemoryDump_CheckMemoryDumpArgs
));
207 mdm_
->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED
,
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(_
, _
))
221 Invoke(&mdp_low_detail
,
222 &MockDumpProvider::OnMemoryDump_CheckMemoryDumpArgs
));
223 mdm_
->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED
,
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(_
, _
))
239 Invoke(&mdp1
, &MockDumpProvider::OnMemoryDump_CheckSessionState
));
240 EXPECT_CALL(mdp2
, OnMemoryDump(_
, _
))
243 Invoke(&mdp2
, &MockDumpProvider::OnMemoryDump_CheckSessionState
));
245 for (int i
= 0; i
< 2; ++i
)
246 mdm_
->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED
,
252 TEST_F(MemoryDumpManagerTest
, MultipleDumpers
) {
253 MockDumpProvider mdp1
;
254 MockDumpProvider mdp2
;
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
,
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
,
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
,
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(_
, _
))
307 Invoke(mdp
, &MockDumpProvider::OnMemoryDump_CheckTaskRunner
));
310 EnableTracing(kTraceCategory
);
312 while (!threads
.empty()) {
313 last_callback_success_
= false;
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.
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.
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());
340 threads
.back()->Stop();
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
;
367 mdm_
->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED
,
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(_
, _
))
386 .WillOnce(Return(true))
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
,
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(_
, _
))
417 .WillOnce(Return(true))
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
,
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;
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
);
454 EXPECT_FALSE(last_callback_success_
);
457 } // namespace trace_event