[email protected] | af82436 | 2011-12-04 14:19:41 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "base/process_util.h" |
| 6 | |
| 7 | #include <ctype.h> |
| 8 | #include <dirent.h> |
| 9 | #include <dlfcn.h> |
| 10 | #include <errno.h> |
| 11 | #include <fcntl.h> |
| 12 | #include <sys/time.h> |
| 13 | #include <sys/types.h> |
| 14 | #include <sys/wait.h> |
| 15 | #include <sys/sysctl.h> |
| 16 | #include <sys/user.h> |
| 17 | #include <time.h> |
| 18 | #include <unistd.h> |
| 19 | |
| 20 | #include "base/file_util.h" |
| 21 | #include "base/logging.h" |
| 22 | #include "base/string_number_conversions.h" |
| 23 | #include "base/string_split.h" |
| 24 | #include "base/string_tokenizer.h" |
| 25 | #include "base/string_util.h" |
| 26 | #include "base/sys_info.h" |
| 27 | |
| 28 | namespace base { |
| 29 | |
| 30 | ProcessId GetParentProcessId(ProcessHandle process) { |
| 31 | struct kinfo_proc info; |
| 32 | size_t length; |
| 33 | int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, process }; |
| 34 | |
| 35 | if (sysctl(mib, arraysize(mib), &info, &length, NULL, 0) < 0) |
| 36 | return -1; |
| 37 | |
| 38 | return info.ki_ppid; |
| 39 | } |
| 40 | |
| 41 | FilePath GetProcessExecutablePath(ProcessHandle process) { |
| 42 | char pathname[PATH_MAX]; |
| 43 | size_t length; |
| 44 | int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, process }; |
| 45 | |
| 46 | length = sizeof(pathname); |
| 47 | |
| 48 | if (sysctl(mib, arraysize(mib), pathname, &length, NULL, 0) < 0 || |
| 49 | length == 0) { |
| 50 | return FilePath(); |
| 51 | } |
| 52 | |
| 53 | return FilePath(std::string(pathname)); |
| 54 | } |
| 55 | |
| 56 | ProcessIterator::ProcessIterator(const ProcessFilter* filter) |
| 57 | : index_of_kinfo_proc_(), |
| 58 | filter_(filter) { |
| 59 | |
| 60 | int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_UID, getuid() }; |
| 61 | |
| 62 | bool done = false; |
| 63 | int try_num = 1; |
| 64 | const int max_tries = 10; |
| 65 | |
| 66 | do { |
| 67 | size_t len = 0; |
| 68 | if (sysctl(mib, arraysize(mib), NULL, &len, NULL, 0) <0 ){ |
| 69 | LOG(ERROR) << "failed to get the size needed for the process list"; |
| 70 | kinfo_procs_.resize(0); |
| 71 | done = true; |
| 72 | } else { |
| 73 | size_t num_of_kinfo_proc = len / sizeof(struct kinfo_proc); |
| 74 | // Leave some spare room for process table growth (more could show up |
| 75 | // between when we check and now) |
| 76 | num_of_kinfo_proc += 16; |
| 77 | kinfo_procs_.resize(num_of_kinfo_proc); |
| 78 | len = num_of_kinfo_proc * sizeof(struct kinfo_proc); |
| 79 | if (sysctl(mib, arraysize(mib), &kinfo_procs_[0], &len, NULL, 0) <0) { |
| 80 | // If we get a mem error, it just means we need a bigger buffer, so |
| 81 | // loop around again. Anything else is a real error and give up. |
| 82 | if (errno != ENOMEM) { |
| 83 | LOG(ERROR) << "failed to get the process list"; |
| 84 | kinfo_procs_.resize(0); |
| 85 | done = true; |
| 86 | } |
| 87 | } else { |
| 88 | // Got the list, just make sure we're sized exactly right |
| 89 | size_t num_of_kinfo_proc = len / sizeof(struct kinfo_proc); |
| 90 | kinfo_procs_.resize(num_of_kinfo_proc); |
| 91 | done = true; |
| 92 | } |
| 93 | } |
| 94 | } while (!done && (try_num++ < max_tries)); |
| 95 | |
| 96 | if (!done) { |
| 97 | LOG(ERROR) << "failed to collect the process list in a few tries"; |
| 98 | kinfo_procs_.resize(0); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | ProcessIterator::~ProcessIterator() { |
| 103 | } |
| 104 | |
| 105 | bool ProcessIterator::CheckForNextProcess() { |
| 106 | std::string data; |
| 107 | |
| 108 | for (; index_of_kinfo_proc_ < kinfo_procs_.size(); ++ index_of_kinfo_proc_) { |
| 109 | size_t length; |
| 110 | struct kinfo_proc kinfo = kinfo_procs_[index_of_kinfo_proc_]; |
| 111 | int mib[] = { CTL_KERN, KERN_PROC_ARGS, kinfo.ki_pid }; |
| 112 | |
| 113 | if ((kinfo.ki_pid > 0) && (kinfo.ki_stat == SZOMB)) |
| 114 | continue; |
| 115 | |
| 116 | length = 0; |
| 117 | if (sysctl(mib, arraysize(mib), NULL, &length, NULL, 0) < 0) { |
| 118 | LOG(ERROR) << "failed to figure out the buffer size for a command line"; |
| 119 | continue; |
| 120 | } |
| 121 | |
| 122 | data.resize(length); |
| 123 | |
| 124 | if (sysctl(mib, arraysize(mib), &data[0], &length, NULL, 0) < 0) { |
| 125 | LOG(ERROR) << "failed to fetch a commandline"; |
| 126 | continue; |
| 127 | } |
| 128 | |
| 129 | std::string delimiters; |
| 130 | delimiters.push_back('\0'); |
| 131 | Tokenize(data, delimiters, &entry_.cmd_line_args_); |
| 132 | |
| 133 | size_t exec_name_end = data.find('\0'); |
| 134 | if (exec_name_end == std::string::npos) { |
| 135 | LOG(ERROR) << "command line data didn't match expected format"; |
| 136 | continue; |
| 137 | } |
| 138 | |
| 139 | entry_.pid_ = kinfo.ki_pid; |
| 140 | entry_.ppid_ = kinfo.ki_ppid; |
| 141 | entry_.gid_ = kinfo.ki_pgid; |
| 142 | |
| 143 | size_t last_slash = data.rfind('/', exec_name_end); |
| 144 | if (last_slash == std::string::npos) { |
| 145 | entry_.exe_file_.assign(data, 0, exec_name_end); |
| 146 | } else { |
| 147 | entry_.exe_file_.assign(data, last_slash + 1, |
| 148 | exec_name_end - last_slash - 1); |
| 149 | } |
| 150 | |
| 151 | // Start w/ the next entry next time through |
| 152 | ++index_of_kinfo_proc_; |
| 153 | |
| 154 | return true; |
| 155 | } |
| 156 | return false; |
| 157 | } |
| 158 | |
| 159 | bool NamedProcessIterator::IncludeEntry() { |
| 160 | if(executable_name_ != entry().exe_file()) |
| 161 | return false; |
| 162 | |
| 163 | return ProcessIterator::IncludeEntry(); |
| 164 | } |
| 165 | |
| 166 | |
| 167 | ProcessMetrics::ProcessMetrics(ProcessHandle process) |
| 168 | : process_(process), |
| 169 | last_time_(0), |
| 170 | last_system_time_(0), |
| 171 | last_cpu_(0) { |
| 172 | processor_count_ = base::SysInfo::NumberOfProcessors(); |
| 173 | } |
| 174 | |
| 175 | // static |
| 176 | ProcessMetrics* ProcessMetrics::CreateProcessMetrics(ProcessHandle process) { |
| 177 | return new ProcessMetrics(process); |
| 178 | } |
| 179 | |
| 180 | size_t ProcessMetrics::GetPagefileUsage() const { |
| 181 | struct kinfo_proc info; |
| 182 | int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, process_ }; |
| 183 | size_t length = sizeof(info); |
| 184 | |
| 185 | if (sysctl(mib, arraysize(mib), &info, &length, NULL, 0) < 0) |
| 186 | return 0; |
| 187 | |
| 188 | return info.ki_size; |
| 189 | } |
| 190 | |
| 191 | size_t ProcessMetrics::GetPeakPagefileUsage() const { |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | size_t ProcessMetrics::GetWorkingSetSize() const { |
| 196 | struct kinfo_proc info; |
| 197 | int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, process_ }; |
| 198 | size_t length = sizeof(info); |
| 199 | |
| 200 | if (sysctl(mib, arraysize(mib), &info, &length, NULL, 0) < 0) |
| 201 | return 0; |
| 202 | |
| 203 | return info.ki_rssize * getpagesize(); |
| 204 | } |
| 205 | |
| 206 | size_t ProcessMetrics::GetPeakWorkingSetSize() const { |
| 207 | return 0; |
| 208 | } |
| 209 | |
| 210 | bool ProcessMetrics::GetMemoryBytes(size_t* private_bytes, |
| 211 | size_t* shared_bytes) { |
| 212 | WorkingSetKBytes ws_usage; |
| 213 | if (!GetWorkingSetKBytes(&ws_usage)) |
| 214 | return false; |
| 215 | |
| 216 | if (private_bytes) |
| 217 | *private_bytes = ws_usage.priv << 10; |
| 218 | |
| 219 | if (shared_bytes) |
| 220 | *shared_bytes = ws_usage.shared * 1024; |
| 221 | |
| 222 | return true; |
| 223 | } |
| 224 | |
| 225 | bool ProcessMetrics::GetWorkingSetKBytes(WorkingSetKBytes* ws_usage) const { |
| 226 | // TODO(bapt) be sure we can't be precise |
| 227 | size_t priv = GetWorkingSetSize(); |
| 228 | if (!priv) |
| 229 | return false; |
| 230 | ws_usage->priv = priv / 1024; |
| 231 | ws_usage->shareable = 0; |
| 232 | ws_usage->shared = 0; |
| 233 | |
| 234 | return true; |
| 235 | } |
| 236 | |
| 237 | bool ProcessMetrics::GetIOCounters(IoCounters* io_counters) const { |
| 238 | return false; |
| 239 | } |
| 240 | |
| 241 | double ProcessMetrics::GetCPUUsage() { |
| 242 | struct kinfo_proc info; |
| 243 | int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, process_ }; |
| 244 | size_t length = sizeof(info); |
| 245 | |
| 246 | struct timeval now; |
| 247 | int retval = gettimeofday(&now, NULL); |
| 248 | if (retval) |
| 249 | return 0; |
| 250 | |
| 251 | if (sysctl(mib, arraysize(mib), &info, &length, NULL, 0) < 0) |
| 252 | return 0; |
| 253 | |
| 254 | return (info.ki_pctcpu / FSCALE) * 100.0; |
| 255 | } |
| 256 | |
| 257 | size_t GetSystemCommitCharge() { |
| 258 | int mib[2], pagesize; |
| 259 | unsigned long mem_total, mem_free, mem_inactive; |
| 260 | size_t length = sizeof(mem_total); |
| 261 | |
| 262 | if (sysctl(mib, arraysize(mib), &mem_total, &length, NULL, 0) < 0) |
| 263 | return 0; |
| 264 | |
| 265 | length = sizeof(mem_free); |
| 266 | if (sysctlbyname("vm.stats.vm.v_free_count", &mem_free, &length, NULL, 0) < 0) |
| 267 | return 0; |
| 268 | |
| 269 | length = sizeof(mem_inactive); |
| 270 | if (sysctlbyname("vm.stats.vm.v_inactive_count", &mem_inactive, &length, |
| 271 | NULL, 0) < 0) { |
| 272 | return 0; |
| 273 | } |
| 274 | |
| 275 | pagesize = getpagesize(); |
| 276 | |
| 277 | return mem_total - (mem_free*pagesize) - (mem_inactive*pagesize); |
| 278 | } |
| 279 | |
| 280 | void EnableTerminationOnOutOfMemory() { |
| 281 | DLOG(WARNING) << "Not feasible."; |
| 282 | } |
| 283 | |
| 284 | void EnableTerminationOnHeapCorruption() { |
| 285 | // Nothing to do. |
| 286 | } |
| 287 | |
| 288 | bool AdjustOOMScore(ProcessId process, int score) { |
| 289 | NOTIMPLEMENTED(); |
| 290 | return false; |
| 291 | } |
| 292 | |
| 293 | } // namespace base |