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

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: Define system process. Refine code according to review suggestion. 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::PostTaskToOwnThreadAndReply(
82 const base::Closure& task,
83 const base::Closure& reply) {
84 auto runner = worker_pool_->GetSequencedTaskRunner(
85 worker_pool_->GetNamedSequenceToken(kSequenceToken));
86 runner->PostTaskAndReply(FROM_HERE, task, reply);
87 }
88
89 void ArcProcessService::RequestSystemProcessList(
81 RequestProcessListCallback callback) { 90 RequestProcessListCallback callback) {
82 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 91 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
83 92
93 auto ret_processes = new vector<ArcProcess>();
94 PostTaskToOwnThreadAndReply(
95 base::Bind(&ArcProcessService::GetArcSystemProcessList,
96 weak_ptr_factory_.GetWeakPtr(),
97 base::Unretained(ret_processes)),
98 base::Bind(&ArcProcessService::CallbackRelay,
99 weak_ptr_factory_.GetWeakPtr(), callback,
100 base::Owned(ret_processes)));
101 }
102
103 void ArcProcessService::GetArcSystemProcessList(
104 vector<ArcProcess>* ret_processes) {
105 DCHECK(thread_checker_.CalledOnValidThread());
106
107 const base::ProcessIterator::ProcessEntries& entry_list =
108 base::ProcessIterator(nullptr).Snapshot();
109 ProcessId arc_init_pid = GetArcInitProcessId(entry_list);
110
111 if (arc_init_pid == kNullProcessId) {
112 return;
113 }
114
115 // Enumerate the child processes of ARC init for gathering ARC System
116 // Procceses
cylee1 2016/06/30 03:45:52 nit: spelling : Processes..
Hsu-Cheng 2016/06/30 09:56:24 Done.
117 for (const base::ProcessEntry& entry : entry_list) {
118 if (entry.parent_pid() == arc_init_pid) {
119 ProcessId child_pid = entry.pid();
120 ProcessId child_nspid = base::Process(child_pid).GetPidInNamespace();
121 std::string process_name =
cylee1 2016/06/30 03:45:52 nit: can be const
Hsu-Cheng 2016/06/30 09:56:24 Done.
122 !entry.cmd_line_args().empty() ? entry.cmd_line_args()[0] : "";
123 if (child_nspid != kNullProcessId) {
124 ArcProcess arc_process(child_nspid, child_pid, process_name,
125 mojom::ProcessState::PERSISTENT);
126 ret_processes->push_back(std::move(arc_process));
cylee1 2016/06/30 03:45:52 Just learnt that you can probably just use the C++
Hsu-Cheng 2016/06/30 09:56:24 magic indeed ... Done
127 }
128 }
129 }
130 }
131
132 bool ArcProcessService::RequestAppProcessList(
133 RequestProcessListCallback callback) {
134 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
135
84 arc::mojom::ProcessInstance* process_instance = 136 arc::mojom::ProcessInstance* process_instance =
85 arc_bridge_service()->process_instance(); 137 arc_bridge_service()->process_instance();
86 if (!process_instance) { 138 if (!process_instance) {
87 return false; 139 return false;
88 } 140 }
89 process_instance->RequestProcessList( 141 process_instance->RequestProcessList(
90 base::Bind(&ArcProcessService::OnReceiveProcessList, 142 base::Bind(&ArcProcessService::OnReceiveProcessList,
91 weak_ptr_factory_.GetWeakPtr(), 143 weak_ptr_factory_.GetWeakPtr(),
92 callback)); 144 callback));
93 return true; 145 return true;
(...skipping 10 matching lines...) Expand all
104 auto ret_processes = new vector<ArcProcess>(); 156 auto ret_processes = new vector<ArcProcess>();
105 // Post to its dedicated worker thread to avoid race condition. 157 // 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. 158 // Since no two tasks with the same token should be run at the same.
107 // Note: GetSequencedTaskRunner's shutdown behavior defaults to 159 // Note: GetSequencedTaskRunner's shutdown behavior defaults to
108 // SKIP_ON_SHUTDOWN (ongoing task blocks shutdown). 160 // SKIP_ON_SHUTDOWN (ongoing task blocks shutdown).
109 // So in theory using Unretained(this) should be fine since the life cycle 161 // So in theory using Unretained(this) should be fine since the life cycle
110 // of |this| is the same as the main browser. 162 // 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 163 // 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 164 // methods without return values. That's why I can't use
113 // PostTaskAndReplyWithResult but handle the return object by myself. 165 // PostTaskAndReplyWithResult but handle the return object by myself.
114 auto runner = worker_pool_->GetSequencedTaskRunner( 166 PostTaskToOwnThreadAndReply(
115 worker_pool_->GetNamedSequenceToken(kSequenceToken));
116 runner->PostTaskAndReply(
117 FROM_HERE,
118 base::Bind(&ArcProcessService::UpdateAndReturnProcessList, 167 base::Bind(&ArcProcessService::UpdateAndReturnProcessList,
119 weak_ptr_factory_.GetWeakPtr(), 168 weak_ptr_factory_.GetWeakPtr(), base::Owned(raw_processes),
120 base::Owned(raw_processes),
121 base::Unretained(ret_processes)), 169 base::Unretained(ret_processes)),
122 base::Bind(&ArcProcessService::CallbackRelay, 170 base::Bind(&ArcProcessService::CallbackRelay,
123 weak_ptr_factory_.GetWeakPtr(), 171 weak_ptr_factory_.GetWeakPtr(), callback,
124 callback,
125 base::Owned(ret_processes))); 172 base::Owned(ret_processes)));
126 } 173 }
127 174
128 void ArcProcessService::CallbackRelay( 175 void ArcProcessService::CallbackRelay(
129 const RequestProcessListCallback& callback, 176 const RequestProcessListCallback& callback,
130 const vector<ArcProcess>* ret_processes) { 177 const vector<ArcProcess>* ret_processes) {
131 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 178 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
132 callback.Run(*ret_processes); 179 callback.Run(*ret_processes);
133 } 180 }
134 181
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
201 // processes may have the same nspid. We need to get the proper subset of 248 // processes may have the same nspid. We need to get the proper subset of
202 // processes to create correct nspid -> pid map. 249 // processes to create correct nspid -> pid map.
203 250
204 // Construct the process tree. 251 // Construct the process tree.
205 // NB: This can contain a loop in case of race conditions. 252 // NB: This can contain a loop in case of race conditions.
206 map<ProcessId, vector<ProcessId> > process_tree; 253 map<ProcessId, vector<ProcessId> > process_tree;
207 for (const base::ProcessEntry& entry : entry_list) 254 for (const base::ProcessEntry& entry : entry_list)
208 process_tree[entry.parent_pid()].push_back(entry.pid()); 255 process_tree[entry.parent_pid()].push_back(entry.pid());
209 256
210 // Find the ARC init process. 257 // Find the ARC init process.
211 ProcessId arc_init_pid = kNullProcessId; 258 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 259
222 // Enumerate all processes under ARC init and create nspid -> pid map. 260 // Enumerate all processes under ARC init and create nspid -> pid map.
223 if (arc_init_pid != kNullProcessId) { 261 if (arc_init_pid != kNullProcessId) {
224 std::queue<ProcessId> queue; 262 std::queue<ProcessId> queue;
225 std::set<ProcessId> visited; 263 std::set<ProcessId> visited;
226 queue.push(arc_init_pid); 264 queue.push(arc_init_pid);
227 while (!queue.empty()) { 265 while (!queue.empty()) {
228 ProcessId pid = queue.front(); 266 ProcessId pid = queue.front();
229 queue.pop(); 267 queue.pop();
230 // Do not visit the same process twice. Otherwise we may enter an infinite 268 // Do not visit the same process twice. Otherwise we may enter an infinite
(...skipping 10 matching lines...) Expand all
241 if (nspid != kNullProcessId && 279 if (nspid != kNullProcessId &&
242 nspid_to_pid_.find(nspid) != nspid_to_pid_.end()) 280 nspid_to_pid_.find(nspid) != nspid_to_pid_.end())
243 nspid_to_pid_[nspid] = pid; 281 nspid_to_pid_[nspid] = pid;
244 282
245 for (ProcessId child_pid : process_tree[pid]) 283 for (ProcessId child_pid : process_tree[pid])
246 queue.push(child_pid); 284 queue.push(child_pid);
247 } 285 }
248 } 286 }
249 } 287 }
250 288
289 // static
290 ProcessId ArcProcessService::GetArcInitProcessId(
291 const base::ProcessIterator::ProcessEntries& entry_list) {
292 // Find the ARC init process.
293 for (const base::ProcessEntry& entry : entry_list) {
294 // TODO(nya): Add more constraints to avoid mismatches.
295 std::string process_name =
296 !entry.cmd_line_args().empty() ? entry.cmd_line_args()[0] : "";
297 if (process_name == "/init") {
298 return entry.pid();
299 }
300 }
301 return kNullProcessId;
302 }
303
251 } // namespace arc 304 } // namespace arc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698