Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 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/param.h> | |
| 16 #include <sys/sysctl.h> | |
| 17 #include <sys/user.h> | |
| 18 #include <time.h> | |
| 19 #include <unistd.h> | |
| 20 | |
| 21 #include "base/file_util.h" | |
| 22 #include "base/logging.h" | |
| 23 #include "base/string_number_conversions.h" | |
| 24 #include "base/string_split.h" | |
| 25 #include "base/string_tokenizer.h" | |
| 26 #include "base/string_util.h" | |
| 27 #include "base/sys_info.h" | |
| 28 #include "base/threading/thread_restrictions.h" | |
| 29 | |
| 30 namespace base { | |
| 31 | |
| 32 ProcessId GetParentProcessId(ProcessHandle process) { | |
| 33 struct kinfo_proc info; | |
| 34 int mib[6]; | |
|
Mark Mentovai
2011/10/12 14:58:02
int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID,
Robert Nagy
2011/10/12 17:03:10
Fixed this and all the others.
Now instead of har
| |
| 35 size_t info_size = sizeof(info); | |
| 36 | |
| 37 mib[0] = CTL_KERN; | |
| 38 mib[1] = KERN_PROC; | |
| 39 mib[2] = KERN_PROC_PID; | |
| 40 mib[3] = process; | |
| 41 mib[4] = info_size; | |
|
Mark Mentovai
2011/10/12 14:58:02
Curious what this is used for, since info_size is
| |
| 42 mib[5] = 400; /* XXX */ | |
|
Mark Mentovai
2011/10/12 14:58:02
You’re going to need to do a little better than XX
| |
| 43 | |
| 44 if (sysctl(mib, 6, &info, &info_size, NULL, 0) < 0) | |
|
Mark Mentovai
2011/10/12 14:58:02
Not 6, but arraysize(mib).
| |
| 45 return -1; | |
| 46 | |
| 47 return info.p_ppid; | |
| 48 } | |
| 49 | |
| 50 FilePath GetProcessExecutablePath(ProcessHandle process) { | |
| 51 return FilePath(std::string("/usr/local/chrome/chrome")); | |
|
Mark Mentovai
2011/10/12 14:58:02
I understand that you don’t know how to get this o
Robert Nagy
2011/10/12 17:03:10
There is no way on OpenBSD to get this from a runn
Mark Mentovai
2011/10/12 18:42:56
Robert Nagy wrote:
| |
| 52 } | |
| 53 | |
| 54 ProcessIterator::ProcessIterator(const ProcessFilter* filter) | |
| 55 : index_of_kinfo_proc_(), | |
|
Mark Mentovai
2011/10/12 14:58:02
Use proper spacing to format the initializer.
htt
| |
| 56 filter_(filter) { | |
| 57 | |
| 58 int mib[6]; | |
|
Mark Mentovai
2011/10/12 14:58:02
int mib[] = { … }; (as above).
| |
| 59 | |
| 60 mib[0] = CTL_KERN; | |
| 61 mib[1] = KERN_PROC; | |
| 62 mib[2] = KERN_PROC_UID; | |
| 63 mib[3] = getuid(); | |
| 64 mib[4] = sizeof(struct kinfo_proc); | |
| 65 mib[5] = 0; | |
|
Mark Mentovai
2011/10/12 14:58:02
What’s this?
| |
| 66 | |
| 67 bool done = false; | |
| 68 int try_num = 1; | |
| 69 const int max_tries = 10; | |
| 70 | |
| 71 do { | |
| 72 size_t len = 0; | |
| 73 if (sysctl(mib, 6, NULL, &len, NULL, 0) <0 ){ | |
|
Mark Mentovai
2011/10/12 14:58:02
< 0)
not
<0 )
Mark Mentovai
2011/10/12 14:58:02
arraysize(mib), not 6.
| |
| 74 LOG(ERROR) << "failed to get the size needed for the process list"; | |
| 75 kinfo_procs_.resize(0); | |
| 76 done = true; | |
| 77 } else { | |
| 78 size_t num_of_kinfo_proc = len / sizeof(struct kinfo_proc); | |
| 79 // Leave some spare room for process table growth (more could show up | |
| 80 // between when we check and now) | |
| 81 num_of_kinfo_proc += 16; | |
| 82 kinfo_procs_.resize(num_of_kinfo_proc); | |
| 83 len = num_of_kinfo_proc * sizeof(struct kinfo_proc); | |
| 84 if (sysctl(mib, 6, &kinfo_procs_[0], &len, NULL, 0) <0) { | |
|
Mark Mentovai
2011/10/12 14:58:02
Space between < and 0.
| |
| 85 // If we get a mem error, it just means we need a bigger buffer, so | |
| 86 // loop around again. Anything else is a real error and give up. | |
| 87 if (errno != ENOMEM) { | |
| 88 LOG(ERROR) << "failed to get the process list"; | |
| 89 kinfo_procs_.resize(0); | |
| 90 done = true; | |
| 91 } | |
| 92 } else { | |
| 93 // Got the list, just make sure we're sized exactly right | |
| 94 size_t num_of_kinfo_proc = len / sizeof(struct kinfo_proc); | |
| 95 kinfo_procs_.resize(num_of_kinfo_proc); | |
| 96 done = true; | |
| 97 } | |
| 98 } | |
| 99 } while (!done && (try_num++ < max_tries)); | |
| 100 | |
| 101 if (!done) { | |
| 102 LOG(ERROR) << "failed to collect the process list in a few tries"; | |
| 103 kinfo_procs_.resize(0); | |
| 104 } | |
| 105 } | |
| 106 | |
| 107 ProcessIterator::~ProcessIterator() { | |
| 108 } | |
| 109 | |
| 110 bool ProcessIterator::CheckForNextProcess() { | |
| 111 std::string data; | |
| 112 | |
| 113 for (; index_of_kinfo_proc_ < kinfo_procs_.size(); ++ index_of_kinfo_proc_) { | |
| 114 int mib[3]; | |
| 115 size_t len; | |
| 116 struct kinfo_proc kinfo = kinfo_procs_[index_of_kinfo_proc_]; | |
| 117 | |
| 118 if ((kinfo.p_pid > 0) && (kinfo.p_stat == SZOMB)) | |
| 119 continue; | |
| 120 | |
| 121 mib[0] = CTL_KERN; | |
|
Mark Mentovai
2011/10/12 14:58:02
And again.
| |
| 122 mib[1] = KERN_PROC_ARGS; | |
| 123 mib[2] = kinfo.p_pid; | |
| 124 | |
| 125 len = 0; | |
| 126 if (sysctl(mib, 3, NULL, &len, NULL, 0) < 0) { | |
|
Mark Mentovai
2011/10/12 14:58:02
And again, arraysize, not a constant.
I don’t und
Robert Nagy
2011/10/12 17:03:10
I've copied a clean Mac implementation and modifie
| |
| 127 LOG(ERROR) << "failed to figure out the buffer size for a command line"; | |
| 128 continue; | |
| 129 } | |
| 130 | |
| 131 data.resize(len); | |
| 132 | |
| 133 if (sysctl(mib, 3, &data[0], &len, NULL, 0) < 0) { | |
|
Mark Mentovai
2011/10/12 14:58:02
Again.
| |
| 134 LOG(ERROR) << "failed to fetch a commandline"; | |
|
Mark Mentovai
2011/10/12 14:58:02
I’m stopping reading this file here because the sa
| |
| 135 continue; | |
| 136 } | |
| 137 | |
| 138 std::string delimiters; | |
| 139 delimiters.push_back('\0'); | |
| 140 Tokenize(data, delimiters, &entry_.cmd_line_args_); | |
| 141 | |
| 142 size_t exec_name_end = data.find('\0'); | |
| 143 if (exec_name_end == std::string::npos) { | |
| 144 LOG(ERROR) << "command line data didn't match expected format"; | |
| 145 continue; | |
| 146 } | |
| 147 | |
| 148 entry_.pid_ = kinfo.p_pid; | |
| 149 entry_.ppid_ = kinfo.p_ppid; | |
| 150 entry_.gid_ = kinfo.p__pgid; | |
| 151 | |
| 152 size_t last_slash = data.rfind('/', exec_name_end); | |
| 153 if (last_slash == std::string::npos) | |
| 154 entry_.exe_file_.assign(data, 0, exec_name_end); | |
| 155 else | |
| 156 entry_.exe_file_.assign(data, last_slash + 1, | |
| 157 exec_name_end - last_slash - 1); | |
| 158 | |
| 159 // Start w/ the next entry next time through | |
| 160 ++index_of_kinfo_proc_; | |
| 161 | |
| 162 return true; | |
| 163 } | |
| 164 return false; | |
| 165 } | |
| 166 | |
| 167 bool NamedProcessIterator::IncludeEntry() { | |
| 168 return (executable_name_ == entry().exe_file() && | |
| 169 ProcessIterator::IncludeEntry()); | |
| 170 } | |
| 171 | |
| 172 | |
| 173 ProcessMetrics::ProcessMetrics(ProcessHandle process) | |
| 174 : process_(process), | |
| 175 last_time_(0), | |
| 176 last_system_time_(0), | |
| 177 last_cpu_(0) { | |
| 178 | |
| 179 processor_count_ = base::SysInfo::NumberOfProcessors(); | |
| 180 } | |
| 181 | |
| 182 // static | |
| 183 ProcessMetrics* ProcessMetrics::CreateProcessMetrics(ProcessHandle process) { | |
| 184 return new ProcessMetrics(process); | |
| 185 } | |
| 186 | |
| 187 size_t ProcessMetrics::GetPagefileUsage() const { | |
| 188 struct kinfo_proc info; | |
| 189 int mib[6]; | |
| 190 size_t info_size = sizeof(info); | |
| 191 | |
| 192 mib[0] = CTL_KERN; | |
| 193 mib[1] = KERN_PROC; | |
| 194 mib[2] = KERN_PROC_PID; | |
| 195 mib[3] = process_; | |
| 196 mib[4] = info_size; | |
| 197 mib[5] = 400; /* XXX */ | |
| 198 | |
| 199 if (sysctl(mib, 6, &info, &info_size, NULL, 0) < 0) | |
| 200 return 0; | |
| 201 | |
| 202 return (info.p_vm_tsize + info.p_vm_dsize + info.p_vm_ssize); | |
| 203 } | |
| 204 | |
| 205 size_t ProcessMetrics::GetPeakPagefileUsage() const { | |
| 206 | |
| 207 return 0; | |
| 208 } | |
| 209 | |
| 210 size_t ProcessMetrics::GetWorkingSetSize() const { | |
| 211 struct kinfo_proc info; | |
| 212 int mib[6]; | |
| 213 size_t info_size = sizeof(info); | |
| 214 | |
| 215 mib[0] = CTL_KERN; | |
| 216 mib[1] = KERN_PROC; | |
| 217 mib[2] = KERN_PROC_PID; | |
| 218 mib[3] = process_; | |
| 219 mib[4] = info_size; | |
| 220 mib[5] = 400; /* XXX */ | |
| 221 | |
| 222 if (sysctl(mib, 6, &info, &info_size, NULL, 0) < 0) | |
| 223 return 0; | |
| 224 | |
| 225 return info.p_vm_rssize * getpagesize(); | |
| 226 } | |
| 227 | |
| 228 size_t ProcessMetrics::GetPeakWorkingSetSize() const { | |
| 229 | |
| 230 return 0; | |
| 231 } | |
| 232 | |
| 233 bool ProcessMetrics::GetMemoryBytes(size_t* private_bytes, | |
| 234 size_t* shared_bytes) { | |
| 235 WorkingSetKBytes ws_usage; | |
| 236 | |
| 237 if (!GetWorkingSetKBytes(&ws_usage)) | |
| 238 return false; | |
| 239 | |
| 240 if (private_bytes) | |
| 241 *private_bytes = ws_usage.priv << 10; | |
| 242 | |
| 243 if (shared_bytes) | |
| 244 *shared_bytes = ws_usage.shared * 1024; | |
| 245 | |
| 246 return true; | |
| 247 } | |
| 248 | |
| 249 bool ProcessMetrics::GetWorkingSetKBytes(WorkingSetKBytes* ws_usage) const { | |
| 250 // TODO(bapt) be sure we can't be precise | |
| 251 size_t priv = GetWorkingSetSize(); | |
| 252 if (!priv) | |
| 253 return false; | |
| 254 ws_usage->priv = priv / 1024; | |
| 255 ws_usage->shareable = 0; | |
| 256 ws_usage->shared = 0; | |
| 257 | |
| 258 return true; | |
| 259 } | |
| 260 | |
| 261 bool ProcessMetrics::GetIOCounters(IoCounters* io_counters) const { | |
| 262 return false; | |
| 263 } | |
| 264 | |
| 265 static int GetProcessCPU(pid_t pid) { | |
| 266 struct kinfo_proc info; | |
| 267 int mib[6]; | |
| 268 size_t info_size = sizeof(info); | |
| 269 | |
| 270 mib[0] = CTL_KERN; | |
| 271 mib[1] = KERN_PROC; | |
| 272 mib[2] = KERN_PROC_PID; | |
| 273 mib[3] = pid; | |
| 274 mib[4] = info_size; | |
| 275 mib[5] = 400; /* XXX */ | |
| 276 | |
| 277 if (sysctl(mib, 6, &info, &info_size, NULL, 0) < 0) | |
| 278 return 0; | |
| 279 | |
| 280 return info.p_pctcpu; | |
| 281 } | |
| 282 | |
| 283 double ProcessMetrics::GetCPUUsage() { | |
| 284 struct timeval now; | |
| 285 | |
| 286 int retval = gettimeofday(&now, NULL); | |
| 287 if (retval) | |
| 288 return 0; | |
| 289 | |
| 290 int64 time = TimeValToMicroseconds(now); | |
| 291 | |
| 292 if (last_time_ == 0) { | |
| 293 // First call, just set the last values. | |
| 294 last_time_ = time; | |
| 295 last_cpu_ = GetProcessCPU(process_); | |
| 296 return 0; | |
| 297 } | |
| 298 | |
| 299 int64 time_delta = time - last_time_; | |
| 300 DCHECK_NE(time_delta, 0); | |
| 301 | |
| 302 if (time_delta == 0) | |
| 303 return 0; | |
| 304 | |
| 305 int cpu = GetProcessCPU(process_); | |
| 306 | |
| 307 last_time_ = time; | |
| 308 last_cpu_ = cpu; | |
| 309 | |
| 310 double percentage = static_cast<double>((cpu * 100.0) / FSCALE); | |
| 311 | |
| 312 return percentage; | |
| 313 } | |
| 314 | |
| 315 size_t GetSystemCommitCharge() { | |
| 316 int mib[2], pagesize; | |
| 317 struct vmtotal vmtotal; | |
| 318 unsigned long mem_total, mem_free, mem_inactive; | |
| 319 size_t len = sizeof(vmtotal); | |
| 320 | |
| 321 mib[0] = CTL_VM; | |
| 322 mib[1] = VM_METER; | |
| 323 | |
| 324 if (sysctl(mib, 2, &vmtotal, &len, NULL, 0) < 0) | |
| 325 return 0; | |
| 326 | |
| 327 mem_total = vmtotal.t_vm; | |
| 328 mem_free = vmtotal.t_free; | |
| 329 mem_inactive = vmtotal.t_vm - vmtotal.t_avm; | |
| 330 | |
| 331 pagesize = getpagesize(); | |
| 332 | |
| 333 return mem_total - (mem_free*pagesize) - (mem_inactive*pagesize); | |
| 334 } | |
| 335 | |
| 336 void EnableTerminationOnOutOfMemory() { | |
| 337 return; | |
|
Mark Mentovai
2011/10/12 14:58:02
No need to return from a void function.
| |
| 338 } | |
| 339 void EnableTerminationOnHeapCorruption() { | |
|
Mark Mentovai
2011/10/12 14:58:02
Blank line to separate functions.
| |
| 340 return; | |
|
Mark Mentovai
2011/10/12 14:58:02
No need to return from a void function.
| |
| 341 } | |
| 342 } | |
|
Mark Mentovai
2011/10/12 14:58:02
Blank line before.
This should be
} // namespac
| |
| OLD | NEW |