OLD | NEW |
---|---|
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "chrome/browser/process_info_snapshot.h" | 5 #include "chrome/browser/process_info_snapshot.h" |
6 | 6 |
7 #include <sys/sysctl.h> | |
8 | |
7 #include <sstream> | 9 #include <sstream> |
8 | 10 |
9 #include "base/command_line.h" | 11 #include "base/command_line.h" |
10 #include "base/logging.h" | 12 #include "base/logging.h" |
11 #include "base/string_number_conversions.h" | 13 #include "base/string_number_conversions.h" |
12 #include "base/string_util.h" | 14 #include "base/string_util.h" |
15 #include "base/sys_info.h" | |
13 #include "base/threading/thread.h" | 16 #include "base/threading/thread.h" |
14 | 17 |
15 // Implementation for the Mac; calls '/bin/ps' for information when | |
16 // |Sample()| is called. | |
17 | |
18 // Default constructor. | 18 // Default constructor. |
19 ProcessInfoSnapshot::ProcessInfoSnapshot() { } | 19 ProcessInfoSnapshot::ProcessInfoSnapshot() { } |
20 | 20 |
21 // Destructor: just call |Reset()| to release everything. | 21 // Destructor: just call |Reset()| to release everything. |
22 ProcessInfoSnapshot::~ProcessInfoSnapshot() { | 22 ProcessInfoSnapshot::~ProcessInfoSnapshot() { |
23 Reset(); | 23 Reset(); |
24 } | 24 } |
25 | 25 |
26 const size_t ProcessInfoSnapshot::kMaxPidListSize = 1000; | 26 const size_t ProcessInfoSnapshot::kMaxPidListSize = 1000; |
27 | 27 |
28 static bool GetKInfoForProcessID(pid_t pid, kinfo_proc* kinfo) { | |
29 int mib[] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, pid}; | |
30 size_t len = sizeof(*kinfo); | |
31 if (sysctl(mib, arraysize(mib), kinfo, &len, NULL, 0) != 0) { | |
32 PLOG(ERROR) << "sysctl() for KERN_PROC"; | |
33 return false; | |
34 } | |
35 return true; | |
36 } | |
37 | |
38 static bool GetExecutableNameForProcessID( | |
39 pid_t pid, | |
40 std::string* executable_name) { | |
41 if (!executable_name) { | |
42 NOTREACHED(); | |
43 return false; | |
44 } | |
45 | |
46 static int s_arg_max = 0; | |
47 if (s_arg_max == 0) { | |
48 int mib[] = {CTL_KERN, KERN_ARGMAX}; | |
49 size_t size = sizeof(s_arg_max); | |
50 if (sysctl(mib, arraysize(mib), &s_arg_max, &size, NULL, 0) != 0) | |
51 PLOG(ERROR) << "sysctl() for KERN_ARGMAX"; | |
52 } | |
53 | |
54 if (s_arg_max == 0) | |
55 return false; | |
56 | |
57 int mib[] = {CTL_KERN, KERN_PROCARGS, pid}; | |
58 size_t size = s_arg_max; | |
59 executable_name->resize(s_arg_max + 1); | |
60 if (sysctl(mib, arraysize(mib), &(*executable_name)[0], | |
61 &size, NULL, 0) != 0) { | |
62 // Don't log the error since it's normal for this to fail. | |
63 return false; | |
64 } | |
65 | |
66 // KERN_PROCARGS returns multiple NULL terminated strings. Truncate | |
67 // executable_name to just the first string. | |
68 size_t end_pos = executable_name->find('\0'); | |
69 if (end_pos != std::string::npos) | |
70 executable_name->resize(end_pos + 1); | |
Mark Mentovai
2011/01/06 18:56:06
Don’t you want end_pos - 1?
You should also check
sail
2011/01/06 19:20:17
Hm... I guess I don't understand how std::string w
sail
2011/01/06 19:48:50
Done.
| |
71 | |
72 return true; | |
73 } | |
74 | |
75 // Converts a byte unit such as 'K' or 'M' into the scale for the unit. | |
76 // The scale can then be used to calculate the number of bytes in a value. | |
77 // The units are based on humanize_number(). See: | |
78 // http://www.opensource.apple.com/source/libutil/libutil-21/humanize_number.c | |
79 static bool ConvertByteUnitToScale(char unit, uint64_t* out_scale) { | |
80 int shift = 0; | |
81 switch (unit) { | |
82 case 'B': | |
83 shift = 0; | |
84 break; | |
85 case 'K': | |
86 case 'k': | |
87 shift = 1; | |
88 break; | |
89 case 'M': | |
90 shift = 2; | |
91 break; | |
92 case 'G': | |
93 shift = 3; | |
94 break; | |
95 case 'T': | |
96 shift = 4; | |
97 break; | |
98 case 'P': | |
99 shift = 5; | |
100 break; | |
101 case 'E': | |
102 shift = 6; | |
103 break; | |
104 default: | |
105 return false; | |
106 } | |
107 | |
108 uint64_t scale = 1; | |
109 for (int i = 0; i < shift; i++) | |
110 scale *= 1024; | |
111 *out_scale = scale; | |
112 | |
113 return true; | |
114 } | |
115 | |
28 // Capture the information by calling '/bin/ps'. | 116 // Capture the information by calling '/bin/ps'. |
29 // Note: we ignore the "tsiz" (text size) display option of ps because it's | 117 // Note: we ignore the "tsiz" (text size) display option of ps because it's |
30 // always zero (tested on 10.5 and 10.6). | 118 // always zero (tested on 10.5 and 10.6). |
31 bool ProcessInfoSnapshot::Sample(std::vector<base::ProcessId> pid_list) { | 119 static bool GetProcessMemoryInfoUsingPS( |
32 const char* kPsPathName = "/bin/ps"; | 120 const std::vector<base::ProcessId>& pid_list, |
33 Reset(); | 121 std::map<int,ProcessInfoSnapshot::ProcInfoEntry>& proc_info_entries) { |
34 | 122 const char kPsPathName[] = "/bin/ps"; |
35 // Nothing to do if no PIDs given. | |
36 if (pid_list.size() == 0) | |
37 return true; | |
38 if (pid_list.size() > kMaxPidListSize) { | |
39 // The spec says |pid_list| *must* not have more than this many entries. | |
40 NOTREACHED(); | |
41 return false; | |
42 } | |
43 | |
44 std::vector<std::string> argv; | 123 std::vector<std::string> argv; |
45 argv.push_back(kPsPathName); | 124 argv.push_back(kPsPathName); |
46 // Get PID, PPID, (real) UID, effective UID, resident set size, virtual memory | 125 |
47 // size, and command. | 126 // Get resident set size, virtual memory size. |
48 argv.push_back("-o"); | 127 argv.push_back("-o"); |
49 argv.push_back("pid=,ppid=,ruid=,uid=,rss=,vsz=,comm="); | 128 argv.push_back("pid=,rss=,vsz="); |
50 // Only display the specified PIDs. | 129 // Only display the specified PIDs. |
51 for (std::vector<base::ProcessId>::iterator it = pid_list.begin(); | 130 for (std::vector<base::ProcessId>::const_iterator it = pid_list.begin(); |
52 it != pid_list.end(); ++it) { | 131 it != pid_list.end(); ++it) { |
53 argv.push_back("-p"); | 132 argv.push_back("-p"); |
54 argv.push_back(base::Int64ToString(static_cast<int64>(*it))); | 133 argv.push_back(base::Int64ToString(static_cast<int64>(*it))); |
55 } | 134 } |
56 | 135 |
57 std::string output; | 136 std::string output; |
58 CommandLine command_line(argv); | 137 CommandLine command_line(argv); |
59 // Limit output read to a megabyte for safety. | 138 // Limit output read to a megabyte for safety. |
60 if (!base::GetAppOutputRestricted(command_line, &output, 1024 * 1024)) { | 139 if (!base::GetAppOutputRestricted(command_line, &output, 1024 * 1024)) { |
61 LOG(ERROR) << "Failure running " << kPsPathName << " to acquire data."; | 140 LOG(ERROR) << "Failure running " << kPsPathName << " to acquire data."; |
62 return false; | 141 return false; |
63 } | 142 } |
64 | 143 |
65 std::istringstream in(output, std::istringstream::in); | 144 std::istringstream in(output, std::istringstream::in); |
66 std::string line; | 145 std::string line; |
67 | 146 |
68 // Process lines until done. | 147 // Process lines until done. |
69 while (true) { | 148 while (true) { |
70 ProcInfoEntry proc_info; | |
71 | |
72 // The format is as specified above to ps (see ps(1)): | 149 // The format is as specified above to ps (see ps(1)): |
73 // "-o pid=,ppid=,ruid=,uid=,rss=,vsz=,comm=". | 150 // "-o pid=,rss=,vsz=". |
74 // Try to read the PID; if we get it, we should be able to get the rest of | 151 // Try to read the PID; if we get it, we should be able to get the rest of |
75 // the line. | 152 // the line. |
76 in >> proc_info.pid; | 153 pid_t pid; |
154 in >> pid; | |
77 if (in.eof()) | 155 if (in.eof()) |
78 break; | 156 break; |
79 in >> proc_info.ppid; | 157 |
80 in >> proc_info.uid; | 158 ProcessInfoSnapshot::ProcInfoEntry proc_info = proc_info_entries[pid]; |
81 in >> proc_info.euid; | 159 proc_info.pid = pid; |
82 in >> proc_info.rss; | 160 in >> proc_info.rss; |
83 in >> proc_info.vsize; | 161 in >> proc_info.vsize; |
162 proc_info.rss *= 1024; // Convert from kilobytes to bytes. | |
163 proc_info.vsize *= 1024; | |
84 in.ignore(1, ' '); // Eat the space. | 164 in.ignore(1, ' '); // Eat the space. |
85 std::getline(in, proc_info.command); // Get the rest of the line. | 165 std::getline(in, proc_info.command); // Get the rest of the line. |
86 if (!in.good()) { | 166 if (!in.good()) { |
87 LOG(ERROR) << "Error parsing output from " << kPsPathName << "."; | 167 LOG(ERROR) << "Error parsing output from " << kPsPathName << "."; |
88 return false; | 168 return false; |
89 } | 169 } |
90 | 170 |
91 // Make sure the new PID isn't already in our list. | |
92 if (proc_info_entries_.find(proc_info.pid) != proc_info_entries_.end()) { | |
93 LOG(ERROR) << "Duplicate PID in output from " << kPsPathName << "."; | |
94 return false; | |
95 } | |
96 | |
97 if (!proc_info.pid || ! proc_info.vsize) { | 171 if (!proc_info.pid || ! proc_info.vsize) { |
98 LOG(WARNING) << "Invalid data from " << kPsPathName << "."; | 172 LOG(WARNING) << "Invalid data from " << kPsPathName << "."; |
99 return false; | 173 return false; |
100 } | 174 } |
101 | 175 |
102 // Record the process information. | 176 // Record the process information. |
103 proc_info_entries_[proc_info.pid] = proc_info; | 177 proc_info_entries[proc_info.pid] = proc_info; |
104 } | 178 } |
105 | 179 |
106 return true; | 180 return true; |
107 } | 181 } |
108 | 182 |
183 static bool GetProcessMemoryInfoUsingTop( | |
184 std::map<int,ProcessInfoSnapshot::ProcInfoEntry>& proc_info_entries) { | |
185 const char kTopPathName[] = "/usr/bin/top"; | |
186 std::vector<std::string> argv; | |
187 argv.push_back(kTopPathName); | |
188 | |
189 // -stats tells top to print just the given fields as ordered. | |
190 argv.push_back("-stats"); | |
191 argv.push_back("pid," // Process ID | |
192 "rsize," // Resident memory | |
193 "rshrd," // Resident shared memory | |
194 "rprvt," // Resident private memory | |
195 "vsize"); // Total virtual memory | |
196 // Run top in logging (non-interactive) mode. | |
197 argv.push_back("-l"); | |
198 argv.push_back("1"); | |
199 | |
200 std::string output; | |
201 CommandLine command_line(argv); | |
202 // Limit output read to a megabyte for safety. | |
203 if (!base::GetAppOutputRestricted(command_line, &output, 1024 * 1024)) { | |
204 LOG(ERROR) << "Failure running " << kTopPathName << " to acquire data."; | |
205 return false; | |
206 } | |
207 | |
208 // Process lines until done. Lines should look something like this: | |
209 // PID RSIZE RSHRD RPRVT VSIZE | |
210 // 58539 1276K+ 336K+ 740K+ 2378M+ | |
211 // 58485 1888K+ 592K+ 1332K+ 2383M+ | |
212 std::istringstream top_in(output, std::istringstream::in); | |
213 std::string line; | |
214 while (std::getline(top_in, line)) { | |
215 std::istringstream in(line, std::istringstream::in); | |
216 | |
217 // Try to read the PID. | |
218 pid_t pid; | |
219 in >> pid; | |
220 if (in.fail()) | |
221 continue; | |
222 | |
223 // Make sure that caller is interested in this process. | |
224 if (proc_info_entries.find(pid) == proc_info_entries.end()) | |
225 continue; | |
226 | |
227 // Skip the - or + sign that top puts after the pid. | |
228 in.get(); | |
229 | |
230 uint64_t values[4]; | |
231 size_t i; | |
232 for (i = 0; i < arraysize(values); i++) { | |
233 in >> values[i]; | |
234 if (in.fail()) | |
235 break; | |
236 std::string unit; | |
237 in >> unit; | |
238 if (in.fail()) | |
239 break; | |
240 | |
241 if (unit.size() == 0) | |
242 break; | |
243 | |
244 uint64_t scale; | |
245 if (!ConvertByteUnitToScale(unit[0], &scale)) | |
246 break; | |
247 values[i] *= scale; | |
248 } | |
249 if (i != arraysize(values)) | |
250 continue; | |
251 | |
252 ProcessInfoSnapshot::ProcInfoEntry proc_info = proc_info_entries[pid]; | |
253 proc_info.rss = values[0]; | |
254 proc_info.rshrd = values[1]; | |
255 proc_info.rprvt = values[2]; | |
256 proc_info.vsize = values[3]; | |
257 // Record the process information. | |
258 proc_info_entries[proc_info.pid] = proc_info; | |
259 } | |
260 | |
261 return true; | |
262 } | |
263 | |
264 static bool GetProcessMemoryInfoUsingTop_10_5( | |
265 std::map<int,ProcessInfoSnapshot::ProcInfoEntry>& proc_info_entries) { | |
266 const char kTopPathName[] = "/usr/bin/top"; | |
267 std::vector<std::string> argv; | |
268 argv.push_back(kTopPathName); | |
269 | |
270 // -p tells top to print just the given fields as ordered. | |
271 argv.push_back("-p"); | |
272 argv.push_back("^aaaaaaaaaaaaaaaaaaaa " // Process ID (PID) | |
273 "^jjjjjjjjjjjjjjjjjjjj " // Resident memory (RSIZE) | |
274 "^iiiiiiiiiiiiiiiiiiii " // Resident shared memory (RSHRD) | |
275 "^hhhhhhhhhhhhhhhhhhhh " // Resident private memory (RPRVT) | |
276 "^llllllllllllllllllll"); // Total virtual memory (VSIZE) | |
277 // Run top in logging (non-interactive) mode. | |
278 argv.push_back("-l"); | |
279 argv.push_back("1"); | |
280 | |
281 std::string output; | |
282 CommandLine command_line(argv); | |
283 // Limit output read to a megabyte for safety. | |
284 if (!base::GetAppOutputRestricted(command_line, &output, 1024 * 1024)) { | |
285 LOG(ERROR) << "Failure running " << kTopPathName << " to acquire data."; | |
286 return false; | |
287 } | |
288 | |
289 // Process lines until done. Lines should look something like this: | |
290 // PID RSIZE RSHRD RPRVT VSIZE | |
291 // 16943 815104 262144 290816 18489344 | |
292 // 16922 954368 720896 278528 18976768 | |
293 std::istringstream top_in(output, std::istringstream::in); | |
294 std::string line; | |
295 while (std::getline(top_in, line)) { | |
296 std::istringstream in(line, std::istringstream::in); | |
297 | |
298 // Try to read the PID. | |
299 pid_t pid; | |
300 in >> pid; | |
301 if (in.fail()) | |
302 continue; | |
303 | |
304 // Make sure that caller is interested in this process. | |
305 if (proc_info_entries.find(pid) == proc_info_entries.end()) | |
306 continue; | |
307 | |
308 uint64_t values[4]; | |
309 size_t i; | |
310 for (i = 0; i < arraysize(values); i++) { | |
311 in >> values[i]; | |
312 if (in.fail()) | |
313 break; | |
314 } | |
315 if (i != arraysize(values)) | |
316 continue; | |
317 | |
318 ProcessInfoSnapshot::ProcInfoEntry proc_info = proc_info_entries[pid]; | |
319 proc_info.rss = values[0]; | |
320 proc_info.rshrd = values[1]; | |
321 proc_info.rprvt = values[2]; | |
322 proc_info.vsize = values[3]; | |
323 // Record the process information. | |
324 proc_info_entries[proc_info.pid] = proc_info; | |
325 } | |
326 | |
327 return true; | |
328 } | |
329 | |
330 | |
331 bool ProcessInfoSnapshot::Sample(std::vector<base::ProcessId> pid_list) { | |
332 Reset(); | |
333 | |
334 // Nothing to do if no PIDs given. | |
335 if (pid_list.size() == 0) | |
336 return true; | |
337 if (pid_list.size() > kMaxPidListSize) { | |
338 // The spec says |pid_list| *must* not have more than this many entries. | |
339 NOTREACHED(); | |
340 return false; | |
341 } | |
342 | |
343 // Get basic process info from KERN_PROC. | |
344 for (std::vector<base::ProcessId>::iterator it = pid_list.begin(); | |
345 it != pid_list.end(); ++it) { | |
346 ProcInfoEntry proc_info; | |
347 proc_info.pid = *it; | |
348 | |
349 kinfo_proc kinfo; | |
350 if (!GetKInfoForProcessID(*it, &kinfo)) | |
351 return false; | |
352 | |
353 proc_info.ppid = kinfo.kp_eproc.e_ppid; | |
354 proc_info.uid = kinfo.kp_eproc.e_pcred.p_ruid; | |
355 proc_info.euid = kinfo.kp_eproc.e_ucred.cr_uid; | |
356 // Note, p_comm is truncated to 16 characters. | |
357 proc_info.command = kinfo.kp_proc.p_comm; | |
358 proc_info_entries_[*it] = proc_info; | |
359 } | |
360 | |
361 // Use KERN_PROCARGS to get the full executable name. This may fail if this | |
362 // process doesn't have privileges to inspect the target process. | |
363 for (std::vector<base::ProcessId>::iterator it = pid_list.begin(); | |
364 it != pid_list.end(); ++it) { | |
365 std::string exectuable_name; | |
366 if (GetExecutableNameForProcessID(*it, &exectuable_name)) { | |
367 ProcInfoEntry proc_info = proc_info_entries_[*it]; | |
368 proc_info.command = exectuable_name; | |
369 } | |
370 } | |
371 | |
372 // Get memory information using top. | |
373 bool memory_info_success = false; | |
374 int32 major, minor, bugfix; | |
375 base::SysInfo::OperatingSystemVersionNumbers(&major, &minor, &bugfix); | |
376 if (major == 10 && minor == 5) | |
377 memory_info_success = GetProcessMemoryInfoUsingTop_10_5(proc_info_entries_); | |
378 else if ((major == 10 && minor >= 6) || major > 10) | |
379 memory_info_success = GetProcessMemoryInfoUsingTop(proc_info_entries_); | |
380 | |
381 // If top didn't work then fall back to ps. | |
382 if (!memory_info_success) { | |
383 memory_info_success = GetProcessMemoryInfoUsingPS(pid_list, | |
384 proc_info_entries_); | |
385 } | |
386 | |
387 return memory_info_success; | |
388 } | |
389 | |
109 // Clear all the stored information. | 390 // Clear all the stored information. |
110 void ProcessInfoSnapshot::Reset() { | 391 void ProcessInfoSnapshot::Reset() { |
111 proc_info_entries_.clear(); | 392 proc_info_entries_.clear(); |
112 } | 393 } |
113 | 394 |
114 bool ProcessInfoSnapshot::GetProcInfo(int pid, | 395 bool ProcessInfoSnapshot::GetProcInfo(int pid, |
115 ProcInfoEntry* proc_info) const { | 396 ProcInfoEntry* proc_info) const { |
116 std::map<int,ProcInfoEntry>::const_iterator it = proc_info_entries_.find(pid); | 397 std::map<int,ProcInfoEntry>::const_iterator it = proc_info_entries_.find(pid); |
117 if (it == proc_info_entries_.end()) | 398 if (it == proc_info_entries_.end()) |
118 return false; | 399 return false; |
(...skipping 13 matching lines...) Loading... | |
132 | 413 |
133 // Failure of |GetProcInfo()| is "normal", due to racing. | 414 // Failure of |GetProcInfo()| is "normal", due to racing. |
134 ProcInfoEntry proc_info; | 415 ProcInfoEntry proc_info; |
135 if (!GetProcInfo(pid, &proc_info)) { | 416 if (!GetProcInfo(pid, &proc_info)) { |
136 usage->priv = 0; | 417 usage->priv = 0; |
137 usage->mapped = 0; | 418 usage->mapped = 0; |
138 usage->image = 0; | 419 usage->image = 0; |
139 return false; | 420 return false; |
140 } | 421 } |
141 | 422 |
142 usage->priv = proc_info.vsize; | 423 usage->priv = proc_info.vsize / 1024; |
143 usage->mapped = 0; | 424 usage->mapped = 0; |
144 usage->image = 0; | 425 usage->image = 0; |
145 return true; | 426 return true; |
146 } | 427 } |
147 | 428 |
148 bool ProcessInfoSnapshot::GetWorkingSetKBytesOfPID( | 429 bool ProcessInfoSnapshot::GetWorkingSetKBytesOfPID( |
149 int pid, | 430 int pid, |
150 base::WorkingSetKBytes* ws_usage) const { | 431 base::WorkingSetKBytes* ws_usage) const { |
151 // Try to avoid crashing on a bug; stats aren't usually so crucial. | 432 // Try to avoid crashing on a bug; stats aren't usually so crucial. |
152 if (!ws_usage) { | 433 if (!ws_usage) { |
153 NOTREACHED(); | 434 NOTREACHED(); |
154 return false; | 435 return false; |
155 } | 436 } |
156 | 437 |
157 // Failure of |GetProcInfo()| is "normal", due to racing. | 438 // Failure of |GetProcInfo()| is "normal", due to racing. |
158 ProcInfoEntry proc_info; | 439 ProcInfoEntry proc_info; |
159 if (!GetProcInfo(pid, &proc_info)) { | 440 if (!GetProcInfo(pid, &proc_info)) { |
160 ws_usage->priv = 0; | 441 ws_usage->priv = 0; |
161 ws_usage->shareable = 0; | 442 ws_usage->shareable = 0; |
162 ws_usage->shared = 0; | 443 ws_usage->shared = 0; |
163 return false; | 444 return false; |
164 } | 445 } |
165 | 446 |
166 ws_usage->priv = 0; | 447 ws_usage->priv = proc_info.rprvt / 1024; |
167 ws_usage->shareable = proc_info.rss; | 448 ws_usage->shareable = proc_info.rss / 1024; |
168 ws_usage->shared = 0; | 449 ws_usage->shared = proc_info.rshrd / 1024; |
169 return true; | 450 return true; |
170 } | 451 } |
OLD | NEW |