| 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 #include <stddef.h> | 9 #include <stddef.h> |
| 10 #include <stdint.h> | 10 #include <stdint.h> |
| 11 #include <winternl.h> | 11 #include <winternl.h> |
| 12 | 12 |
| 13 #include <algorithm> | 13 #include <algorithm> |
| 14 | 14 |
| 15 #include "base/logging.h" | 15 #include "base/logging.h" |
| 16 #include "base/memory/ptr_util.h" |
| 16 #include "base/sys_info.h" | 17 #include "base/sys_info.h" |
| 17 | 18 |
| 18 namespace base { | 19 namespace base { |
| 19 namespace { | 20 namespace { |
| 20 | 21 |
| 21 // System pagesize. This value remains constant on x86/64 architectures. | 22 // System pagesize. This value remains constant on x86/64 architectures. |
| 22 const int PAGESIZE_KB = 4; | 23 const int PAGESIZE_KB = 4; |
| 23 | 24 |
| 24 typedef NTSTATUS(WINAPI* NTQUERYSYSTEMINFORMATION)( | 25 typedef NTSTATUS(WINAPI* NTQUERYSYSTEMINFORMATION)( |
| 25 SYSTEM_INFORMATION_CLASS SystemInformationClass, | 26 SYSTEM_INFORMATION_CLASS SystemInformationClass, |
| 26 PVOID SystemInformation, | 27 PVOID SystemInformation, |
| 27 ULONG SystemInformationLength, | 28 ULONG SystemInformationLength, |
| 28 PULONG ReturnLength); | 29 PULONG ReturnLength); |
| 29 | 30 |
| 30 } // namespace | 31 } // namespace |
| 31 | 32 |
| 32 SystemMemoryInfoKB::SystemMemoryInfoKB() { | 33 SystemMemoryInfoKB::SystemMemoryInfoKB() |
| 33 total = 0; | 34 : total(0), free(0), swap_total(0), swap_free(0) {} |
| 34 free = 0; | |
| 35 swap_total = 0; | |
| 36 swap_free = 0; | |
| 37 } | |
| 38 | 35 |
| 39 SystemMemoryInfoKB::SystemMemoryInfoKB(const SystemMemoryInfoKB& other) = | 36 SystemMemoryInfoKB::SystemMemoryInfoKB(const SystemMemoryInfoKB& other) = |
| 40 default; | 37 default; |
| 41 | 38 |
| 42 ProcessMetrics::~ProcessMetrics() { } | 39 ProcessMetrics::~ProcessMetrics() { } |
| 43 | 40 |
| 44 // static | 41 // static |
| 45 ProcessMetrics* ProcessMetrics::CreateProcessMetrics(ProcessHandle process) { | 42 std::unique_ptr<ProcessMetrics> ProcessMetrics::CreateProcessMetrics( |
| 46 return new ProcessMetrics(process); | 43 ProcessHandle process) { |
| 44 return WrapUnique(new ProcessMetrics(process)); |
| 47 } | 45 } |
| 48 | 46 |
| 49 size_t ProcessMetrics::GetPagefileUsage() const { | 47 size_t ProcessMetrics::GetPagefileUsage() const { |
| 50 PROCESS_MEMORY_COUNTERS pmc; | 48 PROCESS_MEMORY_COUNTERS pmc; |
| 51 if (GetProcessMemoryInfo(process_, &pmc, sizeof(pmc))) { | 49 if (GetProcessMemoryInfo(process_, &pmc, sizeof(pmc))) { |
| 52 return pmc.PagefileUsage; | 50 return pmc.PagefileUsage; |
| 53 } | 51 } |
| 54 return 0; | 52 return 0; |
| 55 } | 53 } |
| 56 | 54 |
| (...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 256 | 254 |
| 257 return static_cast<double>(system_time_delta * 100.0) / time_delta; | 255 return static_cast<double>(system_time_delta * 100.0) / time_delta; |
| 258 } | 256 } |
| 259 | 257 |
| 260 bool ProcessMetrics::GetIOCounters(IoCounters* io_counters) const { | 258 bool ProcessMetrics::GetIOCounters(IoCounters* io_counters) const { |
| 261 return GetProcessIoCounters(process_, io_counters) != FALSE; | 259 return GetProcessIoCounters(process_, io_counters) != FALSE; |
| 262 } | 260 } |
| 263 | 261 |
| 264 ProcessMetrics::ProcessMetrics(ProcessHandle process) | 262 ProcessMetrics::ProcessMetrics(ProcessHandle process) |
| 265 : process_(process), | 263 : process_(process), |
| 266 processor_count_(base::SysInfo::NumberOfProcessors()), | 264 processor_count_(SysInfo::NumberOfProcessors()), |
| 267 last_system_time_(0) { | 265 last_system_time_(0) {} |
| 268 } | |
| 269 | |
| 270 // GetPerformanceInfo is not available on WIN2K. So we'll | |
| 271 // load it on-the-fly. | |
| 272 const wchar_t kPsapiDllName[] = L"psapi.dll"; | |
| 273 typedef BOOL (WINAPI *GetPerformanceInfoFunction) ( | |
| 274 PPERFORMANCE_INFORMATION pPerformanceInformation, | |
| 275 DWORD cb); | |
| 276 | |
| 277 // Beware of races if called concurrently from multiple threads. | |
| 278 static BOOL InternalGetPerformanceInfo( | |
| 279 PPERFORMANCE_INFORMATION pPerformanceInformation, DWORD cb) { | |
| 280 static GetPerformanceInfoFunction GetPerformanceInfo_func = NULL; | |
| 281 if (!GetPerformanceInfo_func) { | |
| 282 HMODULE psapi_dll = ::GetModuleHandle(kPsapiDllName); | |
| 283 if (psapi_dll) | |
| 284 GetPerformanceInfo_func = reinterpret_cast<GetPerformanceInfoFunction>( | |
| 285 GetProcAddress(psapi_dll, "GetPerformanceInfo")); | |
| 286 | |
| 287 if (!GetPerformanceInfo_func) { | |
| 288 // The function could not be loaded! | |
| 289 memset(pPerformanceInformation, 0, cb); | |
| 290 return FALSE; | |
| 291 } | |
| 292 } | |
| 293 return GetPerformanceInfo_func(pPerformanceInformation, cb); | |
| 294 } | |
| 295 | 266 |
| 296 size_t GetSystemCommitCharge() { | 267 size_t GetSystemCommitCharge() { |
| 297 // Get the System Page Size. | 268 // Get the System Page Size. |
| 298 SYSTEM_INFO system_info; | 269 SYSTEM_INFO system_info; |
| 299 GetSystemInfo(&system_info); | 270 GetSystemInfo(&system_info); |
| 300 | 271 |
| 301 PERFORMANCE_INFORMATION info; | 272 PERFORMANCE_INFORMATION info; |
| 302 if (!InternalGetPerformanceInfo(&info, sizeof(info))) { | 273 if (!GetPerformanceInfo(&info, sizeof(info))) { |
| 303 DLOG(ERROR) << "Failed to fetch internal performance info."; | 274 DLOG(ERROR) << "Failed to fetch internal performance info."; |
| 304 return 0; | 275 return 0; |
| 305 } | 276 } |
| 306 return (info.CommitTotal * system_info.dwPageSize) / 1024; | 277 return (info.CommitTotal * system_info.dwPageSize) / 1024; |
| 307 } | 278 } |
| 308 | 279 |
| 309 size_t GetPageSize() { | 280 size_t GetPageSize() { |
| 310 return PAGESIZE_KB * 1024; | 281 return PAGESIZE_KB * 1024; |
| 311 } | 282 } |
| 312 | 283 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 324 | 295 |
| 325 meminfo->total = mem_status.ullTotalPhys / 1024; | 296 meminfo->total = mem_status.ullTotalPhys / 1024; |
| 326 meminfo->free = mem_status.ullAvailPhys / 1024; | 297 meminfo->free = mem_status.ullAvailPhys / 1024; |
| 327 meminfo->swap_total = mem_status.ullTotalPageFile / 1024; | 298 meminfo->swap_total = mem_status.ullTotalPageFile / 1024; |
| 328 meminfo->swap_free = mem_status.ullAvailPageFile / 1024; | 299 meminfo->swap_free = mem_status.ullAvailPageFile / 1024; |
| 329 | 300 |
| 330 return true; | 301 return true; |
| 331 } | 302 } |
| 332 | 303 |
| 333 } // namespace base | 304 } // namespace base |
| OLD | NEW |