| 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/memory/coordinator/coordinator_impl.h" | 5 #include "services/resource_coordinator/memory/coordinator/coordinator_impl.h" |
| 6 | 6 |
| 7 #include "base/bind.h" |
| 7 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 8 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
| 9 #include "base/location.h" | 10 #include "base/location.h" |
| 10 #include "base/logging.h" | 11 #include "base/logging.h" |
| 11 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/single_thread_task_runner.h" |
| 14 #include "base/threading/platform_thread.h" |
| 12 #include "base/threading/thread_task_runner_handle.h" | 15 #include "base/threading/thread_task_runner_handle.h" |
| 13 #include "base/trace_event/memory_dump_manager.h" | 16 #include "base/trace_event/memory_dump_manager.h" |
| 14 #include "base/trace_event/memory_dump_request_args.h" | 17 #include "base/trace_event/memory_dump_request_args.h" |
| 18 #include "services/resource_coordinator/public/cpp/memory/memory_dump_manager_de
legate_impl.h" |
| 15 #include "services/resource_coordinator/public/interfaces/memory/memory_instrume
ntation.mojom.h" | 19 #include "services/resource_coordinator/public/interfaces/memory/memory_instrume
ntation.mojom.h" |
| 16 | 20 |
| 17 namespace memory_instrumentation { | 21 namespace memory_instrumentation { |
| 18 | 22 |
| 19 namespace { | 23 namespace { |
| 20 | 24 |
| 21 base::LazyInstance<CoordinatorImpl>::Leaky g_coordinator = | 25 base::LazyInstance<CoordinatorImpl>::Leaky g_coordinator = |
| 22 LAZY_INSTANCE_INITIALIZER; | 26 LAZY_INSTANCE_INITIALIZER; |
| 23 | 27 |
| 24 } // namespace | 28 } // namespace |
| 25 | 29 |
| 26 // static | 30 // static |
| 27 CoordinatorImpl* CoordinatorImpl::GetInstance() { | 31 CoordinatorImpl* CoordinatorImpl::GetInstance( |
| 28 return g_coordinator.Pointer(); | 32 scoped_refptr<base::SingleThreadTaskRunner> task_runner) { |
| 33 CoordinatorImpl* out = g_coordinator.Pointer(); |
| 34 out->Initialize(task_runner); |
| 35 return out; |
| 29 } | 36 } |
| 30 | 37 |
| 31 // TODO(chiniforooshan): Initialize the global MemoryDumpManager instance here. | 38 CoordinatorImpl::CoordinatorImpl() |
| 32 // This is how the global MemoryDumpManager gets a reference to the delegate on | 39 : failed_memory_dump_count_(0), task_runner_(nullptr) {} |
| 33 // the service (read the browser) process for service process memory dumps. This | |
| 34 // can be done when the delegate implementation is landed. | |
| 35 CoordinatorImpl::CoordinatorImpl() : failed_memory_dump_count_(0) {} | |
| 36 | 40 |
| 37 CoordinatorImpl::~CoordinatorImpl() {} | 41 CoordinatorImpl::~CoordinatorImpl() {} |
| 38 | 42 |
| 43 void CoordinatorImpl::Initialize( |
| 44 scoped_refptr<base::SingleThreadTaskRunner> task_runner) { |
| 45 DCHECK(!task_runner_ || task_runner_ == task_runner); |
| 46 |
| 47 if (!task_runner_) { |
| 48 task_runner_ = task_runner; |
| 49 MemoryDumpManagerDelegateImpl* delegate = |
| 50 MemoryDumpManagerDelegateImpl::GetInstance(); |
| 51 delegate->InitializeWithCoordinator(this, task_runner_); |
| 52 } |
| 53 } |
| 54 |
| 55 void CoordinatorImpl::InitializeForTest( |
| 56 scoped_refptr<base::SingleThreadTaskRunner> task_runner) { |
| 57 task_runner_ = task_runner; |
| 58 } |
| 59 |
| 39 void CoordinatorImpl::BindCoordinatorRequest( | 60 void CoordinatorImpl::BindCoordinatorRequest( |
| 40 mojom::CoordinatorRequest request) { | 61 mojom::CoordinatorRequest request) { |
| 62 DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| 41 bindings_.AddBinding(this, std::move(request)); | 63 bindings_.AddBinding(this, std::move(request)); |
| 42 } | 64 } |
| 43 | 65 |
| 44 CoordinatorImpl::QueuedMemoryDumpRequest::QueuedMemoryDumpRequest( | 66 CoordinatorImpl::QueuedMemoryDumpRequest::QueuedMemoryDumpRequest( |
| 45 const base::trace_event::MemoryDumpRequestArgs args, | 67 const base::trace_event::MemoryDumpRequestArgs args, |
| 46 const RequestGlobalMemoryDumpCallback callback) | 68 const RequestGlobalMemoryDumpCallback callback) |
| 47 : args(args), callback(callback) {} | 69 : args(args), callback(callback) {} |
| 48 | 70 |
| 49 CoordinatorImpl::QueuedMemoryDumpRequest::~QueuedMemoryDumpRequest() {} | 71 CoordinatorImpl::QueuedMemoryDumpRequest::~QueuedMemoryDumpRequest() {} |
| 50 | 72 |
| 51 void CoordinatorImpl::RequestGlobalMemoryDump( | 73 void CoordinatorImpl::RequestGlobalMemoryDump( |
| 52 const base::trace_event::MemoryDumpRequestArgs& args, | 74 const base::trace_event::MemoryDumpRequestArgs& args, |
| 53 const RequestGlobalMemoryDumpCallback& callback) { | 75 const RequestGlobalMemoryDumpCallback& callback) { |
| 54 DCHECK(thread_checker_.CalledOnValidThread()); | 76 DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| 55 | |
| 56 bool another_dump_already_in_progress = !queued_memory_dump_requests_.empty(); | 77 bool another_dump_already_in_progress = !queued_memory_dump_requests_.empty(); |
| 57 | 78 |
| 58 // If this is a periodic memory dump request and there already is another | 79 // If this is a periodic memory dump request and there already is another |
| 59 // request in the queue with the same level of detail, there's no point in | 80 // request in the queue with the same level of detail, there's no point in |
| 60 // enqueuing this request. | 81 // enqueuing this request. |
| 61 if (another_dump_already_in_progress && | 82 if (another_dump_already_in_progress && |
| 62 args.dump_type == base::trace_event::MemoryDumpType::PERIODIC_INTERVAL) { | 83 args.dump_type == base::trace_event::MemoryDumpType::PERIODIC_INTERVAL) { |
| 63 for (const auto& request : queued_memory_dump_requests_) { | 84 for (const auto& request : queued_memory_dump_requests_) { |
| 64 if (request.args.level_of_detail == args.level_of_detail) { | 85 if (request.args.level_of_detail == args.level_of_detail) { |
| 65 VLOG(1) << base::trace_event::MemoryDumpManager::kLogPrefix << " (" | 86 VLOG(1) << base::trace_event::MemoryDumpManager::kLogPrefix << " (" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 80 // If another dump is already in progress, this dump will automatically be | 101 // If another dump is already in progress, this dump will automatically be |
| 81 // scheduled when the other dump finishes. | 102 // scheduled when the other dump finishes. |
| 82 if (another_dump_already_in_progress) | 103 if (another_dump_already_in_progress) |
| 83 return; | 104 return; |
| 84 | 105 |
| 85 PerformNextQueuedGlobalMemoryDump(); | 106 PerformNextQueuedGlobalMemoryDump(); |
| 86 } | 107 } |
| 87 | 108 |
| 88 void CoordinatorImpl::RegisterProcessLocalDumpManager( | 109 void CoordinatorImpl::RegisterProcessLocalDumpManager( |
| 89 mojom::ProcessLocalDumpManagerPtr process_manager) { | 110 mojom::ProcessLocalDumpManagerPtr process_manager) { |
| 90 DCHECK(thread_checker_.CalledOnValidThread()); | 111 DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| 91 | |
| 92 process_manager.set_connection_error_handler( | 112 process_manager.set_connection_error_handler( |
| 93 base::Bind(&CoordinatorImpl::UnregisterProcessLocalDumpManager, | 113 base::Bind(&CoordinatorImpl::UnregisterProcessLocalDumpManager, |
| 94 base::Unretained(this), process_manager.get())); | 114 base::Unretained(this), process_manager.get())); |
| 95 auto result = process_managers_.insert( | 115 auto result = process_managers_.insert( |
| 96 std::make_pair<mojom::ProcessLocalDumpManager*, | 116 std::make_pair<mojom::ProcessLocalDumpManager*, |
| 97 mojom::ProcessLocalDumpManagerPtr>( | 117 mojom::ProcessLocalDumpManagerPtr>( |
| 98 process_manager.get(), std::move(process_manager))); | 118 process_manager.get(), std::move(process_manager))); |
| 99 DCHECK(result.second); | 119 DCHECK(result.second); |
| 100 } | 120 } |
| 101 | 121 |
| 102 void CoordinatorImpl::UnregisterProcessLocalDumpManager( | 122 void CoordinatorImpl::UnregisterProcessLocalDumpManager( |
| 103 mojom::ProcessLocalDumpManager* process_manager) { | 123 mojom::ProcessLocalDumpManager* process_manager) { |
| 104 DCHECK(process_managers_.erase(process_manager) == 1); | 124 size_t num_deleted = process_managers_.erase(process_manager); |
| 125 DCHECK(num_deleted == 1); |
| 105 | 126 |
| 106 // Check if we are waiting for an ack from this process-local manager. | 127 // Check if we are waiting for an ack from this process-local manager. |
| 107 if (pending_process_managers_.find(process_manager) != | 128 if (pending_process_managers_.find(process_manager) != |
| 108 pending_process_managers_.end()) { | 129 pending_process_managers_.end()) { |
| 109 DCHECK(!queued_memory_dump_requests_.empty()); | 130 DCHECK(!queued_memory_dump_requests_.empty()); |
| 110 OnProcessMemoryDumpResponse( | 131 OnProcessMemoryDumpResponse( |
| 111 process_manager, queued_memory_dump_requests_.front().args.dump_guid, | 132 process_manager, queued_memory_dump_requests_.front().args.dump_guid, |
| 112 false /* success */); | 133 false /* success */); |
| 113 } | 134 } |
| 114 } | 135 } |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 // Schedule the next queued dump (if applicable). | 192 // Schedule the next queued dump (if applicable). |
| 172 if (!queued_memory_dump_requests_.empty()) { | 193 if (!queued_memory_dump_requests_.empty()) { |
| 173 base::ThreadTaskRunnerHandle::Get()->PostTask( | 194 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 174 FROM_HERE, | 195 FROM_HERE, |
| 175 base::Bind(&CoordinatorImpl::PerformNextQueuedGlobalMemoryDump, | 196 base::Bind(&CoordinatorImpl::PerformNextQueuedGlobalMemoryDump, |
| 176 base::Unretained(this))); | 197 base::Unretained(this))); |
| 177 } | 198 } |
| 178 } | 199 } |
| 179 | 200 |
| 180 } // namespace memory_instrumentation | 201 } // namespace memory_instrumentation |
| OLD | NEW |