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/public/cpp/memory/memory_dump_manager_de legate_impl.h" | 5 #include "services/resource_coordinator/public/cpp/memory/memory_dump_manager_de legate_impl.h" |
6 | 6 |
7 #include "base/bind.h" | |
8 #include "base/bind_helpers.h" | |
9 #include "base/lazy_instance.h" | |
10 #include "base/single_thread_task_runner.h" | |
11 #include "base/synchronization/lock.h" | |
7 #include "base/trace_event/memory_dump_request_args.h" | 12 #include "base/trace_event/memory_dump_request_args.h" |
8 #include "mojo/public/cpp/bindings/interface_request.h" | 13 #include "mojo/public/cpp/bindings/interface_request.h" |
9 #include "services/resource_coordinator/public/cpp/memory/coordinator.h" | 14 #include "services/resource_coordinator/public/cpp/memory/coordinator.h" |
15 #include "services/resource_coordinator/public/interfaces/memory/constants.mojom .h" | |
10 #include "services/resource_coordinator/public/interfaces/memory/memory_instrume ntation.mojom.h" | 16 #include "services/resource_coordinator/public/interfaces/memory/memory_instrume ntation.mojom.h" |
11 #include "services/service_manager/public/cpp/interface_provider.h" | 17 #include "services/service_manager/public/cpp/interface_provider.h" |
12 | 18 |
13 namespace memory_instrumentation { | 19 namespace memory_instrumentation { |
14 | 20 |
15 MemoryDumpManagerDelegateImpl::MemoryDumpManagerDelegateImpl( | 21 namespace { |
16 service_manager::InterfaceProvider* interface_provider) | 22 |
17 : is_coordinator_(false), binding_(this) { | 23 base::LazyInstance<MemoryDumpManagerDelegateImpl>::Leaky |
24 g_memory_dump_manager_delegate = LAZY_INSTANCE_INITIALIZER; | |
25 | |
26 } // namespace | |
27 | |
28 // static | |
29 MemoryDumpManagerDelegateImpl* MemoryDumpManagerDelegateImpl::GetInstance() { | |
30 return g_memory_dump_manager_delegate.Pointer(); | |
31 } | |
32 | |
33 MemoryDumpManagerDelegateImpl::MemoryDumpManagerDelegateImpl() | |
34 : initialized_(false), | |
35 binding_(this), | |
36 task_runner_(nullptr), | |
37 pending_memory_dump_guid_(0) {} | |
38 | |
39 MemoryDumpManagerDelegateImpl::~MemoryDumpManagerDelegateImpl() {} | |
40 | |
41 void MemoryDumpManagerDelegateImpl::InitializeWithInterfaceProvider( | |
42 service_manager::InterfaceProvider* interface_provider) { | |
43 { | |
44 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
| |
45 DCHECK(!initialized_); | |
46 initialized_ = true; | |
47 } | |
48 | |
18 interface_provider->GetInterface(mojo::MakeRequest(&coordinator_)); | 49 interface_provider->GetInterface(mojo::MakeRequest(&coordinator_)); |
19 coordinator_->RegisterProcessLocalDumpManager( | 50 coordinator_->RegisterProcessLocalDumpManager( |
20 binding_.CreateInterfacePtrAndBind()); | 51 binding_.CreateInterfacePtrAndBind()); |
52 base::trace_event::MemoryDumpManager::GetInstance()->Initialize(this); | |
21 } | 53 } |
22 | 54 |
23 MemoryDumpManagerDelegateImpl::MemoryDumpManagerDelegateImpl( | 55 void MemoryDumpManagerDelegateImpl::InitializeWithCoordinator( |
24 Coordinator* coordinator) | 56 Coordinator* coordinator, |
25 : is_coordinator_(true), binding_(this) { | 57 scoped_refptr<base::SingleThreadTaskRunner> task_runner) { |
58 DCHECK(task_runner); | |
59 if (!task_runner->RunsTasksOnCurrentThread()) { | |
60 task_runner->PostTask( | |
61 FROM_HERE, | |
62 base::Bind(&MemoryDumpManagerDelegateImpl::InitializeWithCoordinator, | |
63 base::Unretained(this), base::Unretained(coordinator), | |
64 task_runner)); | |
65 return; | |
66 } | |
67 | |
68 { | |
69 base::AutoLock lock(initialized_lock_); | |
70 DCHECK(!initialized_); | |
71 initialized_ = true; | |
72 } | |
73 | |
74 task_runner_ = task_runner; | |
26 coordinator->BindCoordinatorRequest(mojo::MakeRequest(&coordinator_)); | 75 coordinator->BindCoordinatorRequest(mojo::MakeRequest(&coordinator_)); |
27 coordinator_->RegisterProcessLocalDumpManager( | 76 coordinator_->RegisterProcessLocalDumpManager( |
28 binding_.CreateInterfacePtrAndBind()); | 77 binding_.CreateInterfacePtrAndBind()); |
78 base::trace_event::MemoryDumpManager::GetInstance()->set_tracing_process_id( | |
79 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
| |
80 base::trace_event::MemoryDumpManager::GetInstance()->Initialize(this); | |
29 } | 81 } |
30 | 82 |
31 MemoryDumpManagerDelegateImpl::~MemoryDumpManagerDelegateImpl() {} | |
32 | |
33 bool MemoryDumpManagerDelegateImpl::IsCoordinator() const { | 83 bool MemoryDumpManagerDelegateImpl::IsCoordinator() const { |
34 return is_coordinator_; | 84 return task_runner_ != nullptr; |
35 } | 85 } |
36 | 86 |
37 void MemoryDumpManagerDelegateImpl::RequestProcessMemoryDump( | 87 void MemoryDumpManagerDelegateImpl::RequestProcessMemoryDump( |
38 const base::trace_event::MemoryDumpRequestArgs& args, | 88 const base::trace_event::MemoryDumpRequestArgs& args, |
39 const RequestProcessMemoryDumpCallback& callback) { | 89 const RequestProcessMemoryDumpCallback& callback) { |
40 MemoryDumpManagerDelegate::CreateProcessDump(args, callback); | 90 MemoryDumpManagerDelegate::CreateProcessDump(args, callback); |
41 } | 91 } |
42 | 92 |
43 void MemoryDumpManagerDelegateImpl::RequestGlobalMemoryDump( | 93 void MemoryDumpManagerDelegateImpl::RequestGlobalMemoryDump( |
44 const base::trace_event::MemoryDumpRequestArgs& args, | 94 const base::trace_event::MemoryDumpRequestArgs& args, |
45 const base::trace_event::MemoryDumpCallback& callback) { | 95 const base::trace_event::MemoryDumpCallback& callback) { |
46 coordinator_->RequestGlobalMemoryDump(args, callback); | 96 // 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.
| |
97 // in the browser process, we do not drop parallel requests in the delegate | |
98 // and so they will be queued by the Coordinator service (see | |
99 // CoordinatorImpl::RequestGlobalMemoryDump). If the delegate is in a child | |
100 // process, parallel requests will be cancelled. | |
101 // | |
102 // TODO(chiniforooshan): After transitioning to the mojo-based service is | |
103 // completed, we should enable queueing parallel global memory dump requests | |
104 // by delegates on all processes. | |
105 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.
| |
106 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.
| |
107 task_runner_->PostTask( | |
108 FROM_HERE, | |
109 base::Bind(&mojom::Coordinator::RequestGlobalMemoryDump, | |
110 base::Unretained(coordinator_.get()), args, callback)); | |
111 return; | |
112 } | |
113 | |
114 { | |
115 base::AutoLock lock(pending_memory_dump_guid_lock_); | |
116 if (pending_memory_dump_guid_) { | |
117 callback.Run(args.dump_guid, false); | |
118 return; | |
119 } | |
120 pending_memory_dump_guid_ = args.dump_guid; | |
121 } | |
122 DCHECK(!task_runner_); | |
123 auto callback_proxy = | |
124 base::Bind(&MemoryDumpManagerDelegateImpl::MemoryDumpCallbackProxy, | |
125 base::Unretained(this), callback); | |
126 coordinator_->RequestGlobalMemoryDump(args, callback_proxy); | |
127 } | |
128 | |
129 void MemoryDumpManagerDelegateImpl::MemoryDumpCallbackProxy( | |
130 const base::trace_event::MemoryDumpCallback& callback, | |
131 uint64_t dump_guid, | |
132 bool success) { | |
133 DCHECK_NE(0U, pending_memory_dump_guid_); | |
134 pending_memory_dump_guid_ = 0; | |
135 callback.Run(dump_guid, success); | |
136 } | |
137 | |
138 void MemoryDumpManagerDelegateImpl::SetAsNonCoordinatorForTesting() { | |
139 task_runner_ = nullptr; | |
47 } | 140 } |
48 | 141 |
49 } // namespace memory_instrumentation | 142 } // namespace memory_instrumentation |
OLD | NEW |