Bluetooth: fix fake device classes
[chromium-blink-merge.git] / base / process_util_freebsd.cc
blob3be93c2b743a75c719b50ccd068d5ae35295b2d5
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.
5 #include "base/process_util.h"
7 #include <ctype.h>
8 #include <dirent.h>
9 #include <dlfcn.h>
10 #include <errno.h>
11 #include <fcntl.h>
12 #include <sys/sysctl.h>
13 #include <sys/time.h>
14 #include <sys/types.h>
15 #include <sys/user.h>
16 #include <sys/wait.h>
17 #include <time.h>
18 #include <unistd.h>
20 #include "base/file_util.h"
21 #include "base/logging.h"
22 #include "base/string_tokenizer.h"
23 #include "base/string_util.h"
24 #include "base/strings/string_number_conversions.h"
25 #include "base/strings/string_split.h"
26 #include "base/sys_info.h"
28 namespace base {
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 };
35 if (sysctl(mib, arraysize(mib), &info, &length, NULL, 0) < 0)
36 return -1;
38 return info.ki_ppid;
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 };
46 length = sizeof(pathname);
48 if (sysctl(mib, arraysize(mib), pathname, &length, NULL, 0) < 0 ||
49 length == 0) {
50 return FilePath();
53 return FilePath(std::string(pathname));
56 ProcessIterator::ProcessIterator(const ProcessFilter* filter)
57 : index_of_kinfo_proc_(),
58 filter_(filter) {
60 int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_UID, getuid() };
62 bool done = false;
63 int try_num = 1;
64 const int max_tries = 10;
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;
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;
94 } while (!done && (try_num++ < max_tries));
96 if (!done) {
97 LOG(ERROR) << "failed to collect the process list in a few tries";
98 kinfo_procs_.resize(0);
102 ProcessIterator::~ProcessIterator() {
105 bool ProcessIterator::CheckForNextProcess() {
106 std::string data;
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 };
113 if ((kinfo.ki_pid > 0) && (kinfo.ki_stat == SZOMB))
114 continue;
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;
122 data.resize(length);
124 if (sysctl(mib, arraysize(mib), &data[0], &length, NULL, 0) < 0) {
125 LOG(ERROR) << "failed to fetch a commandline";
126 continue;
129 std::string delimiters;
130 delimiters.push_back('\0');
131 Tokenize(data, delimiters, &entry_.cmd_line_args_);
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;
139 entry_.pid_ = kinfo.ki_pid;
140 entry_.ppid_ = kinfo.ki_ppid;
141 entry_.gid_ = kinfo.ki_pgid;
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);
151 // Start w/ the next entry next time through
152 ++index_of_kinfo_proc_;
154 return true;
156 return false;
159 bool NamedProcessIterator::IncludeEntry() {
160 if(executable_name_ != entry().exe_file())
161 return false;
163 return ProcessIterator::IncludeEntry();
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();
175 // static
176 ProcessMetrics* ProcessMetrics::CreateProcessMetrics(ProcessHandle process) {
177 return new ProcessMetrics(process);
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);
185 if (sysctl(mib, arraysize(mib), &info, &length, NULL, 0) < 0)
186 return 0;
188 return info.ki_size;
191 size_t ProcessMetrics::GetPeakPagefileUsage() const {
192 return 0;
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);
200 if (sysctl(mib, arraysize(mib), &info, &length, NULL, 0) < 0)
201 return 0;
203 return info.ki_rssize * getpagesize();
206 size_t ProcessMetrics::GetPeakWorkingSetSize() const {
207 return 0;
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;
216 if (private_bytes)
217 *private_bytes = ws_usage.priv << 10;
219 if (shared_bytes)
220 *shared_bytes = ws_usage.shared * 1024;
222 return true;
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;
234 return true;
237 bool ProcessMetrics::GetIOCounters(IoCounters* io_counters) const {
238 return false;
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);
246 struct timeval now;
247 int retval = gettimeofday(&now, NULL);
248 if (retval)
249 return 0;
251 if (sysctl(mib, arraysize(mib), &info, &length, NULL, 0) < 0)
252 return 0;
254 return (info.ki_pctcpu / FSCALE) * 100.0;
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);
262 if (sysctl(mib, arraysize(mib), &mem_total, &length, NULL, 0) < 0)
263 return 0;
265 length = sizeof(mem_free);
266 if (sysctlbyname("vm.stats.vm.v_free_count", &mem_free, &length, NULL, 0) < 0)
267 return 0;
269 length = sizeof(mem_inactive);
270 if (sysctlbyname("vm.stats.vm.v_inactive_count", &mem_inactive, &length,
271 NULL, 0) < 0) {
272 return 0;
275 pagesize = getpagesize();
277 return mem_total - (mem_free*pagesize) - (mem_inactive*pagesize);
280 void EnableTerminationOnOutOfMemory() {
281 DLOG(WARNING) << "Not feasible.";
284 void EnableTerminationOnHeapCorruption() {
285 // Nothing to do.
288 bool AdjustOOMScore(ProcessId process, int score) {
289 NOTIMPLEMENTED();
290 return false;
293 } // namespace base