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