Chromium Code Reviews| 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 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(base::SingleThreadTaskRunner* task_runner) { | |
|
Primiano Tucci (use gerrit)
2017/02/17 19:08:44
Hmm you should't keep pointers to the TaskRunner t
chiniforooshan
2017/02/22 05:16:34
Done.
| |
| 44 DCHECK(!task_runner_ || task_runner_ == task_runner); | |
| 45 if (!task_runner_) { | |
| 46 task_runner_ = task_runner; | |
| 47 if (task_runner->RunsTasksOnCurrentThread()) { | |
| 48 InitializeMemoryDumpManager(); | |
| 49 } else { | |
| 50 task_runner->PostTask( | |
| 51 FROM_HERE, base::Bind(&CoordinatorImpl::InitializeMemoryDumpManager, | |
| 52 base::Unretained(this))); | |
| 53 } | |
| 54 } | |
| 55 } | |
| 56 | |
| 57 void CoordinatorImpl::InitializeForTest( | |
| 58 base::SingleThreadTaskRunner* task_runner) { | |
| 59 task_runner_ = task_runner; | |
| 60 } | |
| 61 | |
| 62 void CoordinatorImpl::InitializeMemoryDumpManager() { | |
| 63 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | |
| 64 auto delegate = | |
| 65 base::MakeUnique<memory_instrumentation::MemoryDumpManagerDelegateImpl>( | |
| 66 this, task_runner_); | |
| 67 base::trace_event::MemoryDumpManager::GetInstance()->Initialize( | |
| 68 std::move(delegate)); | |
| 69 } | |
| 70 | |
| 39 void CoordinatorImpl::BindCoordinatorRequest( | 71 void CoordinatorImpl::BindCoordinatorRequest( |
| 40 mojom::CoordinatorRequest request) { | 72 mojom::CoordinatorRequest request) { |
| 73 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | |
| 41 bindings_.AddBinding(this, std::move(request)); | 74 bindings_.AddBinding(this, std::move(request)); |
| 42 } | 75 } |
| 43 | 76 |
| 44 CoordinatorImpl::QueuedMemoryDumpRequest::QueuedMemoryDumpRequest( | 77 CoordinatorImpl::QueuedMemoryDumpRequest::QueuedMemoryDumpRequest( |
| 45 const base::trace_event::MemoryDumpRequestArgs args, | 78 const base::trace_event::MemoryDumpRequestArgs args, |
| 46 const RequestGlobalMemoryDumpCallback callback) | 79 const RequestGlobalMemoryDumpCallback callback) |
| 47 : args(args), callback(callback) {} | 80 : args(args), callback(callback) {} |
| 48 | 81 |
| 49 CoordinatorImpl::QueuedMemoryDumpRequest::~QueuedMemoryDumpRequest() {} | 82 CoordinatorImpl::QueuedMemoryDumpRequest::~QueuedMemoryDumpRequest() {} |
| 50 | 83 |
| 51 void CoordinatorImpl::RequestGlobalMemoryDump( | 84 void CoordinatorImpl::RequestGlobalMemoryDump( |
| 52 const base::trace_event::MemoryDumpRequestArgs& args, | 85 const base::trace_event::MemoryDumpRequestArgs& args, |
| 53 const RequestGlobalMemoryDumpCallback& callback) { | 86 const RequestGlobalMemoryDumpCallback& callback) { |
| 54 DCHECK(thread_checker_.CalledOnValidThread()); | 87 DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| 55 | |
| 56 bool another_dump_already_in_progress = !queued_memory_dump_requests_.empty(); | 88 bool another_dump_already_in_progress = !queued_memory_dump_requests_.empty(); |
| 57 | 89 |
| 58 // If this is a periodic memory dump request and there already is another | 90 // 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 | 91 // request in the queue with the same level of detail, there's no point in |
| 60 // enqueuing this request. | 92 // enqueuing this request. |
| 61 if (another_dump_already_in_progress && | 93 if (another_dump_already_in_progress && |
| 62 args.dump_type == base::trace_event::MemoryDumpType::PERIODIC_INTERVAL) { | 94 args.dump_type == base::trace_event::MemoryDumpType::PERIODIC_INTERVAL) { |
| 63 for (const auto& request : queued_memory_dump_requests_) { | 95 for (const auto& request : queued_memory_dump_requests_) { |
| 64 if (request.args.level_of_detail == args.level_of_detail) { | 96 if (request.args.level_of_detail == args.level_of_detail) { |
| 65 VLOG(1) << base::trace_event::MemoryDumpManager::kLogPrefix << " (" | 97 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 | 112 // If another dump is already in progress, this dump will automatically be |
| 81 // scheduled when the other dump finishes. | 113 // scheduled when the other dump finishes. |
| 82 if (another_dump_already_in_progress) | 114 if (another_dump_already_in_progress) |
| 83 return; | 115 return; |
| 84 | 116 |
| 85 PerformNextQueuedGlobalMemoryDump(); | 117 PerformNextQueuedGlobalMemoryDump(); |
| 86 } | 118 } |
| 87 | 119 |
| 88 void CoordinatorImpl::RegisterProcessLocalDumpManager( | 120 void CoordinatorImpl::RegisterProcessLocalDumpManager( |
| 89 mojom::ProcessLocalDumpManagerPtr process_manager) { | 121 mojom::ProcessLocalDumpManagerPtr process_manager) { |
| 90 DCHECK(thread_checker_.CalledOnValidThread()); | 122 DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| 91 | |
| 92 process_manager.set_connection_error_handler( | 123 process_manager.set_connection_error_handler( |
| 93 base::Bind(&CoordinatorImpl::UnregisterProcessLocalDumpManager, | 124 base::Bind(&CoordinatorImpl::UnregisterProcessLocalDumpManager, |
| 94 base::Unretained(this), process_manager.get())); | 125 base::Unretained(this), process_manager.get())); |
| 95 auto result = process_managers_.insert( | 126 auto result = process_managers_.insert( |
| 96 std::make_pair<mojom::ProcessLocalDumpManager*, | 127 std::make_pair<mojom::ProcessLocalDumpManager*, |
| 97 mojom::ProcessLocalDumpManagerPtr>( | 128 mojom::ProcessLocalDumpManagerPtr>( |
| 98 process_manager.get(), std::move(process_manager))); | 129 process_manager.get(), std::move(process_manager))); |
| 99 DCHECK(result.second); | 130 DCHECK(result.second); |
| 100 } | 131 } |
| 101 | 132 |
| 102 void CoordinatorImpl::UnregisterProcessLocalDumpManager( | 133 void CoordinatorImpl::UnregisterProcessLocalDumpManager( |
| 103 mojom::ProcessLocalDumpManager* process_manager) { | 134 mojom::ProcessLocalDumpManager* process_manager) { |
| 104 DCHECK(process_managers_.erase(process_manager) == 1); | 135 size_t num_deleted = process_managers_.erase(process_manager); |
| 136 DCHECK(num_deleted == 1); | |
| 105 | 137 |
| 106 // Check if we are waiting for an ack from this process-local manager. | 138 // Check if we are waiting for an ack from this process-local manager. |
| 107 if (pending_process_managers_.find(process_manager) != | 139 if (pending_process_managers_.find(process_manager) != |
| 108 pending_process_managers_.end()) { | 140 pending_process_managers_.end()) { |
| 109 DCHECK(!queued_memory_dump_requests_.empty()); | 141 DCHECK(!queued_memory_dump_requests_.empty()); |
| 110 OnProcessMemoryDumpResponse( | 142 OnProcessMemoryDumpResponse( |
| 111 process_manager, queued_memory_dump_requests_.front().args.dump_guid, | 143 process_manager, queued_memory_dump_requests_.front().args.dump_guid, |
| 112 false /* success */); | 144 false /* success */); |
| 113 } | 145 } |
| 114 } | 146 } |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 171 // Schedule the next queued dump (if applicable). | 203 // Schedule the next queued dump (if applicable). |
| 172 if (!queued_memory_dump_requests_.empty()) { | 204 if (!queued_memory_dump_requests_.empty()) { |
| 173 base::ThreadTaskRunnerHandle::Get()->PostTask( | 205 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 174 FROM_HERE, | 206 FROM_HERE, |
| 175 base::Bind(&CoordinatorImpl::PerformNextQueuedGlobalMemoryDump, | 207 base::Bind(&CoordinatorImpl::PerformNextQueuedGlobalMemoryDump, |
| 176 base::Unretained(this))); | 208 base::Unretained(this))); |
| 177 } | 209 } |
| 178 } | 210 } |
| 179 | 211 |
| 180 } // namespace memory_instrumentation | 212 } // namespace memory_instrumentation |
| OLD | NEW |