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