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