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

Side by Side Diff: base/process_util_openbsd.cc

Issue 8228005: openbsd and freebsd fixes for base (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Add myself to the AUTHORS Created 9 years, 2 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
« no previous file with comments | « base/process_util.h ('k') | base/process_util_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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/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 size_t length;
35 int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, process,
36 sizeof(struct kinfo_proc), 0 };
37
38 if (sysctl(mib, arraysize(mib), NULL, &length, NULL, 0) < 0)
39 return -1;
40
41 mib[5] = (length / sizeof(struct kinfo_proc));
42
43 if (sysctl(mib, arraysize(mib), &info, &length, NULL, 0) < 0)
44 return -1;
45
46 return info.p_ppid;
47 }
48
49 FilePath GetProcessExecutablePath(ProcessHandle process) {
50 return FilePath(std::string("/usr/local/chrome/chrome"));
51 }
52
53 ProcessIterator::ProcessIterator(const ProcessFilter* filter)
54 : index_of_kinfo_proc_(),
55 filter_(filter) {
56
57 int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_UID, getuid(),
58 sizeof(struct kinfo_proc), 0 };
59
60 bool done = false;
61 int try_num = 1;
62 const int max_tries = 10;
63
64 do {
65 size_t len = 0;
66 if (sysctl(mib, arraysize(mib), NULL, &len, NULL, 0) < 0) {
67 LOG(ERROR) << "failed to get the size needed for the process list";
68 kinfo_procs_.resize(0);
69 done = true;
70 } else {
71 size_t num_of_kinfo_proc = len / sizeof(struct kinfo_proc);
72 // Leave some spare room for process table growth (more could show up
73 // between when we check and now)
74 num_of_kinfo_proc += 16;
75 kinfo_procs_.resize(num_of_kinfo_proc);
76 len = num_of_kinfo_proc * sizeof(struct kinfo_proc);
77 if (sysctl(mib, arraysize(mib), &kinfo_procs_[0], &len, NULL, 0) < 0) {
78 // If we get a mem error, it just means we need a bigger buffer, so
79 // loop around again. Anything else is a real error and give up.
80 if (errno != ENOMEM) {
81 LOG(ERROR) << "failed to get the process list";
82 kinfo_procs_.resize(0);
83 done = true;
84 }
85 } else {
86 // Got the list, just make sure we're sized exactly right
87 size_t num_of_kinfo_proc = len / sizeof(struct kinfo_proc);
88 kinfo_procs_.resize(num_of_kinfo_proc);
89 done = true;
90 }
91 }
92 } while (!done && (try_num++ < max_tries));
93
94 if (!done) {
95 LOG(ERROR) << "failed to collect the process list in a few tries";
96 kinfo_procs_.resize(0);
97 }
98 }
99
100 ProcessIterator::~ProcessIterator() {
101 }
102
103 bool ProcessIterator::CheckForNextProcess() {
104 std::string data;
105 for (; index_of_kinfo_proc_ < kinfo_procs_.size(); ++index_of_kinfo_proc_) {
106 kinfo_proc& kinfo = kinfo_procs_[index_of_kinfo_proc_];
107
108 // Skip processes just awaiting collection
109 if ((kinfo.p_pid > 0) && (kinfo.p_stat == SZOMB))
110 continue;
111
112 int mib[] = { CTL_KERN, KERN_PROC_ARGS, kinfo.p_pid };
113
114 // Find out what size buffer we need.
115 size_t data_len = 0;
116 if (sysctl(mib, arraysize(mib), NULL, &data_len, NULL, 0) < 0) {
117 DVPLOG(1) << "failed to figure out the buffer size for a commandline";
118 continue;
119 }
120
121 data.resize(data_len);
122 if (sysctl(mib, arraysize(mib), &data[0], &data_len, NULL, 0) < 0) {
123 DVPLOG(1) << "failed to fetch a commandline";
124 continue;
125 }
126
127 // |data| contains all the command line parameters of the process, separated
128 // by blocks of one or more null characters. We tokenize |data| into a
129 // vector of strings using '\0' as a delimiter and populate
130 // |entry_.cmd_line_args_|.
131 std::string delimiters;
132 delimiters.push_back('\0');
133 Tokenize(data, delimiters, &entry_.cmd_line_args_);
134
135 // |data| starts with the full executable path followed by a null character.
136 // We search for the first instance of '\0' and extract everything before it
137 // to populate |entry_.exe_file_|.
138 size_t exec_name_end = data.find('\0');
139 if (exec_name_end == std::string::npos) {
140 LOG(ERROR) << "command line data didn't match expected format";
141 continue;
142 }
143
144 entry_.pid_ = kinfo.p_pid;
145 entry_.ppid_ = kinfo.p_ppid;
146 entry_.gid_ = kinfo.p__pgid;
147 size_t last_slash = data.rfind('/', exec_name_end);
148 if (last_slash == std::string::npos)
149 entry_.exe_file_.assign(data, 0, exec_name_end);
150 else
151 entry_.exe_file_.assign(data, last_slash + 1,
152 exec_name_end - last_slash - 1);
153 // Start w/ the next entry next time through
154 ++index_of_kinfo_proc_;
155 // Done
156 return true;
157 }
158 return false;
159 }
160
161 bool NamedProcessIterator::IncludeEntry() {
162 return (executable_name_ == entry().exe_file() &&
163 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
173 processor_count_ = base::SysInfo::NumberOfProcessors();
174 }
175
176 // static
177 ProcessMetrics* ProcessMetrics::CreateProcessMetrics(ProcessHandle process) {
178 return new ProcessMetrics(process);
179 }
180
181 size_t ProcessMetrics::GetPagefileUsage() const {
182 struct kinfo_proc info;
183 size_t length;
184 int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, process_,
185 sizeof(struct kinfo_proc), 0 };
186
187 if (sysctl(mib, arraysize(mib), NULL, &length, NULL, 0) < 0)
188 return -1;
189
190 mib[5] = (length / sizeof(struct kinfo_proc));
191
192 if (sysctl(mib, arraysize(mib), &info, &length, NULL, 0) < 0)
193 return -1;
194
195 return (info.p_vm_tsize + info.p_vm_dsize + info.p_vm_ssize);
196 }
197
198 size_t ProcessMetrics::GetPeakPagefileUsage() const {
199
200 return 0;
201 }
202
203 size_t ProcessMetrics::GetWorkingSetSize() const {
204 struct kinfo_proc info;
205 size_t length;
206 int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, process_,
207 sizeof(struct kinfo_proc), 0 };
208
209 if (sysctl(mib, arraysize(mib), NULL, &length, NULL, 0) < 0)
210 return -1;
211
212 mib[5] = (length / sizeof(struct kinfo_proc));
213
214 if (sysctl(mib, arraysize(mib), &info, &length, NULL, 0) < 0)
215 return -1;
216
217 return info.p_vm_rssize * getpagesize();
218 }
219
220 size_t ProcessMetrics::GetPeakWorkingSetSize() const {
221
222 return 0;
223 }
224
225 bool ProcessMetrics::GetMemoryBytes(size_t* private_bytes,
226 size_t* shared_bytes) {
227 WorkingSetKBytes ws_usage;
228
229 if (!GetWorkingSetKBytes(&ws_usage))
230 return false;
231
232 if (private_bytes)
233 *private_bytes = ws_usage.priv << 10;
234
235 if (shared_bytes)
236 *shared_bytes = ws_usage.shared * 1024;
237
238 return true;
239 }
240
241 bool ProcessMetrics::GetWorkingSetKBytes(WorkingSetKBytes* ws_usage) const {
242 // TODO(bapt) be sure we can't be precise
243 size_t priv = GetWorkingSetSize();
244 if (!priv)
245 return false;
246 ws_usage->priv = priv / 1024;
247 ws_usage->shareable = 0;
248 ws_usage->shared = 0;
249
250 return true;
251 }
252
253 bool ProcessMetrics::GetIOCounters(IoCounters* io_counters) const {
254 return false;
255 }
256
257 static int GetProcessCPU(pid_t pid) {
258 struct kinfo_proc info;
259 size_t length;
260 int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, pid,
261 sizeof(struct kinfo_proc), 0 };
262
263 if (sysctl(mib, arraysize(mib), NULL, &length, NULL, 0) < 0)
264 return -1;
265
266 mib[5] = (length / sizeof(struct kinfo_proc));
267
268 if (sysctl(mib, arraysize(mib), &info, &length, NULL, 0) < 0)
269 return 0;
270
271 return info.p_pctcpu;
272 }
273
274 double ProcessMetrics::GetCPUUsage() {
275 struct timeval now;
276
277 int retval = gettimeofday(&now, NULL);
278 if (retval)
279 return 0;
280
281 int64 time = TimeValToMicroseconds(now);
282
283 if (last_time_ == 0) {
284 // First call, just set the last values.
285 last_time_ = time;
286 last_cpu_ = GetProcessCPU(process_);
287 return 0;
288 }
289
290 int64 time_delta = time - last_time_;
291 DCHECK_NE(time_delta, 0);
292
293 if (time_delta == 0)
294 return 0;
295
296 int cpu = GetProcessCPU(process_);
297
298 last_time_ = time;
299 last_cpu_ = cpu;
300
301 double percentage = static_cast<double>((cpu * 100.0) / FSCALE);
302
303 return percentage;
304 }
305
306 size_t GetSystemCommitCharge() {
307 int mib[] = { CTL_VM, VM_METER };
308 int pagesize;
309 struct vmtotal vmtotal;
310 unsigned long mem_total, mem_free, mem_inactive;
311 size_t len = sizeof(vmtotal);
312
313 if (sysctl(mib, arraysize(mib), &vmtotal, &len, NULL, 0) < 0)
314 return 0;
315
316 mem_total = vmtotal.t_vm;
317 mem_free = vmtotal.t_free;
318 mem_inactive = vmtotal.t_vm - vmtotal.t_avm;
319
320 pagesize = getpagesize();
321
322 return mem_total - (mem_free*pagesize) - (mem_inactive*pagesize);
323 }
324
325 void EnableTerminationOnOutOfMemory() {
326 }
327
328 void EnableTerminationOnHeapCorruption() {
329 }
330
331 } // namespace base
OLDNEW
« no previous file with comments | « base/process_util.h ('k') | base/process_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698