| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 "services/resource_coordinator/public/cpp/memory/memory_dump_manager_de
legate_impl.h" | 5 #include "services/resource_coordinator/public/cpp/memory/process_local_dump_man
ager_impl.h" |
| 6 #include "base/bind.h" | 6 #include "base/bind.h" |
| 7 #include "base/bind_helpers.h" | 7 #include "base/bind_helpers.h" |
| 8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "base/trace_event/memory_dump_manager.h" | 10 #include "base/trace_event/memory_dump_manager.h" |
| 11 #include "base/trace_event/memory_dump_request_args.h" | 11 #include "base/trace_event/memory_dump_request_args.h" |
| 12 #include "base/trace_event/trace_config.h" | 12 #include "base/trace_event/trace_config.h" |
| 13 #include "base/trace_event/trace_log.h" | 13 #include "base/trace_event/trace_log.h" |
| 14 #include "mojo/public/cpp/bindings/binding_set.h" | 14 #include "mojo/public/cpp/bindings/binding_set.h" |
| 15 #include "services/resource_coordinator/public/cpp/memory/coordinator.h" | 15 #include "services/resource_coordinator/public/cpp/memory/coordinator.h" |
| (...skipping 18 matching lines...) Expand all Loading... |
| 34 void RequestGlobalMemoryDump( | 34 void RequestGlobalMemoryDump( |
| 35 const base::trace_event::MemoryDumpRequestArgs& args, | 35 const base::trace_event::MemoryDumpRequestArgs& args, |
| 36 const RequestGlobalMemoryDumpCallback& callback) override { | 36 const RequestGlobalMemoryDumpCallback& callback) override { |
| 37 callback.Run(args.dump_guid, true); | 37 callback.Run(args.dump_guid, true); |
| 38 } | 38 } |
| 39 | 39 |
| 40 private: | 40 private: |
| 41 mojo::BindingSet<mojom::Coordinator> bindings_; | 41 mojo::BindingSet<mojom::Coordinator> bindings_; |
| 42 }; | 42 }; |
| 43 | 43 |
| 44 class MemoryDumpManagerDelegateImplTest : public testing::Test { | 44 class ProcessLocalDumpManagerImplTest : public testing::Test { |
| 45 public: | 45 public: |
| 46 void SetUp() override { | 46 void SetUp() override { |
| 47 message_loop_.reset(new base::MessageLoop()); | 47 message_loop_.reset(new base::MessageLoop()); |
| 48 coordinator_.reset(new MockCoordinator()); | 48 coordinator_.reset(new MockCoordinator()); |
| 49 mdm_.reset(new MemoryDumpManager()); | 49 mdm_.reset(new MemoryDumpManager()); |
| 50 MemoryDumpManager::SetInstanceForTesting(mdm_.get()); | 50 MemoryDumpManager::SetInstanceForTesting(mdm_.get()); |
| 51 MemoryDumpManagerDelegateImpl::Config config(coordinator_.get()); | 51 ProcessLocalDumpManagerImpl::Config config(coordinator_.get()); |
| 52 auto delegate = base::MakeUnique<MemoryDumpManagerDelegateImpl>(config); | 52 local_manager_impl_.reset(new ProcessLocalDumpManagerImpl(config)); |
| 53 delegate->SetAsNonCoordinatorForTesting(); | 53 local_manager_impl_->SetAsNonCoordinatorForTesting(); |
| 54 mdm_->Initialize(std::move(delegate)); | |
| 55 | 54 |
| 56 // Enable tracing. | 55 // Enable tracing. |
| 57 std::string category_filter = "-*,"; | 56 std::string category_filter = "-*,"; |
| 58 category_filter += MemoryDumpManager::kTraceCategory; | 57 category_filter += MemoryDumpManager::kTraceCategory; |
| 59 base::trace_event::TraceConfig trace_config(category_filter, ""); | 58 base::trace_event::TraceConfig trace_config(category_filter, ""); |
| 60 base::trace_event::TraceLog::GetInstance()->SetEnabled( | 59 base::trace_event::TraceLog::GetInstance()->SetEnabled( |
| 61 trace_config, base::trace_event::TraceLog::RECORDING_MODE); | 60 trace_config, base::trace_event::TraceLog::RECORDING_MODE); |
| 62 | 61 |
| 63 // Reset the counters. | 62 // Reset the counters. |
| 64 expected_callback_calls_ = 0; | 63 expected_callback_calls_ = 0; |
| 65 dump_requests_received_by_coordinator_ = 0; | 64 dump_requests_received_by_coordinator_ = 0; |
| 66 quit_closure_.Reset(); | 65 quit_closure_.Reset(); |
| 67 } | 66 } |
| 68 | 67 |
| 69 void TearDown() override { | 68 void TearDown() override { |
| 70 base::trace_event::TraceLog::GetInstance()->SetDisabled(); | 69 base::trace_event::TraceLog::GetInstance()->SetDisabled(); |
| 71 MemoryDumpManager::SetInstanceForTesting(nullptr); | 70 MemoryDumpManager::SetInstanceForTesting(nullptr); |
| 72 mdm_.reset(); | 71 mdm_.reset(); |
| 72 local_manager_impl_.reset(); |
| 73 coordinator_.reset(); | 73 coordinator_.reset(); |
| 74 message_loop_.reset(); | 74 message_loop_.reset(); |
| 75 } | 75 } |
| 76 | 76 |
| 77 void OnGlobalMemoryDumpDone(int more_requests, | 77 void OnGlobalMemoryDumpDone(int more_requests, |
| 78 uint64_t dump_guid, | 78 uint64_t dump_guid, |
| 79 bool success) { | 79 bool success) { |
| 80 EXPECT_GT(expected_callback_calls_, 0); | 80 EXPECT_GT(expected_callback_calls_, 0); |
| 81 EXPECT_FALSE(quit_closure_.is_null()); | 81 EXPECT_FALSE(quit_closure_.is_null()); |
| 82 | 82 |
| 83 dump_requests_received_by_coordinator_ += success ? 1 : 0; | 83 dump_requests_received_by_coordinator_ += success ? 1 : 0; |
| 84 expected_callback_calls_--; | 84 expected_callback_calls_--; |
| 85 if (expected_callback_calls_ == 0) | 85 if (expected_callback_calls_ == 0) |
| 86 quit_closure_.Run(); | 86 quit_closure_.Run(); |
| 87 | 87 |
| 88 if (more_requests > 0) | 88 if (more_requests > 0) |
| 89 SequentiallyRequestGlobalDumps(more_requests); | 89 SequentiallyRequestGlobalDumps(more_requests); |
| 90 } | 90 } |
| 91 | 91 |
| 92 void SequentiallyRequestGlobalDumps(int num_requests) { | 92 void SequentiallyRequestGlobalDumps(int num_requests) { |
| 93 MemoryDumpManager::GetInstance()->RequestGlobalDump( | 93 MemoryDumpManager::GetInstance()->RequestGlobalDump( |
| 94 MemoryDumpType::EXPLICITLY_TRIGGERED, MemoryDumpLevelOfDetail::LIGHT, | 94 MemoryDumpType::EXPLICITLY_TRIGGERED, MemoryDumpLevelOfDetail::LIGHT, |
| 95 base::Bind(&MemoryDumpManagerDelegateImplTest::OnGlobalMemoryDumpDone, | 95 base::Bind(&ProcessLocalDumpManagerImplTest::OnGlobalMemoryDumpDone, |
| 96 base::Unretained(this), num_requests - 1)); | 96 base::Unretained(this), num_requests - 1)); |
| 97 } | 97 } |
| 98 | 98 |
| 99 int expected_callback_calls_; | 99 int expected_callback_calls_; |
| 100 int dump_requests_received_by_coordinator_; | 100 int dump_requests_received_by_coordinator_; |
| 101 base::Closure quit_closure_; | 101 base::Closure quit_closure_; |
| 102 | 102 |
| 103 private: | 103 private: |
| 104 std::unique_ptr<base::MessageLoop> message_loop_; | 104 std::unique_ptr<base::MessageLoop> message_loop_; |
| 105 std::unique_ptr<MockCoordinator> coordinator_; | 105 std::unique_ptr<MockCoordinator> coordinator_; |
| 106 std::unique_ptr<MemoryDumpManager> mdm_; | 106 std::unique_ptr<MemoryDumpManager> mdm_; |
| 107 std::unique_ptr<ProcessLocalDumpManagerImpl> local_manager_impl_; |
| 107 }; | 108 }; |
| 108 | 109 |
| 109 // Makes several global dump requests each after receiving the ACK for the | 110 // Makes several global dump requests each after receiving the ACK for the |
| 110 // previous one. There should be no throttling and all requests should be | 111 // previous one. There should be no throttling and all requests should be |
| 111 // forwarded to the coordinator. | 112 // forwarded to the coordinator. |
| 112 TEST_F(MemoryDumpManagerDelegateImplTest, NonOverlappingMemoryDumpRequests) { | 113 TEST_F(ProcessLocalDumpManagerImplTest, NonOverlappingMemoryDumpRequests) { |
| 113 base::RunLoop run_loop; | 114 base::RunLoop run_loop; |
| 114 expected_callback_calls_ = 3; | 115 expected_callback_calls_ = 3; |
| 115 quit_closure_ = run_loop.QuitClosure(); | 116 quit_closure_ = run_loop.QuitClosure(); |
| 116 SequentiallyRequestGlobalDumps(3); | 117 SequentiallyRequestGlobalDumps(3); |
| 117 run_loop.Run(); | 118 run_loop.Run(); |
| 118 EXPECT_EQ(3, dump_requests_received_by_coordinator_); | 119 EXPECT_EQ(3, dump_requests_received_by_coordinator_); |
| 119 } | 120 } |
| 120 | 121 |
| 121 // Makes several global dump requests without waiting for previous requests to | 122 // Makes several global dump requests without waiting for previous requests to |
| 122 // finish. Only the first request should make it to the coordinator. The rest | 123 // finish. Only the first request should make it to the coordinator. The rest |
| 123 // should be cancelled. | 124 // should be cancelled. |
| 124 TEST_F(MemoryDumpManagerDelegateImplTest, OverlappingMemoryDumpRequests) { | 125 TEST_F(ProcessLocalDumpManagerImplTest, OverlappingMemoryDumpRequests) { |
| 125 base::RunLoop run_loop; | 126 base::RunLoop run_loop; |
| 126 expected_callback_calls_ = 3; | 127 expected_callback_calls_ = 3; |
| 127 quit_closure_ = run_loop.QuitClosure(); | 128 quit_closure_ = run_loop.QuitClosure(); |
| 128 SequentiallyRequestGlobalDumps(1); | 129 SequentiallyRequestGlobalDumps(1); |
| 129 SequentiallyRequestGlobalDumps(1); | 130 SequentiallyRequestGlobalDumps(1); |
| 130 SequentiallyRequestGlobalDumps(1); | 131 SequentiallyRequestGlobalDumps(1); |
| 131 run_loop.Run(); | 132 run_loop.Run(); |
| 132 EXPECT_EQ(1, dump_requests_received_by_coordinator_); | 133 EXPECT_EQ(1, dump_requests_received_by_coordinator_); |
| 133 } | 134 } |
| 134 | 135 |
| 135 } // namespace memory_instrumentation | 136 } // namespace memory_instrumentation |
| OLD | NEW |