| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "base/trace_event/memory_dump_manager.h" | |
| 6 | |
| 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" | |
| 17 | |
| 18 using testing::_; | |
| 19 using testing::Between; | |
| 20 using testing::Invoke; | |
| 21 using testing::Return; | |
| 22 | |
| 23 namespace base { | |
| 24 namespace trace_event { | |
| 25 | |
| 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); | |
| 33 } | |
| 34 | |
| 35 bool IsCoordinatorProcess() const override { return false; } | |
| 36 }; | |
| 37 | |
| 38 class MemoryDumpManagerTest : public testing::Test { | |
| 39 public: | |
| 40 void SetUp() override { | |
| 41 message_loop_.reset(new MessageLoop()); | |
| 42 mdm_.reset(new MemoryDumpManager()); | |
| 43 MemoryDumpManager::SetInstanceForTesting(mdm_.get()); | |
| 44 ASSERT_EQ(mdm_, MemoryDumpManager::GetInstance()); | |
| 45 MemoryDumpManager::GetInstance()->Initialize(); | |
| 46 MemoryDumpManager::GetInstance()->SetDelegate(&delegate_); | |
| 47 } | |
| 48 | |
| 49 void TearDown() override { | |
| 50 MemoryDumpManager::SetInstanceForTesting(nullptr); | |
| 51 mdm_.reset(); | |
| 52 message_loop_.reset(); | |
| 53 TraceLog::DeleteForTesting(); | |
| 54 } | |
| 55 | |
| 56 void DumpCallbackAdapter(scoped_refptr<SingleThreadTaskRunner> task_runner, | |
| 57 Closure closure, | |
| 58 uint64 dump_guid, | |
| 59 bool success) { | |
| 60 task_runner->PostTask(FROM_HERE, closure); | |
| 61 } | |
| 62 | |
| 63 protected: | |
| 64 const char* kTraceCategory = MemoryDumpManager::kTraceCategoryForTesting; | |
| 65 | |
| 66 void EnableTracing(const char* category) { | |
| 67 TraceLog::GetInstance()->SetEnabled( | |
| 68 TraceConfig(category, ""), TraceLog::RECORDING_MODE); | |
| 69 } | |
| 70 | |
| 71 void DisableTracing() { TraceLog::GetInstance()->SetDisabled(); } | |
| 72 | |
| 73 scoped_ptr<MemoryDumpManager> mdm_; | |
| 74 | |
| 75 private: | |
| 76 scoped_ptr<MessageLoop> message_loop_; | |
| 77 MemoryDumpManagerDelegateForTesting delegate_; | |
| 78 | |
| 79 // We want our singleton torn down after each test. | |
| 80 ShadowingAtExitManager at_exit_manager_; | |
| 81 }; | |
| 82 | |
| 83 class MockDumpProvider : public MemoryDumpProvider { | |
| 84 public: | |
| 85 MockDumpProvider() | |
| 86 : dump_provider_to_register_or_unregister(nullptr), | |
| 87 last_session_state_(nullptr) {} | |
| 88 | |
| 89 // Ctor used by the RespectTaskRunnerAffinity test. | |
| 90 explicit MockDumpProvider( | |
| 91 const scoped_refptr<SingleThreadTaskRunner>& task_runner) | |
| 92 : last_session_state_(nullptr), task_runner_(task_runner) {} | |
| 93 | |
| 94 virtual ~MockDumpProvider() {} | |
| 95 | |
| 96 MOCK_METHOD1(OnMemoryDump, bool(ProcessMemoryDump* pmd)); | |
| 97 | |
| 98 // OnMemoryDump() override for the RespectTaskRunnerAffinity test. | |
| 99 bool OnMemoryDump_CheckTaskRunner(ProcessMemoryDump* pmd) { | |
| 100 EXPECT_TRUE(task_runner_->RunsTasksOnCurrentThread()); | |
| 101 return true; | |
| 102 } | |
| 103 | |
| 104 // OnMemoryDump() override for the SharedSessionState test. | |
| 105 bool OnMemoryDump_CheckSessionState(ProcessMemoryDump* pmd) { | |
| 106 MemoryDumpSessionState* cur_session_state = pmd->session_state().get(); | |
| 107 if (last_session_state_) | |
| 108 EXPECT_EQ(last_session_state_, cur_session_state); | |
| 109 last_session_state_ = cur_session_state; | |
| 110 return true; | |
| 111 } | |
| 112 | |
| 113 // OnMemoryDump() override for the RegisterDumperWhileDumping test. | |
| 114 bool OnMemoryDump_RegisterExtraDumpProvider(ProcessMemoryDump* pmd) { | |
| 115 MemoryDumpManager::GetInstance()->RegisterDumpProvider( | |
| 116 dump_provider_to_register_or_unregister); | |
| 117 return true; | |
| 118 } | |
| 119 | |
| 120 // OnMemoryDump() override for the UnegisterDumperWhileDumping test. | |
| 121 bool OnMemoryDump_UnregisterDumpProvider(ProcessMemoryDump* pmd) { | |
| 122 MemoryDumpManager::GetInstance()->UnregisterDumpProvider( | |
| 123 dump_provider_to_register_or_unregister); | |
| 124 return true; | |
| 125 } | |
| 126 | |
| 127 // Used by OnMemoryDump_(Un)RegisterExtraDumpProvider. | |
| 128 MemoryDumpProvider* dump_provider_to_register_or_unregister; | |
| 129 | |
| 130 private: | |
| 131 MemoryDumpSessionState* last_session_state_; | |
| 132 scoped_refptr<SingleThreadTaskRunner> task_runner_; | |
| 133 }; | |
| 134 | |
| 135 TEST_F(MemoryDumpManagerTest, SingleDumper) { | |
| 136 MockDumpProvider mdp; | |
| 137 mdm_->RegisterDumpProvider(&mdp); | |
| 138 | |
| 139 // Check that the dumper is not called if the memory category is not enabled. | |
| 140 EnableTracing("foo-and-bar-but-not-memory"); | |
| 141 EXPECT_CALL(mdp, OnMemoryDump(_)).Times(0); | |
| 142 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED); | |
| 143 DisableTracing(); | |
| 144 | |
| 145 // Now repeat enabling the memory category and check that the dumper is | |
| 146 // invoked this time. | |
| 147 EnableTracing(kTraceCategory); | |
| 148 EXPECT_CALL(mdp, OnMemoryDump(_)).Times(3).WillRepeatedly(Return(true)); | |
| 149 for (int i = 0; i < 3; ++i) | |
| 150 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED); | |
| 151 DisableTracing(); | |
| 152 | |
| 153 mdm_->UnregisterDumpProvider(&mdp); | |
| 154 | |
| 155 // Finally check the unregister logic (no calls to the mdp after unregister). | |
| 156 EnableTracing(kTraceCategory); | |
| 157 EXPECT_CALL(mdp, OnMemoryDump(_)).Times(0); | |
| 158 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED); | |
| 159 TraceLog::GetInstance()->SetDisabled(); | |
| 160 } | |
| 161 | |
| 162 TEST_F(MemoryDumpManagerTest, SharedSessionState) { | |
| 163 MockDumpProvider mdp1; | |
| 164 MockDumpProvider mdp2; | |
| 165 mdm_->RegisterDumpProvider(&mdp1); | |
| 166 mdm_->RegisterDumpProvider(&mdp2); | |
| 167 | |
| 168 EnableTracing(kTraceCategory); | |
| 169 EXPECT_CALL(mdp1, OnMemoryDump(_)) | |
| 170 .Times(2) | |
| 171 .WillRepeatedly( | |
| 172 Invoke(&mdp1, &MockDumpProvider::OnMemoryDump_CheckSessionState)); | |
| 173 EXPECT_CALL(mdp2, OnMemoryDump(_)) | |
| 174 .Times(2) | |
| 175 .WillRepeatedly( | |
| 176 Invoke(&mdp2, &MockDumpProvider::OnMemoryDump_CheckSessionState)); | |
| 177 | |
| 178 for (int i = 0; i < 2; ++i) | |
| 179 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED); | |
| 180 | |
| 181 DisableTracing(); | |
| 182 } | |
| 183 | |
| 184 TEST_F(MemoryDumpManagerTest, MultipleDumpers) { | |
| 185 MockDumpProvider mdp1; | |
| 186 MockDumpProvider mdp2; | |
| 187 | |
| 188 // Enable only mdp1. | |
| 189 mdm_->RegisterDumpProvider(&mdp1); | |
| 190 EnableTracing(kTraceCategory); | |
| 191 EXPECT_CALL(mdp1, OnMemoryDump(_)).Times(1).WillRepeatedly(Return(true)); | |
| 192 EXPECT_CALL(mdp2, OnMemoryDump(_)).Times(0); | |
| 193 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED); | |
| 194 DisableTracing(); | |
| 195 | |
| 196 // Invert: enable mdp1 and disable mdp2. | |
| 197 mdm_->UnregisterDumpProvider(&mdp1); | |
| 198 mdm_->RegisterDumpProvider(&mdp2); | |
| 199 EnableTracing(kTraceCategory); | |
| 200 EXPECT_CALL(mdp1, OnMemoryDump(_)).Times(0); | |
| 201 EXPECT_CALL(mdp2, OnMemoryDump(_)).Times(1).WillRepeatedly(Return(true)); | |
| 202 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED); | |
| 203 DisableTracing(); | |
| 204 | |
| 205 // Enable both mdp1 and mdp2. | |
| 206 mdm_->RegisterDumpProvider(&mdp1); | |
| 207 EnableTracing(kTraceCategory); | |
| 208 EXPECT_CALL(mdp1, OnMemoryDump(_)).Times(1).WillRepeatedly(Return(true)); | |
| 209 EXPECT_CALL(mdp2, OnMemoryDump(_)).Times(1).WillRepeatedly(Return(true)); | |
| 210 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED); | |
| 211 DisableTracing(); | |
| 212 } | |
| 213 | |
| 214 // Checks that the MemoryDumpManager respects the thread affinity when a | |
| 215 // MemoryDumpProvider specifies a task_runner(). The test starts creating 8 | |
| 216 // threads and registering a MemoryDumpProvider on each of them. At each | |
| 217 // iteration, one thread is removed, to check the live unregistration logic. | |
| 218 TEST_F(MemoryDumpManagerTest, RespectTaskRunnerAffinity) { | |
| 219 const uint32 kNumInitialThreads = 8; | |
| 220 | |
| 221 ScopedVector<Thread> threads; | |
| 222 ScopedVector<MockDumpProvider> mdps; | |
| 223 | |
| 224 // Create the threads and setup the expectations. Given that at each iteration | |
| 225 // we will pop out one thread/MemoryDumpProvider, each MDP is supposed to be | |
| 226 // invoked a number of times equal to its index. | |
| 227 for (uint32 i = kNumInitialThreads; i > 0; --i) { | |
| 228 threads.push_back(new Thread("test thread")); | |
| 229 threads.back()->Start(); | |
| 230 mdps.push_back(new MockDumpProvider(threads.back()->task_runner())); | |
| 231 MockDumpProvider* mdp = mdps.back(); | |
| 232 mdm_->RegisterDumpProvider(mdp, threads.back()->task_runner()); | |
| 233 EXPECT_CALL(*mdp, OnMemoryDump(_)) | |
| 234 .Times(i) | |
| 235 .WillRepeatedly( | |
| 236 Invoke(mdp, &MockDumpProvider::OnMemoryDump_CheckTaskRunner)); | |
| 237 } | |
| 238 | |
| 239 EnableTracing(kTraceCategory); | |
| 240 | |
| 241 while (!threads.empty()) { | |
| 242 { | |
| 243 RunLoop run_loop; | |
| 244 MemoryDumpCallback callback = | |
| 245 Bind(&MemoryDumpManagerTest::DumpCallbackAdapter, Unretained(this), | |
| 246 MessageLoop::current()->task_runner(), run_loop.QuitClosure()); | |
| 247 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED, callback); | |
| 248 // This nested message loop (|run_loop|) will be quit if and only if | |
| 249 // the RequestGlobalDump callback is invoked. | |
| 250 run_loop.Run(); | |
| 251 } | |
| 252 | |
| 253 // Unregister a MDP and destroy one thread at each iteration to check the | |
| 254 // live unregistration logic. The unregistration needs to happen on the same | |
| 255 // thread the MDP belongs to. | |
| 256 { | |
| 257 RunLoop run_loop; | |
| 258 Closure unregistration = | |
| 259 Bind(&MemoryDumpManager::UnregisterDumpProvider, | |
| 260 Unretained(mdm_.get()), Unretained(mdps.back())); | |
| 261 threads.back()->task_runner()->PostTaskAndReply(FROM_HERE, unregistration, | |
| 262 run_loop.QuitClosure()); | |
| 263 run_loop.Run(); | |
| 264 } | |
| 265 mdps.pop_back(); | |
| 266 threads.back()->Stop(); | |
| 267 threads.pop_back(); | |
| 268 } | |
| 269 | |
| 270 DisableTracing(); | |
| 271 } | |
| 272 | |
| 273 // Enable both dump providers, make sure that mdp gets disabled after 3 failures | |
| 274 // and not disabled after 1. | |
| 275 TEST_F(MemoryDumpManagerTest, DisableFailingDumpers) { | |
| 276 MockDumpProvider mdp1; | |
| 277 MockDumpProvider mdp2; | |
| 278 | |
| 279 mdm_->RegisterDumpProvider(&mdp1); | |
| 280 mdm_->RegisterDumpProvider(&mdp2); | |
| 281 EnableTracing(kTraceCategory); | |
| 282 | |
| 283 EXPECT_CALL(mdp1, OnMemoryDump(_)) | |
| 284 .Times(MemoryDumpManager::kMaxConsecutiveFailuresCount) | |
| 285 .WillRepeatedly(Return(false)); | |
| 286 | |
| 287 EXPECT_CALL(mdp2, OnMemoryDump(_)) | |
| 288 .Times(1 + MemoryDumpManager::kMaxConsecutiveFailuresCount) | |
| 289 .WillOnce(Return(false)) | |
| 290 .WillRepeatedly(Return(true)); | |
| 291 for (int i = 0; i < 1 + MemoryDumpManager::kMaxConsecutiveFailuresCount; | |
| 292 i++) { | |
| 293 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED); | |
| 294 } | |
| 295 | |
| 296 DisableTracing(); | |
| 297 } | |
| 298 | |
| 299 // Sneakily register an extra memory dump provider while an existing one is | |
| 300 // dumping and expect it to take part in the already active tracing session. | |
| 301 TEST_F(MemoryDumpManagerTest, RegisterDumperWhileDumping) { | |
| 302 MockDumpProvider mdp1; | |
| 303 MockDumpProvider mdp2; | |
| 304 | |
| 305 mdp1.dump_provider_to_register_or_unregister = &mdp2; | |
| 306 mdm_->RegisterDumpProvider(&mdp1); | |
| 307 EnableTracing(kTraceCategory); | |
| 308 | |
| 309 EXPECT_CALL(mdp1, OnMemoryDump(_)) | |
| 310 .Times(4) | |
| 311 .WillOnce(Return(true)) | |
| 312 .WillOnce(Invoke( | |
| 313 &mdp1, &MockDumpProvider::OnMemoryDump_RegisterExtraDumpProvider)) | |
| 314 .WillRepeatedly(Return(true)); | |
| 315 | |
| 316 // Depending on the insertion order (before or after mdp1), mdp2 might be | |
| 317 // called also immediately after it gets registered. | |
| 318 EXPECT_CALL(mdp2, OnMemoryDump(_)) | |
| 319 .Times(Between(2, 3)) | |
| 320 .WillRepeatedly(Return(true)); | |
| 321 | |
| 322 for (int i = 0; i < 4; i++) { | |
| 323 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED); | |
| 324 } | |
| 325 | |
| 326 DisableTracing(); | |
| 327 } | |
| 328 | |
| 329 // Like the above, but suddenly unregister the dump provider. | |
| 330 TEST_F(MemoryDumpManagerTest, UnregisterDumperWhileDumping) { | |
| 331 MockDumpProvider mdp1; | |
| 332 MockDumpProvider mdp2; | |
| 333 | |
| 334 mdm_->RegisterDumpProvider(&mdp1, ThreadTaskRunnerHandle::Get()); | |
| 335 mdm_->RegisterDumpProvider(&mdp2, ThreadTaskRunnerHandle::Get()); | |
| 336 mdp1.dump_provider_to_register_or_unregister = &mdp2; | |
| 337 EnableTracing(kTraceCategory); | |
| 338 | |
| 339 EXPECT_CALL(mdp1, OnMemoryDump(_)) | |
| 340 .Times(4) | |
| 341 .WillOnce(Return(true)) | |
| 342 .WillOnce( | |
| 343 Invoke(&mdp1, &MockDumpProvider::OnMemoryDump_UnregisterDumpProvider)) | |
| 344 .WillRepeatedly(Return(true)); | |
| 345 | |
| 346 // Depending on the insertion order (before or after mdp1), mdp2 might have | |
| 347 // been already called when OnMemoryDump_UnregisterDumpProvider happens. | |
| 348 EXPECT_CALL(mdp2, OnMemoryDump(_)) | |
| 349 .Times(Between(1, 2)) | |
| 350 .WillRepeatedly(Return(true)); | |
| 351 | |
| 352 for (int i = 0; i < 4; i++) { | |
| 353 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED); | |
| 354 } | |
| 355 | |
| 356 DisableTracing(); | |
| 357 } | |
| 358 | |
| 359 } // namespace trace_event | |
| 360 } // namespace base | |
| OLD | NEW |