Chromium Code Reviews| Index: services/resource_coordinator/memory/coordinator/coordinator_impl.cc |
| diff --git a/services/resource_coordinator/memory/coordinator/coordinator_impl.cc b/services/resource_coordinator/memory/coordinator/coordinator_impl.cc |
| index 5c133f803c472c845b70ba40936c9706365beaa7..501b57ce0428b5c54f520ad70dba4d9d201e40df 100644 |
| --- a/services/resource_coordinator/memory/coordinator/coordinator_impl.cc |
| +++ b/services/resource_coordinator/memory/coordinator/coordinator_impl.cc |
| @@ -4,14 +4,18 @@ |
| #include "services/resource_coordinator/memory/coordinator/coordinator_impl.h" |
| +#include "base/bind.h" |
| #include "base/bind_helpers.h" |
| #include "base/lazy_instance.h" |
| #include "base/location.h" |
| #include "base/logging.h" |
| #include "base/memory/ref_counted.h" |
| +#include "base/single_thread_task_runner.h" |
| +#include "base/threading/platform_thread.h" |
| #include "base/threading/thread_task_runner_handle.h" |
| #include "base/trace_event/memory_dump_manager.h" |
| #include "base/trace_event/memory_dump_request_args.h" |
| +#include "services/resource_coordinator/public/cpp/memory/memory_dump_manager_delegate_impl.h" |
| #include "services/resource_coordinator/public/interfaces/memory/memory_instrumentation.mojom.h" |
| namespace memory_instrumentation { |
| @@ -24,20 +28,49 @@ base::LazyInstance<CoordinatorImpl>::Leaky g_coordinator = |
| } // namespace |
| // static |
| -CoordinatorImpl* CoordinatorImpl::GetInstance() { |
| - return g_coordinator.Pointer(); |
| +CoordinatorImpl* CoordinatorImpl::GetInstance( |
| + base::SingleThreadTaskRunner* task_runner) { |
| + CoordinatorImpl* out = g_coordinator.Pointer(); |
| + out->Initialize(task_runner); |
| + return out; |
| } |
| -// TODO(chiniforooshan): Initialize the global MemoryDumpManager instance here. |
| -// This is how the global MemoryDumpManager gets a reference to the delegate on |
| -// the service (read the browser) process for service process memory dumps. This |
| -// can be done when the delegate implementation is landed. |
| -CoordinatorImpl::CoordinatorImpl() : failed_memory_dump_count_(0) {} |
| +CoordinatorImpl::CoordinatorImpl() |
| + : failed_memory_dump_count_(0), task_runner_(nullptr) {} |
| CoordinatorImpl::~CoordinatorImpl() {} |
| +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.
|
| + DCHECK(!task_runner_ || task_runner_ == task_runner); |
| + if (!task_runner_) { |
| + task_runner_ = task_runner; |
| + if (task_runner->RunsTasksOnCurrentThread()) { |
| + InitializeMemoryDumpManager(); |
| + } else { |
| + task_runner->PostTask( |
| + FROM_HERE, base::Bind(&CoordinatorImpl::InitializeMemoryDumpManager, |
| + base::Unretained(this))); |
| + } |
| + } |
| +} |
| + |
| +void CoordinatorImpl::InitializeForTest( |
| + base::SingleThreadTaskRunner* task_runner) { |
| + task_runner_ = task_runner; |
| +} |
| + |
| +void CoordinatorImpl::InitializeMemoryDumpManager() { |
| + DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| + auto delegate = |
| + base::MakeUnique<memory_instrumentation::MemoryDumpManagerDelegateImpl>( |
| + this, task_runner_); |
| + base::trace_event::MemoryDumpManager::GetInstance()->Initialize( |
| + std::move(delegate)); |
| +} |
| + |
| void CoordinatorImpl::BindCoordinatorRequest( |
| mojom::CoordinatorRequest request) { |
| + DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| bindings_.AddBinding(this, std::move(request)); |
| } |
| @@ -51,8 +84,7 @@ CoordinatorImpl::QueuedMemoryDumpRequest::~QueuedMemoryDumpRequest() {} |
| void CoordinatorImpl::RequestGlobalMemoryDump( |
| const base::trace_event::MemoryDumpRequestArgs& args, |
| const RequestGlobalMemoryDumpCallback& callback) { |
| - DCHECK(thread_checker_.CalledOnValidThread()); |
| - |
| + DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| bool another_dump_already_in_progress = !queued_memory_dump_requests_.empty(); |
| // If this is a periodic memory dump request and there already is another |
| @@ -87,8 +119,7 @@ void CoordinatorImpl::RequestGlobalMemoryDump( |
| void CoordinatorImpl::RegisterProcessLocalDumpManager( |
| mojom::ProcessLocalDumpManagerPtr process_manager) { |
| - DCHECK(thread_checker_.CalledOnValidThread()); |
| - |
| + DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| process_manager.set_connection_error_handler( |
| base::Bind(&CoordinatorImpl::UnregisterProcessLocalDumpManager, |
| base::Unretained(this), process_manager.get())); |
| @@ -101,7 +132,8 @@ void CoordinatorImpl::RegisterProcessLocalDumpManager( |
| void CoordinatorImpl::UnregisterProcessLocalDumpManager( |
| mojom::ProcessLocalDumpManager* process_manager) { |
| - DCHECK(process_managers_.erase(process_manager) == 1); |
| + size_t num_deleted = process_managers_.erase(process_manager); |
| + DCHECK(num_deleted == 1); |
| // Check if we are waiting for an ack from this process-local manager. |
| if (pending_process_managers_.find(process_manager) != |