OLD | NEW |
---|---|
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" | |
Yusuke Sato
2016/06/30 19:46:52
including _forward.h in .cc seems a bit weird, and
| |
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 Loading... | |
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>(); | |
Luis Héctor Chávez
2016/06/30 16:46:03
Raw pointers scare me a bit.
This can potentially
Yusuke Sato
2016/06/30 19:46:52
+1, although you probably have to make ArcProcessS
cylee1
2016/06/30 21:02:39
When I wrote the code I tried to use PostTaskAndRe
Yusuke Sato
2016/06/30 22:23:53
hmm... Luis, any idea?
Luis Héctor Chávez
2016/06/30 23:21:51
CallbackRelay doesn't need to be a member function
Yusuke Sato
2016/06/30 23:40:50
I think I misspoke. My understanding is that PostT
cylee1
2016/07/01 23:15:14
Yes.. I remember I introduced CallbackRelay becaus
cylee1
2016/07/01 23:15:14
Sorry I don't see where it forcing us to use RefCo
| |
94 PostTaskToOwnThreadAndReply( | |
95 base::Bind(&ArcProcessService::GetArcSystemProcessList, | |
96 weak_ptr_factory_.GetWeakPtr(), | |
97 base::Unretained(ret_processes)), | |
Yusuke Sato
2016/06/30 19:46:52
remove
| |
98 base::Bind(&ArcProcessService::CallbackRelay, | |
99 weak_ptr_factory_.GetWeakPtr(), callback, | |
100 base::Owned(ret_processes))); | |
Yusuke Sato
2016/06/30 19:46:52
remove
| |
101 } | |
102 | |
103 void ArcProcessService::GetArcSystemProcessList( | |
104 vector<ArcProcess>* ret_processes) { | |
Yusuke Sato
2016/06/30 19:46:51
remove once you switch to PostTaskAndReplyWithResu
| |
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 // Processes. | |
117 for (const base::ProcessEntry& entry : entry_list) { | |
118 // TODO(hctsai): For now, we only gather direct child process of init, need | |
119 // to get the processes below. | |
Yusuke Sato
2016/06/30 19:46:52
Can you mention the intalled/dex2oat issue as an e
| |
120 if (entry.parent_pid() == arc_init_pid) { | |
121 const ProcessId child_pid = entry.pid(); | |
122 const ProcessId child_nspid = | |
123 base::Process(child_pid).GetPidInNamespace(); | |
124 const std::string process_name = | |
125 !entry.cmd_line_args().empty() ? entry.cmd_line_args()[0] : ""; | |
Yusuke Sato
2016/06/30 19:46:52
nit: can you factor this out as a function in anon
| |
126 if (child_nspid != kNullProcessId) { | |
127 ret_processes->emplace_back(child_nspid, child_pid, process_name, | |
128 mojom::ProcessState::PERSISTENT); | |
129 } | |
130 } | |
131 } | |
132 } | |
133 | |
134 bool ArcProcessService::RequestAppProcessList( | |
135 RequestProcessListCallback callback) { | |
136 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
137 | |
84 arc::mojom::ProcessInstance* process_instance = | 138 arc::mojom::ProcessInstance* process_instance = |
85 arc_bridge_service()->process_instance(); | 139 arc_bridge_service()->process_instance(); |
86 if (!process_instance) { | 140 if (!process_instance) { |
87 return false; | 141 return false; |
88 } | 142 } |
89 process_instance->RequestProcessList( | 143 process_instance->RequestProcessList( |
90 base::Bind(&ArcProcessService::OnReceiveProcessList, | 144 base::Bind(&ArcProcessService::OnReceiveProcessList, |
91 weak_ptr_factory_.GetWeakPtr(), | 145 weak_ptr_factory_.GetWeakPtr(), |
92 callback)); | 146 callback)); |
93 return true; | 147 return true; |
94 } | 148 } |
95 | 149 |
96 void ArcProcessService::OnReceiveProcessList( | 150 void ArcProcessService::OnReceiveProcessList( |
97 const RequestProcessListCallback& callback, | 151 const RequestProcessListCallback& callback, |
98 mojo::Array<arc::mojom::RunningAppProcessInfoPtr> mojo_processes) { | 152 mojo::Array<arc::mojom::RunningAppProcessInfoPtr> mojo_processes) { |
99 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 153 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
100 | 154 |
101 auto raw_processes = new vector<mojom::RunningAppProcessInfoPtr>(); | 155 auto raw_processes = new vector<mojom::RunningAppProcessInfoPtr>(); |
Yusuke Sato
2016/06/30 19:46:51
Any reason to convert it to vector at this point?
cylee1
2016/06/30 21:02:39
I don't remember the exact reason, but I remember
| |
102 mojo_processes.Swap(raw_processes); | 156 mojo_processes.Swap(raw_processes); |
103 | 157 |
104 auto ret_processes = new vector<ArcProcess>(); | 158 auto ret_processes = new vector<ArcProcess>(); |
Yusuke Sato
2016/06/30 19:46:51
This has the same, potential memory leak issue as
| |
105 // Post to its dedicated worker thread to avoid race condition. | 159 // 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. | 160 // Since no two tasks with the same token should be run at the same. |
107 // Note: GetSequencedTaskRunner's shutdown behavior defaults to | 161 // Note: GetSequencedTaskRunner's shutdown behavior defaults to |
108 // SKIP_ON_SHUTDOWN (ongoing task blocks shutdown). | 162 // SKIP_ON_SHUTDOWN (ongoing task blocks shutdown). |
109 // So in theory using Unretained(this) should be fine since the life cycle | 163 // So in theory using Unretained(this) should be fine since the life cycle |
110 // of |this| is the same as the main browser. | 164 // 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 | 165 // 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 | 166 // methods without return values. That's why I can't use |
113 // PostTaskAndReplyWithResult but handle the return object by myself. | 167 // PostTaskAndReplyWithResult but handle the return object by myself. |
114 auto runner = worker_pool_->GetSequencedTaskRunner( | 168 PostTaskToOwnThreadAndReply( |
115 worker_pool_->GetNamedSequenceToken(kSequenceToken)); | |
116 runner->PostTaskAndReply( | |
117 FROM_HERE, | |
118 base::Bind(&ArcProcessService::UpdateAndReturnProcessList, | 169 base::Bind(&ArcProcessService::UpdateAndReturnProcessList, |
119 weak_ptr_factory_.GetWeakPtr(), | 170 weak_ptr_factory_.GetWeakPtr(), base::Owned(raw_processes), |
Yusuke Sato
2016/06/30 19:46:51
base::Passed(&mojo_processes) ?
Luis Héctor Chávez
2016/06/30 19:50:37
If you have to add a prefix to variables, I prefer
| |
120 base::Owned(raw_processes), | |
121 base::Unretained(ret_processes)), | 171 base::Unretained(ret_processes)), |
Yusuke Sato
2016/06/30 19:46:52
remove
| |
122 base::Bind(&ArcProcessService::CallbackRelay, | 172 base::Bind(&ArcProcessService::CallbackRelay, |
123 weak_ptr_factory_.GetWeakPtr(), | 173 weak_ptr_factory_.GetWeakPtr(), callback, |
124 callback, | |
125 base::Owned(ret_processes))); | 174 base::Owned(ret_processes))); |
Yusuke Sato
2016/06/30 19:46:52
remove
| |
126 } | 175 } |
127 | 176 |
128 void ArcProcessService::CallbackRelay( | 177 void ArcProcessService::CallbackRelay( |
129 const RequestProcessListCallback& callback, | 178 const RequestProcessListCallback& callback, |
130 const vector<ArcProcess>* ret_processes) { | 179 const vector<ArcProcess>* ret_processes) { |
Yusuke Sato
2016/06/30 19:46:51
unique_ptr instead of a raw pointer
cylee1
2016/06/30 21:02:39
If use PostTaskAndReplyWithResult, maybe the vecto
| |
131 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 180 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
132 callback.Run(*ret_processes); | 181 callback.Run(*ret_processes); |
133 } | 182 } |
134 | 183 |
135 void ArcProcessService::UpdateAndReturnProcessList( | 184 void ArcProcessService::UpdateAndReturnProcessList( |
136 const vector<arc::mojom::RunningAppProcessInfoPtr>* raw_processes, | 185 const vector<arc::mojom::RunningAppProcessInfoPtr>* raw_processes, |
Yusuke Sato
2016/06/30 19:46:51
Line 152 receives a smiliar type of object as valu
| |
137 vector<ArcProcess>* ret_processes) { | 186 vector<ArcProcess>* ret_processes) { |
Yusuke Sato
2016/06/30 19:46:52
remove once you switch to ...WithResult().
| |
138 DCHECK(thread_checker_.CalledOnValidThread()); | 187 DCHECK(thread_checker_.CalledOnValidThread()); |
139 | 188 |
140 // Cleanup dead pids in the cache |nspid_to_pid_|. | 189 // Cleanup dead pids in the cache |nspid_to_pid_|. |
141 set<ProcessId> nspid_to_remove; | 190 set<ProcessId> nspid_to_remove; |
142 for (const auto& entry : nspid_to_pid_) { | 191 for (const auto& entry : nspid_to_pid_) { |
143 nspid_to_remove.insert(entry.first); | 192 nspid_to_remove.insert(entry.first); |
144 } | 193 } |
145 bool unmapped_nspid = false; | 194 bool unmapped_nspid = false; |
146 for (const auto& entry : *raw_processes) { | 195 for (const auto& entry : *raw_processes) { |
147 // erase() returns 0 if coudln't find the key. It means a new process. | 196 // erase() returns 0 if coudln't find the key. It means a new process. |
148 if (nspid_to_remove.erase(entry->pid) == 0) { | 197 if (nspid_to_remove.erase(entry->pid) == 0) { |
149 nspid_to_pid_[entry->pid] = kNullProcessId; | 198 nspid_to_pid_[entry->pid] = kNullProcessId; |
150 unmapped_nspid = true; | 199 unmapped_nspid = true; |
151 } | 200 } |
152 } | 201 } |
153 for (const auto& entry : nspid_to_remove) { | 202 for (const auto& entry : nspid_to_remove) { |
154 nspid_to_pid_.erase(entry); | 203 nspid_to_pid_.erase(entry); |
155 } | 204 } |
156 | 205 |
157 // The operation is costly so avoid calling it when possible. | 206 // The operation is costly so avoid calling it when possible. |
158 if (unmapped_nspid) { | 207 if (unmapped_nspid) { |
159 UpdateNspidToPidMap(); | 208 UpdateNspidToPidMap(); |
160 } | 209 } |
161 | 210 |
162 PopulateProcessList(raw_processes, ret_processes); | 211 PopulateProcessList(raw_processes, ret_processes); |
163 } | 212 } |
164 | 213 |
165 void ArcProcessService::PopulateProcessList( | 214 void ArcProcessService::PopulateProcessList( |
166 const vector<arc::mojom::RunningAppProcessInfoPtr>* raw_processes, | 215 const vector<arc::mojom::RunningAppProcessInfoPtr>* raw_processes, |
Yusuke Sato
2016/06/30 19:46:51
mojo::Array<arc::mojom::RunningAppProcessInfoPtr>
| |
167 vector<ArcProcess>* ret_processes) { | 216 vector<ArcProcess>* ret_processes) { |
Yusuke Sato
2016/06/30 19:46:51
Can you use unique_ptr instead of a raw pointer?
| |
168 DCHECK(thread_checker_.CalledOnValidThread()); | 217 DCHECK(thread_checker_.CalledOnValidThread()); |
169 | 218 |
170 for (const auto& entry : *raw_processes) { | 219 for (const auto& entry : *raw_processes) { |
171 const auto it = nspid_to_pid_.find(entry->pid); | 220 const auto it = nspid_to_pid_.find(entry->pid); |
172 // In case the process already dies so couldn't find corresponding pid. | 221 // In case the process already dies so couldn't find corresponding pid. |
cylee1
2016/07/15 23:17:02
Could you keep this comments or re-write it ?
I me
Hsu-Cheng
2016/07/27 08:07:59
Done.
| |
173 if (it != nspid_to_pid_.end() && it->second != kNullProcessId) { | 222 if (it != nspid_to_pid_.end() && it->second != kNullProcessId) { |
174 ArcProcess arc_process(entry->pid, it->second, entry->process_name, | 223 ArcProcess arc_process(entry->pid, it->second, entry->process_name, |
175 entry->process_state); | 224 entry->process_state); |
176 // |entry->packages| is provided only when process.mojom's verion is >=4. | 225 // |entry->packages| is provided only when process.mojom's verion is >=4. |
177 if (entry->packages) { | 226 if (entry->packages) { |
178 for (const auto& package : entry->packages) { | 227 for (const auto& package : entry->packages) { |
179 arc_process.packages().push_back(package.get()); | 228 arc_process.packages().push_back(package.get()); |
180 } | 229 } |
181 } | 230 } |
182 ret_processes->push_back(std::move(arc_process)); | 231 ret_processes->push_back(std::move(arc_process)); |
(...skipping 18 matching lines...) Expand all Loading... | |
201 // processes may have the same nspid. We need to get the proper subset of | 250 // processes may have the same nspid. We need to get the proper subset of |
202 // processes to create correct nspid -> pid map. | 251 // processes to create correct nspid -> pid map. |
203 | 252 |
204 // Construct the process tree. | 253 // Construct the process tree. |
205 // NB: This can contain a loop in case of race conditions. | 254 // NB: This can contain a loop in case of race conditions. |
206 map<ProcessId, vector<ProcessId> > process_tree; | 255 map<ProcessId, vector<ProcessId> > process_tree; |
207 for (const base::ProcessEntry& entry : entry_list) | 256 for (const base::ProcessEntry& entry : entry_list) |
208 process_tree[entry.parent_pid()].push_back(entry.pid()); | 257 process_tree[entry.parent_pid()].push_back(entry.pid()); |
209 | 258 |
210 // Find the ARC init process. | 259 // Find the ARC init process. |
211 ProcessId arc_init_pid = kNullProcessId; | 260 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 | 261 |
222 // Enumerate all processes under ARC init and create nspid -> pid map. | 262 // Enumerate all processes under ARC init and create nspid -> pid map. |
223 if (arc_init_pid != kNullProcessId) { | 263 if (arc_init_pid != kNullProcessId) { |
224 std::queue<ProcessId> queue; | 264 std::queue<ProcessId> queue; |
225 std::set<ProcessId> visited; | 265 std::set<ProcessId> visited; |
Yusuke Sato
2016/06/30 19:46:51
nit: (not your fault but..) remove std:: to be con
| |
226 queue.push(arc_init_pid); | 266 queue.push(arc_init_pid); |
227 while (!queue.empty()) { | 267 while (!queue.empty()) { |
228 ProcessId pid = queue.front(); | 268 ProcessId pid = queue.front(); |
229 queue.pop(); | 269 queue.pop(); |
230 // Do not visit the same process twice. Otherwise we may enter an infinite | 270 // Do not visit the same process twice. Otherwise we may enter an infinite |
231 // loop if |process_tree| contains a loop. | 271 // loop if |process_tree| contains a loop. |
232 if (!visited.insert(pid).second) | 272 if (!visited.insert(pid).second) |
233 continue; | 273 continue; |
234 | 274 |
235 ProcessId nspid = base::Process(pid).GetPidInNamespace(); | 275 ProcessId nspid = base::Process(pid).GetPidInNamespace(); |
236 | 276 |
237 // All ARC processes should be in namespace so nspid is usually non-null, | 277 // All ARC processes should be in namespace so nspid is usually non-null, |
238 // but this can happen if the process has already gone. | 278 // but this can happen if the process has already gone. |
239 // Only add processes we're interested in (those appear as keys in | 279 // Only add processes we're interested in (those appear as keys in |
240 // |nspid_to_pid_|). | 280 // |nspid_to_pid_|). |
241 if (nspid != kNullProcessId && | 281 if (nspid != kNullProcessId && |
242 nspid_to_pid_.find(nspid) != nspid_to_pid_.end()) | 282 nspid_to_pid_.find(nspid) != nspid_to_pid_.end()) |
243 nspid_to_pid_[nspid] = pid; | 283 nspid_to_pid_[nspid] = pid; |
244 | 284 |
245 for (ProcessId child_pid : process_tree[pid]) | 285 for (ProcessId child_pid : process_tree[pid]) |
246 queue.push(child_pid); | 286 queue.push(child_pid); |
247 } | 287 } |
248 } | 288 } |
249 } | 289 } |
250 | 290 |
291 // static | |
292 ProcessId ArcProcessService::GetArcInitProcessId( | |
293 const base::ProcessIterator::ProcessEntries& entry_list) { | |
294 // Find the ARC init process. | |
295 for (const base::ProcessEntry& entry : entry_list) { | |
296 // TODO(nya): Add more constraints to avoid mismatches. | |
297 std::string process_name = | |
298 !entry.cmd_line_args().empty() ? entry.cmd_line_args()[0] : ""; | |
299 if (process_name == "/init") { | |
300 return entry.pid(); | |
301 } | |
302 } | |
303 return kNullProcessId; | |
304 } | |
305 | |
251 } // namespace arc | 306 } // namespace arc |
OLD | NEW |