Chromium Code Reviews| Index: services/resource_coordinator/public/cpp/memory/memory_dump_manager_delegate_impl.cc |
| diff --git a/services/resource_coordinator/public/cpp/memory/memory_dump_manager_delegate_impl.cc b/services/resource_coordinator/public/cpp/memory/memory_dump_manager_delegate_impl.cc |
| index 7072b6775487065afc73b76e77c2a5084d60d0d0..648013d8008408e34840eb18fa0fe985442a8728 100644 |
| --- a/services/resource_coordinator/public/cpp/memory/memory_dump_manager_delegate_impl.cc |
| +++ b/services/resource_coordinator/public/cpp/memory/memory_dump_manager_delegate_impl.cc |
| @@ -4,34 +4,84 @@ |
| #include "services/resource_coordinator/public/cpp/memory/memory_dump_manager_delegate_impl.h" |
| +#include "base/bind.h" |
| +#include "base/bind_helpers.h" |
| +#include "base/lazy_instance.h" |
| +#include "base/single_thread_task_runner.h" |
| +#include "base/synchronization/lock.h" |
| #include "base/trace_event/memory_dump_request_args.h" |
| #include "mojo/public/cpp/bindings/interface_request.h" |
| #include "services/resource_coordinator/public/cpp/memory/coordinator.h" |
| +#include "services/resource_coordinator/public/interfaces/memory/constants.mojom.h" |
| #include "services/resource_coordinator/public/interfaces/memory/memory_instrumentation.mojom.h" |
| #include "services/service_manager/public/cpp/interface_provider.h" |
| namespace memory_instrumentation { |
| -MemoryDumpManagerDelegateImpl::MemoryDumpManagerDelegateImpl( |
| - service_manager::InterfaceProvider* interface_provider) |
| - : is_coordinator_(false), binding_(this) { |
| +namespace { |
| + |
| +base::LazyInstance<MemoryDumpManagerDelegateImpl>::Leaky |
| + g_memory_dump_manager_delegate = LAZY_INSTANCE_INITIALIZER; |
| + |
| +} // namespace |
| + |
| +// static |
| +MemoryDumpManagerDelegateImpl* MemoryDumpManagerDelegateImpl::GetInstance() { |
| + return g_memory_dump_manager_delegate.Pointer(); |
| +} |
| + |
| +MemoryDumpManagerDelegateImpl::MemoryDumpManagerDelegateImpl() |
| + : initialized_(false), |
| + binding_(this), |
| + task_runner_(nullptr), |
| + pending_memory_dump_guid_(0) {} |
| + |
| +MemoryDumpManagerDelegateImpl::~MemoryDumpManagerDelegateImpl() {} |
| + |
| +void MemoryDumpManagerDelegateImpl::InitializeWithInterfaceProvider( |
| + service_manager::InterfaceProvider* interface_provider) { |
| + { |
| + base::AutoLock lock(initialized_lock_); |
|
Primiano Tucci (use gerrit)
2017/03/08 12:05:43
Hmm this is a bit of an odd pattern and honestly s
chiniforooshan
2017/03/08 19:03:43
I learned about static locals today. Thanks!
One
|
| + DCHECK(!initialized_); |
| + initialized_ = true; |
| + } |
| + |
| interface_provider->GetInterface(mojo::MakeRequest(&coordinator_)); |
| coordinator_->RegisterProcessLocalDumpManager( |
| binding_.CreateInterfacePtrAndBind()); |
| + base::trace_event::MemoryDumpManager::GetInstance()->Initialize(this); |
| } |
| -MemoryDumpManagerDelegateImpl::MemoryDumpManagerDelegateImpl( |
| - Coordinator* coordinator) |
| - : is_coordinator_(true), binding_(this) { |
| +void MemoryDumpManagerDelegateImpl::InitializeWithCoordinator( |
| + Coordinator* coordinator, |
| + scoped_refptr<base::SingleThreadTaskRunner> task_runner) { |
| + DCHECK(task_runner); |
| + if (!task_runner->RunsTasksOnCurrentThread()) { |
| + task_runner->PostTask( |
| + FROM_HERE, |
| + base::Bind(&MemoryDumpManagerDelegateImpl::InitializeWithCoordinator, |
| + base::Unretained(this), base::Unretained(coordinator), |
| + task_runner)); |
| + return; |
| + } |
| + |
| + { |
| + base::AutoLock lock(initialized_lock_); |
| + DCHECK(!initialized_); |
| + initialized_ = true; |
| + } |
| + |
| + task_runner_ = task_runner; |
| coordinator->BindCoordinatorRequest(mojo::MakeRequest(&coordinator_)); |
| coordinator_->RegisterProcessLocalDumpManager( |
| binding_.CreateInterfacePtrAndBind()); |
| + base::trace_event::MemoryDumpManager::GetInstance()->set_tracing_process_id( |
| + mojom::kServiceTracingProcessId); |
|
ssid
2017/03/08 18:37:13
How does this fix the problem of browser process n
chiniforooshan
2017/03/08 22:39:03
Note that InitializeWithCoordinator is only called
ssid
2017/03/09 00:23:15
Ah sorry. I thought they both call the same functi
|
| + base::trace_event::MemoryDumpManager::GetInstance()->Initialize(this); |
| } |
| -MemoryDumpManagerDelegateImpl::~MemoryDumpManagerDelegateImpl() {} |
| - |
| bool MemoryDumpManagerDelegateImpl::IsCoordinator() const { |
| - return is_coordinator_; |
| + return task_runner_ != nullptr; |
| } |
| void MemoryDumpManagerDelegateImpl::RequestProcessMemoryDump( |
| @@ -43,7 +93,50 @@ void MemoryDumpManagerDelegateImpl::RequestProcessMemoryDump( |
| void MemoryDumpManagerDelegateImpl::RequestGlobalMemoryDump( |
| const base::trace_event::MemoryDumpRequestArgs& args, |
| const base::trace_event::MemoryDumpCallback& callback) { |
| - coordinator_->RequestGlobalMemoryDump(args, callback); |
| + // Note: This condition is here to match the old behavior. If the delegate is |
|
Primiano Tucci (use gerrit)
2017/03/08 12:05:43
Ah, TIL. Not sure this would ever intended. I thin
ssid
2017/03/08 18:37:13
Some unittests are assuming that the child does on
chiniforooshan
2017/03/08 19:03:43
Done.
|
| + // in the browser process, we do not drop parallel requests in the delegate |
| + // and so they will be queued by the Coordinator service (see |
| + // CoordinatorImpl::RequestGlobalMemoryDump). If the delegate is in a child |
| + // process, parallel requests will be cancelled. |
| + // |
| + // TODO(chiniforooshan): After transitioning to the mojo-based service is |
| + // completed, we should enable queueing parallel global memory dump requests |
| + // by delegates on all processes. |
| + if (task_runner_) { |
|
Primiano Tucci (use gerrit)
2017/03/08 12:05:43
I think that IsCoordinator() here woul make this
ssid
2017/03/08 18:37:13
Sorry about this. I asked to remove is_coordinator
chiniforooshan
2017/03/08 19:03:43
Done.
|
| + DCHECK(task_runner_); |
|
Primiano Tucci (use gerrit)
2017/03/08 12:05:43
uh? how can this fail given the if above?
chiniforooshan
2017/03/08 19:03:43
Done.
|
| + task_runner_->PostTask( |
| + FROM_HERE, |
| + base::Bind(&mojom::Coordinator::RequestGlobalMemoryDump, |
| + base::Unretained(coordinator_.get()), args, callback)); |
| + return; |
| + } |
| + |
| + { |
| + base::AutoLock lock(pending_memory_dump_guid_lock_); |
| + if (pending_memory_dump_guid_) { |
| + callback.Run(args.dump_guid, false); |
| + return; |
| + } |
| + pending_memory_dump_guid_ = args.dump_guid; |
| + } |
| + DCHECK(!task_runner_); |
| + auto callback_proxy = |
| + base::Bind(&MemoryDumpManagerDelegateImpl::MemoryDumpCallbackProxy, |
| + base::Unretained(this), callback); |
| + coordinator_->RequestGlobalMemoryDump(args, callback_proxy); |
| +} |
| + |
| +void MemoryDumpManagerDelegateImpl::MemoryDumpCallbackProxy( |
| + const base::trace_event::MemoryDumpCallback& callback, |
| + uint64_t dump_guid, |
| + bool success) { |
| + DCHECK_NE(0U, pending_memory_dump_guid_); |
| + pending_memory_dump_guid_ = 0; |
| + callback.Run(dump_guid, success); |
| +} |
| + |
| +void MemoryDumpManagerDelegateImpl::SetAsNonCoordinatorForTesting() { |
| + task_runner_ = nullptr; |
| } |
| } // namespace memory_instrumentation |