| 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 <stdint.h> |
| 6 |
| 5 #include "base/callback.h" | 7 #include "base/callback.h" |
| 6 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
| 7 #include "base/thread_task_runner_handle.h" | 9 #include "base/thread_task_runner_handle.h" |
| 8 #include "base/trace_event/memory_dump_manager.h" | 10 #include "base/trace_event/memory_dump_manager.h" |
| 9 #include "base/trace_event/memory_dump_provider.h" | 11 #include "base/trace_event/memory_dump_provider.h" |
| 10 #include "base/trace_event/trace_event.h" | 12 #include "base/trace_event/trace_event.h" |
| 11 #include "components/tracing/child_memory_dump_manager_delegate_impl.h" | 13 #include "components/tracing/child_memory_dump_manager_delegate_impl.h" |
| 12 #include "components/tracing/child_trace_message_filter.h" | 14 #include "components/tracing/child_trace_message_filter.h" |
| 13 #include "components/tracing/tracing_messages.h" | 15 #include "components/tracing/tracing_messages.h" |
| 14 #include "content/public/test/render_view_test.h" | 16 #include "content/public/test/render_view_test.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 29 class MockDumpProvider : public base::trace_event::MemoryDumpProvider { | 31 class MockDumpProvider : public base::trace_event::MemoryDumpProvider { |
| 30 public: | 32 public: |
| 31 MOCK_METHOD2(OnMemoryDump, | 33 MOCK_METHOD2(OnMemoryDump, |
| 32 bool(const MemoryDumpArgs& args, | 34 bool(const MemoryDumpArgs& args, |
| 33 base::trace_event::ProcessMemoryDump* pmd)); | 35 base::trace_event::ProcessMemoryDump* pmd)); |
| 34 }; | 36 }; |
| 35 | 37 |
| 36 class ChildTracingTest : public content::RenderViewTest, public IPC::Listener { | 38 class ChildTracingTest : public content::RenderViewTest, public IPC::Listener { |
| 37 public: | 39 public: |
| 38 // Used as callback argument for MemoryDumpManager::RequestGlobalDump(): | 40 // Used as callback argument for MemoryDumpManager::RequestGlobalDump(): |
| 39 void OnMemoryDumpCallback(uint64 dump_guid, bool status) { | 41 void OnMemoryDumpCallback(uint64_t dump_guid, bool status) { |
| 40 last_callback_dump_guid_ = dump_guid; | 42 last_callback_dump_guid_ = dump_guid; |
| 41 last_callback_status_ = status; | 43 last_callback_status_ = status; |
| 42 ++callback_call_count_; | 44 ++callback_call_count_; |
| 43 } | 45 } |
| 44 | 46 |
| 45 protected: | 47 protected: |
| 46 void SetUp() override { | 48 void SetUp() override { |
| 47 // RenderViewTest::SetUp causes additional registrations, so we first | 49 // RenderViewTest::SetUp causes additional registrations, so we first |
| 48 // register the mock dump provider and ignore registrations from then on. | 50 // register the mock dump provider and ignore registrations from then on. |
| 49 // In addition to the mock dump provider, the TraceLog has already | 51 // In addition to the mock dump provider, the TraceLog has already |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 TracingMsg_BeginTracing msg(trace_config.ToString(), base::TimeTicks(), 0); | 119 TracingMsg_BeginTracing msg(trace_config.ToString(), base::TimeTicks(), 0); |
| 118 SimulateSyntheticMessageFromBrowser(msg); | 120 SimulateSyntheticMessageFromBrowser(msg); |
| 119 } | 121 } |
| 120 | 122 |
| 121 void DisableTracing() { | 123 void DisableTracing() { |
| 122 SimulateSyntheticMessageFromBrowser(TracingMsg_EndTracing()); | 124 SimulateSyntheticMessageFromBrowser(TracingMsg_EndTracing()); |
| 123 } | 125 } |
| 124 | 126 |
| 125 // Simulates a synthetic browser -> child process memory dump request and | 127 // Simulates a synthetic browser -> child process memory dump request and |
| 126 // checks that the child actually sends a response to that. | 128 // checks that the child actually sends a response to that. |
| 127 void RequestProcessMemoryDumpAndCheckResponse(uint64 dump_guid) { | 129 void RequestProcessMemoryDumpAndCheckResponse(uint64_t dump_guid) { |
| 128 SimulateSyntheticMessageFromBrowser(TracingMsg_ProcessMemoryDumpRequest( | 130 SimulateSyntheticMessageFromBrowser(TracingMsg_ProcessMemoryDumpRequest( |
| 129 {dump_guid, MemoryDumpType::EXPLICITLY_TRIGGERED})); | 131 {dump_guid, MemoryDumpType::EXPLICITLY_TRIGGERED})); |
| 130 | 132 |
| 131 // Check that a child -> browser response to the local dump request is sent. | 133 // Check that a child -> browser response to the local dump request is sent. |
| 132 const IPC::Message* msg = | 134 const IPC::Message* msg = |
| 133 WaitForIPCMessage(TracingHostMsg_ProcessMemoryDumpResponse::ID); | 135 WaitForIPCMessage(TracingHostMsg_ProcessMemoryDumpResponse::ID); |
| 134 EXPECT_NE(nullptr, msg); | 136 EXPECT_NE(nullptr, msg); |
| 135 | 137 |
| 136 // Check that the |dump_guid| and the |success| fields are properly set. | 138 // Check that the |dump_guid| and the |success| fields are properly set. |
| 137 TracingHostMsg_ProcessMemoryDumpResponse::Param params; | 139 TracingHostMsg_ProcessMemoryDumpResponse::Param params; |
| 138 TracingHostMsg_ProcessMemoryDumpResponse::Read(msg, ¶ms); | 140 TracingHostMsg_ProcessMemoryDumpResponse::Read(msg, ¶ms); |
| 139 const uint64 resp_guid = base::get<0>(params); | 141 const uint64_t resp_guid = base::get<0>(params); |
| 140 const bool resp_success = base::get<1>(params); | 142 const bool resp_success = base::get<1>(params); |
| 141 EXPECT_EQ(dump_guid, resp_guid); | 143 EXPECT_EQ(dump_guid, resp_guid); |
| 142 EXPECT_TRUE(resp_success); | 144 EXPECT_TRUE(resp_success); |
| 143 } | 145 } |
| 144 | 146 |
| 145 // Retrieves the MemoryDumpRequestArgs of the global memory dump request that | 147 // Retrieves the MemoryDumpRequestArgs of the global memory dump request that |
| 146 // this child process tried to send to the browser. Fails if either none or | 148 // this child process tried to send to the browser. Fails if either none or |
| 147 // multiple requests were sent. | 149 // multiple requests were sent. |
| 148 MemoryDumpRequestArgs GetInterceptedGlobalMemoryDumpRequest() { | 150 MemoryDumpRequestArgs GetInterceptedGlobalMemoryDumpRequest() { |
| 149 const IPC::Message* msg = render_thread_->sink().GetUniqueMessageMatching( | 151 const IPC::Message* msg = render_thread_->sink().GetUniqueMessageMatching( |
| 150 TracingHostMsg_GlobalMemoryDumpRequest::ID); | 152 TracingHostMsg_GlobalMemoryDumpRequest::ID); |
| 151 EXPECT_NE(nullptr, msg); | 153 EXPECT_NE(nullptr, msg); |
| 152 TracingHostMsg_GlobalMemoryDumpRequest::Param params; | 154 TracingHostMsg_GlobalMemoryDumpRequest::Param params; |
| 153 TracingHostMsg_GlobalMemoryDumpRequest::Read(msg, ¶ms); | 155 TracingHostMsg_GlobalMemoryDumpRequest::Read(msg, ¶ms); |
| 154 MemoryDumpRequestArgs args = base::get<0>(params); | 156 MemoryDumpRequestArgs args = base::get<0>(params); |
| 155 EXPECT_NE(0U, args.dump_guid); | 157 EXPECT_NE(0U, args.dump_guid); |
| 156 return args; | 158 return args; |
| 157 } | 159 } |
| 158 | 160 |
| 159 scoped_refptr<ChildTraceMessageFilter> ctmf_; | 161 scoped_refptr<ChildTraceMessageFilter> ctmf_; |
| 160 scoped_ptr<MockDumpProvider> mock_dump_provider_; | 162 scoped_ptr<MockDumpProvider> mock_dump_provider_; |
| 161 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 163 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 162 base::trace_event::MemoryDumpCallback callback_; | 164 base::trace_event::MemoryDumpCallback callback_; |
| 163 uint32_t wait_for_ipc_message_type_; | 165 uint32_t wait_for_ipc_message_type_; |
| 164 base::Closure wait_for_ipc_closure_; | 166 base::Closure wait_for_ipc_closure_; |
| 165 uint32 callback_call_count_; | 167 uint32_t callback_call_count_; |
| 166 uint64 last_callback_dump_guid_; | 168 uint64_t last_callback_dump_guid_; |
| 167 bool last_callback_status_; | 169 bool last_callback_status_; |
| 168 }; | 170 }; |
| 169 | 171 |
| 170 // Covers the case of some browser-initiated memory dumps. | 172 // Covers the case of some browser-initiated memory dumps. |
| 171 TEST_F(ChildTracingTest, BrowserInitiatedMemoryDumps) { | 173 TEST_F(ChildTracingTest, BrowserInitiatedMemoryDumps) { |
| 172 const uint32 kNumDumps = 3; | 174 const uint32_t kNumDumps = 3; |
| 173 | 175 |
| 174 EnableTracingWithMemoryDumps(); | 176 EnableTracingWithMemoryDumps(); |
| 175 EXPECT_CALL(*mock_dump_provider_, OnMemoryDump(_, _)) | 177 EXPECT_CALL(*mock_dump_provider_, OnMemoryDump(_, _)) |
| 176 .Times(kNumDumps) | 178 .Times(kNumDumps) |
| 177 .WillRepeatedly(Return(true)); | 179 .WillRepeatedly(Return(true)); |
| 178 | 180 |
| 179 for (uint32 i = 0; i < kNumDumps; ++i) { | 181 for (uint32_t i = 0; i < kNumDumps; ++i) { |
| 180 render_thread_->sink().ClearMessages(); | 182 render_thread_->sink().ClearMessages(); |
| 181 RequestProcessMemoryDumpAndCheckResponse(i + 1); | 183 RequestProcessMemoryDumpAndCheckResponse(i + 1); |
| 182 } | 184 } |
| 183 | 185 |
| 184 DisableTracing(); | 186 DisableTracing(); |
| 185 } | 187 } |
| 186 | 188 |
| 187 // Covers the case of one simple child-initiated memory dump without callback, | 189 // Covers the case of one simple child-initiated memory dump without callback, |
| 188 // simulating a global memory dump request to the browser (+ response). | 190 // simulating a global memory dump request to the browser (+ response). |
| 189 TEST_F(ChildTracingTest, SingleChildInitiatedMemoryDump) { | 191 TEST_F(ChildTracingTest, SingleChildInitiatedMemoryDump) { |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 267 EXPECT_EQ(2u, callback_call_count_); | 269 EXPECT_EQ(2u, callback_call_count_); |
| 268 EXPECT_EQ(args.dump_guid, last_callback_dump_guid_); | 270 EXPECT_EQ(args.dump_guid, last_callback_dump_guid_); |
| 269 EXPECT_TRUE(last_callback_status_); | 271 EXPECT_TRUE(last_callback_status_); |
| 270 | 272 |
| 271 DisableTracing(); | 273 DisableTracing(); |
| 272 } | 274 } |
| 273 | 275 |
| 274 // Covers the case of five child-initiated global memory dumps. Each global dump | 276 // Covers the case of five child-initiated global memory dumps. Each global dump |
| 275 // request has a callback, which is expected to fail for 3 out of 5 cases. | 277 // request has a callback, which is expected to fail for 3 out of 5 cases. |
| 276 TEST_F(ChildTracingTest, MultipleChildInitiatedMemoryDumpWithFailures) { | 278 TEST_F(ChildTracingTest, MultipleChildInitiatedMemoryDumpWithFailures) { |
| 277 const uint32 kNumRequests = 5; | 279 const uint32_t kNumRequests = 5; |
| 278 MemoryDumpType kDumpType = MemoryDumpType::EXPLICITLY_TRIGGERED; | 280 MemoryDumpType kDumpType = MemoryDumpType::EXPLICITLY_TRIGGERED; |
| 279 | 281 |
| 280 EnableTracingWithMemoryDumps(); | 282 EnableTracingWithMemoryDumps(); |
| 281 EXPECT_CALL(*mock_dump_provider_, OnMemoryDump(_, _)) | 283 EXPECT_CALL(*mock_dump_provider_, OnMemoryDump(_, _)) |
| 282 .Times(kNumRequests) | 284 .Times(kNumRequests) |
| 283 .WillRepeatedly(Return(true)); | 285 .WillRepeatedly(Return(true)); |
| 284 | 286 |
| 285 for (uint32 i = 0; i < kNumRequests; ++i) { | 287 for (uint32_t i = 0; i < kNumRequests; ++i) { |
| 286 render_thread_->sink().ClearMessages(); | 288 render_thread_->sink().ClearMessages(); |
| 287 MemoryDumpManager::GetInstance()->RequestGlobalDump( | 289 MemoryDumpManager::GetInstance()->RequestGlobalDump( |
| 288 kDumpType, MemoryDumpLevelOfDetail::DETAILED, callback_); | 290 kDumpType, MemoryDumpLevelOfDetail::DETAILED, callback_); |
| 289 base::RunLoop().RunUntilIdle(); | 291 base::RunLoop().RunUntilIdle(); |
| 290 | 292 |
| 291 MemoryDumpRequestArgs args = GetInterceptedGlobalMemoryDumpRequest(); | 293 MemoryDumpRequestArgs args = GetInterceptedGlobalMemoryDumpRequest(); |
| 292 EXPECT_EQ(kDumpType, args.dump_type); | 294 EXPECT_EQ(kDumpType, args.dump_type); |
| 293 RequestProcessMemoryDumpAndCheckResponse(args.dump_guid); | 295 RequestProcessMemoryDumpAndCheckResponse(args.dump_guid); |
| 294 | 296 |
| 295 const bool success = (i & 1) ? true : false; | 297 const bool success = (i & 1) ? true : false; |
| 296 SimulateSyntheticMessageFromBrowser( | 298 SimulateSyntheticMessageFromBrowser( |
| 297 TracingMsg_GlobalMemoryDumpResponse(args.dump_guid, success)); | 299 TracingMsg_GlobalMemoryDumpResponse(args.dump_guid, success)); |
| 298 EXPECT_EQ(i + 1, callback_call_count_); | 300 EXPECT_EQ(i + 1, callback_call_count_); |
| 299 EXPECT_EQ(args.dump_guid, last_callback_dump_guid_); | 301 EXPECT_EQ(args.dump_guid, last_callback_dump_guid_); |
| 300 EXPECT_EQ(success, last_callback_status_); | 302 EXPECT_EQ(success, last_callback_status_); |
| 301 } | 303 } |
| 302 | 304 |
| 303 DisableTracing(); | 305 DisableTracing(); |
| 304 } | 306 } |
| 305 | 307 |
| 306 } // namespace tracing | 308 } // namespace tracing |
| OLD | NEW |