Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(589)

Side by Side Diff: base/trace_event/memory_dump_manager_unittest.cc

Issue 1427963002: [tracing] Move memory-infra dumps to dedicated thread (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@memory-infra-names
Patch Set: remove extra reset() Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/trace_event/memory_dump_manager.h" 5 #include "base/trace_event/memory_dump_manager.h"
6 6
7 #include "base/bind_helpers.h" 7 #include "base/bind_helpers.h"
8 #include "base/memory/scoped_vector.h" 8 #include "base/memory/scoped_vector.h"
9 #include "base/message_loop/message_loop.h" 9 #include "base/message_loop/message_loop.h"
10 #include "base/run_loop.h" 10 #include "base/run_loop.h"
11 #include "base/strings/stringprintf.h" 11 #include "base/strings/stringprintf.h"
12 #include "base/synchronization/waitable_event.h"
12 #include "base/test/test_io_thread.h" 13 #include "base/test/test_io_thread.h"
13 #include "base/thread_task_runner_handle.h" 14 #include "base/thread_task_runner_handle.h"
14 #include "base/threading/thread.h" 15 #include "base/threading/thread.h"
15 #include "base/trace_event/memory_dump_provider.h" 16 #include "base/trace_event/memory_dump_provider.h"
16 #include "base/trace_event/process_memory_dump.h" 17 #include "base/trace_event/process_memory_dump.h"
17 #include "base/trace_event/trace_config_memory_test_util.h" 18 #include "base/trace_event/trace_config_memory_test_util.h"
18 #include "testing/gmock/include/gmock/gmock.h" 19 #include "testing/gmock/include/gmock/gmock.h"
19 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
20 21
21 using testing::_; 22 using testing::_;
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
90 } 91 }
91 92
92 void TearDown() override { 93 void TearDown() override {
93 MemoryDumpManager::SetInstanceForTesting(nullptr); 94 MemoryDumpManager::SetInstanceForTesting(nullptr);
94 mdm_.reset(); 95 mdm_.reset();
95 delegate_.reset(); 96 delegate_.reset();
96 message_loop_.reset(); 97 message_loop_.reset();
97 TraceLog::DeleteForTesting(); 98 TraceLog::DeleteForTesting();
98 } 99 }
99 100
101 // Turns a Closure into a MemoryDumpCallback, keeping track of the callback
102 // result and taking care of posting the closure on the correct task runner.
100 void DumpCallbackAdapter(scoped_refptr<SingleThreadTaskRunner> task_runner, 103 void DumpCallbackAdapter(scoped_refptr<SingleThreadTaskRunner> task_runner,
101 Closure closure, 104 Closure closure,
102 uint64 dump_guid, 105 uint64 dump_guid,
103 bool success) { 106 bool success) {
104 last_callback_success_ = success; 107 last_callback_success_ = success;
105 task_runner->PostTask(FROM_HERE, closure); 108 task_runner->PostTask(FROM_HERE, closure);
106 } 109 }
107 110
108 protected: 111 protected:
109 void InitializeMemoryDumpManager(bool is_coordinator) { 112 void InitializeMemoryDumpManager(bool is_coordinator) {
110 mdm_->set_dumper_registrations_ignored_for_testing(true); 113 mdm_->set_dumper_registrations_ignored_for_testing(true);
111 mdm_->Initialize(delegate_.get(), is_coordinator); 114 mdm_->Initialize(delegate_.get(), is_coordinator);
112 } 115 }
113 116
117 void RequestGlobalDumpAndWait(MemoryDumpType dump_type,
118 MemoryDumpLevelOfDetail level_of_detail) {
119 RunLoop run_loop;
120 MemoryDumpCallback callback =
121 Bind(&MemoryDumpManagerTest::DumpCallbackAdapter, Unretained(this),
122 MessageLoop::current()->task_runner(), run_loop.QuitClosure());
123 mdm_->RequestGlobalDump(dump_type, level_of_detail, callback);
124 run_loop.Run();
125 }
126
114 void EnableTracingWithLegacyCategories(const char* category) { 127 void EnableTracingWithLegacyCategories(const char* category) {
115 TraceLog::GetInstance()->SetEnabled(TraceConfig(category, ""), 128 TraceLog::GetInstance()->SetEnabled(TraceConfig(category, ""),
116 TraceLog::RECORDING_MODE); 129 TraceLog::RECORDING_MODE);
117 } 130 }
118 131
119 void EnableTracingWithTraceConfig(const std::string& trace_config) { 132 void EnableTracingWithTraceConfig(const std::string& trace_config) {
120 TraceLog::GetInstance()->SetEnabled(TraceConfig(trace_config), 133 TraceLog::GetInstance()->SetEnabled(TraceConfig(trace_config),
121 TraceLog::RECORDING_MODE); 134 TraceLog::RECORDING_MODE);
122 } 135 }
123 136
(...skipping 22 matching lines...) Expand all
146 // called, but only when memory-infra is enabled. 159 // called, but only when memory-infra is enabled.
147 TEST_F(MemoryDumpManagerTest, SingleDumper) { 160 TEST_F(MemoryDumpManagerTest, SingleDumper) {
148 InitializeMemoryDumpManager(false /* is_coordinator */); 161 InitializeMemoryDumpManager(false /* is_coordinator */);
149 MockMemoryDumpProvider mdp; 162 MockMemoryDumpProvider mdp;
150 RegisterDumpProvider(&mdp); 163 RegisterDumpProvider(&mdp);
151 164
152 // Check that the dumper is not called if the memory category is not enabled. 165 // Check that the dumper is not called if the memory category is not enabled.
153 EnableTracingWithLegacyCategories("foobar-but-not-memory"); 166 EnableTracingWithLegacyCategories("foobar-but-not-memory");
154 EXPECT_CALL(*delegate_, RequestGlobalMemoryDump(_, _)).Times(0); 167 EXPECT_CALL(*delegate_, RequestGlobalMemoryDump(_, _)).Times(0);
155 EXPECT_CALL(mdp, OnMemoryDump(_, _)).Times(0); 168 EXPECT_CALL(mdp, OnMemoryDump(_, _)).Times(0);
156 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED, 169 RequestGlobalDumpAndWait(MemoryDumpType::EXPLICITLY_TRIGGERED,
157 MemoryDumpLevelOfDetail::DETAILED); 170 MemoryDumpLevelOfDetail::DETAILED);
158 DisableTracing(); 171 DisableTracing();
159 172
160 // Now repeat enabling the memory category and check that the dumper is 173 // Now repeat enabling the memory category and check that the dumper is
161 // invoked this time. 174 // invoked this time.
162 EnableTracingWithLegacyCategories(MemoryDumpManager::kTraceCategory); 175 EnableTracingWithLegacyCategories(MemoryDumpManager::kTraceCategory);
163 EXPECT_CALL(*delegate_, RequestGlobalMemoryDump(_, _)).Times(3); 176 EXPECT_CALL(*delegate_, RequestGlobalMemoryDump(_, _)).Times(3);
164 EXPECT_CALL(mdp, OnMemoryDump(_, _)).Times(3).WillRepeatedly(Return(true)); 177 EXPECT_CALL(mdp, OnMemoryDump(_, _)).Times(3).WillRepeatedly(Return(true));
165 for (int i = 0; i < 3; ++i) 178 for (int i = 0; i < 3; ++i)
166 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED, 179 RequestGlobalDumpAndWait(MemoryDumpType::EXPLICITLY_TRIGGERED,
167 MemoryDumpLevelOfDetail::DETAILED); 180 MemoryDumpLevelOfDetail::DETAILED);
168 DisableTracing(); 181 DisableTracing();
169 182
170 mdm_->UnregisterDumpProvider(&mdp); 183 mdm_->UnregisterDumpProvider(&mdp);
171 184
172 // Finally check the unregister logic: the delegate will be invoked but not 185 // Finally check the unregister logic: the delegate will be invoked but not
173 // the dump provider, as it has been unregistered. 186 // the dump provider, as it has been unregistered.
174 EnableTracingWithLegacyCategories(MemoryDumpManager::kTraceCategory); 187 EnableTracingWithLegacyCategories(MemoryDumpManager::kTraceCategory);
175 EXPECT_CALL(*delegate_, RequestGlobalMemoryDump(_, _)).Times(1); 188 EXPECT_CALL(*delegate_, RequestGlobalMemoryDump(_, _)).Times(1);
176 EXPECT_CALL(mdp, OnMemoryDump(_, _)).Times(0); 189 EXPECT_CALL(mdp, OnMemoryDump(_, _)).Times(0);
177 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED, 190 RequestGlobalDumpAndWait(MemoryDumpType::EXPLICITLY_TRIGGERED,
178 MemoryDumpLevelOfDetail::DETAILED); 191 MemoryDumpLevelOfDetail::DETAILED);
179 TraceLog::GetInstance()->SetDisabled(); 192 TraceLog::GetInstance()->SetDisabled();
180 } 193 }
181 194
182 // Checks that requesting dumps with high level of detail actually propagates 195 // Checks that requesting dumps with high level of detail actually propagates
183 // the level of the detail properly to OnMemoryDump() call on dump providers. 196 // the level of the detail properly to OnMemoryDump() call on dump providers.
184 TEST_F(MemoryDumpManagerTest, CheckMemoryDumpArgs) { 197 TEST_F(MemoryDumpManagerTest, CheckMemoryDumpArgs) {
185 InitializeMemoryDumpManager(false /* is_coordinator */); 198 InitializeMemoryDumpManager(false /* is_coordinator */);
186 MockMemoryDumpProvider mdp; 199 MockMemoryDumpProvider mdp;
187 200
188 RegisterDumpProvider(&mdp); 201 RegisterDumpProvider(&mdp);
189 EnableTracingWithLegacyCategories(MemoryDumpManager::kTraceCategory); 202 EnableTracingWithLegacyCategories(MemoryDumpManager::kTraceCategory);
190 EXPECT_CALL(*delegate_, RequestGlobalMemoryDump(_, _)).Times(1); 203 EXPECT_CALL(*delegate_, RequestGlobalMemoryDump(_, _)).Times(1);
191 EXPECT_CALL(mdp, OnMemoryDump(IsDetailedDump(), _)).WillOnce(Return(true)); 204 EXPECT_CALL(mdp, OnMemoryDump(IsDetailedDump(), _)).WillOnce(Return(true));
192 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED, 205 RequestGlobalDumpAndWait(MemoryDumpType::EXPLICITLY_TRIGGERED,
193 MemoryDumpLevelOfDetail::DETAILED); 206 MemoryDumpLevelOfDetail::DETAILED);
194 DisableTracing(); 207 DisableTracing();
195 mdm_->UnregisterDumpProvider(&mdp); 208 mdm_->UnregisterDumpProvider(&mdp);
196 209
197 // Check that requesting dumps with low level of detail actually propagates to 210 // Check that requesting dumps with low level of detail actually propagates to
198 // OnMemoryDump() call on dump providers. 211 // OnMemoryDump() call on dump providers.
199 RegisterDumpProvider(&mdp); 212 RegisterDumpProvider(&mdp);
200 EnableTracingWithLegacyCategories(MemoryDumpManager::kTraceCategory); 213 EnableTracingWithLegacyCategories(MemoryDumpManager::kTraceCategory);
201 EXPECT_CALL(*delegate_, RequestGlobalMemoryDump(_, _)).Times(1); 214 EXPECT_CALL(*delegate_, RequestGlobalMemoryDump(_, _)).Times(1);
202 EXPECT_CALL(mdp, OnMemoryDump(IsLightDump(), _)).WillOnce(Return(true)); 215 EXPECT_CALL(mdp, OnMemoryDump(IsLightDump(), _)).WillOnce(Return(true));
203 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED, 216 RequestGlobalDumpAndWait(MemoryDumpType::EXPLICITLY_TRIGGERED,
204 MemoryDumpLevelOfDetail::LIGHT); 217 MemoryDumpLevelOfDetail::LIGHT);
205 DisableTracing(); 218 DisableTracing();
206 mdm_->UnregisterDumpProvider(&mdp); 219 mdm_->UnregisterDumpProvider(&mdp);
207 } 220 }
208 221
209 // Checks that the SharedSessionState object is acqually shared over time. 222 // Checks that the SharedSessionState object is acqually shared over time.
210 TEST_F(MemoryDumpManagerTest, SharedSessionState) { 223 TEST_F(MemoryDumpManagerTest, SharedSessionState) {
211 InitializeMemoryDumpManager(false /* is_coordinator */); 224 InitializeMemoryDumpManager(false /* is_coordinator */);
212 MockMemoryDumpProvider mdp1; 225 MockMemoryDumpProvider mdp1;
213 MockMemoryDumpProvider mdp2; 226 MockMemoryDumpProvider mdp2;
214 RegisterDumpProvider(&mdp1); 227 RegisterDumpProvider(&mdp1);
(...skipping 10 matching lines...) Expand all
225 return true; 238 return true;
226 })); 239 }));
227 EXPECT_CALL(mdp2, OnMemoryDump(_, _)) 240 EXPECT_CALL(mdp2, OnMemoryDump(_, _))
228 .Times(2) 241 .Times(2)
229 .WillRepeatedly(Invoke([session_state](const MemoryDumpArgs&, 242 .WillRepeatedly(Invoke([session_state](const MemoryDumpArgs&,
230 ProcessMemoryDump* pmd) -> bool { 243 ProcessMemoryDump* pmd) -> bool {
231 EXPECT_EQ(session_state, pmd->session_state().get()); 244 EXPECT_EQ(session_state, pmd->session_state().get());
232 return true; 245 return true;
233 })); 246 }));
234 247
235 for (int i = 0; i < 2; ++i) 248 for (int i = 0; i < 2; ++i) {
236 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED, 249 RequestGlobalDumpAndWait(MemoryDumpType::EXPLICITLY_TRIGGERED,
237 MemoryDumpLevelOfDetail::DETAILED); 250 MemoryDumpLevelOfDetail::DETAILED);
251 }
238 252
239 DisableTracing(); 253 DisableTracing();
240 } 254 }
241 255
242 // Checks that the (Un)RegisterDumpProvider logic behaves sanely. 256 // Checks that the (Un)RegisterDumpProvider logic behaves sanely.
243 TEST_F(MemoryDumpManagerTest, MultipleDumpers) { 257 TEST_F(MemoryDumpManagerTest, MultipleDumpers) {
244 InitializeMemoryDumpManager(false /* is_coordinator */); 258 InitializeMemoryDumpManager(false /* is_coordinator */);
245 MockMemoryDumpProvider mdp1; 259 MockMemoryDumpProvider mdp1;
246 MockMemoryDumpProvider mdp2; 260 MockMemoryDumpProvider mdp2;
247 261
248 // Enable only mdp1. 262 // Enable only mdp1.
249 RegisterDumpProvider(&mdp1); 263 RegisterDumpProvider(&mdp1);
250 EnableTracingWithLegacyCategories(MemoryDumpManager::kTraceCategory); 264 EnableTracingWithLegacyCategories(MemoryDumpManager::kTraceCategory);
251 EXPECT_CALL(*delegate_, RequestGlobalMemoryDump(_, _)).Times(1); 265 EXPECT_CALL(*delegate_, RequestGlobalMemoryDump(_, _)).Times(1);
252 EXPECT_CALL(mdp1, OnMemoryDump(_, _)).WillOnce(Return(true)); 266 EXPECT_CALL(mdp1, OnMemoryDump(_, _)).WillOnce(Return(true));
253 EXPECT_CALL(mdp2, OnMemoryDump(_, _)).Times(0); 267 EXPECT_CALL(mdp2, OnMemoryDump(_, _)).Times(0);
254 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED, 268 RequestGlobalDumpAndWait(MemoryDumpType::EXPLICITLY_TRIGGERED,
255 MemoryDumpLevelOfDetail::DETAILED); 269 MemoryDumpLevelOfDetail::DETAILED);
256 DisableTracing(); 270 DisableTracing();
257 271
258 // Invert: enable mdp1 and disable mdp2. 272 // Invert: enable mdp1 and disable mdp2.
259 mdm_->UnregisterDumpProvider(&mdp1); 273 mdm_->UnregisterDumpProvider(&mdp1);
260 RegisterDumpProvider(&mdp2); 274 RegisterDumpProvider(&mdp2);
261 EnableTracingWithLegacyCategories(MemoryDumpManager::kTraceCategory); 275 EnableTracingWithLegacyCategories(MemoryDumpManager::kTraceCategory);
262 EXPECT_CALL(*delegate_, RequestGlobalMemoryDump(_, _)).Times(1); 276 EXPECT_CALL(*delegate_, RequestGlobalMemoryDump(_, _)).Times(1);
263 EXPECT_CALL(mdp1, OnMemoryDump(_, _)).Times(0); 277 EXPECT_CALL(mdp1, OnMemoryDump(_, _)).Times(0);
264 EXPECT_CALL(mdp2, OnMemoryDump(_, _)).WillOnce(Return(true)); 278 EXPECT_CALL(mdp2, OnMemoryDump(_, _)).WillOnce(Return(true));
265 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED, 279 RequestGlobalDumpAndWait(MemoryDumpType::EXPLICITLY_TRIGGERED,
266 MemoryDumpLevelOfDetail::DETAILED); 280 MemoryDumpLevelOfDetail::DETAILED);
267 DisableTracing(); 281 DisableTracing();
268 282
269 // Enable both mdp1 and mdp2. 283 // Enable both mdp1 and mdp2.
270 RegisterDumpProvider(&mdp1); 284 RegisterDumpProvider(&mdp1);
271 EnableTracingWithLegacyCategories(MemoryDumpManager::kTraceCategory); 285 EnableTracingWithLegacyCategories(MemoryDumpManager::kTraceCategory);
272 EXPECT_CALL(*delegate_, RequestGlobalMemoryDump(_, _)).Times(1); 286 EXPECT_CALL(*delegate_, RequestGlobalMemoryDump(_, _)).Times(1);
273 EXPECT_CALL(mdp1, OnMemoryDump(_, _)).WillOnce(Return(true)); 287 EXPECT_CALL(mdp1, OnMemoryDump(_, _)).WillOnce(Return(true));
274 EXPECT_CALL(mdp2, OnMemoryDump(_, _)).WillOnce(Return(true)); 288 EXPECT_CALL(mdp2, OnMemoryDump(_, _)).WillOnce(Return(true));
275 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED, 289 RequestGlobalDumpAndWait(MemoryDumpType::EXPLICITLY_TRIGGERED,
276 MemoryDumpLevelOfDetail::DETAILED); 290 MemoryDumpLevelOfDetail::DETAILED);
277 DisableTracing(); 291 DisableTracing();
278 } 292 }
279 293
280 // Checks that the dump provider invocations depend only on the current 294 // Checks that the dump provider invocations depend only on the current
281 // registration state and not on previous registrations and dumps. 295 // registration state and not on previous registrations and dumps.
282 TEST_F(MemoryDumpManagerTest, RegistrationConsistency) { 296 TEST_F(MemoryDumpManagerTest, RegistrationConsistency) {
283 InitializeMemoryDumpManager(false /* is_coordinator */); 297 InitializeMemoryDumpManager(false /* is_coordinator */);
284 MockMemoryDumpProvider mdp; 298 MockMemoryDumpProvider mdp;
285 299
286 RegisterDumpProvider(&mdp); 300 RegisterDumpProvider(&mdp);
287 301
288 { 302 {
289 EXPECT_CALL(*delegate_, RequestGlobalMemoryDump(_, _)).Times(1); 303 EXPECT_CALL(*delegate_, RequestGlobalMemoryDump(_, _)).Times(1);
290 EXPECT_CALL(mdp, OnMemoryDump(_, _)).WillOnce(Return(true)); 304 EXPECT_CALL(mdp, OnMemoryDump(_, _)).WillOnce(Return(true));
291 EnableTracingWithLegacyCategories(MemoryDumpManager::kTraceCategory); 305 EnableTracingWithLegacyCategories(MemoryDumpManager::kTraceCategory);
292 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED, 306 RequestGlobalDumpAndWait(MemoryDumpType::EXPLICITLY_TRIGGERED,
293 MemoryDumpLevelOfDetail::DETAILED); 307 MemoryDumpLevelOfDetail::DETAILED);
294 DisableTracing(); 308 DisableTracing();
295 } 309 }
296 310
297 mdm_->UnregisterDumpProvider(&mdp); 311 mdm_->UnregisterDumpProvider(&mdp);
298 312
299 { 313 {
300 EXPECT_CALL(*delegate_, RequestGlobalMemoryDump(_, _)).Times(1); 314 EXPECT_CALL(*delegate_, RequestGlobalMemoryDump(_, _)).Times(1);
301 EXPECT_CALL(mdp, OnMemoryDump(_, _)).Times(0); 315 EXPECT_CALL(mdp, OnMemoryDump(_, _)).Times(0);
302 EnableTracingWithLegacyCategories(MemoryDumpManager::kTraceCategory); 316 EnableTracingWithLegacyCategories(MemoryDumpManager::kTraceCategory);
303 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED, 317 RequestGlobalDumpAndWait(MemoryDumpType::EXPLICITLY_TRIGGERED,
304 MemoryDumpLevelOfDetail::DETAILED); 318 MemoryDumpLevelOfDetail::DETAILED);
305 DisableTracing(); 319 DisableTracing();
306 } 320 }
307 321
308 RegisterDumpProvider(&mdp); 322 RegisterDumpProvider(&mdp);
309 mdm_->UnregisterDumpProvider(&mdp); 323 mdm_->UnregisterDumpProvider(&mdp);
310 324
311 { 325 {
312 EXPECT_CALL(*delegate_, RequestGlobalMemoryDump(_, _)).Times(1); 326 EXPECT_CALL(*delegate_, RequestGlobalMemoryDump(_, _)).Times(1);
313 EXPECT_CALL(mdp, OnMemoryDump(_, _)).Times(0); 327 EXPECT_CALL(mdp, OnMemoryDump(_, _)).Times(0);
314 EnableTracingWithLegacyCategories(MemoryDumpManager::kTraceCategory); 328 EnableTracingWithLegacyCategories(MemoryDumpManager::kTraceCategory);
315 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED, 329 RequestGlobalDumpAndWait(MemoryDumpType::EXPLICITLY_TRIGGERED,
316 MemoryDumpLevelOfDetail::DETAILED); 330 MemoryDumpLevelOfDetail::DETAILED);
317 DisableTracing(); 331 DisableTracing();
318 } 332 }
319 333
320 RegisterDumpProvider(&mdp); 334 RegisterDumpProvider(&mdp);
321 mdm_->UnregisterDumpProvider(&mdp); 335 mdm_->UnregisterDumpProvider(&mdp);
322 RegisterDumpProvider(&mdp); 336 RegisterDumpProvider(&mdp);
323 337
324 { 338 {
325 EXPECT_CALL(*delegate_, RequestGlobalMemoryDump(_, _)).Times(1); 339 EXPECT_CALL(*delegate_, RequestGlobalMemoryDump(_, _)).Times(1);
326 EXPECT_CALL(mdp, OnMemoryDump(_, _)).WillOnce(Return(true)); 340 EXPECT_CALL(mdp, OnMemoryDump(_, _)).WillOnce(Return(true));
327 EnableTracingWithLegacyCategories(MemoryDumpManager::kTraceCategory); 341 EnableTracingWithLegacyCategories(MemoryDumpManager::kTraceCategory);
328 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED, 342 RequestGlobalDumpAndWait(MemoryDumpType::EXPLICITLY_TRIGGERED,
329 MemoryDumpLevelOfDetail::DETAILED); 343 MemoryDumpLevelOfDetail::DETAILED);
330 DisableTracing(); 344 DisableTracing();
331 } 345 }
332 } 346 }
333 347
334 // Checks that the MemoryDumpManager respects the thread affinity when a 348 // Checks that the MemoryDumpManager respects the thread affinity when a
335 // MemoryDumpProvider specifies a task_runner(). The test starts creating 8 349 // MemoryDumpProvider specifies a task_runner(). The test starts creating 8
336 // threads and registering a MemoryDumpProvider on each of them. At each 350 // threads and registering a MemoryDumpProvider on each of them. At each
337 // iteration, one thread is removed, to check the live unregistration logic. 351 // iteration, one thread is removed, to check the live unregistration logic.
338 TEST_F(MemoryDumpManagerTest, RespectTaskRunnerAffinity) { 352 TEST_F(MemoryDumpManagerTest, RespectTaskRunnerAffinity) {
339 InitializeMemoryDumpManager(false /* is_coordinator */); 353 InitializeMemoryDumpManager(false /* is_coordinator */);
(...skipping 19 matching lines...) Expand all
359 [task_runner](const MemoryDumpArgs&, ProcessMemoryDump*) -> bool { 373 [task_runner](const MemoryDumpArgs&, ProcessMemoryDump*) -> bool {
360 EXPECT_TRUE(task_runner->RunsTasksOnCurrentThread()); 374 EXPECT_TRUE(task_runner->RunsTasksOnCurrentThread());
361 return true; 375 return true;
362 })); 376 }));
363 } 377 }
364 378
365 EnableTracingWithLegacyCategories(MemoryDumpManager::kTraceCategory); 379 EnableTracingWithLegacyCategories(MemoryDumpManager::kTraceCategory);
366 380
367 while (!threads.empty()) { 381 while (!threads.empty()) {
368 last_callback_success_ = false; 382 last_callback_success_ = false;
369 { 383 EXPECT_CALL(*delegate_, RequestGlobalMemoryDump(_, _)).Times(1);
370 RunLoop run_loop; 384 RequestGlobalDumpAndWait(MemoryDumpType::EXPLICITLY_TRIGGERED,
371 MemoryDumpCallback callback = 385 MemoryDumpLevelOfDetail::DETAILED);
372 Bind(&MemoryDumpManagerTest::DumpCallbackAdapter, Unretained(this),
373 MessageLoop::current()->task_runner(), run_loop.QuitClosure());
374 EXPECT_CALL(*delegate_, RequestGlobalMemoryDump(_, _)).Times(1);
375 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED,
376 MemoryDumpLevelOfDetail::DETAILED, callback);
377 // This nested message loop (|run_loop|) will quit if and only if the
378 // |callback| passed to RequestGlobalDump() is invoked.
379 run_loop.Run();
380 }
381 EXPECT_TRUE(last_callback_success_); 386 EXPECT_TRUE(last_callback_success_);
382 387
383 // Unregister a MDP and destroy one thread at each iteration to check the 388 // Unregister a MDP and destroy one thread at each iteration to check the
384 // live unregistration logic. The unregistration needs to happen on the same 389 // live unregistration logic. The unregistration needs to happen on the same
385 // thread the MDP belongs to. 390 // thread the MDP belongs to.
386 { 391 {
387 RunLoop run_loop; 392 RunLoop run_loop;
388 Closure unregistration = 393 Closure unregistration =
389 Bind(&MemoryDumpManager::UnregisterDumpProvider, 394 Bind(&MemoryDumpManager::UnregisterDumpProvider,
390 Unretained(mdm_.get()), Unretained(mdps.back())); 395 Unretained(mdm_.get()), Unretained(mdps.back()));
(...skipping 29 matching lines...) Expand all
420 425
421 EXPECT_CALL(mdp2, OnMemoryDump(_, _)) 426 EXPECT_CALL(mdp2, OnMemoryDump(_, _))
422 .WillOnce(Return(false)) 427 .WillOnce(Return(false))
423 .WillOnce(Return(true)) 428 .WillOnce(Return(true))
424 .WillOnce(Return(false)) 429 .WillOnce(Return(false))
425 .WillOnce(Return(false)) 430 .WillOnce(Return(false))
426 .WillOnce(Return(true)) 431 .WillOnce(Return(true))
427 .WillOnce(Return(false)); 432 .WillOnce(Return(false));
428 433
429 for (int i = 0; i < kNumDumps; i++) { 434 for (int i = 0; i < kNumDumps; i++) {
430 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED, 435 RequestGlobalDumpAndWait(MemoryDumpType::EXPLICITLY_TRIGGERED,
431 MemoryDumpLevelOfDetail::DETAILED); 436 MemoryDumpLevelOfDetail::DETAILED);
432 } 437 }
433 438
434 DisableTracing(); 439 DisableTracing();
435 } 440 }
436 441
437 // Sneakily registers an extra memory dump provider while an existing one is 442 // Sneakily registers an extra memory dump provider while an existing one is
438 // dumping and expect it to take part in the already active tracing session. 443 // dumping and expect it to take part in the already active tracing session.
439 TEST_F(MemoryDumpManagerTest, RegisterDumperWhileDumping) { 444 TEST_F(MemoryDumpManagerTest, RegisterDumperWhileDumping) {
440 InitializeMemoryDumpManager(false /* is_coordinator */); 445 InitializeMemoryDumpManager(false /* is_coordinator */);
441 MockMemoryDumpProvider mdp1; 446 MockMemoryDumpProvider mdp1;
(...skipping 14 matching lines...) Expand all
456 })) 461 }))
457 .WillRepeatedly(Return(true)); 462 .WillRepeatedly(Return(true));
458 463
459 // Depending on the insertion order (before or after mdp1), mdp2 might be 464 // Depending on the insertion order (before or after mdp1), mdp2 might be
460 // called also immediately after it gets registered. 465 // called also immediately after it gets registered.
461 EXPECT_CALL(mdp2, OnMemoryDump(_, _)) 466 EXPECT_CALL(mdp2, OnMemoryDump(_, _))
462 .Times(Between(2, 3)) 467 .Times(Between(2, 3))
463 .WillRepeatedly(Return(true)); 468 .WillRepeatedly(Return(true));
464 469
465 for (int i = 0; i < 4; i++) { 470 for (int i = 0; i < 4; i++) {
466 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED, 471 RequestGlobalDumpAndWait(MemoryDumpType::EXPLICITLY_TRIGGERED,
467 MemoryDumpLevelOfDetail::DETAILED); 472 MemoryDumpLevelOfDetail::DETAILED);
468 } 473 }
469 474
470 DisableTracing(); 475 DisableTracing();
471 } 476 }
472 477
473 // Like RegisterDumperWhileDumping, but unregister the dump provider instead. 478 // Like RegisterDumperWhileDumping, but unregister the dump provider instead.
474 TEST_F(MemoryDumpManagerTest, UnregisterDumperWhileDumping) { 479 TEST_F(MemoryDumpManagerTest, UnregisterDumperWhileDumping) {
475 InitializeMemoryDumpManager(false /* is_coordinator */); 480 InitializeMemoryDumpManager(false /* is_coordinator */);
476 MockMemoryDumpProvider mdp1; 481 MockMemoryDumpProvider mdp1;
477 MockMemoryDumpProvider mdp2; 482 MockMemoryDumpProvider mdp2;
(...skipping 14 matching lines...) Expand all
492 })) 497 }))
493 .WillRepeatedly(Return(true)); 498 .WillRepeatedly(Return(true));
494 499
495 // Depending on the insertion order (before or after mdp1), mdp2 might have 500 // Depending on the insertion order (before or after mdp1), mdp2 might have
496 // been already called when UnregisterDumpProvider happens. 501 // been already called when UnregisterDumpProvider happens.
497 EXPECT_CALL(mdp2, OnMemoryDump(_, _)) 502 EXPECT_CALL(mdp2, OnMemoryDump(_, _))
498 .Times(Between(1, 2)) 503 .Times(Between(1, 2))
499 .WillRepeatedly(Return(true)); 504 .WillRepeatedly(Return(true));
500 505
501 for (int i = 0; i < 4; i++) { 506 for (int i = 0; i < 4; i++) {
502 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED, 507 RequestGlobalDumpAndWait(MemoryDumpType::EXPLICITLY_TRIGGERED,
503 MemoryDumpLevelOfDetail::DETAILED); 508 MemoryDumpLevelOfDetail::DETAILED);
504 } 509 }
505 510
506 DisableTracing(); 511 DisableTracing();
507 } 512 }
508 513
509 // Checks that the dump does not abort when unregistering a provider while 514 // Checks that the dump does not abort when unregistering a provider while
510 // dumping from a different thread than the dumping thread. 515 // dumping from a different thread than the dumping thread.
511 TEST_F(MemoryDumpManagerTest, UnregisterDumperFromThreadWhileDumping) { 516 TEST_F(MemoryDumpManagerTest, UnregisterDumperFromThreadWhileDumping) {
512 InitializeMemoryDumpManager(false /* is_coordinator */); 517 InitializeMemoryDumpManager(false /* is_coordinator */);
513 ScopedVector<TestIOThread> threads; 518 ScopedVector<TestIOThread> threads;
514 ScopedVector<MockMemoryDumpProvider> mdps; 519 ScopedVector<MockMemoryDumpProvider> mdps;
515 520
516 for (int i = 0; i < 2; i++) { 521 for (int i = 0; i < 2; i++) {
517 threads.push_back(new TestIOThread(TestIOThread::kAutoStart)); 522 threads.push_back(new TestIOThread(TestIOThread::kAutoStart));
518 mdps.push_back(new MockMemoryDumpProvider()); 523 mdps.push_back(new MockMemoryDumpProvider());
519 RegisterDumpProvider(mdps.back(), threads.back()->task_runner()); 524 RegisterDumpProvider(mdps.back(), threads.back()->task_runner());
520 } 525 }
521 526
522 int on_memory_dump_call_count = 0; 527 int on_memory_dump_call_count = 0;
523 RunLoop run_loop;
524 528
525 // When OnMemoryDump is called on either of the dump providers, it will 529 // When OnMemoryDump is called on either of the dump providers, it will
526 // unregister the other one. 530 // unregister the other one.
527 for (MockMemoryDumpProvider* mdp : mdps) { 531 for (MockMemoryDumpProvider* mdp : mdps) {
528 int other_idx = (mdps.front() == mdp); 532 int other_idx = (mdps.front() == mdp);
529 TestIOThread* other_thread = threads[other_idx]; 533 TestIOThread* other_thread = threads[other_idx];
530 MockMemoryDumpProvider* other_mdp = mdps[other_idx]; 534 MockMemoryDumpProvider* other_mdp = mdps[other_idx];
531 auto on_dump = [this, other_thread, other_mdp, &on_memory_dump_call_count]( 535 auto on_dump = [this, other_thread, other_mdp, &on_memory_dump_call_count](
532 const MemoryDumpArgs& args, ProcessMemoryDump* pmd) { 536 const MemoryDumpArgs& args, ProcessMemoryDump* pmd) {
533 other_thread->PostTaskAndWait( 537 other_thread->PostTaskAndWait(
534 FROM_HERE, base::Bind(&MemoryDumpManager::UnregisterDumpProvider, 538 FROM_HERE, base::Bind(&MemoryDumpManager::UnregisterDumpProvider,
535 base::Unretained(&*mdm_), other_mdp)); 539 base::Unretained(&*mdm_), other_mdp));
536 on_memory_dump_call_count++; 540 on_memory_dump_call_count++;
537 return true; 541 return true;
538 }; 542 };
539 543
540 // OnMemoryDump is called once for the provider that dumps first, and zero 544 // OnMemoryDump is called once for the provider that dumps first, and zero
541 // times for the other provider. 545 // times for the other provider.
542 EXPECT_CALL(*mdp, OnMemoryDump(_, _)) 546 EXPECT_CALL(*mdp, OnMemoryDump(_, _))
543 .Times(AtMost(1)) 547 .Times(AtMost(1))
544 .WillOnce(Invoke(on_dump)); 548 .WillOnce(Invoke(on_dump));
545 } 549 }
546 550
547 last_callback_success_ = false; 551 last_callback_success_ = false;
548 MemoryDumpCallback callback =
549 Bind(&MemoryDumpManagerTest::DumpCallbackAdapter, Unretained(this),
550 MessageLoop::current()->task_runner(), run_loop.QuitClosure());
551
552 EnableTracingWithLegacyCategories(MemoryDumpManager::kTraceCategory); 552 EnableTracingWithLegacyCategories(MemoryDumpManager::kTraceCategory);
553
554 EXPECT_CALL(*delegate_, RequestGlobalMemoryDump(_, _)).Times(1); 553 EXPECT_CALL(*delegate_, RequestGlobalMemoryDump(_, _)).Times(1);
555 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED, 554 RequestGlobalDumpAndWait(MemoryDumpType::EXPLICITLY_TRIGGERED,
556 MemoryDumpLevelOfDetail::DETAILED, callback); 555 MemoryDumpLevelOfDetail::DETAILED);
557
558 run_loop.Run();
559
560 ASSERT_EQ(1, on_memory_dump_call_count); 556 ASSERT_EQ(1, on_memory_dump_call_count);
561 ASSERT_EQ(true, last_callback_success_); 557 ASSERT_EQ(true, last_callback_success_);
562 558
563 DisableTracing(); 559 DisableTracing();
564 } 560 }
565 561
566 // Checks that a NACK callback is invoked if RequestGlobalDump() is called when 562 // Checks that a NACK callback is invoked if RequestGlobalDump() is called when
567 // tracing is not enabled. 563 // tracing is not enabled.
568 TEST_F(MemoryDumpManagerTest, CallbackCalledOnFailure) { 564 TEST_F(MemoryDumpManagerTest, CallbackCalledOnFailure) {
569 InitializeMemoryDumpManager(false /* is_coordinator */); 565 InitializeMemoryDumpManager(false /* is_coordinator */);
570 MockMemoryDumpProvider mdp1; 566 MockMemoryDumpProvider mdp1;
571 RegisterDumpProvider(&mdp1); 567 RegisterDumpProvider(&mdp1);
572 568
573 EXPECT_CALL(*delegate_, RequestGlobalMemoryDump(_, _)).Times(0); 569 EXPECT_CALL(*delegate_, RequestGlobalMemoryDump(_, _)).Times(0);
574 EXPECT_CALL(mdp1, OnMemoryDump(_, _)).Times(0); 570 EXPECT_CALL(mdp1, OnMemoryDump(_, _)).Times(0);
575 571
576 last_callback_success_ = true; 572 last_callback_success_ = true;
577 { 573 RequestGlobalDumpAndWait(MemoryDumpType::EXPLICITLY_TRIGGERED,
578 RunLoop run_loop; 574 MemoryDumpLevelOfDetail::DETAILED);
579 MemoryDumpCallback callback =
580 Bind(&MemoryDumpManagerTest::DumpCallbackAdapter, Unretained(this),
581 MessageLoop::current()->task_runner(), run_loop.QuitClosure());
582 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED,
583 MemoryDumpLevelOfDetail::DETAILED, callback);
584 run_loop.Run();
585 }
586 EXPECT_FALSE(last_callback_success_); 575 EXPECT_FALSE(last_callback_success_);
587 } 576 }
588 577
589 // Checks that is the MemoryDumpManager is initialized after tracing already 578 // Checks that is the MemoryDumpManager is initialized after tracing already
590 // began, it will still late-join the party (real use case: startup tracing). 579 // began, it will still late-join the party (real use case: startup tracing).
591 TEST_F(MemoryDumpManagerTest, InitializedAfterStartOfTracing) { 580 TEST_F(MemoryDumpManagerTest, InitializedAfterStartOfTracing) {
592 MockMemoryDumpProvider mdp; 581 MockMemoryDumpProvider mdp;
593 RegisterDumpProvider(&mdp); 582 RegisterDumpProvider(&mdp);
594 EnableTracingWithLegacyCategories(MemoryDumpManager::kTraceCategory); 583 EnableTracingWithLegacyCategories(MemoryDumpManager::kTraceCategory);
595 584
596 // First check that a RequestGlobalDump() issued before the MemoryDumpManager 585 // First check that a RequestGlobalDump() issued before the MemoryDumpManager
597 // initialization gets NACK-ed cleanly. 586 // initialization gets NACK-ed cleanly.
598 { 587 {
599 EXPECT_CALL(mdp, OnMemoryDump(_, _)).Times(0); 588 EXPECT_CALL(mdp, OnMemoryDump(_, _)).Times(0);
600 EXPECT_CALL(*delegate_, RequestGlobalMemoryDump(_, _)).Times(0); 589 EXPECT_CALL(*delegate_, RequestGlobalMemoryDump(_, _)).Times(0);
601 RunLoop run_loop; 590 RequestGlobalDumpAndWait(MemoryDumpType::EXPLICITLY_TRIGGERED,
602 MemoryDumpCallback callback = 591 MemoryDumpLevelOfDetail::DETAILED);
603 Bind(&MemoryDumpManagerTest::DumpCallbackAdapter, Unretained(this),
604 MessageLoop::current()->task_runner(), run_loop.QuitClosure());
605 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED,
606 MemoryDumpLevelOfDetail::DETAILED, callback);
607 run_loop.Run();
608 EXPECT_FALSE(last_callback_success_); 592 EXPECT_FALSE(last_callback_success_);
609 } 593 }
610 594
611 // Now late-initialize the MemoryDumpManager and check that the 595 // Now late-initialize the MemoryDumpManager and check that the
612 // RequestGlobalDump completes successfully. 596 // RequestGlobalDump completes successfully.
613 { 597 {
614 EXPECT_CALL(mdp, OnMemoryDump(_, _)).Times(1); 598 EXPECT_CALL(mdp, OnMemoryDump(_, _)).Times(1);
615 EXPECT_CALL(*delegate_, RequestGlobalMemoryDump(_, _)).Times(1); 599 EXPECT_CALL(*delegate_, RequestGlobalMemoryDump(_, _)).Times(1);
616 InitializeMemoryDumpManager(false /* is_coordinator */); 600 InitializeMemoryDumpManager(false /* is_coordinator */);
617 RunLoop run_loop; 601 RequestGlobalDumpAndWait(MemoryDumpType::EXPLICITLY_TRIGGERED,
618 MemoryDumpCallback callback = 602 MemoryDumpLevelOfDetail::DETAILED);
619 Bind(&MemoryDumpManagerTest::DumpCallbackAdapter, Unretained(this),
620 MessageLoop::current()->task_runner(), run_loop.QuitClosure());
621 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED,
622 MemoryDumpLevelOfDetail::DETAILED, callback);
623 run_loop.Run();
624 EXPECT_TRUE(last_callback_success_); 603 EXPECT_TRUE(last_callback_success_);
625 } 604 }
626 DisableTracing(); 605 DisableTracing();
627 } 606 }
628 607
629 // This test (and the MemoryDumpManagerTestCoordinator below) crystallizes the 608 // This test (and the MemoryDumpManagerTestCoordinator below) crystallizes the
630 // expectations of the chrome://tracing UI and chrome telemetry w.r.t. periodic 609 // expectations of the chrome://tracing UI and chrome telemetry w.r.t. periodic
631 // dumps in memory-infra, handling gracefully the transition between the legacy 610 // dumps in memory-infra, handling gracefully the transition between the legacy
632 // and the new-style (JSON-based) TraceConfig. 611 // and the new-style (JSON-based) TraceConfig.
633 TEST_F(MemoryDumpManagerTest, TraceConfigExpectations) { 612 TEST_F(MemoryDumpManagerTest, TraceConfigExpectations) {
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
711 // Swallow all the final spurious calls until tracing gets disabled. 690 // Swallow all the final spurious calls until tracing gets disabled.
712 EXPECT_CALL(delegate, RequestGlobalMemoryDump(_, _)).Times(AnyNumber()); 691 EXPECT_CALL(delegate, RequestGlobalMemoryDump(_, _)).Times(AnyNumber());
713 692
714 EnableTracingWithTraceConfig( 693 EnableTracingWithTraceConfig(
715 TraceConfigMemoryTestUtil::GetTraceConfig_PeriodicTriggers( 694 TraceConfigMemoryTestUtil::GetTraceConfig_PeriodicTriggers(
716 kLightDumpPeriodMs, kHeavyDumpPeriodMs)); 695 kLightDumpPeriodMs, kHeavyDumpPeriodMs));
717 run_loop.Run(); 696 run_loop.Run();
718 DisableTracing(); 697 DisableTracing();
719 } 698 }
720 699
700 // Tests against race conditions that might arise when disabling tracing in the
701 // middle of a global memory dump.
702 TEST_F(MemoryDumpManagerTest, DisableTracingWhileDumping) {
703 base::WaitableEvent tracing_disabled_event(false, false);
704 InitializeMemoryDumpManager(false /* is_coordinator */);
705
706 // Register a bound dump provider.
707 scoped_ptr<Thread> mdp_thread(new Thread("test thread"));
708 mdp_thread->Start();
709 MockMemoryDumpProvider mdp_with_affinity;
710 RegisterDumpProvider(&mdp_with_affinity, mdp_thread->task_runner());
711
712 // Register also an unbound dump provider. Unbound dump providers are always
713 // invoked after bound ones.
714 MockMemoryDumpProvider unbound_mdp;
715 RegisterDumpProvider(&unbound_mdp);
716
717 EnableTracingWithLegacyCategories(MemoryDumpManager::kTraceCategory);
718 EXPECT_CALL(*delegate_, RequestGlobalMemoryDump(_, _)).Times(1);
719 EXPECT_CALL(mdp_with_affinity, OnMemoryDump(_, _))
720 .Times(1)
721 .WillOnce(
722 Invoke([&tracing_disabled_event](const MemoryDumpArgs&,
723 ProcessMemoryDump* pmd) -> bool {
724 tracing_disabled_event.Wait();
725
726 // At this point tracing has been disabled and the
727 // MemoryDumpManager.dump_thread_ has been shut down.
728 return true;
729 }));
730
731 // |unbound_mdp| should never be invoked because the thread for unbound dump
732 // providers has been shutdown in the meanwhile.
733 EXPECT_CALL(unbound_mdp, OnMemoryDump(_, _)).Times(0);
734
735 last_callback_success_ = true;
736 RunLoop run_loop;
737 MemoryDumpCallback callback =
738 Bind(&MemoryDumpManagerTest::DumpCallbackAdapter, Unretained(this),
739 MessageLoop::current()->task_runner(), run_loop.QuitClosure());
740 mdm_->RequestGlobalDump(MemoryDumpType::EXPLICITLY_TRIGGERED,
741 MemoryDumpLevelOfDetail::DETAILED, callback);
742 DisableTracing();
743 tracing_disabled_event.Signal();
744 run_loop.Run();
745
746 // RequestGlobalMemoryDump() should be NACK-ed because one of the threads
747 // threads died before we had a chance to PostTask onto them.
748 EXPECT_FALSE(last_callback_success_);
749 }
750
721 } // namespace trace_event 751 } // namespace trace_event
722 } // namespace base 752 } // namespace base
OLDNEW
« no previous file with comments | « base/trace_event/memory_dump_manager.cc ('k') | components/tracing/child_trace_message_filter_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698