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