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 // Testing MemoryDumpManagerDelegate which short-circuits dump requests locally
27 // instead of performing IPC dances.
28 class MemoryDumpManagerDelegateForTesting
: public MemoryDumpManagerDelegate
{
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
{
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);
53 message_loop_
.reset();
54 TraceLog::DeleteForTesting();
57 void DumpCallbackAdapter(scoped_refptr
<SingleThreadTaskRunner
> task_runner
,
61 last_callback_success_
= success
;
62 task_runner
->PostTask(FROM_HERE
, closure
);
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_
;
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
{
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());
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
;
116 // OnMemoryDump() override for the RegisterDumperWhileDumping test.
117 bool OnMemoryDump_RegisterExtraDumpProvider(ProcessMemoryDump
* pmd
) {
118 MemoryDumpManager::GetInstance()->RegisterDumpProvider(
119 dump_provider_to_register_or_unregister
);
123 // OnMemoryDump() override for the UnegisterDumperWhileDumping test.
124 bool OnMemoryDump_UnregisterDumpProvider(ProcessMemoryDump
* pmd
) {
125 MemoryDumpManager::GetInstance()->UnregisterDumpProvider(
126 dump_provider_to_register_or_unregister
);
130 // Used by OnMemoryDump_(Un)RegisterExtraDumpProvider.
131 MemoryDumpProvider
* dump_provider_to_register_or_unregister
;
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
);
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
);
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(_
))
175 Invoke(&mdp1
, &MockDumpProvider::OnMemoryDump_CheckSessionState
));
176 EXPECT_CALL(mdp2
, OnMemoryDump(_
))
179 Invoke(&mdp2
, &MockDumpProvider::OnMemoryDump_CheckSessionState
));
181 for (int i
= 0; i
< 2; ++i
)
182 mdm_
->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED
);
187 TEST_F(MemoryDumpManagerTest
, MultipleDumpers
) {
188 MockDumpProvider mdp1
;
189 MockDumpProvider mdp2
;
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
);
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
);
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
);
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(_
))
239 Invoke(mdp
, &MockDumpProvider::OnMemoryDump_CheckTaskRunner
));
242 EnableTracing(kTraceCategory
);
244 while (!threads
.empty()) {
245 last_callback_success_
= false;
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.
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.
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());
271 threads
.back()->Stop();
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
;
298 mdm_
->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED
);
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(_
))
316 .WillOnce(Return(true))
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
);
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(_
))
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
);
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;
375 MemoryDumpCallback callback
=
376 Bind(&MemoryDumpManagerTest::DumpCallbackAdapter
, Unretained(this),
377 MessageLoop::current()->task_runner(), run_loop
.QuitClosure());
378 mdm_
->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED
, callback
);
381 EXPECT_FALSE(last_callback_success_
);
384 } // namespace trace_event