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

Side by Side Diff: chrome/browser/chromeos/arc/arc_process_service.cc

Issue 2025593003: Show all system process in the task_manager. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: First step of refactor. Eliminate pointers and migrate to PostTaskAndReplyWithResult(). Need more w… Created 4 years, 5 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 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 // The main point of this class is to cache ARC proc nspid<->pid mapping 5 // The main point of this class is to cache ARC proc nspid<->pid mapping
6 // globally. Since the calculation is costly, a dedicated worker thread is 6 // globally. Since the calculation is costly, a dedicated worker thread is
7 // used. All read/write of its internal data structure (i.e., the mapping) 7 // used. All read/write of its internal data structure (i.e., the mapping)
8 // should be on this thread. 8 // should be on this thread.
9 9
10 #include "chrome/browser/chromeos/arc/arc_process_service.h" 10 #include "chrome/browser/chromeos/arc/arc_process_service.h"
11 11
12 #include <queue> 12 #include <queue>
13 #include <set> 13 #include <set>
14 #include <string> 14 #include <string>
15 15
16 #include "base/callback_forward.h"
16 #include "base/process/process.h" 17 #include "base/process/process.h"
17 #include "base/process/process_iterator.h" 18 #include "base/process/process_iterator.h"
18 #include "base/task_runner_util.h" 19 #include "base/task_runner_util.h"
19 #include "base/trace_event/trace_event.h" 20 #include "base/trace_event/trace_event.h"
20 #include "content/public/browser/browser_thread.h" 21 #include "content/public/browser/browser_thread.h"
21 22
22 namespace arc { 23 namespace arc {
23 24
24 namespace { 25 namespace {
25 26
26 const char kSequenceToken[] = "arc_process_service"; 27 constexpr char kSequenceToken[] = "arc_process_service";
27 28
28 // Weak pointer. This class is owned by ArcServiceManager. 29 // Weak pointer. This class is owned by ArcServiceManager.
29 ArcProcessService* g_arc_process_service = nullptr; 30 ArcProcessService* g_arc_process_service = nullptr;
30 31
31 } // namespace 32 } // namespace
32 33
33 using base::kNullProcessId; 34 using base::kNullProcessId;
34 using base::Process; 35 using base::Process;
35 using base::ProcessId; 36 using base::ProcessId;
36 using base::SequencedWorkerPool; 37 using base::SequencedWorkerPool;
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 FROM_HERE, 71 FROM_HERE,
71 base::Bind(&ArcProcessService::Reset, 72 base::Bind(&ArcProcessService::Reset,
72 weak_ptr_factory_.GetWeakPtr())); 73 weak_ptr_factory_.GetWeakPtr()));
73 } 74 }
74 75
75 void ArcProcessService::Reset() { 76 void ArcProcessService::Reset() {
76 DCHECK(thread_checker_.CalledOnValidThread()); 77 DCHECK(thread_checker_.CalledOnValidThread());
77 nspid_to_pid_.clear(); 78 nspid_to_pid_.clear();
78 } 79 }
79 80
80 bool ArcProcessService::RequestProcessList( 81 void ArcProcessService::RequestSystemProcessList(
81 RequestProcessListCallback callback) { 82 RequestProcessListCallback callback) {
82 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 83 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
83 84
85 auto runner = worker_pool_->GetSequencedTaskRunner(
86 worker_pool_->GetNamedSequenceToken(kSequenceToken));
87 base::PostTaskAndReplyWithResult(
88 runner.get(), FROM_HERE,
89 base::Bind(&ArcProcessService::GetArcSystemProcessList), callback);
90 }
91
92 // static
93 vector<ArcProcess> ArcProcessService::GetArcSystemProcessList() {
94 vector<ArcProcess> ret_processes;
95 const base::ProcessIterator::ProcessEntries& entry_list =
96 base::ProcessIterator(nullptr).Snapshot();
97 ProcessId arc_init_pid = GetArcInitProcessId(entry_list);
98
99 if (arc_init_pid == kNullProcessId) {
100 return ret_processes;
101 }
102
103 // Enumerate the child processes of ARC init for gathering ARC System
104 // Processes.
105 for (const base::ProcessEntry& entry : entry_list) {
106 // TODO(hctsai): For now, we only gather direct child process of init, need
107 // to get the processes below. For example, intalled might
Luis Héctor Chávez 2016/07/06 20:28:12 nit: installd
108 // fork dex2oat and it can be executed for minutes.
109 if (entry.parent_pid() == arc_init_pid) {
110 const ProcessId child_pid = entry.pid();
111 const ProcessId child_nspid =
112 base::Process(child_pid).GetPidInNamespace();
113 const std::string process_name =
114 !entry.cmd_line_args().empty() ? entry.cmd_line_args()[0] : "";
115 if (child_nspid != kNullProcessId) {
116 ret_processes.emplace_back(child_nspid, child_pid, process_name,
117 mojom::ProcessState::PERSISTENT);
118 }
119 }
120 }
121
122 return ret_processes;
123 }
124
125 bool ArcProcessService::RequestAppProcessList(
126 RequestProcessListCallback callback) {
127 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
128
84 arc::mojom::ProcessInstance* process_instance = 129 arc::mojom::ProcessInstance* process_instance =
85 arc_bridge_service()->process_instance(); 130 arc_bridge_service()->process_instance();
86 if (!process_instance) { 131 if (!process_instance) {
87 return false; 132 return false;
88 } 133 }
89 process_instance->RequestProcessList( 134 process_instance->RequestProcessList(
90 base::Bind(&ArcProcessService::OnReceiveProcessList, 135 base::Bind(&ArcProcessService::OnReceiveProcessList,
91 weak_ptr_factory_.GetWeakPtr(), 136 weak_ptr_factory_.GetWeakPtr(),
92 callback)); 137 callback));
93 return true; 138 return true;
94 } 139 }
95 140
141 vector<ArcProcess> ArcProcessService::ConvertMojoProcess(
142 mojo::Array<arc::mojom::RunningAppProcessInfoPtr>& mojo_processes) {
143 vector<ArcProcess> ret_processes;
144
145 for (const auto& entry : mojo_processes) {
146 ArcProcess arc_process(entry->pid, nspid_to_pid_[entry->pid],
147 entry->process_name, entry->process_state);
148 // |entry->packages| is provided only when process.mojom's verion is >=4.
149 if (entry->packages) {
150 for (const auto& package : entry->packages) {
151 arc_process.packages().push_back(package.get());
152 }
153 }
154 ret_processes.push_back(std::move(arc_process));
155 }
156
157 return ret_processes;
158 }
159
96 void ArcProcessService::OnReceiveProcessList( 160 void ArcProcessService::OnReceiveProcessList(
97 const RequestProcessListCallback& callback, 161 const RequestProcessListCallback& callback,
98 mojo::Array<arc::mojom::RunningAppProcessInfoPtr> mojo_processes) { 162 mojo::Array<arc::mojom::RunningAppProcessInfoPtr> mojo_processes) {
Luis Héctor Chávez 2016/07/06 20:28:12 nit: if possible, rename all the |mojo_processes|
99 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 163 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
100 164
101 auto raw_processes = new vector<mojom::RunningAppProcessInfoPtr>(); 165 vector<ArcProcess> processes = ConvertMojoProcess(mojo_processes);
102 mojo_processes.Swap(raw_processes);
103
104 auto ret_processes = new vector<ArcProcess>();
105 // Post to its dedicated worker thread to avoid race condition.
106 // Since no two tasks with the same token should be run at the same.
107 // Note: GetSequencedTaskRunner's shutdown behavior defaults to
108 // SKIP_ON_SHUTDOWN (ongoing task blocks shutdown).
109 // So in theory using Unretained(this) should be fine since the life cycle
110 // of |this| is the same as the main browser.
111 // To be safe I still use weak pointers, but weak_ptrs can only bind to
112 // methods without return values. That's why I can't use
113 // PostTaskAndReplyWithResult but handle the return object by myself.
114 auto runner = worker_pool_->GetSequencedTaskRunner( 166 auto runner = worker_pool_->GetSequencedTaskRunner(
115 worker_pool_->GetNamedSequenceToken(kSequenceToken)); 167 worker_pool_->GetNamedSequenceToken(kSequenceToken));
116 runner->PostTaskAndReply( 168 base::PostTaskAndReplyWithResult(
117 FROM_HERE, 169 runner.get(), FROM_HERE,
118 base::Bind(&ArcProcessService::UpdateAndReturnProcessList, 170 base::Bind(&ArcProcessService::UpdateAndReturnProcessList, this,
119 weak_ptr_factory_.GetWeakPtr(), 171 base::Passed(&processes)),
120 base::Owned(raw_processes), 172 callback);
121 base::Unretained(ret_processes)),
122 base::Bind(&ArcProcessService::CallbackRelay,
123 weak_ptr_factory_.GetWeakPtr(),
124 callback,
125 base::Owned(ret_processes)));
126 } 173 }
127 174
128 void ArcProcessService::CallbackRelay( 175 vector<ArcProcess> ArcProcessService::UpdateAndReturnProcessList(
129 const RequestProcessListCallback& callback, 176 const vector<ArcProcess>& processes) {
130 const vector<ArcProcess>* ret_processes) {
131 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
132 callback.Run(*ret_processes);
133 }
134
135 void ArcProcessService::UpdateAndReturnProcessList(
136 const vector<arc::mojom::RunningAppProcessInfoPtr>* raw_processes,
137 vector<ArcProcess>* ret_processes) {
138 DCHECK(thread_checker_.CalledOnValidThread());
139
140 // Cleanup dead pids in the cache |nspid_to_pid_|. 177 // Cleanup dead pids in the cache |nspid_to_pid_|.
141 set<ProcessId> nspid_to_remove; 178 set<ProcessId> nspid_to_remove;
142 for (const auto& entry : nspid_to_pid_) { 179 for (const auto& entry : nspid_to_pid_) {
143 nspid_to_remove.insert(entry.first); 180 nspid_to_remove.insert(entry.first);
144 } 181 }
145 bool unmapped_nspid = false; 182 bool unmapped_nspid = false;
146 for (const auto& entry : *raw_processes) { 183 for (const auto& entry : processes) {
147 // erase() returns 0 if coudln't find the key. It means a new process. 184 // erase() returns 0 if coudln't find the key. It means a new process.
148 if (nspid_to_remove.erase(entry->pid) == 0) { 185 if (nspid_to_remove.erase(entry.pid()) == 0) {
149 nspid_to_pid_[entry->pid] = kNullProcessId; 186 nspid_to_pid_[entry.pid()] = kNullProcessId;
150 unmapped_nspid = true; 187 unmapped_nspid = true;
151 } 188 }
152 } 189 }
153 for (const auto& entry : nspid_to_remove) { 190 for (const auto& entry : nspid_to_remove) {
154 nspid_to_pid_.erase(entry); 191 nspid_to_pid_.erase(entry);
155 } 192 }
156 193
157 // The operation is costly so avoid calling it when possible. 194 // The operation is costly so avoid calling it when possible.
158 if (unmapped_nspid) { 195 if (unmapped_nspid) {
159 UpdateNspidToPidMap(); 196 UpdateNspidToPidMap();
160 } 197 }
161 198
162 PopulateProcessList(raw_processes, ret_processes); 199 return PopulateProcessList(processes);
163 } 200 }
164 201
165 void ArcProcessService::PopulateProcessList( 202 vector<ArcProcess> ArcProcessService::PopulateProcessList(
166 const vector<arc::mojom::RunningAppProcessInfoPtr>* raw_processes, 203 const vector<ArcProcess>& processes) {
167 vector<ArcProcess>* ret_processes) {
168 DCHECK(thread_checker_.CalledOnValidThread()); 204 DCHECK(thread_checker_.CalledOnValidThread());
205 vector<ArcProcess> ret_processes;
169 206
170 for (const auto& entry : *raw_processes) { 207 for (const auto& entry : processes) {
171 const auto it = nspid_to_pid_.find(entry->pid); 208 const auto it = nspid_to_pid_.find(entry.pid());
172 // In case the process already dies so couldn't find corresponding pid. 209 // In case the process already dies so couldn't find corresponding pid.
173 if (it != nspid_to_pid_.end() && it->second != kNullProcessId) { 210 if (it != nspid_to_pid_.end() && it->second != kNullProcessId) {
174 ArcProcess arc_process(entry->pid, it->second, entry->process_name, 211 ArcProcess arc_process(entry.nspid(), entry.pid(), entry.process_name(),
175 entry->process_state); 212 entry.process_state());
176 // |entry->packages| is provided only when process.mojom's verion is >=4. 213 ret_processes.push_back(std::move(arc_process));
177 if (entry->packages) {
178 for (const auto& package : entry->packages) {
179 arc_process.packages().push_back(package.get());
180 }
181 }
182 ret_processes->push_back(std::move(arc_process));
183 } 214 }
184 } 215 }
216 return ret_processes;
185 } 217 }
186 218
187 // Computes a map from PID in ARC namespace to PID in system namespace. 219 // Computes a map from PID in ARC namespace to PID in system namespace.
188 // The returned map contains ARC processes only. 220 // The returned map contains ARC processes only.
189 void ArcProcessService::UpdateNspidToPidMap() { 221 void ArcProcessService::UpdateNspidToPidMap() {
190 DCHECK(thread_checker_.CalledOnValidThread()); 222 DCHECK(thread_checker_.CalledOnValidThread());
191 223
192 TRACE_EVENT0("browser", "ArcProcessService::UpdateNspidToPidMap"); 224 TRACE_EVENT0("browser", "ArcProcessService::UpdateNspidToPidMap");
193 225
194 // NB: Despite of its name, ProcessIterator::Snapshot() may return 226 // NB: Despite of its name, ProcessIterator::Snapshot() may return
195 // inconsistent information because it simply walks procfs. Especially 227 // inconsistent information because it simply walks procfs. Especially
196 // we must not assume the parent-child relationships are consistent. 228 // we must not assume the parent-child relationships are consistent.
197 const base::ProcessIterator::ProcessEntries& entry_list = 229 const base::ProcessIterator::ProcessEntries& entry_list =
198 base::ProcessIterator(nullptr).Snapshot(); 230 base::ProcessIterator(nullptr).Snapshot();
199 231
200 // System may contain many different namespaces so several different 232 // System may contain many different namespaces so several different
201 // processes may have the same nspid. We need to get the proper subset of 233 // processes may have the same nspid. We need to get the proper subset of
202 // processes to create correct nspid -> pid map. 234 // processes to create correct nspid -> pid map.
203 235
204 // Construct the process tree. 236 // Construct the process tree.
205 // NB: This can contain a loop in case of race conditions. 237 // NB: This can contain a loop in case of race conditions.
206 map<ProcessId, vector<ProcessId> > process_tree; 238 map<ProcessId, vector<ProcessId> > process_tree;
207 for (const base::ProcessEntry& entry : entry_list) 239 for (const base::ProcessEntry& entry : entry_list)
208 process_tree[entry.parent_pid()].push_back(entry.pid()); 240 process_tree[entry.parent_pid()].push_back(entry.pid());
209 241
210 // Find the ARC init process. 242 // Find the ARC init process.
211 ProcessId arc_init_pid = kNullProcessId; 243 ProcessId arc_init_pid = GetArcInitProcessId(entry_list);
212 for (const base::ProcessEntry& entry : entry_list) {
213 // TODO(nya): Add more constraints to avoid mismatches.
214 std::string process_name =
215 !entry.cmd_line_args().empty() ? entry.cmd_line_args()[0] : "";
216 if (process_name == "/init") {
217 arc_init_pid = entry.pid();
218 break;
219 }
220 }
221 244
222 // Enumerate all processes under ARC init and create nspid -> pid map. 245 // Enumerate all processes under ARC init and create nspid -> pid map.
223 if (arc_init_pid != kNullProcessId) { 246 if (arc_init_pid != kNullProcessId) {
224 std::queue<ProcessId> queue; 247 std::queue<ProcessId> queue;
225 std::set<ProcessId> visited; 248 std::set<ProcessId> visited;
226 queue.push(arc_init_pid); 249 queue.push(arc_init_pid);
227 while (!queue.empty()) { 250 while (!queue.empty()) {
228 ProcessId pid = queue.front(); 251 ProcessId pid = queue.front();
229 queue.pop(); 252 queue.pop();
230 // Do not visit the same process twice. Otherwise we may enter an infinite 253 // Do not visit the same process twice. Otherwise we may enter an infinite
(...skipping 10 matching lines...) Expand all
241 if (nspid != kNullProcessId && 264 if (nspid != kNullProcessId &&
242 nspid_to_pid_.find(nspid) != nspid_to_pid_.end()) 265 nspid_to_pid_.find(nspid) != nspid_to_pid_.end())
243 nspid_to_pid_[nspid] = pid; 266 nspid_to_pid_[nspid] = pid;
244 267
245 for (ProcessId child_pid : process_tree[pid]) 268 for (ProcessId child_pid : process_tree[pid])
246 queue.push(child_pid); 269 queue.push(child_pid);
247 } 270 }
248 } 271 }
249 } 272 }
250 273
274 // static
275 ProcessId ArcProcessService::GetArcInitProcessId(
276 const base::ProcessIterator::ProcessEntries& entry_list) {
277 // Find the ARC init process.
278 for (const base::ProcessEntry& entry : entry_list) {
279 // TODO(nya): Add more constraints to avoid mismatches.
280 std::string process_name =
281 !entry.cmd_line_args().empty() ? entry.cmd_line_args()[0] : "";
282 if (process_name == "/init") {
283 return entry.pid();
284 }
285 }
286 return kNullProcessId;
287 }
288
251 } // namespace arc 289 } // namespace arc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698