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

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::IntToString(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 }
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 int 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 int value;
111 if (base::StringToInt(proc_stats[field_num], &value))
112 return value;
113 }
114 NOTREACHED();
Jeffrey Yasskin 2012/09/27 23:33:07 This NOTREACHED gets hit when a field in proc is t
115 return 0;
116 }
117
118 // Convenience wrapper around GetProcStatsFieldAsInt(), ParseProcStats() and
119 // ReadProcStats(). See GetProcStatsFieldAsInt() for details.
120 int 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
55 // Reads /proc/<pid>/cmdline and populates |proc_cmd_line_args| with the command 149 // Reads /proc/<pid>/cmdline and populates |proc_cmd_line_args| with the command
56 // line arguments. Returns true if successful. 150 // line arguments. Returns true if successful.
57 // Note: /proc/<pid>/cmdline contains command line arguments separated by single 151 // 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 152 // null characters. We tokenize it into a vector of strings using '\0' as a
59 // delimiter. 153 // delimiter.
60 bool GetProcCmdline(pid_t pid, std::vector<std::string>* proc_cmd_line_args) { 154 bool GetProcCmdline(pid_t pid, std::vector<std::string>* proc_cmd_line_args) {
61 // Synchronously reading files in /proc is safe. 155 // Synchronously reading files in /proc is safe.
62 base::ThreadRestrictions::ScopedAllowIO allow_io; 156 base::ThreadRestrictions::ScopedAllowIO allow_io;
63 157
64 FilePath cmd_line_file = GetProcPidDir(pid).Append("cmdline"); 158 FilePath cmd_line_file = GetProcPidDir(pid).Append("cmdline");
65 std::string cmd_line; 159 std::string cmd_line;
66 if (!file_util::ReadFileToString(cmd_line_file, &cmd_line)) 160 if (!file_util::ReadFileToString(cmd_line_file, &cmd_line))
67 return false; 161 return false;
68 std::string delimiters; 162 std::string delimiters;
69 delimiters.push_back('\0'); 163 delimiters.push_back('\0');
70 Tokenize(cmd_line, delimiters, proc_cmd_line_args); 164 Tokenize(cmd_line, delimiters, proc_cmd_line_args);
71 return true; 165 return true;
72 } 166 }
73 167
168 // Take a /proc directory entry named |d_name|, and if it is the directory for
169 // a process, convert it to a pid_t.
170 // Returns 0 on failure.
171 // e.g. /proc/self/ will return 0, whereas /proc/1234 will return 1234.
172 pid_t ProcDirSlotToPid(const char* d_name) {
173 int i;
174 for (i = 0; i < NAME_MAX && d_name[i]; ++i) {
175 if (!IsAsciiDigit(d_name[i])) {
176 return 0;
177 }
178 }
179 if (i == NAME_MAX)
180 return 0;
181
182 // Read the process's command line.
183 pid_t pid;
184 std::string pid_string(d_name);
185 if (!base::StringToInt(pid_string, &pid)) {
186 NOTREACHED();
187 return 0;
188 }
189 return pid;
190 }
191
74 // Get the total CPU of a single process. Return value is number of jiffies 192 // Get the total CPU of a single process. Return value is number of jiffies
75 // on success or -1 on error. 193 // on success or -1 on error.
76 int GetProcessCPU(pid_t pid) { 194 int GetProcessCPU(pid_t pid) {
77 // Synchronously reading files in /proc is safe. 195 // Use /proc/<pid>/task to find all threads and parse their /stat file.
78 base::ThreadRestrictions::ScopedAllowIO allow_io; 196 FilePath task_path = GetProcPidDir(pid).Append("task");
79 197
80 // Use /proc/<pid>/task to find all threads and parse their /stat file. 198 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) { 199 if (!dir) {
85 DPLOG(ERROR) << "opendir(" << path.value() << ")"; 200 DPLOG(ERROR) << "opendir(" << task_path.value() << ")";
86 return -1; 201 return -1;
87 } 202 }
88 203
89 int total_cpu = 0; 204 int total_cpu = 0;
90 while (struct dirent* ent = readdir(dir)) { 205 while (struct dirent* ent = readdir(dir)) {
91 if (ent->d_name[0] == '.') 206 pid_t tid = ProcDirSlotToPid(ent->d_name);
207 if (!tid)
92 continue; 208 continue;
93 209
94 FilePath stat_path = path.AppendASCII(ent->d_name).AppendASCII("stat"); 210 // Synchronously reading files in /proc is safe.
211 base::ThreadRestrictions::ScopedAllowIO allow_io;
212
95 std::string stat; 213 std::string stat;
214 FilePath stat_path = task_path.Append(ent->d_name).Append(kStatFile);
96 if (file_util::ReadFileToString(stat_path, &stat)) { 215 if (file_util::ReadFileToString(stat_path, &stat)) {
97 int cpu = base::ParseProcStatCPU(stat); 216 int cpu = base::ParseProcStatCPU(stat);
98 if (cpu > 0) 217 if (cpu > 0)
99 total_cpu += cpu; 218 total_cpu += cpu;
100 } 219 }
101 } 220 }
102 closedir(dir); 221 closedir(dir);
103 222
104 return total_cpu; 223 return total_cpu;
105 } 224 }
106 225
107 } // namespace 226 // Read /proc/<pid>/status and returns the value for |field|, or 0 on failure.
108 227 // Only works for fields in the form of "Field: value kB".
109 namespace base { 228 int ReadProcStatusAndGetFieldAsInt(pid_t pid, const std::string& field) {
110 229 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; 230 std::string status;
117 if (!file_util::ReadFileToString(stat_file, &status)) 231 {
118 return -1; 232 // Synchronously reading files in /proc is safe.
233 base::ThreadRestrictions::ScopedAllowIO allow_io;
234 if (!file_util::ReadFileToString(stat_file, &status))
235 return 0;
236 }
119 237
120 StringTokenizer tokenizer(status, ":\n"); 238 StringTokenizer tokenizer(status, ":\n");
121 ParsingState state = KEY_NAME; 239 ParsingState state = KEY_NAME;
122 StringPiece last_key_name; 240 base::StringPiece last_key_name;
123 while (tokenizer.GetNext()) { 241 while (tokenizer.GetNext()) {
124 switch (state) { 242 switch (state) {
125 case KEY_NAME: 243 case KEY_NAME:
126 last_key_name = tokenizer.token_piece(); 244 last_key_name = tokenizer.token_piece();
127 state = KEY_VALUE; 245 state = KEY_VALUE;
128 break; 246 break;
129 case KEY_VALUE: 247 case KEY_VALUE:
130 DCHECK(!last_key_name.empty()); 248 DCHECK(!last_key_name.empty());
131 if (last_key_name == "PPid") { 249 if (last_key_name == field) {
132 int ppid; 250 std::string value_str;
133 base::StringToInt(tokenizer.token_piece(), &ppid); 251 tokenizer.token_piece().CopyToString(&value_str);
134 return ppid; 252 std::string value_str_trimmed;
253 TrimWhitespaceASCII(value_str, TRIM_ALL, &value_str_trimmed);
254 std::vector<std::string> split_value_str;
255 base::SplitString(value_str_trimmed, ' ', &split_value_str);
256 if (split_value_str.size() != 2 || split_value_str[1] != "kB") {
257 NOTREACHED();
258 return 0;
259 }
260 int value;
261 if (!base::StringToInt(split_value_str[0], &value)) {
262 NOTREACHED();
263 return 0;
264 }
265 return value;
135 } 266 }
136 state = KEY_NAME; 267 state = KEY_NAME;
137 break; 268 break;
138 } 269 }
139 } 270 }
140 NOTREACHED(); 271 NOTREACHED();
272 return 0;
273 }
274
275 } // namespace
276
277 namespace base {
278
279 ProcessId GetParentProcessId(ProcessHandle process) {
280 ProcessId pid = ReadProcStatsAndGetFieldAsInt(process, VM_PPID);
281 if (pid)
282 return pid;
141 return -1; 283 return -1;
142 } 284 }
143 285
144 FilePath GetProcessExecutablePath(ProcessHandle process) { 286 FilePath GetProcessExecutablePath(ProcessHandle process) {
145 FilePath stat_file = GetProcPidDir(process).Append("exe"); 287 FilePath stat_file = GetProcPidDir(process).Append("exe");
146 FilePath exe_name; 288 FilePath exe_name;
147 if (!file_util::ReadSymbolicLink(stat_file, &exe_name)) { 289 if (!file_util::ReadSymbolicLink(stat_file, &exe_name)) {
148 // No such process. Happens frequently in e.g. TerminateAllChromeProcesses 290 // No such process. Happens frequently in e.g. TerminateAllChromeProcesses
149 return FilePath(); 291 return FilePath();
150 } 292 }
151 return exe_name; 293 return exe_name;
152 } 294 }
153 295
154 ProcessIterator::ProcessIterator(const ProcessFilter* filter) 296 ProcessIterator::ProcessIterator(const ProcessFilter* filter)
155 : filter_(filter) { 297 : filter_(filter) {
156 procfs_dir_ = opendir(kProcDir); 298 procfs_dir_ = opendir(kProcDir);
157 } 299 }
158 300
159 ProcessIterator::~ProcessIterator() { 301 ProcessIterator::~ProcessIterator() {
160 if (procfs_dir_) { 302 if (procfs_dir_) {
161 closedir(procfs_dir_); 303 closedir(procfs_dir_);
162 procfs_dir_ = NULL; 304 procfs_dir_ = NULL;
163 } 305 }
164 } 306 }
165 307
166 bool ProcessIterator::CheckForNextProcess() { 308 bool ProcessIterator::CheckForNextProcess() {
167 // TODO(port): skip processes owned by different UID 309 // TODO(port): skip processes owned by different UID
168 310
169 dirent* slot = 0; 311 pid_t pid = kNullProcessId;
170 const char* openparen;
171 const char* closeparen;
172 std::vector<std::string> cmd_line_args; 312 std::vector<std::string> cmd_line_args;
313 std::string stats_data;
314 std::vector<std::string> proc_stats;
173 315
174 // Arbitrarily guess that there will never be more than 200 non-process 316 // Arbitrarily guess that there will never be more than 200 non-process
175 // files in /proc. Hardy has 53. 317 // files in /proc. Hardy has 53 and Lucid has 61.
176 int skipped = 0; 318 int skipped = 0;
177 const int kSkipLimit = 200; 319 const int kSkipLimit = 200;
178 while (skipped < kSkipLimit) { 320 while (skipped < kSkipLimit) {
179 slot = readdir(procfs_dir_); 321 dirent* slot = readdir(procfs_dir_);
180 // all done looking through /proc? 322 // all done looking through /proc?
181 if (!slot) 323 if (!slot)
182 return false; 324 return false;
183 325
184 // If not a process, keep looking for one. 326 // If not a process, keep looking for one.
185 bool notprocess = false; 327 pid = ProcDirSlotToPid(slot->d_name);
186 int i; 328 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++; 329 skipped++;
195 continue; 330 continue;
196 } 331 }
197 332
198 // Read the process's command line. 333 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; 334 continue;
203 335
204 // Read the process's status. 336 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; 337 continue;
210 const char* result = fgets(buf, sizeof(buf), fp); 338 if (!ParseProcStats(stats_data, &proc_stats))
211 fclose(fp);
212 if (!result)
213 continue; 339 continue;
214 340
215 // Parse the status. It is formatted like this: 341 std::string runstate = GetProcStatsFieldAsString(proc_stats, VM_STATE);
216 // %d (%s) %c %d %d ... 342 if (runstate.size() != 1) {
217 // pid (name) runstate ppid gid 343 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; 344 continue;
224 char runstate = closeparen[2]; 345 }
225 346
226 // Is the process in 'Zombie' state, i.e. dead but waiting to be reaped? 347 // Is the process in 'Zombie' state, i.e. dead but waiting to be reaped?
227 // Allowed values: D R S T Z 348 // Allowed values: D R S T Z
228 if (runstate != 'Z') 349 if (runstate[0] != 'Z')
229 break; 350 break;
230 351
231 // Nope, it's a zombie; somebody isn't cleaning up after their children. 352 // 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.) 353 // (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. 354 // There could be a lot of zombies, can't really decrement i here.
234 } 355 }
235 if (skipped >= kSkipLimit) { 356 if (skipped >= kSkipLimit) {
236 NOTREACHED(); 357 NOTREACHED();
237 return false; 358 return false;
238 } 359 }
239 360
240 // This seems fragile. 361 entry_.pid_ = pid;
241 entry_.pid_ = atoi(slot->d_name); 362 entry_.ppid_ = GetProcStatsFieldAsInt(proc_stats, VM_PPID);
242 entry_.ppid_ = atoi(closeparen + 3); 363 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()); 364 entry_.cmd_line_args_.assign(cmd_line_args.begin(), cmd_line_args.end());
246 365
247 // TODO(port): read pid's commandline's $0, like killall does. Using the 366 // 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! 367 // short name between openparen and closeparen won't work for long names!
249 int len = closeparen - openparen - 1; 368 entry_.exe_file_ = GetProcStatsFieldAsString(proc_stats, VM_COMM);
250 entry_.exe_file_.assign(openparen + 1, len);
251 return true; 369 return true;
252 } 370 }
253 371
254 bool NamedProcessIterator::IncludeEntry() { 372 bool NamedProcessIterator::IncludeEntry() {
255 if (executable_name_ != entry().exe_file()) 373 if (executable_name_ != entry().exe_file())
256 return false; 374 return false;
257 return ProcessIterator::IncludeEntry(); 375 return ProcessIterator::IncludeEntry();
258 } 376 }
259 377
260 378
261 // static 379 // static
262 ProcessMetrics* ProcessMetrics::CreateProcessMetrics(ProcessHandle process) { 380 ProcessMetrics* ProcessMetrics::CreateProcessMetrics(ProcessHandle process) {
263 return new ProcessMetrics(process); 381 return new ProcessMetrics(process);
264 } 382 }
265 383
266 // On linux, we return vsize. 384 // On linux, we return vsize.
267 size_t ProcessMetrics::GetPagefileUsage() const { 385 size_t ProcessMetrics::GetPagefileUsage() const {
268 std::vector<std::string> proc_stats; 386 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 } 387 }
279 388
280 // On linux, we return the high water mark of vsize. 389 // On linux, we return the high water mark of vsize.
281 size_t ProcessMetrics::GetPeakPagefileUsage() const { 390 size_t ProcessMetrics::GetPeakPagefileUsage() const {
282 std::vector<std::string> proc_stats; 391 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;
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 } 392 }
293 393
294 // On linux, we return RSS. 394 // On linux, we return RSS.
295 size_t ProcessMetrics::GetWorkingSetSize() const { 395 size_t ProcessMetrics::GetWorkingSetSize() const {
296 std::vector<std::string> proc_stats; 396 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 } 397 }
307 398
308 // On linux, we return the high water mark of RSS. 399 // On linux, we return the high water mark of RSS.
309 size_t ProcessMetrics::GetPeakWorkingSetSize() const { 400 size_t ProcessMetrics::GetPeakWorkingSetSize() const {
310 std::vector<std::string> proc_stats; 401 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;
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 } 402 }
321 403
322 bool ProcessMetrics::GetMemoryBytes(size_t* private_bytes, 404 bool ProcessMetrics::GetMemoryBytes(size_t* private_bytes,
323 size_t* shared_bytes) { 405 size_t* shared_bytes) {
324 WorkingSetKBytes ws_usage; 406 WorkingSetKBytes ws_usage;
325 if (!GetWorkingSetKBytes(&ws_usage)) 407 if (!GetWorkingSetKBytes(&ws_usage))
326 return false; 408 return false;
327 409
328 if (private_bytes) 410 if (private_bytes)
329 *private_bytes = ws_usage.priv << 10; 411 *private_bytes = ws_usage.priv * 1024;
330 412
331 if (shared_bytes) 413 if (shared_bytes)
332 *shared_bytes = ws_usage.shared * 1024; 414 *shared_bytes = ws_usage.shared * 1024;
333 415
334 return true; 416 return true;
335 } 417 }
336 418
337 // Private and Shared working set sizes are obtained from /proc/<pid>/statm. 419 // Private and Shared working set sizes are obtained from /proc/<pid>/statm.
338 bool ProcessMetrics::GetWorkingSetKBytes(WorkingSetKBytes* ws_usage) const { 420 bool ProcessMetrics::GetWorkingSetKBytes(WorkingSetKBytes* ws_usage) const {
339 // Use statm instead of smaps because smaps is: 421 // Use statm instead of smaps because smaps is:
340 // a) Large and slow to parse. 422 // a) Large and slow to parse.
341 // b) Unavailable in the SUID sandbox. 423 // b) Unavailable in the SUID sandbox.
342 424
343 // First we need to get the page size, since everything is measured in pages. 425 // First we need to get the page size, since everything is measured in pages.
344 // For details, see: man 5 proc. 426 // For details, see: man 5 proc.
345 const int page_size_kb = sysconf(_SC_PAGE_SIZE) / 1024; 427 const int page_size_kb = getpagesize() / 1024;
346 if (page_size_kb <= 0) 428 if (page_size_kb <= 0)
347 return false; 429 return false;
348 430
349 std::string statm; 431 std::string statm;
350 { 432 {
351 FilePath statm_file = GetProcPidDir(process_).Append("statm"); 433 FilePath statm_file = GetProcPidDir(process_).Append("statm");
352 // Synchronously reading files in /proc is safe. 434 // Synchronously reading files in /proc is safe.
353 base::ThreadRestrictions::ScopedAllowIO allow_io; 435 base::ThreadRestrictions::ScopedAllowIO allow_io;
354 bool ret = file_util::ReadFileToString(statm_file, &statm); 436 bool ret = file_util::ReadFileToString(statm_file, &statm);
355 if (!ret || statm.length() == 0) 437 if (!ret || statm.length() == 0)
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
466 : process_(process), 548 : process_(process),
467 last_time_(0), 549 last_time_(0),
468 last_system_time_(0), 550 last_system_time_(0),
469 last_cpu_(0) { 551 last_cpu_(0) {
470 processor_count_ = base::SysInfo::NumberOfProcessors(); 552 processor_count_ = base::SysInfo::NumberOfProcessors();
471 } 553 }
472 554
473 555
474 // Exposed for testing. 556 // Exposed for testing.
475 int ParseProcStatCPU(const std::string& input) { 557 int ParseProcStatCPU(const std::string& input) {
476 // /proc/<pid>/stat contains the process name in parens. In case the 558 std::vector<std::string> proc_stats;
477 // process name itself contains parens, skip past them. 559 if (!ParseProcStats(input, &proc_stats))
478 std::string::size_type rparen = input.rfind(')');
479 if (rparen == std::string::npos)
480 return -1; 560 return -1;
481 561
482 // From here, we expect a bunch of space-separated fields, where the 562 if (proc_stats.size() <= VM_STIME)
483 // 0-indexed 11th and 12th are utime and stime. On two different machines 563 return -1;
484 // I found 42 and 39 fields, so let's just expect the ones we need. 564 int utime = GetProcStatsFieldAsInt(proc_stats, VM_UTIME);
485 std::vector<std::string> fields; 565 int stime = GetProcStatsFieldAsInt(proc_stats, VM_STIME);
486 base::SplitString(input.substr(rparen + 2), ' ', &fields); 566 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 } 567 }
495 568
496 namespace { 569 namespace {
497 570
498 // The format of /proc/meminfo is: 571 // The format of /proc/meminfo is:
499 // 572 //
500 // MemTotal: 8235324 kB 573 // MemTotal: 8235324 kB
501 // MemFree: 1628304 kB 574 // MemFree: 1628304 kB
502 // Buffers: 429596 kB 575 // Buffers: 429596 kB
503 // Cached: 4728232 kB 576 // Cached: 4728232 kB
504 // ... 577 // ...
505 const size_t kMemTotalIndex = 1; 578 const size_t kMemTotalIndex = 1;
506 const size_t kMemFreeIndex = 4; 579 const size_t kMemFreeIndex = 4;
507 const size_t kMemBuffersIndex = 7; 580 const size_t kMemBuffersIndex = 7;
508 const size_t kMemCachedIndex = 10; 581 const size_t kMemCachedIndex = 10;
509 const size_t kMemActiveAnonIndex = 22; 582 const size_t kMemActiveAnonIndex = 22;
510 const size_t kMemInactiveAnonIndex = 25; 583 const size_t kMemInactiveAnonIndex = 25;
511 584
512 } // namespace 585 } // namespace
513 586
514 bool GetSystemMemoryInfo(SystemMemoryInfoKB* meminfo) { 587 bool GetSystemMemoryInfo(SystemMemoryInfoKB* meminfo) {
515 // Synchronously reading files in /proc is safe. 588 // Synchronously reading files in /proc is safe.
516 base::ThreadRestrictions::ScopedAllowIO allow_io; 589 base::ThreadRestrictions::ScopedAllowIO allow_io;
517 590
518 // Used memory is: total - free - buffers - caches 591 // Used memory is: total - free - buffers - caches
519 FilePath meminfo_file("/proc/meminfo"); 592 FilePath meminfo_file("/proc/meminfo");
520 std::string meminfo_data; 593 std::string meminfo_data;
521 if (!file_util::ReadFileToString(meminfo_file, &meminfo_data)) { 594 if (!file_util::ReadFileToString(meminfo_file, &meminfo_data)) {
522 DLOG(WARNING) << "Failed to open /proc/meminfo."; 595 DLOG(WARNING) << "Failed to open " << meminfo_file.value();
523 return false; 596 return false;
524 } 597 }
525 std::vector<std::string> meminfo_fields; 598 std::vector<std::string> meminfo_fields;
526 SplitStringAlongWhitespace(meminfo_data, &meminfo_fields); 599 SplitStringAlongWhitespace(meminfo_data, &meminfo_fields);
527 600
528 if (meminfo_fields.size() < kMemCachedIndex) { 601 if (meminfo_fields.size() < kMemCachedIndex) {
529 DLOG(WARNING) << "Failed to parse /proc/meminfo. Only found " << 602 DLOG(WARNING) << "Failed to parse " << meminfo_file.value()
530 meminfo_fields.size() << " fields."; 603 << ". Only found " << meminfo_fields.size() << " fields.";
531 return false; 604 return false;
532 } 605 }
533 606
534 DCHECK_EQ(meminfo_fields[kMemTotalIndex-1], "MemTotal:"); 607 DCHECK_EQ(meminfo_fields[kMemTotalIndex-1], "MemTotal:");
535 DCHECK_EQ(meminfo_fields[kMemFreeIndex-1], "MemFree:"); 608 DCHECK_EQ(meminfo_fields[kMemFreeIndex-1], "MemFree:");
536 DCHECK_EQ(meminfo_fields[kMemBuffersIndex-1], "Buffers:"); 609 DCHECK_EQ(meminfo_fields[kMemBuffersIndex-1], "Buffers:");
537 DCHECK_EQ(meminfo_fields[kMemCachedIndex-1], "Cached:"); 610 DCHECK_EQ(meminfo_fields[kMemCachedIndex-1], "Cached:");
538 DCHECK_EQ(meminfo_fields[kMemActiveAnonIndex-1], "Active(anon):"); 611 DCHECK_EQ(meminfo_fields[kMemActiveAnonIndex-1], "Active(anon):");
539 DCHECK_EQ(meminfo_fields[kMemInactiveAnonIndex-1], "Inactive(anon):"); 612 DCHECK_EQ(meminfo_fields[kMemInactiveAnonIndex-1], "Inactive(anon):");
540 613
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after
716 int score_len = static_cast<int>(score_str.length()); 789 int score_len = static_cast<int>(score_str.length());
717 return (score_len == file_util::WriteFile(oom_file, 790 return (score_len == file_util::WriteFile(oom_file,
718 score_str.c_str(), 791 score_str.c_str(),
719 score_len)); 792 score_len));
720 } 793 }
721 794
722 return false; 795 return false;
723 } 796 }
724 797
725 } // namespace base 798 } // 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