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

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

Issue 2777343003: [memory-infra] Add api to enable heap profiling in MemoryDumpManager
Patch Set: nits. Created 3 years, 8 months 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
« no previous file with comments | « base/trace_event/memory_dump_manager.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 <stdint.h> 7 #include <stdint.h>
8 8
9 #include <memory> 9 #include <memory>
10 #include <vector> 10 #include <vector>
11 11
12 #include "base/base_switches.h"
12 #include "base/bind_helpers.h" 13 #include "base/bind_helpers.h"
14 #include "base/command_line.h"
13 #include "base/memory/ptr_util.h" 15 #include "base/memory/ptr_util.h"
14 #include "base/memory/ref_counted_memory.h" 16 #include "base/memory/ref_counted_memory.h"
15 #include "base/message_loop/message_loop.h" 17 #include "base/message_loop/message_loop.h"
16 #include "base/run_loop.h" 18 #include "base/run_loop.h"
17 #include "base/strings/stringprintf.h" 19 #include "base/strings/stringprintf.h"
18 #include "base/synchronization/waitable_event.h" 20 #include "base/synchronization/waitable_event.h"
19 #include "base/test/sequenced_worker_pool_owner.h" 21 #include "base/test/sequenced_worker_pool_owner.h"
20 #include "base/test/test_io_thread.h" 22 #include "base/test/test_io_thread.h"
21 #include "base/test/trace_event_analyzer.h" 23 #include "base/test/trace_event_analyzer.h"
22 #include "base/threading/platform_thread.h" 24 #include "base/threading/platform_thread.h"
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
137 139
138 private: 140 private:
139 bool is_coordinator_; 141 bool is_coordinator_;
140 }; 142 };
141 143
142 class MockMemoryDumpProvider : public MemoryDumpProvider { 144 class MockMemoryDumpProvider : public MemoryDumpProvider {
143 public: 145 public:
144 MOCK_METHOD0(Destructor, void()); 146 MOCK_METHOD0(Destructor, void());
145 MOCK_METHOD2(OnMemoryDump, 147 MOCK_METHOD2(OnMemoryDump,
146 bool(const MemoryDumpArgs& args, ProcessMemoryDump* pmd)); 148 bool(const MemoryDumpArgs& args, ProcessMemoryDump* pmd));
149 MOCK_METHOD1(OnHeapProfilingEnabled, void(bool enabled));
147 MOCK_METHOD1(PollFastMemoryTotal, void(uint64_t* memory_total)); 150 MOCK_METHOD1(PollFastMemoryTotal, void(uint64_t* memory_total));
148 MOCK_METHOD0(SuspendFastMemoryPolling, void()); 151 MOCK_METHOD0(SuspendFastMemoryPolling, void());
149 152
150 MockMemoryDumpProvider() : enable_mock_destructor(false) { 153 MockMemoryDumpProvider() : enable_mock_destructor(false) {
151 ON_CALL(*this, OnMemoryDump(_, _)) 154 ON_CALL(*this, OnMemoryDump(_, _))
152 .WillByDefault(Invoke([](const MemoryDumpArgs&, 155 .WillByDefault(Invoke([](const MemoryDumpArgs&,
153 ProcessMemoryDump* pmd) -> bool { 156 ProcessMemoryDump* pmd) -> bool {
154 // |session_state| should not be null under any circumstances when 157 // |session_state| should not be null under any circumstances when
155 // invoking a memory dump. The problem might arise in race conditions 158 // invoking a memory dump. The problem might arise in race conditions
156 // like crbug.com/600570 . 159 // like crbug.com/600570 .
(...skipping 1128 matching lines...) Expand 10 before | Expand all | Expand 10 after
1285 1288
1286 Thread thread("test thread"); 1289 Thread thread("test thread");
1287 thread.Start(); 1290 thread.Start();
1288 RegisterDumpProvider(&mdp1, thread.task_runner(), kDefaultOptions, 1291 RegisterDumpProvider(&mdp1, thread.task_runner(), kDefaultOptions,
1289 "BlacklistTestDumpProvider"); 1292 "BlacklistTestDumpProvider");
1290 // Unregistering on wrong thread should not crash. 1293 // Unregistering on wrong thread should not crash.
1291 mdm_->UnregisterDumpProvider(&mdp1); 1294 mdm_->UnregisterDumpProvider(&mdp1);
1292 thread.Stop(); 1295 thread.Stop();
1293 } 1296 }
1294 1297
1298 TEST_F(MemoryDumpManagerTest, EnableHeapProfiling_PSEUDO_STACK) {
1299 InitializeMemoryDumpManager(false /* is_coordinator */);
1300 MockMemoryDumpProvider mdp1;
1301 RegisterDumpProvider(&mdp1, nullptr);
1302 testing::InSequence sequence;
1303 EXPECT_CALL(mdp1, OnHeapProfilingEnabled(true)).Times(1);
1304 EXPECT_CALL(mdp1, OnHeapProfilingEnabled(false)).Times(1);
1305
1306 EnableTracingWithLegacyCategories(MemoryDumpManager::kTraceCategory);
1307 mdm_->EnableHeapProfiling(
1308 AllocationContextTracker::CaptureMode::PSEUDO_STACK);
1309 ASSERT_EQ(AllocationContextTracker::CaptureMode::DISABLED,
1310 AllocationContextTracker::capture_mode());
1311 DisableTracing();
1312
1313 mdm_->EnableHeapProfiling(
1314 AllocationContextTracker::CaptureMode::PSEUDO_STACK);
1315 ASSERT_EQ(AllocationContextTracker::CaptureMode::PSEUDO_STACK,
1316 AllocationContextTracker::capture_mode());
1317 mdm_->EnableHeapProfiling(AllocationContextTracker::CaptureMode::DISABLED);
1318 ASSERT_EQ(AllocationContextTracker::CaptureMode::DISABLED,
1319 AllocationContextTracker::capture_mode());
1320 mdm_->EnableHeapProfiling(
1321 AllocationContextTracker::CaptureMode::PSEUDO_STACK);
1322 ASSERT_EQ(AllocationContextTracker::CaptureMode::DISABLED,
1323 AllocationContextTracker::capture_mode());
1324 }
1325
1326 TEST_F(MemoryDumpManagerTest, EnableHeapProfiling_BACKGROUND) {
1327 InitializeMemoryDumpManager(true /* is_coordinator */);
1328 MockMemoryDumpProvider mdp1;
1329 RegisterDumpProvider(&mdp1, nullptr);
1330 testing::InSequence sequence;
1331 EXPECT_CALL(mdp1, OnHeapProfilingEnabled(true)).Times(1);
1332 EXPECT_CALL(mdp1, OnHeapProfilingEnabled(false)).Times(1);
1333
1334 mdm_->EnableHeapProfiling(AllocationContextTracker::CaptureMode::BACKGROUND);
1335 ASSERT_EQ(AllocationContextTracker::CaptureMode::BACKGROUND,
1336 AllocationContextTracker::capture_mode());
1337 mdm_->EnableHeapProfiling(AllocationContextTracker::CaptureMode::DISABLED);
1338 ASSERT_EQ(AllocationContextTracker::CaptureMode::DISABLED,
1339 AllocationContextTracker::capture_mode());
1340 mdm_->EnableHeapProfiling(
1341 AllocationContextTracker::CaptureMode::PSEUDO_STACK);
1342 ASSERT_EQ(AllocationContextTracker::CaptureMode::DISABLED,
1343 AllocationContextTracker::capture_mode());
1344 }
1345
1346 TEST_F(MemoryDumpManagerTest, EnableHeapProfilingIfNeeded) {
1347 InitializeMemoryDumpManager(false /* is_coordinator */);
1348 MockMemoryDumpProvider mdp1;
1349 RegisterDumpProvider(&mdp1, nullptr);
1350 testing::InSequence sequence;
1351 EXPECT_CALL(mdp1, OnHeapProfilingEnabled(true)).Times(1);
1352 EXPECT_CALL(mdp1, OnHeapProfilingEnabled(false)).Times(1);
1353
1354 CommandLine* cmdline = CommandLine::ForCurrentProcess();
1355 cmdline->AppendSwitchASCII(switches::kEnableHeapProfiling, "");
1356 mdm_->EnableHeapProfilingIfNeeded();
1357 ASSERT_EQ(AllocationContextTracker::CaptureMode::PSEUDO_STACK,
1358 AllocationContextTracker::capture_mode());
1359 mdm_->EnableHeapProfiling(AllocationContextTracker::CaptureMode::DISABLED);
1360 ASSERT_EQ(AllocationContextTracker::CaptureMode::DISABLED,
1361 AllocationContextTracker::capture_mode());
1362 mdm_->EnableHeapProfiling(
1363 AllocationContextTracker::CaptureMode::PSEUDO_STACK);
1364 ASSERT_EQ(AllocationContextTracker::CaptureMode::DISABLED,
1365 AllocationContextTracker::capture_mode());
1366 }
1367
1295 } // namespace trace_event 1368 } // namespace trace_event
1296 } // namespace base 1369 } // namespace base
OLDNEW
« no previous file with comments | « base/trace_event/memory_dump_manager.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698