OLD | NEW |
---|---|
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 <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <utility> | 10 #include <utility> |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
111 task_runner->PostTask(from_here, std::move(task)); | 111 task_runner->PostTask(from_here, std::move(task)); |
112 task_runner->PostTask(FROM_HERE, base::BindOnce(&WaitableEvent::Signal, | 112 task_runner->PostTask(FROM_HERE, base::BindOnce(&WaitableEvent::Signal, |
113 base::Unretained(&event))); | 113 base::Unretained(&event))); |
114 // The SequencedTaskRunner guarantees that |event| will only be signaled after | 114 // The SequencedTaskRunner guarantees that |event| will only be signaled after |
115 // |task| is executed. | 115 // |task| is executed. |
116 event.Wait(); | 116 event.Wait(); |
117 } | 117 } |
118 | 118 |
119 // Adapts a ProcessMemoryDumpCallback into a GlobalMemoryDumpCallback by | 119 // Adapts a ProcessMemoryDumpCallback into a GlobalMemoryDumpCallback by |
120 // trimming off the result argument and calling the global callback. | 120 // trimming off the result argument and calling the global callback. |
121 // TODO (fmeawad): we should keep the results for verification, but currently | |
122 // all results are empty. | 121 // all results are empty. |
Primiano Tucci (use gerrit)
2017/04/28 15:04:33
are you intending to keep this "all results are em
hjd
2017/05/02 12:38:37
oops, thanks!
Done.
| |
123 void ProcessDumpCallbackAdapter( | 122 void ProcessDumpCallbackAdapter( |
124 GlobalMemoryDumpCallback callback, | 123 GlobalMemoryDumpCallback callback, |
125 uint64_t dump_guid, | 124 uint64_t dump_guid, |
126 bool success, | 125 bool success, |
127 const base::Optional<base::trace_event::MemoryDumpCallbackResult>&) { | 126 const base::Optional<base::trace_event::MemoryDumpCallbackResult>&) { |
128 callback.Run(dump_guid, success); | 127 callback.Run(dump_guid, success); |
129 } | 128 } |
130 | 129 |
131 // This mocks the RequestGlobalDumpFunction which is typically handled by | 130 // This mocks the RequestGlobalDumpFunction which is typically handled by |
132 // process_local_dump_manager_impl.cc, by short-circuiting dump requests locally | 131 // process_local_dump_manager_impl.cc, by short-circuiting dump requests locally |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
224 } // namespace | 223 } // namespace |
225 | 224 |
226 class MemoryDumpManagerTest : public testing::Test { | 225 class MemoryDumpManagerTest : public testing::Test { |
227 public: | 226 public: |
228 MemoryDumpManagerTest() : testing::Test(), kDefaultOptions() {} | 227 MemoryDumpManagerTest() : testing::Test(), kDefaultOptions() {} |
229 | 228 |
230 void SetUp() override { | 229 void SetUp() override { |
231 last_callback_success_ = false; | 230 last_callback_success_ = false; |
232 message_loop_.reset(new MessageLoop()); | 231 message_loop_.reset(new MessageLoop()); |
233 mdm_.reset(new MemoryDumpManager()); | 232 mdm_.reset(new MemoryDumpManager()); |
233 results_.clear(); | |
234 MemoryDumpManager::SetInstanceForTesting(mdm_.get()); | 234 MemoryDumpManager::SetInstanceForTesting(mdm_.get()); |
235 ASSERT_EQ(mdm_.get(), MemoryDumpManager::GetInstance()); | 235 ASSERT_EQ(mdm_.get(), MemoryDumpManager::GetInstance()); |
236 } | 236 } |
237 | 237 |
238 void TearDown() override { | 238 void TearDown() override { |
239 MemoryDumpManager::SetInstanceForTesting(nullptr); | 239 MemoryDumpManager::SetInstanceForTesting(nullptr); |
240 mdm_.reset(); | 240 mdm_.reset(); |
241 message_loop_.reset(); | 241 message_loop_.reset(); |
242 TraceLog::DeleteForTesting(); | 242 TraceLog::DeleteForTesting(); |
243 } | 243 } |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
286 void DisableTracing() { TraceLog::GetInstance()->SetDisabled(); } | 286 void DisableTracing() { TraceLog::GetInstance()->SetDisabled(); } |
287 | 287 |
288 bool IsPeriodicDumpingEnabled() const { | 288 bool IsPeriodicDumpingEnabled() const { |
289 return MemoryDumpScheduler::GetInstance()->is_enabled_for_testing(); | 289 return MemoryDumpScheduler::GetInstance()->is_enabled_for_testing(); |
290 } | 290 } |
291 | 291 |
292 int GetMaxConsecutiveFailuresCount() const { | 292 int GetMaxConsecutiveFailuresCount() const { |
293 return MemoryDumpManager::kMaxConsecutiveFailuresCount; | 293 return MemoryDumpManager::kMaxConsecutiveFailuresCount; |
294 } | 294 } |
295 | 295 |
296 const std::vector<MemoryDumpCallbackResult>* GetResults() const { | |
297 return &results_; | |
298 } | |
299 | |
296 const MemoryDumpProvider::Options kDefaultOptions; | 300 const MemoryDumpProvider::Options kDefaultOptions; |
297 std::unique_ptr<MemoryDumpManager> mdm_; | 301 std::unique_ptr<MemoryDumpManager> mdm_; |
298 GlobalMemoryDumpHandler global_dump_handler_; | 302 GlobalMemoryDumpHandler global_dump_handler_; |
299 bool last_callback_success_; | 303 bool last_callback_success_; |
300 | 304 |
305 // Adapts a ProcessMemoryDumpCallback into a GlobalMemoryDumpCallback by | |
306 // trimming off the result argument and calling the global callback. | |
Primiano Tucci (use gerrit)
2017/04/28 15:04:33
"trimming off" the result confused me a bit before
hjd
2017/05/02 12:38:37
Done.
| |
307 // all results are empty. | |
308 void ProcessDumpRecordingCallbackAdapter( | |
309 GlobalMemoryDumpCallback callback, | |
310 uint64_t dump_guid, | |
311 bool success, | |
312 const base::Optional<MemoryDumpCallbackResult>& result) { | |
313 if (result.has_value()) { | |
314 results_.push_back(result.value()); | |
315 } | |
316 callback.Run(dump_guid, success); | |
317 } | |
318 | |
301 private: | 319 private: |
302 std::unique_ptr<MessageLoop> message_loop_; | 320 std::unique_ptr<MessageLoop> message_loop_; |
321 std::vector<MemoryDumpCallbackResult> results_; | |
303 | 322 |
304 // We want our singleton torn down after each test. | 323 // We want our singleton torn down after each test. |
305 ShadowingAtExitManager at_exit_manager_; | 324 ShadowingAtExitManager at_exit_manager_; |
306 }; | 325 }; |
307 | 326 |
308 // Basic sanity checks. Registers a memory dump provider and checks that it is | 327 // Basic sanity checks. Registers a memory dump provider and checks that it is |
309 // called, but only when memory-infra is enabled. | 328 // called, but only when memory-infra is enabled. |
310 TEST_F(MemoryDumpManagerTest, SingleDumper) { | 329 TEST_F(MemoryDumpManagerTest, SingleDumper) { |
311 InitializeMemoryDumpManager(false /* is_coordinator */); | 330 InitializeMemoryDumpManager(false /* is_coordinator */); |
312 MockMemoryDumpProvider mdp; | 331 MockMemoryDumpProvider mdp; |
(...skipping 962 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1275 // The callback result should actually be false since (for the moment at | 1294 // The callback result should actually be false since (for the moment at |
1276 // least) a true result means that as well as the dump generally being | 1295 // least) a true result means that as well as the dump generally being |
1277 // successful we also managed to add the dump to the trace. | 1296 // successful we also managed to add the dump to the trace. |
1278 EXPECT_FALSE(last_callback_success_); | 1297 EXPECT_FALSE(last_callback_success_); |
1279 | 1298 |
1280 mdm_->Disable(); | 1299 mdm_->Disable(); |
1281 | 1300 |
1282 mdm_->UnregisterDumpProvider(&mdp); | 1301 mdm_->UnregisterDumpProvider(&mdp); |
1283 } | 1302 } |
1284 | 1303 |
1304 TEST_F(MemoryDumpManagerTest, TestComputingSummary) { | |
Primiano Tucci (use gerrit)
2017/04/28 15:04:33
s/TestComputingSummary/TestSummaryComputation/
ju
hjd
2017/05/02 12:38:37
Done.
| |
1305 InitializeMemoryDumpManager(false /* is_coordinator */); | |
1306 MockMemoryDumpProvider mdp; | |
1307 RegisterDumpProvider(&mdp, ThreadTaskRunnerHandle::Get()); | |
1308 | |
1309 const MemoryDumpSessionState* session_state = | |
1310 mdm_->session_state_for_testing().get(); | |
1311 | |
1312 EXPECT_CALL(global_dump_handler_, RequestGlobalMemoryDump(_, _)) | |
1313 .WillOnce(Invoke([this](const MemoryDumpRequestArgs& args, | |
1314 const GlobalMemoryDumpCallback& callback) { | |
1315 ProcessMemoryDumpCallback process_callback = | |
1316 Bind(&MemoryDumpManagerTest_TestComputingSummary_Test:: | |
1317 ProcessDumpRecordingCallbackAdapter, | |
1318 Unretained(this), callback); | |
1319 mdm_->CreateProcessDump(args, process_callback); | |
1320 })); | |
1321 | |
1322 EXPECT_CALL(mdp, OnMemoryDump(_, _)) | |
1323 .Times(1) | |
1324 .WillRepeatedly(Invoke([session_state](const MemoryDumpArgs&, | |
1325 ProcessMemoryDump* pmd) -> bool { | |
1326 auto* size = MemoryAllocatorDump::kNameSize; | |
1327 auto* bytes = MemoryAllocatorDump::kUnitsBytes; | |
1328 auto kb = 1024; | |
Primiano Tucci (use gerrit)
2017/04/28 15:04:33
const uint32_t kB = 1024;
kb -> kB will both match
hjd
2017/05/02 12:38:37
Done.
| |
1329 | |
1330 pmd->CreateAllocatorDump("malloc")->AddScalar(size, bytes, 1 * kb); | |
1331 pmd->CreateAllocatorDump("malloc/foo")->AddScalar(size, bytes, 99 * kb); | |
1332 | |
1333 pmd->CreateAllocatorDump("blink_gc")->AddScalar(size, bytes, 2 * kb); | |
1334 pmd->CreateAllocatorDump("blink_gc/foo") | |
1335 ->AddScalar(size, bytes, 99 * kb); | |
1336 | |
1337 pmd->CreateAllocatorDump("v8/foo")->AddScalar(size, bytes, 1 * kb); | |
1338 pmd->CreateAllocatorDump("v8/bar")->AddScalar(size, bytes, 2 * kb); | |
1339 pmd->CreateAllocatorDump("v8")->AddScalar(size, bytes, 99 * kb); | |
1340 | |
1341 pmd->CreateAllocatorDump("partition_alloc") | |
1342 ->AddScalar(size, bytes, 99 * kb); | |
1343 pmd->CreateAllocatorDump("partition_alloc/partitions") | |
1344 ->AddScalar(size, bytes, 99 * kb); | |
1345 pmd->CreateAllocatorDump("partition_alloc/partitions/foo") | |
1346 ->AddScalar(size, bytes, 2 * kb); | |
1347 pmd->CreateAllocatorDump("partition_alloc/partitions/bar") | |
1348 ->AddScalar(size, bytes, 2 * kb); | |
1349 pmd->process_totals()->set_resident_set_bytes(5 * kb); | |
1350 pmd->set_has_process_totals(); | |
1351 return true; | |
1352 })); | |
1353 | |
1354 last_callback_success_ = false; | |
1355 | |
1356 EnableTracingWithLegacyCategories(MemoryDumpManager::kTraceCategory); | |
1357 RequestGlobalDumpAndWait(MemoryDumpType::EXPLICITLY_TRIGGERED, | |
1358 MemoryDumpLevelOfDetail::LIGHT); | |
1359 DisableTracing(); | |
1360 | |
Primiano Tucci (use gerrit)
2017/04/28 15:04:33
can you add a comment explaining why the 99 doesn'
hjd
2017/05/02 12:38:36
Yeah XD I ended up wrapping some of the others any
| |
1361 EXPECT_TRUE(last_callback_success_); | |
1362 ASSERT_EQ(1u, GetResults()->size()); | |
1363 MemoryDumpCallbackResult result = GetResults()->front(); | |
1364 EXPECT_EQ(1u, result.chrome_dump.malloc_total_kb); | |
1365 EXPECT_EQ(2u, result.chrome_dump.blink_gc_total_kb); | |
1366 EXPECT_EQ(3u, result.chrome_dump.v8_total_kb); | |
1367 EXPECT_EQ(4u, result.chrome_dump.partition_alloc_total_kb); | |
1368 EXPECT_EQ(5u, result.os_dump.resident_set_kb); | |
1369 }; | |
1370 | |
1285 } // namespace trace_event | 1371 } // namespace trace_event |
1286 } // namespace base | 1372 } // namespace base |
OLD | NEW |