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; }
36 uint64
GetTracingProcessId() const override
{
37 return MemoryDumpManager::kInvalidTracingProcessId
;
41 class MemoryDumpManagerTest
: public testing::Test
{
43 void SetUp() override
{
44 last_callback_success_
= false;
45 message_loop_
.reset(new MessageLoop());
46 mdm_
.reset(new MemoryDumpManager());
47 MemoryDumpManager::SetInstanceForTesting(mdm_
.get());
48 ASSERT_EQ(mdm_
, MemoryDumpManager::GetInstance());
49 MemoryDumpManager::GetInstance()->Initialize();
50 MemoryDumpManager::GetInstance()->SetDelegate(&delegate_
);
53 void TearDown() override
{
54 MemoryDumpManager::SetInstanceForTesting(nullptr);
56 message_loop_
.reset();
57 TraceLog::DeleteForTesting();
60 void DumpCallbackAdapter(scoped_refptr
<SingleThreadTaskRunner
> task_runner
,
64 last_callback_success_
= success
;
65 task_runner
->PostTask(FROM_HERE
, closure
);
69 const char* kTraceCategory
= MemoryDumpManager::kTraceCategoryForTesting
;
71 void EnableTracing(const char* category
) {
72 TraceLog::GetInstance()->SetEnabled(
73 TraceConfig(category
, ""), TraceLog::RECORDING_MODE
);
76 void DisableTracing() { TraceLog::GetInstance()->SetDisabled(); }
78 scoped_ptr
<MemoryDumpManager
> mdm_
;
79 bool last_callback_success_
;
82 scoped_ptr
<MessageLoop
> message_loop_
;
83 MemoryDumpManagerDelegateForTesting delegate_
;
85 // We want our singleton torn down after each test.
86 ShadowingAtExitManager at_exit_manager_
;
89 class MockDumpProvider
: public MemoryDumpProvider
{
92 : dump_provider_to_register_or_unregister(nullptr),
93 last_session_state_(nullptr) {}
95 // Ctor used by the RespectTaskRunnerAffinity test.
96 explicit MockDumpProvider(
97 const scoped_refptr
<SingleThreadTaskRunner
>& task_runner
)
98 : last_session_state_(nullptr), task_runner_(task_runner
) {}
100 virtual ~MockDumpProvider() {}
102 MOCK_METHOD1(OnMemoryDump
, bool(ProcessMemoryDump
* pmd
));
104 // OnMemoryDump() override for the RespectTaskRunnerAffinity test.
105 bool OnMemoryDump_CheckTaskRunner(ProcessMemoryDump
* pmd
) {
106 EXPECT_TRUE(task_runner_
->RunsTasksOnCurrentThread());
110 // OnMemoryDump() override for the SharedSessionState test.
111 bool OnMemoryDump_CheckSessionState(ProcessMemoryDump
* pmd
) {
112 MemoryDumpSessionState
* cur_session_state
= pmd
->session_state().get();
113 if (last_session_state_
)
114 EXPECT_EQ(last_session_state_
, cur_session_state
);
115 last_session_state_
= cur_session_state
;
119 // OnMemoryDump() override for the RegisterDumperWhileDumping test.
120 bool OnMemoryDump_RegisterExtraDumpProvider(ProcessMemoryDump
* pmd
) {
121 MemoryDumpManager::GetInstance()->RegisterDumpProvider(
122 dump_provider_to_register_or_unregister
);
126 // OnMemoryDump() override for the UnegisterDumperWhileDumping test.
127 bool OnMemoryDump_UnregisterDumpProvider(ProcessMemoryDump
* pmd
) {
128 MemoryDumpManager::GetInstance()->UnregisterDumpProvider(
129 dump_provider_to_register_or_unregister
);
133 // Used by OnMemoryDump_(Un)RegisterExtraDumpProvider.
134 MemoryDumpProvider
* dump_provider_to_register_or_unregister
;
137 MemoryDumpSessionState
* last_session_state_
;
138 scoped_refptr
<SingleThreadTaskRunner
> task_runner_
;
141 TEST_F(MemoryDumpManagerTest
, SingleDumper
) {
142 MockDumpProvider mdp
;
143 mdm_
->RegisterDumpProvider(&mdp
);
145 // Check that the dumper is not called if the memory category is not enabled.
146 EnableTracing("foo-and-bar-but-not-memory");
147 EXPECT_CALL(mdp
, OnMemoryDump(_
)).Times(0);
148 mdm_
->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED
);
151 // Now repeat enabling the memory category and check that the dumper is
152 // invoked this time.
153 EnableTracing(kTraceCategory
);
154 EXPECT_CALL(mdp
, OnMemoryDump(_
)).Times(3).WillRepeatedly(Return(true));
155 for (int i
= 0; i
< 3; ++i
)
156 mdm_
->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED
);
159 mdm_
->UnregisterDumpProvider(&mdp
);
161 // Finally check the unregister logic (no calls to the mdp after unregister).
162 EnableTracing(kTraceCategory
);
163 EXPECT_CALL(mdp
, OnMemoryDump(_
)).Times(0);
164 mdm_
->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED
);
165 TraceLog::GetInstance()->SetDisabled();
168 TEST_F(MemoryDumpManagerTest
, SharedSessionState
) {
169 MockDumpProvider mdp1
;
170 MockDumpProvider mdp2
;
171 mdm_
->RegisterDumpProvider(&mdp1
);
172 mdm_
->RegisterDumpProvider(&mdp2
);
174 EnableTracing(kTraceCategory
);
175 EXPECT_CALL(mdp1
, OnMemoryDump(_
))
178 Invoke(&mdp1
, &MockDumpProvider::OnMemoryDump_CheckSessionState
));
179 EXPECT_CALL(mdp2
, OnMemoryDump(_
))
182 Invoke(&mdp2
, &MockDumpProvider::OnMemoryDump_CheckSessionState
));
184 for (int i
= 0; i
< 2; ++i
)
185 mdm_
->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED
);
190 TEST_F(MemoryDumpManagerTest
, MultipleDumpers
) {
191 MockDumpProvider mdp1
;
192 MockDumpProvider mdp2
;
195 mdm_
->RegisterDumpProvider(&mdp1
);
196 EnableTracing(kTraceCategory
);
197 EXPECT_CALL(mdp1
, OnMemoryDump(_
)).Times(1).WillRepeatedly(Return(true));
198 EXPECT_CALL(mdp2
, OnMemoryDump(_
)).Times(0);
199 mdm_
->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED
);
202 // Invert: enable mdp1 and disable mdp2.
203 mdm_
->UnregisterDumpProvider(&mdp1
);
204 mdm_
->RegisterDumpProvider(&mdp2
);
205 EnableTracing(kTraceCategory
);
206 EXPECT_CALL(mdp1
, OnMemoryDump(_
)).Times(0);
207 EXPECT_CALL(mdp2
, OnMemoryDump(_
)).Times(1).WillRepeatedly(Return(true));
208 mdm_
->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED
);
211 // Enable both mdp1 and mdp2.
212 mdm_
->RegisterDumpProvider(&mdp1
);
213 EnableTracing(kTraceCategory
);
214 EXPECT_CALL(mdp1
, OnMemoryDump(_
)).Times(1).WillRepeatedly(Return(true));
215 EXPECT_CALL(mdp2
, OnMemoryDump(_
)).Times(1).WillRepeatedly(Return(true));
216 mdm_
->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED
);
220 // Checks that the MemoryDumpManager respects the thread affinity when a
221 // MemoryDumpProvider specifies a task_runner(). The test starts creating 8
222 // threads and registering a MemoryDumpProvider on each of them. At each
223 // iteration, one thread is removed, to check the live unregistration logic.
224 TEST_F(MemoryDumpManagerTest
, RespectTaskRunnerAffinity
) {
225 const uint32 kNumInitialThreads
= 8;
227 ScopedVector
<Thread
> threads
;
228 ScopedVector
<MockDumpProvider
> mdps
;
230 // Create the threads and setup the expectations. Given that at each iteration
231 // we will pop out one thread/MemoryDumpProvider, each MDP is supposed to be
232 // invoked a number of times equal to its index.
233 for (uint32 i
= kNumInitialThreads
; i
> 0; --i
) {
234 threads
.push_back(new Thread("test thread"));
235 threads
.back()->Start();
236 mdps
.push_back(new MockDumpProvider(threads
.back()->task_runner()));
237 MockDumpProvider
* mdp
= mdps
.back();
238 mdm_
->RegisterDumpProvider(mdp
, threads
.back()->task_runner());
239 EXPECT_CALL(*mdp
, OnMemoryDump(_
))
242 Invoke(mdp
, &MockDumpProvider::OnMemoryDump_CheckTaskRunner
));
245 EnableTracing(kTraceCategory
);
247 while (!threads
.empty()) {
248 last_callback_success_
= false;
251 MemoryDumpCallback callback
=
252 Bind(&MemoryDumpManagerTest::DumpCallbackAdapter
, Unretained(this),
253 MessageLoop::current()->task_runner(), run_loop
.QuitClosure());
254 mdm_
->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED
, callback
);
255 // This nested message loop (|run_loop|) will be quit if and only if
256 // the RequestGlobalDump callback is invoked.
259 EXPECT_TRUE(last_callback_success_
);
261 // Unregister a MDP and destroy one thread at each iteration to check the
262 // live unregistration logic. The unregistration needs to happen on the same
263 // thread the MDP belongs to.
266 Closure unregistration
=
267 Bind(&MemoryDumpManager::UnregisterDumpProvider
,
268 Unretained(mdm_
.get()), Unretained(mdps
.back()));
269 threads
.back()->task_runner()->PostTaskAndReply(FROM_HERE
, unregistration
,
270 run_loop
.QuitClosure());
274 threads
.back()->Stop();
281 // Enable both dump providers, make sure that mdp gets disabled after 3 failures
282 // and not disabled after 1.
283 TEST_F(MemoryDumpManagerTest
, DisableFailingDumpers
) {
284 MockDumpProvider mdp1
;
285 MockDumpProvider mdp2
;
287 mdm_
->RegisterDumpProvider(&mdp1
);
288 mdm_
->RegisterDumpProvider(&mdp2
);
289 EnableTracing(kTraceCategory
);
291 EXPECT_CALL(mdp1
, OnMemoryDump(_
))
292 .Times(MemoryDumpManager::kMaxConsecutiveFailuresCount
)
293 .WillRepeatedly(Return(false));
295 EXPECT_CALL(mdp2
, OnMemoryDump(_
))
296 .Times(1 + MemoryDumpManager::kMaxConsecutiveFailuresCount
)
297 .WillOnce(Return(false))
298 .WillRepeatedly(Return(true));
299 for (int i
= 0; i
< 1 + MemoryDumpManager::kMaxConsecutiveFailuresCount
;
301 mdm_
->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED
);
307 // Sneakily register an extra memory dump provider while an existing one is
308 // dumping and expect it to take part in the already active tracing session.
309 TEST_F(MemoryDumpManagerTest
, RegisterDumperWhileDumping
) {
310 MockDumpProvider mdp1
;
311 MockDumpProvider mdp2
;
313 mdp1
.dump_provider_to_register_or_unregister
= &mdp2
;
314 mdm_
->RegisterDumpProvider(&mdp1
);
315 EnableTracing(kTraceCategory
);
317 EXPECT_CALL(mdp1
, OnMemoryDump(_
))
319 .WillOnce(Return(true))
321 &mdp1
, &MockDumpProvider::OnMemoryDump_RegisterExtraDumpProvider
))
322 .WillRepeatedly(Return(true));
324 // Depending on the insertion order (before or after mdp1), mdp2 might be
325 // called also immediately after it gets registered.
326 EXPECT_CALL(mdp2
, OnMemoryDump(_
))
327 .Times(Between(2, 3))
328 .WillRepeatedly(Return(true));
330 for (int i
= 0; i
< 4; i
++) {
331 mdm_
->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED
);
337 // Like the above, but suddenly unregister the dump provider.
338 TEST_F(MemoryDumpManagerTest
, UnregisterDumperWhileDumping
) {
339 MockDumpProvider mdp1
;
340 MockDumpProvider mdp2
;
342 mdm_
->RegisterDumpProvider(&mdp1
, ThreadTaskRunnerHandle::Get());
343 mdm_
->RegisterDumpProvider(&mdp2
, ThreadTaskRunnerHandle::Get());
344 mdp1
.dump_provider_to_register_or_unregister
= &mdp2
;
345 EnableTracing(kTraceCategory
);
347 EXPECT_CALL(mdp1
, OnMemoryDump(_
))
349 .WillOnce(Return(true))
350 .WillOnce(Invoke(&mdp1
,
351 &MockDumpProvider::OnMemoryDump_UnregisterDumpProvider
))
352 .WillRepeatedly(Return(true));
354 // Depending on the insertion order (before or after mdp1), mdp2 might have
355 // been already called when OnMemoryDump_UnregisterDumpProvider happens.
356 EXPECT_CALL(mdp2
, OnMemoryDump(_
))
357 .Times(Between(1, 2))
358 .WillRepeatedly(Return(true));
360 for (int i
= 0; i
< 4; i
++) {
361 mdm_
->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED
);
367 // Ensures that a NACK callback is invoked if RequestGlobalDump is called when
368 // tracing is not enabled.
369 TEST_F(MemoryDumpManagerTest
, CallbackCalledOnFailure
) {
370 MockDumpProvider mdp1
;
372 mdm_
->RegisterDumpProvider(&mdp1
);
373 EXPECT_CALL(mdp1
, OnMemoryDump(_
)).Times(0);
375 last_callback_success_
= true;
378 MemoryDumpCallback callback
=
379 Bind(&MemoryDumpManagerTest::DumpCallbackAdapter
, Unretained(this),
380 MessageLoop::current()->task_runner(), run_loop
.QuitClosure());
381 mdm_
->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED
, callback
);
384 EXPECT_FALSE(last_callback_success_
);
387 } // namespace trace_event