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