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

Side by Side Diff: content/browser/tracing/memory_tracing_browsertest.cc

Issue 2067793004: [memory-infra] Add support for queueing memory dump requests in the browser process (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address final comments Created 4 years, 5 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 | « no previous file | content/browser/tracing/tracing_controller_impl.h » ('j') | 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 <stdint.h> 5 #include <stdint.h>
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/callback_forward.h"
8 #include "base/command_line.h" 9 #include "base/command_line.h"
9 #include "base/run_loop.h" 10 #include "base/run_loop.h"
10 #include "base/threading/thread_task_runner_handle.h" 11 #include "base/threading/thread_task_runner_handle.h"
11 #include "base/trace_event/memory_dump_manager.h" 12 #include "base/trace_event/memory_dump_manager.h"
12 #include "base/trace_event/memory_dump_provider.h" 13 #include "base/trace_event/memory_dump_provider.h"
13 #include "base/trace_event/memory_dump_request_args.h" 14 #include "base/trace_event/memory_dump_request_args.h"
14 #include "base/trace_event/trace_config_memory_test_util.h" 15 #include "base/trace_event/trace_config_memory_test_util.h"
15 #include "content/public/browser/tracing_controller.h" 16 #include "content/public/browser/tracing_controller.h"
16 #include "content/public/common/content_switches.h" 17 #include "content/public/common/content_switches.h"
17 #include "content/public/test/browser_test_utils.h" 18 #include "content/public/test/browser_test_utils.h"
18 #include "content/public/test/content_browser_test.h" 19 #include "content/public/test/content_browser_test.h"
19 #include "content/public/test/content_browser_test_utils.h" 20 #include "content/public/test/content_browser_test_utils.h"
20 #include "content/shell/browser/shell.h" 21 #include "content/shell/browser/shell.h"
21 #include "testing/gmock/include/gmock/gmock.h" 22 #include "testing/gmock/include/gmock/gmock.h"
22 23
23 using base::trace_event::MemoryDumpArgs; 24 using base::trace_event::MemoryDumpArgs;
25 using base::trace_event::MemoryDumpLevelOfDetail;
24 using base::trace_event::MemoryDumpManager; 26 using base::trace_event::MemoryDumpManager;
25 using base::trace_event::MemoryDumpType; 27 using base::trace_event::MemoryDumpType;
26 using base::trace_event::ProcessMemoryDump; 28 using base::trace_event::ProcessMemoryDump;
27 using testing::_; 29 using testing::_;
28 using testing::Return; 30 using testing::Return;
29 31
30 namespace content { 32 namespace content {
31 33
32 // A mock dump provider, used to check that dump requests actually end up 34 // A mock dump provider, used to check that dump requests actually end up
33 // creating memory dumps. 35 // creating memory dumps.
34 class MockDumpProvider : public base::trace_event::MemoryDumpProvider { 36 class MockDumpProvider : public base::trace_event::MemoryDumpProvider {
35 public: 37 public:
36 MOCK_METHOD2(OnMemoryDump, bool(const MemoryDumpArgs& args, 38 MOCK_METHOD2(OnMemoryDump, bool(const MemoryDumpArgs& args,
37 ProcessMemoryDump* pmd)); 39 ProcessMemoryDump* pmd));
38 }; 40 };
39 41
40 class MemoryTracingTest : public ContentBrowserTest { 42 class MemoryTracingTest : public ContentBrowserTest {
41 public: 43 public:
42 void DoRequestGlobalDump(const base::trace_event::MemoryDumpCallback& cb) { 44 void DoRequestGlobalDump(const MemoryDumpType& dump_type,
43 MemoryDumpManager::GetInstance()->RequestGlobalDump( 45 const MemoryDumpLevelOfDetail& level_of_detail,
44 MemoryDumpType::EXPLICITLY_TRIGGERED, 46 const base::trace_event::MemoryDumpCallback& cb) {
45 base::trace_event::MemoryDumpLevelOfDetail::DETAILED, cb); 47 MemoryDumpManager::GetInstance()->RequestGlobalDump(dump_type,
48 level_of_detail, cb);
46 } 49 }
47 50
48 // Used as callback argument for MemoryDumpManager::RequestGlobalDump(): 51 // Used as callback argument for MemoryDumpManager::RequestGlobalDump():
49 void OnGlobalMemoryDumpDone( 52 void OnGlobalMemoryDumpDone(
50 scoped_refptr<base::SingleThreadTaskRunner> task_runner, 53 scoped_refptr<base::SingleThreadTaskRunner> task_runner,
51 base::Closure closure, 54 base::Closure closure,
55 uint32_t request_index,
52 uint64_t dump_guid, 56 uint64_t dump_guid,
53 bool success) { 57 bool success) {
54 // Make sure we run the RunLoop closure on the same thread that originated 58 // Make sure we run the RunLoop closure on the same thread that originated
55 // the run loop (which is the IN_PROC_BROWSER_TEST_F main thread). 59 // the run loop (which is the IN_PROC_BROWSER_TEST_F main thread).
56 if (!task_runner->RunsTasksOnCurrentThread()) { 60 if (!task_runner->RunsTasksOnCurrentThread()) {
57 task_runner->PostTask( 61 task_runner->PostTask(
58 FROM_HERE, base::Bind(&MemoryTracingTest::OnGlobalMemoryDumpDone, 62 FROM_HERE, base::Bind(&MemoryTracingTest::OnGlobalMemoryDumpDone,
59 base::Unretained(this), task_runner, closure, 63 base::Unretained(this), task_runner, closure,
60 dump_guid, success)); 64 request_index, dump_guid, success));
61 return; 65 return;
62 } 66 }
63 ++callback_call_count_; 67 if (success)
64 last_callback_dump_guid_ = dump_guid; 68 EXPECT_NE(0u, dump_guid);
65 last_callback_success_ = success; 69 OnMemoryDumpDone(request_index, success);
66 closure.Run(); 70 if (!closure.is_null())
71 closure.Run();
72 }
73
74 void RequestGlobalDumpWithClosure(
75 bool from_renderer_thread,
76 const MemoryDumpType& dump_type,
77 const MemoryDumpLevelOfDetail& level_of_detail,
78 const base::Closure& closure) {
79 uint32_t request_index = next_request_index_++;
80 base::trace_event::MemoryDumpCallback callback = base::Bind(
81 &MemoryTracingTest::OnGlobalMemoryDumpDone, base::Unretained(this),
82 base::ThreadTaskRunnerHandle::Get(), closure, request_index);
83 if (from_renderer_thread) {
84 PostTaskToInProcessRendererAndWait(base::Bind(
85 &MemoryTracingTest::DoRequestGlobalDump, base::Unretained(this),
86 dump_type, level_of_detail, callback));
87 } else {
88 DoRequestGlobalDump(dump_type, level_of_detail, callback);
89 }
67 } 90 }
68 91
69 protected: 92 protected:
70 void SetUp() override { 93 void SetUp() override {
71 callback_call_count_ = 0; 94 next_request_index_ = 0;
72 last_callback_dump_guid_ = 0;
73 last_callback_success_ = false;
74 95
75 mock_dump_provider_.reset(new MockDumpProvider()); 96 mock_dump_provider_.reset(new MockDumpProvider());
76 MemoryDumpManager::GetInstance()->RegisterDumpProvider( 97 MemoryDumpManager::GetInstance()->RegisterDumpProvider(
77 mock_dump_provider_.get(), "MockDumpProvider", nullptr); 98 mock_dump_provider_.get(), "MockDumpProvider", nullptr);
78 MemoryDumpManager::GetInstance() 99 MemoryDumpManager::GetInstance()
79 ->set_dumper_registrations_ignored_for_testing(false); 100 ->set_dumper_registrations_ignored_for_testing(false);
80 ContentBrowserTest::SetUp(); 101 ContentBrowserTest::SetUp();
81 } 102 }
82 103
83 void TearDown() override { 104 void TearDown() override {
(...skipping 15 matching lines...) Expand all
99 EXPECT_TRUE(success); 120 EXPECT_TRUE(success);
100 run_loop.Run(); 121 run_loop.Run();
101 } 122 }
102 123
103 void DisableTracing() { 124 void DisableTracing() {
104 bool success = TracingController::GetInstance()->StopTracing(NULL); 125 bool success = TracingController::GetInstance()->StopTracing(NULL);
105 EXPECT_TRUE(success); 126 EXPECT_TRUE(success);
106 base::RunLoop().RunUntilIdle(); 127 base::RunLoop().RunUntilIdle();
107 } 128 }
108 129
109 void RequestGlobalDumpAndWait(bool from_renderer_thread) { 130 void RequestGlobalDumpAndWait(
131 bool from_renderer_thread,
132 const MemoryDumpType& dump_type,
133 const MemoryDumpLevelOfDetail& level_of_detail) {
110 base::RunLoop run_loop; 134 base::RunLoop run_loop;
111 base::trace_event::MemoryDumpCallback callback = base::Bind( 135 RequestGlobalDumpWithClosure(from_renderer_thread, dump_type,
112 &MemoryTracingTest::OnGlobalMemoryDumpDone, base::Unretained(this), 136 level_of_detail, run_loop.QuitClosure());
113 base::ThreadTaskRunnerHandle::Get(), run_loop.QuitClosure());
114 if (from_renderer_thread) {
115 PostTaskToInProcessRendererAndWait(
116 base::Bind(&MemoryTracingTest::DoRequestGlobalDump,
117 base::Unretained(this), callback));
118 } else {
119 DoRequestGlobalDump(callback);
120 }
121 run_loop.Run(); 137 run_loop.Run();
122 } 138 }
123 139
140 void RequestGlobalDump(bool from_renderer_thread,
141 const MemoryDumpType& dump_type,
142 const MemoryDumpLevelOfDetail& level_of_detail) {
143 RequestGlobalDumpWithClosure(from_renderer_thread, dump_type,
144 level_of_detail, base::Closure());
145 }
146
124 void Navigate(Shell* shell) { 147 void Navigate(Shell* shell) {
125 NavigateToURL(shell, GetTestUrl("", "title.html")); 148 NavigateToURL(shell, GetTestUrl("", "title.html"));
126 } 149 }
127 150
151 MOCK_METHOD2(OnMemoryDumpDone, void(uint32_t request_index, bool successful));
152
128 base::Closure on_memory_dump_complete_closure_; 153 base::Closure on_memory_dump_complete_closure_;
129 std::unique_ptr<MockDumpProvider> mock_dump_provider_; 154 std::unique_ptr<MockDumpProvider> mock_dump_provider_;
130 uint32_t callback_call_count_; 155 uint32_t next_request_index_;
131 uint64_t last_callback_dump_guid_;
132 bool last_callback_success_; 156 bool last_callback_success_;
133 }; 157 };
134 158
135 // Ignore SingleProcessMemoryTracingTests for Google Chrome builds because 159 // Ignore SingleProcessMemoryTracingTests for Google Chrome builds because
136 // single-process is not supported on those builds. 160 // single-process is not supported on those builds.
137 #if !defined(GOOGLE_CHROME_BUILD) 161 #if !defined(GOOGLE_CHROME_BUILD)
138 162
139 class SingleProcessMemoryTracingTest : public MemoryTracingTest { 163 class SingleProcessMemoryTracingTest : public MemoryTracingTest {
140 public: 164 public:
141 SingleProcessMemoryTracingTest() {} 165 SingleProcessMemoryTracingTest() {}
142 166
143 void SetUpCommandLine(base::CommandLine* command_line) override { 167 void SetUpCommandLine(base::CommandLine* command_line) override {
144 command_line->AppendSwitch(switches::kSingleProcess); 168 command_line->AppendSwitch(switches::kSingleProcess);
145 } 169 }
146 }; 170 };
147 171
148 // Checks that a memory dump initiated from a the main browser thread ends up in 172 // Checks that a memory dump initiated from a the main browser thread ends up in
149 // a single dump even in single process mode. 173 // a single dump even in single process mode.
150 IN_PROC_BROWSER_TEST_F(SingleProcessMemoryTracingTest, 174 IN_PROC_BROWSER_TEST_F(SingleProcessMemoryTracingTest,
151 BrowserInitiatedSingleDump) { 175 BrowserInitiatedSingleDump) {
152 Navigate(shell()); 176 Navigate(shell());
153 177
154 EXPECT_CALL(*mock_dump_provider_, OnMemoryDump(_,_)).WillOnce(Return(true)); 178 EXPECT_CALL(*mock_dump_provider_, OnMemoryDump(_,_)).WillOnce(Return(true));
179 EXPECT_CALL(*this, OnMemoryDumpDone(_, true /* success */));
155 180
156 EnableMemoryTracing(); 181 EnableMemoryTracing();
157 RequestGlobalDumpAndWait(false /* from_renderer_thread */); 182 RequestGlobalDumpAndWait(false /* from_renderer_thread */,
158 EXPECT_EQ(1u, callback_call_count_); 183 MemoryDumpType::EXPLICITLY_TRIGGERED,
159 EXPECT_NE(0u, last_callback_dump_guid_); 184 MemoryDumpLevelOfDetail::DETAILED);
160 EXPECT_TRUE(last_callback_success_);
161 DisableTracing(); 185 DisableTracing();
162 } 186 }
163 187
164 // Checks that a memory dump initiated from a renderer thread ends up in a 188 // Checks that a memory dump initiated from a renderer thread ends up in a
165 // single dump even in single process mode. 189 // single dump even in single process mode.
166 IN_PROC_BROWSER_TEST_F(SingleProcessMemoryTracingTest, 190 IN_PROC_BROWSER_TEST_F(SingleProcessMemoryTracingTest,
167 RendererInitiatedSingleDump) { 191 RendererInitiatedSingleDump) {
168 Navigate(shell()); 192 Navigate(shell());
169 193
170 EXPECT_CALL(*mock_dump_provider_, OnMemoryDump(_,_)).WillOnce(Return(true)); 194 EXPECT_CALL(*mock_dump_provider_, OnMemoryDump(_,_)).WillOnce(Return(true));
195 EXPECT_CALL(*this, OnMemoryDumpDone(_, true /* success */));
171 196
172 EnableMemoryTracing(); 197 EnableMemoryTracing();
173 RequestGlobalDumpAndWait(true /* from_renderer_thread */); 198 RequestGlobalDumpAndWait(true /* from_renderer_thread */,
174 EXPECT_EQ(1u, callback_call_count_); 199 MemoryDumpType::EXPLICITLY_TRIGGERED,
175 EXPECT_NE(0u, last_callback_dump_guid_); 200 MemoryDumpLevelOfDetail::DETAILED);
176 EXPECT_TRUE(last_callback_success_);
177 DisableTracing(); 201 DisableTracing();
178 } 202 }
179 203
180 IN_PROC_BROWSER_TEST_F(SingleProcessMemoryTracingTest, ManyInterleavedDumps) { 204 IN_PROC_BROWSER_TEST_F(SingleProcessMemoryTracingTest, ManyInterleavedDumps) {
181 Navigate(shell()); 205 Navigate(shell());
182 206
183 EXPECT_CALL(*mock_dump_provider_, OnMemoryDump(_,_)) 207 EXPECT_CALL(*mock_dump_provider_, OnMemoryDump(_,_))
184 .Times(4) 208 .Times(4)
185 .WillRepeatedly(Return(true)); 209 .WillRepeatedly(Return(true));
210 EXPECT_CALL(*this, OnMemoryDumpDone(_, true /* success */)).Times(4);
211
212 EnableMemoryTracing();
213 RequestGlobalDumpAndWait(true /* from_renderer_thread */,
214 MemoryDumpType::EXPLICITLY_TRIGGERED,
215 MemoryDumpLevelOfDetail::DETAILED);
216 RequestGlobalDumpAndWait(false /* from_renderer_thread */,
217 MemoryDumpType::EXPLICITLY_TRIGGERED,
218 MemoryDumpLevelOfDetail::DETAILED);
219 RequestGlobalDumpAndWait(false /* from_renderer_thread */,
220 MemoryDumpType::EXPLICITLY_TRIGGERED,
221 MemoryDumpLevelOfDetail::DETAILED);
222 RequestGlobalDumpAndWait(true /* from_renderer_thread */,
223 MemoryDumpType::EXPLICITLY_TRIGGERED,
224 MemoryDumpLevelOfDetail::DETAILED);
225 DisableTracing();
226 }
227
228 // Checks that, if there already is a memory dump in progress, subsequent memory
229 // dump requests are queued and carried out after it's finished. Also checks
230 // that periodic dump requests fail in case there is already a request in the
231 // queue with the same level of detail.
232 IN_PROC_BROWSER_TEST_F(SingleProcessMemoryTracingTest, QueuedDumps) {
233 Navigate(shell());
186 234
187 EnableMemoryTracing(); 235 EnableMemoryTracing();
188 236
189 RequestGlobalDumpAndWait(true /* from_renderer_thread */); 237 // Issue the following 6 global memory dump requests:
190 EXPECT_NE(0u, last_callback_dump_guid_); 238 //
191 EXPECT_TRUE(last_callback_success_); 239 // 0 (ED) req-------------------------------------->ok
240 // 1 (PD) req->fail(0)
241 // 2 (PL) req------------------------>ok
242 // 3 (PL) req->fail(2)
243 // 4 (EL) req---------->ok
244 // 5 (ED) req--------->ok
245 // 6 (PL) req->ok
246 //
247 // where P=PERIODIC_INTERVAL, E=EXPLICITLY_TRIGGERED, D=DETAILED and L=LIGHT.
192 248
193 RequestGlobalDumpAndWait(false /* from_renderer_thread */); 249 EXPECT_CALL(*mock_dump_provider_, OnMemoryDump(_, _))
194 EXPECT_NE(0u, last_callback_dump_guid_); 250 .Times(5)
195 EXPECT_TRUE(last_callback_success_); 251 .WillRepeatedly(Return(true));
196 252
197 RequestGlobalDumpAndWait(false /* from_renderer_thread */); 253 EXPECT_CALL(*this, OnMemoryDumpDone(0, true /* success */));
198 EXPECT_NE(0u, last_callback_dump_guid_); 254 RequestGlobalDump(true /* from_renderer_thread */,
199 EXPECT_TRUE(last_callback_success_); 255 MemoryDumpType::EXPLICITLY_TRIGGERED,
256 MemoryDumpLevelOfDetail::DETAILED);
200 257
201 RequestGlobalDumpAndWait(true /* from_renderer_thread */); 258 // This dump should fail immediately because there's already a detailed dump
202 EXPECT_EQ(4u, callback_call_count_); 259 // request in the queue.
203 EXPECT_NE(0u, last_callback_dump_guid_); 260 EXPECT_CALL(*this, OnMemoryDumpDone(1, false /* success */));
204 EXPECT_TRUE(last_callback_success_); 261 RequestGlobalDump(true /* from_renderer_thread */,
262 MemoryDumpType::PERIODIC_INTERVAL,
263 MemoryDumpLevelOfDetail::DETAILED);
264
265 EXPECT_CALL(*this, OnMemoryDumpDone(2, true /* success */));
266 RequestGlobalDump(true /* from_renderer_thread */,
267 MemoryDumpType::PERIODIC_INTERVAL,
268 MemoryDumpLevelOfDetail::LIGHT);
269
270 // This dump should fail immediately because there's already a light dump
271 // request in the queue.
272 EXPECT_CALL(*this, OnMemoryDumpDone(3, false /* success */));
273 RequestGlobalDump(true /* from_renderer_thread */,
274 MemoryDumpType::PERIODIC_INTERVAL,
275 MemoryDumpLevelOfDetail::LIGHT);
276
277 EXPECT_CALL(*this, OnMemoryDumpDone(4, true /* success */));
278 RequestGlobalDump(true /* from_renderer_thread */,
279 MemoryDumpType::EXPLICITLY_TRIGGERED,
280 MemoryDumpLevelOfDetail::LIGHT);
281
282 EXPECT_CALL(*this, OnMemoryDumpDone(5, true /* success */));
283 RequestGlobalDumpAndWait(true /* from_renderer_thread */,
284 MemoryDumpType::EXPLICITLY_TRIGGERED,
285 MemoryDumpLevelOfDetail::DETAILED);
286
287 EXPECT_CALL(*this, OnMemoryDumpDone(6, true /* success */));
288 RequestGlobalDumpAndWait(true /* from_renderer_thread */,
289 MemoryDumpType::PERIODIC_INTERVAL,
290 MemoryDumpLevelOfDetail::LIGHT);
205 291
206 DisableTracing(); 292 DisableTracing();
207 } 293 }
208 294
209 #endif // !defined(GOOGLE_CHROME_BUILD) 295 #endif // !defined(GOOGLE_CHROME_BUILD)
210 296
211 // Non-deterministic races under TSan. crbug.com/529678 297 // Non-deterministic races under TSan. crbug.com/529678
212 #if defined(THREAD_SANITIZER) 298 #if defined(THREAD_SANITIZER)
213 #define MAYBE_BrowserInitiatedDump DISABLED_BrowserInitiatedDump 299 #define MAYBE_BrowserInitiatedDump DISABLED_BrowserInitiatedDump
214 #else 300 #else
215 #define MAYBE_BrowserInitiatedDump BrowserInitiatedDump 301 #define MAYBE_BrowserInitiatedDump BrowserInitiatedDump
216 #endif 302 #endif
217 // Checks that a memory dump initiated from a the main browser thread ends up in 303 // Checks that a memory dump initiated from a the main browser thread ends up in
218 // a successful dump. 304 // a successful dump.
219 IN_PROC_BROWSER_TEST_F(MemoryTracingTest, MAYBE_BrowserInitiatedDump) { 305 IN_PROC_BROWSER_TEST_F(MemoryTracingTest, MAYBE_BrowserInitiatedDump) {
220 Navigate(shell()); 306 Navigate(shell());
221 307
222 EXPECT_CALL(*mock_dump_provider_, OnMemoryDump(_,_)).WillOnce(Return(true)); 308 EXPECT_CALL(*mock_dump_provider_, OnMemoryDump(_,_)).WillOnce(Return(true));
309 EXPECT_CALL(*this, OnMemoryDumpDone(_, true /* success */));
223 310
224 EnableMemoryTracing(); 311 EnableMemoryTracing();
225 RequestGlobalDumpAndWait(false /* from_renderer_thread */); 312 RequestGlobalDumpAndWait(false /* from_renderer_thread */,
226 EXPECT_EQ(1u, callback_call_count_); 313 MemoryDumpType::EXPLICITLY_TRIGGERED,
227 EXPECT_NE(0u, last_callback_dump_guid_); 314 MemoryDumpLevelOfDetail::DETAILED);
228 EXPECT_TRUE(last_callback_success_);
229 DisableTracing(); 315 DisableTracing();
230 } 316 }
231 317
232 } // namespace content 318 } // namespace content
OLDNEW
« no previous file with comments | « no previous file | content/browser/tracing/tracing_controller_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698