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

Side by Side Diff: base/process_util_linux.cc

Issue 9584024: Overhaul base/process_util_linux.cc. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 8 years, 9 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "base/process_util.h" 5 #include "base/process_util.h"
6 6
7 #include <ctype.h>
8 #include <dirent.h> 7 #include <dirent.h>
9 #include <dlfcn.h> 8 #include <malloc.h>
10 #include <errno.h>
11 #include <fcntl.h>
12 #include <sys/time.h> 9 #include <sys/time.h>
13 #include <sys/types.h> 10 #include <sys/types.h>
14 #include <sys/wait.h>
15 #include <time.h>
16 #include <unistd.h> 11 #include <unistd.h>
17 12
18 #include "base/file_util.h" 13 #include "base/file_util.h"
19 #include "base/logging.h" 14 #include "base/logging.h"
20 #include "base/string_number_conversions.h" 15 #include "base/string_number_conversions.h"
21 #include "base/string_split.h" 16 #include "base/string_split.h"
22 #include "base/string_tokenizer.h" 17 #include "base/string_tokenizer.h"
23 #include "base/string_util.h" 18 #include "base/string_util.h"
24 #include "base/sys_info.h" 19 #include "base/sys_info.h"
25 #include "base/threading/thread_restrictions.h" 20 #include "base/threading/thread_restrictions.h"
26 21
27 namespace { 22 namespace {
28 23
29 enum ParsingState { 24 enum ParsingState {
30 KEY_NAME, 25 KEY_NAME,
31 KEY_VALUE 26 KEY_VALUE
32 }; 27 };
33 28
34 const char kProcDir[] = "/proc"; 29 const char kProcDir[] = "/proc";
30 const char kStatFile[] = "stat";
35 31
36 // Returns a FilePath to "/proc/pid". 32 // Returns a FilePath to "/proc/pid".
37 FilePath GetProcPidDir(pid_t pid) { 33 FilePath GetProcPidDir(pid_t pid) {
38 return FilePath(kProcDir).Append(base::Int64ToString(pid)); 34 return FilePath(kProcDir).Append(base::Int64ToString(pid));
39 } 35 }
40 36
41 // Reads /proc/<pid>/stat and populates |proc_stats| with the values split by 37 // Fields from /proc/<pid>/stat, 0-based. See man 5 proc.
42 // spaces. Returns true if successful. 38 // If the ordering ever changes, carefully review functions that use these
43 bool GetProcStats(pid_t pid, std::vector<std::string>* proc_stats) { 39 // values.
40 enum ProcStatsFields {
41 VM_COMM = 1, // Filename of executable, without parentheses.
42 VM_STATE = 2, // Letter indicating the state of the process.
43 VM_PPID = 3, // PID of the parent.
44 VM_PGRP = 4, // Process group id.
45 VM_UTIME = 13, // Time scheduled in user mode in clock ticks.
46 VM_STIME = 14, // Time scheduled in kernel mode in clock ticks.
47 VM_VSIZE = 22, // Virtual memory size in bytes.
48 VM_RSS = 23, // Resident Set Size in pages.
49 };
50
51 // Reads /proc/<pid>/stat into |buffer|. Returns true if successful.
52 bool ReadProcStats(pid_t pid, std::string* buffer) {
44 // Synchronously reading files in /proc is safe. 53 // Synchronously reading files in /proc is safe.
45 base::ThreadRestrictions::ScopedAllowIO allow_io; 54 base::ThreadRestrictions::ScopedAllowIO allow_io;
46 55
47 FilePath stat_file = GetProcPidDir(pid).Append("stat"); 56 FilePath stat_file = GetProcPidDir(pid).Append(kStatFile);
48 std::string mem_stats; 57 if (!file_util::ReadFileToString(stat_file, buffer)) {
49 if (!file_util::ReadFileToString(stat_file, &mem_stats)) 58 DLOG(WARNING) << "Failed to get process stats.";
50 return false; 59 return false;
51 base::SplitString(mem_stats, ' ', proc_stats); 60 }
Lei Zhang 2012/03/02 22:52:40 This did not take into account the fact that proce
52 return true; 61 return true;
53 } 62 }
54 63
64 // Takes |stats_data| and populates |proc_stats| with the values split by
65 // spaces. Taking into account the 2nd field may, in itself, contain spaces.
66 // Returns true if successful.
67 bool ParseProcStats(const std::string& stats_data,
68 std::vector<std::string>* proc_stats) {
69 // The stat file is formatted as:
70 // pid (process name) data1 data2 .... dataN
71 // Look for the closing paren by scanning backwards, to avoid being fooled by
72 // processes with ')' in the name.
73 size_t open_parens_idx = stats_data.find(" (");
74 size_t close_parens_idx = stats_data.rfind(") ");
75 if (open_parens_idx == std::string::npos ||
76 close_parens_idx == std::string::npos ||
77 open_parens_idx > close_parens_idx) {
78 NOTREACHED();
79 return false;
80 }
81 open_parens_idx++;
82
83 proc_stats->clear();
84 // PID.
85 proc_stats->push_back(stats_data.substr(0, open_parens_idx));
86 // Process name without parentheses.
87 proc_stats->push_back(
88 stats_data.substr(open_parens_idx + 1,
89 close_parens_idx - (open_parens_idx + 1)));
90
91 // Split the rest.
92 std::vector<std::string> other_stats;
93 base::SplitString(stats_data.substr(close_parens_idx + 2), ' ', &other_stats);
94 for (size_t i = 0; i < other_stats.size(); ++i)
95 proc_stats->push_back(other_stats[i]);
96 return true;
97 }
98
99 // Reads the |field_num|th field from |proc_stats|. Returns 0 on failure.
100 // This version does not handle the first 3 values, since the first value is
101 // simply |pid|, and the next two values are strings.
102 size_t GetProcStatsFieldAsInt(const std::vector<std::string>& proc_stats,
103 ProcStatsFields field_num) {
104 if (field_num < VM_PPID) {
105 NOTREACHED();
106 return 0;
107 }
108
109 if (proc_stats.size() > static_cast<size_t>(field_num)) {
110 size_t value;
111 if (base::StringToUint64(proc_stats[field_num], &value))
112 return value;
113 }
114 NOTREACHED();
115 return 0;
116 }
117
118 // Convenience wrapper around GetProcStatsFieldAsInt(), ParseProcStats() and
119 // ReadProcStats(). See GetProcStatsFieldAsInt() for details.
120 size_t ReadProcStatsAndGetFieldAsInt(pid_t pid, ProcStatsFields field_num) {
121 std::string stats_data;
122 if (!ReadProcStats(pid, &stats_data))
123 return 0;
124 std::vector<std::string> proc_stats;
125 if (!ParseProcStats(stats_data, &proc_stats))
126 return 0;
127 return GetProcStatsFieldAsInt(proc_stats, field_num);
128 }
129
130 // Reads the |field_num|th field from |proc_stats|.
131 // Returns an empty string on failure.
132 // This version only handles VM_COMM and VM_STATE, which are the only fields
133 // that are strings.
134 std::string GetProcStatsFieldAsString(
135 const std::vector<std::string>& proc_stats,
136 ProcStatsFields field_num) {
137 if (field_num < VM_COMM || field_num > VM_STATE) {
138 NOTREACHED();
139 return "";
140 }
141
142 if (proc_stats.size() > static_cast<size_t>(field_num))
143 return proc_stats[field_num];
144
145 NOTREACHED();
146 return 0;
147 }
148
149 // Convenience wrapper around GetProcStatsFieldAsString(), ParseProcStats() and
150 // ReadProcStats(). See GetProcStatsFieldAsString() for details.
151 std::string ReadProcStatsAndGetFieldAsString(pid_t pid,
152 ProcStatsFields field_num) {
Elliot Glaysher 2012/03/02 23:23:44 Is this used?
Lei Zhang 2012/03/02 23:48:40 Nope! Delete.
153 std::string stats_data;
154 if (!ReadProcStats(pid, &stats_data))
155 return "";
156 std::vector<std::string> proc_stats;
157 if (!ParseProcStats(stats_data, &proc_stats))
158 return "";
159 return GetProcStatsFieldAsString(proc_stats, field_num);
160 }
161
55 // Reads /proc/<pid>/cmdline and populates |proc_cmd_line_args| with the command 162 // Reads /proc/<pid>/cmdline and populates |proc_cmd_line_args| with the command
56 // line arguments. Returns true if successful. 163 // line arguments. Returns true if successful.
57 // Note: /proc/<pid>/cmdline contains command line arguments separated by single 164 // Note: /proc/<pid>/cmdline contains command line arguments separated by single
58 // null characters. We tokenize it into a vector of strings using '\0' as a 165 // null characters. We tokenize it into a vector of strings using '\0' as a
59 // delimiter. 166 // delimiter.
60 bool GetProcCmdline(pid_t pid, std::vector<std::string>* proc_cmd_line_args) { 167 bool GetProcCmdline(pid_t pid, std::vector<std::string>* proc_cmd_line_args) {
61 // Synchronously reading files in /proc is safe. 168 // Synchronously reading files in /proc is safe.
62 base::ThreadRestrictions::ScopedAllowIO allow_io; 169 base::ThreadRestrictions::ScopedAllowIO allow_io;
63 170
64 FilePath cmd_line_file = GetProcPidDir(pid).Append("cmdline"); 171 FilePath cmd_line_file = GetProcPidDir(pid).Append("cmdline");
65 std::string cmd_line; 172 std::string cmd_line;
66 if (!file_util::ReadFileToString(cmd_line_file, &cmd_line)) 173 if (!file_util::ReadFileToString(cmd_line_file, &cmd_line))
67 return false; 174 return false;
68 std::string delimiters; 175 std::string delimiters;
69 delimiters.push_back('\0'); 176 delimiters.push_back('\0');
70 Tokenize(cmd_line, delimiters, proc_cmd_line_args); 177 Tokenize(cmd_line, delimiters, proc_cmd_line_args);
71 return true; 178 return true;
72 } 179 }
73 180
181 // Take a /proc directory entry named |d_name|, and if it is the directory for
182 // a process, convert it to a pid_t.
183 // Returns 0 on failure.
184 // e.g. /proc/self/ will return 0, whereas /proc/1234 will return 124.
Elliot Glaysher 2012/03/02 23:23:44 Did you mean 1234?
Lei Zhang 2012/03/02 23:48:40 Ya.
185 pid_t ProcDirSlotToPid(const char* d_name) {
186 int i;
187 for (i = 0; i < NAME_MAX && d_name[i]; ++i) {
188 if (!IsAsciiDigit(d_name[i])) {
189 return 0;
190 }
191 }
192 if (i == NAME_MAX)
193 return 0;
194
195 // Read the process's command line.
196 pid_t pid;
197 std::string pid_string(d_name);
198 if (!base::StringToInt(pid_string, &pid)) {
199 NOTREACHED();
200 return 0;
201 }
202 return pid;
203 }
204
74 // Get the total CPU of a single process. Return value is number of jiffies 205 // Get the total CPU of a single process. Return value is number of jiffies
75 // on success or -1 on error. 206 // on success or -1 on error.
76 int GetProcessCPU(pid_t pid) { 207 int GetProcessCPU(pid_t pid) {
77 // Synchronously reading files in /proc is safe. 208 // Use /proc/<pid>/task to find all threads and parse their /stat file.
78 base::ThreadRestrictions::ScopedAllowIO allow_io; 209 FilePath task_path = GetProcPidDir(pid).Append("task");
79 210
80 // Use /proc/<pid>/task to find all threads and parse their /stat file. 211 DIR* dir = opendir(task_path.value().c_str());
81 FilePath path = GetProcPidDir(pid).Append("task");
82
83 DIR* dir = opendir(path.value().c_str());
84 if (!dir) { 212 if (!dir) {
85 DPLOG(ERROR) << "opendir(" << path.value() << ")"; 213 DPLOG(ERROR) << "opendir(" << task_path.value() << ")";
86 return -1; 214 return -1;
87 } 215 }
88 216
89 int total_cpu = 0; 217 int total_cpu = 0;
90 while (struct dirent* ent = readdir(dir)) { 218 while (struct dirent* ent = readdir(dir)) {
91 if (ent->d_name[0] == '.') 219 pid_t tid = ProcDirSlotToPid(ent->d_name);
220 if (!tid)
92 continue; 221 continue;
93 222
94 FilePath stat_path = path.AppendASCII(ent->d_name).AppendASCII("stat"); 223 // Synchronously reading files in /proc is safe.
224 base::ThreadRestrictions::ScopedAllowIO allow_io;
225
95 std::string stat; 226 std::string stat;
227 FilePath stat_path = task_path.Append(ent->d_name).Append(kStatFile);
96 if (file_util::ReadFileToString(stat_path, &stat)) { 228 if (file_util::ReadFileToString(stat_path, &stat)) {
97 int cpu = base::ParseProcStatCPU(stat); 229 int cpu = base::ParseProcStatCPU(stat);
98 if (cpu > 0) 230 if (cpu > 0)
99 total_cpu += cpu; 231 total_cpu += cpu;
100 } 232 }
101 } 233 }
102 closedir(dir); 234 closedir(dir);
103 235
104 return total_cpu; 236 return total_cpu;
105 } 237 }
106 238
107 } // namespace 239 // Read /proc/<pid>/status and returns the value for |field|, or 0 on failure.
108 240 // Only works for fields in the form of "Field: value kB".
109 namespace base { 241 size_t ReadProcStatusAndGetFieldAsInt(pid_t pid, const std::string& field) {
110 242 FilePath stat_file = GetProcPidDir(pid).Append("status");
111 ProcessId GetParentProcessId(ProcessHandle process) {
112 // Synchronously reading files in /proc is safe.
113 base::ThreadRestrictions::ScopedAllowIO allow_io;
114
115 FilePath stat_file = GetProcPidDir(process).Append("status");
116 std::string status; 243 std::string status;
117 if (!file_util::ReadFileToString(stat_file, &status)) 244 {
118 return -1; 245 // Synchronously reading files in /proc is safe.
246 base::ThreadRestrictions::ScopedAllowIO allow_io;
247 if (!file_util::ReadFileToString(stat_file, &status))
248 return 0;
249 }
119 250
120 StringTokenizer tokenizer(status, ":\n"); 251 StringTokenizer tokenizer(status, ":\n");
121 ParsingState state = KEY_NAME; 252 ParsingState state = KEY_NAME;
122 StringPiece last_key_name; 253 base::StringPiece last_key_name;
123 while (tokenizer.GetNext()) { 254 while (tokenizer.GetNext()) {
124 switch (state) { 255 switch (state) {
125 case KEY_NAME: 256 case KEY_NAME:
126 last_key_name = tokenizer.token_piece(); 257 last_key_name = tokenizer.token_piece();
127 state = KEY_VALUE; 258 state = KEY_VALUE;
128 break; 259 break;
129 case KEY_VALUE: 260 case KEY_VALUE:
130 DCHECK(!last_key_name.empty()); 261 DCHECK(!last_key_name.empty());
131 if (last_key_name == "PPid") { 262 if (last_key_name == field) {
132 int ppid; 263 std::string value_str;
133 base::StringToInt(tokenizer.token_piece(), &ppid); 264 tokenizer.token_piece().CopyToString(&value_str);
134 return ppid; 265 std::string value_str_trimmed;
266 TrimWhitespaceASCII(value_str, TRIM_ALL, &value_str_trimmed);
267 std::vector<std::string> split_value_str;
268 base::SplitString(value_str_trimmed, ' ', &split_value_str);
269 if (split_value_str.size() != 2 || split_value_str[1] != "kB") {
270 NOTREACHED();
271 return 0;
272 }
273 size_t value;
274 if (!base::StringToUint64(split_value_str[0], &value)) {
275 NOTREACHED();
276 return 0;
277 }
278 return value;
135 } 279 }
136 state = KEY_NAME; 280 state = KEY_NAME;
137 break; 281 break;
138 } 282 }
139 } 283 }
140 NOTREACHED(); 284 NOTREACHED();
285 return 0;
286 }
287
288 } // namespace
289
290 namespace base {
291
292 ProcessId GetParentProcessId(ProcessHandle process) {
293 size_t pid = ReadProcStatsAndGetFieldAsInt(process, VM_PPID);
294 if (pid)
295 return pid;
141 return -1; 296 return -1;
142 } 297 }
143 298
144 FilePath GetProcessExecutablePath(ProcessHandle process) { 299 FilePath GetProcessExecutablePath(ProcessHandle process) {
145 FilePath stat_file = GetProcPidDir(process).Append("exe"); 300 FilePath stat_file = GetProcPidDir(process).Append("exe");
146 FilePath exe_name; 301 FilePath exe_name;
147 if (!file_util::ReadSymbolicLink(stat_file, &exe_name)) { 302 if (!file_util::ReadSymbolicLink(stat_file, &exe_name)) {
148 // No such process. Happens frequently in e.g. TerminateAllChromeProcesses 303 // No such process. Happens frequently in e.g. TerminateAllChromeProcesses
149 return FilePath(); 304 return FilePath();
150 } 305 }
151 return exe_name; 306 return exe_name;
152 } 307 }
153 308
154 ProcessIterator::ProcessIterator(const ProcessFilter* filter) 309 ProcessIterator::ProcessIterator(const ProcessFilter* filter)
155 : filter_(filter) { 310 : filter_(filter) {
156 procfs_dir_ = opendir(kProcDir); 311 procfs_dir_ = opendir(kProcDir);
157 } 312 }
158 313
159 ProcessIterator::~ProcessIterator() { 314 ProcessIterator::~ProcessIterator() {
160 if (procfs_dir_) { 315 if (procfs_dir_) {
161 closedir(procfs_dir_); 316 closedir(procfs_dir_);
162 procfs_dir_ = NULL; 317 procfs_dir_ = NULL;
163 } 318 }
164 } 319 }
165 320
166 bool ProcessIterator::CheckForNextProcess() { 321 bool ProcessIterator::CheckForNextProcess() {
167 // TODO(port): skip processes owned by different UID 322 // TODO(port): skip processes owned by different UID
168 323
169 dirent* slot = 0; 324 pid_t pid = kNullProcessId;
170 const char* openparen;
171 const char* closeparen;
172 std::vector<std::string> cmd_line_args; 325 std::vector<std::string> cmd_line_args;
326 std::string stats_data;
327 std::vector<std::string> proc_stats;
173 328
174 // Arbitrarily guess that there will never be more than 200 non-process 329 // Arbitrarily guess that there will never be more than 200 non-process
175 // files in /proc. Hardy has 53. 330 // files in /proc. Hardy has 53 and Lucid has 61.
176 int skipped = 0; 331 int skipped = 0;
177 const int kSkipLimit = 200; 332 const int kSkipLimit = 200;
178 while (skipped < kSkipLimit) { 333 while (skipped < kSkipLimit) {
179 slot = readdir(procfs_dir_); 334 dirent* slot = readdir(procfs_dir_);
180 // all done looking through /proc? 335 // all done looking through /proc?
181 if (!slot) 336 if (!slot)
182 return false; 337 return false;
183 338
184 // If not a process, keep looking for one. 339 // If not a process, keep looking for one.
185 bool notprocess = false; 340 pid = ProcDirSlotToPid(slot->d_name);
186 int i; 341 if (!pid) {
187 for (i = 0; i < NAME_MAX && slot->d_name[i]; ++i) {
188 if (!isdigit(slot->d_name[i])) {
189 notprocess = true;
190 break;
191 }
192 }
193 if (i == NAME_MAX || notprocess) {
194 skipped++; 342 skipped++;
195 continue; 343 continue;
196 } 344 }
197 345
198 // Read the process's command line. 346 if (!GetProcCmdline(pid, &cmd_line_args))
199 std::string pid_string(slot->d_name);
200 int pid;
201 if (StringToInt(pid_string, &pid) && !GetProcCmdline(pid, &cmd_line_args))
202 continue; 347 continue;
203 348
204 // Read the process's status. 349 if (!ReadProcStats(pid, &stats_data))
205 char buf[NAME_MAX + 12];
206 sprintf(buf, "/proc/%s/stat", slot->d_name);
207 FILE* fp = fopen(buf, "r");
208 if (!fp)
209 continue; 350 continue;
210 const char* result = fgets(buf, sizeof(buf), fp); 351 if (!ParseProcStats(stats_data, &proc_stats))
211 fclose(fp);
212 if (!result)
213 continue; 352 continue;
214 353
215 // Parse the status. It is formatted like this: 354 std::string runstate = GetProcStatsFieldAsString(proc_stats, VM_STATE);
216 // %d (%s) %c %d %d ... 355 if (runstate.size() != 1) {
217 // pid (name) runstate ppid gid 356 NOTREACHED();
218 // To avoid being fooled by names containing a closing paren, scan
219 // backwards.
220 openparen = strchr(buf, '(');
221 closeparen = strrchr(buf, ')');
222 if (!openparen || !closeparen)
223 continue; 357 continue;
224 char runstate = closeparen[2]; 358 }
225 359
226 // Is the process in 'Zombie' state, i.e. dead but waiting to be reaped? 360 // Is the process in 'Zombie' state, i.e. dead but waiting to be reaped?
227 // Allowed values: D R S T Z 361 // Allowed values: D R S T Z
228 if (runstate != 'Z') 362 if (runstate[0] != 'Z')
229 break; 363 break;
230 364
231 // Nope, it's a zombie; somebody isn't cleaning up after their children. 365 // Nope, it's a zombie; somebody isn't cleaning up after their children.
232 // (e.g. WaitForProcessesToExit doesn't clean up after dead children yet.) 366 // (e.g. WaitForProcessesToExit doesn't clean up after dead children yet.)
233 // There could be a lot of zombies, can't really decrement i here. 367 // There could be a lot of zombies, can't really decrement i here.
234 } 368 }
235 if (skipped >= kSkipLimit) { 369 if (skipped >= kSkipLimit) {
236 NOTREACHED(); 370 NOTREACHED();
237 return false; 371 return false;
238 } 372 }
239 373
240 // This seems fragile. 374 entry_.pid_ = pid;
241 entry_.pid_ = atoi(slot->d_name); 375 entry_.ppid_ = GetProcStatsFieldAsInt(proc_stats, VM_PPID);
242 entry_.ppid_ = atoi(closeparen + 3); 376 entry_.gid_ = GetProcStatsFieldAsInt(proc_stats, VM_PGRP);
243 entry_.gid_ = atoi(strchr(closeparen + 4, ' '));
244
245 entry_.cmd_line_args_.assign(cmd_line_args.begin(), cmd_line_args.end()); 377 entry_.cmd_line_args_.assign(cmd_line_args.begin(), cmd_line_args.end());
246 378
247 // TODO(port): read pid's commandline's $0, like killall does. Using the 379 // TODO(port): read pid's commandline's $0, like killall does. Using the
248 // short name between openparen and closeparen won't work for long names! 380 // short name between openparen and closeparen won't work for long names!
249 int len = closeparen - openparen - 1; 381 entry_.exe_file_ = GetProcStatsFieldAsInt(proc_stats, VM_COMM);
250 entry_.exe_file_.assign(openparen + 1, len);
251 return true; 382 return true;
252 } 383 }
253 384
254 bool NamedProcessIterator::IncludeEntry() { 385 bool NamedProcessIterator::IncludeEntry() {
255 if (executable_name_ != entry().exe_file()) 386 if (executable_name_ != entry().exe_file())
256 return false; 387 return false;
257 return ProcessIterator::IncludeEntry(); 388 return ProcessIterator::IncludeEntry();
258 } 389 }
259 390
260 391
261 // static 392 // static
262 ProcessMetrics* ProcessMetrics::CreateProcessMetrics(ProcessHandle process) { 393 ProcessMetrics* ProcessMetrics::CreateProcessMetrics(ProcessHandle process) {
263 return new ProcessMetrics(process); 394 return new ProcessMetrics(process);
264 } 395 }
265 396
266 // On linux, we return vsize. 397 // On linux, we return vsize.
267 size_t ProcessMetrics::GetPagefileUsage() const { 398 size_t ProcessMetrics::GetPagefileUsage() const {
268 std::vector<std::string> proc_stats; 399 return ReadProcStatsAndGetFieldAsInt(process_, VM_VSIZE);
269 if (!GetProcStats(process_, &proc_stats))
270 DLOG(WARNING) << "Failed to get process stats.";
271 const size_t kVmSize = 22;
272 if (proc_stats.size() > kVmSize) {
273 int vm_size;
274 base::StringToInt(proc_stats[kVmSize], &vm_size);
275 return static_cast<size_t>(vm_size);
276 }
277 return 0;
278 } 400 }
279 401
280 // On linux, we return the high water mark of vsize. 402 // On linux, we return the high water mark of vsize.
281 size_t ProcessMetrics::GetPeakPagefileUsage() const { 403 size_t ProcessMetrics::GetPeakPagefileUsage() const {
282 std::vector<std::string> proc_stats; 404 return ReadProcStatusAndGetFieldAsInt(process_, "VmPeak") * 1024;
283 if (!GetProcStats(process_, &proc_stats))
284 DLOG(WARNING) << "Failed to get process stats.";
285 const size_t kVmPeak = 21;
Lei Zhang 2012/03/02 22:52:40 This is wrong according to man 5 proc.
286 if (proc_stats.size() > kVmPeak) {
287 int vm_peak;
288 if (base::StringToInt(proc_stats[kVmPeak], &vm_peak))
289 return vm_peak;
290 }
291 return 0;
292 } 405 }
293 406
294 // On linux, we return RSS. 407 // On linux, we return RSS.
295 size_t ProcessMetrics::GetWorkingSetSize() const { 408 size_t ProcessMetrics::GetWorkingSetSize() const {
296 std::vector<std::string> proc_stats; 409 return ReadProcStatsAndGetFieldAsInt(process_, VM_RSS) * getpagesize();
297 if (!GetProcStats(process_, &proc_stats))
298 DLOG(WARNING) << "Failed to get process stats.";
299 const size_t kVmRss = 23;
300 if (proc_stats.size() > kVmRss) {
301 int num_pages;
302 if (base::StringToInt(proc_stats[kVmRss], &num_pages))
303 return static_cast<size_t>(num_pages) * getpagesize();
304 }
305 return 0;
306 } 410 }
307 411
308 // On linux, we return the high water mark of RSS. 412 // On linux, we return the high water mark of RSS.
309 size_t ProcessMetrics::GetPeakWorkingSetSize() const { 413 size_t ProcessMetrics::GetPeakWorkingSetSize() const {
310 std::vector<std::string> proc_stats; 414 return ReadProcStatusAndGetFieldAsInt(process_, "VmHWM") * 1024;
311 if (!GetProcStats(process_, &proc_stats))
312 DLOG(WARNING) << "Failed to get process stats.";
313 const size_t kVmHwm = 23;
Lei Zhang 2012/03/02 22:52:40 This is clearly wrong, because GetPeakWorkingSetSi
314 if (proc_stats.size() > kVmHwm) {
315 int num_pages;
316 base::StringToInt(proc_stats[kVmHwm], &num_pages);
317 return static_cast<size_t>(num_pages) * getpagesize();
318 }
319 return 0;
320 } 415 }
321 416
322 bool ProcessMetrics::GetMemoryBytes(size_t* private_bytes, 417 bool ProcessMetrics::GetMemoryBytes(size_t* private_bytes,
323 size_t* shared_bytes) { 418 size_t* shared_bytes) {
324 WorkingSetKBytes ws_usage; 419 WorkingSetKBytes ws_usage;
325 if (!GetWorkingSetKBytes(&ws_usage)) 420 if (!GetWorkingSetKBytes(&ws_usage))
326 return false; 421 return false;
327 422
328 if (private_bytes) 423 if (private_bytes)
329 *private_bytes = ws_usage.priv << 10; 424 *private_bytes = ws_usage.priv * 1024;
330 425
331 if (shared_bytes) 426 if (shared_bytes)
332 *shared_bytes = ws_usage.shared * 1024; 427 *shared_bytes = ws_usage.shared * 1024;
333 428
334 return true; 429 return true;
335 } 430 }
336 431
337 // Private and Shared working set sizes are obtained from /proc/<pid>/statm. 432 // Private and Shared working set sizes are obtained from /proc/<pid>/statm.
338 bool ProcessMetrics::GetWorkingSetKBytes(WorkingSetKBytes* ws_usage) const { 433 bool ProcessMetrics::GetWorkingSetKBytes(WorkingSetKBytes* ws_usage) const {
339 // Use statm instead of smaps because smaps is: 434 // Use statm instead of smaps because smaps is:
340 // a) Large and slow to parse. 435 // a) Large and slow to parse.
341 // b) Unavailable in the SUID sandbox. 436 // b) Unavailable in the SUID sandbox.
342 437
343 // First we need to get the page size, since everything is measured in pages. 438 // First we need to get the page size, since everything is measured in pages.
344 // For details, see: man 5 proc. 439 // For details, see: man 5 proc.
345 const int page_size_kb = sysconf(_SC_PAGE_SIZE) / 1024; 440 const int page_size_kb = getpagesize() / 1024;
346 if (page_size_kb <= 0) 441 if (page_size_kb <= 0)
347 return false; 442 return false;
348 443
349 std::string statm; 444 std::string statm;
350 { 445 {
351 FilePath statm_file = GetProcPidDir(process_).Append("statm"); 446 FilePath statm_file = GetProcPidDir(process_).Append("statm");
352 // Synchronously reading files in /proc is safe. 447 // Synchronously reading files in /proc is safe.
353 base::ThreadRestrictions::ScopedAllowIO allow_io; 448 base::ThreadRestrictions::ScopedAllowIO allow_io;
354 bool ret = file_util::ReadFileToString(statm_file, &statm); 449 bool ret = file_util::ReadFileToString(statm_file, &statm);
355 if (!ret || statm.length() == 0) 450 if (!ret || statm.length() == 0)
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
466 : process_(process), 561 : process_(process),
467 last_time_(0), 562 last_time_(0),
468 last_system_time_(0), 563 last_system_time_(0),
469 last_cpu_(0) { 564 last_cpu_(0) {
470 processor_count_ = base::SysInfo::NumberOfProcessors(); 565 processor_count_ = base::SysInfo::NumberOfProcessors();
471 } 566 }
472 567
473 568
474 // Exposed for testing. 569 // Exposed for testing.
475 int ParseProcStatCPU(const std::string& input) { 570 int ParseProcStatCPU(const std::string& input) {
476 // /proc/<pid>/stat contains the process name in parens. In case the 571 std::vector<std::string> proc_stats;
477 // process name itself contains parens, skip past them. 572 if (!ParseProcStats(input, &proc_stats))
478 std::string::size_type rparen = input.rfind(')');
479 if (rparen == std::string::npos)
480 return -1; 573 return -1;
481 574
482 // From here, we expect a bunch of space-separated fields, where the 575 if (proc_stats.size() <= VM_STIME)
483 // 0-indexed 11th and 12th are utime and stime. On two different machines 576 return -1;
484 // I found 42 and 39 fields, so let's just expect the ones we need. 577 size_t utime = GetProcStatsFieldAsInt(proc_stats, VM_UTIME);
485 std::vector<std::string> fields; 578 size_t stime = GetProcStatsFieldAsInt(proc_stats, VM_STIME);
486 base::SplitString(input.substr(rparen + 2), ' ', &fields); 579 return utime + stime;
487 if (fields.size() < 13)
488 return -1; // Output not in the format we expect.
489
490 int fields11, fields12;
491 base::StringToInt(fields[11], &fields11);
492 base::StringToInt(fields[12], &fields12);
493 return fields11 + fields12;
494 } 580 }
495 581
496 namespace { 582 namespace {
497 583
498 // The format of /proc/meminfo is: 584 // The format of /proc/meminfo is:
499 // 585 //
500 // MemTotal: 8235324 kB 586 // MemTotal: 8235324 kB
501 // MemFree: 1628304 kB 587 // MemFree: 1628304 kB
502 // Buffers: 429596 kB 588 // Buffers: 429596 kB
503 // Cached: 4728232 kB 589 // Cached: 4728232 kB
504 // ... 590 // ...
505 const size_t kMemTotalIndex = 1; 591 const size_t kMemTotalIndex = 1;
506 const size_t kMemFreeIndex = 4; 592 const size_t kMemFreeIndex = 4;
507 const size_t kMemBuffersIndex = 7; 593 const size_t kMemBuffersIndex = 7;
508 const size_t kMemCachedIndex = 10; 594 const size_t kMemCachedIndex = 10;
509 const size_t kMemActiveAnonIndex = 22; 595 const size_t kMemActiveAnonIndex = 22;
510 const size_t kMemInactiveAnonIndex = 25; 596 const size_t kMemInactiveAnonIndex = 25;
511 597
512 } // namespace 598 } // namespace
513 599
514 bool GetSystemMemoryInfo(SystemMemoryInfoKB* meminfo) { 600 bool GetSystemMemoryInfo(SystemMemoryInfoKB* meminfo) {
515 // Synchronously reading files in /proc is safe. 601 // Synchronously reading files in /proc is safe.
516 base::ThreadRestrictions::ScopedAllowIO allow_io; 602 base::ThreadRestrictions::ScopedAllowIO allow_io;
517 603
518 // Used memory is: total - free - buffers - caches 604 // Used memory is: total - free - buffers - caches
519 FilePath meminfo_file("/proc/meminfo"); 605 FilePath meminfo_file("/proc/meminfo");
520 std::string meminfo_data; 606 std::string meminfo_data;
521 if (!file_util::ReadFileToString(meminfo_file, &meminfo_data)) { 607 if (!file_util::ReadFileToString(meminfo_file, &meminfo_data)) {
522 DLOG(WARNING) << "Failed to open /proc/meminfo."; 608 DLOG(WARNING) << "Failed to open " << meminfo_file.value();
523 return false; 609 return false;
524 } 610 }
525 std::vector<std::string> meminfo_fields; 611 std::vector<std::string> meminfo_fields;
526 SplitStringAlongWhitespace(meminfo_data, &meminfo_fields); 612 SplitStringAlongWhitespace(meminfo_data, &meminfo_fields);
527 613
528 if (meminfo_fields.size() < kMemCachedIndex) { 614 if (meminfo_fields.size() < kMemCachedIndex) {
529 DLOG(WARNING) << "Failed to parse /proc/meminfo. Only found " << 615 DLOG(WARNING) << "Failed to parse " << meminfo_file.value()
530 meminfo_fields.size() << " fields."; 616 << ". Only found " << meminfo_fields.size() << " fields.";
531 return false; 617 return false;
532 } 618 }
533 619
534 DCHECK_EQ(meminfo_fields[kMemTotalIndex-1], "MemTotal:"); 620 DCHECK_EQ(meminfo_fields[kMemTotalIndex-1], "MemTotal:");
535 DCHECK_EQ(meminfo_fields[kMemFreeIndex-1], "MemFree:"); 621 DCHECK_EQ(meminfo_fields[kMemFreeIndex-1], "MemFree:");
536 DCHECK_EQ(meminfo_fields[kMemBuffersIndex-1], "Buffers:"); 622 DCHECK_EQ(meminfo_fields[kMemBuffersIndex-1], "Buffers:");
537 DCHECK_EQ(meminfo_fields[kMemCachedIndex-1], "Cached:"); 623 DCHECK_EQ(meminfo_fields[kMemCachedIndex-1], "Cached:");
538 DCHECK_EQ(meminfo_fields[kMemActiveAnonIndex-1], "Active(anon):"); 624 DCHECK_EQ(meminfo_fields[kMemActiveAnonIndex-1], "Active(anon):");
539 DCHECK_EQ(meminfo_fields[kMemInactiveAnonIndex-1], "Inactive(anon):"); 625 DCHECK_EQ(meminfo_fields[kMemInactiveAnonIndex-1], "Inactive(anon):");
540 626
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after
716 int score_len = static_cast<int>(score_str.length()); 802 int score_len = static_cast<int>(score_str.length());
717 return (score_len == file_util::WriteFile(oom_file, 803 return (score_len == file_util::WriteFile(oom_file,
718 score_str.c_str(), 804 score_str.c_str(),
719 score_len)); 805 score_len));
720 } 806 }
721 807
722 return false; 808 return false;
723 } 809 }
724 810
725 } // namespace base 811 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698