Chromium Code Reviews| Index: chrome/browser/extensions/api/system_info_cpu/cpu_info_provider_win.cc |
| diff --git a/chrome/browser/extensions/api/system_info_cpu/cpu_info_provider_win.cc b/chrome/browser/extensions/api/system_info_cpu/cpu_info_provider_win.cc |
| index 7a9e086cb03bbfcc4786c00908242fefc30f12ca..b886c648979d13fbf4ab720b0b2d17fb8d4c5ff7 100644 |
| --- a/chrome/browser/extensions/api/system_info_cpu/cpu_info_provider_win.cc |
| +++ b/chrome/browser/extensions/api/system_info_cpu/cpu_info_provider_win.cc |
| @@ -4,11 +4,71 @@ |
| #include "chrome/browser/extensions/api/system_info_cpu/cpu_info_provider.h" |
| +#include <windows.h> |
| +#include <winternl.h> |
| + |
| +#include "base/sys_info.h" |
| + |
| namespace extensions { |
| +namespace { |
| + |
| +const wchar_t kNtdll[] = L"ntdll.dll"; |
| +const char kNtQuerySystemInformationName[] = "NtQuerySystemInformation"; |
| + |
| +// See MSDN about NtQuerySystemInformation definition. |
| +typedef DWORD (WINAPI *NtQuerySystemInformationPF)(DWORD system_info_class, |
| + PVOID system_info, |
| + ULONG system_info_length, |
| + PULONG return_length); |
| + |
| +} // namespace |
| + |
| bool CpuInfoProvider::QueryCpuTimePerProcessor(std::vector<CpuTime>* times) { |
| - // TODO(hongbo): use NtQuerySystemInformation to query the cpu time. |
| - return false; |
| + if (!times) |
| + return false; |
| + |
| + std::vector<CpuTime> results; |
| + |
| + HMODULE ntdll = GetModuleHandle(kNtdll); |
| + CHECK(ntdll != NULL); |
| + NtQuerySystemInformationPF NtQuerySystemInformation = |
| + reinterpret_cast<NtQuerySystemInformationPF>( |
| + ::GetProcAddress(ntdll, kNtQuerySystemInformationName)); |
| + |
| + CHECK(NtQuerySystemInformation != NULL); |
| + if (!NtQuerySystemInformation) |
|
alexeypa (please no reviews)
2012/09/13 16:45:48
nit: remove the if statement. CHECK will crash the
Hongbo Min
2012/09/14 02:56:40
Done.
|
| + return false; |
| + |
| + int num_of_processors = base::SysInfo::NumberOfProcessors(); |
| + scoped_array<SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION> processor_info( |
| + new SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION[num_of_processors]); |
| + DCHECK_GT(num_of_processors, 0); |
| + |
| + ULONG bytes = 0, returned_bytes = 0; |
|
alexeypa (please no reviews)
2012/09/13 16:45:48
nit: Sorry, what I mean is:
ULONG bytes = sizeof(
|
| + bytes = sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION) * num_of_processors; |
| + if (!NT_SUCCESS(NtQuerySystemInformation( |
| + SystemProcessorPerformanceInformation, |
| + processor_info.get(), bytes, &returned_bytes))) |
| + return false; |
| + |
| + int returned_num_of_processors = |
|
alexeypa (please no reviews)
2012/09/13 16:45:48
nit: The type should be ULONG since |returned_byte
Hongbo Min
2012/09/14 02:56:40
Yes, but because the num_of_processors is int type
|
| + returned_bytes / sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION); |
| + |
| + if (returned_num_of_processors != num_of_processors) |
| + return false; |
| + |
| + results.reserve(returned_num_of_processors); |
| + for (int i = 0; i < returned_num_of_processors; ++i) { |
| + CpuTime time; |
| + time.kernel = processor_info[i].KernelTime.QuadPart; |
| + time.user = processor_info[i].UserTime.QuadPart; |
| + time.idle = processor_info[i].IdleTime.QuadPart; |
| + results.push_back(time); |
| + } |
| + |
| + results.swap(*times); |
|
alexeypa (please no reviews)
2012/09/13 16:45:48
nit: This probably is more readable if it says tim
Hongbo Min
2012/09/14 02:56:40
Done.
|
| + return true; |
| } |
| } // namespace extensions |