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/process_local_dump_man
ager_impl.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/single_thread_task_runner.h" | 9 #include "base/single_thread_task_runner.h" |
10 #include "base/synchronization/lock.h" | 10 #include "base/synchronization/lock.h" |
11 #include "base/trace_event/memory_dump_request_args.h" | 11 #include "base/trace_event/memory_dump_request_args.h" |
12 #include "mojo/public/cpp/bindings/interface_request.h" | 12 #include "mojo/public/cpp/bindings/interface_request.h" |
13 #include "services/resource_coordinator/public/cpp/memory/coordinator.h" | 13 #include "services/resource_coordinator/public/cpp/memory/coordinator.h" |
14 #include "services/resource_coordinator/public/interfaces/memory/memory_instrume
ntation.mojom.h" | 14 #include "services/resource_coordinator/public/interfaces/memory/memory_instrume
ntation.mojom.h" |
15 #include "services/service_manager/public/cpp/connector.h" | 15 #include "services/service_manager/public/cpp/connector.h" |
16 | 16 |
17 namespace memory_instrumentation { | 17 namespace memory_instrumentation { |
18 | 18 |
19 MemoryDumpManagerDelegateImpl::Config::~Config() {} | 19 ProcessLocalDumpManagerImpl::Config::~Config() {} |
20 | 20 |
21 MemoryDumpManagerDelegateImpl::MemoryDumpManagerDelegateImpl( | 21 // static |
22 const MemoryDumpManagerDelegateImpl::Config& config) | 22 void ProcessLocalDumpManagerImpl::CreateInstance(const Config& config) { |
| 23 static ProcessLocalDumpManagerImpl* instance = nullptr; |
| 24 if (!instance) { |
| 25 instance = new ProcessLocalDumpManagerImpl(config); |
| 26 } else { |
| 27 NOTREACHED(); |
| 28 } |
| 29 } |
| 30 |
| 31 ProcessLocalDumpManagerImpl::ProcessLocalDumpManagerImpl(const Config& config) |
23 : binding_(this), | 32 : binding_(this), |
24 config_(config), | 33 config_(config), |
25 task_runner_(nullptr), | 34 task_runner_(nullptr), |
26 pending_memory_dump_guid_(0) { | 35 pending_memory_dump_guid_(0) { |
27 if (config.connector() != nullptr) { | 36 if (config.connector() != nullptr) { |
28 config.connector()->BindInterface(config.service_name(), | 37 config.connector()->BindInterface(config.service_name(), |
29 mojo::MakeRequest(&coordinator_)); | 38 mojo::MakeRequest(&coordinator_)); |
30 } else { | 39 } else { |
31 task_runner_ = base::ThreadTaskRunnerHandle::Get(); | 40 task_runner_ = base::ThreadTaskRunnerHandle::Get(); |
32 config.coordinator()->BindCoordinatorRequest( | 41 config.coordinator()->BindCoordinatorRequest( |
33 mojo::MakeRequest(&coordinator_)); | 42 mojo::MakeRequest(&coordinator_)); |
34 } | 43 } |
35 coordinator_->RegisterProcessLocalDumpManager( | 44 coordinator_->RegisterProcessLocalDumpManager( |
36 binding_.CreateInterfacePtrAndBind()); | 45 binding_.CreateInterfacePtrAndBind()); |
| 46 |
| 47 // Only one process should handle periodic dumping. |
| 48 bool is_coordinator_process = !!config.coordinator(); |
| 49 base::trace_event::MemoryDumpManager::GetInstance()->Initialize( |
| 50 base::BindRepeating(&ProcessLocalDumpManagerImpl::RequestGlobalMemoryDump, |
| 51 base::Unretained(this)), |
| 52 is_coordinator_process); |
37 } | 53 } |
38 | 54 |
39 MemoryDumpManagerDelegateImpl::~MemoryDumpManagerDelegateImpl() {} | 55 ProcessLocalDumpManagerImpl::~ProcessLocalDumpManagerImpl() {} |
40 | 56 |
41 bool MemoryDumpManagerDelegateImpl::IsCoordinator() const { | 57 void ProcessLocalDumpManagerImpl::RequestProcessMemoryDump( |
42 return task_runner_ != nullptr; | 58 const base::trace_event::MemoryDumpRequestArgs& args, |
| 59 const RequestProcessMemoryDumpCallback& callback) { |
| 60 base::trace_event::MemoryDumpManager::GetInstance()->CreateProcessDump( |
| 61 args, callback); |
43 } | 62 } |
44 | 63 |
45 void MemoryDumpManagerDelegateImpl::RequestProcessMemoryDump( | 64 void ProcessLocalDumpManagerImpl::RequestGlobalMemoryDump( |
46 const base::trace_event::MemoryDumpRequestArgs& args, | |
47 const RequestProcessMemoryDumpCallback& callback) { | |
48 MemoryDumpManagerDelegate::CreateProcessDump(args, callback); | |
49 } | |
50 | |
51 void MemoryDumpManagerDelegateImpl::RequestGlobalMemoryDump( | |
52 const base::trace_event::MemoryDumpRequestArgs& args, | 65 const base::trace_event::MemoryDumpRequestArgs& args, |
53 const base::trace_event::GlobalMemoryDumpCallback& callback) { | 66 const base::trace_event::GlobalMemoryDumpCallback& callback) { |
54 // Note: This condition is here to match the old behavior. If the delegate is | 67 // Note: This condition is here to match the old behavior. If the delegate is |
55 // in the browser process, we do not drop parallel requests in the delegate | 68 // in the browser process, we do not drop parallel requests in the delegate |
56 // and so they will be queued by the Coordinator service (see | 69 // and so they will be queued by the Coordinator service (see |
57 // CoordinatorImpl::RequestGlobalMemoryDump). If the delegate is in a child | 70 // CoordinatorImpl::RequestGlobalMemoryDump). If the delegate is in a child |
58 // process, parallel requests will be cancelled. | 71 // process, parallel requests will be cancelled. |
59 // | 72 // |
60 // TODO(chiniforooshan): Unify the child and browser behavior. | 73 // TODO(chiniforooshan): Unify the child and browser behavior. |
61 if (IsCoordinator()) { | 74 if (task_runner_) { |
62 task_runner_->PostTask( | 75 task_runner_->PostTask( |
63 FROM_HERE, | 76 FROM_HERE, |
64 base::Bind(&mojom::Coordinator::RequestGlobalMemoryDump, | 77 base::Bind(&mojom::Coordinator::RequestGlobalMemoryDump, |
65 base::Unretained(coordinator_.get()), args, callback)); | 78 base::Unretained(coordinator_.get()), args, callback)); |
66 return; | 79 return; |
67 } | 80 } |
68 | 81 |
69 { | 82 { |
70 base::AutoLock lock(pending_memory_dump_guid_lock_); | 83 base::AutoLock lock(pending_memory_dump_guid_lock_); |
71 if (pending_memory_dump_guid_) { | 84 if (pending_memory_dump_guid_) { |
72 callback.Run(args.dump_guid, false); | 85 callback.Run(args.dump_guid, false); |
73 return; | 86 return; |
74 } | 87 } |
75 pending_memory_dump_guid_ = args.dump_guid; | 88 pending_memory_dump_guid_ = args.dump_guid; |
76 } | 89 } |
77 auto callback_proxy = | 90 auto callback_proxy = |
78 base::Bind(&MemoryDumpManagerDelegateImpl::MemoryDumpCallbackProxy, | 91 base::Bind(&ProcessLocalDumpManagerImpl::MemoryDumpCallbackProxy, |
79 base::Unretained(this), callback); | 92 base::Unretained(this), callback); |
80 coordinator_->RequestGlobalMemoryDump(args, callback_proxy); | 93 coordinator_->RequestGlobalMemoryDump(args, callback_proxy); |
81 } | 94 } |
82 | 95 |
83 void MemoryDumpManagerDelegateImpl::MemoryDumpCallbackProxy( | 96 void ProcessLocalDumpManagerImpl::MemoryDumpCallbackProxy( |
84 const base::trace_event::GlobalMemoryDumpCallback& callback, | 97 const base::trace_event::GlobalMemoryDumpCallback& callback, |
85 uint64_t dump_guid, | 98 uint64_t dump_guid, |
86 bool success) { | 99 bool success) { |
87 DCHECK_NE(0U, pending_memory_dump_guid_); | 100 { |
88 pending_memory_dump_guid_ = 0; | 101 base::AutoLock lock(pending_memory_dump_guid_lock_); |
| 102 DCHECK_NE(0U, pending_memory_dump_guid_); |
| 103 pending_memory_dump_guid_ = 0; |
| 104 } |
89 callback.Run(dump_guid, success); | 105 callback.Run(dump_guid, success); |
90 } | 106 } |
91 | 107 |
92 void MemoryDumpManagerDelegateImpl::SetAsNonCoordinatorForTesting() { | 108 void ProcessLocalDumpManagerImpl::SetAsNonCoordinatorForTesting() { |
93 task_runner_ = nullptr; | 109 task_runner_ = nullptr; |
94 } | 110 } |
95 | 111 |
96 } // namespace memory_instrumentation | 112 } // namespace memory_instrumentation |
OLD | NEW |