Add ICU message format support
[chromium-blink-merge.git] / base / trace_event / memory_dump_manager_unittest.cc
blob2fcdff6b8f931e4c0d48026079878af24776c404
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_METHOD2(OnMemoryDump,
103 bool(const MemoryDumpArgs& args, ProcessMemoryDump* pmd));
105 // OnMemoryDump() override for the RespectTaskRunnerAffinity test.
106 bool OnMemoryDump_CheckTaskRunner(const MemoryDumpArgs& args,
107 ProcessMemoryDump* pmd) {
108 EXPECT_TRUE(task_runner_->RunsTasksOnCurrentThread());
109 return true;
112 // OnMemoryDump() override for the SharedSessionState test.
113 bool OnMemoryDump_CheckSessionState(const MemoryDumpArgs& args,
114 ProcessMemoryDump* pmd) {
115 MemoryDumpSessionState* cur_session_state = pmd->session_state().get();
116 if (last_session_state_)
117 EXPECT_EQ(last_session_state_, cur_session_state);
118 last_session_state_ = cur_session_state;
119 return true;
122 // OnMemoryDump() override for the RegisterDumperWhileDumping test.
123 bool OnMemoryDump_RegisterExtraDumpProvider(const MemoryDumpArgs& args,
124 ProcessMemoryDump* pmd) {
125 MemoryDumpManager::GetInstance()->RegisterDumpProvider(
126 dump_provider_to_register_or_unregister);
127 return true;
130 // OnMemoryDump() override for the UnegisterDumperWhileDumping test.
131 bool OnMemoryDump_UnregisterDumpProvider(const MemoryDumpArgs& args,
132 ProcessMemoryDump* pmd) {
133 MemoryDumpManager::GetInstance()->UnregisterDumpProvider(
134 dump_provider_to_register_or_unregister);
135 return true;
138 // Used by OnMemoryDump_(Un)RegisterExtraDumpProvider.
139 MemoryDumpProvider* dump_provider_to_register_or_unregister;
141 private:
142 MemoryDumpSessionState* last_session_state_;
143 scoped_refptr<SingleThreadTaskRunner> task_runner_;
146 TEST_F(MemoryDumpManagerTest, SingleDumper) {
147 MockDumpProvider mdp;
148 mdm_->RegisterDumpProvider(&mdp);
150 // Check that the dumper is not called if the memory category is not enabled.
151 EnableTracing("foo-and-bar-but-not-memory");
152 EXPECT_CALL(mdp, OnMemoryDump(_, _)).Times(0);
153 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED);
154 DisableTracing();
156 // Now repeat enabling the memory category and check that the dumper is
157 // invoked this time.
158 EnableTracing(kTraceCategory);
159 EXPECT_CALL(mdp, OnMemoryDump(_, _)).Times(3).WillRepeatedly(Return(true));
160 for (int i = 0; i < 3; ++i)
161 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED);
162 DisableTracing();
164 mdm_->UnregisterDumpProvider(&mdp);
166 // Finally check the unregister logic (no calls to the mdp after unregister).
167 EnableTracing(kTraceCategory);
168 EXPECT_CALL(mdp, OnMemoryDump(_, _)).Times(0);
169 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED);
170 TraceLog::GetInstance()->SetDisabled();
173 TEST_F(MemoryDumpManagerTest, SharedSessionState) {
174 MockDumpProvider mdp1;
175 MockDumpProvider mdp2;
176 mdm_->RegisterDumpProvider(&mdp1);
177 mdm_->RegisterDumpProvider(&mdp2);
179 EnableTracing(kTraceCategory);
180 EXPECT_CALL(mdp1, OnMemoryDump(_, _))
181 .Times(2)
182 .WillRepeatedly(
183 Invoke(&mdp1, &MockDumpProvider::OnMemoryDump_CheckSessionState));
184 EXPECT_CALL(mdp2, OnMemoryDump(_, _))
185 .Times(2)
186 .WillRepeatedly(
187 Invoke(&mdp2, &MockDumpProvider::OnMemoryDump_CheckSessionState));
189 for (int i = 0; i < 2; ++i)
190 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED);
192 DisableTracing();
195 TEST_F(MemoryDumpManagerTest, MultipleDumpers) {
196 MockDumpProvider mdp1;
197 MockDumpProvider mdp2;
199 // Enable only mdp1.
200 mdm_->RegisterDumpProvider(&mdp1);
201 EnableTracing(kTraceCategory);
202 EXPECT_CALL(mdp1, OnMemoryDump(_, _)).Times(1).WillRepeatedly(Return(true));
203 EXPECT_CALL(mdp2, OnMemoryDump(_, _)).Times(0);
204 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED);
205 DisableTracing();
207 // Invert: enable mdp1 and disable mdp2.
208 mdm_->UnregisterDumpProvider(&mdp1);
209 mdm_->RegisterDumpProvider(&mdp2);
210 EnableTracing(kTraceCategory);
211 EXPECT_CALL(mdp1, OnMemoryDump(_, _)).Times(0);
212 EXPECT_CALL(mdp2, OnMemoryDump(_, _)).Times(1).WillRepeatedly(Return(true));
213 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED);
214 DisableTracing();
216 // Enable both mdp1 and mdp2.
217 mdm_->RegisterDumpProvider(&mdp1);
218 EnableTracing(kTraceCategory);
219 EXPECT_CALL(mdp1, OnMemoryDump(_, _)).Times(1).WillRepeatedly(Return(true));
220 EXPECT_CALL(mdp2, OnMemoryDump(_, _)).Times(1).WillRepeatedly(Return(true));
221 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED);
222 DisableTracing();
225 // Checks that the MemoryDumpManager respects the thread affinity when a
226 // MemoryDumpProvider specifies a task_runner(). The test starts creating 8
227 // threads and registering a MemoryDumpProvider on each of them. At each
228 // iteration, one thread is removed, to check the live unregistration logic.
229 TEST_F(MemoryDumpManagerTest, RespectTaskRunnerAffinity) {
230 const uint32 kNumInitialThreads = 8;
232 ScopedVector<Thread> threads;
233 ScopedVector<MockDumpProvider> mdps;
235 // Create the threads and setup the expectations. Given that at each iteration
236 // we will pop out one thread/MemoryDumpProvider, each MDP is supposed to be
237 // invoked a number of times equal to its index.
238 for (uint32 i = kNumInitialThreads; i > 0; --i) {
239 threads.push_back(new Thread("test thread"));
240 threads.back()->Start();
241 mdps.push_back(new MockDumpProvider(threads.back()->task_runner()));
242 MockDumpProvider* mdp = mdps.back();
243 mdm_->RegisterDumpProvider(mdp, threads.back()->task_runner());
244 EXPECT_CALL(*mdp, OnMemoryDump(_, _))
245 .Times(i)
246 .WillRepeatedly(
247 Invoke(mdp, &MockDumpProvider::OnMemoryDump_CheckTaskRunner));
250 EnableTracing(kTraceCategory);
252 while (!threads.empty()) {
253 last_callback_success_ = false;
255 RunLoop run_loop;
256 MemoryDumpCallback callback =
257 Bind(&MemoryDumpManagerTest::DumpCallbackAdapter, Unretained(this),
258 MessageLoop::current()->task_runner(), run_loop.QuitClosure());
259 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED, callback);
260 // This nested message loop (|run_loop|) will be quit if and only if
261 // the RequestGlobalDump callback is invoked.
262 run_loop.Run();
264 EXPECT_TRUE(last_callback_success_);
266 // Unregister a MDP and destroy one thread at each iteration to check the
267 // live unregistration logic. The unregistration needs to happen on the same
268 // thread the MDP belongs to.
270 RunLoop run_loop;
271 Closure unregistration =
272 Bind(&MemoryDumpManager::UnregisterDumpProvider,
273 Unretained(mdm_.get()), Unretained(mdps.back()));
274 threads.back()->task_runner()->PostTaskAndReply(FROM_HERE, unregistration,
275 run_loop.QuitClosure());
276 run_loop.Run();
278 mdps.pop_back();
279 threads.back()->Stop();
280 threads.pop_back();
283 DisableTracing();
286 // Enable both dump providers, make sure that mdp gets disabled after 3 failures
287 // and not disabled after 1.
288 TEST_F(MemoryDumpManagerTest, DisableFailingDumpers) {
289 MockDumpProvider mdp1;
290 MockDumpProvider mdp2;
292 mdm_->RegisterDumpProvider(&mdp1);
293 mdm_->RegisterDumpProvider(&mdp2);
294 EnableTracing(kTraceCategory);
296 EXPECT_CALL(mdp1, OnMemoryDump(_, _))
297 .Times(MemoryDumpManager::kMaxConsecutiveFailuresCount)
298 .WillRepeatedly(Return(false));
300 EXPECT_CALL(mdp2, OnMemoryDump(_, _))
301 .Times(1 + MemoryDumpManager::kMaxConsecutiveFailuresCount)
302 .WillOnce(Return(false))
303 .WillRepeatedly(Return(true));
304 for (int i = 0; i < 1 + MemoryDumpManager::kMaxConsecutiveFailuresCount;
305 i++) {
306 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED);
309 DisableTracing();
312 // Sneakily register an extra memory dump provider while an existing one is
313 // dumping and expect it to take part in the already active tracing session.
314 TEST_F(MemoryDumpManagerTest, RegisterDumperWhileDumping) {
315 MockDumpProvider mdp1;
316 MockDumpProvider mdp2;
318 mdp1.dump_provider_to_register_or_unregister = &mdp2;
319 mdm_->RegisterDumpProvider(&mdp1);
320 EnableTracing(kTraceCategory);
322 EXPECT_CALL(mdp1, OnMemoryDump(_, _))
323 .Times(4)
324 .WillOnce(Return(true))
325 .WillOnce(Invoke(
326 &mdp1, &MockDumpProvider::OnMemoryDump_RegisterExtraDumpProvider))
327 .WillRepeatedly(Return(true));
329 // Depending on the insertion order (before or after mdp1), mdp2 might be
330 // called also immediately after it gets registered.
331 EXPECT_CALL(mdp2, OnMemoryDump(_, _))
332 .Times(Between(2, 3))
333 .WillRepeatedly(Return(true));
335 for (int i = 0; i < 4; i++) {
336 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED);
339 DisableTracing();
342 // Like the above, but suddenly unregister the dump provider.
343 TEST_F(MemoryDumpManagerTest, UnregisterDumperWhileDumping) {
344 MockDumpProvider mdp1;
345 MockDumpProvider mdp2;
347 mdm_->RegisterDumpProvider(&mdp1, ThreadTaskRunnerHandle::Get());
348 mdm_->RegisterDumpProvider(&mdp2, ThreadTaskRunnerHandle::Get());
349 mdp1.dump_provider_to_register_or_unregister = &mdp2;
350 EnableTracing(kTraceCategory);
352 EXPECT_CALL(mdp1, OnMemoryDump(_, _))
353 .Times(4)
354 .WillOnce(Return(true))
355 .WillOnce(
356 Invoke(&mdp1, &MockDumpProvider::OnMemoryDump_UnregisterDumpProvider))
357 .WillRepeatedly(Return(true));
359 // Depending on the insertion order (before or after mdp1), mdp2 might have
360 // been already called when OnMemoryDump_UnregisterDumpProvider happens.
361 EXPECT_CALL(mdp2, OnMemoryDump(_, _))
362 .Times(Between(1, 2))
363 .WillRepeatedly(Return(true));
365 for (int i = 0; i < 4; i++) {
366 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED);
369 DisableTracing();
372 // Ensures that a NACK callback is invoked if RequestGlobalDump is called when
373 // tracing is not enabled.
374 TEST_F(MemoryDumpManagerTest, CallbackCalledOnFailure) {
375 MockDumpProvider mdp1;
377 mdm_->RegisterDumpProvider(&mdp1);
378 EXPECT_CALL(mdp1, OnMemoryDump(_, _)).Times(0);
380 last_callback_success_ = true;
382 RunLoop run_loop;
383 MemoryDumpCallback callback =
384 Bind(&MemoryDumpManagerTest::DumpCallbackAdapter, Unretained(this),
385 MessageLoop::current()->task_runner(), run_loop.QuitClosure());
386 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED, callback);
387 run_loop.Run();
389 EXPECT_FALSE(last_callback_success_);
392 } // namespace trace_event
393 } // namespace base