Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(134)

Side by Side Diff: services/resource_coordinator/memory/coordinator/coordinator_impl.cc

Issue 2694083005: memory-infra: Finish moving memory_infra from TracingController (Closed)
Patch Set: . Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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) {
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 std::unique_ptr<MemoryDumpManagerDelegateImpl> delegate(
oystein (OOO til 10th of July) 2017/02/16 20:50:01 auto delegate = base::MakeUnique<blah>(...)
chiniforooshan 2017/02/16 22:54:15 Done.
65 new MemoryDumpManagerDelegateImpl(this, task_runner_));
66 base::trace_event::MemoryDumpManager::GetInstance()->Initialize(
67 std::move(delegate));
68 }
69
39 void CoordinatorImpl::BindCoordinatorRequest( 70 void CoordinatorImpl::BindCoordinatorRequest(
40 mojom::CoordinatorRequest request) { 71 mojom::CoordinatorRequest request) {
72 DCHECK(task_runner_->RunsTasksOnCurrentThread());
41 bindings_.AddBinding(this, std::move(request)); 73 bindings_.AddBinding(this, std::move(request));
42 } 74 }
43 75
44 CoordinatorImpl::QueuedMemoryDumpRequest::QueuedMemoryDumpRequest( 76 CoordinatorImpl::QueuedMemoryDumpRequest::QueuedMemoryDumpRequest(
45 const base::trace_event::MemoryDumpRequestArgs args, 77 const base::trace_event::MemoryDumpRequestArgs args,
46 const RequestGlobalMemoryDumpCallback callback) 78 const RequestGlobalMemoryDumpCallback callback)
47 : args(args), callback(callback) {} 79 : args(args), callback(callback) {}
48 80
49 CoordinatorImpl::QueuedMemoryDumpRequest::~QueuedMemoryDumpRequest() {} 81 CoordinatorImpl::QueuedMemoryDumpRequest::~QueuedMemoryDumpRequest() {}
50 82
51 void CoordinatorImpl::RequestGlobalMemoryDump( 83 void CoordinatorImpl::RequestGlobalMemoryDump(
52 const base::trace_event::MemoryDumpRequestArgs& args, 84 const base::trace_event::MemoryDumpRequestArgs& args,
53 const RequestGlobalMemoryDumpCallback& callback) { 85 const RequestGlobalMemoryDumpCallback& callback) {
54 DCHECK(thread_checker_.CalledOnValidThread()); 86 DCHECK(task_runner_->RunsTasksOnCurrentThread());
55
56 bool another_dump_already_in_progress = !queued_memory_dump_requests_.empty(); 87 bool another_dump_already_in_progress = !queued_memory_dump_requests_.empty();
57 88
58 // If this is a periodic memory dump request and there already is another 89 // 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 90 // request in the queue with the same level of detail, there's no point in
60 // enqueuing this request. 91 // enqueuing this request.
61 if (another_dump_already_in_progress && 92 if (another_dump_already_in_progress &&
62 args.dump_type == base::trace_event::MemoryDumpType::PERIODIC_INTERVAL) { 93 args.dump_type == base::trace_event::MemoryDumpType::PERIODIC_INTERVAL) {
63 for (const auto& request : queued_memory_dump_requests_) { 94 for (const auto& request : queued_memory_dump_requests_) {
64 if (request.args.level_of_detail == args.level_of_detail) { 95 if (request.args.level_of_detail == args.level_of_detail) {
65 VLOG(1) << base::trace_event::MemoryDumpManager::kLogPrefix << " (" 96 VLOG(1) << base::trace_event::MemoryDumpManager::kLogPrefix << " ("
(...skipping 14 matching lines...) Expand all
80 // If another dump is already in progress, this dump will automatically be 111 // If another dump is already in progress, this dump will automatically be
81 // scheduled when the other dump finishes. 112 // scheduled when the other dump finishes.
82 if (another_dump_already_in_progress) 113 if (another_dump_already_in_progress)
83 return; 114 return;
84 115
85 PerformNextQueuedGlobalMemoryDump(); 116 PerformNextQueuedGlobalMemoryDump();
86 } 117 }
87 118
88 void CoordinatorImpl::RegisterProcessLocalDumpManager( 119 void CoordinatorImpl::RegisterProcessLocalDumpManager(
89 mojom::ProcessLocalDumpManagerPtr process_manager) { 120 mojom::ProcessLocalDumpManagerPtr process_manager) {
90 DCHECK(thread_checker_.CalledOnValidThread()); 121 DCHECK(task_runner_->RunsTasksOnCurrentThread());
91
92 process_manager.set_connection_error_handler( 122 process_manager.set_connection_error_handler(
93 base::Bind(&CoordinatorImpl::UnregisterProcessLocalDumpManager, 123 base::Bind(&CoordinatorImpl::UnregisterProcessLocalDumpManager,
94 base::Unretained(this), process_manager.get())); 124 base::Unretained(this), process_manager.get()));
95 auto result = process_managers_.insert( 125 auto result = process_managers_.insert(
96 std::make_pair<mojom::ProcessLocalDumpManager*, 126 std::make_pair<mojom::ProcessLocalDumpManager*,
97 mojom::ProcessLocalDumpManagerPtr>( 127 mojom::ProcessLocalDumpManagerPtr>(
98 process_manager.get(), std::move(process_manager))); 128 process_manager.get(), std::move(process_manager)));
99 DCHECK(result.second); 129 DCHECK(result.second);
100 } 130 }
101 131
102 void CoordinatorImpl::UnregisterProcessLocalDumpManager( 132 void CoordinatorImpl::UnregisterProcessLocalDumpManager(
103 mojom::ProcessLocalDumpManager* process_manager) { 133 mojom::ProcessLocalDumpManager* process_manager) {
104 DCHECK(process_managers_.erase(process_manager) == 1); 134 size_t num_deleted = process_managers_.erase(process_manager);
135 DCHECK(num_deleted == 1);
105 136
106 // Check if we are waiting for an ack from this process-local manager. 137 // Check if we are waiting for an ack from this process-local manager.
107 if (pending_process_managers_.find(process_manager) != 138 if (pending_process_managers_.find(process_manager) !=
108 pending_process_managers_.end()) { 139 pending_process_managers_.end()) {
109 DCHECK(!queued_memory_dump_requests_.empty()); 140 DCHECK(!queued_memory_dump_requests_.empty());
110 OnProcessMemoryDumpResponse( 141 OnProcessMemoryDumpResponse(
111 process_manager, queued_memory_dump_requests_.front().args.dump_guid, 142 process_manager, queued_memory_dump_requests_.front().args.dump_guid,
112 false /* success */); 143 false /* success */);
113 } 144 }
114 } 145 }
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
171 // Schedule the next queued dump (if applicable). 202 // Schedule the next queued dump (if applicable).
172 if (!queued_memory_dump_requests_.empty()) { 203 if (!queued_memory_dump_requests_.empty()) {
173 base::ThreadTaskRunnerHandle::Get()->PostTask( 204 base::ThreadTaskRunnerHandle::Get()->PostTask(
174 FROM_HERE, 205 FROM_HERE,
175 base::Bind(&CoordinatorImpl::PerformNextQueuedGlobalMemoryDump, 206 base::Bind(&CoordinatorImpl::PerformNextQueuedGlobalMemoryDump,
176 base::Unretained(this))); 207 base::Unretained(this)));
177 } 208 }
178 } 209 }
179 210
180 } // namespace memory_instrumentation 211 } // namespace memory_instrumentation
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698