base/threading: remove ScopedTracker placed for experiments
[chromium-blink-merge.git] / base / trace_event / memory_dump_manager_unittest.cc
blob26a226e5ab824e7b08d456a0f5553ef0f82900a8
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; }
36 uint64 GetTracingProcessId() const override {
37 return MemoryDumpManager::kInvalidTracingProcessId;
41 class MemoryDumpManagerTest : public testing::Test {
42 public:
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);
55 mdm_.reset();
56 message_loop_.reset();
57 TraceLog::DeleteForTesting();
60 void DumpCallbackAdapter(scoped_refptr<SingleThreadTaskRunner> task_runner,
61 Closure closure,
62 uint64 dump_guid,
63 bool success) {
64 last_callback_success_ = success;
65 task_runner->PostTask(FROM_HERE, closure);
68 protected:
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_;
81 private:
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 {
90 public:
91 MockDumpProvider()
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());
107 return true;
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;
116 return true;
119 // OnMemoryDump() override for the RegisterDumperWhileDumping test.
120 bool OnMemoryDump_RegisterExtraDumpProvider(ProcessMemoryDump* pmd) {
121 MemoryDumpManager::GetInstance()->RegisterDumpProvider(
122 dump_provider_to_register_or_unregister);
123 return true;
126 // OnMemoryDump() override for the UnegisterDumperWhileDumping test.
127 bool OnMemoryDump_UnregisterDumpProvider(ProcessMemoryDump* pmd) {
128 MemoryDumpManager::GetInstance()->UnregisterDumpProvider(
129 dump_provider_to_register_or_unregister);
130 return true;
133 // Used by OnMemoryDump_(Un)RegisterExtraDumpProvider.
134 MemoryDumpProvider* dump_provider_to_register_or_unregister;
136 private:
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);
149 DisableTracing();
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);
157 DisableTracing();
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(_))
176 .Times(2)
177 .WillRepeatedly(
178 Invoke(&mdp1, &MockDumpProvider::OnMemoryDump_CheckSessionState));
179 EXPECT_CALL(mdp2, OnMemoryDump(_))
180 .Times(2)
181 .WillRepeatedly(
182 Invoke(&mdp2, &MockDumpProvider::OnMemoryDump_CheckSessionState));
184 for (int i = 0; i < 2; ++i)
185 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED);
187 DisableTracing();
190 TEST_F(MemoryDumpManagerTest, MultipleDumpers) {
191 MockDumpProvider mdp1;
192 MockDumpProvider mdp2;
194 // Enable only mdp1.
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);
200 DisableTracing();
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);
209 DisableTracing();
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);
217 DisableTracing();
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(_))
240 .Times(i)
241 .WillRepeatedly(
242 Invoke(mdp, &MockDumpProvider::OnMemoryDump_CheckTaskRunner));
245 EnableTracing(kTraceCategory);
247 while (!threads.empty()) {
248 last_callback_success_ = false;
250 RunLoop run_loop;
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.
257 run_loop.Run();
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.
265 RunLoop run_loop;
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());
271 run_loop.Run();
273 mdps.pop_back();
274 threads.back()->Stop();
275 threads.pop_back();
278 DisableTracing();
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;
300 i++) {
301 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED);
304 DisableTracing();
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(_))
318 .Times(4)
319 .WillOnce(Return(true))
320 .WillOnce(Invoke(
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);
334 DisableTracing();
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(_))
348 .Times(4)
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);
364 DisableTracing();
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;
377 RunLoop run_loop;
378 MemoryDumpCallback callback =
379 Bind(&MemoryDumpManagerTest::DumpCallbackAdapter, Unretained(this),
380 MessageLoop::current()->task_runner(), run_loop.QuitClosure());
381 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED, callback);
382 run_loop.Run();
384 EXPECT_FALSE(last_callback_success_);
387 } // namespace trace_event
388 } // namespace base