Chromium Code Reviews| 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/bind.h" | |
| 6 #include "base/command_line.h" | |
| 7 #include "base/run_loop.h" | |
| 8 #include "base/trace_event/memory_dump_manager.h" | |
| 9 #include "base/trace_event/memory_dump_provider.h" | |
| 10 #include "base/trace_event/memory_dump_request_args.h" | |
| 11 #include "content/public/browser/tracing_controller.h" | |
| 12 #include "content/public/common/content_switches.h" | |
| 13 #include "content/public/test/browser_test_utils.h" | |
| 14 #include "content/public/test/content_browser_test.h" | |
| 15 #include "content/public/test/content_browser_test_utils.h" | |
| 16 #include "content/shell/browser/shell.h" | |
| 17 #include "testing/gmock/include/gmock/gmock.h" | |
| 18 | |
| 19 using base::trace_event::MemoryDumpArgs; | |
| 20 using base::trace_event::MemoryDumpManager; | |
| 21 using base::trace_event::MemoryDumpType; | |
| 22 using base::trace_event::ProcessMemoryDump; | |
| 23 using testing::_; | |
| 24 using testing::Gt; | |
|
Primiano Tucci (use gerrit)
2015/08/14 14:01:06
I think you don't need this anymore
Ruud van Asseldonk
2015/08/14 15:47:01
Done.
| |
| 25 using testing::InvokeWithoutArgs; | |
|
Primiano Tucci (use gerrit)
2015/08/14 14:01:06
ditto
Ruud van Asseldonk
2015/08/14 15:47:01
Done.
| |
| 26 using testing::Return; | |
| 27 | |
| 28 namespace content { | |
| 29 | |
| 30 // A mock dump provider, used to check that dump requests actually end up | |
| 31 // creating memory dumps. | |
| 32 class MockDumpProvider : public base::trace_event::MemoryDumpProvider { | |
| 33 public: | |
| 34 MOCK_METHOD2(OnMemoryDump, bool(const MemoryDumpArgs& args, | |
| 35 ProcessMemoryDump* pmd)); | |
| 36 }; | |
| 37 | |
| 38 class MemoryTracingTest : public ContentBrowserTest { | |
| 39 public: | |
| 40 void DoRequestGlobalDump(const base::trace_event::MemoryDumpCallback& cb) { | |
| 41 MemoryDumpArgs dump_args = { MemoryDumpArgs::LevelOfDetail::HIGH }; | |
| 42 MemoryDumpManager::GetInstance()->RequestGlobalDump( | |
| 43 MemoryDumpType::EXPLICITLY_TRIGGERED, dump_args, cb); | |
| 44 } | |
| 45 | |
| 46 // Used as callback argument for MemoryDumpManager::RequestGlobalDump(): | |
| 47 void OnGlobalMemoryDumpDone( | |
| 48 scoped_refptr<base::SingleThreadTaskRunner> task_runner, | |
| 49 base::Closure closure, | |
| 50 uint64 dump_guid, | |
| 51 bool success) { | |
| 52 // Make sure we run the RunLoop closure on the same thread that originated | |
| 53 // the run loop (which is the IN_PROC_BROWSER_TEST_F main thread). | |
| 54 if (!task_runner->RunsTasksOnCurrentThread()) { | |
| 55 task_runner->PostTask( | |
| 56 FROM_HERE, base::Bind(&MemoryTracingTest::OnGlobalMemoryDumpDone, | |
| 57 base::Unretained(this), task_runner, closure, | |
| 58 dump_guid, success)); | |
| 59 return; | |
| 60 } | |
| 61 ++callback_call_count_; | |
| 62 last_callback_dump_guid_ = dump_guid; | |
| 63 last_callback_success_ = success; | |
| 64 closure.Run(); | |
| 65 } | |
| 66 | |
| 67 protected: | |
| 68 void SetUp() override { | |
| 69 callback_call_count_ = 0; | |
| 70 last_callback_dump_guid_ = 0; | |
| 71 last_callback_success_ = false; | |
| 72 MemoryDumpManager::GetInstance()->Initialize(); | |
| 73 mock_dump_provider_.reset(new MockDumpProvider()); | |
| 74 MemoryDumpManager::GetInstance()->RegisterDumpProvider( | |
| 75 mock_dump_provider_.get()); | |
| 76 ContentBrowserTest::SetUp(); | |
| 77 } | |
| 78 | |
| 79 void TearDown() override { | |
| 80 MemoryDumpManager::GetInstance()->UnregisterDumpProvider( | |
| 81 mock_dump_provider_.get()); | |
| 82 mock_dump_provider_.reset(); | |
| 83 ContentBrowserTest::TearDown(); | |
| 84 } | |
| 85 | |
| 86 void EnableMemoryTracing() { | |
| 87 base::RunLoop run_loop; | |
| 88 std::string category_filter = TRACE_DISABLED_BY_DEFAULT("memory-infra"); | |
|
Primiano Tucci (use gerrit)
2015/08/14 14:01:06
Please use MemoryDumpManager::kCategoryForTesting
Ruud van Asseldonk
2015/08/14 15:47:01
Done.
| |
| 89 base::trace_event::TraceConfig trace_config(category_filter, ""); | |
| 90 bool success = TracingController::GetInstance()->EnableRecording( | |
| 91 trace_config, run_loop.QuitClosure()); | |
| 92 EXPECT_TRUE(success); | |
| 93 run_loop.Run(); | |
| 94 } | |
| 95 | |
| 96 void DisableTracing() { | |
| 97 bool success = TracingController::GetInstance()->DisableRecording(NULL); | |
| 98 EXPECT_TRUE(success); | |
| 99 base::RunLoop().RunUntilIdle(); | |
| 100 } | |
| 101 | |
| 102 void RequestGlobalDumpAndWait(bool from_renderer_thread) { | |
| 103 base::RunLoop run_loop; | |
| 104 base::trace_event::MemoryDumpCallback callback = base::Bind( | |
| 105 &MemoryTracingTest::OnGlobalMemoryDumpDone, base::Unretained(this), | |
| 106 base::MessageLoop::current()->task_runner(), run_loop.QuitClosure()); | |
|
Primiano Tucci (use gerrit)
2015/08/14 14:01:06
I think that base::MessageLoop::current()->task_ru
Ruud van Asseldonk
2015/08/14 15:47:01
Done.
| |
| 107 if (from_renderer_thread) { | |
| 108 PostTaskToInProcessRendererAndWait( | |
| 109 base::Bind(&MemoryTracingTest::DoRequestGlobalDump, | |
| 110 base::Unretained(this), callback)); | |
| 111 } else { | |
| 112 DoRequestGlobalDump(callback); | |
| 113 } | |
| 114 run_loop.Run(); | |
| 115 } | |
| 116 | |
| 117 void Navigate(Shell* shell) { | |
| 118 NavigateToURL(shell, GetTestUrl("", "title.html")); | |
| 119 } | |
| 120 | |
| 121 base::Closure on_memory_dump_complete_closure_; | |
| 122 scoped_ptr<MockDumpProvider> mock_dump_provider_; | |
| 123 uint32 callback_call_count_; | |
| 124 uint64 last_callback_dump_guid_; | |
| 125 bool last_callback_success_; | |
| 126 }; | |
| 127 | |
| 128 class SingleProcessMemoryTracingTest : public MemoryTracingTest { | |
| 129 public: | |
| 130 SingleProcessMemoryTracingTest() {} | |
| 131 | |
| 132 void SetUpCommandLine(base::CommandLine* command_line) override { | |
| 133 command_line->AppendSwitch(switches::kSingleProcess); | |
| 134 } | |
| 135 }; | |
| 136 | |
| 137 // Checks that a memory dump initiated from a the main browser thread ends up in | |
| 138 // a single dump even in single process mode. | |
| 139 IN_PROC_BROWSER_TEST_F(SingleProcessMemoryTracingTest, | |
| 140 BrowserInitiatedDumpSingle) { | |
|
Primiano Tucci (use gerrit)
2015/08/14 14:01:06
BrowserInitiatedDumpSingle -> BrowserInitiatedSing
Ruud van Asseldonk
2015/08/14 15:47:01
Done.
| |
| 141 Navigate(shell()); | |
| 142 | |
| 143 EXPECT_CALL(*mock_dump_provider_, OnMemoryDump(_,_)).WillOnce(Return(true)); | |
| 144 | |
| 145 EnableMemoryTracing(); | |
| 146 RequestGlobalDumpAndWait(false /* from_renderer_thread */); | |
| 147 EXPECT_EQ(1u, callback_call_count_); | |
| 148 EXPECT_NE(0u, last_callback_dump_guid_); | |
| 149 EXPECT_TRUE(last_callback_success_); | |
| 150 DisableTracing(); | |
| 151 } | |
| 152 | |
| 153 // Checks that a memory dump initiated from a renderer thread ends up in a | |
| 154 // single dump even in single process mode. | |
| 155 IN_PROC_BROWSER_TEST_F(SingleProcessMemoryTracingTest, | |
| 156 RendererInitiatedDumpSingle) { | |
| 157 Navigate(shell()); | |
| 158 | |
| 159 EXPECT_CALL(*mock_dump_provider_, OnMemoryDump(_,_)).WillOnce(Return(true)); | |
| 160 | |
| 161 EnableMemoryTracing(); | |
| 162 RequestGlobalDumpAndWait(true /* from_renderer_thread */); | |
| 163 EXPECT_EQ(1u, callback_call_count_); | |
| 164 EXPECT_NE(0u, last_callback_dump_guid_); | |
| 165 EXPECT_TRUE(last_callback_success_); | |
| 166 DisableTracing(); | |
| 167 } | |
| 168 | |
| 169 IN_PROC_BROWSER_TEST_F(SingleProcessMemoryTracingTest, ManyInterleavedDumps) { | |
| 170 Navigate(shell()); | |
| 171 | |
| 172 EXPECT_CALL(*mock_dump_provider_, OnMemoryDump(_,_)) | |
| 173 .Times(4) | |
| 174 .WillRepeatedly(Return(true)); | |
| 175 | |
| 176 EnableMemoryTracing(); | |
| 177 | |
| 178 RequestGlobalDumpAndWait(true /* from_renderer_thread */); | |
| 179 EXPECT_NE(0u, last_callback_dump_guid_); | |
| 180 EXPECT_TRUE(last_callback_success_); | |
| 181 | |
| 182 RequestGlobalDumpAndWait(false /* from_renderer_thread */); | |
| 183 EXPECT_NE(0u, last_callback_dump_guid_); | |
| 184 EXPECT_TRUE(last_callback_success_); | |
| 185 | |
| 186 RequestGlobalDumpAndWait(false /* from_renderer_thread */); | |
| 187 EXPECT_NE(0u, last_callback_dump_guid_); | |
| 188 EXPECT_TRUE(last_callback_success_); | |
| 189 | |
| 190 RequestGlobalDumpAndWait(true /* from_renderer_thread */); | |
| 191 EXPECT_EQ(4u, callback_call_count_); | |
| 192 EXPECT_NE(0u, last_callback_dump_guid_); | |
| 193 EXPECT_TRUE(last_callback_success_); | |
| 194 | |
| 195 DisableTracing(); | |
| 196 } | |
| 197 | |
| 198 // Checks that a memory dump initiated from a the main browser thread ends up in | |
| 199 // a successful dump. | |
| 200 IN_PROC_BROWSER_TEST_F(MemoryTracingTest, BrowserInitiatedDump) { | |
| 201 Navigate(shell()); | |
| 202 | |
| 203 EXPECT_CALL(*mock_dump_provider_, OnMemoryDump(_,_)).WillOnce(Return(true)); | |
| 204 | |
| 205 EnableMemoryTracing(); | |
| 206 RequestGlobalDumpAndWait(false /* from_renderer_thread */); | |
| 207 EXPECT_EQ(1u, callback_call_count_); | |
| 208 EXPECT_NE(0u, last_callback_dump_guid_); | |
| 209 EXPECT_TRUE(last_callback_success_); | |
| 210 DisableTracing(); | |
| 211 } | |
| 212 | |
| 213 } // namespace content | |
| OLD | NEW |