OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/process/process_metrics.h" | 5 #include "base/process/process_metrics.h" |
6 | 6 |
7 #include <windows.h> | 7 #include <windows.h> |
8 #include <psapi.h> | 8 #include <psapi.h> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 } | 188 } |
189 | 189 |
190 static uint64 FileTimeToUTC(const FILETIME& ftime) { | 190 static uint64 FileTimeToUTC(const FILETIME& ftime) { |
191 LARGE_INTEGER li; | 191 LARGE_INTEGER li; |
192 li.LowPart = ftime.dwLowDateTime; | 192 li.LowPart = ftime.dwLowDateTime; |
193 li.HighPart = ftime.dwHighDateTime; | 193 li.HighPart = ftime.dwHighDateTime; |
194 return li.QuadPart; | 194 return li.QuadPart; |
195 } | 195 } |
196 | 196 |
197 double ProcessMetrics::GetCPUUsage() { | 197 double ProcessMetrics::GetCPUUsage() { |
198 FILETIME now; | |
199 FILETIME creation_time; | 198 FILETIME creation_time; |
200 FILETIME exit_time; | 199 FILETIME exit_time; |
201 FILETIME kernel_time; | 200 FILETIME kernel_time; |
202 FILETIME user_time; | 201 FILETIME user_time; |
203 | 202 |
204 GetSystemTimeAsFileTime(&now); | |
205 | |
206 if (!GetProcessTimes(process_, &creation_time, &exit_time, | 203 if (!GetProcessTimes(process_, &creation_time, &exit_time, |
207 &kernel_time, &user_time)) { | 204 &kernel_time, &user_time)) { |
208 // We don't assert here because in some cases (such as in the Task Manager) | 205 // We don't assert here because in some cases (such as in the Task Manager) |
209 // we may call this function on a process that has just exited but we have | 206 // we may call this function on a process that has just exited but we have |
210 // not yet received the notification. | 207 // not yet received the notification. |
211 return 0; | 208 return 0; |
212 } | 209 } |
213 int64 system_time = (FileTimeToUTC(kernel_time) + FileTimeToUTC(user_time)) / | 210 int64 system_time = (FileTimeToUTC(kernel_time) + FileTimeToUTC(user_time)) / |
214 processor_count_; | 211 processor_count_; |
215 int64 time = FileTimeToUTC(now); | 212 TimeTicks time = TimeTicks::Now(); |
216 | 213 |
217 if ((last_system_time_ == 0) || (last_cpu_time_ == 0)) { | 214 if (last_system_time_ == 0) { |
218 // First call, just set the last values. | 215 // First call, just set the last values. |
219 last_system_time_ = system_time; | 216 last_system_time_ = system_time; |
220 last_cpu_time_ = time; | 217 last_cpu_time_ = time; |
221 return 0; | 218 return 0; |
222 } | 219 } |
223 | 220 |
224 int64 system_time_delta = system_time - last_system_time_; | 221 int64 system_time_delta = system_time - last_system_time_; |
225 int64 time_delta = time - last_cpu_time_; | 222 // FILETIME is in 100-nanosecond units, so this needs microseconds times 10. |
| 223 int64 time_delta = (time - last_cpu_time_).InMicroseconds() * 10; |
226 DCHECK_NE(0U, time_delta); | 224 DCHECK_NE(0U, time_delta); |
227 if (time_delta == 0) | 225 if (time_delta == 0) |
228 return 0; | 226 return 0; |
229 | 227 |
230 // We add time_delta / 2 so the result is rounded. | 228 // We add time_delta / 2 so the result is rounded. |
231 int cpu = static_cast<int>((system_time_delta * 100 + time_delta / 2) / | 229 int cpu = static_cast<int>((system_time_delta * 100 + time_delta / 2) / |
232 time_delta); | 230 time_delta); |
233 | 231 |
234 last_system_time_ = system_time; | 232 last_system_time_ = system_time; |
235 last_cpu_time_ = time; | 233 last_cpu_time_ = time; |
(...skipping 26 matching lines...) Expand all Loading... |
262 return true; | 260 return true; |
263 } | 261 } |
264 | 262 |
265 bool ProcessMetrics::GetIOCounters(IoCounters* io_counters) const { | 263 bool ProcessMetrics::GetIOCounters(IoCounters* io_counters) const { |
266 return GetProcessIoCounters(process_, io_counters) != FALSE; | 264 return GetProcessIoCounters(process_, io_counters) != FALSE; |
267 } | 265 } |
268 | 266 |
269 ProcessMetrics::ProcessMetrics(ProcessHandle process) | 267 ProcessMetrics::ProcessMetrics(ProcessHandle process) |
270 : process_(process), | 268 : process_(process), |
271 processor_count_(base::SysInfo::NumberOfProcessors()), | 269 processor_count_(base::SysInfo::NumberOfProcessors()), |
272 last_cpu_time_(0), | |
273 last_system_time_(0) { | 270 last_system_time_(0) { |
274 } | 271 } |
275 | 272 |
276 // GetPerformanceInfo is not available on WIN2K. So we'll | 273 // GetPerformanceInfo is not available on WIN2K. So we'll |
277 // load it on-the-fly. | 274 // load it on-the-fly. |
278 const wchar_t kPsapiDllName[] = L"psapi.dll"; | 275 const wchar_t kPsapiDllName[] = L"psapi.dll"; |
279 typedef BOOL (WINAPI *GetPerformanceInfoFunction) ( | 276 typedef BOOL (WINAPI *GetPerformanceInfoFunction) ( |
280 PPERFORMANCE_INFORMATION pPerformanceInformation, | 277 PPERFORMANCE_INFORMATION pPerformanceInformation, |
281 DWORD cb); | 278 DWORD cb); |
282 | 279 |
(...skipping 23 matching lines...) Expand all Loading... |
306 | 303 |
307 PERFORMANCE_INFORMATION info; | 304 PERFORMANCE_INFORMATION info; |
308 if (!InternalGetPerformanceInfo(&info, sizeof(info))) { | 305 if (!InternalGetPerformanceInfo(&info, sizeof(info))) { |
309 DLOG(ERROR) << "Failed to fetch internal performance info."; | 306 DLOG(ERROR) << "Failed to fetch internal performance info."; |
310 return 0; | 307 return 0; |
311 } | 308 } |
312 return (info.CommitTotal * system_info.dwPageSize) / 1024; | 309 return (info.CommitTotal * system_info.dwPageSize) / 1024; |
313 } | 310 } |
314 | 311 |
315 } // namespace base | 312 } // namespace base |
OLD | NEW |