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

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: Move two functions to anonymous namespace. 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 <algorithm>
12 #include <queue> 13 #include <queue>
13 #include <set> 14 #include <set>
14 #include <string> 15 #include <string>
15 16
17 #include "base/callback.h"
16 #include "base/process/process.h" 18 #include "base/process/process.h"
17 #include "base/process/process_iterator.h" 19 #include "base/process/process_iterator.h"
18 #include "base/task_runner_util.h" 20 #include "base/task_runner_util.h"
19 #include "base/trace_event/trace_event.h" 21 #include "base/trace_event/trace_event.h"
20 #include "content/public/browser/browser_thread.h" 22 #include "content/public/browser/browser_thread.h"
21 23
22 namespace arc { 24 namespace arc {
23 25
24 namespace { 26 namespace {
25 27
26 const char kSequenceToken[] = "arc_process_service";
27
28 // Weak pointer. This class is owned by ArcServiceManager. 28 // Weak pointer. This class is owned by ArcServiceManager.
29 ArcProcessService* g_arc_process_service = nullptr; 29 ArcProcessService* g_arc_process_service = nullptr;
30 30
31 // Matches the process name "/init" in the process tree and get the
32 // corresponding process ID.
33 base::ProcessId GetArcInitProcessId(
34 const base::ProcessIterator::ProcessEntries& entry_list) {
35 for (const base::ProcessEntry& entry : entry_list) {
36 // TODO(nya): Add more constraints to avoid mismatches.
37 std::string process_name =
Luis Héctor Chávez 2016/07/15 16:19:41 I know you didn't write this code, but can you imp
Hsu-Cheng 2016/07/27 08:07:59 Done.
38 !entry.cmd_line_args().empty() ? entry.cmd_line_args()[0] : "";
39 if (process_name == "/init") {
40 return entry.pid();
41 }
42 }
43 return base::kNullProcessId;
44 }
45
46 std::vector<arc::ArcProcess> GetArcSystemProcessList() {
47 std::vector<arc::ArcProcess> ret_processes;
48 const base::ProcessIterator::ProcessEntries& entry_list =
49 base::ProcessIterator(nullptr).Snapshot();
50 base::ProcessId arc_init_pid = GetArcInitProcessId(entry_list);
cylee1 2016/07/15 23:17:03 nit: can be const
Hsu-Cheng 2016/07/27 08:07:59 Done.
51
52 if (arc_init_pid == base::kNullProcessId) {
53 return ret_processes;
54 }
55
56 // Enumerate the child processes of ARC init for gathering ARC System
57 // Processes.
58 for (const base::ProcessEntry& entry : entry_list) {
59 // TODO(hctsai): For now, we only gather direct child process of init, need
60 // to get the processes below. For example, installd might
61 // fork dex2oat and it can be executed for minutes.
62 if (entry.parent_pid() == arc_init_pid) {
63 const base::ProcessId child_pid = entry.pid();
64 const base::ProcessId child_nspid =
65 base::Process(child_pid).GetPidInNamespace();
66 const std::string process_name =
cylee1 2016/07/15 23:17:03 nit: can move inside the if
Hsu-Cheng 2016/07/27 08:07:59 Done.
67 !entry.cmd_line_args().empty() ? entry.cmd_line_args()[0] : "";
68 if (child_nspid != base::kNullProcessId) {
69 ret_processes.emplace_back(child_nspid, child_pid, process_name,
70 mojom::ProcessState::PERSISTENT);
71 }
72 }
73 }
74
75 return ret_processes;
76 }
77
31 } // namespace 78 } // namespace
32 79
33 using base::kNullProcessId; 80 using base::kNullProcessId;
34 using base::Process; 81 using base::Process;
35 using base::ProcessId; 82 using base::ProcessId;
36 using base::SequencedWorkerPool; 83 using base::SequencedWorkerPool;
37 using std::map; 84 using std::map;
38 using std::set; 85 using std::set;
39 using std::vector; 86 using std::vector;
40 87
41 ArcProcessService::ArcProcessService(ArcBridgeService* bridge_service) 88 ArcProcessService::ArcProcessService(ArcBridgeService* bridge_service)
42 : ArcService(bridge_service), 89 : ArcService(bridge_service),
43 worker_pool_(new SequencedWorkerPool(1, "arc_process_manager")),
44 weak_ptr_factory_(this) { 90 weak_ptr_factory_(this) {
45 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 91 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
46 arc_bridge_service()->process()->AddObserver(this); 92 arc_bridge_service()->process()->AddObserver(this);
47 DCHECK(!g_arc_process_service); 93 DCHECK(!g_arc_process_service);
48 g_arc_process_service = this; 94 g_arc_process_service = this;
49 // Not intended to be used from the creating thread. 95 // Not intended to be used from the creating thread.
Luis Héctor Chávez 2016/07/15 16:19:42 Remove?
Hsu-Cheng 2016/07/27 08:07:59 Done.
50 thread_checker_.DetachFromThread();
51 } 96 }
52 97
53 ArcProcessService::~ArcProcessService() { 98 ArcProcessService::~ArcProcessService() {
54 DCHECK(g_arc_process_service == this); 99 DCHECK(g_arc_process_service == this);
55 g_arc_process_service = nullptr; 100 g_arc_process_service = nullptr;
56 arc_bridge_service()->process()->RemoveObserver(this); 101 arc_bridge_service()->process()->RemoveObserver(this);
57 worker_pool_->Shutdown();
58 } 102 }
59 103
60 // static 104 // static
61 ArcProcessService* ArcProcessService::Get() { 105 ArcProcessService* ArcProcessService::Get() {
62 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 106 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
63 return g_arc_process_service; 107 return g_arc_process_service;
64 } 108 }
65 109
66 void ArcProcessService::OnInstanceReady() { 110 void ArcProcessService::OnInstanceReady() {
67 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 111 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
68 worker_pool_->PostNamedSequencedWorkerTask( 112 heavy_task_runner_->PostTask(
cylee1 2016/07/15 23:17:03 Are you sure SingleThreadTaskRunner work as intend
Hsu-Cheng 2016/07/27 08:07:59 Done.
69 kSequenceToken,
70 FROM_HERE, 113 FROM_HERE,
71 base::Bind(&ArcProcessService::Reset, 114 base::Bind(&ArcProcessService::Reset, weak_ptr_factory_.GetWeakPtr()));
Luis Héctor Chávez 2016/07/15 16:19:41 This is still problematic, since WeakPtrs are not
cylee1 2016/07/15 23:17:03 To be more clear, you should not use (dereference)
Hsu-Cheng 2016/07/27 08:07:59 I did move on this way. The NSPidToPidMap class is
72 weak_ptr_factory_.GetWeakPtr()));
73 } 115 }
74 116
75 void ArcProcessService::Reset() { 117 void ArcProcessService::Reset() {
76 DCHECK(thread_checker_.CalledOnValidThread()); 118 nspid_to_pid_->clear();
cylee1 2016/07/15 23:17:03 Shouldn't we only modify the object in the dedicat
Hsu-Cheng 2016/07/27 08:07:59 Done.
77 nspid_to_pid_.clear();
78 } 119 }
79 120
80 bool ArcProcessService::RequestProcessList( 121 void ArcProcessService::RequestSystemProcessList(
81 RequestProcessListCallback callback) { 122 RequestProcessListCallback callback) {
82 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 123 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
83 124
125 base::PostTaskAndReplyWithResult(heavy_task_runner_.get(), FROM_HERE,
126 base::Bind(&GetArcSystemProcessList),
127 callback);
128 }
129
130 bool ArcProcessService::RequestAppProcessList(
131 RequestProcessListCallback callback) {
132 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
133
84 arc::mojom::ProcessInstance* process_instance = 134 arc::mojom::ProcessInstance* process_instance =
85 arc_bridge_service()->process()->instance(); 135 arc_bridge_service()->process()->instance();
86 if (!process_instance) { 136 if (!process_instance) {
87 return false; 137 return false;
88 } 138 }
89 process_instance->RequestProcessList( 139 process_instance->RequestProcessList(
90 base::Bind(&ArcProcessService::OnReceiveProcessList, 140 base::Bind(&ArcProcessService::OnReceiveProcessList,
91 weak_ptr_factory_.GetWeakPtr(), 141 weak_ptr_factory_.GetWeakPtr(),
92 callback)); 142 callback));
93 return true; 143 return true;
94 } 144 }
95 145
146 // static
96 void ArcProcessService::OnReceiveProcessList( 147 void ArcProcessService::OnReceiveProcessList(
97 const RequestProcessListCallback& callback, 148 const RequestProcessListCallback& callback,
98 mojo::Array<arc::mojom::RunningAppProcessInfoPtr> mojo_processes) { 149 mojo::Array<arc::mojom::RunningAppProcessInfoPtr> instance_processes) {
99 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 150 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
100 151
101 auto raw_processes = new vector<mojom::RunningAppProcessInfoPtr>(); 152 base::PostTaskAndReplyWithResult(
102 mojo_processes.Swap(raw_processes); 153 heavy_task_runner_.get(), FROM_HERE,
103 154 base::Bind(&ArcProcessService::UpdateAndReturnProcessList, nspid_to_pid_,
104 auto ret_processes = new vector<ArcProcess>(); 155 base::Passed(&instance_processes)),
105 // Post to its dedicated worker thread to avoid race condition. 156 callback);
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(
115 worker_pool_->GetNamedSequenceToken(kSequenceToken));
116 runner->PostTaskAndReply(
117 FROM_HERE,
118 base::Bind(&ArcProcessService::UpdateAndReturnProcessList,
119 weak_ptr_factory_.GetWeakPtr(),
120 base::Owned(raw_processes),
121 base::Unretained(ret_processes)),
122 base::Bind(&ArcProcessService::CallbackRelay,
123 weak_ptr_factory_.GetWeakPtr(),
124 callback,
125 base::Owned(ret_processes)));
126 } 157 }
127 158
128 void ArcProcessService::CallbackRelay( 159 // static
129 const RequestProcessListCallback& callback, 160 vector<ArcProcess> ArcProcessService::UpdateAndReturnProcessList(
130 const vector<ArcProcess>* ret_processes) { 161 scoped_refptr<NSPidToPidMap> pid_map,
131 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 162 mojo::Array<arc::mojom::RunningAppProcessInfoPtr> processes) {
132 callback.Run(*ret_processes); 163 // Cleanup dead pids in the cache |pid_map|.
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_|.
141 set<ProcessId> nspid_to_remove; 164 set<ProcessId> nspid_to_remove;
142 for (const auto& entry : nspid_to_pid_) { 165 for (const auto& entry : *pid_map) {
cylee1 2016/07/15 23:17:03 I think you can create a reference to the derefere
Hsu-Cheng 2016/07/27 08:07:59 Done.
143 nspid_to_remove.insert(entry.first); 166 nspid_to_remove.insert(entry.first);
144 } 167 }
145 bool unmapped_nspid = false; 168 bool unmapped_nspid = false;
146 for (const auto& entry : *raw_processes) { 169 for (const auto& entry : processes) {
147 // erase() returns 0 if coudln't find the key. It means a new process. 170 // erase() returns 0 if coudln't find the key. It means a new process.
148 if (nspid_to_remove.erase(entry->pid) == 0) { 171 if (nspid_to_remove.erase(entry->pid) == 0) {
149 nspid_to_pid_[entry->pid] = kNullProcessId; 172 (*pid_map)[entry->pid] = base::kNullProcessId;
150 unmapped_nspid = true; 173 unmapped_nspid = true;
151 } 174 }
152 } 175 }
153 for (const auto& entry : nspid_to_remove) { 176 for (const auto& entry : nspid_to_remove) {
154 nspid_to_pid_.erase(entry); 177 (*pid_map).erase(entry);
155 } 178 }
156 179
157 // The operation is costly so avoid calling it when possible. 180 // The operation is costly so avoid calling it when possible.
158 if (unmapped_nspid) { 181 if (unmapped_nspid) {
159 UpdateNspidToPidMap(); 182 UpdateNspidToPidMap(*pid_map);
160 } 183 }
161 184
162 PopulateProcessList(raw_processes, ret_processes); 185 return FilterProcessList(*pid_map, std::move(processes));
163 } 186 }
164 187
165 void ArcProcessService::PopulateProcessList( 188 // static
166 const vector<arc::mojom::RunningAppProcessInfoPtr>* raw_processes, 189 vector<ArcProcess> ArcProcessService::FilterProcessList(
167 vector<ArcProcess>* ret_processes) { 190 NSPidToPidMap& pid_map,
cylee1 2016/07/15 23:17:03 const ?
Hsu-Cheng 2016/07/27 08:07:59 Done.
168 DCHECK(thread_checker_.CalledOnValidThread()); 191 mojo::Array<arc::mojom::RunningAppProcessInfoPtr> processes) {
169 192 vector<ArcProcess> ret_processes;
170 for (const auto& entry : *raw_processes) { 193 for (const auto& entry : processes) {
171 const auto it = nspid_to_pid_.find(entry->pid); 194 const auto it = pid_map.find(entry->pid);
172 // In case the process already dies so couldn't find corresponding pid. 195 if (it != pid_map.end() && it->second != base::kNullProcessId) {
173 if (it != nspid_to_pid_.end() && it->second != kNullProcessId) { 196 ArcProcess arc_process(entry->pid, pid_map[entry->pid],
174 ArcProcess arc_process(entry->pid, it->second, entry->process_name, 197 entry->process_name, entry->process_state);
175 entry->process_state);
176 // |entry->packages| is provided only when process.mojom's verion is >=4. 198 // |entry->packages| is provided only when process.mojom's verion is >=4.
177 if (entry->packages) { 199 if (entry->packages) {
178 for (const auto& package : entry->packages) { 200 for (const auto& package : entry->packages) {
179 arc_process.packages().push_back(package.get()); 201 arc_process.packages().push_back(package.get());
180 } 202 }
181 } 203 }
182 ret_processes->push_back(std::move(arc_process)); 204 ret_processes.push_back(std::move(arc_process));
183 } 205 }
184 } 206 }
207 return ret_processes;
185 } 208 }
186 209
187 // Computes a map from PID in ARC namespace to PID in system namespace. 210 // static
188 // The returned map contains ARC processes only. 211 void ArcProcessService::UpdateNspidToPidMap(NSPidToPidMap& pid_map) {
189 void ArcProcessService::UpdateNspidToPidMap() {
190 DCHECK(thread_checker_.CalledOnValidThread());
191
192 TRACE_EVENT0("browser", "ArcProcessService::UpdateNspidToPidMap"); 212 TRACE_EVENT0("browser", "ArcProcessService::UpdateNspidToPidMap");
193 213
194 // NB: Despite of its name, ProcessIterator::Snapshot() may return 214 // NB: Despite of its name, ProcessIterator::Snapshot() may return
195 // inconsistent information because it simply walks procfs. Especially 215 // inconsistent information because it simply walks procfs. Especially
196 // we must not assume the parent-child relationships are consistent. 216 // we must not assume the parent-child relationships are consistent.
197 const base::ProcessIterator::ProcessEntries& entry_list = 217 const base::ProcessIterator::ProcessEntries& entry_list =
198 base::ProcessIterator(nullptr).Snapshot(); 218 base::ProcessIterator(nullptr).Snapshot();
199 219
200 // System may contain many different namespaces so several different
201 // processes may have the same nspid. We need to get the proper subset of
202 // processes to create correct nspid -> pid map.
203
204 // Construct the process tree. 220 // Construct the process tree.
205 // NB: This can contain a loop in case of race conditions. 221 // NB: This can contain a loop in case of race conditions.
206 map<ProcessId, vector<ProcessId> > process_tree; 222 map<ProcessId, vector<ProcessId> > process_tree;
207 for (const base::ProcessEntry& entry : entry_list) 223 for (const base::ProcessEntry& entry : entry_list)
208 process_tree[entry.parent_pid()].push_back(entry.pid()); 224 process_tree[entry.parent_pid()].push_back(entry.pid());
209 225
210 // Find the ARC init process. 226 ProcessId arc_init_pid = GetArcInitProcessId(entry_list);
211 ProcessId arc_init_pid = kNullProcessId;
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 227
222 // Enumerate all processes under ARC init and create nspid -> pid map. 228 // Enumerate all processes under ARC init and create nspid -> pid map.
223 if (arc_init_pid != kNullProcessId) { 229 if (arc_init_pid != kNullProcessId) {
224 std::queue<ProcessId> queue; 230 std::queue<ProcessId> queue;
225 std::set<ProcessId> visited; 231 std::set<ProcessId> visited;
226 queue.push(arc_init_pid); 232 queue.push(arc_init_pid);
227 while (!queue.empty()) { 233 while (!queue.empty()) {
228 ProcessId pid = queue.front(); 234 ProcessId pid = queue.front();
229 queue.pop(); 235 queue.pop();
230 // Do not visit the same process twice. Otherwise we may enter an infinite 236 // Do not visit the same process twice. Otherwise we may enter an infinite
231 // loop if |process_tree| contains a loop. 237 // loop if |process_tree| contains a loop.
232 if (!visited.insert(pid).second) 238 if (!visited.insert(pid).second)
233 continue; 239 continue;
234 240
235 ProcessId nspid = base::Process(pid).GetPidInNamespace(); 241 ProcessId nspid = base::Process(pid).GetPidInNamespace();
236 242
237 // All ARC processes should be in namespace so nspid is usually non-null, 243 // All ARC processes should be in namespace so nspid is usually non-null,
238 // but this can happen if the process has already gone. 244 // but this can happen if the process has already gone.
239 // Only add processes we're interested in (those appear as keys in 245 // Only add processes we're interested in (those appear as keys in
240 // |nspid_to_pid_|). 246 // |pid_map|).
241 if (nspid != kNullProcessId && 247 if (nspid != kNullProcessId && pid_map.find(nspid) != pid_map.end())
242 nspid_to_pid_.find(nspid) != nspid_to_pid_.end()) 248 pid_map[nspid] = pid;
243 nspid_to_pid_[nspid] = pid;
244 249
245 for (ProcessId child_pid : process_tree[pid]) 250 for (ProcessId child_pid : process_tree[pid])
246 queue.push(child_pid); 251 queue.push(child_pid);
247 } 252 }
248 } 253 }
249 } 254 }
250 255
251 } // namespace arc 256 } // namespace arc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698