| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/extensions/api/system_info_cpu/cpu_info_provider.h" | 5 #include "chrome/browser/extensions/api/system_info_cpu/cpu_info_provider.h" |
| 6 | 6 |
| 7 #include <windows.h> |
| 8 #include <winternl.h> |
| 9 |
| 10 #include "base/sys_info.h" |
| 11 |
| 7 namespace extensions { | 12 namespace extensions { |
| 8 | 13 |
| 14 namespace { |
| 15 |
| 16 const wchar_t kNtdll[] = L"ntdll.dll"; |
| 17 const char kNtQuerySystemInformationName[] = "NtQuerySystemInformation"; |
| 18 |
| 19 // See MSDN about NtQuerySystemInformation definition. |
| 20 typedef DWORD (WINAPI *NtQuerySystemInformationPF)(DWORD system_info_class, |
| 21 PVOID system_info, |
| 22 ULONG system_info_length, |
| 23 PULONG return_length); |
| 24 |
| 25 } // namespace |
| 26 |
| 9 bool CpuInfoProvider::QueryCpuTimePerProcessor(std::vector<CpuTime>* times) { | 27 bool CpuInfoProvider::QueryCpuTimePerProcessor(std::vector<CpuTime>* times) { |
| 10 // TODO(hongbo): use NtQuerySystemInformation to query the cpu time. | 28 std::vector<CpuTime> results; |
| 11 return false; | 29 |
| 30 HMODULE ntdll = GetModuleHandle(kNtdll); |
| 31 CHECK(ntdll != NULL); |
| 32 NtQuerySystemInformationPF NtQuerySystemInformation = |
| 33 reinterpret_cast<NtQuerySystemInformationPF>( |
| 34 ::GetProcAddress(ntdll, kNtQuerySystemInformationName)); |
| 35 |
| 36 CHECK(NtQuerySystemInformation != NULL); |
| 37 |
| 38 int num_of_processors = base::SysInfo::NumberOfProcessors(); |
| 39 scoped_array<SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION> processor_info( |
| 40 new SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION[num_of_processors]); |
| 41 |
| 42 ULONG returned_bytes = 0, bytes = |
| 43 sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION) * num_of_processors; |
| 44 if (!NT_SUCCESS(NtQuerySystemInformation( |
| 45 SystemProcessorPerformanceInformation, |
| 46 processor_info.get(), bytes, &returned_bytes))) |
| 47 return false; |
| 48 |
| 49 int returned_num_of_processors = |
| 50 returned_bytes / sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION); |
| 51 |
| 52 if (returned_num_of_processors != num_of_processors) |
| 53 return false; |
| 54 |
| 55 results.reserve(returned_num_of_processors); |
| 56 for (int i = 0; i < returned_num_of_processors; ++i) { |
| 57 CpuTime time; |
| 58 time.kernel = processor_info[i].KernelTime.QuadPart; |
| 59 time.user = processor_info[i].UserTime.QuadPart; |
| 60 time.idle = processor_info[i].IdleTime.QuadPart; |
| 61 results.push_back(time); |
| 62 } |
| 63 |
| 64 times->swap(results); |
| 65 return true; |
| 12 } | 66 } |
| 13 | 67 |
| 14 } // namespace extensions | 68 } // namespace extensions |
| OLD | NEW |