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